blob: af2534189cd68a69ac60c82d0318b5cc85ab2894 [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) {
Harald Welteb9b7c9e2016-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 Welte0b57b082015-08-30 16:33:07 +0200722 int64_t value = 0;
723
Harald Welteb9b7c9e2016-05-01 01:02:17 +0200724 for (i = 1; ; i++) {
725 int upper = 1 << i;
726 if (upper >= max_range_bytes)
Harald Welte0b57b082015-08-30 16:33:07 +0200727 break;
728 }
Harald Welteb9b7c9e2016-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 Welte0b57b082015-08-30 16:33:07 +0200739 if (aper_get_align(pd) != 0)
740 _ASN_DECODE_STARVED;
Harald Welteb9b7c9e2016-05-01 01:02:17 +0200741
742 while (length--) {
Harald Welte0b57b082015-08-30 16:33:07 +0200743 int buf = per_get_few_bits(pd, 8);
744 if (buf < 0)
745 _ASN_DECODE_STARVED;
Harald Welteb9b7c9e2016-05-01 01:02:17 +0200746 value += (((int64_t)buf) << (8 * length));
Harald Welte0b57b082015-08-30 16:33:07 +0200747 }
748
Harald Welteb9b7c9e2016-05-01 01:02:17 +0200749 value += ct->lower_bound;
Harald Welte0b57b082015-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 Welte0b57b082015-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 Welteb9b7c9e2016-05-01 01:02:17 +0200773 value += ct->lower_bound;
Harald Welte0b57b082015-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 Welte0b57b082015-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;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100834 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000835
Lev Walkin7c1dc052016-03-14 03:08:15 -0700836 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000837
838 if(!constraints) constraints = td->per_constraints;
839 ct = constraints ? &constraints->value : 0;
840
841 er.encoded = 0;
842
843 if(ct) {
844 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000845 if(specs && specs->field_unsigned) {
846 unsigned long uval;
847 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700848 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000849 /* Check proper range */
850 if(ct->flags & APC_SEMI_CONSTRAINED) {
851 if(uval < (unsigned long)ct->lower_bound)
852 inext = 1;
853 } else if(ct->range_bits >= 0) {
854 if(uval < (unsigned long)ct->lower_bound
855 || uval > (unsigned long)ct->upper_bound)
856 inext = 1;
857 }
Harald Welte0b57b082015-08-30 16:33:07 +0200858 ASN_DEBUG("Value %lu (%02x/%d) lb %llu ub %llu %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000859 uval, st->buf[0], st->size,
860 ct->lower_bound, ct->upper_bound,
861 inext ? "ext" : "fix");
862 value = uval;
863 } else {
864 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700865 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000866 /* Check proper range */
867 if(ct->flags & APC_SEMI_CONSTRAINED) {
868 if(value < ct->lower_bound)
869 inext = 1;
870 } else if(ct->range_bits >= 0) {
871 if(value < ct->lower_bound
872 || value > ct->upper_bound)
873 inext = 1;
874 }
Harald Welte0b57b082015-08-30 16:33:07 +0200875 ASN_DEBUG("Value %ld (%02x/%d) lb %lld ub %lld %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000876 value, st->buf[0], st->size,
877 ct->lower_bound, ct->upper_bound,
878 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000879 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000880 if(ct->flags & APC_EXTENSIBLE) {
881 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700882 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000883 if(inext) ct = 0;
884 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700885 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000886 }
887 }
888
889
Lev Walkin6c527842014-02-09 04:34:54 -0800890 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000891 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800892 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800893 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
894 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100895 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800896 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700897 ASN__ENCODE_FAILED;
898 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000899 }
900
901 if(ct && ct->lower_bound) {
Harald Welte0b57b082015-08-30 16:33:07 +0200902 ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound);
Lev Walkin523de9e2006-08-18 01:34:18 +0000903 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700904 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000905 }
906
907 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
908 ssize_t mayEncode = uper_put_length(po, end - buf);
909 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700910 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000911 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700912 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000913 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000914 }
915
Lev Walkin7c1dc052016-03-14 03:08:15 -0700916 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000917}
918
Harald Welte0b57b082015-08-30 16:33:07 +0200919asn_enc_rval_t
920INTEGER_encode_aper(asn_TYPE_descriptor_t *td,
921 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
922 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
923 asn_enc_rval_t er;
924 INTEGER_t *st = (INTEGER_t *)sptr;
925 const uint8_t *buf;
926 const uint8_t *end;
927 asn_per_constraint_t *ct;
928 int64_t value = 0;
929
930 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
931
932 if(!constraints) constraints = td->per_constraints;
933 ct = constraints ? &constraints->value : 0;
934
935 er.encoded = 0;
936
937 if(ct) {
938 int inext = 0;
939 if(specs && specs->field_unsigned) {
940 uint64_t uval;
941 if(asn_INTEGER2uint64(st, &uval))
942 _ASN_ENCODE_FAILED;
943 /* Check proper range */
944 if(ct->flags & APC_SEMI_CONSTRAINED) {
945 if(uval < (unsigned long long)ct->lower_bound)
946 inext = 1;
947 } else if(ct->range_bits >= 0) {
948 if(uval < (unsigned long long)ct->lower_bound
949 || uval > (unsigned long long)ct->upper_bound)
950 inext = 1;
951 }
952 ASN_DEBUG("Value %llu (%02x/%d) lb %llu ub %llu %s",
953 uval, st->buf[0], st->size,
954 ct->lower_bound, ct->upper_bound,
955 inext ? "ext" : "fix");
956 value = uval;
957 } else {
958 if(asn_INTEGER2int64(st, &value)) _ASN_ENCODE_FAILED;
959 /* Check proper range */
960 if(ct->flags & APC_SEMI_CONSTRAINED) {
961 if(value < ct->lower_bound)
962 inext = 1;
963 } else if(ct->range_bits >= 0) {
964 if(value < ct->lower_bound
965 || value > ct->upper_bound)
966 inext = 1;
967 }
968 ASN_DEBUG("Value %lld (%02x/%d) lb %lld ub %lld %s",
969 value, st->buf[0], st->size,
970 ct->lower_bound, ct->upper_bound,
971 inext ? "ext" : "fix");
972 }
973 if(ct->flags & APC_EXTENSIBLE) {
974 if(per_put_few_bits(po, inext, 1))
975 _ASN_ENCODE_FAILED;
976 if(inext) ct = 0;
977 } else if(inext) {
978 _ASN_ENCODE_FAILED;
979 }
980 }
981
982 /* X.691, #12.2.2 */
983 if(ct && ct->range_bits >= 0) {
984 /* #10.5.6 */
Harald Welte2b3068f2015-12-19 13:32:58 +0100985 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
986 value, value - ct->lower_bound, ct->range_bits);
987 unsigned long v = value - ct->lower_bound;
Harald Welte0b57b082015-08-30 16:33:07 +0200988
989 /* #12 <= 8 -> alignment ? */
990 if (ct->range_bits < 8) {
Harald Welte2b3068f2015-12-19 13:32:58 +0100991 if(per_put_few_bits(po, 0x00 | v, ct->range_bits))
Harald Welte0b57b082015-08-30 16:33:07 +0200992 _ASN_ENCODE_FAILED;
993 } else if (ct->range_bits == 8) {
994 if(aper_put_align(po) < 0)
995 _ASN_ENCODE_FAILED;
Harald Welte2b3068f2015-12-19 13:32:58 +0100996 if(per_put_few_bits(po, 0x00 | v, ct->range_bits))
Harald Welte0b57b082015-08-30 16:33:07 +0200997 _ASN_ENCODE_FAILED;
998 } else if (ct->range_bits <= 16) {
999 // Consume the bytes to align on octet
1000 if(aper_put_align(po) < 0)
1001 _ASN_ENCODE_FAILED;
Harald Welte2b3068f2015-12-19 13:32:58 +01001002 if(per_put_few_bits(po, 0x0000 | v,
Harald Welte0b57b082015-08-30 16:33:07 +02001003 16))
1004 _ASN_ENCODE_FAILED;
1005 } else {
1006 /* TODO: extend to >64 bits */
Harald Welte2b3068f2015-12-19 13:32:58 +01001007 int64_t v64 = v;
Harald Welte17c28242016-04-30 17:31:33 +02001008 int i, j;
1009 int max_range_bytes = (ct->range_bits >> 3) +
1010 (((ct->range_bits % 8) > 0) ? 1 : 0);
Harald Welte0b57b082015-08-30 16:33:07 +02001011
Harald Welte17c28242016-04-30 17:31:33 +02001012 for (i = 1; ; i++) {
1013 int upper = 1 << i;
1014 if (upper >= max_range_bytes)
1015 break;
1016 }
1017
1018 for (j = sizeof(int64_t) -1; j != 0; j--) {
1019 uint8_t val;
1020 val = v64 >> (j * 8);
1021 if (val != 0)
1022 break;
1023 }
1024
1025 /* Putting length in the minimum number of bits ex: 5 = 3bits */
1026 if (per_put_few_bits(po, j, i))
Harald Welte0b57b082015-08-30 16:33:07 +02001027 _ASN_ENCODE_FAILED;
1028
1029 // Consume the bits to align on octet
1030 if (aper_put_align(po) < 0)
1031 _ASN_ENCODE_FAILED;
Harald Welte17c28242016-04-30 17:31:33 +02001032
Harald Welte0b57b082015-08-30 16:33:07 +02001033 /* Put the value */
Harald Welte17c28242016-04-30 17:31:33 +02001034 for (i = 0; i <= j; i++) {
1035 if(per_put_few_bits(po, (v64 >> (8 * (j - i))) & 0xff, 8))
1036 _ASN_ENCODE_FAILED;
Harald Welte0b57b082015-08-30 16:33:07 +02001037 }
1038 }
1039 _ASN_ENCODED_OK(er);
1040 }
1041
1042 if(ct && ct->lower_bound) {
1043 ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound);
1044 /* TODO: adjust lower bound */
1045 _ASN_ENCODE_FAILED;
1046 }
1047
1048 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
1049 ssize_t mayEncode = aper_put_length(po, -1, end - buf);
1050 if(mayEncode < 0)
1051 _ASN_ENCODE_FAILED;
1052 if(per_put_many_bits(po, buf, 8 * mayEncode))
1053 _ASN_ENCODE_FAILED;
1054 buf += mayEncode;
1055 }
1056
1057 _ASN_ENCODED_OK(er);
1058}
1059
Harald Welteb3a23042015-08-30 16:41:20 +02001060#endif /* ASN_DISABLE_PER_SUPPORT */
1061
Lev Walkinf15320b2004-06-03 03:38:44 +00001062int
Lev Walkin5e033762004-09-29 13:26:15 +00001063asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001064 uint8_t *b, *end;
1065 size_t size;
1066 long l;
1067
1068 /* Sanity checking */
1069 if(!iptr || !iptr->buf || !lptr) {
1070 errno = EINVAL;
1071 return -1;
1072 }
1073
1074 /* Cache the begin/end of the buffer */
1075 b = iptr->buf; /* Start of the INTEGER buffer */
1076 size = iptr->size;
1077 end = b + size; /* Where to stop */
1078
1079 if(size > sizeof(long)) {
1080 uint8_t *end1 = end - 1;
1081 /*
1082 * Slightly more advanced processing,
1083 * able to >sizeof(long) bytes,
1084 * when the actual value is small
1085 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1086 */
1087 /* Skip out the insignificant leading bytes */
1088 for(; b < end1; b++) {
1089 switch(*b) {
1090 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1091 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1092 }
1093 break;
1094 }
1095
1096 size = end - b;
1097 if(size > sizeof(long)) {
1098 /* Still cannot fit the long */
1099 errno = ERANGE;
1100 return -1;
1101 }
1102 }
1103
1104 /* Shortcut processing of a corner case */
1105 if(end == b) {
1106 *lptr = 0;
1107 return 0;
1108 }
1109
1110 /* Perform the sign initialization */
1111 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1112 if((*b >> 7)) l = -1; else l = 0;
1113
1114 /* Conversion engine */
1115 for(; b < end; b++)
1116 l = (l << 8) | *b;
1117
1118 *lptr = l;
1119 return 0;
1120}
Lev Walkind703ff42004-10-21 11:21:25 +00001121
1122int
Harald Welte0b57b082015-08-30 16:33:07 +02001123asn_INTEGER2int64(const INTEGER_t *iptr, int64_t *lptr) {
1124 uint8_t *b, *end;
1125 size_t size;
1126 int64_t l;
1127
1128 /* Sanity checking */
1129 if(!iptr || !iptr->buf || !lptr) {
1130 errno = EINVAL;
1131 return -1;
1132 }
1133
1134 /* Cache the begin/end of the buffer */
1135 b = iptr->buf; /* Start of the INTEGER buffer */
1136 size = iptr->size;
1137 end = b + size; /* Where to stop */
1138
1139 if(size > sizeof(int64_t)) {
1140 uint8_t *end1 = end - 1;
1141 /*
1142 * Slightly more advanced processing,
1143 * able to >sizeof(int64_t) bytes,
1144 * when the actual value is small
1145 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1146 */
1147 /* Skip out the insignificant leading bytes */
1148 for(; b < end1; b++) {
1149 switch(*b) {
1150 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1151 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1152 }
1153 break;
1154 }
1155
1156 size = end - b;
1157 if(size > sizeof(int64_t)) {
1158 /* Still cannot fit the int64_t */
1159 errno = ERANGE;
1160 return -1;
1161 }
1162 }
1163
1164 /* Shortcut processing of a corner case */
1165 if(end == b) {
1166 *lptr = 0;
1167 return 0;
1168 }
1169
1170 /* Perform the sign initialization */
1171 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1172 if((*b >> 7)) l = -1; else l = 0;
1173
1174 /* Conversion engine */
1175 for(; b < end; b++)
1176 l = (l << 8) | *b;
1177
1178 *lptr = l;
1179 return 0;
1180}
1181
1182int
Lev Walkin5c879db2007-11-06 06:23:31 +00001183asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
1184 uint8_t *b, *end;
1185 unsigned long l;
1186 size_t size;
1187
1188 if(!iptr || !iptr->buf || !lptr) {
1189 errno = EINVAL;
1190 return -1;
1191 }
1192
1193 b = iptr->buf;
1194 size = iptr->size;
1195 end = b + size;
1196
1197 /* If all extra leading bytes are zeroes, ignore them */
1198 for(; size > sizeof(unsigned long); b++, size--) {
1199 if(*b) {
1200 /* Value won't fit unsigned long */
1201 errno = ERANGE;
1202 return -1;
1203 }
1204 }
1205
1206 /* Conversion engine */
1207 for(l = 0; b < end; b++)
1208 l = (l << 8) | *b;
1209
1210 *lptr = l;
1211 return 0;
1212}
1213
1214int
Harald Welte0b57b082015-08-30 16:33:07 +02001215asn_INTEGER2uint64(const INTEGER_t *iptr, uint64_t *lptr) {
1216 uint8_t *b, *end;
1217 uint64_t l;
1218 size_t size;
1219
1220 if(!iptr || !iptr->buf || !lptr) {
1221 errno = EINVAL;
1222 return -1;
1223 }
1224
1225 b = iptr->buf;
1226 size = iptr->size;
1227 end = b + size;
1228
1229 /* If all extra leading bytes are zeroes, ignore them */
1230 for(; size > sizeof(uint64_t); b++, size--) {
1231 if(*b) {
1232 /* Value won't fit unsigned long */
1233 errno = ERANGE;
1234 return -1;
1235 }
1236 }
1237
1238 /* Conversion engine */
1239 for(l = 0; b < end; b++)
1240 l = (l << 8) | *b;
1241
1242 *lptr = l;
1243 return 0;
1244}
1245
1246int
Lev Walkin5c879db2007-11-06 06:23:31 +00001247asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1248 uint8_t *buf;
1249 uint8_t *end;
1250 uint8_t *b;
1251 int shr;
1252
1253 if(value <= LONG_MAX)
1254 return asn_long2INTEGER(st, value);
1255
1256 buf = (uint8_t *)MALLOC(1 + sizeof(value));
1257 if(!buf) return -1;
1258
1259 end = buf + (sizeof(value) + 1);
1260 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +00001261 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
1262 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +00001263
1264 if(st->buf) FREEMEM(st->buf);
1265 st->buf = buf;
1266 st->size = 1 + sizeof(value);
1267
1268 return 0;
1269}
1270
1271int
Harald Welte0b57b082015-08-30 16:33:07 +02001272asn_uint642INTEGER(INTEGER_t *st, uint64_t value) {
1273 uint8_t *buf;
1274 uint8_t *end;
1275 uint8_t *b;
1276 int shr;
1277
1278 if(value <= INT64_MAX)
1279 return asn_int642INTEGER(st, value);
1280
1281 buf = (uint8_t *)MALLOC(1 + sizeof(value));
1282 if(!buf) return -1;
1283
1284 end = buf + (sizeof(value) + 1);
1285 buf[0] = 0;
1286 for(b = buf + 1, shr = (sizeof(value)-1)*8; b < end; shr -= 8, b++)
1287 *b = (uint8_t)(value >> shr);
1288
1289 if(st->buf) FREEMEM(st->buf);
1290 st->buf = buf;
1291 st->size = 1 + sizeof(value);
1292
1293 return 0;
1294}
1295
1296int
1297asn_int642INTEGER(INTEGER_t *st, int64_t value) {
1298 uint8_t *buf, *bp;
1299 uint8_t *p;
1300 uint8_t *pstart;
1301 uint8_t *pend1;
1302 int littleEndian = 1; /* Run-time detection */
1303 int add;
1304
1305 if(!st) {
1306 errno = EINVAL;
1307 return -1;
1308 }
1309
1310 buf = (uint8_t *)MALLOC(sizeof(value));
1311 if(!buf) return -1;
1312
1313 if(*(char *)&littleEndian) {
1314 pstart = (uint8_t *)&value + sizeof(value) - 1;
1315 pend1 = (uint8_t *)&value;
1316 add = -1;
1317 } else {
1318 pstart = (uint8_t *)&value;
1319 pend1 = pstart + sizeof(value) - 1;
1320 add = 1;
1321 }
1322
1323 /*
1324 * If the contents octet consists of more than one octet,
1325 * then bits of the first octet and bit 8 of the second octet:
1326 * a) shall not all be ones; and
1327 * b) shall not all be zero.
1328 */
1329 for(p = pstart; p != pend1; p += add) {
1330 switch(*p) {
1331 case 0x00: if((*(p+add) & 0x80) == 0)
1332 continue;
1333 break;
1334 case 0xff: if((*(p+add) & 0x80))
1335 continue;
1336 break;
1337 }
1338 break;
1339 }
1340 /* Copy the integer body */
1341 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1342 *bp++ = *p;
1343
1344 if(st->buf) FREEMEM(st->buf);
1345 st->buf = buf;
1346 st->size = bp - buf;
1347
1348 return 0;
1349}
1350
1351int
Lev Walkind703ff42004-10-21 11:21:25 +00001352asn_long2INTEGER(INTEGER_t *st, long value) {
1353 uint8_t *buf, *bp;
1354 uint8_t *p;
1355 uint8_t *pstart;
1356 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +00001357 int littleEndian = 1; /* Run-time detection */
1358 int add;
Lev Walkind703ff42004-10-21 11:21:25 +00001359
1360 if(!st) {
1361 errno = EINVAL;
1362 return -1;
1363 }
1364
Lev Walkin8471cec2004-10-21 14:02:19 +00001365 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +00001366 if(!buf) return -1;
1367
Lev Walkind7ad5612004-10-26 08:20:46 +00001368 if(*(char *)&littleEndian) {
1369 pstart = (uint8_t *)&value + sizeof(value) - 1;
1370 pend1 = (uint8_t *)&value;
1371 add = -1;
1372 } else {
1373 pstart = (uint8_t *)&value;
1374 pend1 = pstart + sizeof(value) - 1;
1375 add = 1;
1376 }
1377
Lev Walkind703ff42004-10-21 11:21:25 +00001378 /*
1379 * If the contents octet consists of more than one octet,
1380 * then bits of the first octet and bit 8 of the second octet:
1381 * a) shall not all be ones; and
1382 * b) shall not all be zero.
1383 */
Lev Walkin33700162004-10-26 09:03:31 +00001384 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +00001385 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +00001386 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +00001387 continue;
1388 break;
Lev Walkin33700162004-10-26 09:03:31 +00001389 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +00001390 continue;
1391 break;
1392 }
1393 break;
1394 }
1395 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +00001396 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1397 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +00001398
1399 if(st->buf) FREEMEM(st->buf);
1400 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +00001401 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +00001402
1403 return 0;
1404}
Lev Walkinb3751942012-09-02 19:36:47 -07001405
Lev Walkincad560a2013-03-16 07:00:58 -07001406/*
1407 * This function is going to be DEPRECATED soon.
1408 */
Lev Walkine09f9f12012-09-02 23:06:35 -07001409enum asn_strtol_result_e
1410asn_strtol(const char *str, const char *end, long *lp) {
Lev Walkincad560a2013-03-16 07:00:58 -07001411 const char *endp = end;
1412
1413 switch(asn_strtol_lim(str, &endp, lp)) {
1414 case ASN_STRTOL_ERROR_RANGE:
1415 return ASN_STRTOL_ERROR_RANGE;
1416 case ASN_STRTOL_ERROR_INVAL:
1417 return ASN_STRTOL_ERROR_INVAL;
1418 case ASN_STRTOL_EXPECT_MORE:
1419 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1420 case ASN_STRTOL_OK:
1421 return ASN_STRTOL_OK;
1422 case ASN_STRTOL_EXTRA_DATA:
1423 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1424 }
1425
1426 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1427}
1428
1429/*
1430 * Parse the number in the given string until the given *end position,
1431 * returning the position after the last parsed character back using the
1432 * same (*end) pointer.
1433 * WARNING: This behavior is different from the standard strtol(3).
1434 */
1435enum asn_strtol_result_e
1436asn_strtol_lim(const char *str, const char **end, long *lp) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001437 int sign = 1;
1438 long l;
Lev Walkinb3751942012-09-02 19:36:47 -07001439
Lev Walkine09f9f12012-09-02 23:06:35 -07001440 const long upper_boundary = LONG_MAX / 10;
1441 long last_digit_max = LONG_MAX % 10;
1442
Lev Walkincad560a2013-03-16 07:00:58 -07001443 if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001444
1445 switch(*str) {
1446 case '-':
1447 last_digit_max++;
1448 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001449 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001450 case '+':
1451 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001452 if(str >= *end) {
1453 *end = str;
1454 return ASN_STRTOL_EXPECT_MORE;
1455 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001456 }
1457
Lev Walkincad560a2013-03-16 07:00:58 -07001458 for(l = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001459 switch(*str) {
1460 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1461 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1462 int d = *str - '0';
1463 if(l < upper_boundary) {
1464 l = l * 10 + d;
1465 } else if(l == upper_boundary) {
1466 if(d <= last_digit_max) {
1467 if(sign > 0) {
1468 l = l * 10 + d;
1469 } else {
1470 sign = 1;
1471 l = -l * 10 - d;
1472 }
1473 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001474 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001475 return ASN_STRTOL_ERROR_RANGE;
1476 }
1477 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001478 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001479 return ASN_STRTOL_ERROR_RANGE;
1480 }
1481 }
1482 continue;
1483 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001484 *end = str;
1485 *lp = sign * l;
1486 return ASN_STRTOL_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001487 }
1488 }
1489
Lev Walkincad560a2013-03-16 07:00:58 -07001490 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001491 *lp = sign * l;
1492 return ASN_STRTOL_OK;
Lev Walkinb3751942012-09-02 19:36:47 -07001493}
1494