Revert
[yaz-moved-to-github.git] / src / ber_bit.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: ber_bit.c,v 1.4 2005-06-25 15:46:03 adam Exp $
6  */
7
8 /** 
9  * \file ber_bit.c
10  * \brief Implements BER BITSTRING encoding and decoding.
11  *
12  * This source file implements BER encoding and decoding of
13  * the BITSTRING type.
14  */
15
16 #if HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include "odr-priv.h"
21
22 int ber_bitstring(ODR o, Odr_bitmask *p, int cons)
23 {
24     int res, len;
25     const unsigned char *base;
26
27     switch (o->direction)
28     {
29     case ODR_DECODE:
30         if ((res = ber_declen(o->bp, &len, odr_max(o))) < 0)
31         {
32             odr_seterror(o, OPROTO, 4);
33             return 0;
34         }
35         o->bp += res;
36         if (cons)       /* fetch component strings */
37         {
38             base = o->bp;
39             while (odp_more_chunks(o, base, len))
40                 if (!odr_bitstring(o, &p, 0, 0))
41                     return 0;
42             return 1;
43         }
44         /* primitive bitstring */
45         if (len < 0)
46         {
47             odr_seterror(o, OOTHER, 5);
48             return 0;
49         }
50         if (len == 0)
51             return 1;
52         if (len - 1 > ODR_BITMASK_SIZE)
53         {
54             odr_seterror(o, OOTHER, 6);
55             return 0;
56         }
57         if (len > odr_max(o))
58         {
59             odr_seterror(o, OOTHER, 7);
60             return 0;
61         }
62         o->bp++;      /* silently ignore the unused-bits field */
63         len--;
64         memcpy(p->bits + p->top + 1, o->bp, len);
65         p->top += len;
66         o->bp += len;
67         return 1;
68     case ODR_ENCODE:
69         if ((res = ber_enclen(o, p->top + 2, 5, 0)) < 0)
70             return 0;
71         if (odr_putc(o, 0) < 0)    /* no unused bits here */
72             return 0;
73         if (p->top < 0)
74             return 1;
75         if (odr_write(o, p->bits, p->top + 1) < 0)
76             return 0;
77         return 1;
78     case ODR_PRINT:
79         return 1;
80     default: 
81         odr_seterror(o, OOTHER, 8);
82         return 0;
83     }
84 }
85 /*
86  * Local variables:
87  * c-basic-offset: 4
88  * indent-tabs-mode: nil
89  * End:
90  * vim: shiftwidth=4 tabstop=8 expandtab
91  */
92