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