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