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