9c4f6581336ad4bafc2ba19c79b2dcbcff059f49
[yaz-moved-to-github.git] / include / yaz / odr.h
1 /*
2  * Copyright (c) 1995-2004, 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.16 2004-08-13 07:30:06 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 #define ODR_S_SET     0
110 #define ODR_S_CUR     1
111 #define ODR_S_END     2
112
113 typedef struct odr
114 {
115     int direction;       /* the direction of this stream */
116
117     int error;           /* current error state (0==OK) */
118
119     int can_grow;         /* are we allowed to reallocate */
120     unsigned char *buf;            /* memory handle */
121     int size;             /* current buffer size */
122
123     int pos;              /* current position */
124     int top;              /* top of buffer (max pos when decoding) */
125
126     const unsigned char *bp; /* position in buffer (decoding) */
127
128     int t_class;         /* implicit tagging (-1==default tag) */
129     int t_tag;
130
131     int enable_bias;     /* force choice enable flag */
132     int choice_bias;     /* force choice */
133     int lenlen;          /* force length-of-lenght (odr_setlen()) */
134
135     FILE *print;         /* output file handler for direction print */
136     int indent;          /* current indent level for printing */
137
138     NMEM mem;            /* memory handle for decoding (primarily) */
139
140     struct Odr_private *op;
141 } *ODR;
142
143 typedef int (*Odr_fun)(ODR, char **, int, const char *);
144
145 typedef struct odr_arm
146 {
147     int tagmode;
148     int zclass;
149     int tag;
150     int which;
151     Odr_fun fun;
152     char *name;
153 } Odr_arm;
154
155 /*
156  * Error control.
157  */
158 #define ONONE           0
159 #define OMEMORY         1
160 #define OSYSERR         2
161 #define OSPACE          3
162 #define OREQUIRED       4
163 #define OUNEXPECTED     5
164 #define OOTHER          6
165 #define OPROTO          7
166 #define ODATA           8
167 #define OSTACK          9
168 #define OCONLEN        10
169 #define OLENOV         11
170 #define OHTTP          12
171
172 extern char *odr_errlist[];
173
174 YAZ_EXPORT int odr_geterror(ODR o);
175 YAZ_EXPORT int odr_geterrorx(ODR o, int *x);
176 YAZ_EXPORT void odr_seterror(ODR o, int errorno, int errorid);
177 YAZ_EXPORT void odr_setelement(ODR o, const char *addinfo);
178 YAZ_EXPORT const char *odr_getelement(ODR o);
179 YAZ_EXPORT void odr_perror(ODR o, const char *message);
180 YAZ_EXPORT void odr_setprint(ODR o, FILE *file);
181 YAZ_EXPORT ODR odr_createmem(int direction);
182 YAZ_EXPORT void odr_reset(ODR o);
183 YAZ_EXPORT void odr_destroy(ODR o);
184 YAZ_EXPORT void odr_setbuf(ODR o, char *buf, int len, int can_grow);
185 YAZ_EXPORT char *odr_getbuf(ODR o, int *len, int *size);
186 YAZ_EXPORT void *odr_malloc(ODR o, int size);
187 YAZ_EXPORT char *odr_strdup(ODR o, const char *str);
188 YAZ_EXPORT char *odr_strdupn(ODR o, const char *str, size_t n);
189 YAZ_EXPORT int *odr_intdup(ODR o, int v);
190 YAZ_EXPORT NMEM odr_extract_mem(ODR o);
191 YAZ_EXPORT Odr_null *odr_nullval(void);
192 #define odr_release_mem(m) nmem_destroy(m)
193 #define ODR_MEM NMEM
194
195 #define odr_implicit_tag(o, t, p, cl, tg, opt, name)\
196         (odr_implicit_settag((o), cl, tg), t ((o), (p), (opt), name) )
197
198 #define odr_explicit_tag(o, t, p, cl, tg, opt, name)\
199         ((int) (odr_constructed_begin((o), (p), (cl), (tg), 0) ? \
200         t ((o), (p), (opt), name) &&\
201         odr_constructed_end(o) : odr_missing((o), opt, name)))
202
203 #define ODR_MASK_ZERO(mask)\
204     ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
205     (mask)->top = -1))
206
207 #define ODR_MASK_SET(mask, num)\
208     (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
209     (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
210
211 #define ODR_MASK_CLEAR(mask, num)\
212     ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
213
214 #define ODR_MASK_GET(mask, num)  ( ((num) >> 3 <= (mask)->top) ? \
215     ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
216
217
218 #define odr_tell(o) ((o)->pos)
219 #define odr_offset(o) ((o)->bp - (o)->buf)
220 #define odr_ok(o) (!(o)->error)
221 #define odr_getmem(o) ((o)->mem)
222 #define odr_setmem(o, v) ((o)->mem = (v))
223
224 #define ODR_MAXNAME 256
225
226 YAZ_EXPORT int ber_boolean(ODR o, int *val);
227 YAZ_EXPORT int ber_tag(ODR o, void *p, int zclass, int tag,
228                        int *constructed, int opt, const char *name);
229 YAZ_EXPORT int ber_enctag(ODR o, int zclass, int tag, int constructed);
230 YAZ_EXPORT int ber_dectag(const unsigned char *buf, int *zclass,
231                           int *tag, int *constructed, int max);
232 YAZ_EXPORT int odr_bool(ODR o, int **p, int opt, const char *name);
233 YAZ_EXPORT int odr_integer(ODR o, int **p, int opt, const char *name);
234 YAZ_EXPORT int odr_enum(ODR o, int **p, int opt, const char *name);
235 YAZ_EXPORT int odr_implicit_settag(ODR o, int zclass, int tag);
236 YAZ_EXPORT int ber_enclen(ODR o, int len, int lenlen, int exact);
237 YAZ_EXPORT int ber_declen(const unsigned char *buf, int *len, int max);
238 YAZ_EXPORT void odr_prname(ODR o, const char *name);
239 YAZ_EXPORT int ber_null(ODR o);
240 YAZ_EXPORT int odr_null(ODR o, Odr_null **p, int opt, const char *name);
241 YAZ_EXPORT int ber_integer(ODR o, int *val);
242 YAZ_EXPORT int odr_constructed_begin(ODR o, void *p, int zclass, int tag,
243                                      const char *name);
244 YAZ_EXPORT int odr_constructed_end(ODR o);
245 YAZ_EXPORT int odr_sequence_begin(ODR o, void *p, int size, const char *name);
246 YAZ_EXPORT int odr_set_begin(ODR o, void *p, int size, const char *name);
247 YAZ_EXPORT int odr_sequence_end(ODR o);
248 YAZ_EXPORT int odr_set_end(ODR o);
249 YAZ_EXPORT int ber_octetstring(ODR o, Odr_oct *p, int cons);
250 YAZ_EXPORT int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name);
251 YAZ_EXPORT int odp_more_chunks(ODR o, const unsigned char *base, int len);
252 YAZ_EXPORT int odr_constructed_more(ODR o);
253 YAZ_EXPORT int odr_bitstring(ODR o, Odr_bitmask **p, int opt,
254                              const char *name);
255 YAZ_EXPORT int ber_bitstring(ODR o, Odr_bitmask *p, int cons);
256 YAZ_EXPORT int odr_generalstring(ODR o, char **p, int opt, const char *name);
257 YAZ_EXPORT int ber_oidc(ODR o, Odr_oid *p);
258 YAZ_EXPORT int odr_oid(ODR o, Odr_oid **p, int opt, const char *name);
259 YAZ_EXPORT int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp,
260                           const char *name);
261 YAZ_EXPORT int odr_cstring(ODR o, char **p, int opt, const char *name);
262 YAZ_EXPORT int odr_iconv_string(ODR o, char **p, int opt, const char *name);
263 YAZ_EXPORT int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num,
264                                const char *name);
265 YAZ_EXPORT int odr_set_of(ODR o, Odr_fun type, void *p, int *num,
266                           const char *name);
267 YAZ_EXPORT int odr_any(ODR o, Odr_any **p, int opt, const char *name);
268 YAZ_EXPORT int ber_any(ODR o, Odr_any **p);
269 YAZ_EXPORT int completeBER(const unsigned char *buf, int len);
270 YAZ_EXPORT void odr_begin(ODR o);
271 YAZ_EXPORT void odr_end(ODR o);
272 YAZ_EXPORT Odr_oid *odr_oiddup(ODR odr, Odr_oid *o);
273 YAZ_EXPORT Odr_oid *odr_oiddup_nmem(NMEM nmem, Odr_oid *o);
274 YAZ_EXPORT int odr_grow_block(ODR b, int min_bytes);
275 YAZ_EXPORT int odr_write(ODR o, unsigned char *buf, int bytes);
276 YAZ_EXPORT int odr_seek(ODR o, int whence, int offset);
277 YAZ_EXPORT int odr_dumpBER(FILE *f, const char *buf, int len);
278 YAZ_EXPORT void odr_choice_bias(ODR o, int what);
279 YAZ_EXPORT void odr_choice_enable_bias(ODR o, int mode);
280 YAZ_EXPORT int odr_total(ODR o);
281 YAZ_EXPORT char *odr_errmsg(int n);
282 YAZ_EXPORT Odr_oid *odr_getoidbystr(ODR o, const char *str);
283 YAZ_EXPORT Odr_oid *odr_getoidbystr_nmem(NMEM o, const char *str);
284
285 YAZ_EXPORT int odr_initmember(ODR o, void *p, int size);
286 YAZ_EXPORT int odr_peektag(ODR o, int *zclass, int *tag, int *cons);
287 YAZ_EXPORT void odr_setlenlen(ODR o, int len);
288 YAZ_EXPORT int odr_missing(ODR o, int opt, const char *name);
289 YAZ_EXPORT char *odr_prepend(ODR o, const char *prefix, const char *old);
290
291 typedef struct Odr_external
292 {
293     Odr_oid *direct_reference;       /* OPTIONAL */
294     int     *indirect_reference;     /* OPTIONAL */
295     char    *descriptor;             /* OPTIONAL */
296     int which;
297 #define ODR_EXTERNAL_single 0
298 #define ODR_EXTERNAL_octet 1
299 #define ODR_EXTERNAL_arbitrary 2
300     union
301     {
302         Odr_any  *single_ASN1_type;
303         Odr_oct  *octet_aligned; 
304         Odr_bitmask *arbitrary;      /* we aren't really equipped for this*/
305     } u;
306 } Odr_external;
307
308 YAZ_EXPORT int odr_external(ODR o, Odr_external **p, int opt,
309                             const char *name);
310 YAZ_EXPORT int odr_visiblestring(ODR o, char **p, int opt,
311                                  const char *name);
312 YAZ_EXPORT int odr_graphicstring(ODR o, char **p, int opt,
313                                  const char *name);
314 YAZ_EXPORT int odr_generalizedtime(ODR o, char **p, int opt,
315                                    const char *name);
316
317 YAZ_EXPORT int odr_set_charset(ODR o, const char *to, const char *from);
318
319 YAZ_EXPORT void odr_set_stream(ODR o,  void *handle,
320                                void (*stream_write)(ODR o, 
321                                                     void *handle,
322                                                     int type,
323                                                     const char *buf,
324                                                     int len),
325                                void (*stream_close)(void *handle));
326
327 YAZ_EXPORT void odr_printf(ODR o, const char *fmt, ...);
328
329 YAZ_EXPORT const char **odr_get_element_path(ODR o);
330
331 YAZ_END_CDECL
332
333 #include <yaz/xmalloc.h>
334
335 #endif