2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: odr_oct.c,v 1.5 2004-10-02 13:28:26 adam Exp $
15 * Top level octet string en/decoder.
16 * Returns 1 on success, 0 on error.
18 int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name)
26 o->t_class = ODR_UNIVERSAL;
27 o->t_tag = ODR_OCTETSTRING;
29 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
32 return odr_missing(o, opt, name);
33 if (o->direction == ODR_PRINT)
36 odr_printf(o, "OCTETSTRING(len=%d) ", (*p)->len);
38 o->op->stream_write(o, o->print, ODR_OCTETSTRING,
39 (char*) (*p)->buf, (*p)->len);
43 if (o->direction == ODR_DECODE)
45 *p = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct));
50 if (ber_octetstring(o, *p, cons))
52 odr_seterror(o, OOTHER, 43);
57 * Friendlier interface to octetstring.
59 int odr_cstring(ODR o, char **p, int opt, const char *name)
68 o->t_class = ODR_UNIVERSAL;
69 o->t_tag = ODR_OCTETSTRING;
71 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
74 return odr_missing(o, opt, name);
75 if (o->direction == ODR_PRINT)
78 odr_printf(o, "'%s'\n", *p);
81 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
82 if (o->direction == ODR_ENCODE)
84 t->buf = (unsigned char *) *p;
85 t->size = t->len = strlen(*p);
93 if (!ber_octetstring(o, t, cons))
95 if (o->direction == ODR_DECODE)
98 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */
104 * iconv interface to octetstring.
106 int odr_iconv_string(ODR o, char **p, int opt, const char *name)
115 o->t_class = ODR_UNIVERSAL;
116 o->t_tag = ODR_OCTETSTRING;
118 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
121 return odr_missing(o, opt, name);
122 if (o->direction == ODR_PRINT)
125 odr_printf(o, "'%s'\n", *p);
128 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
129 if (o->direction == ODR_ENCODE)
133 if (o->op->iconv_handle != 0)
135 size_t inleft = strlen(*p);
137 size_t outleft = 4 * inleft + 2;
138 char *outbuf = (char *) odr_malloc (o, outleft);
141 t->buf = (unsigned char *) outbuf;
143 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
145 if (ret == (size_t)(-1))
147 odr_seterror(o, ODATA, 44);
150 t->size = t->len = outbuf - (char*) t->buf;
154 t->buf = (unsigned char *) *p;
155 t->size = t->len = strlen(*p);
164 if (!ber_octetstring(o, t, cons))
166 if (o->direction == ODR_DECODE)
170 if (o->op->iconv_handle != 0)
172 size_t inleft = t->len;
173 char *inbuf = (char *) t->buf;
174 size_t outleft = 4 * inleft + 2;
175 char *outbuf = (char *) odr_malloc (o, outleft);
180 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
182 if (ret == (size_t)(-1))
184 odr_seterror(o, ODATA, 45);
187 inleft = outbuf - (char*) *p;
189 (*p)[inleft] = '\0'; /* null terminate it */
193 *p = (char *) t->buf;
194 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */