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
41 BIT_STRING_decode_uper, /* Unaligned PER decoder */
42 BIT_STRING_encode_uper, /* Unaligned PER encoder */
43 OCTET_STRING_decode_aper, /* Aligned PER decoder */
44 OCTET_STRING_encode_aper, /* Aligned PER encoder */
45 #endif /* ASN_DISABLE_PER_SUPPORT */
46 BIT_STRING_random_fill,
47 0 /* Use generic outmost tag fetcher */
49 asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
53 asn_DEF_BIT_STRING_tags,
54 sizeof(asn_DEF_BIT_STRING_tags)
55 / sizeof(asn_DEF_BIT_STRING_tags[0]),
56 asn_DEF_BIT_STRING_tags, /* Same as above */
57 sizeof(asn_DEF_BIT_STRING_tags)
58 / sizeof(asn_DEF_BIT_STRING_tags[0]),
59 { 0, 0, BIT_STRING_constraint },
60 0, 0, /* No members */
61 &asn_SPC_BIT_STRING_specs
65 * BIT STRING generic constraint.
68 BIT_STRING_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
69 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
70 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
73 if((st->size == 0 && st->bits_unused)
74 || st->bits_unused < 0 || st->bits_unused > 7) {
75 ASN__CTFAIL(app_key, td, sptr,
76 "%s: invalid padding byte (%s:%d)",
77 td->name, __FILE__, __LINE__);
81 ASN__CTFAIL(app_key, td, sptr,
82 "%s: value not given (%s:%d)",
83 td->name, __FILE__, __LINE__);
90 static const char *_bit_pattern[16] = {
91 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
92 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
96 BIT_STRING_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
97 int ilevel, enum xer_encoder_flags_e flags,
98 asn_app_consume_bytes_f *cb, void *app_key) {
99 asn_enc_rval_t er = {0, 0, 0};
102 char *scend = scratch + (sizeof(scratch) - 10);
103 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
104 int xcan = (flags & XER_F_CANONICAL);
114 end = buf + st->size - 1; /* Last byte is special */
119 for(; buf < end; buf++) {
121 int nline = xcan?0:(((buf - st->buf) % 8) == 0);
122 if(p >= scend || nline) {
123 ASN__CALLBACK(scratch, p - scratch);
125 if(nline) ASN__TEXT_INDENT(1, ilevel);
127 memcpy(p + 0, _bit_pattern[v >> 4], 4);
128 memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
132 if(!xcan && ((buf - st->buf) % 8) == 0)
133 ASN__TEXT_INDENT(1, ilevel);
134 ASN__CALLBACK(scratch, p - scratch);
139 int ubits = st->bits_unused;
141 for(i = 7; i >= ubits; i--)
142 *p++ = (v & (1 << i)) ? 0x31 : 0x30;
143 ASN__CALLBACK(scratch, p - scratch);
146 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
155 * BIT STRING specific contents printer.
158 BIT_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
159 asn_app_consume_bytes_f *cb, void *app_key) {
160 const char * const h2c = "0123456789ABCDEF";
162 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
167 (void)td; /* Unused argument */
170 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
174 end = buf + st->size;
179 for(; buf < end; buf++) {
180 if((buf - st->buf) % 16 == 0 && (st->size > 16)
183 /* Dump the string */
184 if(cb(scratch, p - scratch, app_key) < 0) return -1;
187 *p++ = h2c[*buf >> 4];
188 *p++ = h2c[*buf & 0x0F];
193 p--; /* Eat the tailing space */
195 if((st->size > 16)) {
199 /* Dump the incomplete 16-bytes row */
200 if(cb(scratch, p - scratch, app_key) < 0)
204 if(st->bits_unused) {
205 int ret = snprintf(scratch, sizeof(scratch), " (%d bit%s unused)",
206 st->bits_unused, st->bits_unused == 1 ? "" : "s");
207 assert(ret > 0 && ret < (ssize_t)sizeof(scratch));
208 if(ret > 0 && ret < (ssize_t)sizeof(scratch)
209 && cb(scratch, ret, app_key) < 0)
217 * Non-destructively remove the trailing 0-bits from the given bit string.
219 static const BIT_STRING_t *
220 BIT_STRING__compactify(const BIT_STRING_t *st, BIT_STRING_t *tmp) {
223 const uint8_t *c_buf;
228 assert(st->bits_unused == 0);
231 for(b = &st->buf[st->size - 1]; b > st->buf && *b == 0; b--) {
234 /* b points to the last byte which may contain data */
239 if(v & 0x0F) unused -= 4;
240 if(v & 0x33) unused -= 2;
241 if(v & 0x55) unused -= 1;
242 tmp->size = b-st->buf + 1;
243 tmp->bits_unused = unused;
245 tmp->size = b-st->buf;
246 tmp->bits_unused = 0;
249 assert(b >= st->buf);
252 unconst.c_buf = st->buf;
253 tmp->buf = unconst.nc_buf;
258 * Lexicographically compare the common prefix of both strings,
259 * and if it is the same return -1 for the smallest string.
262 BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
265 * Remove information about trailing bits, since
266 * X.680 (08/2015) #22.7 "ensure that different semantics are not"
267 * "associated with [values that differ only in] the trailing 0 bits."
269 BIT_STRING_t compact_a, compact_b;
270 const BIT_STRING_t *a = BIT_STRING__compactify(aptr, &compact_a);
271 const BIT_STRING_t *b = BIT_STRING__compactify(bptr, &compact_b);
272 const asn_OCTET_STRING_specifics_t *specs = td->specifics;
274 assert(specs && specs->subvariant == ASN_OSUBV_BIT);
277 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
278 int ret = memcmp(a->buf, b->buf, common_prefix_size);
280 /* Figure out which string with equal prefixes is longer. */
281 if(a->size < b->size) {
283 } else if(a->size > b->size) {
286 /* Figure out how many unused bits */
287 if(a->bits_unused > b->bits_unused) {
289 } else if(a->bits_unused < b->bits_unused) {
298 } else if(!a && !b) {
307 #ifndef ASN_DISABLE_PER_SUPPORT
310 #define RETURN(_code) \
312 asn_dec_rval_t tmprval; \
313 tmprval.code = _code; \
314 tmprval.consumed = consumed_myself; \
318 static asn_per_constraint_t asn_DEF_BIT_STRING_constraint_size = {
319 APC_SEMI_CONSTRAINED, -1, -1, 0, 0};
322 BIT_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
323 const asn_TYPE_descriptor_t *td,
324 const asn_per_constraints_t *constraints, void **sptr,
325 asn_per_data_t *pd) {
326 const asn_OCTET_STRING_specifics_t *specs = td->specifics
327 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
328 : &asn_SPC_BIT_STRING_specs;
329 const asn_per_constraints_t *pc =
330 constraints ? constraints : td->encoding_constraints.per_constraints;
331 const asn_per_constraint_t *csiz;
332 asn_dec_rval_t rval = { RC_OK, 0 };
333 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
334 ssize_t consumed_myself = 0;
342 csiz = &asn_DEF_BIT_STRING_constraint_size;
345 if(specs->subvariant != ASN_OSUBV_BIT) {
346 ASN_DEBUG("Subvariant %d is not BIT OSUBV_BIT", specs->subvariant);
351 * Allocate the string.
354 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
355 if(!st) RETURN(RC_FAIL);
358 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
359 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
360 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
362 if(csiz->flags & APC_EXTENSIBLE) {
363 int inext = per_get_few_bits(pd, 1);
364 if(inext < 0) RETURN(RC_WMORE);
366 csiz = &asn_DEF_BIT_STRING_constraint_size;
370 if(csiz->effective_bits >= 0) {
372 st->size = (csiz->upper_bound + 7) >> 3;
373 st->buf = (uint8_t *)MALLOC(st->size + 1);
374 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
377 /* X.691, #16.5: zero-length encoding */
378 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
379 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
380 if(csiz->effective_bits == 0) {
382 ASN_DEBUG("Encoding BIT STRING size %ld", csiz->upper_bound);
383 ret = per_get_many_bits(pd, st->buf, 0, csiz->upper_bound);
384 if(ret < 0) RETURN(RC_WMORE);
385 consumed_myself += csiz->upper_bound;
386 st->buf[st->size] = 0;
387 st->bits_unused = (8 - (csiz->upper_bound & 0x7)) & 0x7;
399 /* Get the PER length */
400 raw_len = uper_get_length(pd, csiz->effective_bits, csiz->lower_bound,
402 if(raw_len < 0) RETURN(RC_WMORE);
403 if(raw_len == 0 && st->buf) break;
405 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
406 (long)csiz->effective_bits, (long)raw_len,
407 repeat ? "repeat" : "once", td->name);
409 len_bytes = (len_bits + 7) >> 3;
410 if(len_bits & 0x7) st->bits_unused = 8 - (len_bits & 0x7);
411 /* len_bits be multiple of 16K if repeat is set */
412 p = REALLOC(st->buf, st->size + len_bytes + 1);
413 if(!p) RETURN(RC_FAIL);
414 st->buf = (uint8_t *)p;
416 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
417 if(ret < 0) RETURN(RC_WMORE);
418 st->size += len_bytes;
420 st->buf[st->size] = 0; /* nul-terminate */
426 BIT_STRING_encode_uper(const asn_TYPE_descriptor_t *td,
427 const asn_per_constraints_t *constraints,
428 const void *sptr, asn_per_outp_t *po) {
429 const asn_OCTET_STRING_specifics_t *specs =
430 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
431 : &asn_SPC_BIT_STRING_specs;
432 const asn_per_constraints_t *pc =
433 constraints ? constraints : td->encoding_constraints.per_constraints;
434 const asn_per_constraint_t *csiz;
435 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
436 BIT_STRING_t compact_bstr; /* Do not modify this directly! */
437 asn_enc_rval_t er = { 0, 0, 0 };
438 int inext = 0; /* Lies not within extension root */
444 if(!st || (!st->buf && st->size))
447 if(specs->subvariant == ASN_OSUBV_BIT) {
448 if((st->size == 0 && st->bits_unused) || (st->bits_unused & ~7))
457 csiz = &asn_DEF_BIT_STRING_constraint_size;
459 ct_extensible = csiz->flags & APC_EXTENSIBLE;
461 /* Figure out the size without the trailing bits */
462 st = BIT_STRING__compactify(st, &compact_bstr);
463 size_in_bits = 8 * st->size - st->bits_unused;
466 "Encoding %s into %" ASN_PRI_SIZE " bits"
467 " (%ld..%ld, effective %d)%s",
468 td->name, size_in_bits, csiz->lower_bound, csiz->upper_bound,
469 csiz->effective_bits, ct_extensible ? " EXT" : "");
471 /* Figure out whether size lies within PER visible constraint */
473 if(csiz->effective_bits >= 0) {
474 if((ssize_t)size_in_bits > csiz->upper_bound) {
476 csiz = &asn_DEF_BIT_STRING_constraint_size;
487 /* Declare whether length is [not] within extension root */
488 if(per_put_few_bits(po, inext, 1))
492 if(csiz->effective_bits >= 0 && !inext) {
493 int add_trailer = (ssize_t)size_in_bits < csiz->lower_bound;
495 "Encoding %" ASN_PRI_SIZE " bytes (%ld), length (in %d bits) trailer %d; actual "
496 "value %" ASN_PRI_SSIZE "",
497 st->size, size_in_bits - csiz->lower_bound, csiz->effective_bits,
499 add_trailer ? 0 : (ssize_t)size_in_bits - csiz->lower_bound);
500 ret = per_put_few_bits(
501 po, add_trailer ? 0 : (ssize_t)size_in_bits - csiz->lower_bound,
502 csiz->effective_bits);
503 if(ret) ASN__ENCODE_FAILED;
504 ret = per_put_many_bits(po, st->buf, size_in_bits);
505 if(ret) ASN__ENCODE_FAILED;
507 static const uint8_t zeros[16];
508 size_t trailing_zero_bits = csiz->lower_bound - size_in_bits;
509 while(trailing_zero_bits > 0) {
510 if(trailing_zero_bits > 8 * sizeof(zeros)) {
511 ret = per_put_many_bits(po, zeros, 8 * sizeof(zeros));
512 trailing_zero_bits -= 8 * sizeof(zeros);
514 ret = per_put_many_bits(po, zeros, trailing_zero_bits);
515 trailing_zero_bits = 0;
517 if(ret) ASN__ENCODE_FAILED;
523 ASN_DEBUG("Encoding %" ASN_PRI_SIZE " bytes", st->size);
528 ssize_t maySave = uper_put_length(po, size_in_bits, &need_eom);
529 if(maySave < 0) ASN__ENCODE_FAILED;
531 ASN_DEBUG("Encoding %" ASN_PRI_SSIZE " of %" ASN_PRI_SIZE "", maySave, size_in_bits);
533 ret = per_put_many_bits(po, buf, maySave);
534 if(ret) ASN__ENCODE_FAILED;
537 size_in_bits -= maySave;
538 assert(!(maySave & 0x07) || !size_in_bits);
539 if(need_eom && uper_put_length(po, 0, 0))
540 ASN__ENCODE_FAILED; /* End of Message length */
541 } while(size_in_bits);
546 #endif /* ASN_DISABLE_PER_SUPPORT */
548 asn_random_fill_result_t
549 BIT_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
550 const asn_encoding_constraints_t *constraints,
552 const asn_OCTET_STRING_specifics_t *specs =
553 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
554 : &asn_SPC_BIT_STRING_specs;
555 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
556 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
557 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
558 static unsigned lengths[] = {0, 1, 2, 3, 4, 8,
559 126, 127, 128, 16383, 16384, 16385,
560 65534, 65535, 65536, 65537};
564 size_t rnd_bits, rnd_len;
567 if(max_length == 0) return result_skipped;
569 switch(specs->subvariant) {
571 return result_failed;
578 /* Figure out how far we should go */
579 rnd_bits = lengths[asn_random_between(
580 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
581 if(!constraints || !constraints->per_constraints)
582 constraints = &td->encoding_constraints;
583 if(constraints->per_constraints) {
584 const asn_per_constraint_t *pc = &constraints->per_constraints->size;
585 if(pc->flags & APC_CONSTRAINED) {
586 long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
588 : (ssize_t)max_length;
589 if(max_length < (size_t)pc->lower_bound) {
590 return result_skipped;
592 if(pc->flags & APC_EXTENSIBLE) {
593 switch(asn_random_between(0, 5)) {
595 if(pc->lower_bound > 0) {
596 rnd_bits = pc->lower_bound - 1;
601 rnd_bits = pc->upper_bound + 1;
604 /* Keep rnd_bits from the table */
605 if(rnd_bits < max_length) {
610 rnd_bits = asn_random_between(pc->lower_bound,
611 suggested_upper_bound);
615 asn_random_between(pc->lower_bound, suggested_upper_bound);
618 rnd_bits = asn_random_between(0, max_length - 1);
620 } else if(rnd_bits >= max_length) {
621 rnd_bits = asn_random_between(0, max_length - 1);
624 rnd_len = (rnd_bits + 7) / 8;
625 buf = CALLOC(1, rnd_len + 1);
626 if(!buf) return result_failed;
628 bend = &buf[rnd_len];
630 for(b = buf; b < bend; b++) {
631 *(uint8_t *)b = asn_random_between(0, 255);
633 *b = 0; /* Zero-terminate just in case. */
639 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
642 return result_failed;
648 st->bits_unused = (8 - (rnd_bits & 0x7)) & 0x7;
649 if(st->bits_unused) {
650 assert(st->size > 0);
651 st->buf[st->size-1] &= 0xff << st->bits_unused;
654 result_ok.length = st->size;