blob: 1533b4713e1fe8fcf693cdf49998dc13b44fd949 [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 Walkincc159472017-07-06 08:26:36 -070034#ifdef ASN_DISABLE_OER_SUPPORT
35 0,
36 0,
37#else
38 //INTEGER_decode_oer, /* OER decoder */
39 //INTEGER_encode_oer, /* Canonical OER encoder */
40 0,0,
41#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000042 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000043 asn_DEF_INTEGER_tags,
44 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
45 asn_DEF_INTEGER_tags, /* Same as above */
46 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070047 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000048 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000049 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000050 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000051};
52
53/*
Lev Walkinf15320b2004-06-03 03:38:44 +000054 * Encode INTEGER type using DER.
55 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000056asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000057INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000058 int tag_mode, ber_tlv_tag_t tag,
59 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000060 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000061
62 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000063 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000064
65 /*
66 * Canonicalize integer in the buffer.
67 * (Remove too long sign extension, remove some first 0x00 bytes)
68 */
69 if(st->buf) {
70 uint8_t *buf = st->buf;
71 uint8_t *end1 = buf + st->size - 1;
72 int shift;
73
74 /* Compute the number of superfluous leading bytes */
75 for(; buf < end1; buf++) {
76 /*
77 * If the contents octets of an integer value encoding
78 * consist of more than one octet, then the bits of the
79 * first octet and bit 8 of the second octet:
80 * a) shall not all be ones; and
81 * b) shall not all be zero.
82 */
83 switch(*buf) {
84 case 0x00: if((buf[1] & 0x80) == 0)
85 continue;
86 break;
87 case 0xff: if((buf[1] & 0x80))
88 continue;
89 break;
90 }
91 break;
92 }
93
94 /* Remove leading superfluous bytes from the integer */
95 shift = buf - st->buf;
96 if(shift) {
97 uint8_t *nb = st->buf;
98 uint8_t *end;
99
100 st->size -= shift; /* New size, minus bad bytes */
101 end = nb + st->size;
102
103 for(; nb < end; nb++, buf++)
104 *nb = *buf;
105 }
106
107 } /* if(1) */
108
Lev Walkin8e8078a2004-09-26 13:10:40 +0000109 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000110}
111
Lev Walkinc2350112005-03-29 17:19:53 +0000112static 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 +0000113
Lev Walkinf15320b2004-06-03 03:38:44 +0000114/*
115 * INTEGER specific human-readable output.
116 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000117static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700118INTEGER__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 +0000119 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000120 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000121 uint8_t *buf = st->buf;
122 uint8_t *buf_end = st->buf + st->size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700123 intmax_t value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000124 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 char *p;
126 int ret;
127
Lev Walkin97f8edc2013-03-28 04:38:41 -0700128 if(specs && specs->field_unsigned)
Lev Walkin72ec9092017-07-05 05:49:12 -0700129 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
Lev Walkin97f8edc2013-03-28 04:38:41 -0700130 else
Lev Walkin72ec9092017-07-05 05:49:12 -0700131 ret = asn_INTEGER2imax(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000132
Lev Walkinf15320b2004-06-03 03:38:44 +0000133 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700134 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000135 const asn_INTEGER_enum_map_t *el;
136 size_t scrsize;
137 char *scr;
138
Lev Walkin97f8edc2013-03-28 04:38:41 -0700139 el = (value >= 0 || !specs || !specs->field_unsigned)
140 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000141 if(el) {
142 scrsize = el->enum_len + 32;
143 scr = (char *)alloca(scrsize);
144 if(plainOrXER == 0)
145 ret = snprintf(scr, scrsize,
Lev Walkin72ec9092017-07-05 05:49:12 -0700146 "%" PRIdMAX " (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000147 else
148 ret = snprintf(scr, scrsize,
149 "<%s/>", el->enum_name);
150 } else if(plainOrXER && specs && specs->strict_enumeration) {
151 ASN_DEBUG("ASN.1 forbids dealing with "
152 "unknown value of ENUMERATED type");
153 errno = EPERM;
154 return -1;
155 } else {
156 scrsize = sizeof(scratch);
157 scr = scratch;
Lev Walkin72ec9092017-07-05 05:49:12 -0700158 ret = snprintf(
159 scr, scrsize,
160 (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX,
161 value);
162 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000163 assert(ret > 0 && (size_t)ret < scrsize);
164 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
165 } else if(plainOrXER && specs && specs->strict_enumeration) {
166 /*
167 * Here and earlier, we cannot encode the ENUMERATED values
168 * if there is no corresponding identifier.
169 */
170 ASN_DEBUG("ASN.1 forbids dealing with "
171 "unknown value of ENUMERATED type");
172 errno = EPERM;
173 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 }
175
176 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000177 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000178 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700179 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000180 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000182 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000184 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 p = scratch;
186 }
187 *p++ = h2c[*buf >> 4];
188 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000189 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 }
Lev Walkindb13f512004-07-19 17:30:25 +0000191 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000192 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000193
Lev Walkina9cc46e2004-09-22 16:06:28 +0000194 wrote += p - scratch;
195 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
196}
197
198/*
199 * INTEGER specific human-readable output.
200 */
201int
Lev Walkin5e033762004-09-29 13:26:15 +0000202INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000203 asn_app_consume_bytes_f *cb, void *app_key) {
204 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000205 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000206
207 (void)td;
208 (void)ilevel;
209
Lev Walkind500a962005-11-27 13:06:56 +0000210 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000211 ret = cb("<absent>", 8, app_key);
212 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000213 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000214
Lev Walkin8e8078a2004-09-26 13:10:40 +0000215 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000216}
217
Lev Walkine0b56e02005-02-25 12:10:27 +0000218struct e2v_key {
219 const char *start;
220 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700221 const asn_INTEGER_enum_map_t *vemap;
222 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000223};
224static int
225INTEGER__compar_enum2value(const void *kp, const void *am) {
226 const struct e2v_key *key = (const struct e2v_key *)kp;
227 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
228 const char *ptr, *end, *name;
229
230 /* Remap the element (sort by different criterion) */
231 el = key->vemap + key->evmap[el - key->vemap];
232
233 /* Compare strings */
234 for(ptr = key->start, end = key->stop, name = el->enum_name;
235 ptr < end; ptr++, name++) {
236 if(*ptr != *name)
237 return *(const unsigned char *)ptr
238 - *(const unsigned char *)name;
239 }
240 return name[0] ? -1 : 0;
241}
242
243static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000244INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700245 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000246 int count = specs ? specs->map_count : 0;
247 struct e2v_key key;
248 const char *lp;
249
250 if(!count) return NULL;
251
252 /* Guaranteed: assert(lstart < lstop); */
253 /* Figure out the tag name */
254 for(lstart++, lp = lstart; lp < lstop; lp++) {
255 switch(*lp) {
256 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
257 case 0x2f: /* '/' */ case 0x3e: /* '>' */
258 break;
259 default:
260 continue;
261 }
262 break;
263 }
264 if(lp == lstop) return NULL; /* No tag found */
265 lstop = lp;
266
267 key.start = lstart;
268 key.stop = lstop;
269 key.vemap = specs->value2enum;
270 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000271 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
272 specs->value2enum, count, sizeof(specs->value2enum[0]),
273 INTEGER__compar_enum2value);
274 if(el_found) {
275 /* Remap enum2value into value2enum */
276 el_found = key.vemap + key.evmap[el_found - key.vemap];
277 }
278 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000279}
280
281static int
282INTEGER__compar_value2enum(const void *kp, const void *am) {
283 long a = *(const long *)kp;
284 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
285 long b = el->nat_value;
286 if(a < b) return -1;
287 else if(a == b) return 0;
288 else return 1;
289}
290
Lev Walkinc2350112005-03-29 17:19:53 +0000291const asn_INTEGER_enum_map_t *
292INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000293 int count = specs ? specs->map_count : 0;
294 if(!count) return 0;
295 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
296 count, sizeof(specs->value2enum[0]),
297 INTEGER__compar_value2enum);
298}
299
Lev Walkinc744a022006-09-15 18:33:25 +0000300static int
301INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
302 void *p = MALLOC(min_size + 1);
303 if(p) {
304 void *b = st->buf;
305 st->size = 0;
306 st->buf = p;
307 FREEMEM(b);
308 return 0;
309 } else {
310 return -1;
311 }
312}
313
Lev Walkind703ff42004-10-21 11:21:25 +0000314/*
315 * Decode the chunk of XML text encoding INTEGER.
316 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000317static enum xer_pbd_rval
318INTEGER__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 +0000319 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700320 intmax_t dec_value;
321 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000322 const char *lp;
323 const char *lstart = (const char *)chunk_buf;
324 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000325 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700326 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000327 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000328 ST_WAITDIGITS,
329 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700330 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000331 ST_HEXDIGIT1,
332 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700333 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000334 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700335 ST_END_ENUM,
336 ST_UNEXPECTED
337 } state = ST_LEADSPACE;
338 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
339 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000340
Lev Walkinc744a022006-09-15 18:33:25 +0000341 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000342 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
343 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000344
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000345 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
346 return XPBD_SYSTEM_FAILURE;
347
Lev Walkind703ff42004-10-21 11:21:25 +0000348 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000349 * We may have received a tag here. It will be processed inline.
350 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000351 */
Lev Walkinb3751942012-09-02 19:36:47 -0700352 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000353 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000354 switch(lv) {
355 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000356 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700357 case ST_LEADSPACE:
358 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700359 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000360 case ST_SKIPSPHEX:
361 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700362 case ST_DIGITS:
363 dec_value_end = lp;
364 state = ST_DIGITS_TRAILSPACE;
365 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700366 case ST_HEXCOLON:
367 state = ST_HEXDIGITS_TRAILSPACE;
368 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000369 default:
370 break;
371 }
Lev Walkind703ff42004-10-21 11:21:25 +0000372 break;
373 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700374 if(state == ST_LEADSPACE) {
375 dec_value = 0;
376 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000377 state = ST_WAITDIGITS;
378 continue;
379 }
380 break;
381 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700382 if(state == ST_LEADSPACE) {
383 dec_value = 0;
384 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000385 state = ST_WAITDIGITS;
386 continue;
387 }
388 break;
389 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
390 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000391 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700392 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000393 case ST_SKIPSPHEX: /* Fall through */
394 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700395 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000396 state = ST_HEXDIGIT2;
397 continue;
398 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700399 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000400 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700401 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000402 continue;
403 case ST_HEXCOLON:
404 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700405 case ST_LEADSPACE:
406 dec_value = 0;
407 dec_value_start = lp;
408 /* FALL THROUGH */
409 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000410 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700411 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700412 default:
413 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000414 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700415 break;
416 case 0x3c: /* '<', start of XML encoded enumeration */
417 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000418 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000419 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000420 (asn_INTEGER_specifics_t *)
421 td->specifics, lstart, lstop);
422 if(el) {
423 ASN_DEBUG("Found \"%s\" => %ld",
424 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700425 dec_value = el->nat_value;
426 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000427 lp = lstop - 1;
428 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000429 }
430 ASN_DEBUG("Unknown identifier for INTEGER");
431 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000432 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000433 case 0x3a: /* ':' */
434 if(state == ST_HEXCOLON) {
435 /* This colon is expected */
436 state = ST_HEXDIGIT1;
437 continue;
438 } else if(state == ST_DIGITS) {
439 /* The colon here means that we have
440 * decoded the first two hexadecimal
441 * places as a decimal value.
442 * Switch decoding mode. */
443 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000444 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700445 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000446 lp = lstart - 1;
447 continue;
448 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400449 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000450 break;
451 }
452 /* [A-Fa-f] */
453 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
454 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
455 switch(state) {
456 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700457 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000458 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700459 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
460 hex_value += 10;
461 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000462 state = ST_HEXDIGIT2;
463 continue;
464 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700465 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
466 hex_value += 10;
467 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000468 state = ST_HEXCOLON;
469 continue;
470 case ST_DIGITS:
471 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000472 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700473 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000474 lp = lstart - 1;
475 continue;
476 default:
477 break;
478 }
479 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000480 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000481
482 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700483 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400484 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700485 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000486 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000487 }
488
Lev Walkinc744a022006-09-15 18:33:25 +0000489 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700490 case ST_END_ENUM:
491 /* Got a complete and valid enumeration encoded as a tag. */
492 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000493 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700494 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700495 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700496 case ST_DIGITS_TRAILSPACE:
497 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700498 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700499 case ASN_STRTOX_OK:
Lev Walkine09f9f12012-09-02 23:06:35 -0700500 break;
Lev Walkin72ec9092017-07-05 05:49:12 -0700501 case ASN_STRTOX_ERROR_RANGE:
Lev Walkine09f9f12012-09-02 23:06:35 -0700502 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700503 case ASN_STRTOX_ERROR_INVAL:
504 case ASN_STRTOX_EXPECT_MORE:
505 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700506 return XPBD_BROKEN_ENCODING;
507 }
Lev Walkinc744a022006-09-15 18:33:25 +0000508 break;
509 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700510 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000511 st->buf[st->size] = 0; /* Just in case termination */
512 return XPBD_BODY_CONSUMED;
513 case ST_HEXDIGIT1:
514 case ST_HEXDIGIT2:
515 case ST_SKIPSPHEX:
516 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700517 case ST_LEADSPACE:
518 /* Content not found */
519 return XPBD_NOT_BODY_IGNORE;
520 case ST_WAITDIGITS:
521 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700522 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
523 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000524 }
Lev Walkind703ff42004-10-21 11:21:25 +0000525
Lev Walkine09f9f12012-09-02 23:06:35 -0700526 /*
527 * Convert the result of parsing of enumeration or a straight
528 * decimal value into a BER representation.
529 */
530 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000531 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000532
Lev Walkin0fab1a62005-03-09 22:19:25 +0000533 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000534}
535
536asn_dec_rval_t
537INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
538 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000539 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000540
541 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000542 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000543 buf_ptr, size, INTEGER__xer_body_decode);
544}
545
Lev Walkina9cc46e2004-09-22 16:06:28 +0000546asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000547INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000548 int ilevel, enum xer_encoder_flags_e flags,
549 asn_app_consume_bytes_f *cb, void *app_key) {
550 const INTEGER_t *st = (const INTEGER_t *)sptr;
551 asn_enc_rval_t er;
552
553 (void)ilevel;
554 (void)flags;
555
Lev Walkind500a962005-11-27 13:06:56 +0000556 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700557 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000558
Lev Walkine0b56e02005-02-25 12:10:27 +0000559 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700560 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000561
Lev Walkin7c1dc052016-03-14 03:08:15 -0700562 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000563}
564
Lev Walkind7703cf2012-09-03 01:45:03 -0700565#ifndef ASN_DISABLE_PER_SUPPORT
566
Lev Walkin59b176e2005-11-26 11:25:14 +0000567asn_dec_rval_t
568INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
569 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000570 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000571 asn_dec_rval_t rval = { RC_OK, 0 };
572 INTEGER_t *st = (INTEGER_t *)*sptr;
573 asn_per_constraint_t *ct;
574 int repeat;
575
576 (void)opt_codec_ctx;
577
578 if(!st) {
579 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700580 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000581 }
582
583 if(!constraints) constraints = td->per_constraints;
584 ct = constraints ? &constraints->value : 0;
585
586 if(ct && ct->flags & APC_EXTENSIBLE) {
587 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700588 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000589 if(inext) ct = 0;
590 }
591
592 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000593 st->buf = 0;
594 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000595 if(ct) {
596 if(ct->flags & APC_SEMI_CONSTRAINED) {
597 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700598 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000599 st->size = 1;
600 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
601 size_t size = (ct->range_bits + 7) >> 3;
602 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700603 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000604 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000606 }
607
Lev Walkin6c527842014-02-09 04:34:54 -0800608 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000609 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800610 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000611 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
612 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800613 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700614 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800615
616 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700617 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800618 if(uper_get_constrained_whole_number(pd,
619 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700620 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800621 ASN_DEBUG("Got value %lu + low %ld",
622 uvalue, ct->lower_bound);
623 uvalue += ct->lower_bound;
624 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700625 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800626 } else {
Lev Walkin72ec9092017-07-05 05:49:12 -0700627 unsigned long svalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800628 if(uper_get_constrained_whole_number(pd,
629 &svalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700630 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800631 ASN_DEBUG("Got value %ld + low %ld",
632 svalue, ct->lower_bound);
633 svalue += ct->lower_bound;
634 if(asn_long2INTEGER(st, svalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700635 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800636 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000637 return rval;
638 }
639 } else {
640 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
641 }
642
643 /* X.691, #12.2.3, #12.2.4 */
644 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700645 ssize_t len = 0;
646 void *p = NULL;
647 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000648
649 /* Get the PER length */
650 len = uper_get_length(pd, -1, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700651 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000652
653 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700654 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000655 st->buf = (uint8_t *)p;
656
657 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700658 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000659 st->size += len;
660 } while(repeat);
661 st->buf[st->size] = 0; /* JIC */
662
663 /* #12.2.3 */
664 if(ct && ct->lower_bound) {
665 /*
666 * TODO: replace by in-place arithmetics.
667 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700668 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000669 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700670 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700671 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700672 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000673 }
674
675 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000676}
677
Lev Walkin523de9e2006-08-18 01:34:18 +0000678asn_enc_rval_t
679INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
680 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000681 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000682 asn_enc_rval_t er;
683 INTEGER_t *st = (INTEGER_t *)sptr;
684 const uint8_t *buf;
685 const uint8_t *end;
686 asn_per_constraint_t *ct;
687 long value = 0;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100688 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000689
Lev Walkin7c1dc052016-03-14 03:08:15 -0700690 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000691
692 if(!constraints) constraints = td->per_constraints;
693 ct = constraints ? &constraints->value : 0;
694
695 er.encoded = 0;
696
697 if(ct) {
698 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000699 if(specs && specs->field_unsigned) {
700 unsigned long uval;
701 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700702 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000703 /* Check proper range */
704 if(ct->flags & APC_SEMI_CONSTRAINED) {
705 if(uval < (unsigned long)ct->lower_bound)
706 inext = 1;
707 } else if(ct->range_bits >= 0) {
708 if(uval < (unsigned long)ct->lower_bound
709 || uval > (unsigned long)ct->upper_bound)
710 inext = 1;
711 }
712 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
713 uval, st->buf[0], st->size,
714 ct->lower_bound, ct->upper_bound,
715 inext ? "ext" : "fix");
716 value = uval;
717 } else {
718 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700719 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000720 /* Check proper range */
721 if(ct->flags & APC_SEMI_CONSTRAINED) {
722 if(value < ct->lower_bound)
723 inext = 1;
724 } else if(ct->range_bits >= 0) {
725 if(value < ct->lower_bound
726 || value > ct->upper_bound)
727 inext = 1;
728 }
729 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
730 value, st->buf[0], st->size,
731 ct->lower_bound, ct->upper_bound,
732 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000733 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000734 if(ct->flags & APC_EXTENSIBLE) {
735 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700736 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000737 if(inext) ct = 0;
738 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700739 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000740 }
741 }
742
743
Lev Walkin6c527842014-02-09 04:34:54 -0800744 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000745 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800746 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800747 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
748 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100749 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800750 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700751 ASN__ENCODE_FAILED;
752 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000753 }
754
755 if(ct && ct->lower_bound) {
756 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
757 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700758 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000759 }
760
761 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
762 ssize_t mayEncode = uper_put_length(po, end - buf);
763 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700764 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000765 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700766 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000767 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000768 }
769
Lev Walkin7c1dc052016-03-14 03:08:15 -0700770 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000771}
772
Lev Walkind7703cf2012-09-03 01:45:03 -0700773#endif /* ASN_DISABLE_PER_SUPPORT */
774
Lev Walkinf15320b2004-06-03 03:38:44 +0000775int
Lev Walkin72ec9092017-07-05 05:49:12 -0700776asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000777 uint8_t *b, *end;
778 size_t size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700779 intmax_t value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000780
781 /* Sanity checking */
782 if(!iptr || !iptr->buf || !lptr) {
783 errno = EINVAL;
784 return -1;
785 }
786
787 /* Cache the begin/end of the buffer */
788 b = iptr->buf; /* Start of the INTEGER buffer */
789 size = iptr->size;
790 end = b + size; /* Where to stop */
791
Lev Walkin72ec9092017-07-05 05:49:12 -0700792 if(size > sizeof(value)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000793 uint8_t *end1 = end - 1;
794 /*
795 * Slightly more advanced processing,
Lev Walkin72ec9092017-07-05 05:49:12 -0700796 * able to process INTEGERs with >sizeof(value) bytes
797 * when the actual value is small, e.g. for intmax_t == int32_t
798 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000799 */
800 /* Skip out the insignificant leading bytes */
801 for(; b < end1; b++) {
802 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700803 case 0x00: if((b[1] & 0x80) == 0) continue; break;
804 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000805 }
806 break;
807 }
808
809 size = end - b;
Lev Walkin72ec9092017-07-05 05:49:12 -0700810 if(size > sizeof(value)) {
811 /* Still cannot fit the sizeof(value) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000812 errno = ERANGE;
813 return -1;
814 }
815 }
816
817 /* Shortcut processing of a corner case */
818 if(end == b) {
819 *lptr = 0;
820 return 0;
821 }
822
823 /* Perform the sign initialization */
Lev Walkin72ec9092017-07-05 05:49:12 -0700824 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
825 if((*b >> 7)) value = -1; else value = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000826
827 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700828 for(; b < end; b++) {
829 value = (value << 8) | *b;
830 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000831
Lev Walkin72ec9092017-07-05 05:49:12 -0700832 *lptr = value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000833 return 0;
834}
Lev Walkind703ff42004-10-21 11:21:25 +0000835
Lev Walkin72ec9092017-07-05 05:49:12 -0700836/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000837int
Lev Walkin72ec9092017-07-05 05:49:12 -0700838asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000839 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700840 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000841 size_t size;
842
843 if(!iptr || !iptr->buf || !lptr) {
844 errno = EINVAL;
845 return -1;
846 }
847
848 b = iptr->buf;
849 size = iptr->size;
850 end = b + size;
851
852 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700853 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000854 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700855 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000856 errno = ERANGE;
857 return -1;
858 }
859 }
860
861 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700862 for(value = 0; b < end; b++)
863 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000864
Lev Walkin72ec9092017-07-05 05:49:12 -0700865 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000866 return 0;
867}
868
869int
Lev Walkin72ec9092017-07-05 05:49:12 -0700870asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
871 uint8_t *buf;
872 uint8_t *end;
873 uint8_t *b;
874 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000875
Lev Walkin72ec9092017-07-05 05:49:12 -0700876 if(value <= INTMAX_MAX) {
877 return asn_imax2INTEGER(st, value);
878 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000879
Lev Walkin72ec9092017-07-05 05:49:12 -0700880 buf = (uint8_t *)MALLOC(1 + sizeof(value));
881 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000882
Lev Walkin72ec9092017-07-05 05:49:12 -0700883 end = buf + (sizeof(value) + 1);
884 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
885 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
886 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000887
Lev Walkin72ec9092017-07-05 05:49:12 -0700888 if(st->buf) FREEMEM(st->buf);
889 st->buf = buf;
890 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000891
892 return 0;
893}
894
895int
Lev Walkin72ec9092017-07-05 05:49:12 -0700896asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000897 uint8_t *buf, *bp;
898 uint8_t *p;
899 uint8_t *pstart;
900 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000901 int littleEndian = 1; /* Run-time detection */
902 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000903
904 if(!st) {
905 errno = EINVAL;
906 return -1;
907 }
908
Lev Walkin8471cec2004-10-21 14:02:19 +0000909 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000910 if(!buf) return -1;
911
Lev Walkind7ad5612004-10-26 08:20:46 +0000912 if(*(char *)&littleEndian) {
913 pstart = (uint8_t *)&value + sizeof(value) - 1;
914 pend1 = (uint8_t *)&value;
915 add = -1;
916 } else {
917 pstart = (uint8_t *)&value;
918 pend1 = pstart + sizeof(value) - 1;
919 add = 1;
920 }
921
Lev Walkind703ff42004-10-21 11:21:25 +0000922 /*
923 * If the contents octet consists of more than one octet,
924 * then bits of the first octet and bit 8 of the second octet:
925 * a) shall not all be ones; and
926 * b) shall not all be zero.
927 */
Lev Walkin33700162004-10-26 09:03:31 +0000928 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000929 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000930 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000931 continue;
932 break;
Lev Walkin33700162004-10-26 09:03:31 +0000933 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000934 continue;
935 break;
936 }
937 break;
938 }
939 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700940 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000941 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000942
943 if(st->buf) FREEMEM(st->buf);
944 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000945 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000946
947 return 0;
948}
Lev Walkinb3751942012-09-02 19:36:47 -0700949
Lev Walkin72ec9092017-07-05 05:49:12 -0700950int
951asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
952 intmax_t v;
953 if(asn_INTEGER2imax(iptr, &v) == 0) {
954 if(v < LONG_MIN || v > LONG_MAX) {
955 errno = ERANGE;
956 return -1;
957 }
958 *l = v;
959 return 0;
960 } else {
961 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700962 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700963}
Lev Walkincad560a2013-03-16 07:00:58 -0700964
Lev Walkin72ec9092017-07-05 05:49:12 -0700965int
966asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
967 uintmax_t v;
968 if(asn_INTEGER2umax(iptr, &v) == 0) {
969 if(v > ULONG_MAX) {
970 errno = ERANGE;
971 return -1;
972 }
973 *l = v;
974 return 0;
975 } else {
976 return -1;
977 }
978}
979
980int
981asn_long2INTEGER(INTEGER_t *st, long value) {
982 return asn_imax2INTEGER(st, value);
983}
984
985int
986asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
987 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -0700988}
989
990/*
991 * Parse the number in the given string until the given *end position,
992 * returning the position after the last parsed character back using the
993 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -0700994 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -0700995 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700996enum asn_strtox_result_e
997asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700998 int sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -0700999 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -07001000
Lev Walkin72ec9092017-07-05 05:49:12 -07001001 const intmax_t upper_boundary = INTMAX_MAX / 10;
1002 intmax_t last_digit_max = INTMAX_MAX % 10;
Lev Walkine09f9f12012-09-02 23:06:35 -07001003
Lev Walkin72ec9092017-07-05 05:49:12 -07001004 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001005
1006 switch(*str) {
1007 case '-':
1008 last_digit_max++;
1009 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001010 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001011 case '+':
1012 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001013 if(str >= *end) {
1014 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001015 return ASN_STRTOX_EXPECT_MORE;
Lev Walkincad560a2013-03-16 07:00:58 -07001016 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001017 }
1018
Lev Walkin72ec9092017-07-05 05:49:12 -07001019 for(value = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001020 switch(*str) {
1021 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1022 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1023 int d = *str - '0';
Lev Walkin72ec9092017-07-05 05:49:12 -07001024 if(value < upper_boundary) {
1025 value = value * 10 + d;
1026 } else if(value == upper_boundary) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001027 if(d <= last_digit_max) {
1028 if(sign > 0) {
Lev Walkin72ec9092017-07-05 05:49:12 -07001029 value = value * 10 + d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001030 } else {
1031 sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001032 value = -value * 10 - d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001033 }
1034 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001035 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001036 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001037 }
1038 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001039 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001040 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001041 }
1042 }
1043 continue;
1044 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001045 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001046 *intp = sign * value;
1047 return ASN_STRTOX_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001048 }
1049 }
1050
Lev Walkincad560a2013-03-16 07:00:58 -07001051 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001052 *intp = sign * value;
1053 return ASN_STRTOX_OK;
1054}
1055
1056enum asn_strtox_result_e
1057asn_strtol_lim(const char *str, const char **end, long *lp) {
1058 intmax_t value;
1059 switch(asn_strtoimax_lim(str, end, &value)) {
1060 case ASN_STRTOX_ERROR_RANGE:
1061 return ASN_STRTOX_ERROR_RANGE;
1062 case ASN_STRTOX_ERROR_INVAL:
1063 return ASN_STRTOX_ERROR_INVAL;
1064 case ASN_STRTOX_EXPECT_MORE:
1065 return ASN_STRTOX_EXPECT_MORE;
1066 case ASN_STRTOX_OK:
1067 if(value >= LONG_MIN && value <= LONG_MAX) {
1068 *lp = value;
1069 return ASN_STRTOX_OK;
1070 } else {
1071 return ASN_STRTOX_ERROR_RANGE;
1072 }
1073 case ASN_STRTOX_EXTRA_DATA:
1074 if(value >= LONG_MIN && value <= LONG_MAX) {
1075 *lp = value;
1076 return ASN_STRTOX_EXTRA_DATA;
1077 } else {
1078 return ASN_STRTOX_ERROR_RANGE;
1079 }
1080 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001081
1082 assert(!"Unreachable");
1083 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001084}
1085