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