blob: bcdd395c21b799081b5b70dd35e8fc2e49727637 [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 Walkin25307f62016-03-14 04:44:03 -07009#include <stdio.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000010#include <errno.h>
11
12/*
13 * INTEGER basic type description.
14 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070015static const ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000016 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
17};
Lev Walkin5e033762004-09-29 13:26:15 +000018asn_TYPE_descriptor_t asn_DEF_INTEGER = {
Lev Walkinf15320b2004-06-03 03:38:44 +000019 "INTEGER",
Lev Walkindc06f6b2004-10-20 15:50:55 +000020 "INTEGER",
Lev Walkin8e8078a2004-09-26 13:10:40 +000021 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000022 INTEGER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000023 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000024 ber_decode_primitive,
Lev Walkinf15320b2004-06-03 03:38:44 +000025 INTEGER_encode_der,
Lev Walkind703ff42004-10-21 11:21:25 +000026 INTEGER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000027 INTEGER_encode_xer,
Lev Walkind7703cf2012-09-03 01:45:03 -070028#ifdef ASN_DISABLE_PER_SUPPORT
29 0,
30 0,
31#else
Lev Walkin59b176e2005-11-26 11:25:14 +000032 INTEGER_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000033 INTEGER_encode_uper, /* Unaligned PER encoder */
Lev Walkind7703cf2012-09-03 01:45:03 -070034#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000035 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000036 asn_DEF_INTEGER_tags,
37 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
38 asn_DEF_INTEGER_tags, /* Same as above */
39 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000040 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000041 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000042 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000043};
44
45/*
Lev Walkinf15320b2004-06-03 03:38:44 +000046 * Encode INTEGER type using DER.
47 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000048asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000049INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000050 int tag_mode, ber_tlv_tag_t tag,
51 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000052 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000053
54 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000055 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000056
57 /*
58 * Canonicalize integer in the buffer.
59 * (Remove too long sign extension, remove some first 0x00 bytes)
60 */
61 if(st->buf) {
62 uint8_t *buf = st->buf;
63 uint8_t *end1 = buf + st->size - 1;
64 int shift;
65
66 /* Compute the number of superfluous leading bytes */
67 for(; buf < end1; buf++) {
68 /*
69 * If the contents octets of an integer value encoding
70 * consist of more than one octet, then the bits of the
71 * first octet and bit 8 of the second octet:
72 * a) shall not all be ones; and
73 * b) shall not all be zero.
74 */
75 switch(*buf) {
76 case 0x00: if((buf[1] & 0x80) == 0)
77 continue;
78 break;
79 case 0xff: if((buf[1] & 0x80))
80 continue;
81 break;
82 }
83 break;
84 }
85
86 /* Remove leading superfluous bytes from the integer */
87 shift = buf - st->buf;
88 if(shift) {
89 uint8_t *nb = st->buf;
90 uint8_t *end;
91
92 st->size -= shift; /* New size, minus bad bytes */
93 end = nb + st->size;
94
95 for(; nb < end; nb++, buf++)
96 *nb = *buf;
97 }
98
99 } /* if(1) */
100
Lev Walkin8e8078a2004-09-26 13:10:40 +0000101 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000102}
103
Lev Walkinc2350112005-03-29 17:19:53 +0000104static 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 +0000105
Lev Walkinf15320b2004-06-03 03:38:44 +0000106/*
107 * INTEGER specific human-readable output.
108 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000109static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700110INTEGER__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 +0000111 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000112 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 uint8_t *buf = st->buf;
114 uint8_t *buf_end = st->buf + st->size;
Lev Walkin97f8edc2013-03-28 04:38:41 -0700115 signed long value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000116 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 char *p;
118 int ret;
119
Lev Walkin97f8edc2013-03-28 04:38:41 -0700120 if(specs && specs->field_unsigned)
121 ret = asn_INTEGER2ulong(st, (unsigned long *)&value);
122 else
123 ret = asn_INTEGER2long(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000124
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700126 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000127 const asn_INTEGER_enum_map_t *el;
128 size_t scrsize;
129 char *scr;
130
Lev Walkin97f8edc2013-03-28 04:38:41 -0700131 el = (value >= 0 || !specs || !specs->field_unsigned)
132 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000133 if(el) {
134 scrsize = el->enum_len + 32;
135 scr = (char *)alloca(scrsize);
136 if(plainOrXER == 0)
137 ret = snprintf(scr, scrsize,
Lev Walkin97f8edc2013-03-28 04:38:41 -0700138 "%ld (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000139 else
140 ret = snprintf(scr, scrsize,
141 "<%s/>", el->enum_name);
142 } else if(plainOrXER && specs && specs->strict_enumeration) {
143 ASN_DEBUG("ASN.1 forbids dealing with "
144 "unknown value of ENUMERATED type");
145 errno = EPERM;
146 return -1;
147 } else {
148 scrsize = sizeof(scratch);
149 scr = scratch;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000150 ret = snprintf(scr, scrsize,
151 (specs && specs->field_unsigned)
Lev Walkin97f8edc2013-03-28 04:38:41 -0700152 ?"%lu":"%ld", value);
Lev Walkine0b56e02005-02-25 12:10:27 +0000153 }
154 assert(ret > 0 && (size_t)ret < scrsize);
155 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
156 } else if(plainOrXER && specs && specs->strict_enumeration) {
157 /*
158 * Here and earlier, we cannot encode the ENUMERATED values
159 * if there is no corresponding identifier.
160 */
161 ASN_DEBUG("ASN.1 forbids dealing with "
162 "unknown value of ENUMERATED type");
163 errno = EPERM;
164 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 }
166
167 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000168 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700170 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000171 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000173 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000175 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 p = scratch;
177 }
178 *p++ = h2c[*buf >> 4];
179 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000180 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 }
Lev Walkindb13f512004-07-19 17:30:25 +0000182 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000183 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000184
Lev Walkina9cc46e2004-09-22 16:06:28 +0000185 wrote += p - scratch;
186 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
187}
188
189/*
190 * INTEGER specific human-readable output.
191 */
192int
Lev Walkin5e033762004-09-29 13:26:15 +0000193INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000194 asn_app_consume_bytes_f *cb, void *app_key) {
195 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000196 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197
198 (void)td;
199 (void)ilevel;
200
Lev Walkind500a962005-11-27 13:06:56 +0000201 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000202 ret = cb("<absent>", 8, app_key);
203 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000204 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000205
Lev Walkin8e8078a2004-09-26 13:10:40 +0000206 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000207}
208
Lev Walkine0b56e02005-02-25 12:10:27 +0000209struct e2v_key {
210 const char *start;
211 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700212 const asn_INTEGER_enum_map_t *vemap;
213 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000214};
215static int
216INTEGER__compar_enum2value(const void *kp, const void *am) {
217 const struct e2v_key *key = (const struct e2v_key *)kp;
218 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
219 const char *ptr, *end, *name;
220
221 /* Remap the element (sort by different criterion) */
222 el = key->vemap + key->evmap[el - key->vemap];
223
224 /* Compare strings */
225 for(ptr = key->start, end = key->stop, name = el->enum_name;
226 ptr < end; ptr++, name++) {
227 if(*ptr != *name)
228 return *(const unsigned char *)ptr
229 - *(const unsigned char *)name;
230 }
231 return name[0] ? -1 : 0;
232}
233
234static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000235INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700236 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000237 int count = specs ? specs->map_count : 0;
238 struct e2v_key key;
239 const char *lp;
240
241 if(!count) return NULL;
242
243 /* Guaranteed: assert(lstart < lstop); */
244 /* Figure out the tag name */
245 for(lstart++, lp = lstart; lp < lstop; lp++) {
246 switch(*lp) {
247 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
248 case 0x2f: /* '/' */ case 0x3e: /* '>' */
249 break;
250 default:
251 continue;
252 }
253 break;
254 }
255 if(lp == lstop) return NULL; /* No tag found */
256 lstop = lp;
257
258 key.start = lstart;
259 key.stop = lstop;
260 key.vemap = specs->value2enum;
261 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000262 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
263 specs->value2enum, count, sizeof(specs->value2enum[0]),
264 INTEGER__compar_enum2value);
265 if(el_found) {
266 /* Remap enum2value into value2enum */
267 el_found = key.vemap + key.evmap[el_found - key.vemap];
268 }
269 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000270}
271
272static int
273INTEGER__compar_value2enum(const void *kp, const void *am) {
274 long a = *(const long *)kp;
275 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
276 long b = el->nat_value;
277 if(a < b) return -1;
278 else if(a == b) return 0;
279 else return 1;
280}
281
Lev Walkinc2350112005-03-29 17:19:53 +0000282const asn_INTEGER_enum_map_t *
283INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000284 int count = specs ? specs->map_count : 0;
285 if(!count) return 0;
286 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
287 count, sizeof(specs->value2enum[0]),
288 INTEGER__compar_value2enum);
289}
290
Lev Walkinc744a022006-09-15 18:33:25 +0000291static int
292INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
293 void *p = MALLOC(min_size + 1);
294 if(p) {
295 void *b = st->buf;
296 st->size = 0;
297 st->buf = p;
298 FREEMEM(b);
299 return 0;
300 } else {
301 return -1;
302 }
303}
304
Lev Walkind703ff42004-10-21 11:21:25 +0000305/*
306 * Decode the chunk of XML text encoding INTEGER.
307 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000308static enum xer_pbd_rval
309INTEGER__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 +0000310 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinb3751942012-09-02 19:36:47 -0700311 long dec_value;
Lev Walkin0ff71292013-03-25 18:51:51 -0700312 long hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000313 const char *lp;
314 const char *lstart = (const char *)chunk_buf;
315 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000316 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700317 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000318 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000319 ST_WAITDIGITS,
320 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700321 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000322 ST_HEXDIGIT1,
323 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700324 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000325 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700326 ST_END_ENUM,
327 ST_UNEXPECTED
328 } state = ST_LEADSPACE;
329 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
330 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000331
Lev Walkinc744a022006-09-15 18:33:25 +0000332 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000333 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
334 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000335
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000336 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
337 return XPBD_SYSTEM_FAILURE;
338
Lev Walkind703ff42004-10-21 11:21:25 +0000339 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000340 * We may have received a tag here. It will be processed inline.
341 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000342 */
Lev Walkinb3751942012-09-02 19:36:47 -0700343 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000344 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000345 switch(lv) {
346 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000347 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700348 case ST_LEADSPACE:
349 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700350 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000351 case ST_SKIPSPHEX:
352 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700353 case ST_DIGITS:
354 dec_value_end = lp;
355 state = ST_DIGITS_TRAILSPACE;
356 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700357 case ST_HEXCOLON:
358 state = ST_HEXDIGITS_TRAILSPACE;
359 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000360 default:
361 break;
362 }
Lev Walkind703ff42004-10-21 11:21:25 +0000363 break;
364 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700365 if(state == ST_LEADSPACE) {
366 dec_value = 0;
367 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000368 state = ST_WAITDIGITS;
369 continue;
370 }
371 break;
372 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700373 if(state == ST_LEADSPACE) {
374 dec_value = 0;
375 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000376 state = ST_WAITDIGITS;
377 continue;
378 }
379 break;
380 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
381 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000382 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700383 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000384 case ST_SKIPSPHEX: /* Fall through */
385 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700386 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000387 state = ST_HEXDIGIT2;
388 continue;
389 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700390 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000391 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700392 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000393 continue;
394 case ST_HEXCOLON:
395 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700396 case ST_LEADSPACE:
397 dec_value = 0;
398 dec_value_start = lp;
399 /* FALL THROUGH */
400 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000401 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700402 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700403 default:
404 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000405 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700406 break;
407 case 0x3c: /* '<', start of XML encoded enumeration */
408 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000409 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000410 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000411 (asn_INTEGER_specifics_t *)
412 td->specifics, lstart, lstop);
413 if(el) {
414 ASN_DEBUG("Found \"%s\" => %ld",
415 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700416 dec_value = el->nat_value;
417 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000418 lp = lstop - 1;
419 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000420 }
421 ASN_DEBUG("Unknown identifier for INTEGER");
422 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000423 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000424 case 0x3a: /* ':' */
425 if(state == ST_HEXCOLON) {
426 /* This colon is expected */
427 state = ST_HEXDIGIT1;
428 continue;
429 } else if(state == ST_DIGITS) {
430 /* The colon here means that we have
431 * decoded the first two hexadecimal
432 * places as a decimal value.
433 * Switch decoding mode. */
434 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000435 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700436 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000437 lp = lstart - 1;
438 continue;
439 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400440 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000441 break;
442 }
443 /* [A-Fa-f] */
444 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
445 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
446 switch(state) {
447 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700448 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000449 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700450 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
451 hex_value += 10;
452 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000453 state = ST_HEXDIGIT2;
454 continue;
455 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700456 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
457 hex_value += 10;
458 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000459 state = ST_HEXCOLON;
460 continue;
461 case ST_DIGITS:
462 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000463 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700464 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000465 lp = lstart - 1;
466 continue;
467 default:
468 break;
469 }
470 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000471 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000472
473 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700474 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400475 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700476 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000477 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000478 }
479
Lev Walkinc744a022006-09-15 18:33:25 +0000480 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700481 case ST_END_ENUM:
482 /* Got a complete and valid enumeration encoded as a tag. */
483 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000484 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700485 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700486 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700487 case ST_DIGITS_TRAILSPACE:
488 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700489 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700490 case ASN_STRTOL_OK:
491 break;
492 case ASN_STRTOL_ERROR_RANGE:
493 return XPBD_DECODER_LIMIT;
494 case ASN_STRTOL_ERROR_INVAL:
Lev Walkincad560a2013-03-16 07:00:58 -0700495 case ASN_STRTOL_EXPECT_MORE:
496 case ASN_STRTOL_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700497 return XPBD_BROKEN_ENCODING;
498 }
Lev Walkinc744a022006-09-15 18:33:25 +0000499 break;
500 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700501 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000502 st->buf[st->size] = 0; /* Just in case termination */
503 return XPBD_BODY_CONSUMED;
504 case ST_HEXDIGIT1:
505 case ST_HEXDIGIT2:
506 case ST_SKIPSPHEX:
507 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700508 case ST_LEADSPACE:
509 /* Content not found */
510 return XPBD_NOT_BODY_IGNORE;
511 case ST_WAITDIGITS:
512 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700513 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
514 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000515 }
Lev Walkind703ff42004-10-21 11:21:25 +0000516
Lev Walkine09f9f12012-09-02 23:06:35 -0700517 /*
518 * Convert the result of parsing of enumeration or a straight
519 * decimal value into a BER representation.
520 */
521 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000522 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000523
Lev Walkin0fab1a62005-03-09 22:19:25 +0000524 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000525}
526
527asn_dec_rval_t
528INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
529 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000530 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000531
532 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000533 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000534 buf_ptr, size, INTEGER__xer_body_decode);
535}
536
Lev Walkina9cc46e2004-09-22 16:06:28 +0000537asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000538INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000539 int ilevel, enum xer_encoder_flags_e flags,
540 asn_app_consume_bytes_f *cb, void *app_key) {
541 const INTEGER_t *st = (const INTEGER_t *)sptr;
542 asn_enc_rval_t er;
543
544 (void)ilevel;
545 (void)flags;
546
Lev Walkind500a962005-11-27 13:06:56 +0000547 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700548 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000549
Lev Walkine0b56e02005-02-25 12:10:27 +0000550 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700551 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000552
Lev Walkin7c1dc052016-03-14 03:08:15 -0700553 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000554}
555
Lev Walkind7703cf2012-09-03 01:45:03 -0700556#ifndef ASN_DISABLE_PER_SUPPORT
557
Lev Walkin59b176e2005-11-26 11:25:14 +0000558asn_dec_rval_t
559INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
560 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000561 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000562 asn_dec_rval_t rval = { RC_OK, 0 };
563 INTEGER_t *st = (INTEGER_t *)*sptr;
564 asn_per_constraint_t *ct;
565 int repeat;
566
567 (void)opt_codec_ctx;
568
569 if(!st) {
570 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700571 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000572 }
573
574 if(!constraints) constraints = td->per_constraints;
575 ct = constraints ? &constraints->value : 0;
576
577 if(ct && ct->flags & APC_EXTENSIBLE) {
578 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700579 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000580 if(inext) ct = 0;
581 }
582
583 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000584 st->buf = 0;
585 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000586 if(ct) {
587 if(ct->flags & APC_SEMI_CONSTRAINED) {
588 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700589 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000590 st->size = 1;
591 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
592 size_t size = (ct->range_bits + 7) >> 3;
593 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700594 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000595 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000596 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000597 }
598
Lev Walkin6c527842014-02-09 04:34:54 -0800599 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000600 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800601 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000602 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
603 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800604 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700605 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800606
607 if(specs && specs->field_unsigned) {
608 unsigned long uvalue;
609 if(uper_get_constrained_whole_number(pd,
610 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700611 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800612 ASN_DEBUG("Got value %lu + low %ld",
613 uvalue, ct->lower_bound);
614 uvalue += ct->lower_bound;
615 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700616 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800617 } else {
618 unsigned long svalue;
619 if(uper_get_constrained_whole_number(pd,
620 &svalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700621 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800622 ASN_DEBUG("Got value %ld + low %ld",
623 svalue, ct->lower_bound);
624 svalue += ct->lower_bound;
625 if(asn_long2INTEGER(st, svalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700626 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800627 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000628 return rval;
629 }
630 } else {
631 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
632 }
633
634 /* X.691, #12.2.3, #12.2.4 */
635 do {
636 ssize_t len;
637 void *p;
638 int ret;
639
640 /* Get the PER length */
641 len = uper_get_length(pd, -1, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700642 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000643
644 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700645 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000646 st->buf = (uint8_t *)p;
647
648 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700649 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000650 st->size += len;
651 } while(repeat);
652 st->buf[st->size] = 0; /* JIC */
653
654 /* #12.2.3 */
655 if(ct && ct->lower_bound) {
656 /*
657 * TODO: replace by in-place arithmetics.
658 */
659 long value;
660 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700661 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000662 if(asn_long2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700663 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000664 }
665
666 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000667}
668
Lev Walkin523de9e2006-08-18 01:34:18 +0000669asn_enc_rval_t
670INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
671 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000672 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000673 asn_enc_rval_t er;
674 INTEGER_t *st = (INTEGER_t *)sptr;
675 const uint8_t *buf;
676 const uint8_t *end;
677 asn_per_constraint_t *ct;
678 long value = 0;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100679 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000680
Lev Walkin7c1dc052016-03-14 03:08:15 -0700681 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000682
683 if(!constraints) constraints = td->per_constraints;
684 ct = constraints ? &constraints->value : 0;
685
686 er.encoded = 0;
687
688 if(ct) {
689 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000690 if(specs && specs->field_unsigned) {
691 unsigned long uval;
692 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700693 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000694 /* Check proper range */
695 if(ct->flags & APC_SEMI_CONSTRAINED) {
696 if(uval < (unsigned long)ct->lower_bound)
697 inext = 1;
698 } else if(ct->range_bits >= 0) {
699 if(uval < (unsigned long)ct->lower_bound
700 || uval > (unsigned long)ct->upper_bound)
701 inext = 1;
702 }
703 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
704 uval, st->buf[0], st->size,
705 ct->lower_bound, ct->upper_bound,
706 inext ? "ext" : "fix");
707 value = uval;
708 } else {
709 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700710 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000711 /* Check proper range */
712 if(ct->flags & APC_SEMI_CONSTRAINED) {
713 if(value < ct->lower_bound)
714 inext = 1;
715 } else if(ct->range_bits >= 0) {
716 if(value < ct->lower_bound
717 || value > ct->upper_bound)
718 inext = 1;
719 }
720 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
721 value, st->buf[0], st->size,
722 ct->lower_bound, ct->upper_bound,
723 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000724 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000725 if(ct->flags & APC_EXTENSIBLE) {
726 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700727 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000728 if(inext) ct = 0;
729 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700730 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000731 }
732 }
733
734
Lev Walkin6c527842014-02-09 04:34:54 -0800735 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000736 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800737 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800738 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
739 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100740 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800741 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700742 ASN__ENCODE_FAILED;
743 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000744 }
745
746 if(ct && ct->lower_bound) {
747 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
748 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700749 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000750 }
751
752 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
753 ssize_t mayEncode = uper_put_length(po, end - buf);
754 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700755 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000756 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700757 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000758 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000759 }
760
Lev Walkin7c1dc052016-03-14 03:08:15 -0700761 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000762}
763
Lev Walkind7703cf2012-09-03 01:45:03 -0700764#endif /* ASN_DISABLE_PER_SUPPORT */
765
Lev Walkinf15320b2004-06-03 03:38:44 +0000766int
Lev Walkin5e033762004-09-29 13:26:15 +0000767asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000768 uint8_t *b, *end;
769 size_t size;
770 long l;
771
772 /* Sanity checking */
773 if(!iptr || !iptr->buf || !lptr) {
774 errno = EINVAL;
775 return -1;
776 }
777
778 /* Cache the begin/end of the buffer */
779 b = iptr->buf; /* Start of the INTEGER buffer */
780 size = iptr->size;
781 end = b + size; /* Where to stop */
782
783 if(size > sizeof(long)) {
784 uint8_t *end1 = end - 1;
785 /*
786 * Slightly more advanced processing,
787 * able to >sizeof(long) bytes,
788 * when the actual value is small
789 * (0x0000000000abcdef would yield a fine 0x00abcdef)
790 */
791 /* Skip out the insignificant leading bytes */
792 for(; b < end1; b++) {
793 switch(*b) {
794 case 0x00: if((b[1] & 0x80) == 0) continue; break;
795 case 0xff: if((b[1] & 0x80) != 0) continue; break;
796 }
797 break;
798 }
799
800 size = end - b;
801 if(size > sizeof(long)) {
802 /* Still cannot fit the long */
803 errno = ERANGE;
804 return -1;
805 }
806 }
807
808 /* Shortcut processing of a corner case */
809 if(end == b) {
810 *lptr = 0;
811 return 0;
812 }
813
814 /* Perform the sign initialization */
815 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
816 if((*b >> 7)) l = -1; else l = 0;
817
818 /* Conversion engine */
819 for(; b < end; b++)
820 l = (l << 8) | *b;
821
822 *lptr = l;
823 return 0;
824}
Lev Walkind703ff42004-10-21 11:21:25 +0000825
826int
Lev Walkin5c879db2007-11-06 06:23:31 +0000827asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
828 uint8_t *b, *end;
829 unsigned long l;
830 size_t size;
831
832 if(!iptr || !iptr->buf || !lptr) {
833 errno = EINVAL;
834 return -1;
835 }
836
837 b = iptr->buf;
838 size = iptr->size;
839 end = b + size;
840
841 /* If all extra leading bytes are zeroes, ignore them */
842 for(; size > sizeof(unsigned long); b++, size--) {
843 if(*b) {
844 /* Value won't fit unsigned long */
845 errno = ERANGE;
846 return -1;
847 }
848 }
849
850 /* Conversion engine */
851 for(l = 0; b < end; b++)
852 l = (l << 8) | *b;
853
854 *lptr = l;
855 return 0;
856}
857
858int
859asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
860 uint8_t *buf;
861 uint8_t *end;
862 uint8_t *b;
863 int shr;
864
865 if(value <= LONG_MAX)
866 return asn_long2INTEGER(st, value);
867
868 buf = (uint8_t *)MALLOC(1 + sizeof(value));
869 if(!buf) return -1;
870
871 end = buf + (sizeof(value) + 1);
872 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +0000873 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
874 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000875
876 if(st->buf) FREEMEM(st->buf);
877 st->buf = buf;
878 st->size = 1 + sizeof(value);
879
880 return 0;
881}
882
883int
Lev Walkind703ff42004-10-21 11:21:25 +0000884asn_long2INTEGER(INTEGER_t *st, long value) {
885 uint8_t *buf, *bp;
886 uint8_t *p;
887 uint8_t *pstart;
888 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000889 int littleEndian = 1; /* Run-time detection */
890 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000891
892 if(!st) {
893 errno = EINVAL;
894 return -1;
895 }
896
Lev Walkin8471cec2004-10-21 14:02:19 +0000897 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000898 if(!buf) return -1;
899
Lev Walkind7ad5612004-10-26 08:20:46 +0000900 if(*(char *)&littleEndian) {
901 pstart = (uint8_t *)&value + sizeof(value) - 1;
902 pend1 = (uint8_t *)&value;
903 add = -1;
904 } else {
905 pstart = (uint8_t *)&value;
906 pend1 = pstart + sizeof(value) - 1;
907 add = 1;
908 }
909
Lev Walkind703ff42004-10-21 11:21:25 +0000910 /*
911 * If the contents octet consists of more than one octet,
912 * then bits of the first octet and bit 8 of the second octet:
913 * a) shall not all be ones; and
914 * b) shall not all be zero.
915 */
Lev Walkin33700162004-10-26 09:03:31 +0000916 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000917 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000918 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000919 continue;
920 break;
Lev Walkin33700162004-10-26 09:03:31 +0000921 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000922 continue;
923 break;
924 }
925 break;
926 }
927 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000928 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
929 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000930
931 if(st->buf) FREEMEM(st->buf);
932 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000933 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000934
935 return 0;
936}
Lev Walkinb3751942012-09-02 19:36:47 -0700937
Lev Walkincad560a2013-03-16 07:00:58 -0700938/*
939 * This function is going to be DEPRECATED soon.
940 */
Lev Walkine09f9f12012-09-02 23:06:35 -0700941enum asn_strtol_result_e
942asn_strtol(const char *str, const char *end, long *lp) {
Lev Walkincad560a2013-03-16 07:00:58 -0700943 const char *endp = end;
944
945 switch(asn_strtol_lim(str, &endp, lp)) {
946 case ASN_STRTOL_ERROR_RANGE:
947 return ASN_STRTOL_ERROR_RANGE;
948 case ASN_STRTOL_ERROR_INVAL:
949 return ASN_STRTOL_ERROR_INVAL;
950 case ASN_STRTOL_EXPECT_MORE:
951 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
952 case ASN_STRTOL_OK:
953 return ASN_STRTOL_OK;
954 case ASN_STRTOL_EXTRA_DATA:
955 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
956 }
957
958 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
959}
960
961/*
962 * Parse the number in the given string until the given *end position,
963 * returning the position after the last parsed character back using the
964 * same (*end) pointer.
965 * WARNING: This behavior is different from the standard strtol(3).
966 */
967enum asn_strtol_result_e
968asn_strtol_lim(const char *str, const char **end, long *lp) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700969 int sign = 1;
970 long l;
Lev Walkinb3751942012-09-02 19:36:47 -0700971
Lev Walkine09f9f12012-09-02 23:06:35 -0700972 const long upper_boundary = LONG_MAX / 10;
973 long last_digit_max = LONG_MAX % 10;
974
Lev Walkincad560a2013-03-16 07:00:58 -0700975 if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -0700976
977 switch(*str) {
978 case '-':
979 last_digit_max++;
980 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -0400981 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700982 case '+':
983 str++;
Lev Walkincad560a2013-03-16 07:00:58 -0700984 if(str >= *end) {
985 *end = str;
986 return ASN_STRTOL_EXPECT_MORE;
987 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700988 }
989
Lev Walkincad560a2013-03-16 07:00:58 -0700990 for(l = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700991 switch(*str) {
992 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
993 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
994 int d = *str - '0';
995 if(l < upper_boundary) {
996 l = l * 10 + d;
997 } else if(l == upper_boundary) {
998 if(d <= last_digit_max) {
999 if(sign > 0) {
1000 l = l * 10 + d;
1001 } else {
1002 sign = 1;
1003 l = -l * 10 - d;
1004 }
1005 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001006 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001007 return ASN_STRTOL_ERROR_RANGE;
1008 }
1009 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001010 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001011 return ASN_STRTOL_ERROR_RANGE;
1012 }
1013 }
1014 continue;
1015 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001016 *end = str;
1017 *lp = sign * l;
1018 return ASN_STRTOL_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001019 }
1020 }
1021
Lev Walkincad560a2013-03-16 07:00:58 -07001022 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001023 *lp = sign * l;
1024 return ASN_STRTOL_OK;
Lev Walkinb3751942012-09-02 19:36:47 -07001025}
1026