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