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