9c8eda4ca123c32ec58b328c2c39b4a9eb2c2eae
[yaz-moved-to-github.git] / odr / ber_oct.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: ber_oct.c,v $
7  * Revision 1.6  1995-03-17 10:17:41  quinn
8  * Added memory management.
9  *
10  * Revision 1.5  1995/03/08  12:12:10  quinn
11  * Added better error checking.
12  *
13  * Revision 1.4  1995/02/10  15:55:28  quinn
14  * Bug fixes, mostly.
15  *
16  * Revision 1.3  1995/02/03  17:04:34  quinn
17  * *** empty log message ***
18  *
19  * Revision 1.2  1995/02/02  20:38:50  quinn
20  * Updates.
21  *
22  * Revision 1.1  1995/02/02  16:21:52  quinn
23  * First kick.
24  *
25  */
26
27 #include <odr.h>
28
29 int ber_octetstring(ODR o, Odr_oct *p, int cons)
30 {
31     int res, len;
32     unsigned char *base, *c;
33
34     switch (o->direction)
35     {
36         case ODR_DECODE:
37             if ((res = ber_declen(o->bp, &len)) < 0)
38             {
39                 o->error = OPROTO;
40                 return 0;
41             }
42             o->bp += res;
43             o->left -= res;
44             if (cons)       /* fetch component strings */
45             {
46                 base = o->bp;
47                 while (odp_more_chunks(o, base, len))
48                     if (!odr_octetstring(o, &p, 0))
49                         return 0;
50                 return 1;
51             }
52             /* primitive octetstring */
53             if (len < 0)
54             {
55                 o->error = OOTHER;
56                 return 0;
57             }
58             if (len + 1 > p->size - p->len)
59             {
60                 c = odr_malloc(o, p->size += len + 1);
61                 if (p->len)
62                     memcpy(c, p->buf, p->len);
63                 p->buf = c;
64             }
65             if (len)
66                 memcpy(p->buf + p->len, o->bp, len);
67             p->len += len;
68             o->bp += len;
69             o->left -= len;
70             return 1;
71         case ODR_ENCODE:
72             if ((res = ber_enclen(o->bp, p->len, 5, 0)) < 0)
73             {
74                 o->error = OOTHER;
75                 return 0;
76             }
77             o->bp += res;
78             o->left -= res;
79             if (p->len == 0)
80                 return 1;
81             if (p->len > o->left)
82             {
83                 o->error = OSPACE;
84                 return 0;
85             }
86             memcpy(o->bp, p->buf, p->len);
87             o->bp += p->len;
88             o->left -= p->len;
89             return 1;
90         case ODR_PRINT: return 1;
91         default: o->error = OOTHER; return 0;
92     }
93 }