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