blob: 4e085fb119218fa8086a1008edf2df6878859a96 [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 Walkin8077f932017-08-07 17:27:43 -070020 INTEGER_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000021 INTEGER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000022 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000023 ber_decode_primitive,
Lev Walkinf15320b2004-06-03 03:38:44 +000024 INTEGER_encode_der,
Lev Walkind703ff42004-10-21 11:21:25 +000025 INTEGER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000026 INTEGER_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070027#ifdef ASN_DISABLE_OER_SUPPORT
28 0,
29 0,
30#else
Lev Walkin527497f2017-07-14 08:56:36 +040031 INTEGER_decode_oer, /* OER decoder */
32 INTEGER_encode_oer, /* Canonical OER encoder */
Lev Walkincc159472017-07-06 08:26:36 -070033#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040034#ifdef ASN_DISABLE_PER_SUPPORT
35 0,
36 0,
37#else
38 INTEGER_decode_uper, /* Unaligned PER decoder */
39 INTEGER_encode_uper, /* Unaligned PER encoder */
40#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000041 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000042 asn_DEF_INTEGER_tags,
43 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
44 asn_DEF_INTEGER_tags, /* Same as above */
45 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070046 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000047 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000048 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000049 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000050};
51
52/*
Lev Walkinf15320b2004-06-03 03:38:44 +000053 * Encode INTEGER type using DER.
54 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000055asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000056INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000057 int tag_mode, ber_tlv_tag_t tag,
58 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000059 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000060
61 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000062 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000063
64 /*
65 * Canonicalize integer in the buffer.
66 * (Remove too long sign extension, remove some first 0x00 bytes)
67 */
68 if(st->buf) {
69 uint8_t *buf = st->buf;
70 uint8_t *end1 = buf + st->size - 1;
71 int shift;
72
73 /* Compute the number of superfluous leading bytes */
74 for(; buf < end1; buf++) {
75 /*
76 * If the contents octets of an integer value encoding
77 * consist of more than one octet, then the bits of the
78 * first octet and bit 8 of the second octet:
79 * a) shall not all be ones; and
80 * b) shall not all be zero.
81 */
82 switch(*buf) {
83 case 0x00: if((buf[1] & 0x80) == 0)
84 continue;
85 break;
86 case 0xff: if((buf[1] & 0x80))
87 continue;
88 break;
89 }
90 break;
91 }
92
93 /* Remove leading superfluous bytes from the integer */
94 shift = buf - st->buf;
95 if(shift) {
96 uint8_t *nb = st->buf;
97 uint8_t *end;
98
99 st->size -= shift; /* New size, minus bad bytes */
100 end = nb + st->size;
101
102 for(; nb < end; nb++, buf++)
103 *nb = *buf;
104 }
105
106 } /* if(1) */
107
Lev Walkin8e8078a2004-09-26 13:10:40 +0000108 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000109}
110
Lev Walkinc2350112005-03-29 17:19:53 +0000111static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop);
Lev Walkine0b56e02005-02-25 12:10:27 +0000112
Lev Walkinf15320b2004-06-03 03:38:44 +0000113/*
114 * INTEGER specific human-readable output.
115 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000116static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700117INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000118 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000119 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 uint8_t *buf = st->buf;
121 uint8_t *buf_end = st->buf + st->size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700122 intmax_t value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000123 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000124 char *p;
125 int ret;
126
Lev Walkin97f8edc2013-03-28 04:38:41 -0700127 if(specs && specs->field_unsigned)
Lev Walkin72ec9092017-07-05 05:49:12 -0700128 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
Lev Walkin97f8edc2013-03-28 04:38:41 -0700129 else
Lev Walkin72ec9092017-07-05 05:49:12 -0700130 ret = asn_INTEGER2imax(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000131
Lev Walkinf15320b2004-06-03 03:38:44 +0000132 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700133 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000134 const asn_INTEGER_enum_map_t *el;
135 size_t scrsize;
136 char *scr;
137
Lev Walkin97f8edc2013-03-28 04:38:41 -0700138 el = (value >= 0 || !specs || !specs->field_unsigned)
139 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000140 if(el) {
141 scrsize = el->enum_len + 32;
142 scr = (char *)alloca(scrsize);
143 if(plainOrXER == 0)
144 ret = snprintf(scr, scrsize,
Lev Walkin72ec9092017-07-05 05:49:12 -0700145 "%" PRIdMAX " (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000146 else
147 ret = snprintf(scr, scrsize,
148 "<%s/>", el->enum_name);
149 } else if(plainOrXER && specs && specs->strict_enumeration) {
150 ASN_DEBUG("ASN.1 forbids dealing with "
151 "unknown value of ENUMERATED type");
152 errno = EPERM;
153 return -1;
154 } else {
155 scrsize = sizeof(scratch);
156 scr = scratch;
Lev Walkin72ec9092017-07-05 05:49:12 -0700157 ret = snprintf(
158 scr, scrsize,
159 (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX,
160 value);
161 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000162 assert(ret > 0 && (size_t)ret < scrsize);
163 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
164 } else if(plainOrXER && specs && specs->strict_enumeration) {
165 /*
166 * Here and earlier, we cannot encode the ENUMERATED values
167 * if there is no corresponding identifier.
168 */
169 ASN_DEBUG("ASN.1 forbids dealing with "
170 "unknown value of ENUMERATED type");
171 errno = EPERM;
172 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 }
174
175 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000176 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700178 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000179 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000181 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000183 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 p = scratch;
185 }
186 *p++ = h2c[*buf >> 4];
187 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000188 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 }
Lev Walkindb13f512004-07-19 17:30:25 +0000190 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000191 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000192
Lev Walkina9cc46e2004-09-22 16:06:28 +0000193 wrote += p - scratch;
194 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
195}
196
197/*
198 * INTEGER specific human-readable output.
199 */
200int
Lev Walkin5e033762004-09-29 13:26:15 +0000201INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000202 asn_app_consume_bytes_f *cb, void *app_key) {
203 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000204 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000205
206 (void)td;
207 (void)ilevel;
208
Lev Walkind500a962005-11-27 13:06:56 +0000209 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000210 ret = cb("<absent>", 8, app_key);
211 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000212 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000213
Lev Walkin8e8078a2004-09-26 13:10:40 +0000214 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000215}
216
Lev Walkine0b56e02005-02-25 12:10:27 +0000217struct e2v_key {
218 const char *start;
219 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700220 const asn_INTEGER_enum_map_t *vemap;
221 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000222};
223static int
224INTEGER__compar_enum2value(const void *kp, const void *am) {
225 const struct e2v_key *key = (const struct e2v_key *)kp;
226 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
227 const char *ptr, *end, *name;
228
229 /* Remap the element (sort by different criterion) */
230 el = key->vemap + key->evmap[el - key->vemap];
231
232 /* Compare strings */
233 for(ptr = key->start, end = key->stop, name = el->enum_name;
234 ptr < end; ptr++, name++) {
235 if(*ptr != *name)
236 return *(const unsigned char *)ptr
237 - *(const unsigned char *)name;
238 }
239 return name[0] ? -1 : 0;
240}
241
242static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000243INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700244 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000245 int count = specs ? specs->map_count : 0;
246 struct e2v_key key;
247 const char *lp;
248
249 if(!count) return NULL;
250
251 /* Guaranteed: assert(lstart < lstop); */
252 /* Figure out the tag name */
253 for(lstart++, lp = lstart; lp < lstop; lp++) {
254 switch(*lp) {
255 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
256 case 0x2f: /* '/' */ case 0x3e: /* '>' */
257 break;
258 default:
259 continue;
260 }
261 break;
262 }
263 if(lp == lstop) return NULL; /* No tag found */
264 lstop = lp;
265
266 key.start = lstart;
267 key.stop = lstop;
268 key.vemap = specs->value2enum;
269 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000270 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
271 specs->value2enum, count, sizeof(specs->value2enum[0]),
272 INTEGER__compar_enum2value);
273 if(el_found) {
274 /* Remap enum2value into value2enum */
275 el_found = key.vemap + key.evmap[el_found - key.vemap];
276 }
277 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000278}
279
280static int
281INTEGER__compar_value2enum(const void *kp, const void *am) {
282 long a = *(const long *)kp;
283 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
284 long b = el->nat_value;
285 if(a < b) return -1;
286 else if(a == b) return 0;
287 else return 1;
288}
289
Lev Walkinc2350112005-03-29 17:19:53 +0000290const asn_INTEGER_enum_map_t *
291INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000292 int count = specs ? specs->map_count : 0;
293 if(!count) return 0;
294 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
295 count, sizeof(specs->value2enum[0]),
296 INTEGER__compar_value2enum);
297}
298
Lev Walkinc744a022006-09-15 18:33:25 +0000299static int
300INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
301 void *p = MALLOC(min_size + 1);
302 if(p) {
303 void *b = st->buf;
304 st->size = 0;
305 st->buf = p;
306 FREEMEM(b);
307 return 0;
308 } else {
309 return -1;
310 }
311}
312
Lev Walkind703ff42004-10-21 11:21:25 +0000313/*
314 * Decode the chunk of XML text encoding INTEGER.
315 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000316static enum xer_pbd_rval
317INTEGER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000318 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700319 intmax_t dec_value;
320 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000321 const char *lp;
322 const char *lstart = (const char *)chunk_buf;
323 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000324 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700325 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000326 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000327 ST_WAITDIGITS,
328 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700329 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000330 ST_HEXDIGIT1,
331 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700332 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000333 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700334 ST_END_ENUM,
335 ST_UNEXPECTED
336 } state = ST_LEADSPACE;
337 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
338 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000339
Lev Walkinc744a022006-09-15 18:33:25 +0000340 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000341 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
342 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000343
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000344 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
345 return XPBD_SYSTEM_FAILURE;
346
Lev Walkind703ff42004-10-21 11:21:25 +0000347 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000348 * We may have received a tag here. It will be processed inline.
349 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000350 */
Lev Walkinb3751942012-09-02 19:36:47 -0700351 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000352 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000353 switch(lv) {
354 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000355 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700356 case ST_LEADSPACE:
357 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700358 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000359 case ST_SKIPSPHEX:
360 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700361 case ST_DIGITS:
362 dec_value_end = lp;
363 state = ST_DIGITS_TRAILSPACE;
364 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700365 case ST_HEXCOLON:
366 state = ST_HEXDIGITS_TRAILSPACE;
367 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000368 default:
369 break;
370 }
Lev Walkind703ff42004-10-21 11:21:25 +0000371 break;
372 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700373 if(state == ST_LEADSPACE) {
374 dec_value = 0;
375 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000376 state = ST_WAITDIGITS;
377 continue;
378 }
379 break;
380 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700381 if(state == ST_LEADSPACE) {
382 dec_value = 0;
383 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000384 state = ST_WAITDIGITS;
385 continue;
386 }
387 break;
388 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
389 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000390 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700391 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000392 case ST_SKIPSPHEX: /* Fall through */
393 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700394 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000395 state = ST_HEXDIGIT2;
396 continue;
397 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700398 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000399 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700400 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000401 continue;
402 case ST_HEXCOLON:
403 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700404 case ST_LEADSPACE:
405 dec_value = 0;
406 dec_value_start = lp;
407 /* FALL THROUGH */
408 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000409 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700410 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700411 default:
412 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000413 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700414 break;
415 case 0x3c: /* '<', start of XML encoded enumeration */
416 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000417 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000418 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000419 (asn_INTEGER_specifics_t *)
420 td->specifics, lstart, lstop);
421 if(el) {
422 ASN_DEBUG("Found \"%s\" => %ld",
423 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700424 dec_value = el->nat_value;
425 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000426 lp = lstop - 1;
427 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000428 }
429 ASN_DEBUG("Unknown identifier for INTEGER");
430 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000431 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000432 case 0x3a: /* ':' */
433 if(state == ST_HEXCOLON) {
434 /* This colon is expected */
435 state = ST_HEXDIGIT1;
436 continue;
437 } else if(state == ST_DIGITS) {
438 /* The colon here means that we have
439 * decoded the first two hexadecimal
440 * places as a decimal value.
441 * Switch decoding mode. */
442 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000443 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700444 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000445 lp = lstart - 1;
446 continue;
447 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400448 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000449 break;
450 }
451 /* [A-Fa-f] */
452 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
453 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
454 switch(state) {
455 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700456 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000457 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700458 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
459 hex_value += 10;
460 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000461 state = ST_HEXDIGIT2;
462 continue;
463 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700464 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
465 hex_value += 10;
466 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000467 state = ST_HEXCOLON;
468 continue;
469 case ST_DIGITS:
470 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000471 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700472 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000473 lp = lstart - 1;
474 continue;
475 default:
476 break;
477 }
478 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000479 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000480
481 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700482 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400483 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700484 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000485 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000486 }
487
Lev Walkinc744a022006-09-15 18:33:25 +0000488 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700489 case ST_END_ENUM:
490 /* Got a complete and valid enumeration encoded as a tag. */
491 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000492 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700493 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700494 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700495 case ST_DIGITS_TRAILSPACE:
496 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700497 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700498 case ASN_STRTOX_OK:
Lev Walkine09f9f12012-09-02 23:06:35 -0700499 break;
Lev Walkin72ec9092017-07-05 05:49:12 -0700500 case ASN_STRTOX_ERROR_RANGE:
Lev Walkine09f9f12012-09-02 23:06:35 -0700501 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700502 case ASN_STRTOX_ERROR_INVAL:
503 case ASN_STRTOX_EXPECT_MORE:
504 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700505 return XPBD_BROKEN_ENCODING;
506 }
Lev Walkinc744a022006-09-15 18:33:25 +0000507 break;
508 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700509 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000510 st->buf[st->size] = 0; /* Just in case termination */
511 return XPBD_BODY_CONSUMED;
512 case ST_HEXDIGIT1:
513 case ST_HEXDIGIT2:
514 case ST_SKIPSPHEX:
515 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700516 case ST_LEADSPACE:
517 /* Content not found */
518 return XPBD_NOT_BODY_IGNORE;
519 case ST_WAITDIGITS:
520 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700521 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
522 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000523 }
Lev Walkind703ff42004-10-21 11:21:25 +0000524
Lev Walkine09f9f12012-09-02 23:06:35 -0700525 /*
526 * Convert the result of parsing of enumeration or a straight
527 * decimal value into a BER representation.
528 */
529 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000530 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000531
Lev Walkin0fab1a62005-03-09 22:19:25 +0000532 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000533}
534
535asn_dec_rval_t
536INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
537 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000538 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000539
540 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000541 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000542 buf_ptr, size, INTEGER__xer_body_decode);
543}
544
Lev Walkina9cc46e2004-09-22 16:06:28 +0000545asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000546INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000547 int ilevel, enum xer_encoder_flags_e flags,
548 asn_app_consume_bytes_f *cb, void *app_key) {
549 const INTEGER_t *st = (const INTEGER_t *)sptr;
550 asn_enc_rval_t er;
551
552 (void)ilevel;
553 (void)flags;
554
Lev Walkind500a962005-11-27 13:06:56 +0000555 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700556 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000557
Lev Walkine0b56e02005-02-25 12:10:27 +0000558 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700559 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000560
Lev Walkin7c1dc052016-03-14 03:08:15 -0700561 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000562}
563
Lev Walkind7703cf2012-09-03 01:45:03 -0700564#ifndef ASN_DISABLE_PER_SUPPORT
565
Lev Walkin59b176e2005-11-26 11:25:14 +0000566asn_dec_rval_t
567INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700568 const asn_per_constraints_t *constraints, void **sptr,
569 asn_per_data_t *pd) {
570 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000571 asn_dec_rval_t rval = { RC_OK, 0 };
572 INTEGER_t *st = (INTEGER_t *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -0700573 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000574 int repeat;
575
576 (void)opt_codec_ctx;
577
578 if(!st) {
579 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700580 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000581 }
582
583 if(!constraints) constraints = td->per_constraints;
584 ct = constraints ? &constraints->value : 0;
585
586 if(ct && ct->flags & APC_EXTENSIBLE) {
587 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700588 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000589 if(inext) ct = 0;
590 }
591
592 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000593 st->buf = 0;
594 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000595 if(ct) {
596 if(ct->flags & APC_SEMI_CONSTRAINED) {
597 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700598 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000599 st->size = 1;
600 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
601 size_t size = (ct->range_bits + 7) >> 3;
602 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700603 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000604 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000606 }
607
Lev Walkin6c527842014-02-09 04:34:54 -0800608 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000609 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800610 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000611 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
612 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800613 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700614 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800615
616 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700617 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800618 if(uper_get_constrained_whole_number(pd,
619 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700620 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800621 ASN_DEBUG("Got value %lu + low %ld",
622 uvalue, ct->lower_bound);
623 uvalue += ct->lower_bound;
624 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700625 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800626 } else {
Lev Walkin72ec9092017-07-05 05:49:12 -0700627 unsigned long svalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800628 if(uper_get_constrained_whole_number(pd,
629 &svalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700630 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800631 ASN_DEBUG("Got value %ld + low %ld",
632 svalue, ct->lower_bound);
633 svalue += ct->lower_bound;
634 if(asn_long2INTEGER(st, svalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700635 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800636 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000637 return rval;
638 }
639 } else {
640 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
641 }
642
643 /* X.691, #12.2.3, #12.2.4 */
644 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700645 ssize_t len = 0;
646 void *p = NULL;
647 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000648
649 /* Get the PER length */
650 len = uper_get_length(pd, -1, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700651 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000652
653 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700654 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000655 st->buf = (uint8_t *)p;
656
657 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700658 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000659 st->size += len;
660 } while(repeat);
661 st->buf[st->size] = 0; /* JIC */
662
663 /* #12.2.3 */
664 if(ct && ct->lower_bound) {
665 /*
666 * TODO: replace by in-place arithmetics.
667 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700668 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000669 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700670 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700671 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700672 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000673 }
674
675 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000676}
677
Lev Walkin523de9e2006-08-18 01:34:18 +0000678asn_enc_rval_t
679INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700680 const asn_per_constraints_t *constraints, void *sptr,
681 asn_per_outp_t *po) {
682 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000683 asn_enc_rval_t er;
684 INTEGER_t *st = (INTEGER_t *)sptr;
685 const uint8_t *buf;
686 const uint8_t *end;
Lev Walkin494fb702017-08-07 20:07:00 -0700687 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000688 long value = 0;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100689 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000690
Lev Walkin7c1dc052016-03-14 03:08:15 -0700691 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000692
693 if(!constraints) constraints = td->per_constraints;
694 ct = constraints ? &constraints->value : 0;
695
696 er.encoded = 0;
697
698 if(ct) {
699 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000700 if(specs && specs->field_unsigned) {
701 unsigned long uval;
702 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700703 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000704 /* Check proper range */
705 if(ct->flags & APC_SEMI_CONSTRAINED) {
706 if(uval < (unsigned long)ct->lower_bound)
707 inext = 1;
708 } else if(ct->range_bits >= 0) {
709 if(uval < (unsigned long)ct->lower_bound
710 || uval > (unsigned long)ct->upper_bound)
711 inext = 1;
712 }
713 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
714 uval, st->buf[0], st->size,
715 ct->lower_bound, ct->upper_bound,
716 inext ? "ext" : "fix");
717 value = uval;
718 } else {
719 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700720 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000721 /* Check proper range */
722 if(ct->flags & APC_SEMI_CONSTRAINED) {
723 if(value < ct->lower_bound)
724 inext = 1;
725 } else if(ct->range_bits >= 0) {
726 if(value < ct->lower_bound
727 || value > ct->upper_bound)
728 inext = 1;
729 }
730 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
731 value, st->buf[0], st->size,
732 ct->lower_bound, ct->upper_bound,
733 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000734 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000735 if(ct->flags & APC_EXTENSIBLE) {
736 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700737 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000738 if(inext) ct = 0;
739 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700740 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000741 }
742 }
743
744
Lev Walkin6c527842014-02-09 04:34:54 -0800745 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000746 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800747 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800748 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
749 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100750 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800751 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700752 ASN__ENCODE_FAILED;
753 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000754 }
755
756 if(ct && ct->lower_bound) {
757 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
758 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700759 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000760 }
761
762 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
763 ssize_t mayEncode = uper_put_length(po, end - buf);
764 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700765 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000766 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700767 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000768 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000769 }
770
Lev Walkin7c1dc052016-03-14 03:08:15 -0700771 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000772}
773
Lev Walkind7703cf2012-09-03 01:45:03 -0700774#endif /* ASN_DISABLE_PER_SUPPORT */
775
Lev Walkinf15320b2004-06-03 03:38:44 +0000776int
Lev Walkin72ec9092017-07-05 05:49:12 -0700777asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000778 uint8_t *b, *end;
779 size_t size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700780 intmax_t value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000781
782 /* Sanity checking */
783 if(!iptr || !iptr->buf || !lptr) {
784 errno = EINVAL;
785 return -1;
786 }
787
788 /* Cache the begin/end of the buffer */
789 b = iptr->buf; /* Start of the INTEGER buffer */
790 size = iptr->size;
791 end = b + size; /* Where to stop */
792
Lev Walkin72ec9092017-07-05 05:49:12 -0700793 if(size > sizeof(value)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000794 uint8_t *end1 = end - 1;
795 /*
796 * Slightly more advanced processing,
Lev Walkin72ec9092017-07-05 05:49:12 -0700797 * able to process INTEGERs with >sizeof(value) bytes
798 * when the actual value is small, e.g. for intmax_t == int32_t
799 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000800 */
801 /* Skip out the insignificant leading bytes */
802 for(; b < end1; b++) {
803 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700804 case 0x00: if((b[1] & 0x80) == 0) continue; break;
805 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000806 }
807 break;
808 }
809
810 size = end - b;
Lev Walkin72ec9092017-07-05 05:49:12 -0700811 if(size > sizeof(value)) {
812 /* Still cannot fit the sizeof(value) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000813 errno = ERANGE;
814 return -1;
815 }
816 }
817
818 /* Shortcut processing of a corner case */
819 if(end == b) {
820 *lptr = 0;
821 return 0;
822 }
823
824 /* Perform the sign initialization */
Lev Walkin72ec9092017-07-05 05:49:12 -0700825 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
826 if((*b >> 7)) value = -1; else value = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000827
828 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700829 for(; b < end; b++) {
830 value = (value << 8) | *b;
831 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000832
Lev Walkin72ec9092017-07-05 05:49:12 -0700833 *lptr = value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000834 return 0;
835}
Lev Walkind703ff42004-10-21 11:21:25 +0000836
Lev Walkin72ec9092017-07-05 05:49:12 -0700837/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000838int
Lev Walkin72ec9092017-07-05 05:49:12 -0700839asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000840 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700841 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000842 size_t size;
843
844 if(!iptr || !iptr->buf || !lptr) {
845 errno = EINVAL;
846 return -1;
847 }
848
849 b = iptr->buf;
850 size = iptr->size;
851 end = b + size;
852
853 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700854 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000855 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700856 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000857 errno = ERANGE;
858 return -1;
859 }
860 }
861
862 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700863 for(value = 0; b < end; b++)
864 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000865
Lev Walkin72ec9092017-07-05 05:49:12 -0700866 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000867 return 0;
868}
869
870int
Lev Walkin72ec9092017-07-05 05:49:12 -0700871asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
872 uint8_t *buf;
873 uint8_t *end;
874 uint8_t *b;
875 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000876
Lev Walkin72ec9092017-07-05 05:49:12 -0700877 if(value <= INTMAX_MAX) {
878 return asn_imax2INTEGER(st, value);
879 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000880
Lev Walkin72ec9092017-07-05 05:49:12 -0700881 buf = (uint8_t *)MALLOC(1 + sizeof(value));
882 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000883
Lev Walkin72ec9092017-07-05 05:49:12 -0700884 end = buf + (sizeof(value) + 1);
885 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
886 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
887 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000888
Lev Walkin72ec9092017-07-05 05:49:12 -0700889 if(st->buf) FREEMEM(st->buf);
890 st->buf = buf;
891 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000892
893 return 0;
894}
895
896int
Lev Walkin72ec9092017-07-05 05:49:12 -0700897asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000898 uint8_t *buf, *bp;
899 uint8_t *p;
900 uint8_t *pstart;
901 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000902 int littleEndian = 1; /* Run-time detection */
903 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000904
905 if(!st) {
906 errno = EINVAL;
907 return -1;
908 }
909
Lev Walkin8471cec2004-10-21 14:02:19 +0000910 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000911 if(!buf) return -1;
912
Lev Walkind7ad5612004-10-26 08:20:46 +0000913 if(*(char *)&littleEndian) {
914 pstart = (uint8_t *)&value + sizeof(value) - 1;
915 pend1 = (uint8_t *)&value;
916 add = -1;
917 } else {
918 pstart = (uint8_t *)&value;
919 pend1 = pstart + sizeof(value) - 1;
920 add = 1;
921 }
922
Lev Walkind703ff42004-10-21 11:21:25 +0000923 /*
924 * If the contents octet consists of more than one octet,
925 * then bits of the first octet and bit 8 of the second octet:
926 * a) shall not all be ones; and
927 * b) shall not all be zero.
928 */
Lev Walkin33700162004-10-26 09:03:31 +0000929 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000930 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000931 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000932 continue;
933 break;
Lev Walkin33700162004-10-26 09:03:31 +0000934 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000935 continue;
936 break;
937 }
938 break;
939 }
940 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700941 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000942 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000943
944 if(st->buf) FREEMEM(st->buf);
945 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000946 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000947
948 return 0;
949}
Lev Walkinb3751942012-09-02 19:36:47 -0700950
Lev Walkin72ec9092017-07-05 05:49:12 -0700951int
952asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
953 intmax_t v;
954 if(asn_INTEGER2imax(iptr, &v) == 0) {
955 if(v < LONG_MIN || v > LONG_MAX) {
956 errno = ERANGE;
957 return -1;
958 }
959 *l = v;
960 return 0;
961 } else {
962 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700963 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700964}
Lev Walkincad560a2013-03-16 07:00:58 -0700965
Lev Walkin72ec9092017-07-05 05:49:12 -0700966int
967asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
968 uintmax_t v;
969 if(asn_INTEGER2umax(iptr, &v) == 0) {
970 if(v > ULONG_MAX) {
971 errno = ERANGE;
972 return -1;
973 }
974 *l = v;
975 return 0;
976 } else {
977 return -1;
978 }
979}
980
981int
982asn_long2INTEGER(INTEGER_t *st, long value) {
983 return asn_imax2INTEGER(st, value);
984}
985
986int
987asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
988 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -0700989}
990
991/*
992 * Parse the number in the given string until the given *end position,
993 * returning the position after the last parsed character back using the
994 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -0700995 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -0700996 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700997enum asn_strtox_result_e
998asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700999 int sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001000 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -07001001
Lev Walkin72ec9092017-07-05 05:49:12 -07001002 const intmax_t upper_boundary = INTMAX_MAX / 10;
1003 intmax_t last_digit_max = INTMAX_MAX % 10;
Lev Walkine09f9f12012-09-02 23:06:35 -07001004
Lev Walkin72ec9092017-07-05 05:49:12 -07001005 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001006
1007 switch(*str) {
1008 case '-':
1009 last_digit_max++;
1010 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001011 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001012 case '+':
1013 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001014 if(str >= *end) {
1015 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001016 return ASN_STRTOX_EXPECT_MORE;
Lev Walkincad560a2013-03-16 07:00:58 -07001017 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001018 }
1019
Lev Walkin72ec9092017-07-05 05:49:12 -07001020 for(value = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001021 switch(*str) {
1022 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1023 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1024 int d = *str - '0';
Lev Walkin72ec9092017-07-05 05:49:12 -07001025 if(value < upper_boundary) {
1026 value = value * 10 + d;
1027 } else if(value == upper_boundary) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001028 if(d <= last_digit_max) {
1029 if(sign > 0) {
Lev Walkin72ec9092017-07-05 05:49:12 -07001030 value = value * 10 + d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001031 } else {
1032 sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001033 value = -value * 10 - d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001034 }
1035 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001036 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001037 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001038 }
1039 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001040 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001041 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001042 }
1043 }
1044 continue;
1045 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001046 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001047 *intp = sign * value;
1048 return ASN_STRTOX_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001049 }
1050 }
1051
Lev Walkincad560a2013-03-16 07:00:58 -07001052 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001053 *intp = sign * value;
1054 return ASN_STRTOX_OK;
1055}
1056
1057enum asn_strtox_result_e
1058asn_strtol_lim(const char *str, const char **end, long *lp) {
1059 intmax_t value;
1060 switch(asn_strtoimax_lim(str, end, &value)) {
1061 case ASN_STRTOX_ERROR_RANGE:
1062 return ASN_STRTOX_ERROR_RANGE;
1063 case ASN_STRTOX_ERROR_INVAL:
1064 return ASN_STRTOX_ERROR_INVAL;
1065 case ASN_STRTOX_EXPECT_MORE:
1066 return ASN_STRTOX_EXPECT_MORE;
1067 case ASN_STRTOX_OK:
1068 if(value >= LONG_MIN && value <= LONG_MAX) {
1069 *lp = value;
1070 return ASN_STRTOX_OK;
1071 } else {
1072 return ASN_STRTOX_ERROR_RANGE;
1073 }
1074 case ASN_STRTOX_EXTRA_DATA:
1075 if(value >= LONG_MIN && value <= LONG_MAX) {
1076 *lp = value;
1077 return ASN_STRTOX_EXTRA_DATA;
1078 } else {
1079 return ASN_STRTOX_ERROR_RANGE;
1080 }
1081 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001082
1083 assert(!"Unreachable");
1084 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001085}
1086