10 #include "asn1p_integer.h"
12 #define ASN_INTEGER_MAX \
13 (~((asn1c_integer_t)0) \
14 & ~((asn1c_integer_t)1 << (8 * sizeof(asn1c_integer_t) - 1)))
15 #define ASN_INTEGER_MIN (-(ASN_INTEGER_MAX)-1)
18 * Parse the number in the given string until the given *end position,
19 * returning the position after the last parsed character back using the
20 * same (*end) pointer.
21 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
23 enum strtox_result_e {
24 STRTOX_ERROR_RANGE = -3, /* Input outside of supported numeric range */
25 STRTOX_ERROR_INVAL = -2, /* Invalid data encountered (e.g., "+-") */
26 STRTOX_EXPECT_MORE = -1, /* More data expected (e.g. "+") */
27 STRTOX_OK = 0, /* Conversion succeded, number ends at (*end) */
29 1 /* Conversion succeded, but the string has extra stuff */
32 static enum strtox_result_e
33 strtoaint_lim(const char *str, const char **end, asn1c_integer_t *intp) {
34 const asn1c_integer_t upper_boundary = ASN_INTEGER_MAX / 10;
35 int last_digit_max = ASN_INTEGER_MAX % 10;
37 asn1c_integer_t value;
39 if(str >= *end) return STRTOX_ERROR_INVAL;
50 return STRTOX_EXPECT_MORE;
54 for(value = 0; str < (*end); str++) {
56 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
57 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
59 if(value < upper_boundary) {
60 value = value * 10 + d;
61 } else if(value == upper_boundary) {
62 if(d <= last_digit_max) {
64 value = value * 10 + d;
67 value = -value * 10 - d;
71 return STRTOX_ERROR_RANGE;
75 return STRTOX_ERROR_RANGE;
82 return STRTOX_EXTRA_DATA;
92 asn1p_atoi(const char *ptr, asn1c_integer_t *value) {
93 const char *end = ptr + strlen(ptr);
94 if(strtoaint_lim(ptr, &end, value) == STRTOX_OK) {
101 const char *asn1p_itoa(asn1c_integer_t v) {
102 static char static_buf[128];
103 int ret = asn1p_itoa_s(static_buf, sizeof(static_buf), v);
105 assert((size_t)ret < sizeof(static_buf));
112 int asn1p_itoa_s(char *buf, size_t size, asn1c_integer_t v) {
115 if(v >= LONG_MIN && v < LONG_MAX) {
116 int ret = snprintf(buf, size, "%ld", (long)v);
117 if(ret < 0 || (size_t)ret >= size) {
125 if(v == ASN_INTEGER_MIN) {
130 memcpy(buf, "-170141183460469231731687303715884105729", 41);
135 memcpy(buf, "-9223372036854775809", 21);
138 assert(!"unreachable");
143 v = -v; /* Ditch the sign */
146 assert(v > 1000000000L);
147 char restbuf[10] = "000000000\0";
148 const char *rest = asn1p_itoa((long)(v % 1000000000L));
149 size_t restlen = strlen(rest);
150 assert(restlen <= 9);
151 memcpy(restbuf + (9 - restlen), rest, restlen);
154 const char *head = asn1p_itoa(v / 1000000000L);
156 int ret = snprintf(tmp_buf, sizeof(tmp_buf), "%s%s%s", sign ? "-" : "",
158 if(ret < 0 || (size_t)ret >= size) {
159 assert(ret > 0 && (size_t)ret < sizeof(tmp_buf));
162 memcpy(buf, tmp_buf, ret);
168 asn1p_integer_as_INTEGER(asn1c_integer_t value) {
169 abuf *ab = abuf_new();
170 uint8_t buf[sizeof(value) + 1];
176 } while(!((value == 0 && !(bp[-1] & 0x80))
177 || (value == -1 && (bp[-1] & 0x80))));
179 abuf_printf(ab, "{ (uint8_t *)\"");
181 for(const uint8_t *p = bp-1; p >= buf; p--) {
182 abuf_printf(ab, "\\x%02x", *p);
185 abuf_printf(ab, "\\0\", %zu }", bp - buf);