2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: odr_oct.c,v 1.9 2005-06-25 15:46:04 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)
29 o->t_class = ODR_UNIVERSAL;
30 o->t_tag = ODR_OCTETSTRING;
32 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
35 return odr_missing(o, opt, name);
36 if (o->direction == ODR_PRINT)
39 odr_printf(o, "OCTETSTRING(len=%d) ", (*p)->len);
41 o->op->stream_write(o, o->print, ODR_OCTETSTRING,
42 (char*) (*p)->buf, (*p)->len);
46 if (o->direction == ODR_DECODE)
48 *p = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct));
53 if (ber_octetstring(o, *p, cons))
55 odr_seterror(o, OOTHER, 43);
60 * Friendlier interface to octetstring.
62 int odr_cstring(ODR o, char **p, int opt, const char *name)
71 o->t_class = ODR_UNIVERSAL;
72 o->t_tag = ODR_OCTETSTRING;
74 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
77 return odr_missing(o, opt, name);
78 if (o->direction == ODR_PRINT)
81 odr_printf(o, "'%s'\n", *p);
84 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
85 if (o->direction == ODR_ENCODE)
87 t->buf = (unsigned char *) *p;
88 t->size = t->len = strlen(*p);
96 if (!ber_octetstring(o, t, cons))
98 if (o->direction == ODR_DECODE)
100 *p = (char *) t->buf;
101 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */
107 * iconv interface to octetstring.
109 int odr_iconv_string(ODR o, char **p, int opt, const char *name)
118 o->t_class = ODR_UNIVERSAL;
119 o->t_tag = ODR_OCTETSTRING;
121 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
124 return odr_missing(o, opt, name);
125 if (o->direction == ODR_PRINT)
128 odr_printf(o, "'%s'\n", *p);
131 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
132 if (o->direction == ODR_ENCODE)
136 if (o->op->iconv_handle != 0)
138 size_t inleft = strlen(*p);
140 size_t outleft = 4 * inleft + 2;
141 char *outbuf = (char *) odr_malloc (o, outleft);
144 t->buf = (unsigned char *) outbuf;
146 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
148 if (ret == (size_t)(-1))
150 odr_seterror(o, ODATA, 44);
153 t->size = t->len = outbuf - (char*) t->buf;
157 t->buf = (unsigned char *) *p;
158 t->size = t->len = strlen(*p);
167 if (!ber_octetstring(o, t, cons))
169 if (o->direction == ODR_DECODE)
173 if (o->op->iconv_handle != 0)
175 size_t inleft = t->len;
176 char *inbuf = (char *) t->buf;
177 size_t outleft = 4 * inleft + 2;
178 char *outbuf = (char *) odr_malloc (o, outleft);
183 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
185 if (ret == (size_t)(-1))
187 odr_seterror(o, ODATA, 45);
190 inleft = outbuf - (char*) *p;
192 (*p)[inleft] = '\0'; /* null terminate it */
196 *p = (char *) t->buf;
197 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */
205 * indent-tabs-mode: nil
207 * vim: shiftwidth=4 tabstop=8 expandtab