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