2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: odr_oct.c,v 1.3 2004-08-13 07:30:06 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)
37 odr_printf(o, "OCTETSTRING(len=%d) ", (*p)->len);
39 o->op->stream_write(o, o->print, ODR_OCTETSTRING,
40 (*p)->buf, (*p)->len);
44 if (o->direction == ODR_DECODE)
46 *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)
69 o->t_class = ODR_UNIVERSAL;
70 o->t_tag = ODR_OCTETSTRING;
72 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
75 return odr_missing(o, opt, name);
76 if (o->direction == ODR_PRINT)
79 odr_printf(o, "'%s'\n", *p);
82 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
83 if (o->direction == ODR_ENCODE)
85 t->buf = (unsigned char *) *p;
86 t->size = t->len = strlen(*p);
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)
116 o->t_class = ODR_UNIVERSAL;
117 o->t_tag = ODR_OCTETSTRING;
119 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
122 return odr_missing(o, opt, name);
123 if (o->direction == ODR_PRINT)
126 odr_printf(o, "'%s'\n", *p);
129 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
130 if (o->direction == ODR_ENCODE)
134 if (o->op->iconv_handle != 0)
136 size_t inleft = strlen(*p);
138 size_t outleft = 4 * inleft + 2;
139 char *outbuf = (char *) odr_malloc (o, outleft);
142 t->buf = (unsigned char *) outbuf;
144 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
146 if (ret == (size_t)(-1))
148 odr_seterror(o, ODATA, 44);
151 t->size = t->len = outbuf - (char*) t->buf;
155 t->buf = (unsigned char *) *p;
156 t->size = t->len = strlen(*p);
165 if (!ber_octetstring(o, t, cons))
167 if (o->direction == ODR_DECODE)
171 if (o->op->iconv_handle != 0)
173 size_t inleft = t->len;
174 char *inbuf = (char *) t->buf;
175 size_t outleft = 4 * inleft + 2;
176 char *outbuf = (char *) odr_malloc (o, outleft);
181 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
183 if (ret == (size_t)(-1))
185 odr_seterror(o, ODATA, 45);
188 inleft = outbuf - (char*) *p;
190 (*p)[inleft] = '\0'; /* null terminate it */
194 *p = (char *) t->buf;
195 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */