blob: a930b0a65e20be2f6ba736bc2d15262f4d561146 [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};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080017asn_TYPE_operation_t asn_OP_INTEGER = {
Lev Walkin8077f932017-08-07 17:27:43 -070018 INTEGER_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000019 INTEGER_print,
Bi-Ruei, Chiue1bf2192017-08-19 00:55:25 +080020 INTEGER_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000021 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000022 ber_decode_primitive,
Lev Walkinf15320b2004-06-03 03:38:44 +000023 INTEGER_encode_der,
Lev Walkind703ff42004-10-21 11:21:25 +000024 INTEGER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000025 INTEGER_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070026#ifdef ASN_DISABLE_OER_SUPPORT
27 0,
28 0,
29#else
Lev Walkin527497f2017-07-14 08:56:36 +040030 INTEGER_decode_oer, /* OER decoder */
31 INTEGER_encode_oer, /* Canonical OER encoder */
Lev Walkincc159472017-07-06 08:26:36 -070032#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040033#ifdef ASN_DISABLE_PER_SUPPORT
34 0,
35 0,
36#else
37 INTEGER_decode_uper, /* Unaligned PER decoder */
38 INTEGER_encode_uper, /* Unaligned PER encoder */
39#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080040 0 /* Use generic outmost tag fetcher */
41};
42asn_TYPE_descriptor_t asn_DEF_INTEGER = {
43 "INTEGER",
44 "INTEGER",
45 &asn_OP_INTEGER,
46 asn_generic_no_constraint,
Lev Walkin5e033762004-09-29 13:26:15 +000047 asn_DEF_INTEGER_tags,
48 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
49 asn_DEF_INTEGER_tags, /* Same as above */
50 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070051 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000052 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000053 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000054 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000055};
56
57/*
Lev Walkinf15320b2004-06-03 03:38:44 +000058 * Encode INTEGER type using DER.
59 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000060asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000061INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000062 int tag_mode, ber_tlv_tag_t tag,
63 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000064 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000065
66 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000067 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000068
69 /*
70 * Canonicalize integer in the buffer.
71 * (Remove too long sign extension, remove some first 0x00 bytes)
72 */
73 if(st->buf) {
74 uint8_t *buf = st->buf;
75 uint8_t *end1 = buf + st->size - 1;
76 int shift;
77
78 /* Compute the number of superfluous leading bytes */
79 for(; buf < end1; buf++) {
80 /*
81 * If the contents octets of an integer value encoding
82 * consist of more than one octet, then the bits of the
83 * first octet and bit 8 of the second octet:
84 * a) shall not all be ones; and
85 * b) shall not all be zero.
86 */
87 switch(*buf) {
88 case 0x00: if((buf[1] & 0x80) == 0)
89 continue;
90 break;
91 case 0xff: if((buf[1] & 0x80))
92 continue;
93 break;
94 }
95 break;
96 }
97
98 /* Remove leading superfluous bytes from the integer */
99 shift = buf - st->buf;
100 if(shift) {
101 uint8_t *nb = st->buf;
102 uint8_t *end;
103
104 st->size -= shift; /* New size, minus bad bytes */
105 end = nb + st->size;
106
107 for(; nb < end; nb++, buf++)
108 *nb = *buf;
109 }
110
111 } /* if(1) */
112
Lev Walkin8e8078a2004-09-26 13:10:40 +0000113 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000114}
115
Lev Walkind62a6622017-08-22 02:31:02 -0700116static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(
117 const asn_INTEGER_specifics_t *specs, const char *lstart,
118 const char *lstop);
Lev Walkine0b56e02005-02-25 12:10:27 +0000119
Lev Walkinf15320b2004-06-03 03:38:44 +0000120/*
121 * INTEGER specific human-readable output.
122 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000123static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700124INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
Lev Walkind62a6622017-08-22 02:31:02 -0700125 const asn_INTEGER_specifics_t *specs =
126 (const asn_INTEGER_specifics_t *)td->specifics;
127 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000128 uint8_t *buf = st->buf;
129 uint8_t *buf_end = st->buf + st->size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700130 intmax_t value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000131 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000132 char *p;
133 int ret;
134
Lev Walkin97f8edc2013-03-28 04:38:41 -0700135 if(specs && specs->field_unsigned)
Lev Walkin72ec9092017-07-05 05:49:12 -0700136 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
Lev Walkin97f8edc2013-03-28 04:38:41 -0700137 else
Lev Walkin72ec9092017-07-05 05:49:12 -0700138 ret = asn_INTEGER2imax(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000139
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700141 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000142 const asn_INTEGER_enum_map_t *el;
143 size_t scrsize;
144 char *scr;
145
Lev Walkin97f8edc2013-03-28 04:38:41 -0700146 el = (value >= 0 || !specs || !specs->field_unsigned)
147 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000148 if(el) {
149 scrsize = el->enum_len + 32;
150 scr = (char *)alloca(scrsize);
151 if(plainOrXER == 0)
152 ret = snprintf(scr, scrsize,
Lev Walkin72ec9092017-07-05 05:49:12 -0700153 "%" PRIdMAX " (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000154 else
155 ret = snprintf(scr, scrsize,
156 "<%s/>", el->enum_name);
157 } else if(plainOrXER && specs && specs->strict_enumeration) {
158 ASN_DEBUG("ASN.1 forbids dealing with "
159 "unknown value of ENUMERATED type");
160 errno = EPERM;
161 return -1;
162 } else {
163 scrsize = sizeof(scratch);
164 scr = scratch;
Lev Walkin72ec9092017-07-05 05:49:12 -0700165 ret = snprintf(
166 scr, scrsize,
167 (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX,
168 value);
169 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000170 assert(ret > 0 && (size_t)ret < scrsize);
171 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
172 } else if(plainOrXER && specs && specs->strict_enumeration) {
173 /*
174 * Here and earlier, we cannot encode the ENUMERATED values
175 * if there is no corresponding identifier.
176 */
177 ASN_DEBUG("ASN.1 forbids dealing with "
178 "unknown value of ENUMERATED type");
179 errno = EPERM;
180 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 }
182
183 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000184 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700186 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000187 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000189 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000191 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 p = scratch;
193 }
194 *p++ = h2c[*buf >> 4];
195 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000196 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 }
Lev Walkindb13f512004-07-19 17:30:25 +0000198 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000199 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000200
Lev Walkina9cc46e2004-09-22 16:06:28 +0000201 wrote += p - scratch;
202 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
203}
204
205/*
206 * INTEGER specific human-readable output.
207 */
208int
Lev Walkin5e033762004-09-29 13:26:15 +0000209INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000210 asn_app_consume_bytes_f *cb, void *app_key) {
211 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000212 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000213
214 (void)td;
215 (void)ilevel;
216
Lev Walkind500a962005-11-27 13:06:56 +0000217 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000218 ret = cb("<absent>", 8, app_key);
219 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000220 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000221
Lev Walkin8e8078a2004-09-26 13:10:40 +0000222 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000223}
224
Lev Walkine0b56e02005-02-25 12:10:27 +0000225struct e2v_key {
226 const char *start;
227 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700228 const asn_INTEGER_enum_map_t *vemap;
229 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000230};
231static int
232INTEGER__compar_enum2value(const void *kp, const void *am) {
233 const struct e2v_key *key = (const struct e2v_key *)kp;
234 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
235 const char *ptr, *end, *name;
236
237 /* Remap the element (sort by different criterion) */
238 el = key->vemap + key->evmap[el - key->vemap];
239
240 /* Compare strings */
241 for(ptr = key->start, end = key->stop, name = el->enum_name;
242 ptr < end; ptr++, name++) {
243 if(*ptr != *name)
244 return *(const unsigned char *)ptr
245 - *(const unsigned char *)name;
246 }
247 return name[0] ? -1 : 0;
248}
249
250static const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700251INTEGER_map_enum2value(const asn_INTEGER_specifics_t *specs, const char *lstart,
252 const char *lstop) {
253 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000254 int count = specs ? specs->map_count : 0;
255 struct e2v_key key;
256 const char *lp;
257
258 if(!count) return NULL;
259
260 /* Guaranteed: assert(lstart < lstop); */
261 /* Figure out the tag name */
262 for(lstart++, lp = lstart; lp < lstop; lp++) {
263 switch(*lp) {
264 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
265 case 0x2f: /* '/' */ case 0x3e: /* '>' */
266 break;
267 default:
268 continue;
269 }
270 break;
271 }
272 if(lp == lstop) return NULL; /* No tag found */
273 lstop = lp;
274
275 key.start = lstart;
276 key.stop = lstop;
277 key.vemap = specs->value2enum;
278 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000279 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
280 specs->value2enum, count, sizeof(specs->value2enum[0]),
281 INTEGER__compar_enum2value);
282 if(el_found) {
283 /* Remap enum2value into value2enum */
284 el_found = key.vemap + key.evmap[el_found - key.vemap];
285 }
286 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000287}
288
289static int
290INTEGER__compar_value2enum(const void *kp, const void *am) {
291 long a = *(const long *)kp;
292 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
293 long b = el->nat_value;
294 if(a < b) return -1;
295 else if(a == b) return 0;
296 else return 1;
297}
298
Lev Walkinc2350112005-03-29 17:19:53 +0000299const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700300INTEGER_map_value2enum(const asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000301 int count = specs ? specs->map_count : 0;
302 if(!count) return 0;
303 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
304 count, sizeof(specs->value2enum[0]),
305 INTEGER__compar_value2enum);
306}
307
Lev Walkinc744a022006-09-15 18:33:25 +0000308static int
309INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
310 void *p = MALLOC(min_size + 1);
311 if(p) {
312 void *b = st->buf;
313 st->size = 0;
314 st->buf = p;
315 FREEMEM(b);
316 return 0;
317 } else {
318 return -1;
319 }
320}
321
Lev Walkind703ff42004-10-21 11:21:25 +0000322/*
323 * Decode the chunk of XML text encoding INTEGER.
324 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000325static enum xer_pbd_rval
326INTEGER__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 +0000327 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700328 intmax_t dec_value;
329 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000330 const char *lp;
331 const char *lstart = (const char *)chunk_buf;
332 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000333 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700334 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000335 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000336 ST_WAITDIGITS,
337 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700338 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000339 ST_HEXDIGIT1,
340 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700341 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000342 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700343 ST_END_ENUM,
344 ST_UNEXPECTED
345 } state = ST_LEADSPACE;
346 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
347 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000348
Lev Walkinc744a022006-09-15 18:33:25 +0000349 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000350 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
351 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000352
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000353 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
354 return XPBD_SYSTEM_FAILURE;
355
Lev Walkind703ff42004-10-21 11:21:25 +0000356 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000357 * We may have received a tag here. It will be processed inline.
358 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000359 */
Lev Walkinb3751942012-09-02 19:36:47 -0700360 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000361 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000362 switch(lv) {
363 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000364 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700365 case ST_LEADSPACE:
366 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700367 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000368 case ST_SKIPSPHEX:
369 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700370 case ST_DIGITS:
371 dec_value_end = lp;
372 state = ST_DIGITS_TRAILSPACE;
373 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700374 case ST_HEXCOLON:
375 state = ST_HEXDIGITS_TRAILSPACE;
376 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000377 default:
378 break;
379 }
Lev Walkind703ff42004-10-21 11:21:25 +0000380 break;
381 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700382 if(state == ST_LEADSPACE) {
383 dec_value = 0;
384 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000385 state = ST_WAITDIGITS;
386 continue;
387 }
388 break;
389 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700390 if(state == ST_LEADSPACE) {
391 dec_value = 0;
392 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000393 state = ST_WAITDIGITS;
394 continue;
395 }
396 break;
397 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
398 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000399 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700400 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000401 case ST_SKIPSPHEX: /* Fall through */
402 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700403 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000404 state = ST_HEXDIGIT2;
405 continue;
406 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700407 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000408 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700409 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000410 continue;
411 case ST_HEXCOLON:
412 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700413 case ST_LEADSPACE:
414 dec_value = 0;
415 dec_value_start = lp;
416 /* FALL THROUGH */
417 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000418 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700419 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700420 default:
421 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000422 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700423 break;
424 case 0x3c: /* '<', start of XML encoded enumeration */
425 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000426 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000427 el = INTEGER_map_enum2value(
Lev Walkind62a6622017-08-22 02:31:02 -0700428 (const asn_INTEGER_specifics_t *)
Lev Walkine0b56e02005-02-25 12:10:27 +0000429 td->specifics, lstart, lstop);
430 if(el) {
431 ASN_DEBUG("Found \"%s\" => %ld",
432 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700433 dec_value = el->nat_value;
434 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000435 lp = lstop - 1;
436 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000437 }
438 ASN_DEBUG("Unknown identifier for INTEGER");
439 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000440 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000441 case 0x3a: /* ':' */
442 if(state == ST_HEXCOLON) {
443 /* This colon is expected */
444 state = ST_HEXDIGIT1;
445 continue;
446 } else if(state == ST_DIGITS) {
447 /* The colon here means that we have
448 * decoded the first two hexadecimal
449 * places as a decimal value.
450 * Switch decoding mode. */
451 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000452 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700453 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000454 lp = lstart - 1;
455 continue;
456 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400457 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000458 break;
459 }
460 /* [A-Fa-f] */
461 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
462 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
463 switch(state) {
464 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700465 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000466 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700467 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
468 hex_value += 10;
469 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000470 state = ST_HEXDIGIT2;
471 continue;
472 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700473 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
474 hex_value += 10;
475 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000476 state = ST_HEXCOLON;
477 continue;
478 case ST_DIGITS:
479 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000480 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700481 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000482 lp = lstart - 1;
483 continue;
484 default:
485 break;
486 }
487 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000488 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000489
490 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700491 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400492 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700493 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000494 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000495 }
496
Lev Walkinc744a022006-09-15 18:33:25 +0000497 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700498 case ST_END_ENUM:
499 /* Got a complete and valid enumeration encoded as a tag. */
500 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000501 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700502 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700503 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700504 case ST_DIGITS_TRAILSPACE:
505 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700506 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700507 case ASN_STRTOX_OK:
Lev Walkine09f9f12012-09-02 23:06:35 -0700508 break;
Lev Walkin72ec9092017-07-05 05:49:12 -0700509 case ASN_STRTOX_ERROR_RANGE:
Lev Walkine09f9f12012-09-02 23:06:35 -0700510 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700511 case ASN_STRTOX_ERROR_INVAL:
512 case ASN_STRTOX_EXPECT_MORE:
513 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700514 return XPBD_BROKEN_ENCODING;
515 }
Lev Walkinc744a022006-09-15 18:33:25 +0000516 break;
517 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700518 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000519 st->buf[st->size] = 0; /* Just in case termination */
520 return XPBD_BODY_CONSUMED;
521 case ST_HEXDIGIT1:
522 case ST_HEXDIGIT2:
523 case ST_SKIPSPHEX:
524 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700525 case ST_LEADSPACE:
526 /* Content not found */
527 return XPBD_NOT_BODY_IGNORE;
528 case ST_WAITDIGITS:
529 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700530 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
531 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000532 }
Lev Walkind703ff42004-10-21 11:21:25 +0000533
Lev Walkine09f9f12012-09-02 23:06:35 -0700534 /*
535 * Convert the result of parsing of enumeration or a straight
536 * decimal value into a BER representation.
537 */
538 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000539 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000540
Lev Walkin0fab1a62005-03-09 22:19:25 +0000541 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000542}
543
544asn_dec_rval_t
545INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
546 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000547 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000548
549 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000550 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000551 buf_ptr, size, INTEGER__xer_body_decode);
552}
553
Lev Walkina9cc46e2004-09-22 16:06:28 +0000554asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000555INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000556 int ilevel, enum xer_encoder_flags_e flags,
557 asn_app_consume_bytes_f *cb, void *app_key) {
558 const INTEGER_t *st = (const INTEGER_t *)sptr;
559 asn_enc_rval_t er;
560
561 (void)ilevel;
562 (void)flags;
563
Lev Walkind500a962005-11-27 13:06:56 +0000564 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700565 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000566
Lev Walkine0b56e02005-02-25 12:10:27 +0000567 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700568 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000569
Lev Walkin7c1dc052016-03-14 03:08:15 -0700570 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000571}
572
Lev Walkind7703cf2012-09-03 01:45:03 -0700573#ifndef ASN_DISABLE_PER_SUPPORT
574
Lev Walkin59b176e2005-11-26 11:25:14 +0000575asn_dec_rval_t
576INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700577 const asn_per_constraints_t *constraints, void **sptr,
578 asn_per_data_t *pd) {
Lev Walkind62a6622017-08-22 02:31:02 -0700579 const asn_INTEGER_specifics_t *specs =
580 (const asn_INTEGER_specifics_t *)td->specifics;
581 asn_dec_rval_t rval = { RC_OK, 0 };
Lev Walkin59b176e2005-11-26 11:25:14 +0000582 INTEGER_t *st = (INTEGER_t *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -0700583 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000584 int repeat;
585
586 (void)opt_codec_ctx;
587
588 if(!st) {
589 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700590 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000591 }
592
593 if(!constraints) constraints = td->per_constraints;
594 ct = constraints ? &constraints->value : 0;
595
596 if(ct && ct->flags & APC_EXTENSIBLE) {
597 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700598 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000599 if(inext) ct = 0;
600 }
601
602 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000603 st->buf = 0;
604 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 if(ct) {
606 if(ct->flags & APC_SEMI_CONSTRAINED) {
607 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700608 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000609 st->size = 1;
610 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
611 size_t size = (ct->range_bits + 7) >> 3;
612 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700613 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000614 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000615 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000616 }
617
Lev Walkin6c527842014-02-09 04:34:54 -0800618 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000619 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800620 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000621 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
622 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800623 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700624 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800625
626 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700627 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800628 if(uper_get_constrained_whole_number(pd,
629 &uvalue, 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 %lu + low %ld",
632 uvalue, ct->lower_bound);
633 uvalue += ct->lower_bound;
634 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700635 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800636 } else {
Lev Walkin72ec9092017-07-05 05:49:12 -0700637 unsigned long svalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800638 if(uper_get_constrained_whole_number(pd,
639 &svalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700640 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800641 ASN_DEBUG("Got value %ld + low %ld",
642 svalue, ct->lower_bound);
643 svalue += ct->lower_bound;
644 if(asn_long2INTEGER(st, svalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700645 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800646 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000647 return rval;
648 }
649 } else {
650 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
651 }
652
653 /* X.691, #12.2.3, #12.2.4 */
654 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700655 ssize_t len = 0;
656 void *p = NULL;
657 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000658
659 /* Get the PER length */
660 len = uper_get_length(pd, -1, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700661 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000662
663 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700664 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000665 st->buf = (uint8_t *)p;
666
667 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700668 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000669 st->size += len;
670 } while(repeat);
671 st->buf[st->size] = 0; /* JIC */
672
673 /* #12.2.3 */
674 if(ct && ct->lower_bound) {
675 /*
676 * TODO: replace by in-place arithmetics.
677 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700678 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000679 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700680 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700681 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700682 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000683 }
684
685 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000686}
687
Lev Walkin523de9e2006-08-18 01:34:18 +0000688asn_enc_rval_t
689INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700690 const asn_per_constraints_t *constraints, void *sptr,
691 asn_per_outp_t *po) {
Lev Walkind62a6622017-08-22 02:31:02 -0700692 const asn_INTEGER_specifics_t *specs =
693 (const asn_INTEGER_specifics_t *)td->specifics;
694 asn_enc_rval_t er;
Lev Walkin523de9e2006-08-18 01:34:18 +0000695 INTEGER_t *st = (INTEGER_t *)sptr;
696 const uint8_t *buf;
697 const uint8_t *end;
Lev Walkin494fb702017-08-07 20:07:00 -0700698 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000699 long value = 0;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100700 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000701
Lev Walkin7c1dc052016-03-14 03:08:15 -0700702 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000703
704 if(!constraints) constraints = td->per_constraints;
705 ct = constraints ? &constraints->value : 0;
706
707 er.encoded = 0;
708
709 if(ct) {
710 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000711 if(specs && specs->field_unsigned) {
712 unsigned long uval;
713 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700714 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000715 /* Check proper range */
716 if(ct->flags & APC_SEMI_CONSTRAINED) {
717 if(uval < (unsigned long)ct->lower_bound)
718 inext = 1;
719 } else if(ct->range_bits >= 0) {
720 if(uval < (unsigned long)ct->lower_bound
721 || uval > (unsigned long)ct->upper_bound)
722 inext = 1;
723 }
724 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
725 uval, st->buf[0], st->size,
726 ct->lower_bound, ct->upper_bound,
727 inext ? "ext" : "fix");
728 value = uval;
729 } else {
730 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700731 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000732 /* Check proper range */
733 if(ct->flags & APC_SEMI_CONSTRAINED) {
734 if(value < ct->lower_bound)
735 inext = 1;
736 } else if(ct->range_bits >= 0) {
737 if(value < ct->lower_bound
738 || value > ct->upper_bound)
739 inext = 1;
740 }
741 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
742 value, st->buf[0], st->size,
743 ct->lower_bound, ct->upper_bound,
744 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000745 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000746 if(ct->flags & APC_EXTENSIBLE) {
747 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700748 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000749 if(inext) ct = 0;
750 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700751 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000752 }
753 }
754
755
Lev Walkin6c527842014-02-09 04:34:54 -0800756 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000757 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800758 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800759 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
760 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100761 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800762 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700763 ASN__ENCODE_FAILED;
764 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000765 }
766
767 if(ct && ct->lower_bound) {
768 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
769 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700770 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000771 }
772
773 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
774 ssize_t mayEncode = uper_put_length(po, end - buf);
775 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700776 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000777 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700778 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000779 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000780 }
781
Lev Walkin7c1dc052016-03-14 03:08:15 -0700782 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000783}
784
Lev Walkind7703cf2012-09-03 01:45:03 -0700785#endif /* ASN_DISABLE_PER_SUPPORT */
786
Lev Walkinf15320b2004-06-03 03:38:44 +0000787int
Lev Walkin72ec9092017-07-05 05:49:12 -0700788asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000789 uint8_t *b, *end;
790 size_t size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700791 intmax_t value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000792
793 /* Sanity checking */
794 if(!iptr || !iptr->buf || !lptr) {
795 errno = EINVAL;
796 return -1;
797 }
798
799 /* Cache the begin/end of the buffer */
800 b = iptr->buf; /* Start of the INTEGER buffer */
801 size = iptr->size;
802 end = b + size; /* Where to stop */
803
Lev Walkin72ec9092017-07-05 05:49:12 -0700804 if(size > sizeof(value)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000805 uint8_t *end1 = end - 1;
806 /*
807 * Slightly more advanced processing,
Lev Walkin72ec9092017-07-05 05:49:12 -0700808 * able to process INTEGERs with >sizeof(value) bytes
809 * when the actual value is small, e.g. for intmax_t == int32_t
810 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000811 */
812 /* Skip out the insignificant leading bytes */
813 for(; b < end1; b++) {
814 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700815 case 0x00: if((b[1] & 0x80) == 0) continue; break;
816 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000817 }
818 break;
819 }
820
821 size = end - b;
Lev Walkin72ec9092017-07-05 05:49:12 -0700822 if(size > sizeof(value)) {
823 /* Still cannot fit the sizeof(value) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000824 errno = ERANGE;
825 return -1;
826 }
827 }
828
829 /* Shortcut processing of a corner case */
830 if(end == b) {
831 *lptr = 0;
832 return 0;
833 }
834
835 /* Perform the sign initialization */
Lev Walkin72ec9092017-07-05 05:49:12 -0700836 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
837 if((*b >> 7)) value = -1; else value = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000838
839 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700840 for(; b < end; b++) {
841 value = (value << 8) | *b;
842 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000843
Lev Walkin72ec9092017-07-05 05:49:12 -0700844 *lptr = value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000845 return 0;
846}
Lev Walkind703ff42004-10-21 11:21:25 +0000847
Lev Walkin72ec9092017-07-05 05:49:12 -0700848/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000849int
Lev Walkin72ec9092017-07-05 05:49:12 -0700850asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000851 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700852 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000853 size_t size;
854
855 if(!iptr || !iptr->buf || !lptr) {
856 errno = EINVAL;
857 return -1;
858 }
859
860 b = iptr->buf;
861 size = iptr->size;
862 end = b + size;
863
864 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700865 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000866 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700867 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000868 errno = ERANGE;
869 return -1;
870 }
871 }
872
873 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700874 for(value = 0; b < end; b++)
875 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000876
Lev Walkin72ec9092017-07-05 05:49:12 -0700877 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000878 return 0;
879}
880
881int
Lev Walkin72ec9092017-07-05 05:49:12 -0700882asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
883 uint8_t *buf;
884 uint8_t *end;
885 uint8_t *b;
886 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000887
Lev Walkin72ec9092017-07-05 05:49:12 -0700888 if(value <= INTMAX_MAX) {
889 return asn_imax2INTEGER(st, value);
890 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000891
Lev Walkin72ec9092017-07-05 05:49:12 -0700892 buf = (uint8_t *)MALLOC(1 + sizeof(value));
893 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000894
Lev Walkin72ec9092017-07-05 05:49:12 -0700895 end = buf + (sizeof(value) + 1);
896 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
897 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
898 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000899
Lev Walkin72ec9092017-07-05 05:49:12 -0700900 if(st->buf) FREEMEM(st->buf);
901 st->buf = buf;
902 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000903
904 return 0;
905}
906
907int
Lev Walkin72ec9092017-07-05 05:49:12 -0700908asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000909 uint8_t *buf, *bp;
910 uint8_t *p;
911 uint8_t *pstart;
912 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000913 int littleEndian = 1; /* Run-time detection */
914 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000915
916 if(!st) {
917 errno = EINVAL;
918 return -1;
919 }
920
Lev Walkin8471cec2004-10-21 14:02:19 +0000921 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000922 if(!buf) return -1;
923
Lev Walkind7ad5612004-10-26 08:20:46 +0000924 if(*(char *)&littleEndian) {
925 pstart = (uint8_t *)&value + sizeof(value) - 1;
926 pend1 = (uint8_t *)&value;
927 add = -1;
928 } else {
929 pstart = (uint8_t *)&value;
930 pend1 = pstart + sizeof(value) - 1;
931 add = 1;
932 }
933
Lev Walkind703ff42004-10-21 11:21:25 +0000934 /*
935 * If the contents octet consists of more than one octet,
936 * then bits of the first octet and bit 8 of the second octet:
937 * a) shall not all be ones; and
938 * b) shall not all be zero.
939 */
Lev Walkin33700162004-10-26 09:03:31 +0000940 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000941 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000942 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000943 continue;
944 break;
Lev Walkin33700162004-10-26 09:03:31 +0000945 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000946 continue;
947 break;
948 }
949 break;
950 }
951 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700952 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000953 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000954
955 if(st->buf) FREEMEM(st->buf);
956 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000957 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000958
959 return 0;
960}
Lev Walkinb3751942012-09-02 19:36:47 -0700961
Lev Walkin72ec9092017-07-05 05:49:12 -0700962int
963asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
964 intmax_t v;
965 if(asn_INTEGER2imax(iptr, &v) == 0) {
966 if(v < LONG_MIN || v > LONG_MAX) {
967 errno = ERANGE;
968 return -1;
969 }
970 *l = v;
971 return 0;
972 } else {
973 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700974 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700975}
Lev Walkincad560a2013-03-16 07:00:58 -0700976
Lev Walkin72ec9092017-07-05 05:49:12 -0700977int
978asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
979 uintmax_t v;
980 if(asn_INTEGER2umax(iptr, &v) == 0) {
981 if(v > ULONG_MAX) {
982 errno = ERANGE;
983 return -1;
984 }
985 *l = v;
986 return 0;
987 } else {
988 return -1;
989 }
990}
991
992int
993asn_long2INTEGER(INTEGER_t *st, long value) {
994 return asn_imax2INTEGER(st, value);
995}
996
997int
998asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
999 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -07001000}
1001
1002/*
1003 * Parse the number in the given string until the given *end position,
1004 * returning the position after the last parsed character back using the
1005 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -07001006 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -07001007 */
Lev Walkin72ec9092017-07-05 05:49:12 -07001008enum asn_strtox_result_e
1009asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001010 int sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001011 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -07001012
Lev Walkin72ec9092017-07-05 05:49:12 -07001013 const intmax_t upper_boundary = INTMAX_MAX / 10;
1014 intmax_t last_digit_max = INTMAX_MAX % 10;
Lev Walkine09f9f12012-09-02 23:06:35 -07001015
Lev Walkin72ec9092017-07-05 05:49:12 -07001016 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001017
1018 switch(*str) {
1019 case '-':
1020 last_digit_max++;
1021 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001022 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001023 case '+':
1024 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001025 if(str >= *end) {
1026 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001027 return ASN_STRTOX_EXPECT_MORE;
Lev Walkincad560a2013-03-16 07:00:58 -07001028 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001029 }
1030
Lev Walkin72ec9092017-07-05 05:49:12 -07001031 for(value = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001032 switch(*str) {
1033 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1034 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1035 int d = *str - '0';
Lev Walkin72ec9092017-07-05 05:49:12 -07001036 if(value < upper_boundary) {
1037 value = value * 10 + d;
1038 } else if(value == upper_boundary) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001039 if(d <= last_digit_max) {
1040 if(sign > 0) {
Lev Walkin72ec9092017-07-05 05:49:12 -07001041 value = value * 10 + d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001042 } else {
1043 sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001044 value = -value * 10 - d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001045 }
1046 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001047 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001048 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001049 }
1050 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001051 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001052 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001053 }
1054 }
1055 continue;
1056 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001057 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001058 *intp = sign * value;
1059 return ASN_STRTOX_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001060 }
1061 }
1062
Lev Walkincad560a2013-03-16 07:00:58 -07001063 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001064 *intp = sign * value;
1065 return ASN_STRTOX_OK;
1066}
1067
1068enum asn_strtox_result_e
1069asn_strtol_lim(const char *str, const char **end, long *lp) {
1070 intmax_t value;
1071 switch(asn_strtoimax_lim(str, end, &value)) {
1072 case ASN_STRTOX_ERROR_RANGE:
1073 return ASN_STRTOX_ERROR_RANGE;
1074 case ASN_STRTOX_ERROR_INVAL:
1075 return ASN_STRTOX_ERROR_INVAL;
1076 case ASN_STRTOX_EXPECT_MORE:
1077 return ASN_STRTOX_EXPECT_MORE;
1078 case ASN_STRTOX_OK:
1079 if(value >= LONG_MIN && value <= LONG_MAX) {
1080 *lp = value;
1081 return ASN_STRTOX_OK;
1082 } else {
1083 return ASN_STRTOX_ERROR_RANGE;
1084 }
1085 case ASN_STRTOX_EXTRA_DATA:
1086 if(value >= LONG_MIN && value <= LONG_MAX) {
1087 *lp = value;
1088 return ASN_STRTOX_EXTRA_DATA;
1089 } else {
1090 return ASN_STRTOX_ERROR_RANGE;
1091 }
1092 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001093
1094 assert(!"Unreachable");
1095 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001096}
1097
Lev Walkincd2f48e2017-08-10 02:14:59 -07001098int
1099INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1100 const void *bptr) {
1101 const INTEGER_t *a = aptr;
1102 const INTEGER_t *b = bptr;
1103
1104 (void)td;
1105
1106 if(a && b) {
1107 if(a->size && b->size) {
1108 int sign_a = (a->buf[0] & 0x80) ? -1 : 1;
1109 int sign_b = (b->buf[0] & 0x80) ? -1 : 1;
1110
1111 if(sign_a < sign_b) return -1;
1112 if(sign_a > sign_b) return 1;
1113
1114 /* The shortest integer wins, unless comparing negatives */
1115 if(a->size < b->size) {
1116 return -1 * sign_a;
1117 } else if(a->size > b->size) {
1118 return 1 * sign_b;
1119 }
1120
1121 return sign_a * memcmp(a->buf, b->buf, a->size);
1122 } else if(a->size) {
1123 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1124 return (1) * sign;
1125 } else if(b->size) {
1126 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1127 return (-1) * sign;
1128 } else {
1129 return 0;
1130 }
1131 } else if(!a && !b) {
1132 return 0;
1133 } else if(!a) {
1134 return -1;
1135 } else {
1136 return 1;
1137 }
1138
1139}
1140