blob: 2b43cdf7c9193cecdd11e71fa8cb9022f5c4c38e [file] [log] [blame]
Lev Walkin6495ca52019-08-10 20:07:39 -07001/*
2 * Copyright (c) 2003-2019 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 Walkin8e8078a2004-09-26 13:10:40 +000021 ber_decode_primitive,
Lev Walkinf15320b2004-06-03 03:38:44 +000022 INTEGER_encode_der,
Lev Walkind703ff42004-10-21 11:21:25 +000023 INTEGER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000024 INTEGER_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070025#ifdef ASN_DISABLE_OER_SUPPORT
26 0,
27 0,
28#else
Lev Walkin527497f2017-07-14 08:56:36 +040029 INTEGER_decode_oer, /* OER decoder */
30 INTEGER_encode_oer, /* Canonical OER encoder */
Lev Walkincc159472017-07-06 08:26:36 -070031#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040032#ifdef ASN_DISABLE_PER_SUPPORT
33 0,
34 0,
35#else
36 INTEGER_decode_uper, /* Unaligned PER decoder */
37 INTEGER_encode_uper, /* Unaligned PER encoder */
38#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -070039 INTEGER_random_fill,
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,
Lev Walkin5e033762004-09-29 13:26:15 +000046 asn_DEF_INTEGER_tags,
47 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
48 asn_DEF_INTEGER_tags, /* Same as above */
49 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkina5972be2017-09-29 23:15:58 -070050 { 0, 0, asn_generic_no_constraint },
Lev Walkin449f8322004-08-20 13:23:42 +000051 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000052 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000053};
54
55/*
Lev Walkinf15320b2004-06-03 03:38:44 +000056 * Encode INTEGER type using DER.
57 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000058asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070059INTEGER_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
60 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
61 void *app_key) {
62 const INTEGER_t *st = (const INTEGER_t *)sptr;
63 asn_enc_rval_t rval;
64 INTEGER_t effective_integer;
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) {
Lev Walkin20696a42017-10-17 21:27:33 -0700101 union {
102 const uint8_t *c_buf;
103 uint8_t *nc_buf;
104 } unconst;
105 unconst.c_buf = st->buf;
106 effective_integer.buf = unconst.nc_buf + shift;
107 effective_integer.size = st->size - shift;
Lev Walkinf15320b2004-06-03 03:38:44 +0000108
Lev Walkin20696a42017-10-17 21:27:33 -0700109 st = &effective_integer;
110 }
111 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000112
Lev Walkinaf51eaf2017-10-18 00:26:37 -0700113 rval = der_encode_primitive(td, st, tag_mode, tag, cb, app_key);
Lev Walkin20696a42017-10-17 21:27:33 -0700114 if(rval.structure_ptr == &effective_integer) {
115 rval.structure_ptr = sptr;
116 }
117 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118}
119
Lev Walkind62a6622017-08-22 02:31:02 -0700120static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(
121 const asn_INTEGER_specifics_t *specs, const char *lstart,
122 const char *lstop);
Lev Walkine0b56e02005-02-25 12:10:27 +0000123
Lev Walkinf15320b2004-06-03 03:38:44 +0000124/*
125 * INTEGER specific human-readable output.
126 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000127static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700128INTEGER__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 -0700129 const asn_INTEGER_specifics_t *specs =
130 (const asn_INTEGER_specifics_t *)td->specifics;
Lev Walkine2bbbf82017-10-08 18:52:37 -0700131 char scratch[32];
Lev Walkinf15320b2004-06-03 03:38:44 +0000132 uint8_t *buf = st->buf;
133 uint8_t *buf_end = st->buf + st->size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700134 intmax_t value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000135 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000136 char *p;
137 int ret;
138
Lev Walkin97f8edc2013-03-28 04:38:41 -0700139 if(specs && specs->field_unsigned)
Lev Walkin72ec9092017-07-05 05:49:12 -0700140 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
Lev Walkin97f8edc2013-03-28 04:38:41 -0700141 else
Lev Walkin72ec9092017-07-05 05:49:12 -0700142 ret = asn_INTEGER2imax(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000143
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700145 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000146 const asn_INTEGER_enum_map_t *el;
Lev Walkin97f8edc2013-03-28 04:38:41 -0700147 el = (value >= 0 || !specs || !specs->field_unsigned)
148 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000149 if(el) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000150 if(plainOrXER == 0)
Lev Walkine2bbbf82017-10-08 18:52:37 -0700151 return asn__format_to_callback(cb, app_key,
Lev Walkin9f470d62017-10-21 16:27:08 -0700152 "%" ASN_PRIdMAX " (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000153 else
Lev Walkine2bbbf82017-10-08 18:52:37 -0700154 return asn__format_to_callback(cb, app_key,
Lev Walkine0b56e02005-02-25 12:10:27 +0000155 "<%s/>", el->enum_name);
156 } else if(plainOrXER && specs && specs->strict_enumeration) {
157 ASN_DEBUG("ASN.1 forbids dealing with "
158 "unknown value of ENUMERATED type");
159 errno = EPERM;
160 return -1;
161 } else {
Lev Walkine2bbbf82017-10-08 18:52:37 -0700162 return asn__format_to_callback(cb, app_key,
Lev Walkin9f470d62017-10-21 16:27:08 -0700163 (specs && specs->field_unsigned)
164 ? "%" ASN_PRIuMAX
165 : "%" ASN_PRIdMAX,
166 value);
Lev Walkin72ec9092017-07-05 05:49:12 -0700167 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000168 } else if(plainOrXER && specs && specs->strict_enumeration) {
169 /*
170 * Here and earlier, we cannot encode the ENUMERATED values
171 * if there is no corresponding identifier.
172 */
173 ASN_DEBUG("ASN.1 forbids dealing with "
174 "unknown value of ENUMERATED type");
175 errno = EPERM;
176 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 }
178
179 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000180 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700182 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000183 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000185 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000187 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 p = scratch;
189 }
190 *p++ = h2c[*buf >> 4];
191 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000192 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 }
Lev Walkindb13f512004-07-19 17:30:25 +0000194 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000195 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000196
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197 wrote += p - scratch;
198 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
199}
200
201/*
202 * INTEGER specific human-readable output.
203 */
204int
Lev Walkin20696a42017-10-17 21:27:33 -0700205INTEGER_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
206 asn_app_consume_bytes_f *cb, void *app_key) {
207 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000208 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000209
Lev Walkina9cc46e2004-09-22 16:06:28 +0000210 (void)ilevel;
211
Lev Walkind500a962005-11-27 13:06:56 +0000212 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000213 ret = cb("<absent>", 8, app_key);
214 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000215 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000216
Lev Walkin8e8078a2004-09-26 13:10:40 +0000217 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000218}
219
Lev Walkine0b56e02005-02-25 12:10:27 +0000220struct e2v_key {
221 const char *start;
222 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700223 const asn_INTEGER_enum_map_t *vemap;
224 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000225};
226static int
227INTEGER__compar_enum2value(const void *kp, const void *am) {
228 const struct e2v_key *key = (const struct e2v_key *)kp;
229 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
230 const char *ptr, *end, *name;
231
232 /* Remap the element (sort by different criterion) */
233 el = key->vemap + key->evmap[el - key->vemap];
234
235 /* Compare strings */
236 for(ptr = key->start, end = key->stop, name = el->enum_name;
237 ptr < end; ptr++, name++) {
Lev Walkin793982a2017-10-02 14:12:51 -0700238 if(*ptr != *name || !*name)
Lev Walkine0b56e02005-02-25 12:10:27 +0000239 return *(const unsigned char *)ptr
240 - *(const unsigned char *)name;
241 }
242 return name[0] ? -1 : 0;
243}
244
245static const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700246INTEGER_map_enum2value(const asn_INTEGER_specifics_t *specs, const char *lstart,
247 const char *lstop) {
248 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000249 int count = specs ? specs->map_count : 0;
250 struct e2v_key key;
251 const char *lp;
252
253 if(!count) return NULL;
254
255 /* Guaranteed: assert(lstart < lstop); */
256 /* Figure out the tag name */
257 for(lstart++, lp = lstart; lp < lstop; lp++) {
258 switch(*lp) {
259 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
260 case 0x2f: /* '/' */ case 0x3e: /* '>' */
261 break;
262 default:
263 continue;
264 }
265 break;
266 }
267 if(lp == lstop) return NULL; /* No tag found */
268 lstop = lp;
269
270 key.start = lstart;
271 key.stop = lstop;
272 key.vemap = specs->value2enum;
273 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000274 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
275 specs->value2enum, count, sizeof(specs->value2enum[0]),
276 INTEGER__compar_enum2value);
277 if(el_found) {
278 /* Remap enum2value into value2enum */
279 el_found = key.vemap + key.evmap[el_found - key.vemap];
280 }
281 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000282}
283
284static int
285INTEGER__compar_value2enum(const void *kp, const void *am) {
286 long a = *(const long *)kp;
287 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
288 long b = el->nat_value;
289 if(a < b) return -1;
290 else if(a == b) return 0;
291 else return 1;
292}
293
Lev Walkinc2350112005-03-29 17:19:53 +0000294const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700295INTEGER_map_value2enum(const asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000296 int count = specs ? specs->map_count : 0;
297 if(!count) return 0;
298 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
299 count, sizeof(specs->value2enum[0]),
300 INTEGER__compar_value2enum);
301}
302
Lev Walkinc744a022006-09-15 18:33:25 +0000303static int
304INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
305 void *p = MALLOC(min_size + 1);
306 if(p) {
307 void *b = st->buf;
308 st->size = 0;
309 st->buf = p;
310 FREEMEM(b);
311 return 0;
312 } else {
313 return -1;
314 }
315}
316
Lev Walkind703ff42004-10-21 11:21:25 +0000317/*
318 * Decode the chunk of XML text encoding INTEGER.
319 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000320static enum xer_pbd_rval
Lev Walkin20696a42017-10-17 21:27:33 -0700321INTEGER__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
322 const void *chunk_buf, size_t chunk_size) {
323 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700324 intmax_t dec_value;
325 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000326 const char *lp;
327 const char *lstart = (const char *)chunk_buf;
328 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000329 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700330 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000331 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000332 ST_WAITDIGITS,
333 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700334 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000335 ST_HEXDIGIT1,
336 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700337 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000338 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700339 ST_END_ENUM,
340 ST_UNEXPECTED
341 } state = ST_LEADSPACE;
342 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
343 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000344
Lev Walkinc744a022006-09-15 18:33:25 +0000345 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000346 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
347 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000348
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000349 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
350 return XPBD_SYSTEM_FAILURE;
351
Lev Walkind703ff42004-10-21 11:21:25 +0000352 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000353 * We may have received a tag here. It will be processed inline.
354 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000355 */
Lev Walkinb3751942012-09-02 19:36:47 -0700356 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000357 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000358 switch(lv) {
359 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000360 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700361 case ST_LEADSPACE:
362 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700363 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000364 case ST_SKIPSPHEX:
365 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700366 case ST_DIGITS:
367 dec_value_end = lp;
368 state = ST_DIGITS_TRAILSPACE;
369 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700370 case ST_HEXCOLON:
371 state = ST_HEXDIGITS_TRAILSPACE;
372 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000373 default:
374 break;
375 }
Lev Walkind703ff42004-10-21 11:21:25 +0000376 break;
377 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700378 if(state == ST_LEADSPACE) {
379 dec_value = 0;
380 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000381 state = ST_WAITDIGITS;
382 continue;
383 }
384 break;
385 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700386 if(state == ST_LEADSPACE) {
387 dec_value = 0;
388 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000389 state = ST_WAITDIGITS;
390 continue;
391 }
392 break;
393 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
394 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000395 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700396 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000397 case ST_SKIPSPHEX: /* Fall through */
398 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700399 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000400 state = ST_HEXDIGIT2;
401 continue;
402 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700403 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000404 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700405 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000406 continue;
407 case ST_HEXCOLON:
408 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700409 case ST_LEADSPACE:
410 dec_value = 0;
411 dec_value_start = lp;
412 /* FALL THROUGH */
413 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000414 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700415 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700416 default:
417 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000418 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700419 break;
420 case 0x3c: /* '<', start of XML encoded enumeration */
421 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000422 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000423 el = INTEGER_map_enum2value(
Lev Walkind62a6622017-08-22 02:31:02 -0700424 (const asn_INTEGER_specifics_t *)
Lev Walkine0b56e02005-02-25 12:10:27 +0000425 td->specifics, lstart, lstop);
426 if(el) {
427 ASN_DEBUG("Found \"%s\" => %ld",
428 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700429 dec_value = el->nat_value;
430 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000431 lp = lstop - 1;
432 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000433 }
434 ASN_DEBUG("Unknown identifier for INTEGER");
435 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000436 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000437 case 0x3a: /* ':' */
438 if(state == ST_HEXCOLON) {
439 /* This colon is expected */
440 state = ST_HEXDIGIT1;
441 continue;
442 } else if(state == ST_DIGITS) {
443 /* The colon here means that we have
444 * decoded the first two hexadecimal
445 * places as a decimal value.
446 * Switch decoding mode. */
447 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000448 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700449 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000450 lp = lstart - 1;
451 continue;
452 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400453 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000454 break;
455 }
456 /* [A-Fa-f] */
457 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
458 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
459 switch(state) {
460 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700461 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000462 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700463 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
464 hex_value += 10;
465 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000466 state = ST_HEXDIGIT2;
467 continue;
468 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700469 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
470 hex_value += 10;
471 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000472 state = ST_HEXCOLON;
473 continue;
474 case ST_DIGITS:
475 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000476 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700477 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000478 lp = lstart - 1;
479 continue;
480 default:
481 break;
482 }
483 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000484 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000485
486 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700487 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400488 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700489 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000490 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000491 }
492
Lev Walkinc744a022006-09-15 18:33:25 +0000493 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700494 case ST_END_ENUM:
495 /* Got a complete and valid enumeration encoded as a tag. */
496 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000497 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700498 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700499 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700500 case ST_DIGITS_TRAILSPACE:
501 /* The last symbol encountered was a digit. */
Lev Walkin48e82d12017-10-19 03:06:35 -0700502 switch(asn_strtoimax_lim(dec_value_start, &dec_value_end, &dec_value)) {
503 case ASN_STRTOX_OK:
504 if(dec_value >= LONG_MIN && dec_value <= LONG_MAX) {
505 break;
506 } else {
507 /*
508 * We model INTEGER on long for XER,
509 * to avoid rewriting all the tests at once.
510 */
511 ASN_DEBUG("INTEGER exceeds long range");
512 }
513 /* Fall through */
514 case ASN_STRTOX_ERROR_RANGE:
515 ASN_DEBUG("INTEGER decode %s hit range limit", td->name);
516 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700517 case ASN_STRTOX_ERROR_INVAL:
518 case ASN_STRTOX_EXPECT_MORE:
519 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700520 return XPBD_BROKEN_ENCODING;
521 }
Lev Walkinc744a022006-09-15 18:33:25 +0000522 break;
523 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700524 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000525 st->buf[st->size] = 0; /* Just in case termination */
526 return XPBD_BODY_CONSUMED;
527 case ST_HEXDIGIT1:
528 case ST_HEXDIGIT2:
529 case ST_SKIPSPHEX:
530 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700531 case ST_LEADSPACE:
532 /* Content not found */
533 return XPBD_NOT_BODY_IGNORE;
534 case ST_WAITDIGITS:
535 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700536 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
537 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000538 }
Lev Walkind703ff42004-10-21 11:21:25 +0000539
Lev Walkine09f9f12012-09-02 23:06:35 -0700540 /*
541 * Convert the result of parsing of enumeration or a straight
542 * decimal value into a BER representation.
543 */
Lev Walkinb7c58992017-10-06 16:36:32 -0700544 if(asn_imax2INTEGER(st, dec_value)) {
545 ASN_DEBUG("INTEGER decode %s conversion failed", td->name);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000546 return XPBD_SYSTEM_FAILURE;
Lev Walkinb7c58992017-10-06 16:36:32 -0700547 }
Lev Walkind703ff42004-10-21 11:21:25 +0000548
Lev Walkin0fab1a62005-03-09 22:19:25 +0000549 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000550}
551
552asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700553INTEGER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -0700554 const asn_TYPE_descriptor_t *td, void **sptr,
555 const char *opt_mname, const void *buf_ptr, size_t size) {
556 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000557 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000558 buf_ptr, size, INTEGER__xer_body_decode);
559}
560
Lev Walkina9cc46e2004-09-22 16:06:28 +0000561asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700562INTEGER_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
563 int ilevel, enum xer_encoder_flags_e flags,
564 asn_app_consume_bytes_f *cb, void *app_key) {
565 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000566 asn_enc_rval_t er;
567
568 (void)ilevel;
569 (void)flags;
570
Lev Walkind500a962005-11-27 13:06:56 +0000571 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700572 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000573
Lev Walkine0b56e02005-02-25 12:10:27 +0000574 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700575 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000576
Lev Walkin7c1dc052016-03-14 03:08:15 -0700577 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000578}
579
Lev Walkind7703cf2012-09-03 01:45:03 -0700580#ifndef ASN_DISABLE_PER_SUPPORT
581
Lev Walkin59b176e2005-11-26 11:25:14 +0000582asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700583INTEGER_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
584 const asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700585 const asn_per_constraints_t *constraints, void **sptr,
586 asn_per_data_t *pd) {
Lev Walkind62a6622017-08-22 02:31:02 -0700587 const asn_INTEGER_specifics_t *specs =
588 (const asn_INTEGER_specifics_t *)td->specifics;
589 asn_dec_rval_t rval = { RC_OK, 0 };
Lev Walkin59b176e2005-11-26 11:25:14 +0000590 INTEGER_t *st = (INTEGER_t *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -0700591 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000592 int repeat;
593
594 (void)opt_codec_ctx;
595
596 if(!st) {
597 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700598 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000599 }
600
Lev Walkina5972be2017-09-29 23:15:58 -0700601 if(!constraints) constraints = td->encoding_constraints.per_constraints;
Lev Walkin59b176e2005-11-26 11:25:14 +0000602 ct = constraints ? &constraints->value : 0;
603
604 if(ct && ct->flags & APC_EXTENSIBLE) {
605 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700606 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000607 if(inext) ct = 0;
608 }
609
610 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000611 st->buf = 0;
612 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000613 if(ct) {
614 if(ct->flags & APC_SEMI_CONSTRAINED) {
615 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700616 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000617 st->size = 1;
618 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
619 size_t size = (ct->range_bits + 7) >> 3;
620 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700621 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000622 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000623 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000624 }
625
Lev Walkin6c527842014-02-09 04:34:54 -0800626 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000627 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800628 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000629 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
630 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800631 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700632 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800633
634 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700635 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800636 if(uper_get_constrained_whole_number(pd,
637 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700638 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800639 ASN_DEBUG("Got value %lu + low %ld",
640 uvalue, ct->lower_bound);
641 uvalue += ct->lower_bound;
642 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700643 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800644 } else {
Lev Walkin38a91df2017-09-17 23:52:33 -0700645 unsigned long uvalue = 0;
646 long svalue;
Lev Walkin6c527842014-02-09 04:34:54 -0800647 if(uper_get_constrained_whole_number(pd,
Lev Walkin38a91df2017-09-17 23:52:33 -0700648 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700649 ASN__DECODE_STARVED;
Lev Walkin38a91df2017-09-17 23:52:33 -0700650 ASN_DEBUG("Got value %lu + low %ld",
651 uvalue, ct->lower_bound);
Lev Walkincc6a76b2017-10-07 16:23:16 -0700652 if(per_long_range_unrebase(uvalue, ct->lower_bound,
653 ct->upper_bound, &svalue)
654 || asn_long2INTEGER(st, svalue)) {
655 ASN__DECODE_FAILED;
656 }
Lev Walkin6c527842014-02-09 04:34:54 -0800657 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000658 return rval;
659 }
660 } else {
661 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
662 }
663
664 /* X.691, #12.2.3, #12.2.4 */
665 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700666 ssize_t len = 0;
667 void *p = NULL;
668 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000669
670 /* Get the PER length */
Lev Walkin9d1b45f2017-10-01 17:04:48 -0700671 len = uper_get_length(pd, -1, 0, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700672 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000673
674 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700675 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000676 st->buf = (uint8_t *)p;
677
678 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700679 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000680 st->size += len;
681 } while(repeat);
682 st->buf[st->size] = 0; /* JIC */
683
684 /* #12.2.3 */
685 if(ct && ct->lower_bound) {
686 /*
687 * TODO: replace by in-place arithmetics.
688 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700689 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000690 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700691 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700692 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700693 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000694 }
695
696 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000697}
698
Lev Walkin523de9e2006-08-18 01:34:18 +0000699asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700700INTEGER_encode_uper(const asn_TYPE_descriptor_t *td,
701 const asn_per_constraints_t *constraints, const void *sptr,
Lev Walkin494fb702017-08-07 20:07:00 -0700702 asn_per_outp_t *po) {
Lev Walkind62a6622017-08-22 02:31:02 -0700703 const asn_INTEGER_specifics_t *specs =
704 (const asn_INTEGER_specifics_t *)td->specifics;
705 asn_enc_rval_t er;
Lev Walkin20696a42017-10-17 21:27:33 -0700706 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin523de9e2006-08-18 01:34:18 +0000707 const uint8_t *buf;
708 const uint8_t *end;
Lev Walkin494fb702017-08-07 20:07:00 -0700709 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000710 long value = 0;
711
Lev Walkin7c1dc052016-03-14 03:08:15 -0700712 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000713
Lev Walkina5972be2017-09-29 23:15:58 -0700714 if(!constraints) constraints = td->encoding_constraints.per_constraints;
Lev Walkin523de9e2006-08-18 01:34:18 +0000715 ct = constraints ? &constraints->value : 0;
716
717 er.encoded = 0;
718
719 if(ct) {
720 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000721 if(specs && specs->field_unsigned) {
722 unsigned long uval;
723 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700724 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000725 /* Check proper range */
726 if(ct->flags & APC_SEMI_CONSTRAINED) {
727 if(uval < (unsigned long)ct->lower_bound)
728 inext = 1;
729 } else if(ct->range_bits >= 0) {
730 if(uval < (unsigned long)ct->lower_bound
731 || uval > (unsigned long)ct->upper_bound)
732 inext = 1;
733 }
Lev Walkin9f470d62017-10-21 16:27:08 -0700734 ASN_DEBUG("Value %lu (%02x/%" ASN_PRI_SIZE ") lb %lu ub %lu %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000735 uval, st->buf[0], st->size,
736 ct->lower_bound, ct->upper_bound,
737 inext ? "ext" : "fix");
738 value = uval;
739 } else {
740 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700741 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000742 /* Check proper range */
743 if(ct->flags & APC_SEMI_CONSTRAINED) {
744 if(value < ct->lower_bound)
745 inext = 1;
746 } else if(ct->range_bits >= 0) {
747 if(value < ct->lower_bound
748 || value > ct->upper_bound)
749 inext = 1;
750 }
Lev Walkin9f470d62017-10-21 16:27:08 -0700751 ASN_DEBUG("Value %ld (%02x/%" ASN_PRI_SIZE ") lb %ld ub %ld %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000752 value, st->buf[0], st->size,
753 ct->lower_bound, ct->upper_bound,
754 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000755 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000756 if(ct->flags & APC_EXTENSIBLE) {
757 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700758 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000759 if(inext) ct = 0;
760 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700761 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000762 }
763 }
764
765
Lev Walkin6c527842014-02-09 04:34:54 -0800766 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000767 if(ct && ct->range_bits >= 0) {
Lev Walkincc6a76b2017-10-07 16:23:16 -0700768 unsigned long v;
Lev Walkin6c527842014-02-09 04:34:54 -0800769 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800770 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
771 value, value - ct->lower_bound, ct->range_bits);
Lev Walkincc6a76b2017-10-07 16:23:16 -0700772 if(per_long_range_rebase(value, ct->lower_bound, ct->upper_bound, &v)) {
773 ASN__ENCODE_FAILED;
774 }
775 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
776 ASN__ENCODE_FAILED;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700777 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000778 }
779
780 if(ct && ct->lower_bound) {
781 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
782 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700783 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000784 }
785
786 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
Lev Walkin5d947a82017-10-03 01:04:03 -0700787 int need_eom = 0;
788 ssize_t mayEncode = uper_put_length(po, end - buf, &need_eom);
789 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700790 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000791 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700792 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000793 buf += mayEncode;
Lev Walkin5d947a82017-10-03 01:04:03 -0700794 if(need_eom && uper_put_length(po, 0, 0)) ASN__ENCODE_FAILED;
795 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000796
Lev Walkin7c1dc052016-03-14 03:08:15 -0700797 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000798}
799
Lev Walkind7703cf2012-09-03 01:45:03 -0700800#endif /* ASN_DISABLE_PER_SUPPORT */
801
Lev Walkinc346f902017-10-09 20:24:40 -0700802static intmax_t
803asn__integer_convert(const uint8_t *b, const uint8_t *end) {
804 uintmax_t value;
Lev Walkin642b92f2017-09-17 22:16:02 -0700805
Lev Walkinc346f902017-10-09 20:24:40 -0700806 /* Perform the sign initialization */
807 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
808 if((*b >> 7)) {
809 value = (uintmax_t)(-1);
Lev Walkin642b92f2017-09-17 22:16:02 -0700810 } else {
811 value = 0;
812 }
813
814 /* Conversion engine */
Lev Walkinc346f902017-10-09 20:24:40 -0700815 for(; b < end; b++) {
816 value = (value << 8) | *b;
Lev Walkin642b92f2017-09-17 22:16:02 -0700817 }
818
819 return value;
820}
821
Lev Walkinf15320b2004-06-03 03:38:44 +0000822int
Lev Walkin72ec9092017-07-05 05:49:12 -0700823asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000824 uint8_t *b, *end;
825 size_t size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000826
827 /* Sanity checking */
828 if(!iptr || !iptr->buf || !lptr) {
829 errno = EINVAL;
830 return -1;
831 }
832
833 /* Cache the begin/end of the buffer */
834 b = iptr->buf; /* Start of the INTEGER buffer */
835 size = iptr->size;
836 end = b + size; /* Where to stop */
837
Lev Walkin642b92f2017-09-17 22:16:02 -0700838 if(size > sizeof(intmax_t)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000839 uint8_t *end1 = end - 1;
840 /*
841 * Slightly more advanced processing,
Lev Walkin642b92f2017-09-17 22:16:02 -0700842 * able to process INTEGERs with >sizeof(intmax_t) bytes
Lev Walkin72ec9092017-07-05 05:49:12 -0700843 * when the actual value is small, e.g. for intmax_t == int32_t
844 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000845 */
846 /* Skip out the insignificant leading bytes */
847 for(; b < end1; b++) {
848 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700849 case 0x00: if((b[1] & 0x80) == 0) continue; break;
850 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000851 }
852 break;
853 }
854
855 size = end - b;
Lev Walkin642b92f2017-09-17 22:16:02 -0700856 if(size > sizeof(intmax_t)) {
857 /* Still cannot fit the sizeof(intmax_t) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000858 errno = ERANGE;
859 return -1;
860 }
861 }
862
863 /* Shortcut processing of a corner case */
864 if(end == b) {
865 *lptr = 0;
866 return 0;
867 }
868
Lev Walkinc346f902017-10-09 20:24:40 -0700869 *lptr = asn__integer_convert(b, end);
Lev Walkinf15320b2004-06-03 03:38:44 +0000870 return 0;
871}
Lev Walkind703ff42004-10-21 11:21:25 +0000872
Lev Walkin72ec9092017-07-05 05:49:12 -0700873/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000874int
Lev Walkin72ec9092017-07-05 05:49:12 -0700875asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000876 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700877 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000878 size_t size;
879
880 if(!iptr || !iptr->buf || !lptr) {
881 errno = EINVAL;
882 return -1;
883 }
884
885 b = iptr->buf;
886 size = iptr->size;
887 end = b + size;
888
889 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700890 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000891 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700892 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000893 errno = ERANGE;
894 return -1;
895 }
896 }
897
898 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700899 for(value = 0; b < end; b++)
900 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000901
Lev Walkin72ec9092017-07-05 05:49:12 -0700902 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000903 return 0;
904}
905
906int
Lev Walkin72ec9092017-07-05 05:49:12 -0700907asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
908 uint8_t *buf;
909 uint8_t *end;
910 uint8_t *b;
911 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000912
Lev Walkinaa067a92017-10-03 16:09:05 -0700913 if(value <= ((~(uintmax_t)0) >> 1)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700914 return asn_imax2INTEGER(st, value);
915 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000916
Lev Walkin72ec9092017-07-05 05:49:12 -0700917 buf = (uint8_t *)MALLOC(1 + sizeof(value));
918 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000919
Lev Walkin72ec9092017-07-05 05:49:12 -0700920 end = buf + (sizeof(value) + 1);
921 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
922 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
923 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000924
Lev Walkin72ec9092017-07-05 05:49:12 -0700925 if(st->buf) FREEMEM(st->buf);
926 st->buf = buf;
927 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000928
929 return 0;
930}
931
932int
Lev Walkin72ec9092017-07-05 05:49:12 -0700933asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000934 uint8_t *buf, *bp;
935 uint8_t *p;
936 uint8_t *pstart;
937 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000938 int littleEndian = 1; /* Run-time detection */
939 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000940
941 if(!st) {
942 errno = EINVAL;
943 return -1;
944 }
945
Lev Walkin290b4d62017-08-28 17:48:35 -0700946 buf = (uint8_t *)(long *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000947 if(!buf) return -1;
948
Lev Walkind7ad5612004-10-26 08:20:46 +0000949 if(*(char *)&littleEndian) {
950 pstart = (uint8_t *)&value + sizeof(value) - 1;
951 pend1 = (uint8_t *)&value;
952 add = -1;
953 } else {
954 pstart = (uint8_t *)&value;
955 pend1 = pstart + sizeof(value) - 1;
956 add = 1;
957 }
958
Lev Walkind703ff42004-10-21 11:21:25 +0000959 /*
960 * If the contents octet consists of more than one octet,
961 * then bits of the first octet and bit 8 of the second octet:
962 * a) shall not all be ones; and
963 * b) shall not all be zero.
964 */
Lev Walkin33700162004-10-26 09:03:31 +0000965 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000966 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000967 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000968 continue;
969 break;
Lev Walkin33700162004-10-26 09:03:31 +0000970 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000971 continue;
972 break;
973 }
974 break;
975 }
976 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700977 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000978 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000979
980 if(st->buf) FREEMEM(st->buf);
981 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000982 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000983
984 return 0;
985}
Lev Walkinb3751942012-09-02 19:36:47 -0700986
Lev Walkin72ec9092017-07-05 05:49:12 -0700987int
988asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
989 intmax_t v;
990 if(asn_INTEGER2imax(iptr, &v) == 0) {
991 if(v < LONG_MIN || v > LONG_MAX) {
992 errno = ERANGE;
993 return -1;
994 }
995 *l = v;
996 return 0;
997 } else {
998 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700999 }
Lev Walkin72ec9092017-07-05 05:49:12 -07001000}
Lev Walkincad560a2013-03-16 07:00:58 -07001001
Lev Walkin72ec9092017-07-05 05:49:12 -07001002int
1003asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
1004 uintmax_t v;
1005 if(asn_INTEGER2umax(iptr, &v) == 0) {
1006 if(v > ULONG_MAX) {
1007 errno = ERANGE;
1008 return -1;
1009 }
1010 *l = v;
1011 return 0;
1012 } else {
1013 return -1;
1014 }
1015}
1016
1017int
1018asn_long2INTEGER(INTEGER_t *st, long value) {
1019 return asn_imax2INTEGER(st, value);
1020}
1021
1022int
1023asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1024 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -07001025}
1026
1027/*
1028 * Parse the number in the given string until the given *end position,
1029 * returning the position after the last parsed character back using the
1030 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -07001031 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -07001032 */
Lev Walkin72ec9092017-07-05 05:49:12 -07001033enum asn_strtox_result_e
1034asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkin6495ca52019-08-10 20:07:39 -07001035 int sign = 1;
1036 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -07001037
Lev Walkin6495ca52019-08-10 20:07:39 -07001038 const intmax_t asn1_intmax_max = ((~(uintmax_t)0) >> 1);
1039 const intmax_t upper_boundary = asn1_intmax_max / 10;
1040 intmax_t last_digit_max = asn1_intmax_max % 10;
Lev Walkine09f9f12012-09-02 23:06:35 -07001041
Lev Walkin6495ca52019-08-10 20:07:39 -07001042 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001043
Lev Walkin6495ca52019-08-10 20:07:39 -07001044 switch(*str) {
1045 case '-':
1046 last_digit_max++;
1047 sign = -1;
1048 /* FALL THROUGH */
1049 case '+':
1050 str++;
1051 if(str >= *end) {
1052 *end = str;
1053 return ASN_STRTOX_EXPECT_MORE;
1054 }
1055 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001056
Lev Walkin6495ca52019-08-10 20:07:39 -07001057 for(value = 0; str < (*end); str++) {
1058 if(*str >= 0x30 && *str <= 0x39) {
1059 int d = *str - '0';
1060 if(value < upper_boundary) {
1061 value = value * 10 + d;
1062 } else if(value == upper_boundary) {
1063 if(d <= last_digit_max) {
1064 if(sign > 0) {
1065 value = value * 10 + d;
1066 } else {
1067 sign = 1;
1068 value = -value * 10 - d;
1069 }
1070 str += 1;
1071 if(str < *end) {
1072 // If digits continue, we're guaranteed out of range.
1073 *end = str;
1074 if(*str >= 0x30 && *str <= 0x39) {
1075 return ASN_STRTOX_ERROR_RANGE;
1076 } else {
1077 *intp = sign * value;
1078 return ASN_STRTOX_EXTRA_DATA;
1079 }
1080 }
1081 break;
1082 } else {
1083 *end = str;
1084 return ASN_STRTOX_ERROR_RANGE;
1085 }
1086 } else {
1087 *end = str;
1088 return ASN_STRTOX_ERROR_RANGE;
1089 }
1090 } else {
1091 *end = str;
1092 *intp = sign * value;
1093 return ASN_STRTOX_EXTRA_DATA;
1094 }
1095 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001096
Lev Walkin6495ca52019-08-10 20:07:39 -07001097 *end = str;
1098 *intp = sign * value;
1099 return ASN_STRTOX_OK;
Lev Walkin72ec9092017-07-05 05:49:12 -07001100}
1101
Lev Walkin588bf0f2017-10-13 23:51:16 -07001102/*
1103 * Parse the number in the given string until the given *end position,
1104 * returning the position after the last parsed character back using the
1105 * same (*end) pointer.
1106 * WARNING: This behavior is different from the standard strtoul/strtoumax(3).
1107 */
1108enum asn_strtox_result_e
1109asn_strtoumax_lim(const char *str, const char **end, uintmax_t *uintp) {
Lev Walkin6495ca52019-08-10 20:07:39 -07001110 uintmax_t value;
Lev Walkin588bf0f2017-10-13 23:51:16 -07001111
Lev Walkin6495ca52019-08-10 20:07:39 -07001112 const uintmax_t asn1_uintmax_max = ((~(uintmax_t)0));
1113 const uintmax_t upper_boundary = asn1_uintmax_max / 10;
1114 uintmax_t last_digit_max = asn1_uintmax_max % 10;
Lev Walkin588bf0f2017-10-13 23:51:16 -07001115
1116 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
1117
Lev Walkin6495ca52019-08-10 20:07:39 -07001118 switch(*str) {
1119 case '-':
Lev Walkin588bf0f2017-10-13 23:51:16 -07001120 return ASN_STRTOX_ERROR_INVAL;
Lev Walkin6495ca52019-08-10 20:07:39 -07001121 case '+':
1122 str++;
1123 if(str >= *end) {
1124 *end = str;
1125 return ASN_STRTOX_EXPECT_MORE;
1126 }
1127 }
Lev Walkin588bf0f2017-10-13 23:51:16 -07001128
Lev Walkin6495ca52019-08-10 20:07:39 -07001129 for(value = 0; str < (*end); str++) {
1130 if(*str >= 0x30 && *str <= 0x39) {
1131 unsigned int d = *str - '0';
1132 if(value < upper_boundary) {
1133 value = value * 10 + d;
1134 } else if(value == upper_boundary) {
1135 if(d <= last_digit_max) {
Lev Walkin588bf0f2017-10-13 23:51:16 -07001136 value = value * 10 + d;
Lev Walkin6495ca52019-08-10 20:07:39 -07001137 str += 1;
1138 if(str < *end) {
1139 // If digits continue, we're guaranteed out of range.
1140 *end = str;
1141 if(*str >= 0x30 && *str <= 0x39) {
1142 return ASN_STRTOX_ERROR_RANGE;
1143 } else {
1144 *uintp = value;
1145 return ASN_STRTOX_EXTRA_DATA;
1146 }
1147 }
1148 break;
Lev Walkin588bf0f2017-10-13 23:51:16 -07001149 } else {
Lev Walkin6495ca52019-08-10 20:07:39 -07001150 *end = str;
1151 return ASN_STRTOX_ERROR_RANGE;
1152 }
1153 } else {
1154 *end = str;
1155 return ASN_STRTOX_ERROR_RANGE;
1156 }
1157 } else {
1158 *end = str;
1159 *uintp = value;
1160 return ASN_STRTOX_EXTRA_DATA;
1161 }
1162 }
Lev Walkin588bf0f2017-10-13 23:51:16 -07001163
Lev Walkin6495ca52019-08-10 20:07:39 -07001164 *end = str;
1165 *uintp = value;
1166 return ASN_STRTOX_OK;
Lev Walkin588bf0f2017-10-13 23:51:16 -07001167}
1168
Lev Walkin72ec9092017-07-05 05:49:12 -07001169enum asn_strtox_result_e
1170asn_strtol_lim(const char *str, const char **end, long *lp) {
1171 intmax_t value;
1172 switch(asn_strtoimax_lim(str, end, &value)) {
1173 case ASN_STRTOX_ERROR_RANGE:
1174 return ASN_STRTOX_ERROR_RANGE;
1175 case ASN_STRTOX_ERROR_INVAL:
1176 return ASN_STRTOX_ERROR_INVAL;
1177 case ASN_STRTOX_EXPECT_MORE:
1178 return ASN_STRTOX_EXPECT_MORE;
1179 case ASN_STRTOX_OK:
1180 if(value >= LONG_MIN && value <= LONG_MAX) {
1181 *lp = value;
1182 return ASN_STRTOX_OK;
1183 } else {
1184 return ASN_STRTOX_ERROR_RANGE;
1185 }
1186 case ASN_STRTOX_EXTRA_DATA:
1187 if(value >= LONG_MIN && value <= LONG_MAX) {
1188 *lp = value;
1189 return ASN_STRTOX_EXTRA_DATA;
1190 } else {
1191 return ASN_STRTOX_ERROR_RANGE;
1192 }
1193 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001194
1195 assert(!"Unreachable");
1196 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001197}
1198
Lev Walkin588bf0f2017-10-13 23:51:16 -07001199enum asn_strtox_result_e
1200asn_strtoul_lim(const char *str, const char **end, unsigned long *ulp) {
1201 uintmax_t value;
1202 switch(asn_strtoumax_lim(str, end, &value)) {
1203 case ASN_STRTOX_ERROR_RANGE:
1204 return ASN_STRTOX_ERROR_RANGE;
1205 case ASN_STRTOX_ERROR_INVAL:
1206 return ASN_STRTOX_ERROR_INVAL;
1207 case ASN_STRTOX_EXPECT_MORE:
1208 return ASN_STRTOX_EXPECT_MORE;
1209 case ASN_STRTOX_OK:
1210 if(value <= ULONG_MAX) {
1211 *ulp = value;
1212 return ASN_STRTOX_OK;
1213 } else {
1214 return ASN_STRTOX_ERROR_RANGE;
1215 }
1216 case ASN_STRTOX_EXTRA_DATA:
1217 if(value <= ULONG_MAX) {
1218 *ulp = value;
1219 return ASN_STRTOX_EXTRA_DATA;
1220 } else {
1221 return ASN_STRTOX_ERROR_RANGE;
1222 }
1223 }
1224
1225 assert(!"Unreachable");
1226 return ASN_STRTOX_ERROR_INVAL;
1227}
1228
Lev Walkincd2f48e2017-08-10 02:14:59 -07001229int
1230INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1231 const void *bptr) {
1232 const INTEGER_t *a = aptr;
1233 const INTEGER_t *b = bptr;
1234
1235 (void)td;
1236
1237 if(a && b) {
1238 if(a->size && b->size) {
1239 int sign_a = (a->buf[0] & 0x80) ? -1 : 1;
1240 int sign_b = (b->buf[0] & 0x80) ? -1 : 1;
1241
1242 if(sign_a < sign_b) return -1;
1243 if(sign_a > sign_b) return 1;
1244
1245 /* The shortest integer wins, unless comparing negatives */
1246 if(a->size < b->size) {
1247 return -1 * sign_a;
1248 } else if(a->size > b->size) {
1249 return 1 * sign_b;
1250 }
1251
1252 return sign_a * memcmp(a->buf, b->buf, a->size);
1253 } else if(a->size) {
1254 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1255 return (1) * sign;
1256 } else if(b->size) {
1257 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1258 return (-1) * sign;
1259 } else {
1260 return 0;
1261 }
1262 } else if(!a && !b) {
1263 return 0;
1264 } else if(!a) {
1265 return -1;
1266 } else {
1267 return 1;
1268 }
1269
1270}
1271
Lev Walkina5972be2017-09-29 23:15:58 -07001272asn_random_fill_result_t
1273INTEGER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1274 const asn_encoding_constraints_t *constraints,
1275 size_t max_length) {
1276 const asn_INTEGER_specifics_t *specs =
1277 (const asn_INTEGER_specifics_t *)td->specifics;
1278 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
1279 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1280 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1281 INTEGER_t *st = *sptr;
1282 const asn_INTEGER_enum_map_t *emap;
1283 size_t emap_len;
1284 intmax_t value;
1285 int find_inside_map;
1286
1287 if(max_length == 0) return result_skipped;
1288
1289 if(st == NULL) {
1290 st = (INTEGER_t *)CALLOC(1, sizeof(*st));
1291 if(st == NULL) {
1292 return result_failed;
1293 }
1294 }
1295
1296 if(specs) {
1297 emap = specs->value2enum;
1298 emap_len = specs->map_count;
1299 if(specs->strict_enumeration) {
1300 find_inside_map = emap_len > 0;
1301 } else {
1302 find_inside_map = emap_len ? asn_random_between(0, 1) : 0;
1303 }
1304 } else {
1305 emap = 0;
1306 emap_len = 0;
1307 find_inside_map = 0;
1308 }
1309
1310 if(find_inside_map) {
1311 assert(emap_len > 0);
1312 value = emap[asn_random_between(0, emap_len - 1)].nat_value;
1313 } else {
Lev Walkin18741a92017-10-01 12:40:19 -07001314 const asn_per_constraints_t *ct;
Lev Walkina5972be2017-09-29 23:15:58 -07001315
1316 static const long variants[] = {
1317 -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384,
1318 -16383, -257, -256, -255, -254, -129, -128, -127,
1319 -126, -1, 0, 1, 126, 127, 128, 129,
1320 254, 255, 256, 257, 16383, 16384, 16385, 32767,
1321 32768, 32769, 65534, 65535, 65536, 65537};
1322 if(specs && specs->field_unsigned) {
1323 assert(variants[18] == 0);
1324 value = variants[asn_random_between(
1325 18, sizeof(variants) / sizeof(variants[0]) - 1)];
1326 } else {
1327 value = variants[asn_random_between(
1328 0, sizeof(variants) / sizeof(variants[0]) - 1)];
1329 }
1330
1331 if(!constraints) constraints = &td->encoding_constraints;
Lev Walkin18741a92017-10-01 12:40:19 -07001332 ct = constraints ? constraints->per_constraints : 0;
1333 if(ct && (ct->value.flags & APC_CONSTRAINED)) {
1334 if(value < ct->value.lower_bound || value > ct->value.upper_bound) {
1335 value = asn_random_between(ct->value.lower_bound,
1336 ct->value.upper_bound);
1337 }
1338 }
Lev Walkina5972be2017-09-29 23:15:58 -07001339 }
1340
Lev Walkina5972be2017-09-29 23:15:58 -07001341 if(asn_imax2INTEGER(st, value)) {
1342 if(st == *sptr) {
1343 ASN_STRUCT_RESET(*td, st);
1344 } else {
1345 ASN_STRUCT_FREE(*td, st);
1346 }
1347 return result_failed;
1348 } else {
Lev Walkin18741a92017-10-01 12:40:19 -07001349 *sptr = st;
Lev Walkina5972be2017-09-29 23:15:58 -07001350 result_ok.length = st->size;
1351 return result_ok;
1352 }
1353}