Add odr_prepend()
[yaz-moved-to-github.git] / src / ber_bit.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: ber_bit.c,v 1.1 2003-10-27 12:21:30 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, odr_max(o))) < 0)
24         {
25             odr_seterror(o, OPROTO, 4);
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             odr_seterror(o, OOTHER, 5);
41             return 0;
42         }
43         if (len == 0)
44             return 1;
45         if (len - 1 > ODR_BITMASK_SIZE)
46         {
47             odr_seterror(o, OOTHER, 6);
48             return 0;
49         }
50         if (len > odr_max(o))
51         {
52             odr_seterror(o, OOTHER, 7);
53             return 0;
54         }
55         o->bp++;      /* silently ignore the unused-bits field */
56         len--;
57         memcpy(p->bits + p->top + 1, o->bp, len);
58         p->top += len;
59         o->bp += len;
60         return 1;
61     case ODR_ENCODE:
62         if ((res = ber_enclen(o, p->top + 2, 5, 0)) < 0)
63             return 0;
64         if (odr_putc(o, 0) < 0)    /* no unused bits here */
65             return 0;
66         if (p->top < 0)
67             return 1;
68         if (odr_write(o, p->bits, p->top + 1) < 0)
69             return 0;
70         return 1;
71     case ODR_PRINT:
72         return 1;
73     default: 
74         odr_seterror(o, OOTHER, 8);
75         return 0;
76     }
77 }