BIT_STRING.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3  * Redistribution and modifications are permitted subject to BSD license.
4  */
7 
8 /*
9  * BIT STRING basic type description.
10  */
12  (ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
13 };
15  sizeof(BIT_STRING_t),
16  offsetof(BIT_STRING_t, _asn_ctx),
17  ASN_OSUBV_BIT
18 };
20  OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
21 #if !defined(ASN_DISABLE_PRINT_SUPPORT)
23 #else
24  0,
25 #endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
28 #if !defined(ASN_DISABLE_BER_SUPPORT)
29  OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
30  OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
31 #else
32  0,
33  0,
34 #endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
35 #if !defined(ASN_DISABLE_XER_SUPPORT)
38 #else
39  0,
40  0,
41 #endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
42 #if !defined(ASN_DISABLE_JER_SUPPORT)
45 #else
46  0,
47  0,
48 #endif /* !defined(ASN_DISABLE_JER_SUPPORT) */
49 #if !defined(ASN_DISABLE_OER_SUPPORT)
52 #else
53  0,
54  0,
55 #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
56 #if !defined(ASN_DISABLE_UPER_SUPPORT)
57  BIT_STRING_decode_uper, /* Unaligned PER decoder */
58  BIT_STRING_encode_uper, /* Unaligned PER encoder */
59 #else
60  0,
61  0,
62 #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
63 #if !defined(ASN_DISABLE_APER_SUPPORT)
64  OCTET_STRING_decode_aper, /* Aligned PER decoder */
65  OCTET_STRING_encode_aper, /* Aligned PER encoder */
66 #else
67  0,
68  0,
69 #endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
70 #if !defined(ASN_DISABLE_RFILL_SUPPORT)
72 #else
73  0,
74 #endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
75  0 /* Use generic outmost tag fetcher */
76 };
78  "BIT STRING",
79  "BIT_STRING",
83  / sizeof(asn_DEF_BIT_STRING_tags[0]),
84  asn_DEF_BIT_STRING_tags, /* Same as above */
86  / sizeof(asn_DEF_BIT_STRING_tags[0]),
87  {
88 #if !defined(ASN_DISABLE_OER_SUPPORT)
89  0,
90 #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
91 #if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
92  0,
93 #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
94 #if !defined(ASN_DISABLE_JER_SUPPORT)
95  0,
96 #endif /* !defined(ASN_DISABLE_JER_SUPPORT) */
98  },
99  0, 0, /* No members */
101 };
102 
103 /*
104  * BIT STRING generic constraint.
105  */
106 int
107 BIT_STRING_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
108  asn_app_constraint_failed_f *ctfailcb, void *app_key) {
109  const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
110 
111  if(st && st->buf) {
112  if((st->size == 0 && st->bits_unused)
113  || st->bits_unused < 0 || st->bits_unused > 7) {
114  ASN__CTFAIL(app_key, td, sptr,
115  "%s: invalid padding byte (%s:%d)",
116  td->name, __FILE__, __LINE__);
117  return -1;
118  }
119  } else {
120  ASN__CTFAIL(app_key, td, sptr,
121  "%s: value not given (%s:%d)",
122  td->name, __FILE__, __LINE__);
123  return -1;
124  }
125 
126  return 0;
127 }
128 
129 /*
130  * Non-destructively remove the trailing 0-bits from the given bit string.
131  */
132 const BIT_STRING_t *
134  const uint8_t *b;
135  union {
136  const uint8_t *c_buf;
137  uint8_t *nc_buf;
138  } unconst;
139 
140  if(st->size == 0) {
141  assert(st->bits_unused == 0);
142  return st;
143  } else {
144  for(b = &st->buf[st->size - 1]; b > st->buf && *b == 0; b--) {
145  ;
146  }
147  /* b points to the last byte which may contain data */
148  if(*b) {
149  int unused = 7;
150  uint8_t v = *b;
151  v &= -(int8_t)v;
152  if(v & 0x0F) unused -= 4;
153  if(v & 0x33) unused -= 2;
154  if(v & 0x55) unused -= 1;
155  tmp->size = b-st->buf + 1;
156  tmp->bits_unused = unused;
157  } else {
158  tmp->size = b-st->buf;
159  tmp->bits_unused = 0;
160  }
161 
162  assert(b >= st->buf);
163  }
164 
165  unconst.c_buf = st->buf;
166  tmp->buf = unconst.nc_buf;
167  return tmp;
168 }
169 
170 /*
171  * Lexicographically compare the common prefix of both strings,
172  * and if it is the same return -1 for the smallest string.
173  */
174 int
175 BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
176  const void *bptr) {
177  /*
178  * Remove information about trailing bits, since
179  * X.680 (08/2015) #22.7 "ensure that different semantics are not"
180  * "associated with [values that differ only in] the trailing 0 bits."
181  */
182  BIT_STRING_t compact_a, compact_b;
183  const BIT_STRING_t *a = BIT_STRING__compactify(aptr, &compact_a);
184  const BIT_STRING_t *b = BIT_STRING__compactify(bptr, &compact_b);
185  const asn_OCTET_STRING_specifics_t *specs = td->specifics;
186 
187  (void)specs;
188  assert(specs && specs->subvariant == ASN_OSUBV_BIT);
189 
190  if(a && b) {
191  size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
192  int ret = memcmp(a->buf, b->buf, common_prefix_size);
193  if(ret == 0) {
194  /* Figure out which string with equal prefixes is longer. */
195  if(a->size < b->size) {
196  return -1;
197  } else if(a->size > b->size) {
198  return 1;
199  } else {
200  /* Figure out how many unused bits */
201  if(a->bits_unused > b->bits_unused) {
202  return -1;
203  } else if(a->bits_unused < b->bits_unused) {
204  return 1;
205  } else {
206  return 0;
207  }
208  }
209  } else {
210  return ret;
211  }
212  } else if(!a && !b) {
213  return 0;
214  } else if(!a) {
215  return -1;
216  } else {
217  return 1;
218  }
219 }
220 
221 int
222 BIT_STRING_copy(const asn_TYPE_descriptor_t *td, void **aptr,
223  const void *bptr) {
224  const asn_OCTET_STRING_specifics_t *specs = td->specifics;
225  BIT_STRING_t *a = (BIT_STRING_t *)*aptr;
226  const BIT_STRING_t *b = (const BIT_STRING_t *)bptr;
227 
228  if(!b) {
229  if(a) {
230  FREEMEM(a->buf);
231  FREEMEM(a);
232  *aptr = 0;
233  }
234  return 0;
235  }
236 
237  if(!a) {
238  a = *aptr = CALLOC(1, specs->struct_size);
239  if(!a) return -1;
240  }
241 
242  uint8_t* buf = MALLOC(b->size + 1);
243  if(!buf) return -1;
244  memcpy(buf, b->buf, b->size);
245  buf[b->size] = 0;
246 
247  FREEMEM(a->buf);
248  a->buf = buf;
249  a->size = b->size;
250  a->bits_unused = b->bits_unused;
251 
252  return 0;
253 }
BIT_STRING_encode_xer
xer_type_encoder_f BIT_STRING_encode_xer
Definition: BIT_STRING.h:45
OCTET_STRING_decode_xer_binary
xer_type_decoder_f OCTET_STRING_decode_xer_binary
Definition: OCTET_STRING.h:43
asn_TYPE_operation_s
Definition: constr_TYPE.h:184
OCTET_STRING_free
asn_struct_free_f OCTET_STRING_free
Definition: OCTET_STRING.h:24
BIT_STRING_constraint
int BIT_STRING_constraint(const asn_TYPE_descriptor_t *td, const void *sptr, asn_app_constraint_failed_f *ctfailcb, void *app_key)
Definition: BIT_STRING.c:107
asn_app_constraint_failed_f
void() asn_app_constraint_failed_f(void *application_specific_key, const struct asn_TYPE_descriptor_s *type_descriptor_which_failed, const void *structure_which_failed_ptr, const char *error_message_format,...) CC_PRINTFLIKE(4
Definition: asn_application.h:167
ber_tlv_tag_t
unsigned ber_tlv_tag_t
Definition: ber_tlv_tag.h:18
BIT_STRING_s::buf
uint8_t * buf
Definition: BIT_STRING.h:15
OCTET_STRING_encode_der
der_type_encoder_f OCTET_STRING_encode_der
Definition: OCTET_STRING.h:38
ASN_TAG_CLASS_UNIVERSAL
@ ASN_TAG_CLASS_UNIVERSAL
Definition: ber_tlv_tag.h:13
asn_TYPE_descriptor_s::name
const char * name
Definition: constr_TYPE.h:225
BIT_STRING_s::size
size_t size
Definition: BIT_STRING.h:16
BIT_STRING_encode_jer
jer_type_encoder_f BIT_STRING_encode_jer
Definition: BIT_STRING.h:50
CALLOC
#define CALLOC(nmemb, size)
Definition: asn_internal.h:37
asn_OCTET_STRING_specifics_s::struct_size
unsigned struct_size
Definition: OCTET_STRING.h:131
BIT_STRING_encode_uper
per_type_encoder_f BIT_STRING_encode_uper
Definition: BIT_STRING.h:60
asn_DEF_BIT_STRING_tags
static const ber_tlv_tag_t asn_DEF_BIT_STRING_tags[]
Definition: BIT_STRING.c:11
BIT_STRING_s
Definition: BIT_STRING.h:14
asn_OCTET_STRING_specifics_s
Definition: OCTET_STRING.h:125
OCTET_STRING_encode_aper
per_type_encoder_f OCTET_STRING_encode_aper
Definition: OCTET_STRING.h:67
FREEMEM
#define FREEMEM(ptr)
Definition: asn_internal.h:40
BIT_STRING_decode_jer
jer_type_decoder_f BIT_STRING_decode_jer
Definition: BIT_STRING.h:49
asn_TYPE_descriptor_s
Definition: constr_TYPE.h:224
asn_DEF_BIT_STRING
asn_TYPE_descriptor_t asn_DEF_BIT_STRING
Definition: BIT_STRING.c:77
OCTET_STRING_decode_aper
per_type_decoder_f OCTET_STRING_decode_aper
Definition: OCTET_STRING.h:66
asn_OP_BIT_STRING
asn_TYPE_operation_t asn_OP_BIT_STRING
Definition: BIT_STRING.c:19
BIT_STRING_copy
int BIT_STRING_copy(const asn_TYPE_descriptor_t *td, void **aptr, const void *bptr)
Definition: BIT_STRING.c:222
BIT_STRING_decode_oer
oer_type_decoder_f BIT_STRING_decode_oer
Definition: BIT_STRING.h:54
offsetof
#define offsetof(s, m)
Definition: asn_system.h:132
asn_TYPE_descriptor_s::specifics
const void * specifics
Definition: constr_TYPE.h:259
asn_SPC_BIT_STRING_specs
asn_OCTET_STRING_specifics_t asn_SPC_BIT_STRING_specs
Definition: BIT_STRING.c:14
BIT_STRING_t
struct BIT_STRING_s BIT_STRING_t
asn_OCTET_STRING_specifics_s::subvariant
enum asn_OCTET_STRING_specifics_s::asn_OS_Subvariant subvariant
BIT_STRING_encode_oer
oer_type_encoder_f BIT_STRING_encode_oer
Definition: BIT_STRING.h:55
BIT_STRING_random_fill
asn_random_fill_f BIT_STRING_random_fill
Definition: BIT_STRING.h:68
BIT_STRING_decode_uper
per_type_decoder_f BIT_STRING_decode_uper
Definition: BIT_STRING.h:59
MALLOC
#define MALLOC(size)
Definition: asn_internal.h:38
BIT_STRING_compare
int BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr)
Definition: BIT_STRING.c:175
BIT_STRING_print
asn_struct_print_f BIT_STRING_print
Definition: BIT_STRING.h:30
asn_internal.h
ASN__CTFAIL
#define ASN__CTFAIL
Definition: constraints.h:57
OCTET_STRING_decode_ber
ber_type_decoder_f OCTET_STRING_decode_ber
Definition: OCTET_STRING.h:37
BIT_STRING.h
BIT_STRING_s::bits_unused
int bits_unused
Definition: BIT_STRING.h:18
BIT_STRING__compactify
const BIT_STRING_t * BIT_STRING__compactify(const BIT_STRING_t *st, BIT_STRING_t *tmp)
Definition: BIT_STRING.c:133


etsi_its_cam_ts_coding
Author(s): Jean-Pierre Busch , Guido Küppers , Lennart Reiher
autogenerated on Sun May 18 2025 02:21:22