blob: 00aea439658b68ffd5663c819ec756da964e5535 [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 Walkincc159472017-07-06 08:26:36 -070027#ifdef ASN_DISABLE_OER_SUPPORT
28 0,
29 0,
30#else
Lev Walkin527497f2017-07-14 08:56:36 +040031 INTEGER_decode_oer, /* OER decoder */
32 INTEGER_encode_oer, /* Canonical OER encoder */
Lev Walkincc159472017-07-06 08:26:36 -070033#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040034#ifdef ASN_DISABLE_PER_SUPPORT
35 0,
36 0,
37#else
38 INTEGER_decode_uper, /* Unaligned PER decoder */
39 INTEGER_encode_uper, /* Unaligned PER encoder */
40#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000041 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000042 asn_DEF_INTEGER_tags,
43 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
44 asn_DEF_INTEGER_tags, /* Same as above */
45 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070046 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000047 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000048 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000049 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000050};
51
52/*
Lev Walkinf15320b2004-06-03 03:38:44 +000053 * Encode INTEGER type using DER.
54 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000055asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000056INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000057 int tag_mode, ber_tlv_tag_t tag,
58 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000059 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000060
61 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000062 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000063
64 /*
65 * Canonicalize integer in the buffer.
66 * (Remove too long sign extension, remove some first 0x00 bytes)
67 */
68 if(st->buf) {
69 uint8_t *buf = st->buf;
70 uint8_t *end1 = buf + st->size - 1;
71 int shift;
72
73 /* Compute the number of superfluous leading bytes */
74 for(; buf < end1; buf++) {
75 /*
76 * If the contents octets of an integer value encoding
77 * consist of more than one octet, then the bits of the
78 * first octet and bit 8 of the second octet:
79 * a) shall not all be ones; and
80 * b) shall not all be zero.
81 */
82 switch(*buf) {
83 case 0x00: if((buf[1] & 0x80) == 0)
84 continue;
85 break;
86 case 0xff: if((buf[1] & 0x80))
87 continue;
88 break;
89 }
90 break;
91 }
92
93 /* Remove leading superfluous bytes from the integer */
94 shift = buf - st->buf;
95 if(shift) {
96 uint8_t *nb = st->buf;
97 uint8_t *end;
98
99 st->size -= shift; /* New size, minus bad bytes */
100 end = nb + st->size;
101
102 for(; nb < end; nb++, buf++)
103 *nb = *buf;
104 }
105
106 } /* if(1) */
107
Lev Walkin8e8078a2004-09-26 13:10:40 +0000108 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000109}
110
Lev Walkinc2350112005-03-29 17:19:53 +0000111static 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 +0000112
Lev Walkinf15320b2004-06-03 03:38:44 +0000113/*
114 * INTEGER specific human-readable output.
115 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000116static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700117INTEGER__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 +0000118 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000119 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 uint8_t *buf = st->buf;
121 uint8_t *buf_end = st->buf + st->size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700122 intmax_t value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000123 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000124 char *p;
125 int ret;
126
Lev Walkin97f8edc2013-03-28 04:38:41 -0700127 if(specs && specs->field_unsigned)
Lev Walkin72ec9092017-07-05 05:49:12 -0700128 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
Lev Walkin97f8edc2013-03-28 04:38:41 -0700129 else
Lev Walkin72ec9092017-07-05 05:49:12 -0700130 ret = asn_INTEGER2imax(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000131
Lev Walkinf15320b2004-06-03 03:38:44 +0000132 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700133 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000134 const asn_INTEGER_enum_map_t *el;
135 size_t scrsize;
136 char *scr;
137
Lev Walkin97f8edc2013-03-28 04:38:41 -0700138 el = (value >= 0 || !specs || !specs->field_unsigned)
139 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000140 if(el) {
141 scrsize = el->enum_len + 32;
142 scr = (char *)alloca(scrsize);
143 if(plainOrXER == 0)
144 ret = snprintf(scr, scrsize,
Lev Walkin72ec9092017-07-05 05:49:12 -0700145 "%" PRIdMAX " (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000146 else
147 ret = snprintf(scr, scrsize,
148 "<%s/>", el->enum_name);
149 } else if(plainOrXER && specs && specs->strict_enumeration) {
150 ASN_DEBUG("ASN.1 forbids dealing with "
151 "unknown value of ENUMERATED type");
152 errno = EPERM;
153 return -1;
154 } else {
155 scrsize = sizeof(scratch);
156 scr = scratch;
Lev Walkin72ec9092017-07-05 05:49:12 -0700157 ret = snprintf(
158 scr, scrsize,
159 (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX,
160 value);
161 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000162 assert(ret > 0 && (size_t)ret < scrsize);
163 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
164 } else if(plainOrXER && specs && specs->strict_enumeration) {
165 /*
166 * Here and earlier, we cannot encode the ENUMERATED values
167 * if there is no corresponding identifier.
168 */
169 ASN_DEBUG("ASN.1 forbids dealing with "
170 "unknown value of ENUMERATED type");
171 errno = EPERM;
172 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 }
174
175 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000176 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700178 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000179 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000181 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000183 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 p = scratch;
185 }
186 *p++ = h2c[*buf >> 4];
187 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000188 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 }
Lev Walkindb13f512004-07-19 17:30:25 +0000190 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000191 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000192
Lev Walkina9cc46e2004-09-22 16:06:28 +0000193 wrote += p - scratch;
194 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
195}
196
197/*
198 * INTEGER specific human-readable output.
199 */
200int
Lev Walkin5e033762004-09-29 13:26:15 +0000201INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000202 asn_app_consume_bytes_f *cb, void *app_key) {
203 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000204 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000205
206 (void)td;
207 (void)ilevel;
208
Lev Walkind500a962005-11-27 13:06:56 +0000209 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000210 ret = cb("<absent>", 8, app_key);
211 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000212 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000213
Lev Walkin8e8078a2004-09-26 13:10:40 +0000214 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000215}
216
Lev Walkine0b56e02005-02-25 12:10:27 +0000217struct e2v_key {
218 const char *start;
219 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700220 const asn_INTEGER_enum_map_t *vemap;
221 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000222};
223static int
224INTEGER__compar_enum2value(const void *kp, const void *am) {
225 const struct e2v_key *key = (const struct e2v_key *)kp;
226 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
227 const char *ptr, *end, *name;
228
229 /* Remap the element (sort by different criterion) */
230 el = key->vemap + key->evmap[el - key->vemap];
231
232 /* Compare strings */
233 for(ptr = key->start, end = key->stop, name = el->enum_name;
234 ptr < end; ptr++, name++) {
235 if(*ptr != *name)
236 return *(const unsigned char *)ptr
237 - *(const unsigned char *)name;
238 }
239 return name[0] ? -1 : 0;
240}
241
242static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000243INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700244 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000245 int count = specs ? specs->map_count : 0;
246 struct e2v_key key;
247 const char *lp;
248
249 if(!count) return NULL;
250
251 /* Guaranteed: assert(lstart < lstop); */
252 /* Figure out the tag name */
253 for(lstart++, lp = lstart; lp < lstop; lp++) {
254 switch(*lp) {
255 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
256 case 0x2f: /* '/' */ case 0x3e: /* '>' */
257 break;
258 default:
259 continue;
260 }
261 break;
262 }
263 if(lp == lstop) return NULL; /* No tag found */
264 lstop = lp;
265
266 key.start = lstart;
267 key.stop = lstop;
268 key.vemap = specs->value2enum;
269 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000270 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
271 specs->value2enum, count, sizeof(specs->value2enum[0]),
272 INTEGER__compar_enum2value);
273 if(el_found) {
274 /* Remap enum2value into value2enum */
275 el_found = key.vemap + key.evmap[el_found - key.vemap];
276 }
277 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000278}
279
280static int
281INTEGER__compar_value2enum(const void *kp, const void *am) {
282 long a = *(const long *)kp;
283 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
284 long b = el->nat_value;
285 if(a < b) return -1;
286 else if(a == b) return 0;
287 else return 1;
288}
289
Lev Walkinc2350112005-03-29 17:19:53 +0000290const asn_INTEGER_enum_map_t *
291INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000292 int count = specs ? specs->map_count : 0;
293 if(!count) return 0;
294 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
295 count, sizeof(specs->value2enum[0]),
296 INTEGER__compar_value2enum);
297}
298
Lev Walkinc744a022006-09-15 18:33:25 +0000299static int
300INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
301 void *p = MALLOC(min_size + 1);
302 if(p) {
303 void *b = st->buf;
304 st->size = 0;
305 st->buf = p;
306 FREEMEM(b);
307 return 0;
308 } else {
309 return -1;
310 }
311}
312
Lev Walkind703ff42004-10-21 11:21:25 +0000313/*
314 * Decode the chunk of XML text encoding INTEGER.
315 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000316static enum xer_pbd_rval
317INTEGER__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 +0000318 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700319 intmax_t dec_value;
320 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000321 const char *lp;
322 const char *lstart = (const char *)chunk_buf;
323 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000324 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700325 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000326 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000327 ST_WAITDIGITS,
328 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700329 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000330 ST_HEXDIGIT1,
331 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700332 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000333 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700334 ST_END_ENUM,
335 ST_UNEXPECTED
336 } state = ST_LEADSPACE;
337 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
338 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000339
Lev Walkinc744a022006-09-15 18:33:25 +0000340 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000341 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
342 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000343
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000344 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
345 return XPBD_SYSTEM_FAILURE;
346
Lev Walkind703ff42004-10-21 11:21:25 +0000347 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000348 * We may have received a tag here. It will be processed inline.
349 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000350 */
Lev Walkinb3751942012-09-02 19:36:47 -0700351 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000352 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000353 switch(lv) {
354 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000355 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700356 case ST_LEADSPACE:
357 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700358 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000359 case ST_SKIPSPHEX:
360 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700361 case ST_DIGITS:
362 dec_value_end = lp;
363 state = ST_DIGITS_TRAILSPACE;
364 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700365 case ST_HEXCOLON:
366 state = ST_HEXDIGITS_TRAILSPACE;
367 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000368 default:
369 break;
370 }
Lev Walkind703ff42004-10-21 11:21:25 +0000371 break;
372 case 0x2d: /* '-' */
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 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700381 if(state == ST_LEADSPACE) {
382 dec_value = 0;
383 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000384 state = ST_WAITDIGITS;
385 continue;
386 }
387 break;
388 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
389 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000390 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700391 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000392 case ST_SKIPSPHEX: /* Fall through */
393 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700394 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000395 state = ST_HEXDIGIT2;
396 continue;
397 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700398 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000399 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700400 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000401 continue;
402 case ST_HEXCOLON:
403 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700404 case ST_LEADSPACE:
405 dec_value = 0;
406 dec_value_start = lp;
407 /* FALL THROUGH */
408 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000409 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700410 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700411 default:
412 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000413 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700414 break;
415 case 0x3c: /* '<', start of XML encoded enumeration */
416 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000417 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000418 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000419 (asn_INTEGER_specifics_t *)
420 td->specifics, lstart, lstop);
421 if(el) {
422 ASN_DEBUG("Found \"%s\" => %ld",
423 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700424 dec_value = el->nat_value;
425 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000426 lp = lstop - 1;
427 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000428 }
429 ASN_DEBUG("Unknown identifier for INTEGER");
430 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000431 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000432 case 0x3a: /* ':' */
433 if(state == ST_HEXCOLON) {
434 /* This colon is expected */
435 state = ST_HEXDIGIT1;
436 continue;
437 } else if(state == ST_DIGITS) {
438 /* The colon here means that we have
439 * decoded the first two hexadecimal
440 * places as a decimal value.
441 * Switch decoding mode. */
442 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000443 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700444 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000445 lp = lstart - 1;
446 continue;
447 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400448 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000449 break;
450 }
451 /* [A-Fa-f] */
452 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
453 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
454 switch(state) {
455 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700456 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000457 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700458 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
459 hex_value += 10;
460 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000461 state = ST_HEXDIGIT2;
462 continue;
463 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700464 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
465 hex_value += 10;
466 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000467 state = ST_HEXCOLON;
468 continue;
469 case ST_DIGITS:
470 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000471 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700472 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000473 lp = lstart - 1;
474 continue;
475 default:
476 break;
477 }
478 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000479 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000480
481 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700482 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400483 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700484 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000485 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000486 }
487
Lev Walkinc744a022006-09-15 18:33:25 +0000488 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700489 case ST_END_ENUM:
490 /* Got a complete and valid enumeration encoded as a tag. */
491 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000492 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700493 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700494 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700495 case ST_DIGITS_TRAILSPACE:
496 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700497 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700498 case ASN_STRTOX_OK:
Lev Walkine09f9f12012-09-02 23:06:35 -0700499 break;
Lev Walkin72ec9092017-07-05 05:49:12 -0700500 case ASN_STRTOX_ERROR_RANGE:
Lev Walkine09f9f12012-09-02 23:06:35 -0700501 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700502 case ASN_STRTOX_ERROR_INVAL:
503 case ASN_STRTOX_EXPECT_MORE:
504 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700505 return XPBD_BROKEN_ENCODING;
506 }
Lev Walkinc744a022006-09-15 18:33:25 +0000507 break;
508 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700509 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000510 st->buf[st->size] = 0; /* Just in case termination */
511 return XPBD_BODY_CONSUMED;
512 case ST_HEXDIGIT1:
513 case ST_HEXDIGIT2:
514 case ST_SKIPSPHEX:
515 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700516 case ST_LEADSPACE:
517 /* Content not found */
518 return XPBD_NOT_BODY_IGNORE;
519 case ST_WAITDIGITS:
520 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700521 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
522 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000523 }
Lev Walkind703ff42004-10-21 11:21:25 +0000524
Lev Walkine09f9f12012-09-02 23:06:35 -0700525 /*
526 * Convert the result of parsing of enumeration or a straight
527 * decimal value into a BER representation.
528 */
529 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000530 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000531
Lev Walkin0fab1a62005-03-09 22:19:25 +0000532 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000533}
534
535asn_dec_rval_t
536INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
537 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000538 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000539
540 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000541 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000542 buf_ptr, size, INTEGER__xer_body_decode);
543}
544
Lev Walkina9cc46e2004-09-22 16:06:28 +0000545asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000546INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000547 int ilevel, enum xer_encoder_flags_e flags,
548 asn_app_consume_bytes_f *cb, void *app_key) {
549 const INTEGER_t *st = (const INTEGER_t *)sptr;
550 asn_enc_rval_t er;
551
552 (void)ilevel;
553 (void)flags;
554
Lev Walkind500a962005-11-27 13:06:56 +0000555 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700556 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000557
Lev Walkine0b56e02005-02-25 12:10:27 +0000558 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700559 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000560
Lev Walkin7c1dc052016-03-14 03:08:15 -0700561 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000562}
563
Lev Walkind7703cf2012-09-03 01:45:03 -0700564#ifndef ASN_DISABLE_PER_SUPPORT
565
Lev Walkin59b176e2005-11-26 11:25:14 +0000566asn_dec_rval_t
567INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
568 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000569 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000570 asn_dec_rval_t rval = { RC_OK, 0 };
571 INTEGER_t *st = (INTEGER_t *)*sptr;
572 asn_per_constraint_t *ct;
573 int repeat;
574
575 (void)opt_codec_ctx;
576
577 if(!st) {
578 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700579 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000580 }
581
582 if(!constraints) constraints = td->per_constraints;
583 ct = constraints ? &constraints->value : 0;
584
585 if(ct && ct->flags & APC_EXTENSIBLE) {
586 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700587 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000588 if(inext) ct = 0;
589 }
590
591 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000592 st->buf = 0;
593 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000594 if(ct) {
595 if(ct->flags & APC_SEMI_CONSTRAINED) {
596 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700597 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000598 st->size = 1;
599 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
600 size_t size = (ct->range_bits + 7) >> 3;
601 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700602 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000603 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000604 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 }
606
Lev Walkin6c527842014-02-09 04:34:54 -0800607 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000608 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800609 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000610 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
611 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800612 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700613 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800614
615 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700616 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800617 if(uper_get_constrained_whole_number(pd,
618 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700619 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800620 ASN_DEBUG("Got value %lu + low %ld",
621 uvalue, ct->lower_bound);
622 uvalue += ct->lower_bound;
623 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700624 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800625 } else {
Lev Walkin72ec9092017-07-05 05:49:12 -0700626 unsigned long svalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800627 if(uper_get_constrained_whole_number(pd,
628 &svalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700629 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800630 ASN_DEBUG("Got value %ld + low %ld",
631 svalue, ct->lower_bound);
632 svalue += ct->lower_bound;
633 if(asn_long2INTEGER(st, svalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700634 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800635 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000636 return rval;
637 }
638 } else {
639 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
640 }
641
642 /* X.691, #12.2.3, #12.2.4 */
643 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700644 ssize_t len = 0;
645 void *p = NULL;
646 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000647
648 /* Get the PER length */
649 len = uper_get_length(pd, -1, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700650 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000651
652 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700653 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000654 st->buf = (uint8_t *)p;
655
656 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700657 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000658 st->size += len;
659 } while(repeat);
660 st->buf[st->size] = 0; /* JIC */
661
662 /* #12.2.3 */
663 if(ct && ct->lower_bound) {
664 /*
665 * TODO: replace by in-place arithmetics.
666 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700667 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000668 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700669 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700670 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700671 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000672 }
673
674 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000675}
676
Lev Walkin523de9e2006-08-18 01:34:18 +0000677asn_enc_rval_t
678INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
679 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000680 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000681 asn_enc_rval_t er;
682 INTEGER_t *st = (INTEGER_t *)sptr;
683 const uint8_t *buf;
684 const uint8_t *end;
685 asn_per_constraint_t *ct;
686 long value = 0;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100687 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000688
Lev Walkin7c1dc052016-03-14 03:08:15 -0700689 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000690
691 if(!constraints) constraints = td->per_constraints;
692 ct = constraints ? &constraints->value : 0;
693
694 er.encoded = 0;
695
696 if(ct) {
697 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000698 if(specs && specs->field_unsigned) {
699 unsigned long uval;
700 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700701 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000702 /* Check proper range */
703 if(ct->flags & APC_SEMI_CONSTRAINED) {
704 if(uval < (unsigned long)ct->lower_bound)
705 inext = 1;
706 } else if(ct->range_bits >= 0) {
707 if(uval < (unsigned long)ct->lower_bound
708 || uval > (unsigned long)ct->upper_bound)
709 inext = 1;
710 }
711 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
712 uval, st->buf[0], st->size,
713 ct->lower_bound, ct->upper_bound,
714 inext ? "ext" : "fix");
715 value = uval;
716 } else {
717 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700718 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000719 /* Check proper range */
720 if(ct->flags & APC_SEMI_CONSTRAINED) {
721 if(value < ct->lower_bound)
722 inext = 1;
723 } else if(ct->range_bits >= 0) {
724 if(value < ct->lower_bound
725 || value > ct->upper_bound)
726 inext = 1;
727 }
728 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
729 value, st->buf[0], st->size,
730 ct->lower_bound, ct->upper_bound,
731 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000732 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000733 if(ct->flags & APC_EXTENSIBLE) {
734 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700735 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000736 if(inext) ct = 0;
737 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700738 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000739 }
740 }
741
742
Lev Walkin6c527842014-02-09 04:34:54 -0800743 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000744 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800745 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800746 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
747 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100748 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800749 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700750 ASN__ENCODE_FAILED;
751 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000752 }
753
754 if(ct && ct->lower_bound) {
755 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
756 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700757 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000758 }
759
760 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
761 ssize_t mayEncode = uper_put_length(po, end - buf);
762 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700763 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000764 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700765 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000766 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000767 }
768
Lev Walkin7c1dc052016-03-14 03:08:15 -0700769 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000770}
771
Lev Walkind7703cf2012-09-03 01:45:03 -0700772#endif /* ASN_DISABLE_PER_SUPPORT */
773
Lev Walkinf15320b2004-06-03 03:38:44 +0000774int
Lev Walkin72ec9092017-07-05 05:49:12 -0700775asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000776 uint8_t *b, *end;
777 size_t size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700778 intmax_t value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000779
780 /* Sanity checking */
781 if(!iptr || !iptr->buf || !lptr) {
782 errno = EINVAL;
783 return -1;
784 }
785
786 /* Cache the begin/end of the buffer */
787 b = iptr->buf; /* Start of the INTEGER buffer */
788 size = iptr->size;
789 end = b + size; /* Where to stop */
790
Lev Walkin72ec9092017-07-05 05:49:12 -0700791 if(size > sizeof(value)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000792 uint8_t *end1 = end - 1;
793 /*
794 * Slightly more advanced processing,
Lev Walkin72ec9092017-07-05 05:49:12 -0700795 * able to process INTEGERs with >sizeof(value) bytes
796 * when the actual value is small, e.g. for intmax_t == int32_t
797 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000798 */
799 /* Skip out the insignificant leading bytes */
800 for(; b < end1; b++) {
801 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700802 case 0x00: if((b[1] & 0x80) == 0) continue; break;
803 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000804 }
805 break;
806 }
807
808 size = end - b;
Lev Walkin72ec9092017-07-05 05:49:12 -0700809 if(size > sizeof(value)) {
810 /* Still cannot fit the sizeof(value) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000811 errno = ERANGE;
812 return -1;
813 }
814 }
815
816 /* Shortcut processing of a corner case */
817 if(end == b) {
818 *lptr = 0;
819 return 0;
820 }
821
822 /* Perform the sign initialization */
Lev Walkin72ec9092017-07-05 05:49:12 -0700823 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
824 if((*b >> 7)) value = -1; else value = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000825
826 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700827 for(; b < end; b++) {
828 value = (value << 8) | *b;
829 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000830
Lev Walkin72ec9092017-07-05 05:49:12 -0700831 *lptr = value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000832 return 0;
833}
Lev Walkind703ff42004-10-21 11:21:25 +0000834
Lev Walkin72ec9092017-07-05 05:49:12 -0700835/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000836int
Lev Walkin72ec9092017-07-05 05:49:12 -0700837asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000838 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700839 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000840 size_t size;
841
842 if(!iptr || !iptr->buf || !lptr) {
843 errno = EINVAL;
844 return -1;
845 }
846
847 b = iptr->buf;
848 size = iptr->size;
849 end = b + size;
850
851 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700852 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000853 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700854 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000855 errno = ERANGE;
856 return -1;
857 }
858 }
859
860 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700861 for(value = 0; b < end; b++)
862 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000863
Lev Walkin72ec9092017-07-05 05:49:12 -0700864 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000865 return 0;
866}
867
868int
Lev Walkin72ec9092017-07-05 05:49:12 -0700869asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
870 uint8_t *buf;
871 uint8_t *end;
872 uint8_t *b;
873 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000874
Lev Walkin72ec9092017-07-05 05:49:12 -0700875 if(value <= INTMAX_MAX) {
876 return asn_imax2INTEGER(st, value);
877 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000878
Lev Walkin72ec9092017-07-05 05:49:12 -0700879 buf = (uint8_t *)MALLOC(1 + sizeof(value));
880 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000881
Lev Walkin72ec9092017-07-05 05:49:12 -0700882 end = buf + (sizeof(value) + 1);
883 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
884 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
885 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000886
Lev Walkin72ec9092017-07-05 05:49:12 -0700887 if(st->buf) FREEMEM(st->buf);
888 st->buf = buf;
889 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000890
891 return 0;
892}
893
894int
Lev Walkin72ec9092017-07-05 05:49:12 -0700895asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000896 uint8_t *buf, *bp;
897 uint8_t *p;
898 uint8_t *pstart;
899 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000900 int littleEndian = 1; /* Run-time detection */
901 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000902
903 if(!st) {
904 errno = EINVAL;
905 return -1;
906 }
907
Lev Walkin8471cec2004-10-21 14:02:19 +0000908 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000909 if(!buf) return -1;
910
Lev Walkind7ad5612004-10-26 08:20:46 +0000911 if(*(char *)&littleEndian) {
912 pstart = (uint8_t *)&value + sizeof(value) - 1;
913 pend1 = (uint8_t *)&value;
914 add = -1;
915 } else {
916 pstart = (uint8_t *)&value;
917 pend1 = pstart + sizeof(value) - 1;
918 add = 1;
919 }
920
Lev Walkind703ff42004-10-21 11:21:25 +0000921 /*
922 * If the contents octet consists of more than one octet,
923 * then bits of the first octet and bit 8 of the second octet:
924 * a) shall not all be ones; and
925 * b) shall not all be zero.
926 */
Lev Walkin33700162004-10-26 09:03:31 +0000927 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000928 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000929 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000930 continue;
931 break;
Lev Walkin33700162004-10-26 09:03:31 +0000932 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000933 continue;
934 break;
935 }
936 break;
937 }
938 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700939 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000940 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000941
942 if(st->buf) FREEMEM(st->buf);
943 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000944 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000945
946 return 0;
947}
Lev Walkinb3751942012-09-02 19:36:47 -0700948
Lev Walkin72ec9092017-07-05 05:49:12 -0700949int
950asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
951 intmax_t v;
952 if(asn_INTEGER2imax(iptr, &v) == 0) {
953 if(v < LONG_MIN || v > LONG_MAX) {
954 errno = ERANGE;
955 return -1;
956 }
957 *l = v;
958 return 0;
959 } else {
960 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700961 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700962}
Lev Walkincad560a2013-03-16 07:00:58 -0700963
Lev Walkin72ec9092017-07-05 05:49:12 -0700964int
965asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
966 uintmax_t v;
967 if(asn_INTEGER2umax(iptr, &v) == 0) {
968 if(v > ULONG_MAX) {
969 errno = ERANGE;
970 return -1;
971 }
972 *l = v;
973 return 0;
974 } else {
975 return -1;
976 }
977}
978
979int
980asn_long2INTEGER(INTEGER_t *st, long value) {
981 return asn_imax2INTEGER(st, value);
982}
983
984int
985asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
986 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -0700987}
988
989/*
990 * Parse the number in the given string until the given *end position,
991 * returning the position after the last parsed character back using the
992 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -0700993 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -0700994 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700995enum asn_strtox_result_e
996asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700997 int sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -0700998 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -0700999
Lev Walkin72ec9092017-07-05 05:49:12 -07001000 const intmax_t upper_boundary = INTMAX_MAX / 10;
1001 intmax_t last_digit_max = INTMAX_MAX % 10;
Lev Walkine09f9f12012-09-02 23:06:35 -07001002
Lev Walkin72ec9092017-07-05 05:49:12 -07001003 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001004
1005 switch(*str) {
1006 case '-':
1007 last_digit_max++;
1008 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001009 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001010 case '+':
1011 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001012 if(str >= *end) {
1013 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001014 return ASN_STRTOX_EXPECT_MORE;
Lev Walkincad560a2013-03-16 07:00:58 -07001015 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001016 }
1017
Lev Walkin72ec9092017-07-05 05:49:12 -07001018 for(value = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001019 switch(*str) {
1020 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1021 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1022 int d = *str - '0';
Lev Walkin72ec9092017-07-05 05:49:12 -07001023 if(value < upper_boundary) {
1024 value = value * 10 + d;
1025 } else if(value == upper_boundary) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001026 if(d <= last_digit_max) {
1027 if(sign > 0) {
Lev Walkin72ec9092017-07-05 05:49:12 -07001028 value = value * 10 + d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001029 } else {
1030 sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001031 value = -value * 10 - d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001032 }
1033 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001034 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001035 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001036 }
1037 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001038 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001039 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001040 }
1041 }
1042 continue;
1043 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001044 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001045 *intp = sign * value;
1046 return ASN_STRTOX_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001047 }
1048 }
1049
Lev Walkincad560a2013-03-16 07:00:58 -07001050 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001051 *intp = sign * value;
1052 return ASN_STRTOX_OK;
1053}
1054
1055enum asn_strtox_result_e
1056asn_strtol_lim(const char *str, const char **end, long *lp) {
1057 intmax_t value;
1058 switch(asn_strtoimax_lim(str, end, &value)) {
1059 case ASN_STRTOX_ERROR_RANGE:
1060 return ASN_STRTOX_ERROR_RANGE;
1061 case ASN_STRTOX_ERROR_INVAL:
1062 return ASN_STRTOX_ERROR_INVAL;
1063 case ASN_STRTOX_EXPECT_MORE:
1064 return ASN_STRTOX_EXPECT_MORE;
1065 case ASN_STRTOX_OK:
1066 if(value >= LONG_MIN && value <= LONG_MAX) {
1067 *lp = value;
1068 return ASN_STRTOX_OK;
1069 } else {
1070 return ASN_STRTOX_ERROR_RANGE;
1071 }
1072 case ASN_STRTOX_EXTRA_DATA:
1073 if(value >= LONG_MIN && value <= LONG_MAX) {
1074 *lp = value;
1075 return ASN_STRTOX_EXTRA_DATA;
1076 } else {
1077 return ASN_STRTOX_ERROR_RANGE;
1078 }
1079 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001080
1081 assert(!"Unreachable");
1082 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001083}
1084