1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
30 * \brief Header for ODR (Open Data Representation)
39 #include <yaz/yconfig.h>
42 /* for definition of Odr_oid */
43 #include <yaz/oid_util.h>
47 typedef nmem_int_t Odr_int;
48 typedef nmem_bool_t Odr_bool;
49 #define ODR_INT_PRINTF NMEM_INT_PRINTF
59 #define ODR_IMPLICIT 0
60 #define ODR_EXPLICIT 1
65 #define ODR_UNIVERSAL 0
66 #define ODR_APPLICATION 1
75 #define ODR_BITSTRING 3
76 #define ODR_OCTETSTRING 4
80 #define ODR_EXTERNAL 8
83 #define ODR_SEQUENCE 16
85 #define ODR_NUMERICSTRING 18
86 #define ODR_PRINTABLESTRING 19
87 #define ODR_GENERALIZEDTIME 24
88 #define ODR_GRAPHICSTRING 25
89 #define ODR_VISIBLESTRING 26
90 #define ODR_GENERALSTRING 27
93 * odr stream directions
99 typedef struct odr_oct
105 typedef void Odr_null;
106 extern Odr_null *ODR_NULLVAL;
108 typedef Odr_oct Odr_any;
110 typedef struct odr_bitmask
112 #define ODR_BITMASK_SIZE 256
113 char bits[ODR_BITMASK_SIZE];
121 typedef struct odr *ODR;
123 /** ODR handle and the public structs */
126 int direction; /* the direction of this stream */
128 int error; /* current error state (0==OK) */
130 NMEM mem; /* memory handle for decoding (primarily) */
132 struct Odr_private *op;
135 typedef int (*Odr_fun)(ODR, char **, int, const char *);
137 typedef struct odr_arm
155 #define OUNEXPECTED 5
164 extern char *odr_errlist[];
166 YAZ_EXPORT int odr_offset(ODR o);
167 YAZ_EXPORT int odr_geterror(ODR o);
168 YAZ_EXPORT int odr_geterrorx(ODR o, int *x);
169 YAZ_EXPORT void odr_seterror(ODR o, int errorno, int errorid);
170 YAZ_EXPORT void odr_setelement(ODR o, const char *addinfo);
171 YAZ_EXPORT const char *odr_getelement(ODR o);
172 YAZ_EXPORT void odr_perror(ODR o, const char *message);
173 YAZ_EXPORT void odr_setprint(ODR o, FILE *file);
174 YAZ_EXPORT ODR odr_createmem(int direction);
175 YAZ_EXPORT void odr_reset(ODR o);
176 YAZ_EXPORT void odr_destroy(ODR o);
177 YAZ_EXPORT void odr_setbuf(ODR o, char *buf, int len, int can_grow);
178 YAZ_EXPORT char *odr_getbuf(ODR o, int *len, int *size);
179 YAZ_EXPORT void *odr_malloc(ODR o, size_t size);
180 YAZ_EXPORT char *odr_strdup(ODR o, const char *str);
181 YAZ_EXPORT char *odr_strdupn(ODR o, const char *str, size_t n);
182 YAZ_EXPORT char *odr_strdup_null(ODR o, const char *str);
183 YAZ_EXPORT Odr_int *odr_intdup(ODR o, Odr_int v);
184 YAZ_EXPORT Odr_bool *odr_booldup(ODR o, Odr_bool v);
185 YAZ_EXPORT Odr_oct *odr_create_Odr_oct(ODR o, const char *buf, int sz);
186 YAZ_EXPORT NMEM odr_extract_mem(ODR o);
187 YAZ_EXPORT Odr_null *odr_nullval(void);
188 #define odr_release_mem(m) nmem_destroy(m)
191 #define odr_implicit_tag(o, t, p, cl, tg, opt, name)\
192 (odr_implicit_settag((o), cl, tg), t ((o), (p), (opt), name) )
194 #define odr_explicit_tag(o, t, p, cl, tg, opt, name)\
195 ((int) (odr_constructed_begin((o), (p), (cl), (tg), 0) ? \
196 t ((o), (p), (opt), name) &&\
197 odr_constructed_end(o) : odr_missing((o), opt, name)))
199 #define ODR_MASK_ZERO(mask)\
200 ((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
203 #define ODR_MASK_SET(mask, num)\
204 (((mask)->bits[(num) >> 3] |= 0X80 >> ((num) & 0X07)),\
205 (void) ((mask)->top < (num) >> 3 ? ((mask)->top = (num) >> 3) : 0))
207 #define ODR_MASK_CLEAR(mask, num)\
208 ((mask)->bits[(num) >> 3] &= ~(0X80 >> ((num) & 0X07)))
210 #define ODR_MASK_GET(mask, num) ( ((num) >> 3 <= (mask)->top) ? \
211 ((mask)->bits[(num) >> 3] & (0X80 >> ((num) & 0X07)) ? 1 : 0) : 0)
214 #define odr_ok(o) (!(o)->error)
215 #define odr_getmem(o) ((o)->mem)
217 #define ODR_MAXNAME 256
219 YAZ_EXPORT int ber_boolean(ODR o, int *val);
220 YAZ_EXPORT int ber_tag(ODR o, void *p, int zclass, int tag,
221 int *constructed, int opt, const char *name);
222 YAZ_EXPORT int ber_enctag(ODR o, int zclass, int tag, int constructed);
223 YAZ_EXPORT int ber_dectag(const char *buf, int *zclass,
224 int *tag, int *constructed, int max);
225 YAZ_EXPORT int odr_bool(ODR o, Odr_bool **p, int opt, const char *name);
226 YAZ_EXPORT int odr_integer(ODR o, Odr_int **p, int opt, const char *name);
227 YAZ_EXPORT int odr_enum(ODR o, Odr_int **p, int opt, const char *name);
228 YAZ_EXPORT int odr_implicit_settag(ODR o, int zclass, int tag);
229 YAZ_EXPORT int ber_enclen(ODR o, int len, int lenlen, int exact);
230 YAZ_EXPORT int ber_declen(const char *buf, int *len, int max);
231 YAZ_EXPORT void odr_prname(ODR o, const char *name);
232 YAZ_EXPORT int ber_null(ODR o);
233 YAZ_EXPORT int odr_null(ODR o, Odr_null **p, int opt, const char *name);
234 YAZ_EXPORT int ber_integer(ODR o, Odr_int *val);
235 YAZ_EXPORT int odr_constructed_begin(ODR o, void *p, int zclass, int tag,
237 YAZ_EXPORT int odr_constructed_end(ODR o);
238 YAZ_EXPORT int odr_sequence_begin(ODR o, void *p, int size, const char *name);
239 YAZ_EXPORT int odr_set_begin(ODR o, void *p, int size, const char *name);
240 YAZ_EXPORT int odr_sequence_end(ODR o);
241 YAZ_EXPORT int odr_set_end(ODR o);
242 YAZ_EXPORT int ber_octetstring(ODR o, Odr_oct *p, int cons);
243 YAZ_EXPORT int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name);
244 YAZ_EXPORT int odp_more_chunks(ODR o, const char *base, int len);
245 YAZ_EXPORT int odr_constructed_more(ODR o);
246 YAZ_EXPORT int odr_bitstring(ODR o, Odr_bitmask **p, int opt,
248 YAZ_EXPORT int ber_bitstring(ODR o, Odr_bitmask *p, int cons);
249 YAZ_EXPORT int odr_generalstring(ODR o, char **p, int opt, const char *name);
250 YAZ_EXPORT int ber_oidc(ODR o, Odr_oid *p, int max_oid_size);
251 YAZ_EXPORT int odr_oid(ODR o, Odr_oid **p, int opt, const char *name);
252 YAZ_EXPORT int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp,
254 YAZ_EXPORT int odr_cstring(ODR o, char **p, int opt, const char *name);
255 YAZ_EXPORT int odr_iconv_string(ODR o, char **p, int opt, const char *name);
256 YAZ_EXPORT int odr_sequence_of(ODR o, Odr_fun type, void *p, int *num,
258 YAZ_EXPORT int odr_set_of(ODR o, Odr_fun type, void *p, int *num,
260 YAZ_EXPORT int odr_any(ODR o, Odr_any **p, int opt, const char *name);
262 YAZ_EXPORT int ber_any(ODR o, Odr_any **p);
263 /** \brief determine whether a buffer is a complete BER buffer
264 \param buf BER buffer
265 \param len length of buffer
266 \retval 0 package is incomplete
267 \retval >0 package is complete and length is return value
269 YAZ_EXPORT int completeBER(const char *buf, int len);
271 YAZ_EXPORT void odr_begin(ODR o);
272 YAZ_EXPORT void odr_end(ODR o);
273 YAZ_EXPORT Odr_oid *odr_oiddup(ODR odr, const Odr_oid *o);
274 YAZ_EXPORT Odr_oid *odr_oiddup_nmem(NMEM nmem, const Odr_oid *o);
275 YAZ_EXPORT int odr_grow_block(ODR b, int min_bytes);
276 YAZ_EXPORT int odr_write(ODR o, const char *buf, int bytes);
277 YAZ_EXPORT int odr_seek(ODR o, int whence, int offset);
278 YAZ_EXPORT int odr_dumpBER(FILE *f, const char *buf, int len);
279 YAZ_EXPORT void odr_choice_bias(ODR o, int what);
280 YAZ_EXPORT void odr_choice_enable_bias(ODR o, int mode);
281 YAZ_EXPORT size_t odr_total(ODR o);
282 YAZ_EXPORT char *odr_errmsg(int n);
283 YAZ_EXPORT Odr_oid *odr_getoidbystr(ODR o, const char *str);
284 YAZ_EXPORT Odr_oid *odr_getoidbystr_nmem(NMEM o, const char *str);
286 YAZ_EXPORT int odr_initmember(ODR o, void *p, int size);
287 YAZ_EXPORT int odr_peektag(ODR o, int *zclass, int *tag, int *cons);
288 YAZ_EXPORT void odr_setlenlen(ODR o, int len);
289 YAZ_EXPORT int odr_missing(ODR o, int opt, const char *name);
290 YAZ_EXPORT char *odr_prepend(ODR o, const char *prefix, const char *old);
292 typedef struct Odr_external
294 Odr_oid *direct_reference; /* OPTIONAL */
295 Odr_int *indirect_reference; /* OPTIONAL */
296 char *descriptor; /* OPTIONAL */
298 #define ODR_EXTERNAL_single 0
299 #define ODR_EXTERNAL_octet 1
300 #define ODR_EXTERNAL_arbitrary 2
303 Odr_any *single_ASN1_type;
304 Odr_oct *octet_aligned;
305 Odr_bitmask *arbitrary; /* we aren't really equipped for this*/
309 YAZ_EXPORT int odr_external(ODR o, Odr_external **p, int opt,
311 YAZ_EXPORT int odr_visiblestring(ODR o, char **p, int opt,
313 YAZ_EXPORT int odr_graphicstring(ODR o, char **p, int opt,
315 YAZ_EXPORT int odr_generalizedtime(ODR o, char **p, int opt,
318 YAZ_EXPORT int odr_set_charset(ODR o, const char *to, const char *from);
320 YAZ_EXPORT void odr_set_stream(ODR o, void *handle,
321 void (*stream_write)(ODR o,
326 void (*stream_close)(void *handle));
328 YAZ_EXPORT void odr_printf(ODR o, const char *fmt, ...);
330 YAZ_EXPORT const char **odr_get_element_path(ODR o);
332 YAZ_EXPORT Odr_int odr_atoi(const char *s);
334 YAZ_EXPORT Odr_int odr_strtol(const char *nptr, char **endptr, int base);
338 #include <yaz/xmalloc.h>
344 * c-file-style: "Stroustrup"
345 * indent-tabs-mode: nil
347 * vim: shiftwidth=4 tabstop=8 expandtab