3 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <BIT_STRING.h>
8 #include <asn_internal.h>
11 * BIT STRING basic type description.
13 static const ber_tlv_tag_t asn_DEF_BIT_STRING_tags[] = {
14 (ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
16 asn_OCTET_STRING_specifics_t asn_SPC_BIT_STRING_specs = {
18 offsetof(BIT_STRING_t, _asn_ctx),
21 asn_TYPE_operation_t asn_OP_BIT_STRING = {
22 OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
25 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
26 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
27 OCTET_STRING_decode_xer_binary,
28 BIT_STRING_encode_xer,
29 #ifdef ASN_DISABLE_OER_SUPPORT
33 BIT_STRING_decode_oer,
34 BIT_STRING_encode_oer,
35 #endif /* ASN_DISABLE_OER_SUPPORT */
36 #ifdef ASN_DISABLE_PER_SUPPORT
42 BIT_STRING_decode_uper, /* Unaligned PER decoder */
43 BIT_STRING_encode_uper, /* Unaligned PER encoder */
44 OCTET_STRING_decode_aper, /* Aligned PER decoder */
45 OCTET_STRING_encode_aper, /* Aligned PER encoder */
46 #endif /* ASN_DISABLE_PER_SUPPORT */
47 BIT_STRING_random_fill,
48 0 /* Use generic outmost tag fetcher */
50 asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
54 asn_DEF_BIT_STRING_tags,
55 sizeof(asn_DEF_BIT_STRING_tags)
56 / sizeof(asn_DEF_BIT_STRING_tags[0]),
57 asn_DEF_BIT_STRING_tags, /* Same as above */
58 sizeof(asn_DEF_BIT_STRING_tags)
59 / sizeof(asn_DEF_BIT_STRING_tags[0]),
60 { 0, 0, BIT_STRING_constraint },
61 0, 0, /* No members */
62 &asn_SPC_BIT_STRING_specs
66 * BIT STRING generic constraint.
69 BIT_STRING_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
70 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
71 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
74 if((st->size == 0 && st->bits_unused)
75 || st->bits_unused < 0 || st->bits_unused > 7) {
76 ASN__CTFAIL(app_key, td, sptr,
77 "%s: invalid padding byte (%s:%d)",
78 td->name, __FILE__, __LINE__);
82 ASN__CTFAIL(app_key, td, sptr,
83 "%s: value not given (%s:%d)",
84 td->name, __FILE__, __LINE__);
91 static const char *_bit_pattern[16] = {
92 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
93 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
97 BIT_STRING_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
98 int ilevel, enum xer_encoder_flags_e flags,
99 asn_app_consume_bytes_f *cb, void *app_key) {
100 asn_enc_rval_t er = {0, 0, 0};
103 char *scend = scratch + (sizeof(scratch) - 10);
104 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
105 int xcan = (flags & XER_F_CANONICAL);
115 end = buf + st->size - 1; /* Last byte is special */
120 for(; buf < end; buf++) {
122 int nline = xcan?0:(((buf - st->buf) % 8) == 0);
123 if(p >= scend || nline) {
124 ASN__CALLBACK(scratch, p - scratch);
126 if(nline) ASN__TEXT_INDENT(1, ilevel);
128 memcpy(p + 0, _bit_pattern[v >> 4], 4);
129 memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
133 if(!xcan && ((buf - st->buf) % 8) == 0)
134 ASN__TEXT_INDENT(1, ilevel);
135 ASN__CALLBACK(scratch, p - scratch);
140 int ubits = st->bits_unused;
142 for(i = 7; i >= ubits; i--)
143 *p++ = (v & (1 << i)) ? 0x31 : 0x30;
144 ASN__CALLBACK(scratch, p - scratch);
147 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
156 * BIT STRING specific contents printer.
159 BIT_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
160 asn_app_consume_bytes_f *cb, void *app_key) {
161 const char * const h2c = "0123456789ABCDEF";
163 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
168 (void)td; /* Unused argument */
171 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
175 end = buf + st->size;
180 for(; buf < end; buf++) {
181 if((buf - st->buf) % 16 == 0 && (st->size > 16)
184 /* Dump the string */
185 if(cb(scratch, p - scratch, app_key) < 0) return -1;
188 *p++ = h2c[*buf >> 4];
189 *p++ = h2c[*buf & 0x0F];
194 p--; /* Eat the tailing space */
196 if((st->size > 16)) {
200 /* Dump the incomplete 16-bytes row */
201 if(cb(scratch, p - scratch, app_key) < 0)
205 if(st->bits_unused) {
206 int ret = snprintf(scratch, sizeof(scratch), " (%d bit%s unused)",
207 st->bits_unused, st->bits_unused == 1 ? "" : "s");
208 assert(ret > 0 && ret < (ssize_t)sizeof(scratch));
209 if(ret > 0 && ret < (ssize_t)sizeof(scratch)
210 && cb(scratch, ret, app_key) < 0)
218 * Non-destructively remove the trailing 0-bits from the given bit string.
220 static const BIT_STRING_t *
221 BIT_STRING__compactify(const BIT_STRING_t *st, BIT_STRING_t *tmp) {
224 const uint8_t *c_buf;
229 assert(st->bits_unused == 0);
232 for(b = &st->buf[st->size - 1]; b > st->buf && *b == 0; b--) {
235 /* b points to the last byte which may contain data */
240 if(v & 0x0F) unused -= 4;
241 if(v & 0x33) unused -= 2;
242 if(v & 0x55) unused -= 1;
243 tmp->size = b-st->buf + 1;
244 tmp->bits_unused = unused;
246 tmp->size = b-st->buf;
247 tmp->bits_unused = 0;
250 assert(b >= st->buf);
253 unconst.c_buf = st->buf;
254 tmp->buf = unconst.nc_buf;
259 * Lexicographically compare the common prefix of both strings,
260 * and if it is the same return -1 for the smallest string.
263 BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
266 * Remove information about trailing bits, since
267 * X.680 (08/2015) #22.7 "ensure that different semantics are not"
268 * "associated with [values that differ only in] the trailing 0 bits."
270 BIT_STRING_t compact_a, compact_b;
271 const BIT_STRING_t *a = BIT_STRING__compactify(aptr, &compact_a);
272 const BIT_STRING_t *b = BIT_STRING__compactify(bptr, &compact_b);
273 const asn_OCTET_STRING_specifics_t *specs = td->specifics;
275 assert(specs && specs->subvariant == ASN_OSUBV_BIT);
278 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
279 int ret = memcmp(a->buf, b->buf, common_prefix_size);
281 /* Figure out which string with equal prefixes is longer. */
282 if(a->size < b->size) {
284 } else if(a->size > b->size) {
287 /* Figure out how many unused bits */
288 if(a->bits_unused > b->bits_unused) {
290 } else if(a->bits_unused < b->bits_unused) {
299 } else if(!a && !b) {
308 #ifndef ASN_DISABLE_PER_SUPPORT
311 #define RETURN(_code) \
313 asn_dec_rval_t tmprval; \
314 tmprval.code = _code; \
315 tmprval.consumed = consumed_myself; \
319 static asn_per_constraint_t asn_DEF_BIT_STRING_constraint_size = {
320 APC_SEMI_CONSTRAINED, -1, -1, 0, 0};
323 BIT_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
324 const asn_TYPE_descriptor_t *td,
325 const asn_per_constraints_t *constraints, void **sptr,
326 asn_per_data_t *pd) {
327 const asn_OCTET_STRING_specifics_t *specs = td->specifics
328 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
329 : &asn_SPC_BIT_STRING_specs;
330 const asn_per_constraints_t *pc =
331 constraints ? constraints : td->encoding_constraints.per_constraints;
332 const asn_per_constraint_t *csiz;
333 asn_dec_rval_t rval = { RC_OK, 0 };
334 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
335 ssize_t consumed_myself = 0;
343 csiz = &asn_DEF_BIT_STRING_constraint_size;
346 if(specs->subvariant != ASN_OSUBV_BIT) {
347 ASN_DEBUG("Subvariant %d is not BIT OSUBV_BIT", specs->subvariant);
352 * Allocate the string.
355 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
356 if(!st) RETURN(RC_FAIL);
359 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
360 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
361 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
363 if(csiz->flags & APC_EXTENSIBLE) {
364 int inext = per_get_few_bits(pd, 1);
365 if(inext < 0) RETURN(RC_WMORE);
367 csiz = &asn_DEF_BIT_STRING_constraint_size;
371 if(csiz->effective_bits >= 0) {
373 st->size = (csiz->upper_bound + 7) >> 3;
374 st->buf = (uint8_t *)MALLOC(st->size + 1);
375 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
378 /* X.691, #16.5: zero-length encoding */
379 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
380 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
381 if(csiz->effective_bits == 0) {
383 ASN_DEBUG("Encoding BIT STRING size %ld", csiz->upper_bound);
384 ret = per_get_many_bits(pd, st->buf, 0, csiz->upper_bound);
385 if(ret < 0) RETURN(RC_WMORE);
386 consumed_myself += csiz->upper_bound;
387 st->buf[st->size] = 0;
388 st->bits_unused = (8 - (csiz->upper_bound & 0x7)) & 0x7;
400 /* Get the PER length */
401 raw_len = uper_get_length(pd, csiz->effective_bits, csiz->lower_bound,
403 if(raw_len < 0) RETURN(RC_WMORE);
404 if(raw_len == 0 && st->buf) break;
406 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
407 (long)csiz->effective_bits, (long)raw_len,
408 repeat ? "repeat" : "once", td->name);
410 len_bytes = (len_bits + 7) >> 3;
411 if(len_bits & 0x7) st->bits_unused = 8 - (len_bits & 0x7);
412 /* len_bits be multiple of 16K if repeat is set */
413 p = REALLOC(st->buf, st->size + len_bytes + 1);
414 if(!p) RETURN(RC_FAIL);
415 st->buf = (uint8_t *)p;
417 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
418 if(ret < 0) RETURN(RC_WMORE);
419 st->size += len_bytes;
421 st->buf[st->size] = 0; /* nul-terminate */
427 BIT_STRING_encode_uper(const asn_TYPE_descriptor_t *td,
428 const asn_per_constraints_t *constraints,
429 const void *sptr, asn_per_outp_t *po) {
430 const asn_OCTET_STRING_specifics_t *specs =
431 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
432 : &asn_SPC_BIT_STRING_specs;
433 const asn_per_constraints_t *pc =
434 constraints ? constraints : td->encoding_constraints.per_constraints;
435 const asn_per_constraint_t *csiz;
436 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
437 BIT_STRING_t compact_bstr; /* Do not modify this directly! */
438 asn_enc_rval_t er = { 0, 0, 0 };
439 int inext = 0; /* Lies not within extension root */
445 if(!st || (!st->buf && st->size))
448 if(specs->subvariant == ASN_OSUBV_BIT) {
449 if((st->size == 0 && st->bits_unused) || (st->bits_unused & ~7))
458 csiz = &asn_DEF_BIT_STRING_constraint_size;
460 ct_extensible = csiz->flags & APC_EXTENSIBLE;
462 /* Figure out the size without the trailing bits */
463 st = BIT_STRING__compactify(st, &compact_bstr);
464 size_in_bits = 8 * st->size - st->bits_unused;
467 "Encoding %s into %" ASN_PRI_SIZE " bits"
468 " (%ld..%ld, effective %d)%s",
469 td->name, size_in_bits, csiz->lower_bound, csiz->upper_bound,
470 csiz->effective_bits, ct_extensible ? " EXT" : "");
472 /* Figure out whether size lies within PER visible constraint */
474 if(csiz->effective_bits >= 0) {
475 if((ssize_t)size_in_bits > csiz->upper_bound) {
477 csiz = &asn_DEF_BIT_STRING_constraint_size;
488 /* Declare whether length is [not] within extension root */
489 if(per_put_few_bits(po, inext, 1))
493 if(csiz->effective_bits >= 0 && !inext) {
494 int add_trailer = (ssize_t)size_in_bits < csiz->lower_bound;
496 "Encoding %" ASN_PRI_SIZE " bytes (%ld), length (in %d bits) trailer %d; actual "
497 "value %" ASN_PRI_SSIZE "",
498 st->size, size_in_bits - csiz->lower_bound, csiz->effective_bits,
500 add_trailer ? 0 : (ssize_t)size_in_bits - csiz->lower_bound);
501 ret = per_put_few_bits(
502 po, add_trailer ? 0 : (ssize_t)size_in_bits - csiz->lower_bound,
503 csiz->effective_bits);
504 if(ret) ASN__ENCODE_FAILED;
505 ret = per_put_many_bits(po, st->buf, size_in_bits);
506 if(ret) ASN__ENCODE_FAILED;
508 static const uint8_t zeros[16];
509 size_t trailing_zero_bits = csiz->lower_bound - size_in_bits;
510 while(trailing_zero_bits > 0) {
511 if(trailing_zero_bits > 8 * sizeof(zeros)) {
512 ret = per_put_many_bits(po, zeros, 8 * sizeof(zeros));
513 trailing_zero_bits -= 8 * sizeof(zeros);
515 ret = per_put_many_bits(po, zeros, trailing_zero_bits);
516 trailing_zero_bits = 0;
518 if(ret) ASN__ENCODE_FAILED;
524 ASN_DEBUG("Encoding %" ASN_PRI_SIZE " bytes", st->size);
529 ssize_t maySave = uper_put_length(po, size_in_bits, &need_eom);
530 if(maySave < 0) ASN__ENCODE_FAILED;
532 ASN_DEBUG("Encoding %" ASN_PRI_SSIZE " of %" ASN_PRI_SIZE "", maySave, size_in_bits);
534 ret = per_put_many_bits(po, buf, maySave);
535 if(ret) ASN__ENCODE_FAILED;
538 size_in_bits -= maySave;
539 assert(!(maySave & 0x07) || !size_in_bits);
540 if(need_eom && uper_put_length(po, 0, 0))
541 ASN__ENCODE_FAILED; /* End of Message length */
542 } while(size_in_bits);
547 #endif /* ASN_DISABLE_PER_SUPPORT */
549 asn_random_fill_result_t
550 BIT_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
551 const asn_encoding_constraints_t *constraints,
553 const asn_OCTET_STRING_specifics_t *specs =
554 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
555 : &asn_SPC_BIT_STRING_specs;
556 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
557 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
558 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
559 static unsigned lengths[] = {0, 1, 2, 3, 4, 8,
560 126, 127, 128, 16383, 16384, 16385,
561 65534, 65535, 65536, 65537};
565 size_t rnd_bits, rnd_len;
568 if(max_length == 0) return result_skipped;
570 switch(specs->subvariant) {
572 return result_failed;
579 /* Figure out how far we should go */
580 rnd_bits = lengths[asn_random_between(
581 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
582 if(!constraints || !constraints->per_constraints)
583 constraints = &td->encoding_constraints;
584 if(constraints->per_constraints) {
585 const asn_per_constraint_t *pc = &constraints->per_constraints->size;
586 if(pc->flags & APC_CONSTRAINED) {
587 long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
589 : (ssize_t)max_length;
590 if(max_length < (size_t)pc->lower_bound) {
591 return result_skipped;
593 if(pc->flags & APC_EXTENSIBLE) {
594 switch(asn_random_between(0, 5)) {
596 if(pc->lower_bound > 0) {
597 rnd_bits = pc->lower_bound - 1;
602 rnd_bits = pc->upper_bound + 1;
605 /* Keep rnd_bits from the table */
606 if(rnd_bits < max_length) {
611 rnd_bits = asn_random_between(pc->lower_bound,
612 suggested_upper_bound);
616 asn_random_between(pc->lower_bound, suggested_upper_bound);
619 rnd_bits = asn_random_between(0, max_length - 1);
621 } else if(rnd_bits >= max_length) {
622 rnd_bits = asn_random_between(0, max_length - 1);
625 rnd_len = (rnd_bits + 7) / 8;
626 buf = CALLOC(1, rnd_len + 1);
627 if(!buf) return result_failed;
629 bend = &buf[rnd_len];
631 for(b = buf; b < bend; b++) {
632 *(uint8_t *)b = asn_random_between(0, 255);
634 *b = 0; /* Zero-terminate just in case. */
640 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
643 return result_failed;
649 st->bits_unused = (8 - (rnd_bits & 0x7)) & 0x7;
650 if(st->bits_unused) {
651 assert(st->size > 0);
652 st->buf[st->size-1] &= 0xff << st->bits_unused;
655 result_ok.length = st->size;