1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ODR OCTET codec
16 * Top level octet string en/decoder.
17 * Returns 1 on success, 0 on error.
19 int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name)
25 if (o->op->t_class < 0)
27 o->op->t_class = ODR_UNIVERSAL;
28 o->op->t_tag = ODR_OCTETSTRING;
30 res = ber_tag(o, p, o->op->t_class, o->op->t_tag, &cons, opt, name);
34 return odr_missing(o, opt, name);
35 if (o->direction == ODR_PRINT)
38 odr_printf(o, "OCTETSTRING(len=%d) ", (*p)->len);
40 o->op->stream_write(o, o->op->print, ODR_OCTETSTRING,
41 (char*) (*p)->buf, (*p)->len);
45 if (o->direction == ODR_DECODE)
47 *p = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct));
52 if (ber_octetstring(o, *p, cons))
54 odr_seterror(o, OOTHER, 43);
59 * Friendlier interface to octetstring.
61 int odr_cstring(ODR o, char **p, int opt, const char *name)
68 if (o->op->t_class < 0)
70 o->op->t_class = ODR_UNIVERSAL;
71 o->op->t_tag = ODR_OCTETSTRING;
73 res = ber_tag(o, p, o->op->t_class, o->op->t_tag, &cons, opt, name);
77 return odr_missing(o, opt, name);
78 if (o->direction == ODR_PRINT)
81 odr_printf(o, "'%s'\n", *p);
84 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
85 if (o->direction == ODR_ENCODE)
87 t->buf = (unsigned char *) *p;
88 t->size = t->len = strlen(*p);
96 if (!ber_octetstring(o, t, cons))
98 if (o->direction == ODR_DECODE)
100 *p = (char *) t->buf;
101 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */
107 * iconv interface to octetstring.
109 int odr_iconv_string(ODR o, char **p, int opt, const char *name)
116 if (o->op->t_class < 0)
118 o->op->t_class = ODR_UNIVERSAL;
119 o->op->t_tag = ODR_OCTETSTRING;
121 res = ber_tag(o, p, o->op->t_class, o->op->t_tag, &cons, opt, name);
125 return odr_missing(o, opt, name);
126 if (o->direction == ODR_PRINT)
129 odr_printf(o, "'%s'\n", *p);
132 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
133 if (o->direction == ODR_ENCODE)
137 if (o->op->iconv_handle != 0)
139 size_t inleft = strlen(*p);
141 size_t outleft = 4 * inleft + 2;
142 char *outbuf = (char *) odr_malloc (o, outleft);
145 t->buf = (unsigned char *) outbuf;
147 ret = yaz_iconv(o->op->iconv_handle, &inbuf, &inleft,
149 if (ret == (size_t)(-1))
151 odr_seterror(o, ODATA, 44);
154 ret = yaz_iconv(o->op->iconv_handle, 0, 0,
157 if (ret == (size_t)(-1))
159 odr_seterror(o, ODATA, 44);
162 t->size = t->len = outbuf - (char*) t->buf;
166 t->buf = (unsigned char *) *p;
167 t->size = t->len = strlen(*p);
176 if (!ber_octetstring(o, t, cons))
178 if (o->direction == ODR_DECODE)
182 if (o->op->iconv_handle != 0)
184 size_t inleft = t->len;
185 char *inbuf = (char *) t->buf;
186 size_t outleft = 4 * inleft + 2;
187 char *outbuf = (char *) odr_malloc (o, outleft);
192 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
194 if (ret == (size_t)(-1))
196 odr_seterror(o, ODATA, 45);
199 ret = yaz_iconv(o->op->iconv_handle, 0, 0,
201 if (ret == (size_t)(-1))
203 odr_seterror(o, ODATA, 45);
206 inleft = outbuf - (char*) *p;
208 (*p)[inleft] = '\0'; /* null terminate it */
212 *p = (char *) t->buf;
213 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */
221 * c-file-style: "Stroustrup"
222 * indent-tabs-mode: nil
224 * vim: shiftwidth=4 tabstop=8 expandtab