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