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>
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 = {
19 #if !defined(ASN_DISABLE_PRINT_SUPPORT)
23 #endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
25 #if !defined(ASN_DISABLE_BER_SUPPORT)
31 #endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
32 #if !defined(ASN_DISABLE_XER_SUPPORT)
38 #endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
39 #if !defined(ASN_DISABLE_JER_SUPPORT)
43 #endif /* !defined(ASN_DISABLE_JER_SUPPORT) */
44 #if !defined(ASN_DISABLE_OER_SUPPORT)
45 INTEGER_decode_oer, /* OER decoder */
46 INTEGER_encode_oer, /* Canonical OER encoder */
50 #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
51 #if !defined(ASN_DISABLE_UPER_SUPPORT)
52 INTEGER_decode_uper, /* Unaligned PER decoder */
53 INTEGER_encode_uper, /* Unaligned PER encoder */
57 #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
58 #if !defined(ASN_DISABLE_APER_SUPPORT)
59 INTEGER_decode_aper, /* Aligned PER decoder */
60 INTEGER_encode_aper, /* Aligned PER encoder */
64 #endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
65 #if !defined(ASN_DISABLE_RFILL_SUPPORT)
69 #endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
70 0 /* Use generic outmost tag fetcher */
72 asn_TYPE_descriptor_t asn_DEF_INTEGER = {
77 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
78 asn_DEF_INTEGER_tags, /* Same as above */
79 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
81 #if !defined(ASN_DISABLE_OER_SUPPORT)
83 #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
84 #if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
86 #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
87 asn_generic_no_constraint
89 0, 0, /* No members */
94 * INTEGER specific human-readable output.
97 INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
98 const asn_INTEGER_specifics_t *specs =
99 (const asn_INTEGER_specifics_t *)td->specifics;
101 uint8_t *buf = st->buf;
102 uint8_t *buf_end = st->buf + st->size;
108 if(specs && specs->field_unsigned)
109 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
111 ret = asn_INTEGER2imax(st, &value);
113 /* Simple case: the integer size is small */
115 const asn_INTEGER_enum_map_t *el;
116 el = (value >= 0 || !specs || !specs->field_unsigned)
117 ? INTEGER_map_value2enum(specs, value) : 0;
120 return asn__format_to_callback(cb, app_key,
121 "%" ASN_PRIdMAX " (%s)", value, el->enum_name);
123 return asn__format_to_callback(cb, app_key,
124 "<%s/>", el->enum_name);
125 } else if(plainOrXER && specs && specs->strict_enumeration) {
126 ASN_DEBUG("ASN.1 forbids dealing with "
127 "unknown value of ENUMERATED type");
131 return asn__format_to_callback(cb, app_key,
132 (specs && specs->field_unsigned)
137 } else if(plainOrXER && specs && specs->strict_enumeration) {
139 * Here and earlier, we cannot encode the ENUMERATED values
140 * if there is no corresponding identifier.
142 ASN_DEBUG("ASN.1 forbids dealing with "
143 "unknown value of ENUMERATED type");
148 /* Output in the long xx:yy:zz... format */
149 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
150 for(p = scratch; buf < buf_end; buf++) {
151 const char * const h2c = "0123456789ABCDEF";
152 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
154 if(cb(scratch, p - scratch, app_key) < 0)
156 wrote += p - scratch;
159 *p++ = h2c[*buf >> 4];
160 *p++ = h2c[*buf & 0x0F];
161 *p++ = 0x3a; /* ":" */
164 p--; /* Remove the last ":" */
166 wrote += p - scratch;
167 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
171 INTEGER__compar_value2enum(const void *kp, const void *am) {
172 long a = *(const long *)kp;
173 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
174 long b = el->nat_value;
176 else if(a == b) return 0;
180 const asn_INTEGER_enum_map_t *
181 INTEGER_map_value2enum(const asn_INTEGER_specifics_t *specs, long value) {
182 int count = specs ? specs->map_count : 0;
184 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
185 count, sizeof(specs->value2enum[0]),
186 INTEGER__compar_value2enum);
190 asn__integer_convert(const uint8_t *b, const uint8_t *end) {
193 /* Perform the sign initialization */
194 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
196 value = (uintmax_t)(-1);
201 /* Conversion engine */
202 for(; b < end; b++) {
203 value = (value << 8) | *b;
210 asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
214 /* Sanity checking */
215 if(!iptr || !iptr->buf || !lptr) {
220 /* Cache the begin/end of the buffer */
221 b = iptr->buf; /* Start of the INTEGER buffer */
223 end = b + size; /* Where to stop */
225 if(size > sizeof(intmax_t)) {
226 uint8_t *end1 = end - 1;
228 * Slightly more advanced processing,
229 * able to process INTEGERs with >sizeof(intmax_t) bytes
230 * when the actual value is small, e.g. for intmax_t == int32_t
231 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
233 /* Skip out the insignificant leading bytes */
234 for(; b < end1; b++) {
236 case 0x00: if((b[1] & 0x80) == 0) continue; break;
237 case 0xff: if((b[1] & 0x80) != 0) continue; break;
243 if(size > sizeof(intmax_t)) {
244 /* Still cannot fit the sizeof(intmax_t) */
250 /* Shortcut processing of a corner case */
256 *lptr = asn__integer_convert(b, end);
260 /* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
262 asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
267 if(!iptr || !iptr->buf || !lptr) {
276 /* If all extra leading bytes are zeroes, ignore them */
277 for(; size > sizeof(value); b++, size--) {
279 /* Value won't fit into uintmax_t */
285 /* Conversion engine */
286 for(value = 0; b < end; b++)
287 value = (value << 8) | *b;
294 asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
300 if(value <= ((~(uintmax_t)0) >> 1)) {
301 return asn_imax2INTEGER(st, value);
304 buf = (uint8_t *)MALLOC(1 + sizeof(value));
307 end = buf + (sizeof(value) + 1);
308 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
309 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
310 *b = (uint8_t)(value >> shr);
312 if(st->buf) FREEMEM(st->buf);
314 st->size = 1 + sizeof(value);
320 asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
325 int littleEndian = 1; /* Run-time detection */
333 buf = (uint8_t *)(long *)MALLOC(sizeof(value));
336 if(*(char *)&littleEndian) {
337 pstart = (uint8_t *)&value + sizeof(value) - 1;
338 pend1 = (uint8_t *)&value;
341 pstart = (uint8_t *)&value;
342 pend1 = pstart + sizeof(value) - 1;
347 * If the contents octet consists of more than one octet,
348 * then bits of the first octet and bit 8 of the second octet:
349 * a) shall not all be ones; and
350 * b) shall not all be zero.
352 for(p = pstart; p != pend1; p += add) {
354 case 0x00: if((*(p+add) & 0x80) == 0)
357 case 0xff: if((*(p+add) & 0x80))
363 /* Copy the integer body */
364 for(bp = buf, pend1 += add; p != pend1; p += add)
367 if(st->buf) FREEMEM(st->buf);
375 asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
377 if(asn_INTEGER2imax(iptr, &v) == 0) {
378 if(v < LONG_MIN || v > LONG_MAX) {
390 asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
392 if(asn_INTEGER2umax(iptr, &v) == 0) {
405 asn_long2INTEGER(INTEGER_t *st, long value) {
406 return asn_imax2INTEGER(st, value);
410 asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
411 return asn_imax2INTEGER(st, value);
416 asn_uint642INTEGER(INTEGER_t *st, uint64_t value) {
422 if(value <= INT64_MAX)
423 return asn_int642INTEGER(st, value);
425 buf = (uint8_t *)MALLOC(1 + sizeof(value));
428 end = buf + (sizeof(value) + 1);
430 for(b = buf + 1, shr = (sizeof(value)-1)*8; b < end; shr -= 8, b++)
431 *b = (uint8_t)(value >> shr);
433 if(st->buf) FREEMEM(st->buf);
435 st->size = 1 + sizeof(value);
441 asn_int642INTEGER(INTEGER_t *st, int64_t value) {
446 int littleEndian = 1; /* Run-time detection */
454 buf = (uint8_t *)MALLOC(sizeof(value));
457 if(*(char *)&littleEndian) {
458 pstart = (uint8_t *)&value + sizeof(value) - 1;
459 pend1 = (uint8_t *)&value;
462 pstart = (uint8_t *)&value;
463 pend1 = pstart + sizeof(value) - 1;
468 * If the contents octet consists of more than one octet,
469 * then bits of the first octet and bit 8 of the second octet:
470 * a) shall not all be ones; and
471 * b) shall not all be zero.
473 for(p = pstart; p != pend1; p += add) {
475 case 0x00: if((*(p+add) & 0x80) == 0)
478 case 0xff: if((*(p+add) & 0x80))
484 /* Copy the integer body */
485 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
488 if(st->buf) FREEMEM(st->buf);
496 * Parse the number in the given string until the given *end position,
497 * returning the position after the last parsed character back using the
498 * same (*end) pointer.
499 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
501 enum asn_strtox_result_e
502 asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
506 const intmax_t asn1_intmax_max = ((~(uintmax_t)0) >> 1);
507 const intmax_t upper_boundary = asn1_intmax_max / 10;
508 intmax_t last_digit_max = asn1_intmax_max % 10;
510 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
521 return ASN_STRTOX_EXPECT_MORE;
525 for(value = 0; str < (*end); str++) {
526 if(*str >= 0x30 && *str <= 0x39) {
528 if(value < upper_boundary) {
529 value = value * 10 + d;
530 } else if(value == upper_boundary) {
531 if(d <= last_digit_max) {
533 value = value * 10 + d;
536 value = -value * 10 - d;
540 // If digits continue, we're guaranteed out of range.
542 if(*str >= 0x30 && *str <= 0x39) {
543 return ASN_STRTOX_ERROR_RANGE;
545 *intp = sign * value;
546 return ASN_STRTOX_EXTRA_DATA;
552 return ASN_STRTOX_ERROR_RANGE;
556 return ASN_STRTOX_ERROR_RANGE;
560 *intp = sign * value;
561 return ASN_STRTOX_EXTRA_DATA;
566 *intp = sign * value;
567 return ASN_STRTOX_OK;
571 * Parse the number in the given string until the given *end position,
572 * returning the position after the last parsed character back using the
573 * same (*end) pointer.
574 * WARNING: This behavior is different from the standard strtoul/strtoumax(3).
576 enum asn_strtox_result_e
577 asn_strtoumax_lim(const char *str, const char **end, uintmax_t *uintp) {
580 const uintmax_t asn1_uintmax_max = ((~(uintmax_t)0));
581 const uintmax_t upper_boundary = asn1_uintmax_max / 10;
582 uintmax_t last_digit_max = asn1_uintmax_max % 10;
584 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
588 return ASN_STRTOX_ERROR_INVAL;
593 return ASN_STRTOX_EXPECT_MORE;
597 for(value = 0; str < (*end); str++) {
598 if(*str >= 0x30 && *str <= 0x39) {
599 unsigned int d = *str - '0';
600 if(value < upper_boundary) {
601 value = value * 10 + d;
602 } else if(value == upper_boundary) {
603 if(d <= last_digit_max) {
604 value = value * 10 + d;
607 // If digits continue, we're guaranteed out of range.
609 if(*str >= 0x30 && *str <= 0x39) {
610 return ASN_STRTOX_ERROR_RANGE;
613 return ASN_STRTOX_EXTRA_DATA;
619 return ASN_STRTOX_ERROR_RANGE;
623 return ASN_STRTOX_ERROR_RANGE;
628 return ASN_STRTOX_EXTRA_DATA;
634 return ASN_STRTOX_OK;
637 enum asn_strtox_result_e
638 asn_strtol_lim(const char *str, const char **end, long *lp) {
640 switch(asn_strtoimax_lim(str, end, &value)) {
641 case ASN_STRTOX_ERROR_RANGE:
642 return ASN_STRTOX_ERROR_RANGE;
643 case ASN_STRTOX_ERROR_INVAL:
644 return ASN_STRTOX_ERROR_INVAL;
645 case ASN_STRTOX_EXPECT_MORE:
646 return ASN_STRTOX_EXPECT_MORE;
648 if(value >= LONG_MIN && value <= LONG_MAX) {
650 return ASN_STRTOX_OK;
652 return ASN_STRTOX_ERROR_RANGE;
654 case ASN_STRTOX_EXTRA_DATA:
655 if(value >= LONG_MIN && value <= LONG_MAX) {
657 return ASN_STRTOX_EXTRA_DATA;
659 return ASN_STRTOX_ERROR_RANGE;
663 assert(!"Unreachable");
664 return ASN_STRTOX_ERROR_INVAL;
667 enum asn_strtox_result_e
668 asn_strtoul_lim(const char *str, const char **end, unsigned long *ulp) {
670 switch(asn_strtoumax_lim(str, end, &value)) {
671 case ASN_STRTOX_ERROR_RANGE:
672 return ASN_STRTOX_ERROR_RANGE;
673 case ASN_STRTOX_ERROR_INVAL:
674 return ASN_STRTOX_ERROR_INVAL;
675 case ASN_STRTOX_EXPECT_MORE:
676 return ASN_STRTOX_EXPECT_MORE;
678 if(value <= ULONG_MAX) {
680 return ASN_STRTOX_OK;
682 return ASN_STRTOX_ERROR_RANGE;
684 case ASN_STRTOX_EXTRA_DATA:
685 if(value <= ULONG_MAX) {
687 return ASN_STRTOX_EXTRA_DATA;
689 return ASN_STRTOX_ERROR_RANGE;
693 assert(!"Unreachable");
694 return ASN_STRTOX_ERROR_INVAL;
698 INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
700 const INTEGER_t *a = aptr;
701 const INTEGER_t *b = bptr;
706 if(a->size && b->size) {
707 int sign_a = (a->buf[0] & 0x80) ? -1 : 1;
708 int sign_b = (b->buf[0] & 0x80) ? -1 : 1;
710 if(sign_a < sign_b) return -1;
711 if(sign_a > sign_b) return 1;
713 /* The shortest integer wins, unless comparing negatives */
714 if(a->size < b->size) {
716 } else if(a->size > b->size) {
720 return sign_a * memcmp(a->buf, b->buf, a->size);
722 int sign = (a->buf[0] & 0x80) ? -1 : 1;
725 int sign = (a->buf[0] & 0x80) ? -1 : 1;
730 } else if(!a && !b) {