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