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