Remove 3 yaz_log from old ber_oct code
[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 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         if (len + 1 > p->size - p->len)
62         {
63             c = (unsigned char *)odr_malloc(o, p->size += len + 1);
64             if (p->len)
65                 memcpy(c, p->buf, p->len);
66             p->buf = c;
67         }
68         if (len)
69             memcpy(p->buf + p->len, o->bp, len);
70         p->len += len;
71         o->bp += len;
72         /* the final null is really not part of the buffer, but */
73         /* it helps somes applications that assumes C strings */
74         if (len)
75             p->buf[p->len] = '\0';
76 #else
77         p->len = len;
78         p->buf = odr_malloc(o, len + 1);
79         memcpy(p->buf, o->bp, len);
80         p->buf[len] = '\0';
81         o->bp += len;
82 #endif
83         return 1;
84     case ODR_ENCODE:
85         if ((res = ber_enclen(o, p->len, 5, 0)) < 0)
86             return 0;
87         if (p->len == 0)
88             return 1;
89         if (odr_write(o, p->buf, p->len) < 0)
90             return 0;
91         return 1;
92     case ODR_PRINT:
93         return 1;
94     default:
95         odr_seterror(o, OOTHER, 17);
96         return 0;
97     }
98 }
99 /*
100  * Local variables:
101  * c-basic-offset: 4
102  * c-file-style: "Stroustrup"
103  * indent-tabs-mode: nil
104  * End:
105  * vim: shiftwidth=4 tabstop=8 expandtab
106  */
107