Implemented odr_strdup. Added Reference ID to backend server API.
[yaz-moved-to-github.git] / include / odr.h
1 /*
2  * Copyright (c) 1995-1999, Index Data.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation, in whole or in part, for any purpose, is hereby granted,
6  * provided that:
7  *
8  * 1. This copyright and permission notice appear in all copies of the
9  * software and its documentation. Notices of copyright or attribution
10  * which appear at the beginning of any file must remain unchanged.
11  *
12  * 2. The name of Index Data or the individual authors may not be used to
13  * endorse or promote products derived from this software without specific
14  * prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19  * IN NO EVENT SHALL INDEX DATA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
20  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR
22  * NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * $Log: odr.h,v $
27  * Revision 1.28  1999-03-31 11:18:25  adam
28  * Implemented odr_strdup. Added Reference ID to backend server API.
29  *
30  * Revision 1.27  1999/01/08 11:23:17  adam
31  * Added const modifier to some of the BER/ODR encoding routines.
32  *
33  * Revision 1.26  1998/03/20 17:29:20  adam
34  * Include of odr_use.h in odr.h. Added prototype for odr_enum.
35  *
36  * Revision 1.25  1998/02/11 11:53:33  adam
37  * Changed code so that it compiles as C++.
38  *
39  * Revision 1.24  1997/11/24 11:33:56  adam
40  * Using function odr_nullval() instead of global ODR_NULLVAL when
41  * appropriate.
42  *
43  * Revision 1.23  1997/09/01 08:49:49  adam
44  * New windows NT/95 port using MSV5.0. To export DLL functions the
45  * YAZ_EXPORT modifier was added. Defined in yconfig.h.
46  *
47  * Revision 1.22  1997/07/21 12:44:30  adam
48  * Moved definitions of nmem_block and nmem_control.
49  *
50  * Revision 1.21  1997/05/14 06:53:41  adam
51  * C++ support.
52  *
53  * Revision 1.20  1997/04/30 08:52:08  quinn
54  * Null
55  *
56  * Revision 1.19  1996/10/08  12:58:08  adam
57  * New ODR function, odr_choice_enable_bias, to control behaviour of
58  * odr_choice_bias.
59  *
60  * Revision 1.18  1996/07/26  13:38:14  quinn
61  * Various smaller things. Gathered header-files.
62  *
63  * Revision 1.17  1995/11/08  17:41:27  quinn
64  * Smallish.
65  *
66  * Revision 1.16  1995/11/01  13:54:36  quinn
67  * Minor adjustments
68  *
69  * Revision 1.15  1995/10/18  16:12:36  quinn
70  * Better diagnostics.
71  *
72  * Revision 1.14  1995/09/29  17:12:04  quinn
73  * Smallish
74  *
75  * Revision 1.13  1995/09/29  17:01:49  quinn
76  * More Windows work
77  *
78  * Revision 1.12  1995/09/27  15:02:48  quinn
79  * Modified function heads & prototypes.
80  *
81  * Revision 1.11  1995/08/15  12:00:09  quinn
82  * Updated External
83  *
84  * Revision 1.10  1995/08/10  08:54:33  quinn
85  * Added Explain.
86  *
87  * Revision 1.9  1995/06/19  12:38:26  quinn
88  * Reorganized include-files. Added small features.
89  *
90  * Revision 1.8  1995/06/16  13:16:04  quinn
91  * Fixed Defaultdiagformat.
92  *
93  * Revision 1.7  1995/05/29  08:11:32  quinn
94  * Moved oid from odr/asn to util.
95  *
96  * Revision 1.6  1995/05/22  14:47:00  quinn
97  * ODR_NULLVAL --> void
98  *
99  * Revision 1.5  1995/05/22  11:31:24  quinn
100  * Added PDUs
101  *
102  * Revision 1.4  1995/05/16  08:50:33  quinn
103  * License, documentation, and memory fixes
104  *
105  * Revision 1.3  1995/05/15  11:55:54  quinn
106  * Work on asynchronous activity.
107  *
108  * Revision 1.2  1995/04/18  08:14:37  quinn
109  * Added dynamic memory allocation on encoding
110  *
111  * Revision 1.1  1995/03/30  09:39:41  quinn
112  * Moved .h files to include directory
113  *
114  * Revision 1.15  1995/03/29  15:39:57  quinn
115  * Fixed bugs in the bitmask operations
116  *
117  * Revision 1.14  1995/03/27  08:33:15  quinn
118  * Added more OID utilities.
119  *
120  * Revision 1.13  1995/03/17  10:17:44  quinn
121  * Added memory management.
122  *
123  * Revision 1.12  1995/03/14  10:27:38  quinn
124  * Modified makefile to use common lib
125  * Beginning to add memory management to odr
126  *
127  * Revision 1.11  1995/03/10  11:44:41  quinn
128  * Fixed serious stack-bug in odr_cons_begin
129  *
130  * Revision 1.10  1995/03/08  12:12:16  quinn
131  * Added better error checking.
132  *
133  * Revision 1.9  1995/03/07  10:10:00  quinn
134  * Added some headers for Adam.
135  *
136  * Revision 1.8  1995/03/07  09:23:16  quinn
137  * Installing top-level API and documentation.
138  *
139  * Revision 1.7  1995/02/10  15:55:29  quinn
140  * Bug fixes, mostly.
141  *
142  * Revision 1.6  1995/02/09  15:51:47  quinn
143  * Works better now.
144  *
145  * Revision 1.5  1995/02/07  17:52:59  quinn
146  * A damn mess, but now things work, I think.
147  *
148  * Revision 1.4  1995/02/06  16:45:03  quinn
149  * Small mods.
150  *
151  * Revision 1.3  1995/02/03  17:04:36  quinn
152  * *** empty log message ***
153  *
154  * Revision 1.2  1995/02/02  20:38:50  quinn
155  * Updates.
156  *
157  * Revision 1.1  1995/02/02  16:21:53  quinn
158  * First kick.
159  *
160  */
161
162 #ifndef ODR_H
163 #define ODR_H
164
165 #include <stdio.h>
166 #include <string.h>
167
168 #include <nmem.h>
169 #include <yconfig.h>
170
171 #ifdef __cplusplus
172 extern "C" {
173 #endif
174
175 #ifndef bool_t
176 #define bool_t int
177 #endif
178
179 /*
180  * Tag modes
181  */
182 #define ODR_NONE -1
183 #define ODR_IMPLICIT 0
184 #define ODR_EXPLICIT 1
185
186 /*
187  * Classes
188  */
189 #define ODR_UNIVERSAL   0
190 #define ODR_APPLICATION 1
191 #define ODR_CONTEXT     2
192 #define ODR_PRIVATE     3
193
194 /*
195  * UNIVERSAL tags
196  */
197 #define ODR_BOOLEAN     1
198 #define ODR_INTEGER     2
199 #define ODR_BITSTRING   3
200 #define ODR_OCTETSTRING 4
201 #define ODR_NULL        5
202 #define ODR_OID         6
203 #define ODR_ODESC       7
204 #define ODR_EXTERNAL    8
205 #define ODR_REAL        9
206 #define ODR_ENUM        10
207 #define ODR_SEQUENCE    16
208 #define ODR_SET         17
209 #define ODR_NUMERICSTRING   18
210 #define ODR_PRINTABLESTRING 19
211 #define ODR_GENERALIZEDTIME 24
212 #define ODR_GRAPHICSTRING   25
213 #define ODR_VISIBLESTRING   26
214 #define ODR_GENERALSTRING   27
215
216 /*
217  * odr stream directions
218  */
219 #define ODR_DECODE      0
220 #define ODR_ENCODE      1
221 #define ODR_PRINT       2
222
223 typedef struct odr_oct
224 {
225     unsigned char *buf;
226     int len;
227     int size;
228 } Odr_oct;
229
230 typedef void Odr_null;
231 extern Odr_null *ODR_NULLVAL;
232
233 typedef Odr_oct Odr_any;
234
235 typedef struct odr_bitmask
236 {
237 #define ODR_BITMASK_SIZE 256
238     unsigned char bits[ODR_BITMASK_SIZE];
239     int top;
240 } Odr_bitmask;
241
242 typedef int Odr_oid;   /* terminate by -1 */
243
244 typedef struct odr_constack
245 {
246     const unsigned char *base;   /* starting point of data */
247     int base_offset;
248     int len;                     /* length of data, if known, else -1
249                                         (decoding only) */
250     const unsigned char *lenb;   /* where to encode length */
251     int len_offset;
252     int lenlen;                  /* length of length-field */
253 } odr_constack;
254
255 #define ODR_S_SET     0
256 #define ODR_S_CUR     1
257 #define ODR_S_END     2
258
259 typedef struct odr_ecblock
260 {
261     int can_grow;         /* are we allowed to reallocate */
262     unsigned char *buf;            /* memory handle */
263     int pos;              /* current position */
264     int top;              /* top of buffer */
265     int size;             /* current buffer size */
266 } odr_ecblock;
267
268 typedef struct {      /* used to be statics in ber_tag... */
269     int lclass;
270     int ltag;
271     int br;
272     int lcons;
273 } Odr_ber_tag;
274
275 typedef struct odr
276 {
277     int direction;       /* the direction of this stream */
278
279     int error;           /* current error state (0==OK) */
280     const unsigned char *buf;  /* for encoding or decoding */
281     int buflen;          /* size of buffer for encoding, len for decoding */
282     const unsigned char *bp;   /* position in buffer */
283     int left;            /* bytes remaining in buffer */
284
285     odr_ecblock ecb;     /* memory control block */
286
287     int t_class;         /* implicit tagging (-1==default tag) */
288     int t_tag;
289
290     int enable_bias;     /* force choice enable flag */
291     int choice_bias;     /* force choice */
292     int lenlen;          /* force length-of-lenght (odr_setlen()) */
293
294     FILE *print;         /* output file for direction print */
295     int indent;          /* current indent level for printing */
296
297     NMEM mem;            /* memory handle for decoding (primarily) */
298
299     /* stack for constructed types */
300 #define ODR_MAX_STACK 50
301     int stackp;          /* top of stack (-1 == initial state) */
302     odr_constack stack[ODR_MAX_STACK];
303
304     Odr_ber_tag odr_ber_tag;
305 } *ODR;
306
307 typedef int (*Odr_fun)(ODR, char **, int);
308
309 typedef struct odr_arm
310 {
311     int tagmode;
312     int zclass;
313     int tag;
314     int which;
315     Odr_fun fun;
316 } Odr_arm;
317
318 /*
319  * Error control.
320  */
321 #define ONONE           0
322 #define OMEMORY         1
323 #define OSYSERR         2
324 #define OSPACE          3
325 #define OREQUIRED       4
326 #define OUNEXPECTED     5
327 #define OOTHER          6
328 #define OPROTO          7
329 #define ODATA           8
330 #define OSTACK          9
331 #define OCONLEN        10
332 #define OLENOV         11
333
334 extern char *odr_errlist[];
335
336 YAZ_EXPORT int odr_geterror(ODR o);
337 YAZ_EXPORT void odr_perror(ODR o, char *message);
338 YAZ_EXPORT void odr_setprint(ODR o, FILE *file);
339 YAZ_EXPORT ODR odr_createmem(int direction);
340 YAZ_EXPORT void odr_reset(ODR o);
341 YAZ_EXPORT void odr_destroy(ODR o);
342 YAZ_EXPORT void odr_setbuf(ODR o, char *buf, int len, int can_grow);
343 YAZ_EXPORT char *odr_getbuf(ODR o, int *len, int *size);
344 YAZ_EXPORT void *odr_malloc(ODR o, int size);
345 YAZ_EXPORT char *odr_strdup(ODR o, const char *str);
346 YAZ_EXPORT NMEM odr_extract_mem(ODR o);
347 YAZ_EXPORT Odr_null *odr_nullval(void);
348 #define odr_release_mem(m) nmem_destroy(m)
349 #define ODR_MEM NMEM
350
351 #define odr_implicit(o, t, p, cl, tg, opt)\
352         (odr_implicit_settag((o), cl, tg), t ((o), (p), opt) )
353
354 #define odr_explicit(o, t, p, cl, tg, opt)\
355         ((int) (odr_constructed_begin((o), (p), (cl), (tg)) ? \
356         t ((o), (p), (opt)) &&\
357         odr_constructed_end(o) : opt))
358
359 #define ODR_MASK_ZERO(mask)\
360     ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
361     (mask)->top = -1))
362
363 #define ODR_MASK_SET(mask, num)\
364     (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
365     (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
366
367 #define ODR_MASK_CLEAR(mask, num)\
368     ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
369
370 #define ODR_MASK_GET(mask, num)  ( ((num) >> 3 <= (mask)->top) ? \
371     ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
372
373 /* Private macro.
374  * write a single character at the current position - grow buffer if
375  * necessary.
376  * (no, we're not usually this anal about our macros, but this baby is
377  *  next to unreadable without some indentation  :)
378  */
379 #define odr_putc(o, c) \
380 ( \
381     ( \
382         (o)->ecb.pos < (o)->ecb.size ? \
383         ( \
384             (o)->ecb.buf[(o)->ecb.pos++] = (c), \
385             0 \
386         ) : \
387         ( \
388             odr_grow_block(&(o)->ecb, 1) == 0 ? \
389             ( \
390                 (o)->ecb.buf[(o)->ecb.pos++] = (c), \
391                 0 \
392             ) : \
393             ( \
394                 (o)->error = OSPACE, \
395                 -1 \
396             ) \
397         ) \
398     ) == 0 ? \
399     ( \
400         (o)->ecb.pos > (o)->ecb.top ? \
401         ( \
402             (o)->ecb.top = (o)->ecb.pos, \
403             0 \
404         ) : \
405         0 \
406     ) : \
407         -1 \
408 ) \
409
410 #define odr_tell(o) ((o)->ecb.pos)
411 #define odr_offset(o) ((o)->bp - (o)->buf)
412 #define odr_ok(o) (!(o)->error)
413 #define odr_getmem(o) ((o)->mem)
414 #define odr_setmem(o, v) ((o)->mem = (v))
415
416 #define ODR_MAXNAME 256
417
418 #ifdef __cplusplus
419 }
420 #endif
421
422 #include <odr_use.h>
423 #include <prt.h>
424 #include <xmalloc.h>
425
426 #endif