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