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