blob: aa02858534b7361666e6c04e188ea236bd1f9d86 [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 Welteb3a23042015-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 Welte0b57b082015-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 Welte0b57b082015-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 Walkin7c1dc052016-03-14 03:08:15 -0700551 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000552
Lev Walkine0b56e02005-02-25 12:10:27 +0000553 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700554 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000555
Lev Walkin7c1dc052016-03-14 03:08:15 -0700556 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000557}
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)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700574 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000575 }
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 Walkin7c1dc052016-03-14 03:08:15 -0700582 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);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700592 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000593 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);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700597 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000598 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 Walkin7c1dc052016-03-14 03:08:15 -0700608 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))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700614 ASN__DECODE_STARVED;
Harald Welte0b57b082015-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))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700619 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800620 } else {
621 unsigned long svalue;
622 if(uper_get_constrained_whole_number(pd,
623 &svalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700624 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800625 ASN_DEBUG("Got value %ld + low %ld",
626 svalue, ct->lower_bound);
627 svalue += ct->lower_bound;
628 if(asn_long2INTEGER(st, svalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700629 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800630 }
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 Walkin7c1dc052016-03-14 03:08:15 -0700645 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000646
647 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700648 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000649 st->buf = (uint8_t *)p;
650
651 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700652 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))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700664 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000665 if(asn_long2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700666 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000667 }
668
669 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000670}
671
Harald Welte0b57b082015-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) {
719 int max_range_bytes = (ct->range_bits >> 3) + 1;
720 int length, i;
721 int64_t value = 0;
722
723 for (i = 0; i < max_range_bytes; i++) {
724 int upper = 1 << (i + 1);
725 if (upper > max_range_bytes)
726 break;
727 }
728 if ((length = per_get_few_bits(pd, i + 1)) < 0)
729 _ASN_DECODE_STARVED;
730 if (aper_get_align(pd) != 0)
731 _ASN_DECODE_STARVED;
732 ASN_DEBUG("Got length %d", length + 1);
733 for (i = 0; i < length + 1; i++) {
734 int buf = per_get_few_bits(pd, 8);
735 if (buf < 0)
736 _ASN_DECODE_STARVED;
737 value += (((int64_t)buf) << (8 * i));
738 }
739
740 if((specs && specs->field_unsigned)
741 ? asn_uint642INTEGER(st, value)
742 : asn_int642INTEGER(st, value))
743 _ASN_DECODE_FAILED;
744 ASN_DEBUG("Got value %lld + low %lld",
745 value, ct->lower_bound);
746 value += ct->lower_bound;
747 } else {
748 long value = 0;
749 if (ct->range_bits < 8) {
750 value = per_get_few_bits(pd, ct->range_bits);
751 if(value < 0) _ASN_DECODE_STARVED;
752 } else if (ct->range_bits == 8) {
753 if (aper_get_align(pd) < 0)
754 _ASN_DECODE_FAILED;
755 value = per_get_few_bits(pd, ct->range_bits);
756 if(value < 0) _ASN_DECODE_STARVED;
757 } else {
758 /* Align */
759 if (aper_get_align(pd) < 0)
760 _ASN_DECODE_FAILED;
761 value = per_get_few_bits(pd, 16);
762 if(value < 0) _ASN_DECODE_STARVED;
763 }
764 if((specs && specs->field_unsigned)
765 ? asn_ulong2INTEGER(st, value)
766 : asn_long2INTEGER(st, value))
767 _ASN_DECODE_FAILED;
768 ASN_DEBUG("Got value %ld + low %lld",
769 value, ct->lower_bound);
770 value += ct->lower_bound;
771 }
772 return rval;
773 } else {
774 _ASN_DECODE_FAILED;
775 }
776 } else {
777 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
778 }
779
780 /* X.691, #12.2.3, #12.2.4 */
781 do {
782 ssize_t len;
783 void *p;
784 int ret;
785
786 /* Get the PER length */
787 len = aper_get_length(pd, -1, -1, &repeat);
788 if(len < 0) _ASN_DECODE_STARVED;
789
790 p = REALLOC(st->buf, st->size + len + 1);
791 if(!p) _ASN_DECODE_FAILED;
792 st->buf = (uint8_t *)p;
793
794 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
795 if(ret < 0) _ASN_DECODE_STARVED;
796 st->size += len;
797 } while(repeat);
798 st->buf[st->size] = 0; /* JIC */
799
800 /* #12.2.3 */
801 if(ct && ct->lower_bound) {
802 /*
803 * TODO: replace by in-place arithmetics.
804 */
805 long value;
806 if(asn_INTEGER2long(st, &value))
807 _ASN_DECODE_FAILED;
808 if(asn_long2INTEGER(st, value + ct->lower_bound))
809 _ASN_DECODE_FAILED;
810 }
811
812 return rval;
813}
814
Lev Walkin523de9e2006-08-18 01:34:18 +0000815asn_enc_rval_t
816INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
817 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000818 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000819 asn_enc_rval_t er;
820 INTEGER_t *st = (INTEGER_t *)sptr;
821 const uint8_t *buf;
822 const uint8_t *end;
823 asn_per_constraint_t *ct;
824 long value = 0;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100825 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000826
Lev Walkin7c1dc052016-03-14 03:08:15 -0700827 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000828
829 if(!constraints) constraints = td->per_constraints;
830 ct = constraints ? &constraints->value : 0;
831
832 er.encoded = 0;
833
834 if(ct) {
835 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000836 if(specs && specs->field_unsigned) {
837 unsigned long uval;
838 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700839 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000840 /* Check proper range */
841 if(ct->flags & APC_SEMI_CONSTRAINED) {
842 if(uval < (unsigned long)ct->lower_bound)
843 inext = 1;
844 } else if(ct->range_bits >= 0) {
845 if(uval < (unsigned long)ct->lower_bound
846 || uval > (unsigned long)ct->upper_bound)
847 inext = 1;
848 }
Harald Welte0b57b082015-08-30 16:33:07 +0200849 ASN_DEBUG("Value %lu (%02x/%d) lb %llu ub %llu %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000850 uval, st->buf[0], st->size,
851 ct->lower_bound, ct->upper_bound,
852 inext ? "ext" : "fix");
853 value = uval;
854 } else {
855 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700856 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000857 /* Check proper range */
858 if(ct->flags & APC_SEMI_CONSTRAINED) {
859 if(value < ct->lower_bound)
860 inext = 1;
861 } else if(ct->range_bits >= 0) {
862 if(value < ct->lower_bound
863 || value > ct->upper_bound)
864 inext = 1;
865 }
Harald Welte0b57b082015-08-30 16:33:07 +0200866 ASN_DEBUG("Value %ld (%02x/%d) lb %lld ub %lld %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000867 value, st->buf[0], st->size,
868 ct->lower_bound, ct->upper_bound,
869 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000870 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000871 if(ct->flags & APC_EXTENSIBLE) {
872 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700873 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000874 if(inext) ct = 0;
875 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700876 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000877 }
878 }
879
880
Lev Walkin6c527842014-02-09 04:34:54 -0800881 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000882 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800883 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800884 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
885 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100886 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800887 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700888 ASN__ENCODE_FAILED;
889 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000890 }
891
892 if(ct && ct->lower_bound) {
Harald Welte0b57b082015-08-30 16:33:07 +0200893 ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound);
Lev Walkin523de9e2006-08-18 01:34:18 +0000894 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700895 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000896 }
897
898 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
899 ssize_t mayEncode = uper_put_length(po, end - buf);
900 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700901 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000902 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700903 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000904 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000905 }
906
Lev Walkin7c1dc052016-03-14 03:08:15 -0700907 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000908}
909
Harald Welte0b57b082015-08-30 16:33:07 +0200910asn_enc_rval_t
911INTEGER_encode_aper(asn_TYPE_descriptor_t *td,
912 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
913 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
914 asn_enc_rval_t er;
915 INTEGER_t *st = (INTEGER_t *)sptr;
916 const uint8_t *buf;
917 const uint8_t *end;
918 asn_per_constraint_t *ct;
919 int64_t value = 0;
920
921 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
922
923 if(!constraints) constraints = td->per_constraints;
924 ct = constraints ? &constraints->value : 0;
925
926 er.encoded = 0;
927
928 if(ct) {
929 int inext = 0;
930 if(specs && specs->field_unsigned) {
931 uint64_t uval;
932 if(asn_INTEGER2uint64(st, &uval))
933 _ASN_ENCODE_FAILED;
934 /* Check proper range */
935 if(ct->flags & APC_SEMI_CONSTRAINED) {
936 if(uval < (unsigned long long)ct->lower_bound)
937 inext = 1;
938 } else if(ct->range_bits >= 0) {
939 if(uval < (unsigned long long)ct->lower_bound
940 || uval > (unsigned long long)ct->upper_bound)
941 inext = 1;
942 }
943 ASN_DEBUG("Value %llu (%02x/%d) lb %llu ub %llu %s",
944 uval, st->buf[0], st->size,
945 ct->lower_bound, ct->upper_bound,
946 inext ? "ext" : "fix");
947 value = uval;
948 } else {
949 if(asn_INTEGER2int64(st, &value)) _ASN_ENCODE_FAILED;
950 /* Check proper range */
951 if(ct->flags & APC_SEMI_CONSTRAINED) {
952 if(value < ct->lower_bound)
953 inext = 1;
954 } else if(ct->range_bits >= 0) {
955 if(value < ct->lower_bound
956 || value > ct->upper_bound)
957 inext = 1;
958 }
959 ASN_DEBUG("Value %lld (%02x/%d) lb %lld ub %lld %s",
960 value, st->buf[0], st->size,
961 ct->lower_bound, ct->upper_bound,
962 inext ? "ext" : "fix");
963 }
964 if(ct->flags & APC_EXTENSIBLE) {
965 if(per_put_few_bits(po, inext, 1))
966 _ASN_ENCODE_FAILED;
967 if(inext) ct = 0;
968 } else if(inext) {
969 _ASN_ENCODE_FAILED;
970 }
971 }
972
973 /* X.691, #12.2.2 */
974 if(ct && ct->range_bits >= 0) {
975 /* #10.5.6 */
Harald Welte2b3068f2015-12-19 13:32:58 +0100976 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
977 value, value - ct->lower_bound, ct->range_bits);
978 unsigned long v = value - ct->lower_bound;
Harald Welte0b57b082015-08-30 16:33:07 +0200979
980 /* #12 <= 8 -> alignment ? */
981 if (ct->range_bits < 8) {
Harald Welte2b3068f2015-12-19 13:32:58 +0100982 if(per_put_few_bits(po, 0x00 | v, ct->range_bits))
Harald Welte0b57b082015-08-30 16:33:07 +0200983 _ASN_ENCODE_FAILED;
984 } else if (ct->range_bits == 8) {
985 if(aper_put_align(po) < 0)
986 _ASN_ENCODE_FAILED;
Harald Welte2b3068f2015-12-19 13:32:58 +0100987 if(per_put_few_bits(po, 0x00 | v, ct->range_bits))
Harald Welte0b57b082015-08-30 16:33:07 +0200988 _ASN_ENCODE_FAILED;
989 } else if (ct->range_bits <= 16) {
990 // Consume the bytes to align on octet
991 if(aper_put_align(po) < 0)
992 _ASN_ENCODE_FAILED;
Harald Welte2b3068f2015-12-19 13:32:58 +0100993 if(per_put_few_bits(po, 0x0000 | v,
Harald Welte0b57b082015-08-30 16:33:07 +0200994 16))
995 _ASN_ENCODE_FAILED;
996 } else {
997 /* TODO: extend to >64 bits */
Harald Welte2b3068f2015-12-19 13:32:58 +0100998 int64_t v64 = v;
Harald Welte0b57b082015-08-30 16:33:07 +0200999 int i;
1000
1001 /* Putting length - 1 in the minimum number of bits ex: 5 = 3bits */
1002 if (per_put_few_bits(po, st->size - 1, (ct->range_bits >> 3)-1))
1003 _ASN_ENCODE_FAILED;
1004
1005 // Consume the bits to align on octet
1006 if (aper_put_align(po) < 0)
1007 _ASN_ENCODE_FAILED;
1008 /* Put the value */
1009 for (i = 0; i < st->size; i++) {
Harald Welte2b3068f2015-12-19 13:32:58 +01001010 if(per_put_few_bits(po, (v64 >> (8 * (st->size - i - 1))) & 0xff, 8)) _ASN_ENCODE_FAILED;
Harald Welte0b57b082015-08-30 16:33:07 +02001011 }
1012 }
1013 _ASN_ENCODED_OK(er);
1014 }
1015
1016 if(ct && ct->lower_bound) {
1017 ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound);
1018 /* TODO: adjust lower bound */
1019 _ASN_ENCODE_FAILED;
1020 }
1021
1022 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
1023 ssize_t mayEncode = aper_put_length(po, -1, end - buf);
1024 if(mayEncode < 0)
1025 _ASN_ENCODE_FAILED;
1026 if(per_put_many_bits(po, buf, 8 * mayEncode))
1027 _ASN_ENCODE_FAILED;
1028 buf += mayEncode;
1029 }
1030
1031 _ASN_ENCODED_OK(er);
1032}
1033
Harald Welteb3a23042015-08-30 16:41:20 +02001034#endif /* ASN_DISABLE_PER_SUPPORT */
1035
Lev Walkinf15320b2004-06-03 03:38:44 +00001036int
Lev Walkin5e033762004-09-29 13:26:15 +00001037asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001038 uint8_t *b, *end;
1039 size_t size;
1040 long l;
1041
1042 /* Sanity checking */
1043 if(!iptr || !iptr->buf || !lptr) {
1044 errno = EINVAL;
1045 return -1;
1046 }
1047
1048 /* Cache the begin/end of the buffer */
1049 b = iptr->buf; /* Start of the INTEGER buffer */
1050 size = iptr->size;
1051 end = b + size; /* Where to stop */
1052
1053 if(size > sizeof(long)) {
1054 uint8_t *end1 = end - 1;
1055 /*
1056 * Slightly more advanced processing,
1057 * able to >sizeof(long) bytes,
1058 * when the actual value is small
1059 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1060 */
1061 /* Skip out the insignificant leading bytes */
1062 for(; b < end1; b++) {
1063 switch(*b) {
1064 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1065 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1066 }
1067 break;
1068 }
1069
1070 size = end - b;
1071 if(size > sizeof(long)) {
1072 /* Still cannot fit the long */
1073 errno = ERANGE;
1074 return -1;
1075 }
1076 }
1077
1078 /* Shortcut processing of a corner case */
1079 if(end == b) {
1080 *lptr = 0;
1081 return 0;
1082 }
1083
1084 /* Perform the sign initialization */
1085 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1086 if((*b >> 7)) l = -1; else l = 0;
1087
1088 /* Conversion engine */
1089 for(; b < end; b++)
1090 l = (l << 8) | *b;
1091
1092 *lptr = l;
1093 return 0;
1094}
Lev Walkind703ff42004-10-21 11:21:25 +00001095
1096int
Harald Welte0b57b082015-08-30 16:33:07 +02001097asn_INTEGER2int64(const INTEGER_t *iptr, int64_t *lptr) {
1098 uint8_t *b, *end;
1099 size_t size;
1100 int64_t l;
1101
1102 /* Sanity checking */
1103 if(!iptr || !iptr->buf || !lptr) {
1104 errno = EINVAL;
1105 return -1;
1106 }
1107
1108 /* Cache the begin/end of the buffer */
1109 b = iptr->buf; /* Start of the INTEGER buffer */
1110 size = iptr->size;
1111 end = b + size; /* Where to stop */
1112
1113 if(size > sizeof(int64_t)) {
1114 uint8_t *end1 = end - 1;
1115 /*
1116 * Slightly more advanced processing,
1117 * able to >sizeof(int64_t) bytes,
1118 * when the actual value is small
1119 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1120 */
1121 /* Skip out the insignificant leading bytes */
1122 for(; b < end1; b++) {
1123 switch(*b) {
1124 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1125 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1126 }
1127 break;
1128 }
1129
1130 size = end - b;
1131 if(size > sizeof(int64_t)) {
1132 /* Still cannot fit the int64_t */
1133 errno = ERANGE;
1134 return -1;
1135 }
1136 }
1137
1138 /* Shortcut processing of a corner case */
1139 if(end == b) {
1140 *lptr = 0;
1141 return 0;
1142 }
1143
1144 /* Perform the sign initialization */
1145 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1146 if((*b >> 7)) l = -1; else l = 0;
1147
1148 /* Conversion engine */
1149 for(; b < end; b++)
1150 l = (l << 8) | *b;
1151
1152 *lptr = l;
1153 return 0;
1154}
1155
1156int
Lev Walkin5c879db2007-11-06 06:23:31 +00001157asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
1158 uint8_t *b, *end;
1159 unsigned long l;
1160 size_t size;
1161
1162 if(!iptr || !iptr->buf || !lptr) {
1163 errno = EINVAL;
1164 return -1;
1165 }
1166
1167 b = iptr->buf;
1168 size = iptr->size;
1169 end = b + size;
1170
1171 /* If all extra leading bytes are zeroes, ignore them */
1172 for(; size > sizeof(unsigned long); b++, size--) {
1173 if(*b) {
1174 /* Value won't fit unsigned long */
1175 errno = ERANGE;
1176 return -1;
1177 }
1178 }
1179
1180 /* Conversion engine */
1181 for(l = 0; b < end; b++)
1182 l = (l << 8) | *b;
1183
1184 *lptr = l;
1185 return 0;
1186}
1187
1188int
Harald Welte0b57b082015-08-30 16:33:07 +02001189asn_INTEGER2uint64(const INTEGER_t *iptr, uint64_t *lptr) {
1190 uint8_t *b, *end;
1191 uint64_t l;
1192 size_t size;
1193
1194 if(!iptr || !iptr->buf || !lptr) {
1195 errno = EINVAL;
1196 return -1;
1197 }
1198
1199 b = iptr->buf;
1200 size = iptr->size;
1201 end = b + size;
1202
1203 /* If all extra leading bytes are zeroes, ignore them */
1204 for(; size > sizeof(uint64_t); b++, size--) {
1205 if(*b) {
1206 /* Value won't fit unsigned long */
1207 errno = ERANGE;
1208 return -1;
1209 }
1210 }
1211
1212 /* Conversion engine */
1213 for(l = 0; b < end; b++)
1214 l = (l << 8) | *b;
1215
1216 *lptr = l;
1217 return 0;
1218}
1219
1220int
Lev Walkin5c879db2007-11-06 06:23:31 +00001221asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1222 uint8_t *buf;
1223 uint8_t *end;
1224 uint8_t *b;
1225 int shr;
1226
1227 if(value <= LONG_MAX)
1228 return asn_long2INTEGER(st, value);
1229
1230 buf = (uint8_t *)MALLOC(1 + sizeof(value));
1231 if(!buf) return -1;
1232
1233 end = buf + (sizeof(value) + 1);
1234 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +00001235 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
1236 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +00001237
1238 if(st->buf) FREEMEM(st->buf);
1239 st->buf = buf;
1240 st->size = 1 + sizeof(value);
1241
1242 return 0;
1243}
1244
1245int
Harald Welte0b57b082015-08-30 16:33:07 +02001246asn_uint642INTEGER(INTEGER_t *st, uint64_t value) {
1247 uint8_t *buf;
1248 uint8_t *end;
1249 uint8_t *b;
1250 int shr;
1251
1252 if(value <= INT64_MAX)
1253 return asn_int642INTEGER(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;
1260 for(b = buf + 1, shr = (sizeof(value)-1)*8; b < end; shr -= 8, b++)
1261 *b = (uint8_t)(value >> shr);
1262
1263 if(st->buf) FREEMEM(st->buf);
1264 st->buf = buf;
1265 st->size = 1 + sizeof(value);
1266
1267 return 0;
1268}
1269
1270int
1271asn_int642INTEGER(INTEGER_t *st, int64_t value) {
1272 uint8_t *buf, *bp;
1273 uint8_t *p;
1274 uint8_t *pstart;
1275 uint8_t *pend1;
1276 int littleEndian = 1; /* Run-time detection */
1277 int add;
1278
1279 if(!st) {
1280 errno = EINVAL;
1281 return -1;
1282 }
1283
1284 buf = (uint8_t *)MALLOC(sizeof(value));
1285 if(!buf) return -1;
1286
1287 if(*(char *)&littleEndian) {
1288 pstart = (uint8_t *)&value + sizeof(value) - 1;
1289 pend1 = (uint8_t *)&value;
1290 add = -1;
1291 } else {
1292 pstart = (uint8_t *)&value;
1293 pend1 = pstart + sizeof(value) - 1;
1294 add = 1;
1295 }
1296
1297 /*
1298 * If the contents octet consists of more than one octet,
1299 * then bits of the first octet and bit 8 of the second octet:
1300 * a) shall not all be ones; and
1301 * b) shall not all be zero.
1302 */
1303 for(p = pstart; p != pend1; p += add) {
1304 switch(*p) {
1305 case 0x00: if((*(p+add) & 0x80) == 0)
1306 continue;
1307 break;
1308 case 0xff: if((*(p+add) & 0x80))
1309 continue;
1310 break;
1311 }
1312 break;
1313 }
1314 /* Copy the integer body */
1315 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1316 *bp++ = *p;
1317
1318 if(st->buf) FREEMEM(st->buf);
1319 st->buf = buf;
1320 st->size = bp - buf;
1321
1322 return 0;
1323}
1324
1325int
Lev Walkind703ff42004-10-21 11:21:25 +00001326asn_long2INTEGER(INTEGER_t *st, long value) {
1327 uint8_t *buf, *bp;
1328 uint8_t *p;
1329 uint8_t *pstart;
1330 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +00001331 int littleEndian = 1; /* Run-time detection */
1332 int add;
Lev Walkind703ff42004-10-21 11:21:25 +00001333
1334 if(!st) {
1335 errno = EINVAL;
1336 return -1;
1337 }
1338
Lev Walkin8471cec2004-10-21 14:02:19 +00001339 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +00001340 if(!buf) return -1;
1341
Lev Walkind7ad5612004-10-26 08:20:46 +00001342 if(*(char *)&littleEndian) {
1343 pstart = (uint8_t *)&value + sizeof(value) - 1;
1344 pend1 = (uint8_t *)&value;
1345 add = -1;
1346 } else {
1347 pstart = (uint8_t *)&value;
1348 pend1 = pstart + sizeof(value) - 1;
1349 add = 1;
1350 }
1351
Lev Walkind703ff42004-10-21 11:21:25 +00001352 /*
1353 * If the contents octet consists of more than one octet,
1354 * then bits of the first octet and bit 8 of the second octet:
1355 * a) shall not all be ones; and
1356 * b) shall not all be zero.
1357 */
Lev Walkin33700162004-10-26 09:03:31 +00001358 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +00001359 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +00001360 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +00001361 continue;
1362 break;
Lev Walkin33700162004-10-26 09:03:31 +00001363 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +00001364 continue;
1365 break;
1366 }
1367 break;
1368 }
1369 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +00001370 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1371 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +00001372
1373 if(st->buf) FREEMEM(st->buf);
1374 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +00001375 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +00001376
1377 return 0;
1378}
Lev Walkinb3751942012-09-02 19:36:47 -07001379
Lev Walkincad560a2013-03-16 07:00:58 -07001380/*
1381 * This function is going to be DEPRECATED soon.
1382 */
Lev Walkine09f9f12012-09-02 23:06:35 -07001383enum asn_strtol_result_e
1384asn_strtol(const char *str, const char *end, long *lp) {
Lev Walkincad560a2013-03-16 07:00:58 -07001385 const char *endp = end;
1386
1387 switch(asn_strtol_lim(str, &endp, lp)) {
1388 case ASN_STRTOL_ERROR_RANGE:
1389 return ASN_STRTOL_ERROR_RANGE;
1390 case ASN_STRTOL_ERROR_INVAL:
1391 return ASN_STRTOL_ERROR_INVAL;
1392 case ASN_STRTOL_EXPECT_MORE:
1393 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1394 case ASN_STRTOL_OK:
1395 return ASN_STRTOL_OK;
1396 case ASN_STRTOL_EXTRA_DATA:
1397 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1398 }
1399
1400 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1401}
1402
1403/*
1404 * Parse the number in the given string until the given *end position,
1405 * returning the position after the last parsed character back using the
1406 * same (*end) pointer.
1407 * WARNING: This behavior is different from the standard strtol(3).
1408 */
1409enum asn_strtol_result_e
1410asn_strtol_lim(const char *str, const char **end, long *lp) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001411 int sign = 1;
1412 long l;
Lev Walkinb3751942012-09-02 19:36:47 -07001413
Lev Walkine09f9f12012-09-02 23:06:35 -07001414 const long upper_boundary = LONG_MAX / 10;
1415 long last_digit_max = LONG_MAX % 10;
1416
Lev Walkincad560a2013-03-16 07:00:58 -07001417 if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001418
1419 switch(*str) {
1420 case '-':
1421 last_digit_max++;
1422 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001423 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001424 case '+':
1425 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001426 if(str >= *end) {
1427 *end = str;
1428 return ASN_STRTOL_EXPECT_MORE;
1429 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001430 }
1431
Lev Walkincad560a2013-03-16 07:00:58 -07001432 for(l = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001433 switch(*str) {
1434 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1435 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1436 int d = *str - '0';
1437 if(l < upper_boundary) {
1438 l = l * 10 + d;
1439 } else if(l == upper_boundary) {
1440 if(d <= last_digit_max) {
1441 if(sign > 0) {
1442 l = l * 10 + d;
1443 } else {
1444 sign = 1;
1445 l = -l * 10 - d;
1446 }
1447 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001448 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001449 return ASN_STRTOL_ERROR_RANGE;
1450 }
1451 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001452 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001453 return ASN_STRTOL_ERROR_RANGE;
1454 }
1455 }
1456 continue;
1457 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001458 *end = str;
1459 *lp = sign * l;
1460 return ASN_STRTOL_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001461 }
1462 }
1463
Lev Walkincad560a2013-03-16 07:00:58 -07001464 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001465 *lp = sign * l;
1466 return ASN_STRTOL_OK;
Lev Walkinb3751942012-09-02 19:36:47 -07001467}
1468