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