Improved installation. Moved header files to include/yaz.
[yaz-moved-to-github.git] / include / yaz / odr.h
1 /*
2  * Copyright (c) 1995-1999, 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.1 1999-11-30 13:47:11 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 odr_ecblock
127 {
128     int can_grow;         /* are we allowed to reallocate */
129     unsigned char *buf;            /* memory handle */
130     int pos;              /* current position */
131     int top;              /* top of buffer */
132     int size;             /* current buffer size */
133 } odr_ecblock;
134
135 typedef struct {      /* used to be statics in ber_tag... */
136     int lclass;
137     int ltag;
138     int br;
139     int lcons;
140 } Odr_ber_tag;
141
142 typedef struct odr
143 {
144     int direction;       /* the direction of this stream */
145
146     int error;           /* current error state (0==OK) */
147     const unsigned char *buf;  /* for encoding or decoding */
148     int buflen;          /* size of buffer for encoding, len for decoding */
149     const unsigned char *bp;   /* position in buffer */
150     int left;            /* bytes remaining in buffer */
151
152     odr_ecblock ecb;     /* memory control block */
153
154     int t_class;         /* implicit tagging (-1==default tag) */
155     int t_tag;
156
157     int enable_bias;     /* force choice enable flag */
158     int choice_bias;     /* force choice */
159     int lenlen;          /* force length-of-lenght (odr_setlen()) */
160
161     FILE *print;         /* output file for direction print */
162     int indent;          /* current indent level for printing */
163
164     NMEM mem;            /* memory handle for decoding (primarily) */
165
166     /* stack for constructed types */
167 #define ODR_MAX_STACK 50
168     int stackp;          /* top of stack (-1 == initial state) */
169     odr_constack stack[ODR_MAX_STACK];
170
171     Odr_ber_tag odr_ber_tag;
172 } *ODR;
173
174 typedef int (*Odr_fun)(ODR, char **, int, const char *);
175
176 typedef struct odr_arm
177 {
178     int tagmode;
179     int zclass;
180     int tag;
181     int which;
182     Odr_fun fun;
183     char *name;
184 } Odr_arm;
185
186 /*
187  * Error control.
188  */
189 #define ONONE           0
190 #define OMEMORY         1
191 #define OSYSERR         2
192 #define OSPACE          3
193 #define OREQUIRED       4
194 #define OUNEXPECTED     5
195 #define OOTHER          6
196 #define OPROTO          7
197 #define ODATA           8
198 #define OSTACK          9
199 #define OCONLEN        10
200 #define OLENOV         11
201
202 extern char *odr_errlist[];
203
204 YAZ_EXPORT int odr_geterror(ODR o);
205 YAZ_EXPORT void odr_perror(ODR o, char *message);
206 YAZ_EXPORT void odr_setprint(ODR o, FILE *file);
207 YAZ_EXPORT ODR odr_createmem(int direction);
208 YAZ_EXPORT void odr_reset(ODR o);
209 YAZ_EXPORT void odr_destroy(ODR o);
210 YAZ_EXPORT void odr_setbuf(ODR o, char *buf, int len, int can_grow);
211 YAZ_EXPORT char *odr_getbuf(ODR o, int *len, int *size);
212 YAZ_EXPORT void *odr_malloc(ODR o, int size);
213 YAZ_EXPORT char *odr_strdup(ODR o, const char *str);
214 YAZ_EXPORT NMEM odr_extract_mem(ODR o);
215 YAZ_EXPORT Odr_null *odr_nullval(void);
216 #define odr_release_mem(m) nmem_destroy(m)
217 #define ODR_MEM NMEM
218
219 #define odr_implicit(o, t, p, cl, tg, opt)\
220         (odr_implicit_settag((o), cl, tg), t ((o), (p), (opt), 0) )
221
222 #define odr_implicit_tag(o, t, p, cl, tg, opt, name)\
223         (odr_implicit_settag((o), cl, tg), t ((o), (p), (opt), name) )
224
225 #define odr_explicit(o, t, p, cl, tg, opt)\
226         ((int) (odr_constructed_begin((o), (p), (cl), (tg), 0) ? \
227         t ((o), (p), (opt), 0) &&\
228         odr_constructed_end(o) : opt))
229
230 #define odr_explicit_tag(o, t, p, cl, tg, opt, name)\
231         ((int) (odr_constructed_begin((o), (p), (cl), (tg), 0) ? \
232         t ((o), (p), (opt), name) &&\
233         odr_constructed_end(o) : opt))
234
235 #define ODR_MASK_ZERO(mask)\
236     ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
237     (mask)->top = -1))
238
239 #define ODR_MASK_SET(mask, num)\
240     (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
241     (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
242
243 #define ODR_MASK_CLEAR(mask, num)\
244     ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
245
246 #define ODR_MASK_GET(mask, num)  ( ((num) >> 3 <= (mask)->top) ? \
247     ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
248
249 /* Private macro.
250  * write a single character at the current position - grow buffer if
251  * necessary.
252  * (no, we're not usually this anal about our macros, but this baby is
253  *  next to unreadable without some indentation  :)
254  */
255 #define odr_putc(o, c) \
256 ( \
257     ( \
258         (o)->ecb.pos < (o)->ecb.size ? \
259         ( \
260             (o)->ecb.buf[(o)->ecb.pos++] = (c), \
261             0 \
262         ) : \
263         ( \
264             odr_grow_block(&(o)->ecb, 1) == 0 ? \
265             ( \
266                 (o)->ecb.buf[(o)->ecb.pos++] = (c), \
267                 0 \
268             ) : \
269             ( \
270                 (o)->error = OSPACE, \
271                 -1 \
272             ) \
273         ) \
274     ) == 0 ? \
275     ( \
276         (o)->ecb.pos > (o)->ecb.top ? \
277         ( \
278             (o)->ecb.top = (o)->ecb.pos, \
279             0 \
280         ) : \
281         0 \
282     ) : \
283         -1 \
284 ) \
285
286 #define odr_tell(o) ((o)->ecb.pos)
287 #define odr_offset(o) ((o)->bp - (o)->buf)
288 #define odr_ok(o) (!(o)->error)
289 #define odr_getmem(o) ((o)->mem)
290 #define odr_setmem(o, v) ((o)->mem = (v))
291
292 #define ODR_MAXNAME 256
293
294 YAZ_EXPORT int ber_boolean(ODR o, int *val);
295 YAZ_EXPORT int ber_tag(ODR o, void *p, int zclass, int tag,
296                        int *constructed, int opt);
297 YAZ_EXPORT int ber_enctag(ODR o, int zclass, int tag, int constructed);
298 YAZ_EXPORT int ber_dectag(const unsigned char *buf, int *zclass,
299                           int *tag, int *constructed);
300 YAZ_EXPORT int odr_bool(ODR o, int **p, int opt, const char *name);
301 YAZ_EXPORT int odr_integer(ODR o, int **p, int opt, const char *name);
302 YAZ_EXPORT int odr_enum(ODR o, int **p, int opt, const char *name);
303 YAZ_EXPORT int odr_implicit_settag(ODR o, int zclass, int tag);
304 YAZ_EXPORT int ber_enclen(ODR o, int len, int lenlen, int exact);
305 YAZ_EXPORT int ber_declen(const unsigned char *buf, int *len);
306 YAZ_EXPORT void odr_prname(ODR o, const char *name);
307 YAZ_EXPORT int ber_null(ODR o);
308 YAZ_EXPORT int odr_null(ODR o, Odr_null **p, int opt, const char *name);
309 YAZ_EXPORT int ber_integer(ODR o, int *val);
310 YAZ_EXPORT int odr_constructed_begin(ODR o, void *p, int zclass, int tag,
311                                      const char *name);
312 YAZ_EXPORT int odr_constructed_end(ODR o);
313 YAZ_EXPORT int odr_sequence_begin(ODR o, void *p, int size, const char *name);
314 YAZ_EXPORT int odr_set_begin(ODR o, void *p, int size, const char *name);
315 YAZ_EXPORT int odr_sequence_end(ODR o);
316 YAZ_EXPORT int odr_set_end(ODR o);
317 YAZ_EXPORT int ber_octetstring(ODR o, Odr_oct *p, int cons);
318 YAZ_EXPORT int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name);
319 YAZ_EXPORT int odp_more_chunks(ODR o, const unsigned char *base, int len);
320 YAZ_EXPORT int odr_constructed_more(ODR o);
321 YAZ_EXPORT int odr_bitstring(ODR o, Odr_bitmask **p, int opt,
322                              const char *name);
323 YAZ_EXPORT int ber_bitstring(ODR o, Odr_bitmask *p, int cons);
324 YAZ_EXPORT int odr_generalstring(ODR o, char **p, int opt, const char *name);
325 YAZ_EXPORT int ber_oidc(ODR o, Odr_oid *p);
326 YAZ_EXPORT int odr_oid(ODR o, Odr_oid **p, int opt, const char *name);
327 YAZ_EXPORT int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp,
328                           const char *name);
329 YAZ_EXPORT int odr_cstring(ODR o, char **p, int opt, const char *name);
330 YAZ_EXPORT int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num,
331                                const char *name);
332 YAZ_EXPORT int odr_set_of(ODR o, Odr_fun type, void *p, int *num,
333                           const char *name);
334 YAZ_EXPORT int odr_any(ODR o, Odr_any **p, int opt, const char *name);
335 YAZ_EXPORT int ber_any(ODR o, Odr_any **p);
336 YAZ_EXPORT int completeBER(const unsigned char *buf, int len);
337 YAZ_EXPORT void odr_begin(ODR o);
338 YAZ_EXPORT void odr_end(ODR o);
339 YAZ_EXPORT Odr_oid *odr_oiddup(ODR odr, Odr_oid *o);
340 YAZ_EXPORT Odr_oid *odr_oiddup_nmem(NMEM nmem, Odr_oid *o);
341 YAZ_EXPORT int odr_grow_block(odr_ecblock *b, int min_bytes);
342 YAZ_EXPORT int odr_write(ODR o, unsigned char *buf, int bytes);
343 YAZ_EXPORT int odr_seek(ODR o, int whence, int offset);
344 YAZ_EXPORT int odr_dumpBER(FILE *f, const char *buf, int len);
345 YAZ_EXPORT void odr_choice_bias(ODR o, int what);
346 YAZ_EXPORT void odr_choice_enable_bias(ODR o, int mode);
347 YAZ_EXPORT int odr_total(ODR o);
348 YAZ_EXPORT char *odr_errmsg(int n);
349 YAZ_EXPORT Odr_oid *odr_getoidbystr(ODR o, char *str);
350 YAZ_EXPORT Odr_oid *odr_getoidbystr_nmem(NMEM o, char *str);
351 YAZ_EXPORT int odr_initmember(ODR o, void *p, int size);
352 YAZ_EXPORT int odr_peektag(ODR o, int *zclass, int *tag, int *cons);
353 YAZ_EXPORT void odr_setlenlen(ODR o, int len);
354
355 typedef struct Odr_external
356 {
357     Odr_oid *direct_reference;       /* OPTIONAL */
358     int     *indirect_reference;     /* OPTIONAL */
359     char    *descriptor;             /* OPTIONAL */
360     int which;
361 #define ODR_EXTERNAL_single 0
362 #define ODR_EXTERNAL_octet 1
363 #define ODR_EXTERNAL_arbitrary 2
364     union
365     {
366         Odr_any  *single_ASN1_type;
367         Odr_oct  *octet_aligned; 
368         Odr_bitmask *arbitrary;      /* we aren't really equipped for this*/
369     } u;
370 } Odr_external;
371
372 YAZ_EXPORT int odr_external(ODR o, Odr_external **p, int opt,
373                             const char *name);
374 YAZ_EXPORT int odr_visiblestring(ODR o, char **p, int opt,
375                                  const char *name);
376 YAZ_EXPORT int odr_graphicstring(ODR o, char **p, int opt,
377                                  const char *name);
378 YAZ_EXPORT int odr_generalizedtime(ODR o, char **p, int opt,
379                                    const char *name);
380 #ifdef __cplusplus
381 }
382 #endif
383
384 #include <yaz/xmalloc.h>
385
386 #endif