Ups... memory leak
[yaz-moved-to-github.git] / src / odr_bit.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file odr_bit.c
8  * \brief Implements ODR BITSTRING codec
9  */
10
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <string.h>
16 #include "odr-priv.h"
17
18 /*
19  * Top level bitstring string en/decoder.
20  * Returns 1 on success, 0 on error.
21  */
22 int odr_bitstring(ODR o, Odr_bitmask **p, int opt, const char *name)
23 {
24     int res, cons = 0;
25
26     if (o->error)
27         return 0;
28     if (o->op->t_class < 0)
29     {
30         o->op->t_class = ODR_UNIVERSAL;
31         o->op->t_tag = ODR_BITSTRING;
32     }
33     res = ber_tag(o, p, o->op->t_class, o->op->t_tag, &cons, opt, name);
34     if (res < 0)
35         return 0;
36     if (!res)
37         return odr_missing(o, opt, name);
38     if (o->direction == ODR_PRINT)
39     {
40         int i = ODR_BITMASK_SIZE;
41         int j;
42         odr_prname(o, name);
43         odr_printf(o, "BITSTRING(len=%d) ",(*p)->top + 1);
44         while (--i > 0)
45             if (ODR_MASK_GET(*p, i))
46                 break;
47         for (j = 0; j <= i; j++)
48         {
49             odr_printf(o, "%c", ODR_MASK_GET(*p, j) ? '1' : '0');
50             if (j && ((j+1)&7) == 0)
51                 odr_printf(o, "-");
52         }
53         odr_printf(o, "\n");
54         return 1;
55     }
56     if (o->direction == ODR_DECODE)
57     {
58         *p = (Odr_bitmask *)odr_malloc(o, sizeof(Odr_bitmask));
59         memset((*p)->bits, 0, ODR_BITMASK_SIZE);
60         (*p)->top = -1;
61     }
62 #if 0
63     /* ignoring the cons helps with at least one target. 
64      * http://bugzilla.indexdata.dk/cgi-bin/bugzilla/show_bug.cgi?id=24
65      */
66     return ber_bitstring(o, *p, 0);
67 #else
68     return ber_bitstring(o, *p, cons);
69 #endif
70 }
71 /*
72  * Local variables:
73  * c-basic-offset: 4
74  * c-file-style: "Stroustrup"
75  * indent-tabs-mode: nil
76  * End:
77  * vim: shiftwidth=4 tabstop=8 expandtab
78  */
79