blob: bb6f1bd5ddc539f0754cd6ab1bceb7bc3a371e02 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin6c527842014-02-09 04:34:54 -08002 * Copyright (c) 2003-2014 Lev Walkin <vlm@lionet.info>.
Lev Walkine0b56e02005-02-25 12:10:27 +00003 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <INTEGER.h>
Lev Walkind703ff42004-10-21 11:21:25 +00008#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <errno.h>
10
11/*
12 * INTEGER basic type description.
13 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070014static const ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
16};
Lev Walkin5e033762004-09-29 13:26:15 +000017asn_TYPE_descriptor_t asn_DEF_INTEGER = {
Lev Walkinf15320b2004-06-03 03:38:44 +000018 "INTEGER",
Lev Walkindc06f6b2004-10-20 15:50:55 +000019 "INTEGER",
Lev Walkin8e8078a2004-09-26 13:10:40 +000020 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000021 INTEGER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000022 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000023 ber_decode_primitive,
Lev Walkinf15320b2004-06-03 03:38:44 +000024 INTEGER_encode_der,
Lev Walkind703ff42004-10-21 11:21:25 +000025 INTEGER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000026 INTEGER_encode_xer,
Lev Walkind7703cf2012-09-03 01:45:03 -070027#ifdef ASN_DISABLE_PER_SUPPORT
28 0,
29 0,
30#else
Lev Walkin59b176e2005-11-26 11:25:14 +000031 INTEGER_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000032 INTEGER_encode_uper, /* Unaligned PER encoder */
Lev Walkind7703cf2012-09-03 01:45:03 -070033#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000034 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000035 asn_DEF_INTEGER_tags,
36 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
37 asn_DEF_INTEGER_tags, /* Same as above */
38 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000039 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000040 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000041 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000042};
43
44/*
Lev Walkinf15320b2004-06-03 03:38:44 +000045 * Encode INTEGER type using DER.
46 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000047asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000048INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000049 int tag_mode, ber_tlv_tag_t tag,
50 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000051 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000052
53 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000054 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000055
56 /*
57 * Canonicalize integer in the buffer.
58 * (Remove too long sign extension, remove some first 0x00 bytes)
59 */
60 if(st->buf) {
61 uint8_t *buf = st->buf;
62 uint8_t *end1 = buf + st->size - 1;
63 int shift;
64
65 /* Compute the number of superfluous leading bytes */
66 for(; buf < end1; buf++) {
67 /*
68 * If the contents octets of an integer value encoding
69 * consist of more than one octet, then the bits of the
70 * first octet and bit 8 of the second octet:
71 * a) shall not all be ones; and
72 * b) shall not all be zero.
73 */
74 switch(*buf) {
75 case 0x00: if((buf[1] & 0x80) == 0)
76 continue;
77 break;
78 case 0xff: if((buf[1] & 0x80))
79 continue;
80 break;
81 }
82 break;
83 }
84
85 /* Remove leading superfluous bytes from the integer */
86 shift = buf - st->buf;
87 if(shift) {
88 uint8_t *nb = st->buf;
89 uint8_t *end;
90
91 st->size -= shift; /* New size, minus bad bytes */
92 end = nb + st->size;
93
94 for(; nb < end; nb++, buf++)
95 *nb = *buf;
96 }
97
98 } /* if(1) */
99
Lev Walkin8e8078a2004-09-26 13:10:40 +0000100 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000101}
102
Lev Walkinc2350112005-03-29 17:19:53 +0000103static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop);
Lev Walkine0b56e02005-02-25 12:10:27 +0000104
Lev Walkinf15320b2004-06-03 03:38:44 +0000105/*
106 * INTEGER specific human-readable output.
107 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000108static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700109INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000110 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000111 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000112 uint8_t *buf = st->buf;
113 uint8_t *buf_end = st->buf + st->size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700114 intmax_t value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000115 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 char *p;
117 int ret;
118
Lev Walkin97f8edc2013-03-28 04:38:41 -0700119 if(specs && specs->field_unsigned)
Lev Walkin72ec9092017-07-05 05:49:12 -0700120 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
Lev Walkin97f8edc2013-03-28 04:38:41 -0700121 else
Lev Walkin72ec9092017-07-05 05:49:12 -0700122 ret = asn_INTEGER2imax(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000123
Lev Walkinf15320b2004-06-03 03:38:44 +0000124 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700125 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000126 const asn_INTEGER_enum_map_t *el;
127 size_t scrsize;
128 char *scr;
129
Lev Walkin97f8edc2013-03-28 04:38:41 -0700130 el = (value >= 0 || !specs || !specs->field_unsigned)
131 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000132 if(el) {
133 scrsize = el->enum_len + 32;
134 scr = (char *)alloca(scrsize);
135 if(plainOrXER == 0)
136 ret = snprintf(scr, scrsize,
Lev Walkin72ec9092017-07-05 05:49:12 -0700137 "%" PRIdMAX " (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000138 else
139 ret = snprintf(scr, scrsize,
140 "<%s/>", el->enum_name);
141 } else if(plainOrXER && specs && specs->strict_enumeration) {
142 ASN_DEBUG("ASN.1 forbids dealing with "
143 "unknown value of ENUMERATED type");
144 errno = EPERM;
145 return -1;
146 } else {
147 scrsize = sizeof(scratch);
148 scr = scratch;
Lev Walkin72ec9092017-07-05 05:49:12 -0700149 ret = snprintf(
150 scr, scrsize,
151 (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX,
152 value);
153 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000154 assert(ret > 0 && (size_t)ret < scrsize);
155 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
156 } else if(plainOrXER && specs && specs->strict_enumeration) {
157 /*
158 * Here and earlier, we cannot encode the ENUMERATED values
159 * if there is no corresponding identifier.
160 */
161 ASN_DEBUG("ASN.1 forbids dealing with "
162 "unknown value of ENUMERATED type");
163 errno = EPERM;
164 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 }
166
167 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000168 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700170 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000171 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000173 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000175 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 p = scratch;
177 }
178 *p++ = h2c[*buf >> 4];
179 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000180 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 }
Lev Walkindb13f512004-07-19 17:30:25 +0000182 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000183 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000184
Lev Walkina9cc46e2004-09-22 16:06:28 +0000185 wrote += p - scratch;
186 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
187}
188
189/*
190 * INTEGER specific human-readable output.
191 */
192int
Lev Walkin5e033762004-09-29 13:26:15 +0000193INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000194 asn_app_consume_bytes_f *cb, void *app_key) {
195 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000196 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197
198 (void)td;
199 (void)ilevel;
200
Lev Walkind500a962005-11-27 13:06:56 +0000201 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000202 ret = cb("<absent>", 8, app_key);
203 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000204 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000205
Lev Walkin8e8078a2004-09-26 13:10:40 +0000206 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000207}
208
Lev Walkine0b56e02005-02-25 12:10:27 +0000209struct e2v_key {
210 const char *start;
211 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700212 const asn_INTEGER_enum_map_t *vemap;
213 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000214};
215static int
216INTEGER__compar_enum2value(const void *kp, const void *am) {
217 const struct e2v_key *key = (const struct e2v_key *)kp;
218 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
219 const char *ptr, *end, *name;
220
221 /* Remap the element (sort by different criterion) */
222 el = key->vemap + key->evmap[el - key->vemap];
223
224 /* Compare strings */
225 for(ptr = key->start, end = key->stop, name = el->enum_name;
226 ptr < end; ptr++, name++) {
227 if(*ptr != *name)
228 return *(const unsigned char *)ptr
229 - *(const unsigned char *)name;
230 }
231 return name[0] ? -1 : 0;
232}
233
234static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000235INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700236 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000237 int count = specs ? specs->map_count : 0;
238 struct e2v_key key;
239 const char *lp;
240
241 if(!count) return NULL;
242
243 /* Guaranteed: assert(lstart < lstop); */
244 /* Figure out the tag name */
245 for(lstart++, lp = lstart; lp < lstop; lp++) {
246 switch(*lp) {
247 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
248 case 0x2f: /* '/' */ case 0x3e: /* '>' */
249 break;
250 default:
251 continue;
252 }
253 break;
254 }
255 if(lp == lstop) return NULL; /* No tag found */
256 lstop = lp;
257
258 key.start = lstart;
259 key.stop = lstop;
260 key.vemap = specs->value2enum;
261 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000262 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
263 specs->value2enum, count, sizeof(specs->value2enum[0]),
264 INTEGER__compar_enum2value);
265 if(el_found) {
266 /* Remap enum2value into value2enum */
267 el_found = key.vemap + key.evmap[el_found - key.vemap];
268 }
269 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000270}
271
272static int
273INTEGER__compar_value2enum(const void *kp, const void *am) {
274 long a = *(const long *)kp;
275 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
276 long b = el->nat_value;
277 if(a < b) return -1;
278 else if(a == b) return 0;
279 else return 1;
280}
281
Lev Walkinc2350112005-03-29 17:19:53 +0000282const asn_INTEGER_enum_map_t *
283INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000284 int count = specs ? specs->map_count : 0;
285 if(!count) return 0;
286 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
287 count, sizeof(specs->value2enum[0]),
288 INTEGER__compar_value2enum);
289}
290
Lev Walkinc744a022006-09-15 18:33:25 +0000291static int
292INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
293 void *p = MALLOC(min_size + 1);
294 if(p) {
295 void *b = st->buf;
296 st->size = 0;
297 st->buf = p;
298 FREEMEM(b);
299 return 0;
300 } else {
301 return -1;
302 }
303}
304
Lev Walkind703ff42004-10-21 11:21:25 +0000305/*
306 * Decode the chunk of XML text encoding INTEGER.
307 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000308static enum xer_pbd_rval
309INTEGER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000310 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700311 intmax_t dec_value;
312 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000313 const char *lp;
314 const char *lstart = (const char *)chunk_buf;
315 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000316 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700317 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000318 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000319 ST_WAITDIGITS,
320 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700321 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000322 ST_HEXDIGIT1,
323 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700324 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000325 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700326 ST_END_ENUM,
327 ST_UNEXPECTED
328 } state = ST_LEADSPACE;
329 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
330 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000331
Lev Walkinc744a022006-09-15 18:33:25 +0000332 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000333 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
334 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000335
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000336 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
337 return XPBD_SYSTEM_FAILURE;
338
Lev Walkind703ff42004-10-21 11:21:25 +0000339 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000340 * We may have received a tag here. It will be processed inline.
341 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000342 */
Lev Walkinb3751942012-09-02 19:36:47 -0700343 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000344 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000345 switch(lv) {
346 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000347 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700348 case ST_LEADSPACE:
349 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700350 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000351 case ST_SKIPSPHEX:
352 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700353 case ST_DIGITS:
354 dec_value_end = lp;
355 state = ST_DIGITS_TRAILSPACE;
356 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700357 case ST_HEXCOLON:
358 state = ST_HEXDIGITS_TRAILSPACE;
359 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000360 default:
361 break;
362 }
Lev Walkind703ff42004-10-21 11:21:25 +0000363 break;
364 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700365 if(state == ST_LEADSPACE) {
366 dec_value = 0;
367 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000368 state = ST_WAITDIGITS;
369 continue;
370 }
371 break;
372 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700373 if(state == ST_LEADSPACE) {
374 dec_value = 0;
375 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000376 state = ST_WAITDIGITS;
377 continue;
378 }
379 break;
380 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
381 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000382 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700383 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000384 case ST_SKIPSPHEX: /* Fall through */
385 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700386 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000387 state = ST_HEXDIGIT2;
388 continue;
389 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700390 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000391 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700392 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000393 continue;
394 case ST_HEXCOLON:
395 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700396 case ST_LEADSPACE:
397 dec_value = 0;
398 dec_value_start = lp;
399 /* FALL THROUGH */
400 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000401 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700402 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700403 default:
404 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000405 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700406 break;
407 case 0x3c: /* '<', start of XML encoded enumeration */
408 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000409 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000410 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000411 (asn_INTEGER_specifics_t *)
412 td->specifics, lstart, lstop);
413 if(el) {
414 ASN_DEBUG("Found \"%s\" => %ld",
415 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700416 dec_value = el->nat_value;
417 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000418 lp = lstop - 1;
419 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000420 }
421 ASN_DEBUG("Unknown identifier for INTEGER");
422 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000423 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000424 case 0x3a: /* ':' */
425 if(state == ST_HEXCOLON) {
426 /* This colon is expected */
427 state = ST_HEXDIGIT1;
428 continue;
429 } else if(state == ST_DIGITS) {
430 /* The colon here means that we have
431 * decoded the first two hexadecimal
432 * places as a decimal value.
433 * Switch decoding mode. */
434 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000435 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700436 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000437 lp = lstart - 1;
438 continue;
439 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400440 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000441 break;
442 }
443 /* [A-Fa-f] */
444 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
445 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
446 switch(state) {
447 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700448 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000449 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700450 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
451 hex_value += 10;
452 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000453 state = ST_HEXDIGIT2;
454 continue;
455 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700456 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
457 hex_value += 10;
458 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000459 state = ST_HEXCOLON;
460 continue;
461 case ST_DIGITS:
462 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000463 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700464 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000465 lp = lstart - 1;
466 continue;
467 default:
468 break;
469 }
470 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000471 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000472
473 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700474 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400475 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700476 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000477 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000478 }
479
Lev Walkinc744a022006-09-15 18:33:25 +0000480 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700481 case ST_END_ENUM:
482 /* Got a complete and valid enumeration encoded as a tag. */
483 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000484 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700485 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700486 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700487 case ST_DIGITS_TRAILSPACE:
488 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700489 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700490 case ASN_STRTOX_OK:
Lev Walkine09f9f12012-09-02 23:06:35 -0700491 break;
Lev Walkin72ec9092017-07-05 05:49:12 -0700492 case ASN_STRTOX_ERROR_RANGE:
Lev Walkine09f9f12012-09-02 23:06:35 -0700493 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700494 case ASN_STRTOX_ERROR_INVAL:
495 case ASN_STRTOX_EXPECT_MORE:
496 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700497 return XPBD_BROKEN_ENCODING;
498 }
Lev Walkinc744a022006-09-15 18:33:25 +0000499 break;
500 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700501 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000502 st->buf[st->size] = 0; /* Just in case termination */
503 return XPBD_BODY_CONSUMED;
504 case ST_HEXDIGIT1:
505 case ST_HEXDIGIT2:
506 case ST_SKIPSPHEX:
507 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700508 case ST_LEADSPACE:
509 /* Content not found */
510 return XPBD_NOT_BODY_IGNORE;
511 case ST_WAITDIGITS:
512 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700513 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
514 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000515 }
Lev Walkind703ff42004-10-21 11:21:25 +0000516
Lev Walkine09f9f12012-09-02 23:06:35 -0700517 /*
518 * Convert the result of parsing of enumeration or a straight
519 * decimal value into a BER representation.
520 */
521 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000522 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000523
Lev Walkin0fab1a62005-03-09 22:19:25 +0000524 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000525}
526
527asn_dec_rval_t
528INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
529 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000530 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000531
532 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000533 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000534 buf_ptr, size, INTEGER__xer_body_decode);
535}
536
Lev Walkina9cc46e2004-09-22 16:06:28 +0000537asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000538INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000539 int ilevel, enum xer_encoder_flags_e flags,
540 asn_app_consume_bytes_f *cb, void *app_key) {
541 const INTEGER_t *st = (const INTEGER_t *)sptr;
542 asn_enc_rval_t er;
543
544 (void)ilevel;
545 (void)flags;
546
Lev Walkind500a962005-11-27 13:06:56 +0000547 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700548 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000549
Lev Walkine0b56e02005-02-25 12:10:27 +0000550 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700551 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000552
Lev Walkin7c1dc052016-03-14 03:08:15 -0700553 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000554}
555
Lev Walkind7703cf2012-09-03 01:45:03 -0700556#ifndef ASN_DISABLE_PER_SUPPORT
557
Lev Walkin59b176e2005-11-26 11:25:14 +0000558asn_dec_rval_t
559INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
560 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000561 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000562 asn_dec_rval_t rval = { RC_OK, 0 };
563 INTEGER_t *st = (INTEGER_t *)*sptr;
564 asn_per_constraint_t *ct;
565 int repeat;
566
567 (void)opt_codec_ctx;
568
569 if(!st) {
570 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700571 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000572 }
573
574 if(!constraints) constraints = td->per_constraints;
575 ct = constraints ? &constraints->value : 0;
576
577 if(ct && ct->flags & APC_EXTENSIBLE) {
578 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700579 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000580 if(inext) ct = 0;
581 }
582
583 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000584 st->buf = 0;
585 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000586 if(ct) {
587 if(ct->flags & APC_SEMI_CONSTRAINED) {
588 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700589 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000590 st->size = 1;
591 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
592 size_t size = (ct->range_bits + 7) >> 3;
593 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700594 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000595 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000596 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000597 }
598
Lev Walkin6c527842014-02-09 04:34:54 -0800599 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000600 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800601 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000602 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
603 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800604 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700605 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800606
607 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700608 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800609 if(uper_get_constrained_whole_number(pd,
610 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700611 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800612 ASN_DEBUG("Got value %lu + low %ld",
613 uvalue, ct->lower_bound);
614 uvalue += ct->lower_bound;
615 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700616 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800617 } else {
Lev Walkin72ec9092017-07-05 05:49:12 -0700618 unsigned long svalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800619 if(uper_get_constrained_whole_number(pd,
620 &svalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700621 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800622 ASN_DEBUG("Got value %ld + low %ld",
623 svalue, ct->lower_bound);
624 svalue += ct->lower_bound;
625 if(asn_long2INTEGER(st, svalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700626 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800627 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000628 return rval;
629 }
630 } else {
631 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
632 }
633
634 /* X.691, #12.2.3, #12.2.4 */
635 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700636 ssize_t len = 0;
637 void *p = NULL;
638 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000639
640 /* Get the PER length */
641 len = uper_get_length(pd, -1, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700642 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000643
644 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700645 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000646 st->buf = (uint8_t *)p;
647
648 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700649 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000650 st->size += len;
651 } while(repeat);
652 st->buf[st->size] = 0; /* JIC */
653
654 /* #12.2.3 */
655 if(ct && ct->lower_bound) {
656 /*
657 * TODO: replace by in-place arithmetics.
658 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700659 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000660 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700661 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700662 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700663 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000664 }
665
666 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000667}
668
Lev Walkin523de9e2006-08-18 01:34:18 +0000669asn_enc_rval_t
670INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
671 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000672 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000673 asn_enc_rval_t er;
674 INTEGER_t *st = (INTEGER_t *)sptr;
675 const uint8_t *buf;
676 const uint8_t *end;
677 asn_per_constraint_t *ct;
678 long value = 0;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100679 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000680
Lev Walkin7c1dc052016-03-14 03:08:15 -0700681 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000682
683 if(!constraints) constraints = td->per_constraints;
684 ct = constraints ? &constraints->value : 0;
685
686 er.encoded = 0;
687
688 if(ct) {
689 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000690 if(specs && specs->field_unsigned) {
691 unsigned long uval;
692 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700693 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000694 /* Check proper range */
695 if(ct->flags & APC_SEMI_CONSTRAINED) {
696 if(uval < (unsigned long)ct->lower_bound)
697 inext = 1;
698 } else if(ct->range_bits >= 0) {
699 if(uval < (unsigned long)ct->lower_bound
700 || uval > (unsigned long)ct->upper_bound)
701 inext = 1;
702 }
703 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
704 uval, st->buf[0], st->size,
705 ct->lower_bound, ct->upper_bound,
706 inext ? "ext" : "fix");
707 value = uval;
708 } else {
709 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700710 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000711 /* Check proper range */
712 if(ct->flags & APC_SEMI_CONSTRAINED) {
713 if(value < ct->lower_bound)
714 inext = 1;
715 } else if(ct->range_bits >= 0) {
716 if(value < ct->lower_bound
717 || value > ct->upper_bound)
718 inext = 1;
719 }
720 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
721 value, st->buf[0], st->size,
722 ct->lower_bound, ct->upper_bound,
723 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000724 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000725 if(ct->flags & APC_EXTENSIBLE) {
726 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700727 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000728 if(inext) ct = 0;
729 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700730 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000731 }
732 }
733
734
Lev Walkin6c527842014-02-09 04:34:54 -0800735 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000736 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800737 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800738 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
739 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100740 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800741 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700742 ASN__ENCODE_FAILED;
743 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000744 }
745
746 if(ct && ct->lower_bound) {
747 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
748 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700749 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000750 }
751
752 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
753 ssize_t mayEncode = uper_put_length(po, end - buf);
754 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700755 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000756 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700757 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000758 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000759 }
760
Lev Walkin7c1dc052016-03-14 03:08:15 -0700761 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000762}
763
Lev Walkind7703cf2012-09-03 01:45:03 -0700764#endif /* ASN_DISABLE_PER_SUPPORT */
765
Lev Walkinf15320b2004-06-03 03:38:44 +0000766int
Lev Walkin72ec9092017-07-05 05:49:12 -0700767asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000768 uint8_t *b, *end;
769 size_t size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700770 intmax_t value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000771
772 /* Sanity checking */
773 if(!iptr || !iptr->buf || !lptr) {
774 errno = EINVAL;
775 return -1;
776 }
777
778 /* Cache the begin/end of the buffer */
779 b = iptr->buf; /* Start of the INTEGER buffer */
780 size = iptr->size;
781 end = b + size; /* Where to stop */
782
Lev Walkin72ec9092017-07-05 05:49:12 -0700783 if(size > sizeof(value)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000784 uint8_t *end1 = end - 1;
785 /*
786 * Slightly more advanced processing,
Lev Walkin72ec9092017-07-05 05:49:12 -0700787 * able to process INTEGERs with >sizeof(value) bytes
788 * when the actual value is small, e.g. for intmax_t == int32_t
789 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000790 */
791 /* Skip out the insignificant leading bytes */
792 for(; b < end1; b++) {
793 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700794 case 0x00: if((b[1] & 0x80) == 0) continue; break;
795 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000796 }
797 break;
798 }
799
800 size = end - b;
Lev Walkin72ec9092017-07-05 05:49:12 -0700801 if(size > sizeof(value)) {
802 /* Still cannot fit the sizeof(value) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000803 errno = ERANGE;
804 return -1;
805 }
806 }
807
808 /* Shortcut processing of a corner case */
809 if(end == b) {
810 *lptr = 0;
811 return 0;
812 }
813
814 /* Perform the sign initialization */
Lev Walkin72ec9092017-07-05 05:49:12 -0700815 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
816 if((*b >> 7)) value = -1; else value = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000817
818 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700819 for(; b < end; b++) {
820 value = (value << 8) | *b;
821 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000822
Lev Walkin72ec9092017-07-05 05:49:12 -0700823 *lptr = value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000824 return 0;
825}
Lev Walkind703ff42004-10-21 11:21:25 +0000826
Lev Walkin72ec9092017-07-05 05:49:12 -0700827/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000828int
Lev Walkin72ec9092017-07-05 05:49:12 -0700829asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000830 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700831 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000832 size_t size;
833
834 if(!iptr || !iptr->buf || !lptr) {
835 errno = EINVAL;
836 return -1;
837 }
838
839 b = iptr->buf;
840 size = iptr->size;
841 end = b + size;
842
843 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700844 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000845 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700846 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000847 errno = ERANGE;
848 return -1;
849 }
850 }
851
852 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700853 for(value = 0; b < end; b++)
854 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000855
Lev Walkin72ec9092017-07-05 05:49:12 -0700856 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000857 return 0;
858}
859
860int
Lev Walkin72ec9092017-07-05 05:49:12 -0700861asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
862 uint8_t *buf;
863 uint8_t *end;
864 uint8_t *b;
865 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000866
Lev Walkin72ec9092017-07-05 05:49:12 -0700867 if(value <= INTMAX_MAX) {
868 return asn_imax2INTEGER(st, value);
869 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000870
Lev Walkin72ec9092017-07-05 05:49:12 -0700871 buf = (uint8_t *)MALLOC(1 + sizeof(value));
872 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000873
Lev Walkin72ec9092017-07-05 05:49:12 -0700874 end = buf + (sizeof(value) + 1);
875 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
876 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
877 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000878
Lev Walkin72ec9092017-07-05 05:49:12 -0700879 if(st->buf) FREEMEM(st->buf);
880 st->buf = buf;
881 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000882
883 return 0;
884}
885
886int
Lev Walkin72ec9092017-07-05 05:49:12 -0700887asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000888 uint8_t *buf, *bp;
889 uint8_t *p;
890 uint8_t *pstart;
891 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000892 int littleEndian = 1; /* Run-time detection */
893 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000894
895 if(!st) {
896 errno = EINVAL;
897 return -1;
898 }
899
Lev Walkin8471cec2004-10-21 14:02:19 +0000900 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000901 if(!buf) return -1;
902
Lev Walkind7ad5612004-10-26 08:20:46 +0000903 if(*(char *)&littleEndian) {
904 pstart = (uint8_t *)&value + sizeof(value) - 1;
905 pend1 = (uint8_t *)&value;
906 add = -1;
907 } else {
908 pstart = (uint8_t *)&value;
909 pend1 = pstart + sizeof(value) - 1;
910 add = 1;
911 }
912
Lev Walkind703ff42004-10-21 11:21:25 +0000913 /*
914 * If the contents octet consists of more than one octet,
915 * then bits of the first octet and bit 8 of the second octet:
916 * a) shall not all be ones; and
917 * b) shall not all be zero.
918 */
Lev Walkin33700162004-10-26 09:03:31 +0000919 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000920 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000921 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000922 continue;
923 break;
Lev Walkin33700162004-10-26 09:03:31 +0000924 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000925 continue;
926 break;
927 }
928 break;
929 }
930 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700931 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000932 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000933
934 if(st->buf) FREEMEM(st->buf);
935 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000936 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000937
938 return 0;
939}
Lev Walkinb3751942012-09-02 19:36:47 -0700940
Lev Walkin72ec9092017-07-05 05:49:12 -0700941int
942asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
943 intmax_t v;
944 if(asn_INTEGER2imax(iptr, &v) == 0) {
945 if(v < LONG_MIN || v > LONG_MAX) {
946 errno = ERANGE;
947 return -1;
948 }
949 *l = v;
950 return 0;
951 } else {
952 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700953 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700954}
Lev Walkincad560a2013-03-16 07:00:58 -0700955
Lev Walkin72ec9092017-07-05 05:49:12 -0700956int
957asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
958 uintmax_t v;
959 if(asn_INTEGER2umax(iptr, &v) == 0) {
960 if(v > ULONG_MAX) {
961 errno = ERANGE;
962 return -1;
963 }
964 *l = v;
965 return 0;
966 } else {
967 return -1;
968 }
969}
970
971int
972asn_long2INTEGER(INTEGER_t *st, long value) {
973 return asn_imax2INTEGER(st, value);
974}
975
976int
977asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
978 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -0700979}
980
981/*
982 * Parse the number in the given string until the given *end position,
983 * returning the position after the last parsed character back using the
984 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -0700985 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -0700986 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700987enum asn_strtox_result_e
988asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700989 int sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -0700990 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -0700991
Lev Walkin72ec9092017-07-05 05:49:12 -0700992 const intmax_t upper_boundary = INTMAX_MAX / 10;
993 intmax_t last_digit_max = INTMAX_MAX % 10;
Lev Walkine09f9f12012-09-02 23:06:35 -0700994
Lev Walkin72ec9092017-07-05 05:49:12 -0700995 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -0700996
997 switch(*str) {
998 case '-':
999 last_digit_max++;
1000 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001001 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001002 case '+':
1003 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001004 if(str >= *end) {
1005 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001006 return ASN_STRTOX_EXPECT_MORE;
Lev Walkincad560a2013-03-16 07:00:58 -07001007 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001008 }
1009
Lev Walkin72ec9092017-07-05 05:49:12 -07001010 for(value = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001011 switch(*str) {
1012 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1013 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1014 int d = *str - '0';
Lev Walkin72ec9092017-07-05 05:49:12 -07001015 if(value < upper_boundary) {
1016 value = value * 10 + d;
1017 } else if(value == upper_boundary) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001018 if(d <= last_digit_max) {
1019 if(sign > 0) {
Lev Walkin72ec9092017-07-05 05:49:12 -07001020 value = value * 10 + d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001021 } else {
1022 sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001023 value = -value * 10 - d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001024 }
1025 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001026 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001027 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001028 }
1029 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001030 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001031 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001032 }
1033 }
1034 continue;
1035 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001036 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001037 *intp = sign * value;
1038 return ASN_STRTOX_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001039 }
1040 }
1041
Lev Walkincad560a2013-03-16 07:00:58 -07001042 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001043 *intp = sign * value;
1044 return ASN_STRTOX_OK;
1045}
1046
1047enum asn_strtox_result_e
1048asn_strtol_lim(const char *str, const char **end, long *lp) {
1049 intmax_t value;
1050 switch(asn_strtoimax_lim(str, end, &value)) {
1051 case ASN_STRTOX_ERROR_RANGE:
1052 return ASN_STRTOX_ERROR_RANGE;
1053 case ASN_STRTOX_ERROR_INVAL:
1054 return ASN_STRTOX_ERROR_INVAL;
1055 case ASN_STRTOX_EXPECT_MORE:
1056 return ASN_STRTOX_EXPECT_MORE;
1057 case ASN_STRTOX_OK:
1058 if(value >= LONG_MIN && value <= LONG_MAX) {
1059 *lp = value;
1060 return ASN_STRTOX_OK;
1061 } else {
1062 return ASN_STRTOX_ERROR_RANGE;
1063 }
1064 case ASN_STRTOX_EXTRA_DATA:
1065 if(value >= LONG_MIN && value <= LONG_MAX) {
1066 *lp = value;
1067 return ASN_STRTOX_EXTRA_DATA;
1068 } else {
1069 return ASN_STRTOX_ERROR_RANGE;
1070 }
1071 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001072
1073 assert(!"Unreachable");
1074 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001075}
1076