YAZ  5.34.0
odr_bit.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 
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 
int ber_bitstring(ODR o, Odr_bitmask *p, int cons)
Definition: ber_bit.c:20
int ber_tag(ODR o, void *p, int zclass, int tag, int *constructed, int opt, const char *name)
Encode/decode BER tags.
Definition: ber_tag.c:34
int opt
Definition: initopt.c:19
char * name
Definition: initopt.c:18
Internal ODR definitions.
void odr_printf(ODR o, const char *fmt,...)
Definition: odr.c:290
#define ODR_DECODE
Definition: odr.h:95
#define ODR_UNIVERSAL
Definition: odr.h:65
#define ODR_BITSTRING
Definition: odr.h:75
#define ODR_PRINT
Definition: odr.h:97
#define ODR_MASK_GET(mask, num)
Definition: odr.h:211
#define ODR_BITMASK_SIZE
Definition: odr.h:112
int odr_bitstring(ODR o, Odr_bitmask **p, int opt, const char *name)
Definition: odr_bit.c:22
void * odr_malloc(ODR o, size_t size)
Definition: odr_mem.c:31
void odr_prname(ODR o, const char *name)
Definition: odr_util.c:18
int odr_missing(ODR o, int opt, const char *name)
Definition: odr_util.c:82
int t_class
Definition: odr-priv.h:107
Definition: odr.h:125
struct Odr_private * op
Definition: odr.h:132
int error
Definition: odr.h:128
int direction
Definition: odr.h:126