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