blob: b3f0da12b3f8bf43239b2608af749132d248142d [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,
Harald Welte36cd7dd2015-08-30 16:41:20 +020030 0,
31 0,
Lev Walkind7703cf2012-09-03 01:45:03 -070032#else
Lev Walkin59b176e2005-11-26 11:25:14 +000033 INTEGER_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000034 INTEGER_encode_uper, /* Unaligned PER encoder */
Harald Welte498c9712015-08-30 16:33:07 +020035 INTEGER_decode_aper,
36 INTEGER_encode_aper,
Lev Walkind7703cf2012-09-03 01:45:03 -070037#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000038 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000039 asn_DEF_INTEGER_tags,
40 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
41 asn_DEF_INTEGER_tags, /* Same as above */
42 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000043 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000044 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000045 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000046};
47
48/*
Lev Walkinf15320b2004-06-03 03:38:44 +000049 * Encode INTEGER type using DER.
50 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000051asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000052INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000053 int tag_mode, ber_tlv_tag_t tag,
54 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000055 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000056
57 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000058 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000059
60 /*
61 * Canonicalize integer in the buffer.
62 * (Remove too long sign extension, remove some first 0x00 bytes)
63 */
64 if(st->buf) {
65 uint8_t *buf = st->buf;
66 uint8_t *end1 = buf + st->size - 1;
67 int shift;
68
69 /* Compute the number of superfluous leading bytes */
70 for(; buf < end1; buf++) {
71 /*
72 * If the contents octets of an integer value encoding
73 * consist of more than one octet, then the bits of the
74 * first octet and bit 8 of the second octet:
75 * a) shall not all be ones; and
76 * b) shall not all be zero.
77 */
78 switch(*buf) {
79 case 0x00: if((buf[1] & 0x80) == 0)
80 continue;
81 break;
82 case 0xff: if((buf[1] & 0x80))
83 continue;
84 break;
85 }
86 break;
87 }
88
89 /* Remove leading superfluous bytes from the integer */
90 shift = buf - st->buf;
91 if(shift) {
92 uint8_t *nb = st->buf;
93 uint8_t *end;
94
95 st->size -= shift; /* New size, minus bad bytes */
96 end = nb + st->size;
97
98 for(; nb < end; nb++, buf++)
99 *nb = *buf;
100 }
101
102 } /* if(1) */
103
Lev Walkin8e8078a2004-09-26 13:10:40 +0000104 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000105}
106
Lev Walkinc2350112005-03-29 17:19:53 +0000107static 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 +0000108
Lev Walkinf15320b2004-06-03 03:38:44 +0000109/*
110 * INTEGER specific human-readable output.
111 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000112static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700113INTEGER__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 +0000114 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000115 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 uint8_t *buf = st->buf;
117 uint8_t *buf_end = st->buf + st->size;
Lev Walkin97f8edc2013-03-28 04:38:41 -0700118 signed long value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000119 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 char *p;
121 int ret;
122
Lev Walkin97f8edc2013-03-28 04:38:41 -0700123 if(specs && specs->field_unsigned)
124 ret = asn_INTEGER2ulong(st, (unsigned long *)&value);
125 else
126 ret = asn_INTEGER2long(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000127
Lev Walkinf15320b2004-06-03 03:38:44 +0000128 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700129 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000130 const asn_INTEGER_enum_map_t *el;
131 size_t scrsize;
132 char *scr;
133
Lev Walkin97f8edc2013-03-28 04:38:41 -0700134 el = (value >= 0 || !specs || !specs->field_unsigned)
135 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000136 if(el) {
137 scrsize = el->enum_len + 32;
138 scr = (char *)alloca(scrsize);
139 if(plainOrXER == 0)
140 ret = snprintf(scr, scrsize,
Lev Walkin97f8edc2013-03-28 04:38:41 -0700141 "%ld (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000142 else
143 ret = snprintf(scr, scrsize,
144 "<%s/>", el->enum_name);
145 } else if(plainOrXER && specs && specs->strict_enumeration) {
146 ASN_DEBUG("ASN.1 forbids dealing with "
147 "unknown value of ENUMERATED type");
148 errno = EPERM;
149 return -1;
150 } else {
151 scrsize = sizeof(scratch);
152 scr = scratch;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000153 ret = snprintf(scr, scrsize,
154 (specs && specs->field_unsigned)
Lev Walkin97f8edc2013-03-28 04:38:41 -0700155 ?"%lu":"%ld", value);
Lev Walkine0b56e02005-02-25 12:10:27 +0000156 }
157 assert(ret > 0 && (size_t)ret < scrsize);
158 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
159 } else if(plainOrXER && specs && specs->strict_enumeration) {
160 /*
161 * Here and earlier, we cannot encode the ENUMERATED values
162 * if there is no corresponding identifier.
163 */
164 ASN_DEBUG("ASN.1 forbids dealing with "
165 "unknown value of ENUMERATED type");
166 errno = EPERM;
167 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000168 }
169
170 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000171 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700173 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000174 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000176 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000178 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 p = scratch;
180 }
181 *p++ = h2c[*buf >> 4];
182 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000183 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 }
Lev Walkindb13f512004-07-19 17:30:25 +0000185 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000186 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000187
Lev Walkina9cc46e2004-09-22 16:06:28 +0000188 wrote += p - scratch;
189 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
190}
191
192/*
193 * INTEGER specific human-readable output.
194 */
195int
Lev Walkin5e033762004-09-29 13:26:15 +0000196INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197 asn_app_consume_bytes_f *cb, void *app_key) {
198 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000199 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200
201 (void)td;
202 (void)ilevel;
203
Lev Walkind500a962005-11-27 13:06:56 +0000204 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000205 ret = cb("<absent>", 8, app_key);
206 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000207 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208
Lev Walkin8e8078a2004-09-26 13:10:40 +0000209 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000210}
211
Lev Walkine0b56e02005-02-25 12:10:27 +0000212struct e2v_key {
213 const char *start;
214 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700215 const asn_INTEGER_enum_map_t *vemap;
216 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000217};
218static int
219INTEGER__compar_enum2value(const void *kp, const void *am) {
220 const struct e2v_key *key = (const struct e2v_key *)kp;
221 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
222 const char *ptr, *end, *name;
223
224 /* Remap the element (sort by different criterion) */
225 el = key->vemap + key->evmap[el - key->vemap];
226
227 /* Compare strings */
228 for(ptr = key->start, end = key->stop, name = el->enum_name;
229 ptr < end; ptr++, name++) {
230 if(*ptr != *name)
231 return *(const unsigned char *)ptr
232 - *(const unsigned char *)name;
233 }
234 return name[0] ? -1 : 0;
235}
236
237static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000238INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700239 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000240 int count = specs ? specs->map_count : 0;
241 struct e2v_key key;
242 const char *lp;
243
244 if(!count) return NULL;
245
246 /* Guaranteed: assert(lstart < lstop); */
247 /* Figure out the tag name */
248 for(lstart++, lp = lstart; lp < lstop; lp++) {
249 switch(*lp) {
250 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
251 case 0x2f: /* '/' */ case 0x3e: /* '>' */
252 break;
253 default:
254 continue;
255 }
256 break;
257 }
258 if(lp == lstop) return NULL; /* No tag found */
259 lstop = lp;
260
261 key.start = lstart;
262 key.stop = lstop;
263 key.vemap = specs->value2enum;
264 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000265 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
266 specs->value2enum, count, sizeof(specs->value2enum[0]),
267 INTEGER__compar_enum2value);
268 if(el_found) {
269 /* Remap enum2value into value2enum */
270 el_found = key.vemap + key.evmap[el_found - key.vemap];
271 }
272 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000273}
274
275static int
276INTEGER__compar_value2enum(const void *kp, const void *am) {
277 long a = *(const long *)kp;
278 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
279 long b = el->nat_value;
280 if(a < b) return -1;
281 else if(a == b) return 0;
282 else return 1;
283}
284
Lev Walkinc2350112005-03-29 17:19:53 +0000285const asn_INTEGER_enum_map_t *
286INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000287 int count = specs ? specs->map_count : 0;
288 if(!count) return 0;
289 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
290 count, sizeof(specs->value2enum[0]),
291 INTEGER__compar_value2enum);
292}
293
Lev Walkinc744a022006-09-15 18:33:25 +0000294static int
295INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
296 void *p = MALLOC(min_size + 1);
297 if(p) {
298 void *b = st->buf;
299 st->size = 0;
300 st->buf = p;
301 FREEMEM(b);
302 return 0;
303 } else {
304 return -1;
305 }
306}
307
Lev Walkind703ff42004-10-21 11:21:25 +0000308/*
309 * Decode the chunk of XML text encoding INTEGER.
310 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000311static enum xer_pbd_rval
312INTEGER__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 +0000313 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinb3751942012-09-02 19:36:47 -0700314 long dec_value;
Lev Walkin0ff71292013-03-25 18:51:51 -0700315 long hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000316 const char *lp;
317 const char *lstart = (const char *)chunk_buf;
318 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000319 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700320 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000321 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000322 ST_WAITDIGITS,
323 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700324 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000325 ST_HEXDIGIT1,
326 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700327 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000328 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700329 ST_END_ENUM,
330 ST_UNEXPECTED
331 } state = ST_LEADSPACE;
332 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
333 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000334
Lev Walkinc744a022006-09-15 18:33:25 +0000335 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000336 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
337 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000338
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000339 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
340 return XPBD_SYSTEM_FAILURE;
341
Lev Walkind703ff42004-10-21 11:21:25 +0000342 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000343 * We may have received a tag here. It will be processed inline.
344 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000345 */
Lev Walkinb3751942012-09-02 19:36:47 -0700346 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000347 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000348 switch(lv) {
349 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000350 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700351 case ST_LEADSPACE:
352 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700353 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000354 case ST_SKIPSPHEX:
355 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700356 case ST_DIGITS:
357 dec_value_end = lp;
358 state = ST_DIGITS_TRAILSPACE;
359 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700360 case ST_HEXCOLON:
361 state = ST_HEXDIGITS_TRAILSPACE;
362 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000363 default:
364 break;
365 }
Lev Walkind703ff42004-10-21 11:21:25 +0000366 break;
367 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700368 if(state == ST_LEADSPACE) {
369 dec_value = 0;
370 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000371 state = ST_WAITDIGITS;
372 continue;
373 }
374 break;
375 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700376 if(state == ST_LEADSPACE) {
377 dec_value = 0;
378 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000379 state = ST_WAITDIGITS;
380 continue;
381 }
382 break;
383 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
384 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000385 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700386 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000387 case ST_SKIPSPHEX: /* Fall through */
388 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700389 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000390 state = ST_HEXDIGIT2;
391 continue;
392 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700393 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000394 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700395 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000396 continue;
397 case ST_HEXCOLON:
398 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700399 case ST_LEADSPACE:
400 dec_value = 0;
401 dec_value_start = lp;
402 /* FALL THROUGH */
403 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000404 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700405 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700406 default:
407 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000408 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700409 break;
410 case 0x3c: /* '<', start of XML encoded enumeration */
411 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000412 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000413 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000414 (asn_INTEGER_specifics_t *)
415 td->specifics, lstart, lstop);
416 if(el) {
Harald Welte498c9712015-08-30 16:33:07 +0200417 ASN_DEBUG("Found \"%s\" => %lld",
Lev Walkine0b56e02005-02-25 12:10:27 +0000418 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700419 dec_value = el->nat_value;
420 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000421 lp = lstop - 1;
422 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000423 }
424 ASN_DEBUG("Unknown identifier for INTEGER");
425 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000426 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000427 case 0x3a: /* ':' */
428 if(state == ST_HEXCOLON) {
429 /* This colon is expected */
430 state = ST_HEXDIGIT1;
431 continue;
432 } else if(state == ST_DIGITS) {
433 /* The colon here means that we have
434 * decoded the first two hexadecimal
435 * places as a decimal value.
436 * Switch decoding mode. */
437 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000438 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700439 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000440 lp = lstart - 1;
441 continue;
442 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400443 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000444 break;
445 }
446 /* [A-Fa-f] */
447 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
448 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
449 switch(state) {
450 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700451 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000452 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700453 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
454 hex_value += 10;
455 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000456 state = ST_HEXDIGIT2;
457 continue;
458 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700459 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
460 hex_value += 10;
461 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000462 state = ST_HEXCOLON;
463 continue;
464 case ST_DIGITS:
465 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000466 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700467 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000468 lp = lstart - 1;
469 continue;
470 default:
471 break;
472 }
473 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000474 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000475
476 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700477 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400478 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700479 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000480 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000481 }
482
Lev Walkinc744a022006-09-15 18:33:25 +0000483 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700484 case ST_END_ENUM:
485 /* Got a complete and valid enumeration encoded as a tag. */
486 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000487 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700488 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700489 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700490 case ST_DIGITS_TRAILSPACE:
491 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700492 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700493 case ASN_STRTOL_OK:
494 break;
495 case ASN_STRTOL_ERROR_RANGE:
496 return XPBD_DECODER_LIMIT;
497 case ASN_STRTOL_ERROR_INVAL:
Lev Walkincad560a2013-03-16 07:00:58 -0700498 case ASN_STRTOL_EXPECT_MORE:
499 case ASN_STRTOL_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700500 return XPBD_BROKEN_ENCODING;
501 }
Lev Walkinc744a022006-09-15 18:33:25 +0000502 break;
503 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700504 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000505 st->buf[st->size] = 0; /* Just in case termination */
506 return XPBD_BODY_CONSUMED;
507 case ST_HEXDIGIT1:
508 case ST_HEXDIGIT2:
509 case ST_SKIPSPHEX:
510 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700511 case ST_LEADSPACE:
512 /* Content not found */
513 return XPBD_NOT_BODY_IGNORE;
514 case ST_WAITDIGITS:
515 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700516 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
517 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000518 }
Lev Walkind703ff42004-10-21 11:21:25 +0000519
Lev Walkine09f9f12012-09-02 23:06:35 -0700520 /*
521 * Convert the result of parsing of enumeration or a straight
522 * decimal value into a BER representation.
523 */
524 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000525 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000526
Lev Walkin0fab1a62005-03-09 22:19:25 +0000527 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000528}
529
530asn_dec_rval_t
531INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
532 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000533 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000534
535 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000536 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000537 buf_ptr, size, INTEGER__xer_body_decode);
538}
539
Lev Walkina9cc46e2004-09-22 16:06:28 +0000540asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000541INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000542 int ilevel, enum xer_encoder_flags_e flags,
543 asn_app_consume_bytes_f *cb, void *app_key) {
544 const INTEGER_t *st = (const INTEGER_t *)sptr;
545 asn_enc_rval_t er;
546
547 (void)ilevel;
548 (void)flags;
549
Lev Walkind500a962005-11-27 13:06:56 +0000550 if(!st || !st->buf)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000551 _ASN_ENCODE_FAILED;
552
Lev Walkine0b56e02005-02-25 12:10:27 +0000553 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000554 if(er.encoded < 0) _ASN_ENCODE_FAILED;
555
Lev Walkin59b176e2005-11-26 11:25:14 +0000556 _ASN_ENCODED_OK(er);
557}
558
Lev Walkind7703cf2012-09-03 01:45:03 -0700559#ifndef ASN_DISABLE_PER_SUPPORT
560
Lev Walkin59b176e2005-11-26 11:25:14 +0000561asn_dec_rval_t
562INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
563 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000564 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000565 asn_dec_rval_t rval = { RC_OK, 0 };
566 INTEGER_t *st = (INTEGER_t *)*sptr;
567 asn_per_constraint_t *ct;
568 int repeat;
569
570 (void)opt_codec_ctx;
571
572 if(!st) {
573 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
574 if(!st) _ASN_DECODE_FAILED;
575 }
576
577 if(!constraints) constraints = td->per_constraints;
578 ct = constraints ? &constraints->value : 0;
579
580 if(ct && ct->flags & APC_EXTENSIBLE) {
581 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000582 if(inext < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000583 if(inext) ct = 0;
584 }
585
586 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000587 st->buf = 0;
588 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000589 if(ct) {
590 if(ct->flags & APC_SEMI_CONSTRAINED) {
591 st->buf = (uint8_t *)CALLOC(1, 2);
592 if(!st->buf) _ASN_DECODE_FAILED;
593 st->size = 1;
594 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
595 size_t size = (ct->range_bits + 7) >> 3;
596 st->buf = (uint8_t *)MALLOC(1 + size + 1);
597 if(!st->buf) _ASN_DECODE_FAILED;
598 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000599 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000600 }
601
Lev Walkin6c527842014-02-09 04:34:54 -0800602 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000603 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800604 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
606 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800607 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin59b176e2005-11-26 11:25:14 +0000608 _ASN_DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800609
610 if(specs && specs->field_unsigned) {
611 unsigned long uvalue;
612 if(uper_get_constrained_whole_number(pd,
613 &uvalue, ct->range_bits))
614 _ASN_DECODE_STARVED;
Harald Welte498c9712015-08-30 16:33:07 +0200615 ASN_DEBUG("Got value %lu + low %lld",
Lev Walkin6c527842014-02-09 04:34:54 -0800616 uvalue, ct->lower_bound);
617 uvalue += ct->lower_bound;
618 if(asn_ulong2INTEGER(st, uvalue))
619 _ASN_DECODE_FAILED;
620 } else {
621 unsigned long svalue;
622 if(uper_get_constrained_whole_number(pd,
623 &svalue, ct->range_bits))
624 _ASN_DECODE_STARVED;
625 ASN_DEBUG("Got value %ld + low %ld",
626 svalue, ct->lower_bound);
627 svalue += ct->lower_bound;
628 if(asn_long2INTEGER(st, svalue))
629 _ASN_DECODE_FAILED;
630 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000631 return rval;
632 }
633 } else {
634 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
635 }
636
637 /* X.691, #12.2.3, #12.2.4 */
638 do {
639 ssize_t len;
640 void *p;
641 int ret;
642
643 /* Get the PER length */
644 len = uper_get_length(pd, -1, &repeat);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000645 if(len < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000646
647 p = REALLOC(st->buf, st->size + len + 1);
648 if(!p) _ASN_DECODE_FAILED;
649 st->buf = (uint8_t *)p;
650
651 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000652 if(ret < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000653 st->size += len;
654 } while(repeat);
655 st->buf[st->size] = 0; /* JIC */
656
657 /* #12.2.3 */
658 if(ct && ct->lower_bound) {
659 /*
660 * TODO: replace by in-place arithmetics.
661 */
662 long value;
663 if(asn_INTEGER2long(st, &value))
664 _ASN_DECODE_FAILED;
665 if(asn_long2INTEGER(st, value + ct->lower_bound))
666 _ASN_DECODE_FAILED;
667 }
668
669 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000670}
671
Harald Welte498c9712015-08-30 16:33:07 +0200672asn_dec_rval_t
673INTEGER_decode_aper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
674 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
675 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
676 asn_dec_rval_t rval = { RC_OK, 0 };
677 INTEGER_t *st = (INTEGER_t *)*sptr;
678 asn_per_constraint_t *ct;
679 int repeat;
680
681 (void)opt_codec_ctx;
682
683 if(!st) {
684 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
685 if(!st) _ASN_DECODE_FAILED;
686 }
687
688 if(!constraints) constraints = td->per_constraints;
689 ct = constraints ? &constraints->value : 0;
690
691 if(ct && ct->flags & APC_EXTENSIBLE) {
692 int inext = per_get_few_bits(pd, 1);
693 if(inext < 0) _ASN_DECODE_STARVED;
694 if(inext) ct = 0;
695 }
696
697 FREEMEM(st->buf);
698 st->buf = 0;
699 st->size = 0;
700 if(ct) {
701 if(ct->flags & APC_SEMI_CONSTRAINED) {
702 st->buf = (uint8_t *)CALLOC(1, 2);
703 if(!st->buf) _ASN_DECODE_FAILED;
704 st->size = 1;
705 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
706 size_t size = (ct->range_bits + 7) >> 3;
707 st->buf = (uint8_t *)MALLOC(1 + size + 1);
708 if(!st->buf) _ASN_DECODE_FAILED;
709 st->size = size;
710 }
711 }
712
713 /* X.691, #12.2.2 */
714 if(ct && ct->flags != APC_UNCONSTRAINED) {
715 /* #10.5.6 */
716 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
717 if(ct->range_bits >= 0) {
718 if (ct->range_bits > 16) {
Harald Welte1b38a282016-05-01 01:02:17 +0200719 int max_range_bytes = (ct->range_bits >> 3) +
720 (((ct->range_bits % 8) > 0) ? 1 : 0);
721 int length = 0, i;
Harald Welte498c9712015-08-30 16:33:07 +0200722 int64_t value = 0;
723
Harald Welte1b38a282016-05-01 01:02:17 +0200724 for (i = 1; ; i++) {
725 int upper = 1 << i;
726 if (upper >= max_range_bytes)
Harald Welte498c9712015-08-30 16:33:07 +0200727 break;
728 }
Harald Welte1b38a282016-05-01 01:02:17 +0200729 ASN_DEBUG("Can encode %d (%d bytes) in %d bits", ct->range_bits,
730 max_range_bytes, i);
731
732 if ((length = per_get_few_bits(pd, i)) < 0)
733 _ASN_DECODE_FAILED;
734
735 /* X.691 #12.2.6 length determinant + lb (1) */
736 length += 1;
737 ASN_DEBUG("Got length %d", length);
738
Harald Welte498c9712015-08-30 16:33:07 +0200739 if (aper_get_align(pd) != 0)
740 _ASN_DECODE_STARVED;
Harald Welte1b38a282016-05-01 01:02:17 +0200741
742 while (length--) {
Harald Welte498c9712015-08-30 16:33:07 +0200743 int buf = per_get_few_bits(pd, 8);
744 if (buf < 0)
745 _ASN_DECODE_STARVED;
Harald Welte1b38a282016-05-01 01:02:17 +0200746 value += (((int64_t)buf) << (8 * length));
Harald Welte498c9712015-08-30 16:33:07 +0200747 }
748
Harald Welte1b38a282016-05-01 01:02:17 +0200749 value += ct->lower_bound;
Harald Welte498c9712015-08-30 16:33:07 +0200750 if((specs && specs->field_unsigned)
751 ? asn_uint642INTEGER(st, value)
752 : asn_int642INTEGER(st, value))
753 _ASN_DECODE_FAILED;
754 ASN_DEBUG("Got value %lld + low %lld",
755 value, ct->lower_bound);
Harald Welte498c9712015-08-30 16:33:07 +0200756 } else {
757 long value = 0;
758 if (ct->range_bits < 8) {
759 value = per_get_few_bits(pd, ct->range_bits);
760 if(value < 0) _ASN_DECODE_STARVED;
761 } else if (ct->range_bits == 8) {
762 if (aper_get_align(pd) < 0)
763 _ASN_DECODE_FAILED;
764 value = per_get_few_bits(pd, ct->range_bits);
765 if(value < 0) _ASN_DECODE_STARVED;
766 } else {
767 /* Align */
768 if (aper_get_align(pd) < 0)
769 _ASN_DECODE_FAILED;
770 value = per_get_few_bits(pd, 16);
771 if(value < 0) _ASN_DECODE_STARVED;
772 }
Harald Welte1b38a282016-05-01 01:02:17 +0200773 value += ct->lower_bound;
Harald Welte498c9712015-08-30 16:33:07 +0200774 if((specs && specs->field_unsigned)
775 ? asn_ulong2INTEGER(st, value)
776 : asn_long2INTEGER(st, value))
777 _ASN_DECODE_FAILED;
778 ASN_DEBUG("Got value %ld + low %lld",
779 value, ct->lower_bound);
Harald Welte498c9712015-08-30 16:33:07 +0200780 }
781 return rval;
782 } else {
783 _ASN_DECODE_FAILED;
784 }
785 } else {
786 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
787 }
788
789 /* X.691, #12.2.3, #12.2.4 */
790 do {
791 ssize_t len;
792 void *p;
793 int ret;
794
795 /* Get the PER length */
796 len = aper_get_length(pd, -1, -1, &repeat);
797 if(len < 0) _ASN_DECODE_STARVED;
798
799 p = REALLOC(st->buf, st->size + len + 1);
800 if(!p) _ASN_DECODE_FAILED;
801 st->buf = (uint8_t *)p;
802
803 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
804 if(ret < 0) _ASN_DECODE_STARVED;
805 st->size += len;
806 } while(repeat);
807 st->buf[st->size] = 0; /* JIC */
808
809 /* #12.2.3 */
810 if(ct && ct->lower_bound) {
811 /*
812 * TODO: replace by in-place arithmetics.
813 */
814 long value;
815 if(asn_INTEGER2long(st, &value))
816 _ASN_DECODE_FAILED;
817 if(asn_long2INTEGER(st, value + ct->lower_bound))
818 _ASN_DECODE_FAILED;
819 }
820
821 return rval;
822}
823
Lev Walkin523de9e2006-08-18 01:34:18 +0000824asn_enc_rval_t
825INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
826 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000827 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000828 asn_enc_rval_t er;
829 INTEGER_t *st = (INTEGER_t *)sptr;
830 const uint8_t *buf;
831 const uint8_t *end;
832 asn_per_constraint_t *ct;
833 long value = 0;
834
835 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
836
837 if(!constraints) constraints = td->per_constraints;
838 ct = constraints ? &constraints->value : 0;
839
840 er.encoded = 0;
841
842 if(ct) {
843 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000844 if(specs && specs->field_unsigned) {
845 unsigned long uval;
846 if(asn_INTEGER2ulong(st, &uval))
847 _ASN_ENCODE_FAILED;
848 /* Check proper range */
849 if(ct->flags & APC_SEMI_CONSTRAINED) {
850 if(uval < (unsigned long)ct->lower_bound)
851 inext = 1;
852 } else if(ct->range_bits >= 0) {
853 if(uval < (unsigned long)ct->lower_bound
854 || uval > (unsigned long)ct->upper_bound)
855 inext = 1;
856 }
Harald Welte498c9712015-08-30 16:33:07 +0200857 ASN_DEBUG("Value %lu (%02x/%d) lb %llu ub %llu %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000858 uval, st->buf[0], st->size,
859 ct->lower_bound, ct->upper_bound,
860 inext ? "ext" : "fix");
861 value = uval;
862 } else {
863 if(asn_INTEGER2long(st, &value))
864 _ASN_ENCODE_FAILED;
865 /* Check proper range */
866 if(ct->flags & APC_SEMI_CONSTRAINED) {
867 if(value < ct->lower_bound)
868 inext = 1;
869 } else if(ct->range_bits >= 0) {
870 if(value < ct->lower_bound
871 || value > ct->upper_bound)
872 inext = 1;
873 }
Harald Welte498c9712015-08-30 16:33:07 +0200874 ASN_DEBUG("Value %ld (%02x/%d) lb %lld ub %lld %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000875 value, st->buf[0], st->size,
876 ct->lower_bound, ct->upper_bound,
877 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000878 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000879 if(ct->flags & APC_EXTENSIBLE) {
880 if(per_put_few_bits(po, inext, 1))
881 _ASN_ENCODE_FAILED;
882 if(inext) ct = 0;
883 } else if(inext) {
884 _ASN_ENCODE_FAILED;
885 }
886 }
887
888
Lev Walkin6c527842014-02-09 04:34:54 -0800889 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000890 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800891 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800892 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
893 value, value - ct->lower_bound, ct->range_bits);
894 unsigned long v = value - ct->lower_bound;
895 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin6c527842014-02-09 04:34:54 -0800896 _ASN_ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000897 _ASN_ENCODED_OK(er);
898 }
899
900 if(ct && ct->lower_bound) {
Harald Welte498c9712015-08-30 16:33:07 +0200901 ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound);
Lev Walkin523de9e2006-08-18 01:34:18 +0000902 /* TODO: adjust lower bound */
903 _ASN_ENCODE_FAILED;
904 }
905
906 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
907 ssize_t mayEncode = uper_put_length(po, end - buf);
908 if(mayEncode < 0)
909 _ASN_ENCODE_FAILED;
910 if(per_put_many_bits(po, buf, 8 * mayEncode))
911 _ASN_ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000912 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000913 }
914
915 _ASN_ENCODED_OK(er);
916}
917
Harald Welte498c9712015-08-30 16:33:07 +0200918asn_enc_rval_t
919INTEGER_encode_aper(asn_TYPE_descriptor_t *td,
920 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
921 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
922 asn_enc_rval_t er;
923 INTEGER_t *st = (INTEGER_t *)sptr;
924 const uint8_t *buf;
925 const uint8_t *end;
926 asn_per_constraint_t *ct;
927 int64_t value = 0;
928
929 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
930
931 if(!constraints) constraints = td->per_constraints;
932 ct = constraints ? &constraints->value : 0;
933
934 er.encoded = 0;
935
936 if(ct) {
937 int inext = 0;
938 if(specs && specs->field_unsigned) {
939 uint64_t uval;
940 if(asn_INTEGER2uint64(st, &uval))
941 _ASN_ENCODE_FAILED;
942 /* Check proper range */
943 if(ct->flags & APC_SEMI_CONSTRAINED) {
944 if(uval < (unsigned long long)ct->lower_bound)
945 inext = 1;
946 } else if(ct->range_bits >= 0) {
947 if(uval < (unsigned long long)ct->lower_bound
948 || uval > (unsigned long long)ct->upper_bound)
949 inext = 1;
950 }
951 ASN_DEBUG("Value %llu (%02x/%d) lb %llu ub %llu %s",
952 uval, st->buf[0], st->size,
953 ct->lower_bound, ct->upper_bound,
954 inext ? "ext" : "fix");
955 value = uval;
956 } else {
957 if(asn_INTEGER2int64(st, &value)) _ASN_ENCODE_FAILED;
958 /* Check proper range */
959 if(ct->flags & APC_SEMI_CONSTRAINED) {
960 if(value < ct->lower_bound)
961 inext = 1;
962 } else if(ct->range_bits >= 0) {
963 if(value < ct->lower_bound
964 || value > ct->upper_bound)
965 inext = 1;
966 }
967 ASN_DEBUG("Value %lld (%02x/%d) lb %lld ub %lld %s",
968 value, st->buf[0], st->size,
969 ct->lower_bound, ct->upper_bound,
970 inext ? "ext" : "fix");
971 }
972 if(ct->flags & APC_EXTENSIBLE) {
973 if(per_put_few_bits(po, inext, 1))
974 _ASN_ENCODE_FAILED;
975 if(inext) ct = 0;
976 } else if(inext) {
977 _ASN_ENCODE_FAILED;
978 }
979 }
980
981 /* X.691, #12.2.2 */
982 if(ct && ct->range_bits >= 0) {
983 /* #10.5.6 */
Harald Welte0ff796f2015-12-19 13:32:58 +0100984 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
985 value, value - ct->lower_bound, ct->range_bits);
986 unsigned long v = value - ct->lower_bound;
Harald Welte498c9712015-08-30 16:33:07 +0200987
988 /* #12 <= 8 -> alignment ? */
989 if (ct->range_bits < 8) {
Harald Welte0ff796f2015-12-19 13:32:58 +0100990 if(per_put_few_bits(po, 0x00 | v, ct->range_bits))
Harald Welte498c9712015-08-30 16:33:07 +0200991 _ASN_ENCODE_FAILED;
992 } else if (ct->range_bits == 8) {
993 if(aper_put_align(po) < 0)
994 _ASN_ENCODE_FAILED;
Harald Welte0ff796f2015-12-19 13:32:58 +0100995 if(per_put_few_bits(po, 0x00 | v, ct->range_bits))
Harald Welte498c9712015-08-30 16:33:07 +0200996 _ASN_ENCODE_FAILED;
997 } else if (ct->range_bits <= 16) {
998 // Consume the bytes to align on octet
999 if(aper_put_align(po) < 0)
1000 _ASN_ENCODE_FAILED;
Harald Welte0ff796f2015-12-19 13:32:58 +01001001 if(per_put_few_bits(po, 0x0000 | v,
Harald Welte498c9712015-08-30 16:33:07 +02001002 16))
1003 _ASN_ENCODE_FAILED;
1004 } else {
1005 /* TODO: extend to >64 bits */
Harald Welte0ff796f2015-12-19 13:32:58 +01001006 int64_t v64 = v;
Harald Welte50823b82016-04-30 17:31:33 +02001007 int i, j;
1008 int max_range_bytes = (ct->range_bits >> 3) +
1009 (((ct->range_bits % 8) > 0) ? 1 : 0);
Harald Welte498c9712015-08-30 16:33:07 +02001010
Harald Welte50823b82016-04-30 17:31:33 +02001011 for (i = 1; ; i++) {
1012 int upper = 1 << i;
1013 if (upper >= max_range_bytes)
1014 break;
1015 }
1016
1017 for (j = sizeof(int64_t) -1; j != 0; j--) {
1018 uint8_t val;
1019 val = v64 >> (j * 8);
1020 if (val != 0)
1021 break;
1022 }
1023
1024 /* Putting length in the minimum number of bits ex: 5 = 3bits */
1025 if (per_put_few_bits(po, j, i))
Harald Welte498c9712015-08-30 16:33:07 +02001026 _ASN_ENCODE_FAILED;
1027
1028 // Consume the bits to align on octet
1029 if (aper_put_align(po) < 0)
1030 _ASN_ENCODE_FAILED;
Harald Welte50823b82016-04-30 17:31:33 +02001031
Harald Welte498c9712015-08-30 16:33:07 +02001032 /* Put the value */
Harald Welte50823b82016-04-30 17:31:33 +02001033 for (i = 0; i <= j; i++) {
1034 if(per_put_few_bits(po, (v64 >> (8 * (j - i))) & 0xff, 8))
1035 _ASN_ENCODE_FAILED;
Harald Welte498c9712015-08-30 16:33:07 +02001036 }
1037 }
1038 _ASN_ENCODED_OK(er);
1039 }
1040
1041 if(ct && ct->lower_bound) {
1042 ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound);
1043 /* TODO: adjust lower bound */
1044 _ASN_ENCODE_FAILED;
1045 }
1046
1047 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
1048 ssize_t mayEncode = aper_put_length(po, -1, end - buf);
1049 if(mayEncode < 0)
1050 _ASN_ENCODE_FAILED;
1051 if(per_put_many_bits(po, buf, 8 * mayEncode))
1052 _ASN_ENCODE_FAILED;
1053 buf += mayEncode;
1054 }
1055
1056 _ASN_ENCODED_OK(er);
1057}
1058
Harald Welte36cd7dd2015-08-30 16:41:20 +02001059#endif /* ASN_DISABLE_PER_SUPPORT */
1060
Lev Walkinf15320b2004-06-03 03:38:44 +00001061int
Lev Walkin5e033762004-09-29 13:26:15 +00001062asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001063 uint8_t *b, *end;
1064 size_t size;
1065 long l;
1066
1067 /* Sanity checking */
1068 if(!iptr || !iptr->buf || !lptr) {
1069 errno = EINVAL;
1070 return -1;
1071 }
1072
1073 /* Cache the begin/end of the buffer */
1074 b = iptr->buf; /* Start of the INTEGER buffer */
1075 size = iptr->size;
1076 end = b + size; /* Where to stop */
1077
1078 if(size > sizeof(long)) {
1079 uint8_t *end1 = end - 1;
1080 /*
1081 * Slightly more advanced processing,
1082 * able to >sizeof(long) bytes,
1083 * when the actual value is small
1084 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1085 */
1086 /* Skip out the insignificant leading bytes */
1087 for(; b < end1; b++) {
1088 switch(*b) {
1089 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1090 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1091 }
1092 break;
1093 }
1094
1095 size = end - b;
1096 if(size > sizeof(long)) {
1097 /* Still cannot fit the long */
1098 errno = ERANGE;
1099 return -1;
1100 }
1101 }
1102
1103 /* Shortcut processing of a corner case */
1104 if(end == b) {
1105 *lptr = 0;
1106 return 0;
1107 }
1108
1109 /* Perform the sign initialization */
1110 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1111 if((*b >> 7)) l = -1; else l = 0;
1112
1113 /* Conversion engine */
1114 for(; b < end; b++)
1115 l = (l << 8) | *b;
1116
1117 *lptr = l;
1118 return 0;
1119}
Lev Walkind703ff42004-10-21 11:21:25 +00001120
1121int
Harald Welte498c9712015-08-30 16:33:07 +02001122asn_INTEGER2int64(const INTEGER_t *iptr, int64_t *lptr) {
1123 uint8_t *b, *end;
1124 size_t size;
1125 int64_t l;
1126
1127 /* Sanity checking */
1128 if(!iptr || !iptr->buf || !lptr) {
1129 errno = EINVAL;
1130 return -1;
1131 }
1132
1133 /* Cache the begin/end of the buffer */
1134 b = iptr->buf; /* Start of the INTEGER buffer */
1135 size = iptr->size;
1136 end = b + size; /* Where to stop */
1137
1138 if(size > sizeof(int64_t)) {
1139 uint8_t *end1 = end - 1;
1140 /*
1141 * Slightly more advanced processing,
1142 * able to >sizeof(int64_t) bytes,
1143 * when the actual value is small
1144 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1145 */
1146 /* Skip out the insignificant leading bytes */
1147 for(; b < end1; b++) {
1148 switch(*b) {
1149 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1150 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1151 }
1152 break;
1153 }
1154
1155 size = end - b;
1156 if(size > sizeof(int64_t)) {
1157 /* Still cannot fit the int64_t */
1158 errno = ERANGE;
1159 return -1;
1160 }
1161 }
1162
1163 /* Shortcut processing of a corner case */
1164 if(end == b) {
1165 *lptr = 0;
1166 return 0;
1167 }
1168
1169 /* Perform the sign initialization */
1170 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1171 if((*b >> 7)) l = -1; else l = 0;
1172
1173 /* Conversion engine */
1174 for(; b < end; b++)
1175 l = (l << 8) | *b;
1176
1177 *lptr = l;
1178 return 0;
1179}
1180
1181int
Lev Walkin5c879db2007-11-06 06:23:31 +00001182asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
1183 uint8_t *b, *end;
1184 unsigned long l;
1185 size_t size;
1186
1187 if(!iptr || !iptr->buf || !lptr) {
1188 errno = EINVAL;
1189 return -1;
1190 }
1191
1192 b = iptr->buf;
1193 size = iptr->size;
1194 end = b + size;
1195
1196 /* If all extra leading bytes are zeroes, ignore them */
1197 for(; size > sizeof(unsigned long); b++, size--) {
1198 if(*b) {
1199 /* Value won't fit unsigned long */
1200 errno = ERANGE;
1201 return -1;
1202 }
1203 }
1204
1205 /* Conversion engine */
1206 for(l = 0; b < end; b++)
1207 l = (l << 8) | *b;
1208
1209 *lptr = l;
1210 return 0;
1211}
1212
1213int
Harald Welte498c9712015-08-30 16:33:07 +02001214asn_INTEGER2uint64(const INTEGER_t *iptr, uint64_t *lptr) {
1215 uint8_t *b, *end;
1216 uint64_t l;
1217 size_t size;
1218
1219 if(!iptr || !iptr->buf || !lptr) {
1220 errno = EINVAL;
1221 return -1;
1222 }
1223
1224 b = iptr->buf;
1225 size = iptr->size;
1226 end = b + size;
1227
1228 /* If all extra leading bytes are zeroes, ignore them */
1229 for(; size > sizeof(uint64_t); b++, size--) {
1230 if(*b) {
1231 /* Value won't fit unsigned long */
1232 errno = ERANGE;
1233 return -1;
1234 }
1235 }
1236
1237 /* Conversion engine */
1238 for(l = 0; b < end; b++)
1239 l = (l << 8) | *b;
1240
1241 *lptr = l;
1242 return 0;
1243}
1244
1245int
Lev Walkin5c879db2007-11-06 06:23:31 +00001246asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1247 uint8_t *buf;
1248 uint8_t *end;
1249 uint8_t *b;
1250 int shr;
1251
1252 if(value <= LONG_MAX)
1253 return asn_long2INTEGER(st, value);
1254
1255 buf = (uint8_t *)MALLOC(1 + sizeof(value));
1256 if(!buf) return -1;
1257
1258 end = buf + (sizeof(value) + 1);
1259 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +00001260 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
1261 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +00001262
1263 if(st->buf) FREEMEM(st->buf);
1264 st->buf = buf;
1265 st->size = 1 + sizeof(value);
1266
1267 return 0;
1268}
1269
1270int
Harald Welte498c9712015-08-30 16:33:07 +02001271asn_uint642INTEGER(INTEGER_t *st, uint64_t value) {
1272 uint8_t *buf;
1273 uint8_t *end;
1274 uint8_t *b;
1275 int shr;
1276
1277 if(value <= INT64_MAX)
1278 return asn_int642INTEGER(st, value);
1279
1280 buf = (uint8_t *)MALLOC(1 + sizeof(value));
1281 if(!buf) return -1;
1282
1283 end = buf + (sizeof(value) + 1);
1284 buf[0] = 0;
1285 for(b = buf + 1, shr = (sizeof(value)-1)*8; b < end; shr -= 8, b++)
1286 *b = (uint8_t)(value >> shr);
1287
1288 if(st->buf) FREEMEM(st->buf);
1289 st->buf = buf;
1290 st->size = 1 + sizeof(value);
1291
1292 return 0;
1293}
1294
1295int
1296asn_int642INTEGER(INTEGER_t *st, int64_t value) {
1297 uint8_t *buf, *bp;
1298 uint8_t *p;
1299 uint8_t *pstart;
1300 uint8_t *pend1;
1301 int littleEndian = 1; /* Run-time detection */
1302 int add;
1303
1304 if(!st) {
1305 errno = EINVAL;
1306 return -1;
1307 }
1308
1309 buf = (uint8_t *)MALLOC(sizeof(value));
1310 if(!buf) return -1;
1311
1312 if(*(char *)&littleEndian) {
1313 pstart = (uint8_t *)&value + sizeof(value) - 1;
1314 pend1 = (uint8_t *)&value;
1315 add = -1;
1316 } else {
1317 pstart = (uint8_t *)&value;
1318 pend1 = pstart + sizeof(value) - 1;
1319 add = 1;
1320 }
1321
1322 /*
1323 * If the contents octet consists of more than one octet,
1324 * then bits of the first octet and bit 8 of the second octet:
1325 * a) shall not all be ones; and
1326 * b) shall not all be zero.
1327 */
1328 for(p = pstart; p != pend1; p += add) {
1329 switch(*p) {
1330 case 0x00: if((*(p+add) & 0x80) == 0)
1331 continue;
1332 break;
1333 case 0xff: if((*(p+add) & 0x80))
1334 continue;
1335 break;
1336 }
1337 break;
1338 }
1339 /* Copy the integer body */
1340 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1341 *bp++ = *p;
1342
1343 if(st->buf) FREEMEM(st->buf);
1344 st->buf = buf;
1345 st->size = bp - buf;
1346
1347 return 0;
1348}
1349
1350int
Lev Walkind703ff42004-10-21 11:21:25 +00001351asn_long2INTEGER(INTEGER_t *st, long value) {
1352 uint8_t *buf, *bp;
1353 uint8_t *p;
1354 uint8_t *pstart;
1355 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +00001356 int littleEndian = 1; /* Run-time detection */
1357 int add;
Lev Walkind703ff42004-10-21 11:21:25 +00001358
1359 if(!st) {
1360 errno = EINVAL;
1361 return -1;
1362 }
1363
Lev Walkin8471cec2004-10-21 14:02:19 +00001364 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +00001365 if(!buf) return -1;
1366
Lev Walkind7ad5612004-10-26 08:20:46 +00001367 if(*(char *)&littleEndian) {
1368 pstart = (uint8_t *)&value + sizeof(value) - 1;
1369 pend1 = (uint8_t *)&value;
1370 add = -1;
1371 } else {
1372 pstart = (uint8_t *)&value;
1373 pend1 = pstart + sizeof(value) - 1;
1374 add = 1;
1375 }
1376
Lev Walkind703ff42004-10-21 11:21:25 +00001377 /*
1378 * If the contents octet consists of more than one octet,
1379 * then bits of the first octet and bit 8 of the second octet:
1380 * a) shall not all be ones; and
1381 * b) shall not all be zero.
1382 */
Lev Walkin33700162004-10-26 09:03:31 +00001383 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +00001384 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +00001385 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +00001386 continue;
1387 break;
Lev Walkin33700162004-10-26 09:03:31 +00001388 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +00001389 continue;
1390 break;
1391 }
1392 break;
1393 }
1394 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +00001395 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1396 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +00001397
1398 if(st->buf) FREEMEM(st->buf);
1399 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +00001400 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +00001401
1402 return 0;
1403}
Lev Walkinb3751942012-09-02 19:36:47 -07001404
Lev Walkincad560a2013-03-16 07:00:58 -07001405/*
1406 * This function is going to be DEPRECATED soon.
1407 */
Lev Walkine09f9f12012-09-02 23:06:35 -07001408enum asn_strtol_result_e
1409asn_strtol(const char *str, const char *end, long *lp) {
Lev Walkincad560a2013-03-16 07:00:58 -07001410 const char *endp = end;
1411
1412 switch(asn_strtol_lim(str, &endp, lp)) {
1413 case ASN_STRTOL_ERROR_RANGE:
1414 return ASN_STRTOL_ERROR_RANGE;
1415 case ASN_STRTOL_ERROR_INVAL:
1416 return ASN_STRTOL_ERROR_INVAL;
1417 case ASN_STRTOL_EXPECT_MORE:
1418 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1419 case ASN_STRTOL_OK:
1420 return ASN_STRTOL_OK;
1421 case ASN_STRTOL_EXTRA_DATA:
1422 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1423 }
1424
1425 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1426}
1427
1428/*
1429 * Parse the number in the given string until the given *end position,
1430 * returning the position after the last parsed character back using the
1431 * same (*end) pointer.
1432 * WARNING: This behavior is different from the standard strtol(3).
1433 */
1434enum asn_strtol_result_e
1435asn_strtol_lim(const char *str, const char **end, long *lp) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001436 int sign = 1;
1437 long l;
Lev Walkinb3751942012-09-02 19:36:47 -07001438
Lev Walkine09f9f12012-09-02 23:06:35 -07001439 const long upper_boundary = LONG_MAX / 10;
1440 long last_digit_max = LONG_MAX % 10;
1441
Lev Walkincad560a2013-03-16 07:00:58 -07001442 if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001443
1444 switch(*str) {
1445 case '-':
1446 last_digit_max++;
1447 sign = -1;
1448 case '+':
1449 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001450 if(str >= *end) {
1451 *end = str;
1452 return ASN_STRTOL_EXPECT_MORE;
1453 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001454 }
1455
Lev Walkincad560a2013-03-16 07:00:58 -07001456 for(l = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001457 switch(*str) {
1458 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1459 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1460 int d = *str - '0';
1461 if(l < upper_boundary) {
1462 l = l * 10 + d;
1463 } else if(l == upper_boundary) {
1464 if(d <= last_digit_max) {
1465 if(sign > 0) {
1466 l = l * 10 + d;
1467 } else {
1468 sign = 1;
1469 l = -l * 10 - d;
1470 }
1471 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001472 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001473 return ASN_STRTOL_ERROR_RANGE;
1474 }
1475 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001476 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001477 return ASN_STRTOL_ERROR_RANGE;
1478 }
1479 }
1480 continue;
1481 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001482 *end = str;
1483 *lp = sign * l;
1484 return ASN_STRTOL_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001485 }
1486 }
1487
Lev Walkincad560a2013-03-16 07:00:58 -07001488 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001489 *lp = sign * l;
1490 return ASN_STRTOL_OK;
Lev Walkinb3751942012-09-02 19:36:47 -07001491}
1492