2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: odr_oct.c,v 1.1 2003-10-27 12:21:33 adam Exp $
16 * Top level octet string en/decoder.
17 * Returns 1 on success, 0 on error.
19 int odr_octetstring(ODR o, Odr_oct **p, int opt, const char *name)
27 o->t_class = ODR_UNIVERSAL;
28 o->t_tag = ODR_OCTETSTRING;
30 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
33 return odr_missing(o, opt, name);
34 if (o->direction == ODR_PRINT)
38 fprintf(o->print, "OCTETSTRING(len=%d)", (*p)->len);
39 for (i = 0; i<(*p)->len; i++)
41 if (i < 5 || i > ((*p)->len - 4))
43 fprintf (o->print, " %02X", (*p)->buf[i]);
46 fprintf (o->print, " .. ");
48 fprintf(o->print, "\n");
51 if (o->direction == ODR_DECODE)
53 *p = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct));
58 if (ber_octetstring(o, *p, cons))
60 odr_seterror(o, OOTHER, 43);
65 * Friendlier interface to octetstring.
67 int odr_cstring(ODR o, char **p, int opt, const char *name)
76 o->t_class = ODR_UNIVERSAL;
77 o->t_tag = ODR_OCTETSTRING;
79 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
82 return odr_missing(o, opt, name);
83 if (o->direction == ODR_PRINT)
86 fprintf(o->print, "'%s'\n", *p);
89 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
90 if (o->direction == ODR_ENCODE)
92 t->buf = (unsigned char *) *p;
93 t->size = t->len = strlen(*p);
101 if (!ber_octetstring(o, t, cons))
103 if (o->direction == ODR_DECODE)
105 *p = (char *) t->buf;
106 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */
112 * iconv interface to octetstring.
114 int odr_iconv_string(ODR o, char **p, int opt, const char *name)
123 o->t_class = ODR_UNIVERSAL;
124 o->t_tag = ODR_OCTETSTRING;
126 if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt, name)) < 0)
129 return odr_missing(o, opt, name);
130 if (o->direction == ODR_PRINT)
133 fprintf(o->print, "'%s'\n", *p);
136 t = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)); /* wrapper for octstring */
137 if (o->direction == ODR_ENCODE)
141 if (o->op->iconv_handle != 0)
143 size_t inleft = strlen(*p);
145 size_t outleft = 4 * inleft + 2;
146 char *outbuf = (char *) odr_malloc (o, outleft);
149 t->buf = (unsigned char *) outbuf;
151 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
153 if (ret == (size_t)(-1))
155 odr_seterror(o, ODATA, 44);
158 t->size = t->len = outbuf - (char*) t->buf;
162 t->buf = (unsigned char *) *p;
163 t->size = t->len = strlen(*p);
172 if (!ber_octetstring(o, t, cons))
174 if (o->direction == ODR_DECODE)
178 if (o->op->iconv_handle != 0)
180 size_t inleft = t->len;
181 char *inbuf = (char *) t->buf;
182 size_t outleft = 4 * inleft + 2;
183 char *outbuf = (char *) odr_malloc (o, outleft);
188 ret = yaz_iconv (o->op->iconv_handle, &inbuf, &inleft,
190 if (ret == (size_t)(-1))
192 odr_seterror(o, ODATA, 45);
195 inleft = outbuf - (char*) *p;
197 (*p)[inleft] = '\0'; /* null terminate it */
201 *p = (char *) t->buf;
202 *(*p + t->len) = '\0'; /* ber_octs reserves space for this */