2 * Copyright (c) 2003-2019 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
8 #include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
12 * INTEGER basic type description.
14 static const ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
15 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
17 asn_TYPE_operation_t asn_OP_INTEGER = {
25 #ifdef ASN_DISABLE_OER_SUPPORT
29 INTEGER_decode_oer, /* OER decoder */
30 INTEGER_encode_oer, /* Canonical OER encoder */
31 #endif /* ASN_DISABLE_OER_SUPPORT */
32 #ifdef ASN_DISABLE_PER_SUPPORT
36 INTEGER_decode_uper, /* Unaligned PER decoder */
37 INTEGER_encode_uper, /* Unaligned PER encoder */
38 #endif /* ASN_DISABLE_PER_SUPPORT */
40 0 /* Use generic outmost tag fetcher */
42 asn_TYPE_descriptor_t asn_DEF_INTEGER = {
47 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
48 asn_DEF_INTEGER_tags, /* Same as above */
49 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
50 { 0, 0, asn_generic_no_constraint },
51 0, 0, /* No members */
56 * Encode INTEGER type using DER.
59 INTEGER_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
60 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
62 const INTEGER_t *st = (const INTEGER_t *)sptr;
64 INTEGER_t effective_integer;
66 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
67 cb?"Encoding":"Estimating", td->name, tag_mode);
70 * Canonicalize integer in the buffer.
71 * (Remove too long sign extension, remove some first 0x00 bytes)
74 uint8_t *buf = st->buf;
75 uint8_t *end1 = buf + st->size - 1;
78 /* Compute the number of superfluous leading bytes */
79 for(; buf < end1; buf++) {
81 * If the contents octets of an integer value encoding
82 * consist of more than one octet, then the bits of the
83 * first octet and bit 8 of the second octet:
84 * a) shall not all be ones; and
85 * b) shall not all be zero.
88 case 0x00: if((buf[1] & 0x80) == 0)
91 case 0xff: if((buf[1] & 0x80))
98 /* Remove leading superfluous bytes from the integer */
99 shift = buf - st->buf;
102 const uint8_t *c_buf;
105 unconst.c_buf = st->buf;
106 effective_integer.buf = unconst.nc_buf + shift;
107 effective_integer.size = st->size - shift;
109 st = &effective_integer;
113 rval = der_encode_primitive(td, st, tag_mode, tag, cb, app_key);
114 if(rval.structure_ptr == &effective_integer) {
115 rval.structure_ptr = sptr;
120 static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(
121 const asn_INTEGER_specifics_t *specs, const char *lstart,
125 * INTEGER specific human-readable output.
128 INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
129 const asn_INTEGER_specifics_t *specs =
130 (const asn_INTEGER_specifics_t *)td->specifics;
132 uint8_t *buf = st->buf;
133 uint8_t *buf_end = st->buf + st->size;
139 if(specs && specs->field_unsigned)
140 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
142 ret = asn_INTEGER2imax(st, &value);
144 /* Simple case: the integer size is small */
146 const asn_INTEGER_enum_map_t *el;
147 el = (value >= 0 || !specs || !specs->field_unsigned)
148 ? INTEGER_map_value2enum(specs, value) : 0;
151 return asn__format_to_callback(cb, app_key,
152 "%" ASN_PRIdMAX " (%s)", value, el->enum_name);
154 return asn__format_to_callback(cb, app_key,
155 "<%s/>", el->enum_name);
156 } else if(plainOrXER && specs && specs->strict_enumeration) {
157 ASN_DEBUG("ASN.1 forbids dealing with "
158 "unknown value of ENUMERATED type");
162 return asn__format_to_callback(cb, app_key,
163 (specs && specs->field_unsigned)
168 } else if(plainOrXER && specs && specs->strict_enumeration) {
170 * Here and earlier, we cannot encode the ENUMERATED values
171 * if there is no corresponding identifier.
173 ASN_DEBUG("ASN.1 forbids dealing with "
174 "unknown value of ENUMERATED type");
179 /* Output in the long xx:yy:zz... format */
180 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
181 for(p = scratch; buf < buf_end; buf++) {
182 const char * const h2c = "0123456789ABCDEF";
183 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
185 if(cb(scratch, p - scratch, app_key) < 0)
187 wrote += p - scratch;
190 *p++ = h2c[*buf >> 4];
191 *p++ = h2c[*buf & 0x0F];
192 *p++ = 0x3a; /* ":" */
195 p--; /* Remove the last ":" */
197 wrote += p - scratch;
198 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
202 * INTEGER specific human-readable output.
205 INTEGER_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
206 asn_app_consume_bytes_f *cb, void *app_key) {
207 const INTEGER_t *st = (const INTEGER_t *)sptr;
213 ret = cb("<absent>", 8, app_key);
215 ret = INTEGER__dump(td, st, cb, app_key, 0);
217 return (ret < 0) ? -1 : 0;
223 const asn_INTEGER_enum_map_t *vemap;
224 const unsigned int *evmap;
227 INTEGER__compar_enum2value(const void *kp, const void *am) {
228 const struct e2v_key *key = (const struct e2v_key *)kp;
229 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
230 const char *ptr, *end, *name;
232 /* Remap the element (sort by different criterion) */
233 el = key->vemap + key->evmap[el - key->vemap];
235 /* Compare strings */
236 for(ptr = key->start, end = key->stop, name = el->enum_name;
237 ptr < end; ptr++, name++) {
238 if(*ptr != *name || !*name)
239 return *(const unsigned char *)ptr
240 - *(const unsigned char *)name;
242 return name[0] ? -1 : 0;
245 static const asn_INTEGER_enum_map_t *
246 INTEGER_map_enum2value(const asn_INTEGER_specifics_t *specs, const char *lstart,
248 const asn_INTEGER_enum_map_t *el_found;
249 int count = specs ? specs->map_count : 0;
253 if(!count) return NULL;
255 /* Guaranteed: assert(lstart < lstop); */
256 /* Figure out the tag name */
257 for(lstart++, lp = lstart; lp < lstop; lp++) {
259 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
260 case 0x2f: /* '/' */ case 0x3e: /* '>' */
267 if(lp == lstop) return NULL; /* No tag found */
272 key.vemap = specs->value2enum;
273 key.evmap = specs->enum2value;
274 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
275 specs->value2enum, count, sizeof(specs->value2enum[0]),
276 INTEGER__compar_enum2value);
278 /* Remap enum2value into value2enum */
279 el_found = key.vemap + key.evmap[el_found - key.vemap];
285 INTEGER__compar_value2enum(const void *kp, const void *am) {
286 long a = *(const long *)kp;
287 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
288 long b = el->nat_value;
290 else if(a == b) return 0;
294 const asn_INTEGER_enum_map_t *
295 INTEGER_map_value2enum(const asn_INTEGER_specifics_t *specs, long value) {
296 int count = specs ? specs->map_count : 0;
298 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
299 count, sizeof(specs->value2enum[0]),
300 INTEGER__compar_value2enum);
304 INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
305 void *p = MALLOC(min_size + 1);
318 * Decode the chunk of XML text encoding INTEGER.
320 static enum xer_pbd_rval
321 INTEGER__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
322 const void *chunk_buf, size_t chunk_size) {
323 INTEGER_t *st = (INTEGER_t *)sptr;
325 intmax_t hex_value = 0;
327 const char *lstart = (const char *)chunk_buf;
328 const char *lstop = lstart + chunk_size;
334 ST_DIGITS_TRAILSPACE,
337 ST_HEXDIGITS_TRAILSPACE,
341 } state = ST_LEADSPACE;
342 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
343 const char *dec_value_end = 0;
346 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
347 (long)chunk_size, *lstart, lstop[-1]);
349 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
350 return XPBD_SYSTEM_FAILURE;
353 * We may have received a tag here. It will be processed inline.
354 * Use strtoul()-like code and serialize the result.
356 for(lp = lstart; lp < lstop; lp++) {
359 case 0x09: case 0x0a: case 0x0d: case 0x20:
362 case ST_DIGITS_TRAILSPACE:
363 case ST_HEXDIGITS_TRAILSPACE:
368 state = ST_DIGITS_TRAILSPACE;
371 state = ST_HEXDIGITS_TRAILSPACE;
378 if(state == ST_LEADSPACE) {
380 dec_value_start = lp;
381 state = ST_WAITDIGITS;
386 if(state == ST_LEADSPACE) {
388 dec_value_start = lp;
389 state = ST_WAITDIGITS;
393 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
394 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
396 case ST_DIGITS: continue;
397 case ST_SKIPSPHEX: /* Fall through */
399 hex_value = (lv - 0x30) << 4;
400 state = ST_HEXDIGIT2;
403 hex_value += (lv - 0x30);
405 st->buf[st->size++] = (uint8_t)hex_value;
408 return XPBD_BROKEN_ENCODING;
411 dec_value_start = lp;
420 case 0x3c: /* '<', start of XML encoded enumeration */
421 if(state == ST_LEADSPACE) {
422 const asn_INTEGER_enum_map_t *el;
423 el = INTEGER_map_enum2value(
424 (const asn_INTEGER_specifics_t *)
425 td->specifics, lstart, lstop);
427 ASN_DEBUG("Found \"%s\" => %ld",
428 el->enum_name, el->nat_value);
429 dec_value = el->nat_value;
434 ASN_DEBUG("Unknown identifier for INTEGER");
436 return XPBD_BROKEN_ENCODING;
438 if(state == ST_HEXCOLON) {
439 /* This colon is expected */
440 state = ST_HEXDIGIT1;
442 } else if(state == ST_DIGITS) {
443 /* The colon here means that we have
444 * decoded the first two hexadecimal
445 * places as a decimal value.
446 * Switch decoding mode. */
447 ASN_DEBUG("INTEGER re-evaluate as hex form");
448 state = ST_SKIPSPHEX;
453 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
457 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
458 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
461 case ST_LEADSPACE: /* Fall through */
463 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
466 state = ST_HEXDIGIT2;
469 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
471 st->buf[st->size++] = (uint8_t)hex_value;
475 ASN_DEBUG("INTEGER re-evaluate as hex form");
476 state = ST_SKIPSPHEX;
486 /* Found extra non-numeric stuff */
487 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
488 lv, (long)(lp - lstart));
489 state = ST_UNEXPECTED;
495 /* Got a complete and valid enumeration encoded as a tag. */
498 dec_value_end = lstop;
500 case ST_DIGITS_TRAILSPACE:
501 /* The last symbol encountered was a digit. */
502 switch(asn_strtoimax_lim(dec_value_start, &dec_value_end, &dec_value)) {
504 if(dec_value >= LONG_MIN && dec_value <= LONG_MAX) {
508 * We model INTEGER on long for XER,
509 * to avoid rewriting all the tests at once.
511 ASN_DEBUG("INTEGER exceeds long range");
514 case ASN_STRTOX_ERROR_RANGE:
515 ASN_DEBUG("INTEGER decode %s hit range limit", td->name);
516 return XPBD_DECODER_LIMIT;
517 case ASN_STRTOX_ERROR_INVAL:
518 case ASN_STRTOX_EXPECT_MORE:
519 case ASN_STRTOX_EXTRA_DATA:
520 return XPBD_BROKEN_ENCODING;
524 case ST_HEXDIGITS_TRAILSPACE:
525 st->buf[st->size] = 0; /* Just in case termination */
526 return XPBD_BODY_CONSUMED;
530 return XPBD_BROKEN_ENCODING;
532 /* Content not found */
533 return XPBD_NOT_BODY_IGNORE;
536 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
537 return XPBD_BROKEN_ENCODING; /* No digits */
541 * Convert the result of parsing of enumeration or a straight
542 * decimal value into a BER representation.
544 if(asn_imax2INTEGER(st, dec_value)) {
545 ASN_DEBUG("INTEGER decode %s conversion failed", td->name);
546 return XPBD_SYSTEM_FAILURE;
549 return XPBD_BODY_CONSUMED;
553 INTEGER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
554 const asn_TYPE_descriptor_t *td, void **sptr,
555 const char *opt_mname, const void *buf_ptr, size_t size) {
556 return xer_decode_primitive(opt_codec_ctx, td,
557 sptr, sizeof(INTEGER_t), opt_mname,
558 buf_ptr, size, INTEGER__xer_body_decode);
562 INTEGER_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
563 int ilevel, enum xer_encoder_flags_e flags,
564 asn_app_consume_bytes_f *cb, void *app_key) {
565 const INTEGER_t *st = (const INTEGER_t *)sptr;
574 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
575 if(er.encoded < 0) ASN__ENCODE_FAILED;
580 #ifndef ASN_DISABLE_PER_SUPPORT
583 INTEGER_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
584 const asn_TYPE_descriptor_t *td,
585 const asn_per_constraints_t *constraints, void **sptr,
586 asn_per_data_t *pd) {
587 const asn_INTEGER_specifics_t *specs =
588 (const asn_INTEGER_specifics_t *)td->specifics;
589 asn_dec_rval_t rval = { RC_OK, 0 };
590 INTEGER_t *st = (INTEGER_t *)*sptr;
591 const asn_per_constraint_t *ct;
597 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
598 if(!st) ASN__DECODE_FAILED;
601 if(!constraints) constraints = td->encoding_constraints.per_constraints;
602 ct = constraints ? &constraints->value : 0;
604 if(ct && ct->flags & APC_EXTENSIBLE) {
605 int inext = per_get_few_bits(pd, 1);
606 if(inext < 0) ASN__DECODE_STARVED;
614 if(ct->flags & APC_SEMI_CONSTRAINED) {
615 st->buf = (uint8_t *)CALLOC(1, 2);
616 if(!st->buf) ASN__DECODE_FAILED;
618 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
619 size_t size = (ct->range_bits + 7) >> 3;
620 st->buf = (uint8_t *)MALLOC(1 + size + 1);
621 if(!st->buf) ASN__DECODE_FAILED;
626 /* X.691-2008/11, #13.2.2, constrained whole number */
627 if(ct && ct->flags != APC_UNCONSTRAINED) {
629 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
630 if(ct->range_bits >= 0) {
631 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
634 if(specs && specs->field_unsigned) {
635 unsigned long uvalue = 0;
636 if(uper_get_constrained_whole_number(pd,
637 &uvalue, ct->range_bits))
639 ASN_DEBUG("Got value %lu + low %ld",
640 uvalue, ct->lower_bound);
641 uvalue += ct->lower_bound;
642 if(asn_ulong2INTEGER(st, uvalue))
645 unsigned long uvalue = 0;
647 if(uper_get_constrained_whole_number(pd,
648 &uvalue, ct->range_bits))
650 ASN_DEBUG("Got value %lu + low %ld",
651 uvalue, ct->lower_bound);
652 if(per_long_range_unrebase(uvalue, ct->lower_bound,
653 ct->upper_bound, &svalue)
654 || asn_long2INTEGER(st, svalue)) {
661 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
664 /* X.691, #12.2.3, #12.2.4 */
670 /* Get the PER length */
671 len = uper_get_length(pd, -1, 0, &repeat);
672 if(len < 0) ASN__DECODE_STARVED;
674 p = REALLOC(st->buf, st->size + len + 1);
675 if(!p) ASN__DECODE_FAILED;
676 st->buf = (uint8_t *)p;
678 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
679 if(ret < 0) ASN__DECODE_STARVED;
682 st->buf[st->size] = 0; /* JIC */
685 if(ct && ct->lower_bound) {
687 * TODO: replace by in-place arithmetics.
690 if(asn_INTEGER2long(st, &value))
692 if(asn_imax2INTEGER(st, value + ct->lower_bound))
700 INTEGER_encode_uper(const asn_TYPE_descriptor_t *td,
701 const asn_per_constraints_t *constraints, const void *sptr,
702 asn_per_outp_t *po) {
703 const asn_INTEGER_specifics_t *specs =
704 (const asn_INTEGER_specifics_t *)td->specifics;
706 const INTEGER_t *st = (const INTEGER_t *)sptr;
709 const asn_per_constraint_t *ct;
712 if(!st || st->size == 0) ASN__ENCODE_FAILED;
714 if(!constraints) constraints = td->encoding_constraints.per_constraints;
715 ct = constraints ? &constraints->value : 0;
721 if(specs && specs->field_unsigned) {
723 if(asn_INTEGER2ulong(st, &uval))
725 /* Check proper range */
726 if(ct->flags & APC_SEMI_CONSTRAINED) {
727 if(uval < (unsigned long)ct->lower_bound)
729 } else if(ct->range_bits >= 0) {
730 if(uval < (unsigned long)ct->lower_bound
731 || uval > (unsigned long)ct->upper_bound)
734 ASN_DEBUG("Value %lu (%02x/%" ASN_PRI_SIZE ") lb %lu ub %lu %s",
735 uval, st->buf[0], st->size,
736 ct->lower_bound, ct->upper_bound,
737 inext ? "ext" : "fix");
740 if(asn_INTEGER2long(st, &value))
742 /* Check proper range */
743 if(ct->flags & APC_SEMI_CONSTRAINED) {
744 if(value < ct->lower_bound)
746 } else if(ct->range_bits >= 0) {
747 if(value < ct->lower_bound
748 || value > ct->upper_bound)
751 ASN_DEBUG("Value %ld (%02x/%" ASN_PRI_SIZE ") lb %ld ub %ld %s",
752 value, st->buf[0], st->size,
753 ct->lower_bound, ct->upper_bound,
754 inext ? "ext" : "fix");
756 if(ct->flags & APC_EXTENSIBLE) {
757 if(per_put_few_bits(po, inext, 1))
766 /* X.691-11/2008, #13.2.2, test if constrained whole number */
767 if(ct && ct->range_bits >= 0) {
769 /* #11.5.6 -> #11.3 */
770 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
771 value, value - ct->lower_bound, ct->range_bits);
772 if(per_long_range_rebase(value, ct->lower_bound, ct->upper_bound, &v)) {
775 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
780 if(ct && ct->lower_bound) {
781 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
782 /* TODO: adjust lower bound */
786 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
788 ssize_t mayEncode = uper_put_length(po, end - buf, &need_eom);
791 if(per_put_many_bits(po, buf, 8 * mayEncode))
794 if(need_eom && uper_put_length(po, 0, 0)) ASN__ENCODE_FAILED;
800 #endif /* ASN_DISABLE_PER_SUPPORT */
803 asn__integer_convert(const uint8_t *b, const uint8_t *end) {
806 /* Perform the sign initialization */
807 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
809 value = (uintmax_t)(-1);
814 /* Conversion engine */
815 for(; b < end; b++) {
816 value = (value << 8) | *b;
823 asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
827 /* Sanity checking */
828 if(!iptr || !iptr->buf || !lptr) {
833 /* Cache the begin/end of the buffer */
834 b = iptr->buf; /* Start of the INTEGER buffer */
836 end = b + size; /* Where to stop */
838 if(size > sizeof(intmax_t)) {
839 uint8_t *end1 = end - 1;
841 * Slightly more advanced processing,
842 * able to process INTEGERs with >sizeof(intmax_t) bytes
843 * when the actual value is small, e.g. for intmax_t == int32_t
844 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
846 /* Skip out the insignificant leading bytes */
847 for(; b < end1; b++) {
849 case 0x00: if((b[1] & 0x80) == 0) continue; break;
850 case 0xff: if((b[1] & 0x80) != 0) continue; break;
856 if(size > sizeof(intmax_t)) {
857 /* Still cannot fit the sizeof(intmax_t) */
863 /* Shortcut processing of a corner case */
869 *lptr = asn__integer_convert(b, end);
873 /* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
875 asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
880 if(!iptr || !iptr->buf || !lptr) {
889 /* If all extra leading bytes are zeroes, ignore them */
890 for(; size > sizeof(value); b++, size--) {
892 /* Value won't fit into uintmax_t */
898 /* Conversion engine */
899 for(value = 0; b < end; b++)
900 value = (value << 8) | *b;
907 asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
913 if(value <= ((~(uintmax_t)0) >> 1)) {
914 return asn_imax2INTEGER(st, value);
917 buf = (uint8_t *)MALLOC(1 + sizeof(value));
920 end = buf + (sizeof(value) + 1);
921 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
922 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
923 *b = (uint8_t)(value >> shr);
925 if(st->buf) FREEMEM(st->buf);
927 st->size = 1 + sizeof(value);
933 asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
938 int littleEndian = 1; /* Run-time detection */
946 buf = (uint8_t *)(long *)MALLOC(sizeof(value));
949 if(*(char *)&littleEndian) {
950 pstart = (uint8_t *)&value + sizeof(value) - 1;
951 pend1 = (uint8_t *)&value;
954 pstart = (uint8_t *)&value;
955 pend1 = pstart + sizeof(value) - 1;
960 * If the contents octet consists of more than one octet,
961 * then bits of the first octet and bit 8 of the second octet:
962 * a) shall not all be ones; and
963 * b) shall not all be zero.
965 for(p = pstart; p != pend1; p += add) {
967 case 0x00: if((*(p+add) & 0x80) == 0)
970 case 0xff: if((*(p+add) & 0x80))
976 /* Copy the integer body */
977 for(bp = buf, pend1 += add; p != pend1; p += add)
980 if(st->buf) FREEMEM(st->buf);
988 asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
990 if(asn_INTEGER2imax(iptr, &v) == 0) {
991 if(v < LONG_MIN || v > LONG_MAX) {
1003 asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
1005 if(asn_INTEGER2umax(iptr, &v) == 0) {
1018 asn_long2INTEGER(INTEGER_t *st, long value) {
1019 return asn_imax2INTEGER(st, value);
1023 asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1024 return asn_imax2INTEGER(st, value);
1028 * Parse the number in the given string until the given *end position,
1029 * returning the position after the last parsed character back using the
1030 * same (*end) pointer.
1031 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
1033 enum asn_strtox_result_e
1034 asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
1038 const intmax_t asn1_intmax_max = ((~(uintmax_t)0) >> 1);
1039 const intmax_t upper_boundary = asn1_intmax_max / 10;
1040 intmax_t last_digit_max = asn1_intmax_max % 10;
1042 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
1053 return ASN_STRTOX_EXPECT_MORE;
1057 for(value = 0; str < (*end); str++) {
1058 if(*str >= 0x30 && *str <= 0x39) {
1060 if(value < upper_boundary) {
1061 value = value * 10 + d;
1062 } else if(value == upper_boundary) {
1063 if(d <= last_digit_max) {
1065 value = value * 10 + d;
1068 value = -value * 10 - d;
1072 // If digits continue, we're guaranteed out of range.
1074 if(*str >= 0x30 && *str <= 0x39) {
1075 return ASN_STRTOX_ERROR_RANGE;
1077 *intp = sign * value;
1078 return ASN_STRTOX_EXTRA_DATA;
1084 return ASN_STRTOX_ERROR_RANGE;
1088 return ASN_STRTOX_ERROR_RANGE;
1092 *intp = sign * value;
1093 return ASN_STRTOX_EXTRA_DATA;
1098 *intp = sign * value;
1099 return ASN_STRTOX_OK;
1103 * Parse the number in the given string until the given *end position,
1104 * returning the position after the last parsed character back using the
1105 * same (*end) pointer.
1106 * WARNING: This behavior is different from the standard strtoul/strtoumax(3).
1108 enum asn_strtox_result_e
1109 asn_strtoumax_lim(const char *str, const char **end, uintmax_t *uintp) {
1112 const uintmax_t asn1_uintmax_max = ((~(uintmax_t)0));
1113 const uintmax_t upper_boundary = asn1_uintmax_max / 10;
1114 uintmax_t last_digit_max = asn1_uintmax_max % 10;
1116 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
1120 return ASN_STRTOX_ERROR_INVAL;
1125 return ASN_STRTOX_EXPECT_MORE;
1129 for(value = 0; str < (*end); str++) {
1130 if(*str >= 0x30 && *str <= 0x39) {
1131 unsigned int d = *str - '0';
1132 if(value < upper_boundary) {
1133 value = value * 10 + d;
1134 } else if(value == upper_boundary) {
1135 if(d <= last_digit_max) {
1136 value = value * 10 + d;
1139 // If digits continue, we're guaranteed out of range.
1141 if(*str >= 0x30 && *str <= 0x39) {
1142 return ASN_STRTOX_ERROR_RANGE;
1145 return ASN_STRTOX_EXTRA_DATA;
1151 return ASN_STRTOX_ERROR_RANGE;
1155 return ASN_STRTOX_ERROR_RANGE;
1160 return ASN_STRTOX_EXTRA_DATA;
1166 return ASN_STRTOX_OK;
1169 enum asn_strtox_result_e
1170 asn_strtol_lim(const char *str, const char **end, long *lp) {
1172 switch(asn_strtoimax_lim(str, end, &value)) {
1173 case ASN_STRTOX_ERROR_RANGE:
1174 return ASN_STRTOX_ERROR_RANGE;
1175 case ASN_STRTOX_ERROR_INVAL:
1176 return ASN_STRTOX_ERROR_INVAL;
1177 case ASN_STRTOX_EXPECT_MORE:
1178 return ASN_STRTOX_EXPECT_MORE;
1180 if(value >= LONG_MIN && value <= LONG_MAX) {
1182 return ASN_STRTOX_OK;
1184 return ASN_STRTOX_ERROR_RANGE;
1186 case ASN_STRTOX_EXTRA_DATA:
1187 if(value >= LONG_MIN && value <= LONG_MAX) {
1189 return ASN_STRTOX_EXTRA_DATA;
1191 return ASN_STRTOX_ERROR_RANGE;
1195 assert(!"Unreachable");
1196 return ASN_STRTOX_ERROR_INVAL;
1199 enum asn_strtox_result_e
1200 asn_strtoul_lim(const char *str, const char **end, unsigned long *ulp) {
1202 switch(asn_strtoumax_lim(str, end, &value)) {
1203 case ASN_STRTOX_ERROR_RANGE:
1204 return ASN_STRTOX_ERROR_RANGE;
1205 case ASN_STRTOX_ERROR_INVAL:
1206 return ASN_STRTOX_ERROR_INVAL;
1207 case ASN_STRTOX_EXPECT_MORE:
1208 return ASN_STRTOX_EXPECT_MORE;
1210 if(value <= ULONG_MAX) {
1212 return ASN_STRTOX_OK;
1214 return ASN_STRTOX_ERROR_RANGE;
1216 case ASN_STRTOX_EXTRA_DATA:
1217 if(value <= ULONG_MAX) {
1219 return ASN_STRTOX_EXTRA_DATA;
1221 return ASN_STRTOX_ERROR_RANGE;
1225 assert(!"Unreachable");
1226 return ASN_STRTOX_ERROR_INVAL;
1230 INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1232 const INTEGER_t *a = aptr;
1233 const INTEGER_t *b = bptr;
1238 if(a->size && b->size) {
1239 int sign_a = (a->buf[0] & 0x80) ? -1 : 1;
1240 int sign_b = (b->buf[0] & 0x80) ? -1 : 1;
1242 if(sign_a < sign_b) return -1;
1243 if(sign_a > sign_b) return 1;
1245 /* The shortest integer wins, unless comparing negatives */
1246 if(a->size < b->size) {
1248 } else if(a->size > b->size) {
1252 return sign_a * memcmp(a->buf, b->buf, a->size);
1253 } else if(a->size) {
1254 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1256 } else if(b->size) {
1257 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1262 } else if(!a && !b) {
1272 asn_random_fill_result_t
1273 INTEGER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1274 const asn_encoding_constraints_t *constraints,
1275 size_t max_length) {
1276 const asn_INTEGER_specifics_t *specs =
1277 (const asn_INTEGER_specifics_t *)td->specifics;
1278 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
1279 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1280 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1281 INTEGER_t *st = *sptr;
1282 const asn_INTEGER_enum_map_t *emap;
1285 int find_inside_map;
1287 if(max_length == 0) return result_skipped;
1290 st = (INTEGER_t *)CALLOC(1, sizeof(*st));
1292 return result_failed;
1297 emap = specs->value2enum;
1298 emap_len = specs->map_count;
1299 if(specs->strict_enumeration) {
1300 find_inside_map = emap_len > 0;
1302 find_inside_map = emap_len ? asn_random_between(0, 1) : 0;
1307 find_inside_map = 0;
1310 if(find_inside_map) {
1311 assert(emap_len > 0);
1312 value = emap[asn_random_between(0, emap_len - 1)].nat_value;
1314 const asn_per_constraints_t *ct;
1316 static const long variants[] = {
1317 -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384,
1318 -16383, -257, -256, -255, -254, -129, -128, -127,
1319 -126, -1, 0, 1, 126, 127, 128, 129,
1320 254, 255, 256, 257, 16383, 16384, 16385, 32767,
1321 32768, 32769, 65534, 65535, 65536, 65537};
1322 if(specs && specs->field_unsigned) {
1323 assert(variants[18] == 0);
1324 value = variants[asn_random_between(
1325 18, sizeof(variants) / sizeof(variants[0]) - 1)];
1327 value = variants[asn_random_between(
1328 0, sizeof(variants) / sizeof(variants[0]) - 1)];
1331 if(!constraints) constraints = &td->encoding_constraints;
1332 ct = constraints ? constraints->per_constraints : 0;
1333 if(ct && (ct->value.flags & APC_CONSTRAINED)) {
1334 if(value < ct->value.lower_bound || value > ct->value.upper_bound) {
1335 value = asn_random_between(ct->value.lower_bound,
1336 ct->value.upper_bound);
1341 if(asn_imax2INTEGER(st, value)) {
1343 ASN_STRUCT_RESET(*td, st);
1345 ASN_STRUCT_FREE(*td, st);
1347 return result_failed;
1350 result_ok.length = st->size;