Comment.
[yaz-moved-to-github.git] / src / ber_oct.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: ber_oct.c,v 1.1 2003-10-27 12:21:30 adam Exp $
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include "odr-priv.h"
13
14 int ber_octetstring(ODR o, Odr_oct *p, int cons)
15 {
16     int res, len;
17     const unsigned char *base;
18     unsigned char *c;
19
20     switch (o->direction)
21     {
22         case ODR_DECODE:
23             if ((res = ber_declen(o->bp, &len, odr_max(o))) < 0)
24             {
25                 odr_seterror(o, OPROTO, 14);
26                 return 0;
27             }
28             o->bp += res;
29             if (cons)       /* fetch component strings */
30             {
31                 base = o->bp;
32                 while (odp_more_chunks(o, base, len))
33                     if (!odr_octetstring(o, &p, 0, 0))
34                         return 0;
35                 return 1;
36             }
37             /* primitive octetstring */
38             if (len < 0)
39             {
40                 odr_seterror(o, OOTHER, 15);
41                 return 0;
42             }
43             if (len > odr_max(o))
44             {
45                 odr_seterror(o, OOTHER, 16);
46                 return 0;
47             }
48             if (len + 1 > p->size - p->len)
49             {
50                 c = (unsigned char *)odr_malloc(o, p->size += len + 1);
51                 if (p->len)
52                     memcpy(c, p->buf, p->len);
53                 p->buf = c;
54             }
55             if (len)
56                 memcpy(p->buf + p->len, o->bp, len);
57             p->len += len;
58             o->bp += len;
59             /* the final null is really not part of the buffer, but */
60             /* it helps somes applications that assumes C strings */
61             if (len)
62                 p->buf[p->len] = '\0';
63             return 1;
64         case ODR_ENCODE:
65             if ((res = ber_enclen(o, p->len, 5, 0)) < 0)
66                 return 0;
67             if (p->len == 0)
68                 return 1;
69             if (odr_write(o, p->buf, p->len) < 0)
70                 return 0;
71             return 1;
72         case ODR_PRINT: return 1;
73         default: odr_seterror(o, OOTHER, 17); return 0;
74     }
75 }