Added debug logging to verify ZOOM_EVENT_RECV_SEARCH
[yaz-moved-to-github.git] / src / ber_oct.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 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
20 int ber_octetstring(ODR o, Odr_oct *p, int cons)
21 {
22     int res, len;
23     const unsigned char *base;
24     unsigned char *c;
25
26     switch (o->direction)
27     {
28     case ODR_DECODE:
29         if ((res = ber_declen(o->bp, &len, odr_max(o))) < 0)
30         {
31             odr_seterror(o, OPROTO, 14);
32             return 0;
33         }
34         o->bp += res;
35         if (cons)       /* fetch component strings */
36         {
37             base = o->bp;
38             while (odp_more_chunks(o, base, len))
39                 if (!odr_octetstring(o, &p, 0, 0))
40                     return 0;
41             return 1;
42         }
43         /* primitive octetstring */
44         if (len < 0)
45         {
46             odr_seterror(o, OOTHER, 15);
47             return 0;
48         }
49         if (len > odr_max(o))
50         {
51             odr_seterror(o, OOTHER, 16);
52             return 0;
53         }
54         if (len + 1 > p->size - p->len)
55         {
56             c = (unsigned char *)odr_malloc(o, p->size += len + 1);
57             if (p->len)
58                 memcpy(c, p->buf, p->len);
59             p->buf = c;
60         }
61         if (len)
62             memcpy(p->buf + p->len, o->bp, len);
63         p->len += len;
64         o->bp += len;
65         /* the final null is really not part of the buffer, but */
66         /* it helps somes applications that assumes C strings */
67         if (len)
68             p->buf[p->len] = '\0';
69         return 1;
70     case ODR_ENCODE:
71         if ((res = ber_enclen(o, p->len, 5, 0)) < 0)
72             return 0;
73         if (p->len == 0)
74             return 1;
75         if (odr_write(o, p->buf, p->len) < 0)
76             return 0;
77         return 1;
78     case ODR_PRINT:
79         return 1;
80     default:
81         odr_seterror(o, OOTHER, 17);
82         return 0;
83     }
84 }
85 /*
86  * Local variables:
87  * c-basic-offset: 4
88  * c-file-style: "Stroustrup"
89  * indent-tabs-mode: nil
90  * End:
91  * vim: shiftwidth=4 tabstop=8 expandtab
92  */
93