2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_internal.h>
6 #include <BIT_STRING.h>
7 #include <asn_internal.h>
10 * BIT STRING basic type description.
12 static const ber_tlv_tag_t asn_DEF_BIT_STRING_tags[] = {
13 (ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
15 asn_OCTET_STRING_specifics_t asn_SPC_BIT_STRING_specs = {
17 offsetof(BIT_STRING_t, _asn_ctx),
20 asn_TYPE_operation_t asn_OP_BIT_STRING = {
21 OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
24 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
25 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
26 OCTET_STRING_decode_xer_binary,
27 BIT_STRING_encode_xer,
28 #ifdef ASN_DISABLE_OER_SUPPORT
32 BIT_STRING_decode_oer,
33 BIT_STRING_encode_oer,
34 #endif /* ASN_DISABLE_OER_SUPPORT */
35 #ifdef ASN_DISABLE_PER_SUPPORT
39 BIT_STRING_decode_uper, /* Unaligned PER decoder */
40 BIT_STRING_encode_uper, /* Unaligned PER encoder */
41 #endif /* ASN_DISABLE_PER_SUPPORT */
42 BIT_STRING_random_fill,
43 0 /* Use generic outmost tag fetcher */
45 asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
49 asn_DEF_BIT_STRING_tags,
50 sizeof(asn_DEF_BIT_STRING_tags)
51 / sizeof(asn_DEF_BIT_STRING_tags[0]),
52 asn_DEF_BIT_STRING_tags, /* Same as above */
53 sizeof(asn_DEF_BIT_STRING_tags)
54 / sizeof(asn_DEF_BIT_STRING_tags[0]),
55 { 0, 0, BIT_STRING_constraint },
56 0, 0, /* No members */
57 &asn_SPC_BIT_STRING_specs
61 * BIT STRING generic constraint.
64 BIT_STRING_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
65 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
66 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
69 if((st->size == 0 && st->bits_unused)
70 || st->bits_unused < 0 || st->bits_unused > 7) {
71 ASN__CTFAIL(app_key, td, sptr,
72 "%s: invalid padding byte (%s:%d)",
73 td->name, __FILE__, __LINE__);
77 ASN__CTFAIL(app_key, td, sptr,
78 "%s: value not given (%s:%d)",
79 td->name, __FILE__, __LINE__);
86 static const char *_bit_pattern[16] = {
87 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
88 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
92 BIT_STRING_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
93 int ilevel, enum xer_encoder_flags_e flags,
94 asn_app_consume_bytes_f *cb, void *app_key) {
98 char *scend = scratch + (sizeof(scratch) - 10);
99 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
100 int xcan = (flags & XER_F_CANONICAL);
110 end = buf + st->size - 1; /* Last byte is special */
115 for(; buf < end; buf++) {
117 int nline = xcan?0:(((buf - st->buf) % 8) == 0);
118 if(p >= scend || nline) {
119 ASN__CALLBACK(scratch, p - scratch);
121 if(nline) ASN__TEXT_INDENT(1, ilevel);
123 memcpy(p + 0, _bit_pattern[v >> 4], 4);
124 memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
128 if(!xcan && ((buf - st->buf) % 8) == 0)
129 ASN__TEXT_INDENT(1, ilevel);
130 ASN__CALLBACK(scratch, p - scratch);
135 int ubits = st->bits_unused;
137 for(i = 7; i >= ubits; i--)
138 *p++ = (v & (1 << i)) ? 0x31 : 0x30;
139 ASN__CALLBACK(scratch, p - scratch);
142 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
151 * BIT STRING specific contents printer.
154 BIT_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
155 asn_app_consume_bytes_f *cb, void *app_key) {
156 const char * const h2c = "0123456789ABCDEF";
158 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
163 (void)td; /* Unused argument */
166 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
170 end = buf + st->size;
175 for(; buf < end; buf++) {
176 if((buf - st->buf) % 16 == 0 && (st->size > 16)
179 /* Dump the string */
180 if(cb(scratch, p - scratch, app_key) < 0) return -1;
183 *p++ = h2c[*buf >> 4];
184 *p++ = h2c[*buf & 0x0F];
189 p--; /* Eat the tailing space */
191 if((st->size > 16)) {
195 /* Dump the incomplete 16-bytes row */
196 if(cb(scratch, p - scratch, app_key) < 0)
200 if(st->bits_unused) {
201 int ret = snprintf(scratch, sizeof(scratch), " (%d bit%s unused)",
202 st->bits_unused, st->bits_unused == 1 ? "" : "s");
203 assert(ret > 0 && ret < (ssize_t)sizeof(scratch));
204 if(ret > 0 && ret < (ssize_t)sizeof(scratch)
205 && cb(scratch, ret, app_key) < 0)
213 * Non-destructively remove the trailing 0-bits from the given bit string.
215 static const BIT_STRING_t *
216 BIT_STRING__compactify(const BIT_STRING_t *st, BIT_STRING_t *tmp) {
219 const uint8_t *c_buf;
224 assert(st->bits_unused == 0);
227 for(b = &st->buf[st->size - 1]; b > st->buf && *b == 0; b--) {
230 /* b points to the last byte which may contain data */
235 if(v & 0x0F) unused -= 4;
236 if(v & 0x33) unused -= 2;
237 if(v & 0x55) unused -= 1;
238 tmp->size = b-st->buf + 1;
239 tmp->bits_unused = unused;
241 tmp->size = b-st->buf;
242 tmp->bits_unused = 0;
245 assert(b >= st->buf);
248 unconst.c_buf = st->buf;
249 tmp->buf = unconst.nc_buf;
254 * Lexicographically compare the common prefix of both strings,
255 * and if it is the same return -1 for the smallest string.
258 BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
261 * Remove information about trailing bits, since
262 * X.680 (08/2015) #22.7 "ensure that different semantics are not"
263 * "associated with [values that differ only in] the trailing 0 bits."
265 BIT_STRING_t compact_a, compact_b;
266 const BIT_STRING_t *a = BIT_STRING__compactify(aptr, &compact_a);
267 const BIT_STRING_t *b = BIT_STRING__compactify(bptr, &compact_b);
268 const asn_OCTET_STRING_specifics_t *specs = td->specifics;
270 assert(specs && specs->subvariant == ASN_OSUBV_BIT);
273 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
274 int ret = memcmp(a->buf, b->buf, common_prefix_size);
276 /* Figure out which string with equal prefixes is longer. */
277 if(a->size < b->size) {
279 } else if(a->size > b->size) {
282 /* Figure out how many unused bits */
283 if(a->bits_unused > b->bits_unused) {
285 } else if(a->bits_unused < b->bits_unused) {
294 } else if(!a && !b) {
303 #ifndef ASN_DISABLE_PER_SUPPORT
306 #define RETURN(_code) \
308 asn_dec_rval_t tmprval; \
309 tmprval.code = _code; \
310 tmprval.consumed = consumed_myself; \
314 static asn_per_constraint_t asn_DEF_BIT_STRING_constraint_size = {
315 APC_SEMI_CONSTRAINED, -1, -1, 0, 0};
318 BIT_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
319 const asn_TYPE_descriptor_t *td,
320 const asn_per_constraints_t *constraints, void **sptr,
321 asn_per_data_t *pd) {
322 const asn_OCTET_STRING_specifics_t *specs = td->specifics
323 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
324 : &asn_SPC_BIT_STRING_specs;
325 const asn_per_constraints_t *pc =
326 constraints ? constraints : td->encoding_constraints.per_constraints;
327 const asn_per_constraint_t *csiz;
328 asn_dec_rval_t rval = { RC_OK, 0 };
329 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
330 ssize_t consumed_myself = 0;
338 csiz = &asn_DEF_BIT_STRING_constraint_size;
341 if(specs->subvariant != ASN_OSUBV_BIT) {
342 ASN_DEBUG("Subvariant %d is not BIT OSUBV_BIT", specs->subvariant);
347 * Allocate the string.
350 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
351 if(!st) RETURN(RC_FAIL);
354 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
355 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
356 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
358 if(csiz->flags & APC_EXTENSIBLE) {
359 int inext = per_get_few_bits(pd, 1);
360 if(inext < 0) RETURN(RC_WMORE);
362 csiz = &asn_DEF_BIT_STRING_constraint_size;
366 if(csiz->effective_bits >= 0) {
368 st->size = (csiz->upper_bound + 7) >> 3;
369 st->buf = (uint8_t *)MALLOC(st->size + 1);
370 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
373 /* X.691, #16.5: zero-length encoding */
374 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
375 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
376 if(csiz->effective_bits == 0) {
378 ASN_DEBUG("Encoding BIT STRING size %ld", csiz->upper_bound);
379 ret = per_get_many_bits(pd, st->buf, 0, csiz->upper_bound);
380 if(ret < 0) RETURN(RC_WMORE);
381 consumed_myself += csiz->upper_bound;
382 st->buf[st->size] = 0;
383 st->bits_unused = (8 - (csiz->upper_bound & 0x7)) & 0x7;
395 /* Get the PER length */
396 raw_len = uper_get_length(pd, csiz->effective_bits, csiz->lower_bound,
398 if(raw_len < 0) RETURN(RC_WMORE);
399 if(raw_len == 0 && st->buf) break;
401 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
402 (long)csiz->effective_bits, (long)raw_len,
403 repeat ? "repeat" : "once", td->name);
405 len_bytes = (len_bits + 7) >> 3;
406 if(len_bits & 0x7) st->bits_unused = 8 - (len_bits & 0x7);
407 /* len_bits be multiple of 16K if repeat is set */
408 p = REALLOC(st->buf, st->size + len_bytes + 1);
409 if(!p) RETURN(RC_FAIL);
410 st->buf = (uint8_t *)p;
412 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
413 if(ret < 0) RETURN(RC_WMORE);
414 st->size += len_bytes;
416 st->buf[st->size] = 0; /* nul-terminate */
422 BIT_STRING_encode_uper(const asn_TYPE_descriptor_t *td,
423 const asn_per_constraints_t *constraints,
424 const void *sptr, asn_per_outp_t *po) {
425 const asn_OCTET_STRING_specifics_t *specs =
426 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
427 : &asn_SPC_BIT_STRING_specs;
428 const asn_per_constraints_t *pc =
429 constraints ? constraints : td->encoding_constraints.per_constraints;
430 const asn_per_constraint_t *csiz;
431 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
432 BIT_STRING_t compact_bstr; /* Do not modify this directly! */
433 asn_enc_rval_t er = { 0, 0, 0 };
434 int inext = 0; /* Lies not within extension root */
440 if(!st || (!st->buf && st->size))
443 if(specs->subvariant == ASN_OSUBV_BIT) {
444 if((st->size == 0 && st->bits_unused) || (st->bits_unused & ~7))
453 csiz = &asn_DEF_BIT_STRING_constraint_size;
455 ct_extensible = csiz->flags & APC_EXTENSIBLE;
457 /* Figure out the size without the trailing bits */
458 st = BIT_STRING__compactify(st, &compact_bstr);
459 size_in_bits = 8 * st->size - st->bits_unused;
462 "Encoding %s into %" ASN_PRI_SIZE " bits"
463 " (%ld..%ld, effective %d)%s",
464 td->name, size_in_bits, csiz->lower_bound, csiz->upper_bound,
465 csiz->effective_bits, ct_extensible ? " EXT" : "");
467 /* Figure out whether size lies within PER visible constraint */
469 if(csiz->effective_bits >= 0) {
470 if((ssize_t)size_in_bits > csiz->upper_bound) {
472 csiz = &asn_DEF_BIT_STRING_constraint_size;
483 /* Declare whether length is [not] within extension root */
484 if(per_put_few_bits(po, inext, 1))
488 if(csiz->effective_bits >= 0 && !inext) {
489 int add_trailer = (ssize_t)size_in_bits < csiz->lower_bound;
491 "Encoding %" ASN_PRI_SIZE " bytes (%ld), length (in %d bits) trailer %d; actual "
492 "value %" ASN_PRI_SSIZE "",
493 st->size, size_in_bits - csiz->lower_bound, csiz->effective_bits,
495 add_trailer ? 0 : (ssize_t)size_in_bits - csiz->lower_bound);
496 ret = per_put_few_bits(
497 po, add_trailer ? 0 : (ssize_t)size_in_bits - csiz->lower_bound,
498 csiz->effective_bits);
499 if(ret) ASN__ENCODE_FAILED;
500 ret = per_put_many_bits(po, st->buf, size_in_bits);
501 if(ret) ASN__ENCODE_FAILED;
503 static const uint8_t zeros[16];
504 size_t trailing_zero_bits = csiz->lower_bound - size_in_bits;
505 while(trailing_zero_bits > 0) {
506 if(trailing_zero_bits > 8 * sizeof(zeros)) {
507 ret = per_put_many_bits(po, zeros, 8 * sizeof(zeros));
508 trailing_zero_bits -= 8 * sizeof(zeros);
510 ret = per_put_many_bits(po, zeros, trailing_zero_bits);
511 trailing_zero_bits = 0;
513 if(ret) ASN__ENCODE_FAILED;
519 ASN_DEBUG("Encoding %" ASN_PRI_SIZE " bytes", st->size);
524 ssize_t maySave = uper_put_length(po, size_in_bits, &need_eom);
525 if(maySave < 0) ASN__ENCODE_FAILED;
527 ASN_DEBUG("Encoding %" ASN_PRI_SSIZE " of %" ASN_PRI_SIZE "", maySave, size_in_bits);
529 ret = per_put_many_bits(po, buf, maySave);
530 if(ret) ASN__ENCODE_FAILED;
533 size_in_bits -= maySave;
534 assert(!(maySave & 0x07) || !size_in_bits);
535 if(need_eom && uper_put_length(po, 0, 0))
536 ASN__ENCODE_FAILED; /* End of Message length */
537 } while(size_in_bits);
542 #endif /* ASN_DISABLE_PER_SUPPORT */
544 asn_random_fill_result_t
545 BIT_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
546 const asn_encoding_constraints_t *constraints,
548 const asn_OCTET_STRING_specifics_t *specs =
549 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
550 : &asn_SPC_BIT_STRING_specs;
551 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
552 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
553 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
554 static unsigned lengths[] = {0, 1, 2, 3, 4, 8,
555 126, 127, 128, 16383, 16384, 16385,
556 65534, 65535, 65536, 65537};
560 size_t rnd_bits, rnd_len;
563 if(max_length == 0) return result_skipped;
565 switch(specs->subvariant) {
567 return result_failed;
574 /* Figure out how far we should go */
575 rnd_bits = lengths[asn_random_between(
576 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
577 if(!constraints || !constraints->per_constraints)
578 constraints = &td->encoding_constraints;
579 if(constraints->per_constraints) {
580 const asn_per_constraint_t *pc = &constraints->per_constraints->size;
581 if(pc->flags & APC_CONSTRAINED) {
582 long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
584 : (ssize_t)max_length;
585 if(max_length < (size_t)pc->lower_bound) {
586 return result_skipped;
588 if(pc->flags & APC_EXTENSIBLE) {
589 switch(asn_random_between(0, 5)) {
591 if(pc->lower_bound > 0) {
592 rnd_bits = pc->lower_bound - 1;
597 rnd_bits = pc->upper_bound + 1;
600 /* Keep rnd_bits from the table */
601 if(rnd_bits < max_length) {
606 rnd_bits = asn_random_between(pc->lower_bound,
607 suggested_upper_bound);
611 asn_random_between(pc->lower_bound, suggested_upper_bound);
614 rnd_bits = asn_random_between(0, max_length - 1);
616 } else if(rnd_bits >= max_length) {
617 rnd_bits = asn_random_between(0, max_length - 1);
620 rnd_len = (rnd_bits + 7) / 8;
621 buf = CALLOC(1, rnd_len + 1);
622 if(!buf) return result_failed;
624 bend = &buf[rnd_len];
626 for(b = buf; b < bend; b++) {
627 *(uint8_t *)b = asn_random_between(0, 255);
629 *b = 0; /* Zero-terminate just in case. */
635 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
638 return result_failed;
644 st->bits_unused = (8 - (rnd_bits & 0x7)) & 0x7;
645 if(st->bits_unused) {
646 assert(st->size > 0);
647 st->buf[st->size-1] &= 0xff << st->bits_unused;
650 result_ok.length = st->size;