Bug fixes, mostly.
[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.4  1995-02-10 15:55:28  quinn
8  * Bug fixes, mostly.
9  *
10  * Revision 1.3  1995/02/03  17:04:34  quinn
11  * *** empty log message ***
12  *
13  * Revision 1.2  1995/02/02  20:38:50  quinn
14  * Updates.
15  *
16  * Revision 1.1  1995/02/02  16:21:52  quinn
17  * First kick.
18  *
19  */
20
21 #include <odr.h>
22
23 int ber_octetstring(ODR o, Odr_oct *p, int cons)
24 {
25     int res, len;
26     unsigned char *base, *c;
27
28     switch (o->direction)
29     {
30         case ODR_DECODE:
31             if ((res = ber_declen(o->bp, &len)) < 0)
32                 return 0;
33             o->bp += res;
34             o->left -= 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))
40                         return 0;
41                 return 1;
42             }
43             /* primitive octetstring */
44             if (len < 0)
45                 return 0;
46             if (len + 1 > p->size - p->len)
47             {
48                 c = nalloc(o, p->size += len + 1);
49                 if (p->len)
50                     memcpy(c, p->buf, p->len);
51                 p->buf = c;
52             }
53             if (len)
54                 memcpy(p->buf + p->len, o->bp, len);
55             p->len += len;
56             o->bp += len;
57             o->left -= len;
58             return 1;
59         case ODR_ENCODE:
60             if ((res = ber_enclen(o->bp, p->len, 5, 0)) < 0)
61                 return 0;
62             o->bp += res;
63             o->left -= res;
64             if (p->len == 0)
65                 return 1;
66             if (p->len > o->left)
67                 return 0;
68             memcpy(o->bp, p->buf, p->len);
69             o->bp += p->len;
70             o->left -= p->len;
71             return 1;
72         case ODR_PRINT: return 1;
73         default: return 0;
74     }
75 }