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