2 * Copyright (c) 1995-2003, Index Data.
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,
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.
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.
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
26 * $Id: odr.h,v 1.10 2003-03-18 13:34:35 adam Exp $
35 #include <yaz/yconfig.h>
48 #define ODR_IMPLICIT 0
49 #define ODR_EXPLICIT 1
54 #define ODR_UNIVERSAL 0
55 #define ODR_APPLICATION 1
64 #define ODR_BITSTRING 3
65 #define ODR_OCTETSTRING 4
69 #define ODR_EXTERNAL 8
72 #define ODR_SEQUENCE 16
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
82 * odr stream directions
88 typedef struct odr_oct
95 typedef void Odr_null;
96 extern Odr_null *ODR_NULLVAL;
98 typedef Odr_oct Odr_any;
100 typedef struct odr_bitmask
102 #define ODR_BITMASK_SIZE 256
103 unsigned char bits[ODR_BITMASK_SIZE];
107 typedef int Odr_oid; /* terminate by -1 */
109 typedef struct odr_constack
111 const unsigned char *base; /* starting point of data */
113 int len; /* length of data, if known, else -1
115 const unsigned char *lenb; /* where to encode length */
117 int lenlen; /* length of length-field */
126 int direction; /* the direction of this stream */
128 int error; /* current error state (0==OK) */
130 int can_grow; /* are we allowed to reallocate */
131 unsigned char *buf; /* memory handle */
132 int size; /* current buffer size */
134 int pos; /* current position */
135 int top; /* top of buffer (max pos when decoding) */
137 const unsigned char *bp; /* position in buffer (decoding) */
139 int t_class; /* implicit tagging (-1==default tag) */
142 int enable_bias; /* force choice enable flag */
143 int choice_bias; /* force choice */
144 int lenlen; /* force length-of-lenght (odr_setlen()) */
146 FILE *print; /* output file for direction print */
147 int indent; /* current indent level for printing */
149 NMEM mem; /* memory handle for decoding (primarily) */
151 struct Odr_private *op;
154 typedef int (*Odr_fun)(ODR, char **, int, const char *);
156 typedef struct odr_arm
174 #define OUNEXPECTED 5
183 extern char *odr_errlist[];
185 YAZ_EXPORT int odr_geterror(ODR o);
186 YAZ_EXPORT int odr_geterrorx(ODR o, int *x);
187 YAZ_EXPORT void odr_seterror(ODR o, int errorno, int errorid);
188 YAZ_EXPORT void odr_perror(ODR o, char *message);
189 YAZ_EXPORT void odr_setprint(ODR o, FILE *file);
190 YAZ_EXPORT ODR odr_createmem(int direction);
191 YAZ_EXPORT void odr_reset(ODR o);
192 YAZ_EXPORT void odr_destroy(ODR o);
193 YAZ_EXPORT void odr_setbuf(ODR o, char *buf, int len, int can_grow);
194 YAZ_EXPORT char *odr_getbuf(ODR o, int *len, int *size);
195 YAZ_EXPORT void *odr_malloc(ODR o, int size);
196 YAZ_EXPORT char *odr_strdup(ODR o, const char *str);
197 YAZ_EXPORT char *odr_strdupn(ODR o, const char *str, size_t n);
198 YAZ_EXPORT int *odr_intdup(ODR o, int v);
199 YAZ_EXPORT NMEM odr_extract_mem(ODR o);
200 YAZ_EXPORT Odr_null *odr_nullval(void);
201 #define odr_release_mem(m) nmem_destroy(m)
204 #define odr_implicit(o, t, p, cl, tg, opt)\
205 (odr_implicit_settag((o), cl, tg), t ((o), (p), (opt), 0) )
207 #define odr_implicit_tag(o, t, p, cl, tg, opt, name)\
208 (odr_implicit_settag((o), cl, tg), t ((o), (p), (opt), name) )
210 #define odr_explicit(o, t, p, cl, tg, opt)\
211 ((int) (odr_constructed_begin((o), (p), (cl), (tg), 0) ? \
212 t ((o), (p), (opt), 0) &&\
213 odr_constructed_end(o) : opt))
215 #define odr_explicit_tag(o, t, p, cl, tg, opt, name)\
216 ((int) (odr_constructed_begin((o), (p), (cl), (tg), 0) ? \
217 t ((o), (p), (opt), name) &&\
218 odr_constructed_end(o) : opt))
220 #define ODR_MASK_ZERO(mask)\
221 ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
224 #define ODR_MASK_SET(mask, num)\
225 (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
226 (mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0)
228 #define ODR_MASK_CLEAR(mask, num)\
229 ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
231 #define ODR_MASK_GET(mask, num) ( ((num) >> 3 <= (mask)->top) ? \
232 ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
235 #define odr_tell(o) ((o)->pos)
236 #define odr_offset(o) ((o)->bp - (o)->buf)
237 #define odr_ok(o) (!(o)->error)
238 #define odr_getmem(o) ((o)->mem)
239 #define odr_setmem(o, v) ((o)->mem = (v))
241 #define ODR_MAXNAME 256
243 YAZ_EXPORT int ber_boolean(ODR o, int *val);
244 YAZ_EXPORT int ber_tag(ODR o, void *p, int zclass, int tag,
245 int *constructed, int opt);
246 YAZ_EXPORT int ber_enctag(ODR o, int zclass, int tag, int constructed);
247 YAZ_EXPORT int ber_dectag(const unsigned char *buf, int *zclass,
248 int *tag, int *constructed, int max);
249 YAZ_EXPORT int odr_bool(ODR o, int **p, int opt, const char *name);
250 YAZ_EXPORT int odr_integer(ODR o, int **p, int opt, const char *name);
251 YAZ_EXPORT int odr_enum(ODR o, int **p, int opt, const char *name);
252 YAZ_EXPORT int odr_implicit_settag(ODR o, int zclass, int tag);
253 YAZ_EXPORT int ber_enclen(ODR o, int len, int lenlen, int exact);
254 YAZ_EXPORT int ber_declen(const unsigned char *buf, int *len, int max);
255 YAZ_EXPORT void odr_prname(ODR o, const char *name);
256 YAZ_EXPORT int ber_null(ODR o);
257 YAZ_EXPORT int odr_null(ODR o, Odr_null **p, int opt, const char *name);
258 YAZ_EXPORT int ber_integer(ODR o, int *val);
259 YAZ_EXPORT int odr_constructed_begin(ODR o, void *p, int zclass, int tag,
261 YAZ_EXPORT int odr_constructed_end(ODR o);
262 YAZ_EXPORT int odr_sequence_begin(ODR o, void *p, int size, const char *name);
263 YAZ_EXPORT int odr_set_begin(ODR o, void *p, int size, const char *name);
264 YAZ_EXPORT int odr_sequence_end(ODR o);
265 YAZ_EXPORT int odr_set_end(ODR o);
266 YAZ_EXPORT int ber_octetstring(ODR o, Odr_oct *p, int cons);
267 YAZ_EXPORT int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name);
268 YAZ_EXPORT int odp_more_chunks(ODR o, const unsigned char *base, int len);
269 YAZ_EXPORT int odr_constructed_more(ODR o);
270 YAZ_EXPORT int odr_bitstring(ODR o, Odr_bitmask **p, int opt,
272 YAZ_EXPORT int ber_bitstring(ODR o, Odr_bitmask *p, int cons);
273 YAZ_EXPORT int odr_generalstring(ODR o, char **p, int opt, const char *name);
274 YAZ_EXPORT int ber_oidc(ODR o, Odr_oid *p);
275 YAZ_EXPORT int odr_oid(ODR o, Odr_oid **p, int opt, const char *name);
276 YAZ_EXPORT int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp,
278 YAZ_EXPORT int odr_cstring(ODR o, char **p, int opt, const char *name);
279 YAZ_EXPORT int odr_iconv_string(ODR o, char **p, int opt, const char *name);
280 YAZ_EXPORT int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num,
282 YAZ_EXPORT int odr_set_of(ODR o, Odr_fun type, void *p, int *num,
284 YAZ_EXPORT int odr_any(ODR o, Odr_any **p, int opt, const char *name);
285 YAZ_EXPORT int ber_any(ODR o, Odr_any **p);
286 YAZ_EXPORT int completeBER(const unsigned char *buf, int len);
287 YAZ_EXPORT void odr_begin(ODR o);
288 YAZ_EXPORT void odr_end(ODR o);
289 YAZ_EXPORT Odr_oid *odr_oiddup(ODR odr, Odr_oid *o);
290 YAZ_EXPORT Odr_oid *odr_oiddup_nmem(NMEM nmem, Odr_oid *o);
291 YAZ_EXPORT int odr_grow_block(ODR b, int min_bytes);
292 YAZ_EXPORT int odr_write(ODR o, unsigned char *buf, int bytes);
293 YAZ_EXPORT int odr_seek(ODR o, int whence, int offset);
294 YAZ_EXPORT int odr_dumpBER(FILE *f, const char *buf, int len);
295 YAZ_EXPORT void odr_choice_bias(ODR o, int what);
296 YAZ_EXPORT void odr_choice_enable_bias(ODR o, int mode);
297 YAZ_EXPORT int odr_total(ODR o);
298 YAZ_EXPORT char *odr_errmsg(int n);
299 YAZ_EXPORT Odr_oid *odr_getoidbystr(ODR o, const char *str);
300 YAZ_EXPORT Odr_oid *odr_getoidbystr_nmem(NMEM o, const char *str);
302 YAZ_EXPORT int odr_initmember(ODR o, void *p, int size);
303 YAZ_EXPORT int odr_peektag(ODR o, int *zclass, int *tag, int *cons);
304 YAZ_EXPORT void odr_setlenlen(ODR o, int len);
306 typedef struct Odr_external
308 Odr_oid *direct_reference; /* OPTIONAL */
309 int *indirect_reference; /* OPTIONAL */
310 char *descriptor; /* OPTIONAL */
312 #define ODR_EXTERNAL_single 0
313 #define ODR_EXTERNAL_octet 1
314 #define ODR_EXTERNAL_arbitrary 2
317 Odr_any *single_ASN1_type;
318 Odr_oct *octet_aligned;
319 Odr_bitmask *arbitrary; /* we aren't really equipped for this*/
323 YAZ_EXPORT int odr_external(ODR o, Odr_external **p, int opt,
325 YAZ_EXPORT int odr_visiblestring(ODR o, char **p, int opt,
327 YAZ_EXPORT int odr_graphicstring(ODR o, char **p, int opt,
329 YAZ_EXPORT int odr_generalizedtime(ODR o, char **p, int opt,
332 YAZ_EXPORT int odr_set_charset(ODR o, const char *to, const char *from);
336 #include <yaz/xmalloc.h>