a25ad20825741b15624a374551fd4b6b717d32fc
[yaz-moved-to-github.git] / include / yaz / odr.h
1 /*
2  * Copyright (c) 1995-2007, Index Data
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 /* $Id: odr.h,v 1.25 2007-01-03 08:42:14 adam Exp $ */
28
29 /**
30  * \file odr.h
31  * \brief Header for ODR (Open Data Representation)
32  */
33
34 #ifndef ODR_H
35 #define ODR_H
36
37 #include <stdio.h>
38 #include <string.h>
39
40 #include <yaz/yconfig.h>
41 #include <yaz/nmem.h>
42
43 YAZ_BEGIN_CDECL
44
45 #ifndef bool_t
46 #define bool_t int
47 #endif
48
49 /*
50  * Tag modes
51  */
52 #define ODR_NONE -1
53 #define ODR_IMPLICIT 0
54 #define ODR_EXPLICIT 1
55
56 /*
57  * Classes
58  */
59 #define ODR_UNIVERSAL   0
60 #define ODR_APPLICATION 1
61 #define ODR_CONTEXT     2
62 #define ODR_PRIVATE     3
63
64 /*
65  * UNIVERSAL tags
66  */
67 #define ODR_BOOLEAN     1
68 #define ODR_INTEGER     2
69 #define ODR_BITSTRING   3
70 #define ODR_OCTETSTRING 4
71 #define ODR_NULL        5
72 #define ODR_OID         6
73 #define ODR_ODESC       7
74 #define ODR_EXTERNAL    8
75 #define ODR_REAL        9
76 #define ODR_ENUM        10
77 #define ODR_SEQUENCE    16
78 #define ODR_SET         17
79 #define ODR_NUMERICSTRING   18
80 #define ODR_PRINTABLESTRING 19
81 #define ODR_GENERALIZEDTIME 24
82 #define ODR_GRAPHICSTRING   25
83 #define ODR_VISIBLESTRING   26
84 #define ODR_GENERALSTRING   27
85
86 /*
87  * odr stream directions
88  */
89 #define ODR_DECODE      0
90 #define ODR_ENCODE      1
91 #define ODR_PRINT       2
92
93 typedef struct odr_oct
94 {
95     unsigned char *buf;
96     int len;
97     int size;
98 } Odr_oct;
99
100 typedef void Odr_null;
101 extern Odr_null *ODR_NULLVAL;
102
103 typedef Odr_oct Odr_any;
104
105 typedef struct odr_bitmask
106 {
107 #define ODR_BITMASK_SIZE 256
108     unsigned char bits[ODR_BITMASK_SIZE];
109     int top;
110 } Odr_bitmask;
111
112 typedef int Odr_oid;   /* terminate by -1 */
113
114 #define ODR_S_SET     0
115 #define ODR_S_CUR     1
116 #define ODR_S_END     2
117
118 typedef struct odr
119 {
120     int direction;       /* the direction of this stream */
121
122     int error;           /* current error state (0==OK) */
123
124     int can_grow;         /* are we allowed to reallocate */
125     unsigned char *buf;            /* memory handle */
126     int size;             /* current buffer size (encoding+decoding) */
127
128     int pos;              /* current position (encoding) */
129     int top;              /* top of buffer (max pos when encoding) */
130
131     const unsigned char *bp; /* position in buffer (decoding) */
132
133     int t_class;         /* implicit tagging (-1==default tag) */
134     int t_tag;
135
136     int enable_bias;     /* force choice enable flag */
137     int choice_bias;     /* force choice */
138     int lenlen;          /* force length-of-lenght (odr_setlen()) */
139
140     FILE *print;         /* output file handler for direction print */
141     int indent;          /* current indent level for printing */
142
143     NMEM mem;            /* memory handle for decoding (primarily) */
144
145     struct Odr_private *op;
146 } *ODR;
147
148 typedef int (*Odr_fun)(ODR, char **, int, const char *);
149
150 typedef struct odr_arm
151 {
152     int tagmode;
153     int zclass;
154     int tag;
155     int which;
156     Odr_fun fun;
157     char *name;
158 } Odr_arm;
159
160 /*
161  * Error control.
162  */
163 #define ONONE           0
164 #define OMEMORY         1
165 #define OSYSERR         2
166 #define OSPACE          3
167 #define OREQUIRED       4
168 #define OUNEXPECTED     5
169 #define OOTHER          6
170 #define OPROTO          7
171 #define ODATA           8
172 #define OSTACK          9
173 #define OCONLEN        10
174 #define OLENOV         11
175 #define OHTTP          12
176
177 extern char *odr_errlist[];
178
179 YAZ_EXPORT int odr_geterror(ODR o);
180 YAZ_EXPORT int odr_geterrorx(ODR o, int *x);
181 YAZ_EXPORT void odr_seterror(ODR o, int errorno, int errorid);
182 YAZ_EXPORT void odr_setelement(ODR o, const char *addinfo);
183 YAZ_EXPORT const char *odr_getelement(ODR o);
184 YAZ_EXPORT void odr_perror(ODR o, const char *message);
185 YAZ_EXPORT void odr_setprint(ODR o, FILE *file);
186 YAZ_EXPORT ODR odr_createmem(int direction);
187 YAZ_EXPORT void odr_reset(ODR o);
188 YAZ_EXPORT void odr_destroy(ODR o);
189 YAZ_EXPORT void odr_setbuf(ODR o, char *buf, int len, int can_grow);
190 YAZ_EXPORT char *odr_getbuf(ODR o, int *len, int *size);
191 YAZ_EXPORT void *odr_malloc(ODR o, int size);
192 YAZ_EXPORT char *odr_strdup(ODR o, const char *str);
193 YAZ_EXPORT char *odr_strdupn(ODR o, const char *str, size_t n);
194 YAZ_EXPORT int *odr_intdup(ODR o, int v);
195 YAZ_EXPORT Odr_oct *odr_create_Odr_oct(ODR o, const unsigned char *buf,
196                                        int sz);
197 YAZ_EXPORT NMEM odr_extract_mem(ODR o);
198 YAZ_EXPORT Odr_null *odr_nullval(void);
199 #define odr_release_mem(m) nmem_destroy(m)
200 #define ODR_MEM NMEM
201
202 #define odr_implicit_tag(o, t, p, cl, tg, opt, name)\
203         (odr_implicit_settag((o), cl, tg), t ((o), (p), (opt), name) )
204
205 #define odr_explicit_tag(o, t, p, cl, tg, opt, name)\
206         ((int) (odr_constructed_begin((o), (p), (cl), (tg), 0) ? \
207         t ((o), (p), (opt), name) &&\
208         odr_constructed_end(o) : odr_missing((o), opt, name)))
209
210 #define ODR_MASK_ZERO(mask)\
211     ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
212     (mask)->top = -1))
213
214 #define ODR_MASK_SET(mask, num)\
215     (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
216     (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
217
218 #define ODR_MASK_CLEAR(mask, num)\
219     ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
220
221 #define ODR_MASK_GET(mask, num)  ( ((num) >> 3 <= (mask)->top) ? \
222     ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
223
224
225 #define odr_tell(o) ((o)->pos)
226 #define odr_offset(o) ((o)->bp - (o)->buf)
227 #define odr_ok(o) (!(o)->error)
228 #define odr_getmem(o) ((o)->mem)
229 #define odr_setmem(o, v) ((o)->mem = (v))
230
231 #define ODR_MAXNAME 256
232
233 YAZ_EXPORT int ber_boolean(ODR o, int *val);
234 YAZ_EXPORT int ber_tag(ODR o, void *p, int zclass, int tag,
235                        int *constructed, int opt, const char *name);
236 YAZ_EXPORT int ber_enctag(ODR o, int zclass, int tag, int constructed);
237 YAZ_EXPORT int ber_dectag(const unsigned char *buf, int *zclass,
238                           int *tag, int *constructed, int max);
239 YAZ_EXPORT int odr_bool(ODR o, int **p, int opt, const char *name);
240 YAZ_EXPORT int odr_integer(ODR o, int **p, int opt, const char *name);
241 YAZ_EXPORT int odr_enum(ODR o, int **p, int opt, const char *name);
242 YAZ_EXPORT int odr_implicit_settag(ODR o, int zclass, int tag);
243 YAZ_EXPORT int ber_enclen(ODR o, int len, int lenlen, int exact);
244 YAZ_EXPORT int ber_declen(const unsigned char *buf, int *len, int max);
245 YAZ_EXPORT void odr_prname(ODR o, const char *name);
246 YAZ_EXPORT int ber_null(ODR o);
247 YAZ_EXPORT int odr_null(ODR o, Odr_null **p, int opt, const char *name);
248 YAZ_EXPORT int ber_integer(ODR o, int *val);
249 YAZ_EXPORT int odr_constructed_begin(ODR o, void *p, int zclass, int tag,
250                                      const char *name);
251 YAZ_EXPORT int odr_constructed_end(ODR o);
252 YAZ_EXPORT int odr_sequence_begin(ODR o, void *p, int size, const char *name);
253 YAZ_EXPORT int odr_set_begin(ODR o, void *p, int size, const char *name);
254 YAZ_EXPORT int odr_sequence_end(ODR o);
255 YAZ_EXPORT int odr_set_end(ODR o);
256 YAZ_EXPORT int ber_octetstring(ODR o, Odr_oct *p, int cons);
257 YAZ_EXPORT int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name);
258 YAZ_EXPORT int odp_more_chunks(ODR o, const unsigned char *base, int len);
259 YAZ_EXPORT int odr_constructed_more(ODR o);
260 YAZ_EXPORT int odr_bitstring(ODR o, Odr_bitmask **p, int opt,
261                              const char *name);
262 YAZ_EXPORT int ber_bitstring(ODR o, Odr_bitmask *p, int cons);
263 YAZ_EXPORT int odr_generalstring(ODR o, char **p, int opt, const char *name);
264 YAZ_EXPORT int ber_oidc(ODR o, Odr_oid *p, int max_oid_size);
265 YAZ_EXPORT int odr_oid(ODR o, Odr_oid **p, int opt, const char *name);
266 YAZ_EXPORT int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp,
267                           const char *name);
268 YAZ_EXPORT int odr_cstring(ODR o, char **p, int opt, const char *name);
269 YAZ_EXPORT int odr_iconv_string(ODR o, char **p, int opt, const char *name);
270 YAZ_EXPORT int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num,
271                                const char *name);
272 YAZ_EXPORT int odr_set_of(ODR o, Odr_fun type, void *p, int *num,
273                           const char *name);
274 YAZ_EXPORT int odr_any(ODR o, Odr_any **p, int opt, const char *name);
275
276 YAZ_EXPORT int ber_any(ODR o, Odr_any **p);
277 /** \brief determine whether a buffer is a complete BER buffer
278     \param buf BER buffer
279     \param len length of buffer
280     \retval 0 package is incomplete
281     \retval >0 package is complete and length is return value
282 */
283 YAZ_EXPORT int completeBER(const unsigned char *buf, int len);
284
285 YAZ_EXPORT void odr_begin(ODR o);
286 YAZ_EXPORT void odr_end(ODR o);
287 YAZ_EXPORT Odr_oid *odr_oiddup(ODR odr, Odr_oid *o);
288 YAZ_EXPORT Odr_oid *odr_oiddup_nmem(NMEM nmem, Odr_oid *o);
289 YAZ_EXPORT int odr_grow_block(ODR b, int min_bytes);
290 YAZ_EXPORT int odr_write(ODR o, unsigned char *buf, int bytes);
291 YAZ_EXPORT int odr_seek(ODR o, int whence, int offset);
292 YAZ_EXPORT int odr_dumpBER(FILE *f, const char *buf, int len);
293 YAZ_EXPORT void odr_choice_bias(ODR o, int what);
294 YAZ_EXPORT void odr_choice_enable_bias(ODR o, int mode);
295 YAZ_EXPORT int odr_total(ODR o);
296 YAZ_EXPORT char *odr_errmsg(int n);
297 YAZ_EXPORT Odr_oid *odr_getoidbystr(ODR o, const char *str);
298 YAZ_EXPORT Odr_oid *odr_getoidbystr_nmem(NMEM o, const char *str);
299
300 YAZ_EXPORT int odr_initmember(ODR o, void *p, int size);
301 YAZ_EXPORT int odr_peektag(ODR o, int *zclass, int *tag, int *cons);
302 YAZ_EXPORT void odr_setlenlen(ODR o, int len);
303 YAZ_EXPORT int odr_missing(ODR o, int opt, const char *name);
304 YAZ_EXPORT char *odr_prepend(ODR o, const char *prefix, const char *old);
305
306 typedef struct Odr_external
307 {
308     Odr_oid *direct_reference;       /* OPTIONAL */
309     int     *indirect_reference;     /* OPTIONAL */
310     char    *descriptor;             /* OPTIONAL */
311     int which;
312 #define ODR_EXTERNAL_single 0
313 #define ODR_EXTERNAL_octet 1
314 #define ODR_EXTERNAL_arbitrary 2
315     union
316     {
317         Odr_any  *single_ASN1_type;
318         Odr_oct  *octet_aligned; 
319         Odr_bitmask *arbitrary;      /* we aren't really equipped for this*/
320     } u;
321 } Odr_external;
322
323 YAZ_EXPORT int odr_external(ODR o, Odr_external **p, int opt,
324                             const char *name);
325 YAZ_EXPORT int odr_visiblestring(ODR o, char **p, int opt,
326                                  const char *name);
327 YAZ_EXPORT int odr_graphicstring(ODR o, char **p, int opt,
328                                  const char *name);
329 YAZ_EXPORT int odr_generalizedtime(ODR o, char **p, int opt,
330                                    const char *name);
331
332 YAZ_EXPORT int odr_set_charset(ODR o, const char *to, const char *from);
333
334 YAZ_EXPORT void odr_set_stream(ODR o,  void *handle,
335                                void (*stream_write)(ODR o, 
336                                                     void *handle,
337                                                     int type,
338                                                     const char *buf,
339                                                     int len),
340                                void (*stream_close)(void *handle));
341
342 YAZ_EXPORT void odr_printf(ODR o, const char *fmt, ...);
343
344 YAZ_EXPORT const char **odr_get_element_path(ODR o);
345
346 YAZ_END_CDECL
347
348 #include <yaz/xmalloc.h>
349
350 #endif
351 /*
352  * Local variables:
353  * c-basic-offset: 4
354  * indent-tabs-mode: nil
355  * End:
356  * vim: shiftwidth=4 tabstop=8 expandtab
357  */
358