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