Removed uses of assert(3). Cleanup of ODR. CCL parser update so
[yaz-moved-to-github.git] / include / yaz / odr.h
1 /*
2  * Copyright (c) 1995-2000, 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  * $Id: odr.h,v 1.2 2000-01-31 13:15:21 adam Exp $
27  */
28
29 #ifndef ODR_H
30 #define ODR_H
31
32 #include <stdio.h>
33 #include <string.h>
34
35 #include <yaz/yconfig.h>
36 #include <yaz/nmem.h>
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 #ifndef bool_t
43 #define bool_t int
44 #endif
45
46 /*
47  * Tag modes
48  */
49 #define ODR_NONE -1
50 #define ODR_IMPLICIT 0
51 #define ODR_EXPLICIT 1
52
53 /*
54  * Classes
55  */
56 #define ODR_UNIVERSAL   0
57 #define ODR_APPLICATION 1
58 #define ODR_CONTEXT     2
59 #define ODR_PRIVATE     3
60
61 /*
62  * UNIVERSAL tags
63  */
64 #define ODR_BOOLEAN     1
65 #define ODR_INTEGER     2
66 #define ODR_BITSTRING   3
67 #define ODR_OCTETSTRING 4
68 #define ODR_NULL        5
69 #define ODR_OID         6
70 #define ODR_ODESC       7
71 #define ODR_EXTERNAL    8
72 #define ODR_REAL        9
73 #define ODR_ENUM        10
74 #define ODR_SEQUENCE    16
75 #define ODR_SET         17
76 #define ODR_NUMERICSTRING   18
77 #define ODR_PRINTABLESTRING 19
78 #define ODR_GENERALIZEDTIME 24
79 #define ODR_GRAPHICSTRING   25
80 #define ODR_VISIBLESTRING   26
81 #define ODR_GENERALSTRING   27
82
83 /*
84  * odr stream directions
85  */
86 #define ODR_DECODE      0
87 #define ODR_ENCODE      1
88 #define ODR_PRINT       2
89
90 typedef struct odr_oct
91 {
92     unsigned char *buf;
93     int len;
94     int size;
95 } Odr_oct;
96
97 typedef void Odr_null;
98 extern Odr_null *ODR_NULLVAL;
99
100 typedef Odr_oct Odr_any;
101
102 typedef struct odr_bitmask
103 {
104 #define ODR_BITMASK_SIZE 256
105     unsigned char bits[ODR_BITMASK_SIZE];
106     int top;
107 } Odr_bitmask;
108
109 typedef int Odr_oid;   /* terminate by -1 */
110
111 typedef struct odr_constack
112 {
113     const unsigned char *base;   /* starting point of data */
114     int base_offset;
115     int len;                     /* length of data, if known, else -1
116                                         (decoding only) */
117     const unsigned char *lenb;   /* where to encode length */
118     int len_offset;
119     int lenlen;                  /* length of length-field */
120 } odr_constack;
121
122 #define ODR_S_SET     0
123 #define ODR_S_CUR     1
124 #define ODR_S_END     2
125
126 typedef struct {      /* used to be statics in ber_tag... */
127     int lclass;
128     int ltag;
129     int br;
130     int lcons;
131 } Odr_ber_tag;
132
133 typedef struct odr
134 {
135     int direction;       /* the direction of this stream */
136
137     int error;           /* current error state (0==OK) */
138
139     int can_grow;         /* are we allowed to reallocate */
140     unsigned char *buf;            /* memory handle */
141     int size;             /* current buffer size */
142
143     int pos;              /* current position */
144     int top;              /* top of buffer (max pos when decoding) */
145
146     const unsigned char *bp; /* position in buffer (decoding) */
147
148     int t_class;         /* implicit tagging (-1==default tag) */
149     int t_tag;
150
151     int enable_bias;     /* force choice enable flag */
152     int choice_bias;     /* force choice */
153     int lenlen;          /* force length-of-lenght (odr_setlen()) */
154
155     FILE *print;         /* output file for direction print */
156     int indent;          /* current indent level for printing */
157
158     NMEM mem;            /* memory handle for decoding (primarily) */
159
160     /* stack for constructed types */
161 #define ODR_MAX_STACK 50
162     int stackp;          /* top of stack (-1 == initial state) */
163     odr_constack stack[ODR_MAX_STACK];
164
165     Odr_ber_tag odr_ber_tag;
166 } *ODR;
167
168 typedef int (*Odr_fun)(ODR, char **, int, const char *);
169
170 typedef struct odr_arm
171 {
172     int tagmode;
173     int zclass;
174     int tag;
175     int which;
176     Odr_fun fun;
177     char *name;
178 } Odr_arm;
179
180 /*
181  * Error control.
182  */
183 #define ONONE           0
184 #define OMEMORY         1
185 #define OSYSERR         2
186 #define OSPACE          3
187 #define OREQUIRED       4
188 #define OUNEXPECTED     5
189 #define OOTHER          6
190 #define OPROTO          7
191 #define ODATA           8
192 #define OSTACK          9
193 #define OCONLEN        10
194 #define OLENOV         11
195
196 extern char *odr_errlist[];
197
198 YAZ_EXPORT int odr_geterror(ODR o);
199 YAZ_EXPORT void odr_perror(ODR o, char *message);
200 YAZ_EXPORT void odr_setprint(ODR o, FILE *file);
201 YAZ_EXPORT ODR odr_createmem(int direction);
202 YAZ_EXPORT void odr_reset(ODR o);
203 YAZ_EXPORT void odr_destroy(ODR o);
204 YAZ_EXPORT void odr_setbuf(ODR o, char *buf, int len, int can_grow);
205 YAZ_EXPORT char *odr_getbuf(ODR o, int *len, int *size);
206 YAZ_EXPORT void *odr_malloc(ODR o, int size);
207 YAZ_EXPORT char *odr_strdup(ODR o, const char *str);
208 YAZ_EXPORT NMEM odr_extract_mem(ODR o);
209 YAZ_EXPORT Odr_null *odr_nullval(void);
210 #define odr_release_mem(m) nmem_destroy(m)
211 #define ODR_MEM NMEM
212
213 #define odr_implicit(o, t, p, cl, tg, opt)\
214         (odr_implicit_settag((o), cl, tg), t ((o), (p), (opt), 0) )
215
216 #define odr_implicit_tag(o, t, p, cl, tg, opt, name)\
217         (odr_implicit_settag((o), cl, tg), t ((o), (p), (opt), name) )
218
219 #define odr_explicit(o, t, p, cl, tg, opt)\
220         ((int) (odr_constructed_begin((o), (p), (cl), (tg), 0) ? \
221         t ((o), (p), (opt), 0) &&\
222         odr_constructed_end(o) : opt))
223
224 #define odr_explicit_tag(o, t, p, cl, tg, opt, name)\
225         ((int) (odr_constructed_begin((o), (p), (cl), (tg), 0) ? \
226         t ((o), (p), (opt), name) &&\
227         odr_constructed_end(o) : opt))
228
229 #define ODR_MASK_ZERO(mask)\
230     ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
231     (mask)->top = -1))
232
233 #define ODR_MASK_SET(mask, num)\
234     (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
235     (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
236
237 #define ODR_MASK_CLEAR(mask, num)\
238     ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
239
240 #define ODR_MASK_GET(mask, num)  ( ((num) >> 3 <= (mask)->top) ? \
241     ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
242
243 /* Private macro.
244  * write a single character at the current position - grow buffer if
245  * necessary.
246  * (no, we're not usually this anal about our macros, but this baby is
247  *  next to unreadable without some indentation  :)
248  */
249 #define odr_putc(o, c) \
250 ( \
251     ( \
252         (o)->pos < (o)->size ? \
253         ( \
254             (o)->buf[(o)->pos++] = (c), \
255             0 \
256         ) : \
257         ( \
258             odr_grow_block((o), 1) == 0 ? \
259             ( \
260                 (o)->buf[(o)->pos++] = (c), \
261                 0 \
262             ) : \
263             ( \
264                 (o)->error = OSPACE, \
265                 -1 \
266             ) \
267         ) \
268     ) == 0 ? \
269     ( \
270         (o)->pos > (o)->top ? \
271         ( \
272             (o)->top = (o)->pos, \
273             0 \
274         ) : \
275         0 \
276     ) : \
277         -1 \
278 ) \
279
280 #define odr_tell(o) ((o)->pos)
281 #define odr_offset(o) ((o)->bp - (o)->buf)
282 #define odr_ok(o) (!(o)->error)
283 #define odr_getmem(o) ((o)->mem)
284 #define odr_setmem(o, v) ((o)->mem = (v))
285
286 #define ODR_MAXNAME 256
287
288 YAZ_EXPORT int ber_boolean(ODR o, int *val);
289 YAZ_EXPORT int ber_tag(ODR o, void *p, int zclass, int tag,
290                        int *constructed, int opt);
291 YAZ_EXPORT int ber_enctag(ODR o, int zclass, int tag, int constructed);
292 YAZ_EXPORT int ber_dectag(const unsigned char *buf, int *zclass,
293                           int *tag, int *constructed);
294 YAZ_EXPORT int odr_bool(ODR o, int **p, int opt, const char *name);
295 YAZ_EXPORT int odr_integer(ODR o, int **p, int opt, const char *name);
296 YAZ_EXPORT int odr_enum(ODR o, int **p, int opt, const char *name);
297 YAZ_EXPORT int odr_implicit_settag(ODR o, int zclass, int tag);
298 YAZ_EXPORT int ber_enclen(ODR o, int len, int lenlen, int exact);
299 YAZ_EXPORT int ber_declen(const unsigned char *buf, int *len);
300 YAZ_EXPORT void odr_prname(ODR o, const char *name);
301 YAZ_EXPORT int ber_null(ODR o);
302 YAZ_EXPORT int odr_null(ODR o, Odr_null **p, int opt, const char *name);
303 YAZ_EXPORT int ber_integer(ODR o, int *val);
304 YAZ_EXPORT int odr_constructed_begin(ODR o, void *p, int zclass, int tag,
305                                      const char *name);
306 YAZ_EXPORT int odr_constructed_end(ODR o);
307 YAZ_EXPORT int odr_sequence_begin(ODR o, void *p, int size, const char *name);
308 YAZ_EXPORT int odr_set_begin(ODR o, void *p, int size, const char *name);
309 YAZ_EXPORT int odr_sequence_end(ODR o);
310 YAZ_EXPORT int odr_set_end(ODR o);
311 YAZ_EXPORT int ber_octetstring(ODR o, Odr_oct *p, int cons);
312 YAZ_EXPORT int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name);
313 YAZ_EXPORT int odp_more_chunks(ODR o, const unsigned char *base, int len);
314 YAZ_EXPORT int odr_constructed_more(ODR o);
315 YAZ_EXPORT int odr_bitstring(ODR o, Odr_bitmask **p, int opt,
316                              const char *name);
317 YAZ_EXPORT int ber_bitstring(ODR o, Odr_bitmask *p, int cons);
318 YAZ_EXPORT int odr_generalstring(ODR o, char **p, int opt, const char *name);
319 YAZ_EXPORT int ber_oidc(ODR o, Odr_oid *p);
320 YAZ_EXPORT int odr_oid(ODR o, Odr_oid **p, int opt, const char *name);
321 YAZ_EXPORT int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp,
322                           const char *name);
323 YAZ_EXPORT int odr_cstring(ODR o, char **p, int opt, const char *name);
324 YAZ_EXPORT int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num,
325                                const char *name);
326 YAZ_EXPORT int odr_set_of(ODR o, Odr_fun type, void *p, int *num,
327                           const char *name);
328 YAZ_EXPORT int odr_any(ODR o, Odr_any **p, int opt, const char *name);
329 YAZ_EXPORT int ber_any(ODR o, Odr_any **p);
330 YAZ_EXPORT int completeBER(const unsigned char *buf, int len);
331 YAZ_EXPORT void odr_begin(ODR o);
332 YAZ_EXPORT void odr_end(ODR o);
333 YAZ_EXPORT Odr_oid *odr_oiddup(ODR odr, Odr_oid *o);
334 YAZ_EXPORT Odr_oid *odr_oiddup_nmem(NMEM nmem, Odr_oid *o);
335 YAZ_EXPORT int odr_grow_block(ODR b, int min_bytes);
336 YAZ_EXPORT int odr_write(ODR o, unsigned char *buf, int bytes);
337 YAZ_EXPORT int odr_seek(ODR o, int whence, int offset);
338 YAZ_EXPORT int odr_dumpBER(FILE *f, const char *buf, int len);
339 YAZ_EXPORT void odr_choice_bias(ODR o, int what);
340 YAZ_EXPORT void odr_choice_enable_bias(ODR o, int mode);
341 YAZ_EXPORT int odr_total(ODR o);
342 YAZ_EXPORT char *odr_errmsg(int n);
343 YAZ_EXPORT Odr_oid *odr_getoidbystr(ODR o, char *str);
344 YAZ_EXPORT Odr_oid *odr_getoidbystr_nmem(NMEM o, char *str);
345 YAZ_EXPORT int odr_initmember(ODR o, void *p, int size);
346 YAZ_EXPORT int odr_peektag(ODR o, int *zclass, int *tag, int *cons);
347 YAZ_EXPORT void odr_setlenlen(ODR o, int len);
348
349 typedef struct Odr_external
350 {
351     Odr_oid *direct_reference;       /* OPTIONAL */
352     int     *indirect_reference;     /* OPTIONAL */
353     char    *descriptor;             /* OPTIONAL */
354     int which;
355 #define ODR_EXTERNAL_single 0
356 #define ODR_EXTERNAL_octet 1
357 #define ODR_EXTERNAL_arbitrary 2
358     union
359     {
360         Odr_any  *single_ASN1_type;
361         Odr_oct  *octet_aligned; 
362         Odr_bitmask *arbitrary;      /* we aren't really equipped for this*/
363     } u;
364 } Odr_external;
365
366 YAZ_EXPORT int odr_external(ODR o, Odr_external **p, int opt,
367                             const char *name);
368 YAZ_EXPORT int odr_visiblestring(ODR o, char **p, int opt,
369                                  const char *name);
370 YAZ_EXPORT int odr_graphicstring(ODR o, char **p, int opt,
371                                  const char *name);
372 YAZ_EXPORT int odr_generalizedtime(ODR o, char **p, int opt,
373                                    const char *name);
374 #ifdef __cplusplus
375 }
376 #endif
377
378 #include <yaz/xmalloc.h>
379
380 #endif