Fix typo -- spurious "size" parameter to odr_reset()
[yaz-moved-to-github.git] / doc / odr.xml
1  <chapter id="odr"><title>The ODR Module</title>
2   
3   <sect1 id="odr.introduction"><title>Introduction</title>
4
5    <para>
6      &odr; is the BER-encoding/decoding subsystem of &yaz;. Care as been taken
7     to isolate &odr; from the rest of the package - specifically from the
8     transport interface. &odr; may be used in any context where basic
9     ASN.1/BER representations are used.
10    </para>
11
12    <para>
13     If you are only interested in writing a Z39.50 implementation based on
14     the PDUs that are already provided with &yaz;, you only need to concern
15     yourself with the section on managing ODR streams
16     (<xref linkend="odr.use"/>). Only if you need to
17     implement ASN.1 beyond that which has been provided, should you
18     worry about the second half of the documentation
19     (<xref linkend="odr.programming"/>).
20     If you use one of the higher-level interfaces, you can skip this
21     section entirely.
22    </para>
23
24    <para>
25     This is important, so we'll repeat it for emphasis: <emphasis>You do
26      not need to read <xref linkend="odr.programming"/>
27      to implement Z39.50 with &yaz;.</emphasis>
28    </para>
29
30    <para>
31     If you need a part of the protocol that isn't already in &yaz;, you
32     should contact the authors before going to work on it yourself: We
33     might already be working on it. Conversely, if you implement a useful
34     part of the protocol before us, we'd be happy to include it in a
35     future release.
36    </para>
37
38   </sect1>
39   <sect1 id="odr.use"><title>Using ODR</title>
40
41    <sect2 id="odr.streams"><title>ODR Streams</title>
42
43     <para>
44      Conceptually, the ODR stream is the source of encoded data in the
45      decoding mode; when encoding, it is the receptacle for the encoded
46      data. Before you can use an ODR stream it must be allocated. This is
47      done with the function
48     </para>
49
50     <synopsis>
51      ODR odr_createmem(int direction);
52     </synopsis>
53
54     <para>
55      The <function>odr_createmem()</function> function takes as argument one
56      of three manifest constants: <literal>ODR_ENCODE</literal>,
57      <literal>ODR_DECODE</literal>, or <literal>ODR_PRINT</literal>.
58      An &odr; stream can be in only one mode - it is not possible to change
59      its mode once it's selected. Typically, your program will allocate
60      at least two ODR streams - one for decoding, and one for encoding.
61     </para>
62
63     <para>
64      When you're done with the stream, you can use
65     </para>
66
67     <synopsis>
68      void odr_destroy(ODR o);
69     </synopsis>
70
71     <para>
72      to release the resources allocated for the stream.
73     </para>
74    </sect2>
75
76    <sect2 id="odr.memory.management"><title id="memory">Memory Management</title>
77
78     <para>
79      Two forms of memory management take place in the &odr; system. The first
80      one, which has to do with allocating little bits of memory (sometimes
81      quite large bits of memory, actually) when a protocol package is
82      decoded, and turned into a complex of interlinked structures. This
83      section deals with this system, and how you can use it for your own
84      purposes. The next section deals with the memory management which is
85      required when encoding data - to make sure that a large enough buffer is
86      available to hold the fully encoded PDU.
87     </para>
88
89     <para>
90      The &odr; module has its own memory management system, which is
91      used whenever memory is required. Specifically, it is used to allocate
92      space for data when decoding incoming PDUs. You can use the memory
93      system for your own purposes, by using the function
94     </para>
95
96     <synopsis>
97      void *odr_malloc(ODR o, int size);
98     </synopsis>
99
100     <para>
101      You can't use the normal <function>free(2)</function> routine to free
102      memory allocated by this function, and &odr; doesn't provide a parallel
103      function. Instead, you can call
104     </para>
105
106     <synopsis>
107      void odr_reset(ODR o);
108     </synopsis>
109
110     <para>
111      when you are done with the
112      memory: Everything allocated since the last call to
113      <function>odr_reset()</function> is released.
114      The <function>odr_reset()</function> call is also required to clear
115      up an error condition on a stream.
116     </para>
117
118     <para>
119      The function
120     </para>
121
122     <synopsis>
123      int odr_total(ODR o);
124     </synopsis>
125
126     <para>
127      returns the number of bytes allocated on the stream since the last call to
128      <function>odr_reset()</function>.
129     </para>
130
131     <para>
132      The memory subsystem of &odr; is fairly efficient at allocating and
133      releasing little bits of memory. Rather than managing the individual,
134      small bits of space, the system maintains a free-list of larger chunks
135      of memory, which are handed out in small bits. This scheme is
136      generally known as a <emphasis>nibble memory</emphasis> system.
137      It is very useful for maintaining short-lived constructions such
138      as protocol PDUs.
139     </para>
140
141     <para>
142      If you want to retain a bit of memory beyond the next call to
143      <function>odr_reset()</function>, you can use the function
144     </para>
145
146     <synopsis>
147      ODR_MEM odr_extract_mem(ODR o);
148     </synopsis>
149
150     <para>
151      This function will give you control of the memory recently allocated
152      on the ODR stream. The memory will live (past calls to
153      <function>odr_reset()</function>), until you call the function
154     </para>
155
156     <synopsis>
157      void odr_release_mem(ODR_MEM p);
158     </synopsis>
159
160     <para>
161      The opaque <literal>ODR_MEM</literal> handle has no other purpose than
162      referencing the memory block for you until you want to release it.
163     </para>
164
165     <para>
166      You can use <function>odr_extract_mem()</function> repeatedly between
167      allocating data, to retain individual control of separate chunks of data.
168     </para>
169
170    </sect2>
171    <sect2 id="odr.encoding.and.decoding"><title>Encoding and Decoding Data</title>
172
173     <para>
174      When encoding data, the ODR stream will write the encoded octet string
175      in an internal buffer. To retrieve the data, use the function
176     </para>
177
178     <synopsis>
179      char *odr_getbuf(ODR o, int *len, int *size);
180     </synopsis>
181
182     <para>
183      The integer pointed to by len is set to the length of the encoded
184      data, and a pointer to that data is returned. <literal>*size</literal>
185      is set to the size of the buffer (unless <literal>size</literal> is null,
186      signaling that you are not interested in the size). The next call to
187      a primitive function using the same &odr; stream will overwrite the
188      data, unless a different buffer has been supplied using the call
189     </para>
190
191     <synopsis>
192      void odr_setbuf(ODR o, char *buf, int len, int can_grow);
193     </synopsis>
194
195     <para>
196      which sets the encoding (or decoding) buffer used by
197      <literal>o</literal> to <literal>buf</literal>, using the length
198      <literal>len</literal>.
199      Before a call to an encoding function, you can use
200      <function>odr_setbuf()</function> to provide the stream with an encoding
201      buffer of sufficient size (length). The <literal>can_grow</literal>
202      parameter tells the encoding &odr; stream whether it is allowed to use
203      <function>realloc(2)</function> to increase the size of the buffer when
204      necessary. The default condition of a new encoding stream is equivalent
205      to the results of calling
206     </para>
207
208     <synopsis>
209      odr_setbuf(stream, 0, 0, 1);
210     </synopsis>
211
212     <para>
213      In this case, the stream will allocate and reallocate memory as
214      necessary. The stream reallocates memory by repeatedly doubling the
215      size of the buffer - the result is that the buffer will typically
216      reach its maximum, working size with only a small number of reallocation
217      operations. The memory is freed by the stream when the latter is destroyed,
218      unless it was assigned by the user with the <literal>can_grow</literal>
219      parameter set to zero (in this case, you are expected to retain
220      control of the memory yourself).
221     </para>
222
223     <para>
224      To assume full control of an encoded buffer, you must first call
225      <function>odr_getbuf()</function> to fetch the buffer and its length.
226      Next, you should call <function>odr_setbuf()</function> to provide a
227      different buffer (or a null pointer) to the stream. In the simplest
228      case, you will reuse the same buffer over and over again, and you
229      will just need to call <function>odr_getbuf()</function> after each
230      encoding operation to get the length and address of the buffer.
231      Note that the stream may reallocate the buffer during an encoding
232      operation, so it is necessary to retrieve the correct address after
233      each encoding operation.
234     </para>
235
236     <para>
237      It is important to realize that the ODR stream will not release this
238      memory when you call <function>odr_reset()</function>: It will
239      merely update its internal pointers to prepare for the encoding of a
240      new data value.
241      When the stream is released by the <function>odr_destroy()</function>
242      function, the memory given to it by <function>odr_setbuf</function> will
243      be released <emphasis>only</emphasis> if the <literal>can_grow</literal>
244      parameter to <function>odr_setbuf()</function> was nonzero. The
245      <literal>can_grow</literal> parameter, in other words, is a way of
246      signaling who is to own the buffer, you or the ODR stream. If you never call
247      <function>odr_setbuf()</function> on your encoding stream, which is
248      typically the case, the buffer allocated by the stream will belong to
249      the stream by default.
250     </para>
251
252     <para>
253      When you wish to decode data, you should first call
254      <function>odr_setbuf()</function>, to tell the decoding stream
255      where to find the encoded data, and how long the buffer is
256      (the <literal>can_grow</literal> parameter is ignored by a decoding
257      stream). After this, you can call the function corresponding to the
258      data you wish to decode (eg, <function>odr_integer()</function> odr
259      <function>z_APDU()</function>).
260     </para>
261     
262     <example id="example.odr.encoding.and.decoding.functions">
263      <title>Encoding and decoding functions</title>
264      <synopsis>
265       int odr_integer(ODR o, int **p, int optional, const char *name);
266       
267       int z_APDU(ODR o, Z_APDU **p, int optional, const char *name);
268      </synopsis>
269     </example>
270
271     <para>
272      If the data is absent (or doesn't match the tag corresponding to
273      the type), the return value will be either 0 or 1 depending on the
274      <literal>optional</literal> flag. If <literal>optional</literal>
275      is 0 and the data is absent, an error flag will be raised in the
276      stream, and you'll need to call <function>odr_reset()</function> before
277      you can use the stream again. If <literal>optional</literal> is
278      nonzero, the pointer <emphasis>pointed</emphasis> to/ by
279      <literal>p</literal> will be set to the null value, and the function
280      will return 1.
281      The <literal>name</literal> argument is used to pretty-print the
282      tag in question. It may be set to <literal>NULL</literal> if
283      pretty-printing is not desired.
284     </para>
285
286     <para>
287      If the data value is found where it's expected, the pointer
288      <emphasis>pointed to</emphasis> by the <literal>p</literal> argument
289      will be set to point to the decoded type.
290      The space for the type will be allocated and owned by the &odr;
291      stream, and it will live until you call
292      <function>odr_reset()</function> on the stream. You cannot use
293      <function>free(2)</function> to release the memory.
294      You can decode several data elements (by repeated calls to
295      <function>odr_setbuf()</function> and your decoding function), and
296      new memory will be allocated each time. When you do call 
297      <function>odr_reset()</function>, everything decoded since the
298      last call to <function>odr_reset()</function> will be released.
299     </para>
300
301     <example id="example.odr.encoding.of.integer">
302      <title>Encoding and decoding of an integer</title>
303      <para>
304       The use of the double indirection can be a little confusing at first
305       (its purpose will become clear later on, hopefully),
306       so an example is in order. We'll encode an integer value, and
307       immediately decode it again using a different stream. A useless, but
308       informative operation.
309      </para>
310      <programlisting><![CDATA[
311 void do_nothing_useful(int value)
312 {
313     ODR encode, decode;
314     int *valp, *resvalp;
315     char *bufferp;
316     int len;
317      
318     /* allocate streams */
319     if (!(encode = odr_createmem(ODR_ENCODE)))
320         return;
321     if (!(decode = odr_createmem(ODR_DECODE)))
322         return;
323
324     valp = &amp;value;
325     if (odr_integer(encode, &amp;valp, 0, 0) == 0)
326     {
327         printf("encoding went bad\n");
328         return;
329     }
330     bufferp = odr_getbuf(encode, &amp;len);
331     printf("length of encoded data is &percnt;d\n", len);
332
333     /* now let's decode the thing again */
334     odr_setbuf(decode, bufferp, len);
335     if (odr_integer(decode, &amp;resvalp, 0, 0) == 0)
336     {
337         printf("decoding went bad\n");
338         return;
339     }
340     printf("the value is &percnt;d\n", *resvalp);
341
342     /* clean up */
343     odr_destroy(encode);
344     odr_destroy(decode);
345 }
346 ]]>
347      </programlisting>
348      <para>
349       This looks like a lot of work, offhand. In practice, the &odr; streams
350       will typically be allocated once, in the beginning of your program
351       (or at the beginning of a new network session), and the encoding
352       and decoding will only take place in a few, isolated places in your
353       program, so the overhead is quite manageable.
354      </para>
355     </example>
356     
357    </sect2>
358
359    <sect2 id="odr.printing"><title>Printing</title>
360     <para>
361      When an ODR stream is created of type <literal>ODR_PRINT</literal>
362      the ODR module will print the contents of a PDU in a readable format.
363      By default output is written to the <literal>stderr</literal> stream.
364      This behavior can be changed, however, by calling the function
365      <synopsis>
366       odr_setprint(ODR o, FILE *file);
367      </synopsis>
368      before encoders or decoders are being invoked.
369      It is also possible to direct the output to a buffer (of indeed
370      another file), by using the more generic mechanism:
371      <synopsis>
372       void odr_set_stream(ODR o, void *handle,
373                          void (*stream_write)(ODR o, void *handle, int type,
374                                               const char *buf, int len),
375                          void (*stream_close)(void *handle));
376      </synopsis>
377      Here the user provides an opaque handle and two handlers,
378      <replaceable>stream_write</replaceable> for writing,
379      and <replaceable>stream_close</replaceable> which is supposed
380      to close/free resources associated with handle. 
381      The <replaceable>stream_close</replaceable> handler is optional and
382      if NULL for the function is provided, it will not be invoked.
383      The <replaceable>stream_write</replaceable> takes the ODR handle
384      as parameter, the user defined handle, a type 
385      <literal>ODR_OCTETSTRING</literal>, <literal>ODR_VISIBLESTRING</literal>
386      which indicates the type of contents is being written.
387     </para>
388     <para>
389      Another utility useful for diagnostics (error handling) or as
390      part of the printing facilities is:
391      <synopsis>
392       const char **odr_get_element_path(ODR o);
393      </synopsis>
394      which returns a list of current elements that ODR deals with at the 
395      moment. For the returned array, say <literal>ar</literal>, 
396      <literal>ar[0]</literal> is the top level element,
397      <literal>ar[n]</literal> is the last. The last element has the
398      property that <literal>ar[n+1] == NULL</literal>.
399     </para>
400     <example id="example.odr.element.path.record">
401      <title>Element Path for record</title>
402      <para>
403       For a database record part of a PresentResponse the
404       array returned by <function>odr_get_element</function>
405       is <literal>presentResponse</literal>, <literal>databaseOrSurDiagnostics</literal>, <literal>?</literal>, <literal>record</literal>, <literal>?</literal>, <literal>databaseRecord</literal> . The question mark appears due to 
406       unnamed constructions.
407      </para>
408      </example>
409    </sect2>
410    <sect2 id="odr.diagnostics"><title>Diagnostics</title>
411
412     <para>
413      The encoding/decoding functions all return 0 when an error occurs.
414      Until you call <function>odr_reset()</function>, you cannot use the
415      stream again, and any function called will immediately return 0.
416     </para>
417
418     <para>
419      To provide information to the programmer or administrator, the function
420     </para>
421
422     <synopsis>
423      void odr_perror(ODR o, char *message);
424     </synopsis>
425
426     <para>
427      is provided, which prints the <literal>message</literal> argument to
428      <literal>stderr</literal> along with an error message from the stream.
429     </para>
430
431     <para>
432      You can also use the function
433     </para>
434
435     <synopsis>
436      int odr_geterror(ODR o);
437     </synopsis>
438
439     <para>
440      to get the current error number from the screen. The number will be
441      one of these constants:
442     </para>
443
444     <table frame="top" id="odr.error.codes">
445      <title>ODR Error codes</title>
446      <tgroup cols="2">
447       <thead>
448        <row>
449         <entry>code</entry>
450         <entry>Description</entry>
451        </row>
452       </thead>
453       <tbody>
454        <row>
455         <entry>OMEMORY</entry><entry>Memory allocation failed.</entry>
456        </row>
457
458        <row>
459         <entry>OSYSERR</entry><entry>A system- or library call has failed.
460          The standard diagnostic variable <literal>errno</literal> should be
461          examined to determine the actual error.</entry>
462        </row>
463
464        <row>
465         <entry>OSPACE</entry><entry>No more space for encoding.
466          This will only occur when the user has explicitly provided a
467          buffer for an encoding stream without allowing the system to
468          allocate more space.</entry>
469        </row>
470
471        <row>
472         <entry>OREQUIRED</entry><entry>This is a common protocol error; A
473          required data element was missing during encoding or decoding.</entry>
474        </row>
475
476        <row>
477         <entry>OUNEXPECTED</entry><entry>An unexpected data element was
478          found during decoding.</entry>
479        </row>
480
481        <row><entry>OOTHER</entry><entry>Other error. This is typically an
482          indication of misuse of the &odr; system by the programmer, and also
483          that the diagnostic system isn't as good as it should be, yet.</entry>
484        </row>
485       </tbody>
486      </tgroup>
487     </table>
488
489     <para>
490      The character string array
491     </para>
492
493     <synopsis>
494      char *odr_errlist[]
495     </synopsis>
496
497     <para>
498      can be indexed by the error code to obtain a human-readable
499      representation of the problem.
500     </para>
501
502    </sect2>
503    <sect2 id="odr.summary.and.synopsis">
504     <title>Summary and Synopsis</title>
505
506     <synopsis>
507      #include &lt;odr.h>
508
509      ODR odr_createmem(int direction);
510
511      void odr_destroy(ODR o);
512
513      void odr_reset(ODR o);
514
515      char *odr_getbuf(ODR o, int *len);
516
517      void odr_setbuf(ODR o, char *buf, int len);
518
519      void *odr_malloc(ODR o, int size);
520
521      ODR_MEM odr_extract_mem(ODR o);
522
523      void odr_release_mem(ODR_MEM r);
524
525      int odr_geterror(ODR o);
526
527      void odr_perror(char *message);
528
529      extern char *odr_errlist[];
530     </synopsis>
531
532    </sect2>
533   </sect1>
534
535   <sect1 id="odr.programming"><title>Programming with ODR</title>
536
537    <para>
538     The API of &odr; is designed to reflect the structure of ASN.1, rather
539     than BER itself. Future releases may be able to represent data in
540     other external forms.
541    </para>
542
543    <tip>
544     <para>
545      There is an ASN.1 tutorial available at
546      <ulink url="&url.asn.1.tutorial;">this site</ulink>.
547      This site also has standards for ASN.1 (X.680) and BER (X.690) 
548      <ulink url="&url.asn.1.standards;">online</ulink>.
549     </para>
550    </tip>
551    
552    <para>
553     The ODR interface is based loosely on that of the Sun Microsystems
554     XDR routines.
555     Specifically, each function which corresponds to an ASN.1 primitive
556     type has a dual function. Depending on the settings of the ODR
557     stream which is supplied as a parameter, the function may be used
558     either to encode or decode data. The functions that can be built
559     using these primitive functions, to represent more complex data types,
560     share this quality. The result is that you only have to enter the
561     definition for a type once - and you have the functionality of encoding,
562     decoding (and pretty-printing) all in one unit.
563     The resulting C source code is quite compact, and is a pretty
564     straightforward representation of the source ASN.1 specification. 
565    </para>
566    
567    <para>
568     In many cases, the model of the XDR functions works quite well in this
569     role.
570     In others, it is less elegant. Most of the hassle comes from the optional
571     SEQUENCE members which don't exist in XDR.
572    </para>
573
574    <sect2 id="odr.primitive.asn1.types">
575     <title>The Primitive ASN.1 Types</title>
576
577     <para>
578      ASN.1 defines a number of primitive types (many of which correspond
579      roughly to primitive types in structured programming languages, such as C).
580     </para>
581
582     <sect3 id="odr.integer"><title>INTEGER</title>
583
584      <para>
585       The &odr; function for encoding or decoding (or printing) the ASN.1
586       INTEGER type looks like this:
587      </para>
588
589      <synopsis>
590       int odr_integer(ODR o, int **p, int optional, const char *name);
591      </synopsis>
592
593      <para>
594       (we don't allow values that can't be contained in a C integer.)
595      </para>
596      
597      <para>
598       This form is typical of the primitive &odr; functions. They are named
599       after the type of data that they encode or decode. They take an &odr;
600       stream, an indirect reference to the type in question, and an
601       <literal>optional</literal> flag (corresponding to the OPTIONAL keyword
602       of ASN.1) as parameters. They all return an integer value of either one
603       or zero.
604       When you use the primitive functions to construct encoders for complex
605       types of your own, you should follow this model as well. This
606       ensures that your new types can be reused as elements in yet more
607       complex types.
608      </para>
609
610      <para>
611       The <literal>o</literal> parameter should obviously refer to a properly
612       initialized &odr; stream of the right type (encoding/decoding/printing)
613       for the operation that you wish to perform.
614      </para>
615
616      <para>
617       When encoding or printing, the function first looks at
618       <literal>* p</literal>. If <literal>* p</literal> (the pointer pointed
619       to by <literal>p</literal>) is a null pointer, this is taken to mean that
620       the data element is absent. If the <literal>optional</literal> parameter
621       is nonzero, the function will return one (signifying success) without
622       any further processing. If the <literal>optional</literal> is zero, an
623       internal error flag is set in the &odr; stream, and the function will
624       return 0. No further operations can be carried out on the stream without
625       a call to the function <function>odr_reset()</function>.
626      </para>
627
628      <para>
629       If <literal>*p</literal> is not a null pointer, it is expected to
630       point to an instance of the data type. The data will be subjected to
631       the encoding rules, and the result will be placed in the buffer held
632       by the &odr; stream.
633      </para>
634
635      <para>
636       The other ASN.1 primitives have similar functions that operate in
637       similar manners:
638      </para>
639     </sect3>
640     <sect3 id="odr.boolean"><title>BOOLEAN</title>
641
642      <synopsis>
643 int odr_bool(ODR o, bool_t **p, int optional, const char *name);
644      </synopsis>
645
646     </sect3>
647     <sect3 id="odr.real"><title>REAL</title>
648
649      <para>
650       Not defined.
651      </para>
652
653     </sect3>
654     <sect3 id="odr.null"><title>NULL</title>
655
656      <synopsis>
657 int odr_null(ODR o, bool_t **p, int optional, const char *name);
658      </synopsis>
659
660      <para>
661       In this case, the value of **p is not important. If <literal>*p</literal>
662       is different from the null pointer, the null value is present, otherwise
663       it's absent.
664      </para>
665
666     </sect3>
667     <sect3 id="odr.octet.string"><title>OCTET STRING</title>
668
669      <synopsis>
670 typedef struct odr_oct
671 {
672     unsigned char *buf;
673     int len;
674     int size;
675 } Odr_oct;
676
677 int odr_octetstring(ODR o, Odr_oct **p, int optional,
678                     const char *name);
679      </synopsis>
680
681      <para>
682       The <literal>buf</literal> field should point to the character array
683       that holds the octetstring. The <literal>len</literal> field holds the
684       actual length, while the <literal>size</literal> field gives the size
685       of the allocated array (not of interest to you, in most cases).
686       The character array need not be null terminated.
687      </para>
688
689      <para>
690       To make things a little easier, an alternative is given for string
691       types that are not expected to contain embedded NULL characters (eg.
692       VisibleString):
693      </para>
694
695      <synopsis>
696       int odr_cstring(ODR o, char **p, int optional, const char *name);
697      </synopsis>
698
699      <para>
700       Which encoded or decodes between OCTETSTRING representations and
701       null-terminates C strings.
702      </para>
703
704      <para>
705       Functions are provided for the derived string types, eg:
706      </para>
707
708      <synopsis>
709 int odr_visiblestring(ODR o, char **p, int optional,
710                       const char *name);
711      </synopsis>
712
713     </sect3>
714     <sect3 id="odr.bit.string"><title>BIT STRING</title>
715
716      <synopsis>
717 int odr_bitstring(ODR o, Odr_bitmask **p, int optional,
718                   const char *name);
719      </synopsis>
720
721      <para>
722       The opaque type <literal>Odr_bitmask</literal> is only suitable for
723       holding relatively brief bit strings, eg. for options fields, etc.
724       The constant <literal>ODR_BITMASK_SIZE</literal> multiplied by 8
725       gives the maximum possible number of bits.
726      </para>
727
728      <para>
729       A set of macros are provided for manipulating the
730       <literal>Odr_bitmask</literal> type:
731      </para>
732
733      <synopsis>
734 void ODR_MASK_ZERO(Odr_bitmask *b);
735
736 void ODR_MASK_SET(Odr_bitmask *b, int bitno);
737
738 void ODR_MASK_CLEAR(Odr_bitmask *b, int bitno);
739
740 int ODR_MASK_GET(Odr_bitmask *b, int bitno);
741      </synopsis>
742
743      <para>
744       The functions are modeled after the manipulation functions that
745       accompany the <literal>fd_set</literal> type used by the
746       <function>select(2)</function> call.
747       <literal>ODR_MASK_ZERO</literal> should always be called first on a
748       new bitmask, to initialize the bits to zero.
749      </para>
750     </sect3>
751
752     <sect3 id="odr.object.identifier"><title>OBJECT IDENTIFIER</title>
753
754      <synopsis>
755 int odr_oid(ODR o, Odr_oid **p, int optional, const char *name);
756      </synopsis>
757
758      <para>
759       The C OID representation is simply an array of integers, terminated by
760       the value -1 (the <literal>Odr_oid</literal> type is synonymous with
761       the <literal>short</literal> type).
762       We suggest that you use the OID database module (see
763       <xref linkend="tools.oid.database"/>) to handle object identifiers
764       in your application.
765      </para>
766
767     </sect3>
768    </sect2>
769    <sect2 id="odr.tagging.primitive.types"><title>Tagging Primitive Types</title> <!-- tag.prim -->
770
771     <para>
772      The simplest way of tagging a type is to use the
773      <function>odr_implicit_tag()</function> or 
774      <function>odr_explicit_tag()</function> macros:
775     </para>
776
777     <synopsis>
778 int odr_implicit_tag(ODR o, Odr_fun fun, int class, int tag,
779                      int optional, const char *name);
780
781 int odr_explicit_tag(ODR o, Odr_fun fun, int class, int tag,
782                      int optional, const char *name);
783     </synopsis>
784
785     <para>
786      To create a type derived from the integer type by implicit tagging, you
787      might write:
788     </para>
789
790     <screen>
791      MyInt ::= [210] IMPLICIT INTEGER
792     </screen>
793
794     <para>
795      In the &odr; system, this would be written like:
796     </para>
797
798     <screen>
799 int myInt(ODR o, int **p, int optional, const char *name)
800 {
801     return odr_implicit_tag(o, odr_integer, p,
802                             ODR_CONTEXT, 210, optional, name);
803 }
804     </screen>
805
806     <para>
807      The function <function>myInt()</function> can then be used like any of
808      the primitive functions provided by &odr;. Note that the behavior of
809      <function>odr_explicit_tag()</function>
810      and <function>odr_implicit_tag()</function> macros
811      act exactly the same as the functions they are applied to - they
812      respond to error conditions, etc, in the same manner - they
813      simply have three extra parameters. The class parameter may
814      take one of the values: <literal>ODR_CONTEXT</literal>,
815      <literal>ODR_PRIVATE</literal>, <literal>ODR_UNIVERSAL</literal>, or
816      <literal>/ODR_APPLICATION</literal>.
817     </para>
818
819    </sect2>
820    <sect2 id="odr.constructed.types"><title>Constructed Types</title>
821
822     <para>
823      Constructed types are created by combining primitive types. The
824       &odr; system only implements the SEQUENCE and SEQUENCE OF constructions
825      (although adding the rest of the container types should be simple
826      enough, if the need arises).
827     </para>
828
829     <para>
830      For implementing SEQUENCEs, the functions
831     </para>
832
833     <synopsis>
834 int odr_sequence_begin(ODR o, void *p, int size, const char *name);
835 int odr_sequence_end(ODR o);
836     </synopsis>
837
838     <para>
839      are provided.
840     </para>
841
842     <para>
843      The <function>odr_sequence_begin()</function> function should be
844      called in the beginning of a function that implements a SEQUENCE type.
845      Its parameters are the &odr; stream, a pointer (to a pointer to the type
846      you're implementing), and the <literal>size</literal> of the type
847      (typically a C structure). On encoding, it returns 1 if
848      <literal>* p</literal> is a null pointer. The <literal>size</literal>
849      parameter is ignored. On decoding, it returns 1 if the type is found in
850      the data stream. <literal>size</literal> bytes of memory are allocated,
851      and <literal>*p</literal> is set to point to this space.
852      <function>odr_sequence_end()</function> is called at the end of the
853      complex function. Assume that a type is defined like this:
854     </para>
855
856     <screen>
857 MySequence ::= SEQUENCE {
858      intval INTEGER,
859      boolval BOOLEAN OPTIONAL
860 }
861     </screen>
862
863     <para>
864      The corresponding &odr; encoder/decoder function and the associated data
865      structures could be written like this:
866     </para>
867
868     <screen>
869 typedef struct MySequence
870 {
871     int *intval;
872     bool_t *boolval;
873 } MySequence;
874      
875 int mySequence(ODR o, MySequence **p, int optional, const char *name)
876 {
877     if (odr_sequence_begin(o, p, sizeof(**p), name) == 0)
878         return optional &amp;&amp; odr_ok(o);
879     return
880         odr_integer(o, &amp;(*p)->intval, 0, "intval") &amp;&amp;
881         odr_bool(o, &amp;(*p)->boolval, 1, "boolval") &amp;&amp;
882         odr_sequence_end(o);
883 }
884
885     </screen>
886
887     <para>
888      Note the 1 in the call to <function>odr_bool()</function>, to mark
889      that the sequence member is optional.
890      If either of the member types had been tagged, the macros
891      <function>odr_implicit_tag()</function> or
892      <function>odr_explicit_tag()</function>
893      could have been used.
894      The new function can be used exactly like the standard functions provided
895      with &odr;. It will encode, decode or pretty-print a data value of the
896      <literal>MySequence</literal> type. We like to name types with an
897      initial capital, as done in ASN.1 definitions, and to name the
898      corresponding function with the first character of the name in lower case.
899      You could, of course, name your structures, types, and functions any way
900      you please - as long as you're consistent, and your code is easily readable.
901      <literal>odr_ok</literal> is just that - a predicate that returns the
902      state of the stream. It is used to ensure that the behavior of the new
903      type is compatible with the interface of the primitive types.
904     </para>
905
906    </sect2>
907    <sect2 id="odr.tagging.constructed.types">
908     <title>Tagging Constructed Types</title>
909
910     <note>
911      <para>
912       See <xref linkend="odr.tagging.primitive.types"/> for information on how to tag
913       the primitive types, as well as types that are already defined.
914      </para>
915     </note>
916
917     <sect3 id="odr.implicit.tagging">
918      <title>Implicit Tagging</title>
919
920      <para>
921       Assume the type above had been defined as
922      </para>
923
924      <screen>
925 MySequence ::= [10] IMPLICIT SEQUENCE {
926       intval INTEGER,
927       boolval BOOLEAN OPTIONAL
928 }
929      </screen>
930
931      <para>
932       You would implement this in &odr; by calling the function
933      </para>
934
935      <synopsis>
936 int odr_implicit_settag(ODR o, int class, int tag);
937      </synopsis>
938
939      <para>
940       which overrides the tag of the type immediately following it. The
941       macro <function>odr_implicit_tag()</function> works by calling
942       <function>odr_implicit_settag()</function> immediately
943       before calling the function pointer argument.
944       Your type function could look like this:
945      </para>
946
947      <screen>
948 int mySequence(ODR o, MySequence **p, int optional, const char *name)
949 {
950     if (odr_implicit_settag(o, ODR_CONTEXT, 10) == 0 ||
951         odr_sequence_begin(o, p, sizeof(**p), name) == 0)
952         return optional &amp;&amp; odr_ok(o);
953     return
954         odr_integer(o, &amp;(*p)->intval, 0, "intval") &amp;&amp;
955         odr_bool(o, &amp;(*p)->boolval, 1, "boolval") &amp;&amp;
956         odr_sequence_end(o);
957 }
958      </screen>
959
960      <para>
961       The definition of the structure <literal>MySequence</literal> would be
962       the same.
963      </para>
964     </sect3>
965
966     <sect3 id="odr.explicit.tagging"><title>Explicit Tagging</title>
967
968      <para>
969       Explicit tagging of constructed types is a little more complicated,
970       since you are in effect adding a level of construction to the data.
971      </para>
972
973      <para>
974       Assume the definition:
975      </para>
976
977      <screen>
978 MySequence ::= [10] IMPLICIT SEQUENCE {
979    intval INTEGER,
980    boolval BOOLEAN OPTIONAL
981 }
982      </screen>
983
984      <para>
985       Since the new type has an extra level of construction, two new functions
986       are needed to encapsulate the base type:
987      </para>
988
989      <synopsis>
990 int odr_constructed_begin(ODR o, void *p, int class, int tag,
991                           const char *name);
992
993 int odr_constructed_end(ODR o);
994      </synopsis>
995
996      <para>
997       Assume that the IMPLICIT in the type definition above were replaced
998       with EXPLICIT (or that the IMPLICIT keyword were simply deleted, which
999       would be equivalent). The structure definition would look the same,
1000       but the function would look like this:
1001      </para>
1002
1003      <screen>
1004 int mySequence(ODR o, MySequence **p, int optional, const char *name)
1005 {
1006     if (odr_constructed_begin(o, p, ODR_CONTEXT, 10, name) == 0)
1007         return optional &amp;&amp; odr_ok(o);
1008     if (o->direction == ODR_DECODE)
1009         *p = odr_malloc(o, sizeof(**p));
1010     if (odr_sequence_begin(o, p, sizeof(**p), 0) == 0)
1011     {
1012         *p = 0; /* this is almost certainly a protocol error */
1013         return 0;
1014     }
1015     return
1016         odr_integer(o, &amp;(*p)->intval, 0, "intval") &amp;&amp;
1017         odr_bool(o, &amp;(*p)->boolval, 1, "boolval") &amp;&amp;
1018         odr_sequence_end(o) &amp;&amp;
1019         odr_constructed_end(o);
1020 }
1021      </screen>
1022
1023      <para>
1024       Notice that the interface here gets kind of nasty. The reason is
1025       simple: Explicitly tagged, constructed types are fairly rare in
1026       the protocols that we care about, so the
1027       esthetic annoyance (not to mention the dangers of a cluttered
1028       interface) is less than the time that would be required to develop a
1029       better interface. Nevertheless, it is far from satisfying, and it's a
1030       point that will be worked on in the future. One option for you would
1031       be to simply apply the <function>odr_explicit_tag()</function> macro to
1032       the first function, and not
1033       have to worry about <function>odr_constructed_*</function> yourself.
1034       Incidentally, as you might have guessed, the
1035       <function>odr_sequence_</function> functions are themselves
1036       implemented using the <function>/odr_constructed_</function> functions.
1037      </para>
1038
1039     </sect3>
1040    </sect2>
1041    <sect2 id="odr.sequence.of"><title>SEQUENCE OF</title>
1042
1043     <para>
1044      To handle sequences (arrays) of a specific type, the function
1045     </para>
1046
1047     <synopsis>
1048 int odr_sequence_of(ODR o, int (*fun)(ODR o, void *p, int optional),
1049                     void *p, int *num, const char *name);
1050     </synopsis>
1051
1052     <para>
1053      The <literal>fun</literal> parameter is a pointer to the decoder/encoder
1054      function of the type. <literal>p</literal> is a pointer to an array of
1055      pointers to your type. <literal>num</literal> is the number of elements
1056      in the array.
1057     </para>
1058
1059     <para>
1060      Assume a type
1061     </para>
1062
1063     <screen>
1064 MyArray ::= SEQUENCE OF INTEGER
1065     </screen>
1066
1067     <para>
1068      The C representation might be
1069     </para>
1070
1071     <screen>
1072 typedef struct MyArray
1073 {
1074     int num_elements;
1075     int **elements;
1076 } MyArray;
1077     </screen>
1078
1079     <para>
1080      And the function might look like
1081     </para>
1082
1083     <screen>
1084 int myArray(ODR o, MyArray **p, int optional, const char *name)
1085 {
1086     if (o->direction == ODR_DECODE)
1087         *p = odr_malloc(o, sizeof(**p));
1088     if (odr_sequence_of(o, odr_integer, &amp;(*p)->elements,
1089         &amp;(*p)->num_elements, name))
1090         return 1;
1091     *p = 0;
1092         return optional &amp;&amp; odr_ok(o);
1093 }
1094     </screen>
1095
1096    </sect2>
1097    <sect2 id="odr.choice.types"><title>CHOICE Types</title>
1098
1099     <para>
1100      The choice type is used fairly often in some ASN.1 definitions, so
1101      some work has gone into streamlining its interface.
1102     </para>
1103
1104     <para>
1105      CHOICE types are handled by the function:
1106     </para>
1107
1108     <synopsis>
1109 int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp,
1110                const char *name);
1111     </synopsis>
1112
1113     <para>
1114      The <literal>arm</literal> array is used to describe each of the possible
1115      types that the CHOICE type may assume. Internally in your application,
1116      the CHOICE type is represented as a discriminated union. That is, a
1117      C union accompanied by an integer (or enum) identifying the active
1118      'arm' of the union.
1119      <literal>whichp</literal> is a pointer to the union discriminator.
1120      When encoding, it is examined to determine the current type.
1121      When decoding, it is set to reference the type that was found in
1122      the input stream.
1123     </para>
1124
1125     <para>
1126      The Odr_arm type is defined thus:
1127     </para>
1128
1129     <screen>
1130 typedef struct odr_arm
1131 {
1132     int tagmode;
1133     int class;
1134     int tag;
1135     int which;
1136     Odr_fun fun;
1137     char *name;
1138 } Odr_arm;
1139     </screen>
1140
1141     <para>
1142      The interpretation of the fields are:
1143     </para>
1144
1145     <variablelist>
1146      <varlistentry><term>tagmode</term>
1147       <listitem><para>Either <literal>ODR_IMPLICIT</literal>,
1148         <literal>ODR_EXPLICIT</literal>, or <literal>ODR_NONE</literal> (-1)
1149         to mark no tagging.</para></listitem>
1150      </varlistentry>
1151
1152      <varlistentry><term>which</term>
1153       <listitem><para>The value of the discriminator that corresponds to
1154         this CHOICE element. Typically, it will be a #defined constant, or
1155         an enum member.</para></listitem>
1156      </varlistentry>
1157
1158      <varlistentry><term>fun</term>
1159       <listitem><para>A pointer to a function that implements the type of
1160         the CHOICE member. It may be either a standard &odr; type or a type
1161         defined by yourself.</para></listitem>
1162      </varlistentry>
1163
1164      <varlistentry><term>name</term>
1165       <listitem><para>Name of tag.</para></listitem>
1166      </varlistentry>
1167     </variablelist>
1168
1169     <para>
1170      A handy way to prepare the array for use by the
1171      <function>odr_choice()</function> function is to
1172      define it as a static, initialized array in the beginning of your
1173      decoding/encoding function. Assume the type definition:
1174     </para>
1175
1176     <screen>
1177 MyChoice ::= CHOICE {
1178     untagged INTEGER,
1179     tagged   [99] IMPLICIT INTEGER,
1180     other    BOOLEAN
1181 }
1182     </screen>
1183
1184     <para>
1185      Your C type might look like
1186     </para>
1187
1188     <screen>
1189 typedef struct MyChoice
1190 {
1191     enum
1192     {
1193         MyChoice_untagged,
1194         MyChoice_tagged,
1195         MyChoice_other
1196     } which;
1197     union
1198     {
1199         int *untagged;
1200         int *tagged;
1201         bool_t *other;
1202     } u;
1203 };
1204     </screen>
1205
1206     <para>
1207      And your function could look like this:
1208     </para>
1209
1210     <screen>
1211 int myChoice(ODR o, MyChoice **p, int optional, const char *name)
1212 {
1213     static Odr_arm arm[] =
1214     {
1215       {-1, -1, -1, MyChoice_untagged, odr_integer, "untagged"},
1216       {ODR_IMPLICIT, ODR_CONTEXT, 99, MyChoice_tagged, odr_integer,
1217       "tagged"},
1218       {-1, -1, -1, MyChoice_other, odr_boolean, "other"},
1219       {-1, -1, -1, -1, 0}
1220     };
1221
1222     if (o->direction == ODR_DECODE)
1223         *p = odr_malloc(o, sizeof(**p);
1224     else if (!*p)
1225         return optional &amp;&amp; odr_ok(o);
1226
1227     if (odr_choice(o, arm, &amp;(*p)->u, &amp;(*p)->which), name)
1228         return 1;
1229     *p = 0;
1230         return optional &amp;&amp; odr_ok(o);
1231 }
1232     </screen>
1233
1234     <para>
1235      In some cases (say, a non-optional choice which is a member of a
1236      sequence), you can "embed" the union and its discriminator in the
1237      structure belonging to the enclosing type, and you won't need to
1238      fiddle with memory allocation to create a separate structure to
1239      wrap the discriminator and union.
1240     </para>
1241
1242     <para>
1243      The corresponding function is somewhat nicer in the Sun XDR interface.
1244      Most of the complexity of this interface comes from the possibility of
1245      declaring sequence elements (including CHOICEs) optional.
1246     </para>
1247
1248     <para>
1249      The ASN.1 specifications naturally requires that each member of a
1250      CHOICE have a distinct tag, so they can be told apart on decoding.
1251      Sometimes it can be useful to define a CHOICE that has multiple types
1252      that share the same tag. You'll need some other mechanism, perhaps
1253      keyed to the context of the CHOICE type. In effect, we would like to
1254      introduce a level of context-sensitiveness to our ASN.1 specification.
1255      When encoding an internal representation, we have no problem, as long
1256      as each CHOICE member has a distinct discriminator value. For
1257      decoding, we need a way to tell the choice function to look for a
1258      specific arm of the table. The function
1259     </para>
1260
1261     <synopsis>
1262 void odr_choice_bias(ODR o, int what);
1263     </synopsis>
1264
1265     <para>
1266      provides this functionality. When called, it leaves a notice for the next
1267      call to <function>odr_choice()</function> to be called on the decoding
1268      stream <literal>o</literal> that only the <literal>arm</literal> entry with
1269      a <literal>which</literal> field equal to <literal>what</literal>
1270      should be tried.
1271     </para>
1272
1273     <para>
1274      The most important application (perhaps the only one, really) is in
1275      the definition of application-specific EXTERNAL encoders/decoders
1276      which will automatically decode an ANY member given the direct or
1277      indirect reference.
1278     </para>
1279
1280    </sect2>
1281   </sect1>
1282
1283   <sect1 id="odr.debugging"><title>Debugging</title>
1284
1285    <para>
1286     The protocol modules are suffering somewhat from a lack of diagnostic
1287     tools at the moment. Specifically ways to pretty-print PDUs that
1288     aren't recognized by the system. We'll include something to this end
1289     in a not-too-distant release. In the meantime, what we do when we get
1290     packages we don't understand is to compile the ODR module with
1291     <literal>ODR_DEBUG</literal> defined. This causes the module to dump tracing
1292     information as it processes data units. With this output and the
1293     protocol specification (Z39.50), it is generally fairly easy to see
1294     what goes wrong.
1295    </para>
1296   </sect1>
1297  </chapter>
1298  <!-- Keep this comment at the end of the file
1299  Local variables:
1300  mode: sgml
1301  sgml-omittag:t
1302  sgml-shorttag:t
1303  sgml-minimize-attributes:nil
1304  sgml-always-quote-attributes:t
1305  sgml-indent-step:1
1306  sgml-indent-data:t
1307  sgml-parent-document: "yaz.xml"
1308  sgml-local-catalogs: nil
1309  sgml-namecase-general:t
1310  End:
1311  -->