1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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));
51 if (ber_octetstring(o, *p, cons))
53 odr_seterror(o, OOTHER, 43);
58 * Friendlier interface to octetstring.
60 int odr_cstring(ODR o, char **p, int opt, const char *name)
67 if (o->op->t_class < 0)
69 o->op->t_class = ODR_UNIVERSAL;
70 o->op->t_tag = ODR_OCTETSTRING;
72 res = ber_tag(o, p, o->op->t_class, o->op->t_tag, &cons, opt, name);
76 return odr_missing(o, opt, name);
77 if (o->direction == ODR_PRINT)
80 odr_printf(o, "'%s'\n", *p);
83 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
84 if (o->direction == ODR_ENCODE)
94 if (!ber_octetstring(o, t, cons))
96 if (o->direction == ODR_DECODE)
99 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */
105 * iconv interface to octetstring.
107 int odr_iconv_string(ODR o, char **p, int opt, const char *name)
114 if (o->op->t_class < 0)
116 o->op->t_class = ODR_UNIVERSAL;
117 o->op->t_tag = ODR_OCTETSTRING;
119 res = ber_tag(o, p, o->op->t_class, o->op->t_tag, &cons, opt, name);
123 return odr_missing(o, opt, name);
124 if (o->direction == ODR_PRINT)
127 odr_printf(o, "'%s'\n", *p);
130 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
131 if (o->direction == ODR_ENCODE)
135 if (o->op->iconv_handle != 0)
137 size_t inleft = strlen(*p);
139 size_t outleft = 4 * inleft + 2;
140 char *outbuf = (char *) odr_malloc (o, outleft);
145 ret = yaz_iconv(o->op->iconv_handle, &inbuf, &inleft,
147 if (ret == (size_t)(-1))
149 odr_seterror(o, ODATA, 44);
152 ret = yaz_iconv(o->op->iconv_handle, 0, 0,
155 if (ret == (size_t)(-1))
157 odr_seterror(o, ODATA, 44);
160 t->len = outbuf - (char*) t->buf;
173 if (!ber_octetstring(o, t, cons))
175 if (o->direction == ODR_DECODE)
179 if (o->op->iconv_handle != 0)
181 size_t inleft = t->len;
182 char *inbuf = (char *) t->buf;
183 size_t outleft = 4 * inleft + 2;
184 char *outbuf = (char *) odr_malloc (o, outleft);
189 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
191 if (ret == (size_t)(-1))
193 odr_seterror(o, ODATA, 45);
196 ret = yaz_iconv(o->op->iconv_handle, 0, 0,
198 if (ret == (size_t)(-1))
200 odr_seterror(o, ODATA, 45);
203 inleft = outbuf - (char*) *p;
205 (*p)[inleft] = '\0'; /* null terminate it */
209 *p = (char *) t->buf;
210 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */
218 * c-file-style: "Stroustrup"
219 * indent-tabs-mode: nil
221 * vim: shiftwidth=4 tabstop=8 expandtab