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