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