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