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