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