c0dc7554f233b0f7f65e7f0920d482a10fa242df
[yaz-moved-to-github.git] / include / yaz / odr.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 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
28 /**
29  * \file odr.h
30  * \brief Header for ODR (Open Data Representation)
31  */
32
33 #ifndef ODR_H
34 #define ODR_H
35
36 #include <stdio.h>
37 #include <string.h>
38
39 #include <yaz/yconfig.h>
40 #include <yaz/nmem.h>
41
42 /* for definition of Odr_oid */
43 #include <yaz/oid_util.h>
44
45 YAZ_BEGIN_CDECL
46
47 #define ODR_INT_PRINTF "%lld"
48
49 typedef long long int odr_int_t;
50 typedef int odr_bool_t;
51
52 #ifndef bool_t
53 #define bool_t int
54 #endif
55
56 /*
57  * Tag modes
58  */
59 #define ODR_NONE -1
60 #define ODR_IMPLICIT 0
61 #define ODR_EXPLICIT 1
62
63 /*
64  * Classes
65  */
66 #define ODR_UNIVERSAL   0
67 #define ODR_APPLICATION 1
68 #define ODR_CONTEXT     2
69 #define ODR_PRIVATE     3
70
71 /*
72  * UNIVERSAL tags
73  */
74 #define ODR_BOOLEAN     1
75 #define ODR_INTEGER     2
76 #define ODR_BITSTRING   3
77 #define ODR_OCTETSTRING 4
78 #define ODR_NULL        5
79 #define ODR_OID         6
80 #define ODR_ODESC       7
81 #define ODR_EXTERNAL    8
82 #define ODR_REAL        9
83 #define ODR_ENUM        10
84 #define ODR_SEQUENCE    16
85 #define ODR_SET         17
86 #define ODR_NUMERICSTRING   18
87 #define ODR_PRINTABLESTRING 19
88 #define ODR_GENERALIZEDTIME 24
89 #define ODR_GRAPHICSTRING   25
90 #define ODR_VISIBLESTRING   26
91 #define ODR_GENERALSTRING   27
92
93 /*
94  * odr stream directions
95  */
96 #define ODR_DECODE      0
97 #define ODR_ENCODE      1
98 #define ODR_PRINT       2
99
100 typedef struct odr_oct
101 {
102     unsigned char *buf;
103     int len;
104     int size;
105 } Odr_oct;
106
107 typedef void Odr_null;
108 extern Odr_null *ODR_NULLVAL;
109
110 typedef Odr_oct Odr_any;
111
112 typedef struct odr_bitmask
113 {
114 #define ODR_BITMASK_SIZE 256
115     unsigned char bits[ODR_BITMASK_SIZE];
116     int top;
117 } Odr_bitmask;
118
119 #define ODR_S_SET     0
120 #define ODR_S_CUR     1
121 #define ODR_S_END     2
122
123 typedef struct odr *ODR;
124
125 /** ODR handle and the public structs */
126 struct odr
127 {
128     int direction;       /* the direction of this stream */
129
130     int error;            /* current error state (0==OK) */
131
132     unsigned char *buf;            /* memory handle */
133     int top;              /* top of buffer (max pos when encoding) */
134     int size;             /* current buffer size (encoding+decoding) */
135
136     int pos;              /* current position (encoding) */
137
138     const unsigned char *bp; /* position in buffer (decoding) */
139
140     NMEM mem;            /* memory handle for decoding (primarily) */
141
142     struct Odr_private *op;
143 };
144
145 typedef int (*Odr_fun)(ODR, char **, int, const char *);
146
147 typedef struct odr_arm
148 {
149     int tagmode;
150     int zclass;
151     int tag;
152     int which;
153     Odr_fun fun;
154     char *name;
155 } Odr_arm;
156
157 /*
158  * Error control.
159  */
160 #define ONONE           0
161 #define OMEMORY         1
162 #define OSYSERR         2
163 #define OSPACE          3
164 #define OREQUIRED       4
165 #define OUNEXPECTED     5
166 #define OOTHER          6
167 #define OPROTO          7
168 #define ODATA           8
169 #define OSTACK          9
170 #define OCONLEN        10
171 #define OLENOV         11
172 #define OHTTP          12
173
174 extern char *odr_errlist[];
175
176 YAZ_EXPORT int odr_geterror(ODR o);
177 YAZ_EXPORT int odr_geterrorx(ODR o, int *x);
178 YAZ_EXPORT void odr_seterror(ODR o, int errorno, int errorid);
179 YAZ_EXPORT void odr_setelement(ODR o, const char *addinfo);
180 YAZ_EXPORT const char *odr_getelement(ODR o);
181 YAZ_EXPORT void odr_perror(ODR o, const char *message);
182 YAZ_EXPORT void odr_setprint(ODR o, FILE *file);
183 YAZ_EXPORT ODR odr_createmem(int direction);
184 YAZ_EXPORT void odr_reset(ODR o);
185 YAZ_EXPORT void odr_destroy(ODR o);
186 YAZ_EXPORT void odr_setbuf(ODR o, char *buf, int len, int can_grow);
187 YAZ_EXPORT char *odr_getbuf(ODR o, int *len, int *size);
188 YAZ_EXPORT void *odr_malloc(ODR o, int size);
189 YAZ_EXPORT char *odr_strdup(ODR o, const char *str);
190 YAZ_EXPORT char *odr_strdupn(ODR o, const char *str, size_t n);
191 YAZ_EXPORT char *odr_strdup_null(ODR o, const char *str);
192 YAZ_EXPORT odr_int_t *odr_intdup(ODR o, odr_int_t v);
193 YAZ_EXPORT odr_bool_t *odr_booldup(ODR o, odr_bool_t v);
194 YAZ_EXPORT Odr_oct *odr_create_Odr_oct(ODR o, const unsigned char *buf,
195                                        int sz);
196 YAZ_EXPORT NMEM odr_extract_mem(ODR o);
197 YAZ_EXPORT Odr_null *odr_nullval(void);
198 #define odr_release_mem(m) nmem_destroy(m)
199 #define ODR_MEM NMEM
200
201 #define odr_implicit_tag(o, t, p, cl, tg, opt, name)\
202         (odr_implicit_settag((o), cl, tg), t ((o), (p), (opt), name) )
203
204 #define odr_explicit_tag(o, t, p, cl, tg, opt, name)\
205         ((int) (odr_constructed_begin((o), (p), (cl), (tg), 0) ? \
206         t ((o), (p), (opt), name) &&\
207         odr_constructed_end(o) : odr_missing((o), opt, name)))
208
209 #define ODR_MASK_ZERO(mask)\
210     ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
211     (mask)->top = -1))
212
213 #define ODR_MASK_SET(mask, num)\
214     (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
215     (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
216
217 #define ODR_MASK_CLEAR(mask, num)\
218     ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
219
220 #define ODR_MASK_GET(mask, num)  ( ((num) >> 3 <= (mask)->top) ? \
221     ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
222
223
224 #define odr_tell(o) ((o)->pos)
225 #define odr_offset(o) ((o)->bp - (o)->buf)
226 #define odr_ok(o) (!(o)->error)
227 #define odr_getmem(o) ((o)->mem)
228
229 #define ODR_MAXNAME 256
230
231 YAZ_EXPORT int ber_boolean(ODR o, int *val);
232 YAZ_EXPORT int ber_tag(ODR o, void *p, int zclass, int tag,
233                        int *constructed, int opt, const char *name);
234 YAZ_EXPORT int ber_enctag(ODR o, int zclass, int tag, int constructed);
235 YAZ_EXPORT int ber_dectag(const unsigned char *buf, int *zclass,
236                           int *tag, int *constructed, int max);
237 YAZ_EXPORT int odr_bool(ODR o, int **p, int opt, const char *name);
238 YAZ_EXPORT int odr_integer(ODR o, odr_int_t **p, int opt, const char *name);
239 YAZ_EXPORT int odr_enum(ODR o, odr_int_t **p, int opt, const char *name);
240 YAZ_EXPORT int odr_implicit_settag(ODR o, int zclass, int tag);
241 YAZ_EXPORT int ber_enclen(ODR o, int len, int lenlen, int exact);
242 YAZ_EXPORT int ber_declen(const unsigned char *buf, int *len, int max);
243 YAZ_EXPORT void odr_prname(ODR o, const char *name);
244 YAZ_EXPORT int ber_null(ODR o);
245 YAZ_EXPORT int odr_null(ODR o, Odr_null **p, int opt, const char *name);
246 YAZ_EXPORT int ber_integer(ODR o, odr_int_t *val);
247 YAZ_EXPORT int odr_constructed_begin(ODR o, void *p, int zclass, int tag,
248                                      const char *name);
249 YAZ_EXPORT int odr_constructed_end(ODR o);
250 YAZ_EXPORT int odr_sequence_begin(ODR o, void *p, int size, const char *name);
251 YAZ_EXPORT int odr_set_begin(ODR o, void *p, int size, const char *name);
252 YAZ_EXPORT int odr_sequence_end(ODR o);
253 YAZ_EXPORT int odr_set_end(ODR o);
254 YAZ_EXPORT int ber_octetstring(ODR o, Odr_oct *p, int cons);
255 YAZ_EXPORT int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name);
256 YAZ_EXPORT int odp_more_chunks(ODR o, const unsigned char *base, int len);
257 YAZ_EXPORT int odr_constructed_more(ODR o);
258 YAZ_EXPORT int odr_bitstring(ODR o, Odr_bitmask **p, int opt,
259                              const char *name);
260 YAZ_EXPORT int ber_bitstring(ODR o, Odr_bitmask *p, int cons);
261 YAZ_EXPORT int odr_generalstring(ODR o, char **p, int opt, const char *name);
262 YAZ_EXPORT int ber_oidc(ODR o, Odr_oid *p, int max_oid_size);
263 YAZ_EXPORT int odr_oid(ODR o, Odr_oid **p, int opt, const char *name);
264 YAZ_EXPORT int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp,
265                           const char *name);
266 YAZ_EXPORT int odr_cstring(ODR o, char **p, int opt, const char *name);
267 YAZ_EXPORT int odr_iconv_string(ODR o, char **p, int opt, const char *name);
268 YAZ_EXPORT int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num,
269                                const char *name);
270 YAZ_EXPORT int odr_set_of(ODR o, Odr_fun type, void *p, int *num,
271                           const char *name);
272 YAZ_EXPORT int odr_any(ODR o, Odr_any **p, int opt, const char *name);
273
274 YAZ_EXPORT int ber_any(ODR o, Odr_any **p);
275 /** \brief determine whether a buffer is a complete BER buffer
276     \param buf BER buffer
277     \param len length of buffer
278     \retval 0 package is incomplete
279     \retval >0 package is complete and length is return value
280 */
281 YAZ_EXPORT int completeBER(const unsigned char *buf, int len);
282
283 YAZ_EXPORT void odr_begin(ODR o);
284 YAZ_EXPORT void odr_end(ODR o);
285 YAZ_EXPORT Odr_oid *odr_oiddup(ODR odr, const Odr_oid *o);
286 YAZ_EXPORT Odr_oid *odr_oiddup_nmem(NMEM nmem, const Odr_oid *o);
287 YAZ_EXPORT int odr_grow_block(ODR b, int min_bytes);
288 YAZ_EXPORT int odr_write(ODR o, unsigned char *buf, int bytes);
289 YAZ_EXPORT int odr_seek(ODR o, int whence, int offset);
290 YAZ_EXPORT int odr_dumpBER(FILE *f, const char *buf, int len);
291 YAZ_EXPORT void odr_choice_bias(ODR o, int what);
292 YAZ_EXPORT void odr_choice_enable_bias(ODR o, int mode);
293 YAZ_EXPORT int odr_total(ODR o);
294 YAZ_EXPORT char *odr_errmsg(int n);
295 YAZ_EXPORT Odr_oid *odr_getoidbystr(ODR o, const char *str);
296 YAZ_EXPORT Odr_oid *odr_getoidbystr_nmem(NMEM o, const char *str);
297
298 YAZ_EXPORT int odr_initmember(ODR o, void *p, int size);
299 YAZ_EXPORT int odr_peektag(ODR o, int *zclass, int *tag, int *cons);
300 YAZ_EXPORT void odr_setlenlen(ODR o, int len);
301 YAZ_EXPORT int odr_missing(ODR o, int opt, const char *name);
302 YAZ_EXPORT char *odr_prepend(ODR o, const char *prefix, const char *old);
303
304 typedef struct Odr_external
305 {
306     Odr_oid *direct_reference;       /* OPTIONAL */
307     odr_int_t *indirect_reference;   /* OPTIONAL */
308     char    *descriptor;             /* OPTIONAL */
309     int which;
310 #define ODR_EXTERNAL_single 0
311 #define ODR_EXTERNAL_octet 1
312 #define ODR_EXTERNAL_arbitrary 2
313     union
314     {
315         Odr_any  *single_ASN1_type;
316         Odr_oct  *octet_aligned; 
317         Odr_bitmask *arbitrary;      /* we aren't really equipped for this*/
318     } u;
319 } Odr_external;
320
321 YAZ_EXPORT int odr_external(ODR o, Odr_external **p, int opt,
322                             const char *name);
323 YAZ_EXPORT int odr_visiblestring(ODR o, char **p, int opt,
324                                  const char *name);
325 YAZ_EXPORT int odr_graphicstring(ODR o, char **p, int opt,
326                                  const char *name);
327 YAZ_EXPORT int odr_generalizedtime(ODR o, char **p, int opt,
328                                    const char *name);
329
330 YAZ_EXPORT int odr_set_charset(ODR o, const char *to, const char *from);
331
332 YAZ_EXPORT void odr_set_stream(ODR o,  void *handle,
333                                void (*stream_write)(ODR o, 
334                                                     void *handle,
335                                                     int type,
336                                                     const char *buf,
337                                                     int len),
338                                void (*stream_close)(void *handle));
339
340 YAZ_EXPORT void odr_printf(ODR o, const char *fmt, ...);
341
342 YAZ_EXPORT const char **odr_get_element_path(ODR o);
343
344 YAZ_END_CDECL
345
346 #include <yaz/xmalloc.h>
347
348 #endif
349 /*
350  * Local variables:
351  * c-basic-offset: 4
352  * c-file-style: "Stroustrup"
353  * indent-tabs-mode: nil
354  * End:
355  * vim: shiftwidth=4 tabstop=8 expandtab
356  */
357