1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements ber_octetstring
10 * This source file implements BER encoding and decoding of
11 * the OCTETSTRING type.
22 int ber_octetstring(ODR o, Odr_oct *p, int cons)
33 if ((res = ber_declen(o->bp, &len, odr_max(o))) < 0)
35 odr_seterror(o, OPROTO, 14);
39 if (cons) /* fetch component strings */
42 while (odp_more_chunks(o, base, len))
43 if (!odr_octetstring(o, &p, 0, 0))
47 /* primitive octetstring */
50 odr_seterror(o, OOTHER, 15);
55 odr_seterror(o, OOTHER, 16);
61 yaz_log(YLOG_LOG, "DECODE OCTET1 p->size=%d p->len=%d", p->size, p->len);
62 if (len + 1 > p->size - p->len)
64 c = (unsigned char *)odr_malloc(o, p->size += len + 1);
65 yaz_log(YLOG_LOG, "DECODE COPY p->size=%d p->len=%d", p->size, p->len);
67 memcpy(c, p->buf, p->len);
71 memcpy(p->buf + p->len, o->bp, len);
73 yaz_log(YLOG_LOG, "DECODE OCTET2 p->size=%d p->len=%d", p->size, p->len);
75 /* the final null is really not part of the buffer, but */
76 /* it helps somes applications that assumes C strings */
78 p->buf[p->len] = '\0';
81 p->buf = odr_malloc(o, len + 1);
82 memcpy(p->buf, o->bp, len);
88 if ((res = ber_enclen(o, p->len, 5, 0)) < 0)
92 if (odr_write(o, p->buf, p->len) < 0)
98 odr_seterror(o, OOTHER, 17);
105 * c-file-style: "Stroustrup"
106 * indent-tabs-mode: nil
108 * vim: shiftwidth=4 tabstop=8 expandtab