2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: odr_oct.c,v 1.6 2004-10-15 00:19:00 adam Exp $
9 * \brief Implements ODR OCTET codec
19 * Top level octet string en/decoder.
20 * Returns 1 on success, 0 on error.
22 int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name)
30 o->t_class = ODR_UNIVERSAL;
31 o->t_tag = ODR_OCTETSTRING;
33 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
36 return odr_missing(o, opt, name);
37 if (o->direction == ODR_PRINT)
40 odr_printf(o, "OCTETSTRING(len=%d) ", (*p)->len);
42 o->op->stream_write(o, o->print, ODR_OCTETSTRING,
43 (char*) (*p)->buf, (*p)->len);
47 if (o->direction == ODR_DECODE)
49 *p = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct));
54 if (ber_octetstring(o, *p, cons))
56 odr_seterror(o, OOTHER, 43);
61 * Friendlier interface to octetstring.
63 int odr_cstring(ODR o, char **p, int opt, const char *name)
72 o->t_class = ODR_UNIVERSAL;
73 o->t_tag = ODR_OCTETSTRING;
75 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
78 return odr_missing(o, opt, name);
79 if (o->direction == ODR_PRINT)
82 odr_printf(o, "'%s'\n", *p);
85 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
86 if (o->direction == ODR_ENCODE)
88 t->buf = (unsigned char *) *p;
89 t->size = t->len = strlen(*p);
97 if (!ber_octetstring(o, t, cons))
99 if (o->direction == ODR_DECODE)
101 *p = (char *) t->buf;
102 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */
108 * iconv interface to octetstring.
110 int odr_iconv_string(ODR o, char **p, int opt, const char *name)
119 o->t_class = ODR_UNIVERSAL;
120 o->t_tag = ODR_OCTETSTRING;
122 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
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 t->size = t->len = outbuf - (char*) t->buf;
158 t->buf = (unsigned char *) *p;
159 t->size = t->len = strlen(*p);
168 if (!ber_octetstring(o, t, cons))
170 if (o->direction == ODR_DECODE)
174 if (o->op->iconv_handle != 0)
176 size_t inleft = t->len;
177 char *inbuf = (char *) t->buf;
178 size_t outleft = 4 * inleft + 2;
179 char *outbuf = (char *) odr_malloc (o, outleft);
184 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
186 if (ret == (size_t)(-1))
188 odr_seterror(o, ODATA, 45);
191 inleft = outbuf - (char*) *p;
193 (*p)[inleft] = '\0'; /* null terminate it */
197 *p = (char *) t->buf;
198 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */