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