Add Zthes tag-set -- where was it?!
[yaz-moved-to-github.git] / odr / ber_bit.c
1 /*
2  * Copyright (c) 1995-2002, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: ber_bit.c,v 1.13 2002-07-25 12:51:08 adam Exp $
7  *
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include "odr-priv.h"
14
15 int ber_bitstring(ODR o, Odr_bitmask *p, int cons)
16 {
17     int res, len;
18     const unsigned char *base;
19
20     switch (o->direction)
21     {
22         case ODR_DECODE:
23             if ((res = ber_declen(o->bp, &len)) < 0)
24             {
25                 o->error = OPROTO;
26                 return 0;
27             }
28             o->bp += res;
29             if (cons)       /* fetch component strings */
30             {
31                 base = o->bp;
32                 while (odp_more_chunks(o, base, len))
33                     if (!odr_bitstring(o, &p, 0, 0))
34                         return 0;
35                 return 1;
36             }
37             /* primitive bitstring */
38             if (len < 0)
39             {
40                 o->error = OOTHER;
41                 return 0;
42             }
43             if (len == 0)
44                 return 1;
45             if (len - 1 > ODR_BITMASK_SIZE)
46             {
47                 o->error = OOTHER;
48                 return 0;
49             }
50             o->bp++;      /* silently ignore the unused-bits field */
51             len--;
52             memcpy(p->bits + p->top + 1, o->bp, len);
53             p->top += len;
54             o->bp += len;
55             return 1;
56         case ODR_ENCODE:
57             if ((res = ber_enclen(o, p->top + 2, 5, 0)) < 0)
58                 return 0;
59             if (odr_putc(o, 0) < 0)    /* no unused bits here */
60                 return 0;
61             if (p->top < 0)
62                 return 1;
63             if (odr_write(o, p->bits, p->top + 1) < 0)
64                 return 0;
65             return 1;
66         case ODR_PRINT: return 1;
67         default: o->error = OOTHER; return 0;
68     }
69 }