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