2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: odr_oct.c,v 1.13 2007-05-21 11:51:01 adam Exp $
9 * \brief Implements ODR OCTET codec
18 * Top level octet string en/decoder.
19 * Returns 1 on success, 0 on error.
21 int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name)
27 if (o->op->t_class < 0)
29 o->op->t_class = ODR_UNIVERSAL;
30 o->op->t_tag = ODR_OCTETSTRING;
32 res = ber_tag(o, p, o->op->t_class, o->op->t_tag, &cons, opt, name);
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->op->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)
70 if (o->op->t_class < 0)
72 o->op->t_class = ODR_UNIVERSAL;
73 o->op->t_tag = ODR_OCTETSTRING;
75 res = ber_tag(o, p, o->op->t_class, o->op->t_tag, &cons, opt, name);
79 return odr_missing(o, opt, name);
80 if (o->direction == ODR_PRINT)
83 odr_printf(o, "'%s'\n", *p);
86 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
87 if (o->direction == ODR_ENCODE)
89 t->buf = (unsigned char *) *p;
90 t->size = t->len = strlen(*p);
98 if (!ber_octetstring(o, t, cons))
100 if (o->direction == ODR_DECODE)
102 *p = (char *) t->buf;
103 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */
109 * iconv interface to octetstring.
111 int odr_iconv_string(ODR o, char **p, int opt, const char *name)
118 if (o->op->t_class < 0)
120 o->op->t_class = ODR_UNIVERSAL;
121 o->op->t_tag = ODR_OCTETSTRING;
123 res = ber_tag(o, p, o->op->t_class, o->op->t_tag, &cons, opt, name);
127 return odr_missing(o, opt, name);
128 if (o->direction == ODR_PRINT)
131 odr_printf(o, "'%s'\n", *p);
134 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
135 if (o->direction == ODR_ENCODE)
139 if (o->op->iconv_handle != 0)
141 size_t inleft = strlen(*p);
143 size_t outleft = 4 * inleft + 2;
144 char *outbuf = (char *) odr_malloc (o, outleft);
147 t->buf = (unsigned char *) outbuf;
149 ret = yaz_iconv(o->op->iconv_handle, &inbuf, &inleft,
151 if (ret == (size_t)(-1))
153 odr_seterror(o, ODATA, 44);
156 ret = yaz_iconv(o->op->iconv_handle, 0, 0,
159 if (ret == (size_t)(-1))
161 odr_seterror(o, ODATA, 44);
164 t->size = t->len = outbuf - (char*) t->buf;
168 t->buf = (unsigned char *) *p;
169 t->size = t->len = strlen(*p);
178 if (!ber_octetstring(o, t, cons))
180 if (o->direction == ODR_DECODE)
184 if (o->op->iconv_handle != 0)
186 size_t inleft = t->len;
187 char *inbuf = (char *) t->buf;
188 size_t outleft = 4 * inleft + 2;
189 char *outbuf = (char *) odr_malloc (o, outleft);
194 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
196 if (ret == (size_t)(-1))
198 odr_seterror(o, ODATA, 45);
201 ret = yaz_iconv(o->op->iconv_handle, 0, 0,
203 if (ret == (size_t)(-1))
205 odr_seterror(o, ODATA, 45);
208 inleft = outbuf - (char*) *p;
210 (*p)[inleft] = '\0'; /* null terminate it */
214 *p = (char *) t->buf;
215 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */
223 * indent-tabs-mode: nil
225 * vim: shiftwidth=4 tabstop=8 expandtab