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