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