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