Get rid of size member of Odr_oct
[yaz-moved-to-github.git] / src / ber_oct.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file ber_oct.c
8  * \brief Implements ber_octetstring
9  *
10  * This source file implements BER encoding and decoding of
11  * the OCTETSTRING type.
12  */
13
14 #if HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17
18 #include "odr-priv.h"
19 #include <yaz/log.h>
20 #include <assert.h>
21
22 int ber_octetstring(ODR o, Odr_oct *p, int cons)
23 {
24     int res, len;
25     const unsigned char *base;
26 #if OCT_SIZE
27     unsigned char *c;
28 #endif
29
30     switch (o->direction)
31     {
32     case ODR_DECODE:
33         if ((res = ber_declen(o->bp, &len, odr_max(o))) < 0)
34         {
35             odr_seterror(o, OPROTO, 14);
36             return 0;
37         }
38         o->bp += res;
39         if (cons)       /* fetch component strings */
40         {
41             base = o->bp;
42             while (odp_more_chunks(o, base, len))
43                 if (!odr_octetstring(o, &p, 0, 0))
44                     return 0;
45             return 1;
46         }
47         /* primitive octetstring */
48         if (len < 0)
49         {
50             odr_seterror(o, OOTHER, 15);
51             return 0;
52         }
53         if (len > odr_max(o))
54         {
55             odr_seterror(o, OOTHER, 16);
56             return 0;
57         }
58 #if OCT_SIZE
59         assert(p->size == 0);
60         assert(p->len == 0);
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)
63         {
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);
66             if (p->len)
67                 memcpy(c, p->buf, p->len);
68             p->buf = c;
69         }
70         if (len)
71             memcpy(p->buf + p->len, o->bp, len);
72         p->len += len;
73         yaz_log(YLOG_LOG, "DECODE OCTET2 p->size=%d p->len=%d", p->size, p->len);
74         o->bp += len;
75         /* the final null is really not part of the buffer, but */
76         /* it helps somes applications that assumes C strings */
77         if (len)
78             p->buf[p->len] = '\0';
79 #else
80         p->len = len;
81         p->buf = odr_malloc(o, len + 1);
82         memcpy(p->buf, o->bp, len);
83         p->buf[len] = '\0';
84         o->bp += len;
85 #endif
86         return 1;
87     case ODR_ENCODE:
88         if ((res = ber_enclen(o, p->len, 5, 0)) < 0)
89             return 0;
90         if (p->len == 0)
91             return 1;
92         if (odr_write(o, p->buf, p->len) < 0)
93             return 0;
94         return 1;
95     case ODR_PRINT:
96         return 1;
97     default:
98         odr_seterror(o, OOTHER, 17);
99         return 0;
100     }
101 }
102 /*
103  * Local variables:
104  * c-basic-offset: 4
105  * c-file-style: "Stroustrup"
106  * indent-tabs-mode: nil
107  * End:
108  * vim: shiftwidth=4 tabstop=8 expandtab
109  */
110