blob: 165f440b56cba4ca0f795aa3df22ae7e588bffce [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 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 Walkin72ec9092017-07-05 05:49:12 -0700152 "%" 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 Walkin72ec9092017-07-05 05:49:12 -0700163 (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX,
164 value);
165 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000166 } else if(plainOrXER && specs && specs->strict_enumeration) {
167 /*
168 * Here and earlier, we cannot encode the ENUMERATED values
169 * if there is no corresponding identifier.
170 */
171 ASN_DEBUG("ASN.1 forbids dealing with "
172 "unknown value of ENUMERATED type");
173 errno = EPERM;
174 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 }
176
177 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000178 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700180 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000181 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000183 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000185 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 p = scratch;
187 }
188 *p++ = h2c[*buf >> 4];
189 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000190 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 }
Lev Walkindb13f512004-07-19 17:30:25 +0000192 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000193 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000194
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195 wrote += p - scratch;
196 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
197}
198
199/*
200 * INTEGER specific human-readable output.
201 */
202int
Lev Walkin20696a42017-10-17 21:27:33 -0700203INTEGER_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
204 asn_app_consume_bytes_f *cb, void *app_key) {
205 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000206 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000207
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208 (void)ilevel;
209
Lev Walkind500a962005-11-27 13:06:56 +0000210 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000211 ret = cb("<absent>", 8, app_key);
212 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000213 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000214
Lev Walkin8e8078a2004-09-26 13:10:40 +0000215 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000216}
217
Lev Walkine0b56e02005-02-25 12:10:27 +0000218struct e2v_key {
219 const char *start;
220 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700221 const asn_INTEGER_enum_map_t *vemap;
222 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000223};
224static int
225INTEGER__compar_enum2value(const void *kp, const void *am) {
226 const struct e2v_key *key = (const struct e2v_key *)kp;
227 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
228 const char *ptr, *end, *name;
229
230 /* Remap the element (sort by different criterion) */
231 el = key->vemap + key->evmap[el - key->vemap];
232
233 /* Compare strings */
234 for(ptr = key->start, end = key->stop, name = el->enum_name;
235 ptr < end; ptr++, name++) {
Lev Walkin793982a2017-10-02 14:12:51 -0700236 if(*ptr != *name || !*name)
Lev Walkine0b56e02005-02-25 12:10:27 +0000237 return *(const unsigned char *)ptr
238 - *(const unsigned char *)name;
239 }
240 return name[0] ? -1 : 0;
241}
242
243static const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700244INTEGER_map_enum2value(const asn_INTEGER_specifics_t *specs, const char *lstart,
245 const char *lstop) {
246 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000247 int count = specs ? specs->map_count : 0;
248 struct e2v_key key;
249 const char *lp;
250
251 if(!count) return NULL;
252
253 /* Guaranteed: assert(lstart < lstop); */
254 /* Figure out the tag name */
255 for(lstart++, lp = lstart; lp < lstop; lp++) {
256 switch(*lp) {
257 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
258 case 0x2f: /* '/' */ case 0x3e: /* '>' */
259 break;
260 default:
261 continue;
262 }
263 break;
264 }
265 if(lp == lstop) return NULL; /* No tag found */
266 lstop = lp;
267
268 key.start = lstart;
269 key.stop = lstop;
270 key.vemap = specs->value2enum;
271 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000272 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
273 specs->value2enum, count, sizeof(specs->value2enum[0]),
274 INTEGER__compar_enum2value);
275 if(el_found) {
276 /* Remap enum2value into value2enum */
277 el_found = key.vemap + key.evmap[el_found - key.vemap];
278 }
279 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000280}
281
282static int
283INTEGER__compar_value2enum(const void *kp, const void *am) {
284 long a = *(const long *)kp;
285 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
286 long b = el->nat_value;
287 if(a < b) return -1;
288 else if(a == b) return 0;
289 else return 1;
290}
291
Lev Walkinc2350112005-03-29 17:19:53 +0000292const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700293INTEGER_map_value2enum(const asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000294 int count = specs ? specs->map_count : 0;
295 if(!count) return 0;
296 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
297 count, sizeof(specs->value2enum[0]),
298 INTEGER__compar_value2enum);
299}
300
Lev Walkinc744a022006-09-15 18:33:25 +0000301static int
302INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
303 void *p = MALLOC(min_size + 1);
304 if(p) {
305 void *b = st->buf;
306 st->size = 0;
307 st->buf = p;
308 FREEMEM(b);
309 return 0;
310 } else {
311 return -1;
312 }
313}
314
Lev Walkind703ff42004-10-21 11:21:25 +0000315/*
316 * Decode the chunk of XML text encoding INTEGER.
317 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000318static enum xer_pbd_rval
Lev Walkin20696a42017-10-17 21:27:33 -0700319INTEGER__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
320 const void *chunk_buf, size_t chunk_size) {
321 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700322 intmax_t dec_value;
323 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000324 const char *lp;
325 const char *lstart = (const char *)chunk_buf;
326 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000327 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700328 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000329 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000330 ST_WAITDIGITS,
331 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700332 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000333 ST_HEXDIGIT1,
334 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700335 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000336 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700337 ST_END_ENUM,
338 ST_UNEXPECTED
339 } state = ST_LEADSPACE;
340 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
341 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000342
Lev Walkinc744a022006-09-15 18:33:25 +0000343 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000344 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
345 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000346
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000347 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
348 return XPBD_SYSTEM_FAILURE;
349
Lev Walkind703ff42004-10-21 11:21:25 +0000350 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000351 * We may have received a tag here. It will be processed inline.
352 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000353 */
Lev Walkinb3751942012-09-02 19:36:47 -0700354 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000355 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000356 switch(lv) {
357 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000358 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700359 case ST_LEADSPACE:
360 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700361 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000362 case ST_SKIPSPHEX:
363 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700364 case ST_DIGITS:
365 dec_value_end = lp;
366 state = ST_DIGITS_TRAILSPACE;
367 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700368 case ST_HEXCOLON:
369 state = ST_HEXDIGITS_TRAILSPACE;
370 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000371 default:
372 break;
373 }
Lev Walkind703ff42004-10-21 11:21:25 +0000374 break;
375 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700376 if(state == ST_LEADSPACE) {
377 dec_value = 0;
378 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000379 state = ST_WAITDIGITS;
380 continue;
381 }
382 break;
383 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700384 if(state == ST_LEADSPACE) {
385 dec_value = 0;
386 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000387 state = ST_WAITDIGITS;
388 continue;
389 }
390 break;
391 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
392 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000393 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700394 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000395 case ST_SKIPSPHEX: /* Fall through */
396 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700397 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000398 state = ST_HEXDIGIT2;
399 continue;
400 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700401 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000402 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700403 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000404 continue;
405 case ST_HEXCOLON:
406 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700407 case ST_LEADSPACE:
408 dec_value = 0;
409 dec_value_start = lp;
410 /* FALL THROUGH */
411 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000412 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700413 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700414 default:
415 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000416 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700417 break;
418 case 0x3c: /* '<', start of XML encoded enumeration */
419 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000420 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000421 el = INTEGER_map_enum2value(
Lev Walkind62a6622017-08-22 02:31:02 -0700422 (const asn_INTEGER_specifics_t *)
Lev Walkine0b56e02005-02-25 12:10:27 +0000423 td->specifics, lstart, lstop);
424 if(el) {
425 ASN_DEBUG("Found \"%s\" => %ld",
426 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700427 dec_value = el->nat_value;
428 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000429 lp = lstop - 1;
430 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000431 }
432 ASN_DEBUG("Unknown identifier for INTEGER");
433 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000434 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000435 case 0x3a: /* ':' */
436 if(state == ST_HEXCOLON) {
437 /* This colon is expected */
438 state = ST_HEXDIGIT1;
439 continue;
440 } else if(state == ST_DIGITS) {
441 /* The colon here means that we have
442 * decoded the first two hexadecimal
443 * places as a decimal value.
444 * Switch decoding mode. */
445 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000446 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700447 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000448 lp = lstart - 1;
449 continue;
450 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400451 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000452 break;
453 }
454 /* [A-Fa-f] */
455 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
456 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
457 switch(state) {
458 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700459 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000460 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700461 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
462 hex_value += 10;
463 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000464 state = ST_HEXDIGIT2;
465 continue;
466 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700467 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
468 hex_value += 10;
469 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000470 state = ST_HEXCOLON;
471 continue;
472 case ST_DIGITS:
473 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000474 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700475 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000476 lp = lstart - 1;
477 continue;
478 default:
479 break;
480 }
481 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000482 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000483
484 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700485 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400486 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700487 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000488 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000489 }
490
Lev Walkinc744a022006-09-15 18:33:25 +0000491 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700492 case ST_END_ENUM:
493 /* Got a complete and valid enumeration encoded as a tag. */
494 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000495 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700496 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700497 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700498 case ST_DIGITS_TRAILSPACE:
499 /* The last symbol encountered was a digit. */
Lev Walkinb7c58992017-10-06 16:36:32 -0700500 switch(asn_strtoimax_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700501 case ASN_STRTOX_OK:
Lev Walkinb7c58992017-10-06 16:36:32 -0700502 if(dec_value >= LONG_MIN && dec_value <= LONG_MAX) {
503 break;
504 } else {
505 /*
506 * We model INTEGER on long for XER,
507 * to avoid rewriting all the tests at once.
508 */
509 ASN_DEBUG("INTEGER exceeds long range");
510 /* Fall through */
511 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700512 case ASN_STRTOX_ERROR_RANGE:
Lev Walkinb7c58992017-10-06 16:36:32 -0700513 ASN_DEBUG("INTEGER decode %s hit range limit", td->name);
Lev Walkine09f9f12012-09-02 23:06:35 -0700514 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700515 case ASN_STRTOX_ERROR_INVAL:
516 case ASN_STRTOX_EXPECT_MORE:
517 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700518 return XPBD_BROKEN_ENCODING;
519 }
Lev Walkinc744a022006-09-15 18:33:25 +0000520 break;
521 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700522 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000523 st->buf[st->size] = 0; /* Just in case termination */
524 return XPBD_BODY_CONSUMED;
525 case ST_HEXDIGIT1:
526 case ST_HEXDIGIT2:
527 case ST_SKIPSPHEX:
528 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700529 case ST_LEADSPACE:
530 /* Content not found */
531 return XPBD_NOT_BODY_IGNORE;
532 case ST_WAITDIGITS:
533 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700534 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
535 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000536 }
Lev Walkind703ff42004-10-21 11:21:25 +0000537
Lev Walkine09f9f12012-09-02 23:06:35 -0700538 /*
539 * Convert the result of parsing of enumeration or a straight
540 * decimal value into a BER representation.
541 */
Lev Walkinb7c58992017-10-06 16:36:32 -0700542 if(asn_imax2INTEGER(st, dec_value)) {
543 ASN_DEBUG("INTEGER decode %s conversion failed", td->name);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000544 return XPBD_SYSTEM_FAILURE;
Lev Walkinb7c58992017-10-06 16:36:32 -0700545 }
Lev Walkind703ff42004-10-21 11:21:25 +0000546
Lev Walkin0fab1a62005-03-09 22:19:25 +0000547 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000548}
549
550asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700551INTEGER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -0700552 const asn_TYPE_descriptor_t *td, void **sptr,
553 const char *opt_mname, const void *buf_ptr, size_t size) {
554 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000555 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000556 buf_ptr, size, INTEGER__xer_body_decode);
557}
558
Lev Walkina9cc46e2004-09-22 16:06:28 +0000559asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700560INTEGER_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
561 int ilevel, enum xer_encoder_flags_e flags,
562 asn_app_consume_bytes_f *cb, void *app_key) {
563 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000564 asn_enc_rval_t er;
565
566 (void)ilevel;
567 (void)flags;
568
Lev Walkind500a962005-11-27 13:06:56 +0000569 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700570 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000571
Lev Walkine0b56e02005-02-25 12:10:27 +0000572 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700573 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000574
Lev Walkin7c1dc052016-03-14 03:08:15 -0700575 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000576}
577
Lev Walkind7703cf2012-09-03 01:45:03 -0700578#ifndef ASN_DISABLE_PER_SUPPORT
579
Lev Walkin59b176e2005-11-26 11:25:14 +0000580asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700581INTEGER_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
582 const asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700583 const asn_per_constraints_t *constraints, void **sptr,
584 asn_per_data_t *pd) {
Lev Walkind62a6622017-08-22 02:31:02 -0700585 const asn_INTEGER_specifics_t *specs =
586 (const asn_INTEGER_specifics_t *)td->specifics;
587 asn_dec_rval_t rval = { RC_OK, 0 };
Lev Walkin59b176e2005-11-26 11:25:14 +0000588 INTEGER_t *st = (INTEGER_t *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -0700589 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000590 int repeat;
591
592 (void)opt_codec_ctx;
593
594 if(!st) {
595 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700596 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000597 }
598
Lev Walkina5972be2017-09-29 23:15:58 -0700599 if(!constraints) constraints = td->encoding_constraints.per_constraints;
Lev Walkin59b176e2005-11-26 11:25:14 +0000600 ct = constraints ? &constraints->value : 0;
601
602 if(ct && ct->flags & APC_EXTENSIBLE) {
603 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700604 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 if(inext) ct = 0;
606 }
607
608 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000609 st->buf = 0;
610 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000611 if(ct) {
612 if(ct->flags & APC_SEMI_CONSTRAINED) {
613 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700614 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000615 st->size = 1;
616 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
617 size_t size = (ct->range_bits + 7) >> 3;
618 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700619 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000620 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000621 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000622 }
623
Lev Walkin6c527842014-02-09 04:34:54 -0800624 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000625 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800626 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000627 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
628 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800629 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700630 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800631
632 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700633 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800634 if(uper_get_constrained_whole_number(pd,
635 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700636 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800637 ASN_DEBUG("Got value %lu + low %ld",
638 uvalue, ct->lower_bound);
639 uvalue += ct->lower_bound;
640 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700641 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800642 } else {
Lev Walkin38a91df2017-09-17 23:52:33 -0700643 unsigned long uvalue = 0;
644 long svalue;
Lev Walkin6c527842014-02-09 04:34:54 -0800645 if(uper_get_constrained_whole_number(pd,
Lev Walkin38a91df2017-09-17 23:52:33 -0700646 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700647 ASN__DECODE_STARVED;
Lev Walkin38a91df2017-09-17 23:52:33 -0700648 ASN_DEBUG("Got value %lu + low %ld",
649 uvalue, ct->lower_bound);
Lev Walkincc6a76b2017-10-07 16:23:16 -0700650 if(per_long_range_unrebase(uvalue, ct->lower_bound,
651 ct->upper_bound, &svalue)
652 || asn_long2INTEGER(st, svalue)) {
653 ASN__DECODE_FAILED;
654 }
Lev Walkin6c527842014-02-09 04:34:54 -0800655 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000656 return rval;
657 }
658 } else {
659 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
660 }
661
662 /* X.691, #12.2.3, #12.2.4 */
663 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700664 ssize_t len = 0;
665 void *p = NULL;
666 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000667
668 /* Get the PER length */
Lev Walkin9d1b45f2017-10-01 17:04:48 -0700669 len = uper_get_length(pd, -1, 0, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700670 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000671
672 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700673 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000674 st->buf = (uint8_t *)p;
675
676 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700677 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000678 st->size += len;
679 } while(repeat);
680 st->buf[st->size] = 0; /* JIC */
681
682 /* #12.2.3 */
683 if(ct && ct->lower_bound) {
684 /*
685 * TODO: replace by in-place arithmetics.
686 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700687 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000688 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700689 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700690 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700691 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000692 }
693
694 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000695}
696
Lev Walkin523de9e2006-08-18 01:34:18 +0000697asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700698INTEGER_encode_uper(const asn_TYPE_descriptor_t *td,
699 const asn_per_constraints_t *constraints, const void *sptr,
Lev Walkin494fb702017-08-07 20:07:00 -0700700 asn_per_outp_t *po) {
Lev Walkind62a6622017-08-22 02:31:02 -0700701 const asn_INTEGER_specifics_t *specs =
702 (const asn_INTEGER_specifics_t *)td->specifics;
703 asn_enc_rval_t er;
Lev Walkin20696a42017-10-17 21:27:33 -0700704 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin523de9e2006-08-18 01:34:18 +0000705 const uint8_t *buf;
706 const uint8_t *end;
Lev Walkin494fb702017-08-07 20:07:00 -0700707 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000708 long value = 0;
709
Lev Walkin7c1dc052016-03-14 03:08:15 -0700710 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000711
Lev Walkina5972be2017-09-29 23:15:58 -0700712 if(!constraints) constraints = td->encoding_constraints.per_constraints;
Lev Walkin523de9e2006-08-18 01:34:18 +0000713 ct = constraints ? &constraints->value : 0;
714
715 er.encoded = 0;
716
717 if(ct) {
718 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000719 if(specs && specs->field_unsigned) {
720 unsigned long uval;
721 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700722 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000723 /* Check proper range */
724 if(ct->flags & APC_SEMI_CONSTRAINED) {
725 if(uval < (unsigned long)ct->lower_bound)
726 inext = 1;
727 } else if(ct->range_bits >= 0) {
728 if(uval < (unsigned long)ct->lower_bound
729 || uval > (unsigned long)ct->upper_bound)
730 inext = 1;
731 }
Lev Walkin67afc362017-10-03 15:47:26 -0700732 ASN_DEBUG("Value %lu (%02x/%zu) lb %lu ub %lu %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000733 uval, st->buf[0], st->size,
734 ct->lower_bound, ct->upper_bound,
735 inext ? "ext" : "fix");
736 value = uval;
737 } else {
738 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700739 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000740 /* Check proper range */
741 if(ct->flags & APC_SEMI_CONSTRAINED) {
742 if(value < ct->lower_bound)
743 inext = 1;
744 } else if(ct->range_bits >= 0) {
745 if(value < ct->lower_bound
746 || value > ct->upper_bound)
747 inext = 1;
748 }
Lev Walkin67afc362017-10-03 15:47:26 -0700749 ASN_DEBUG("Value %ld (%02x/%zu) lb %ld ub %ld %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000750 value, st->buf[0], st->size,
751 ct->lower_bound, ct->upper_bound,
752 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000753 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000754 if(ct->flags & APC_EXTENSIBLE) {
755 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700756 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000757 if(inext) ct = 0;
758 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700759 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000760 }
761 }
762
763
Lev Walkin6c527842014-02-09 04:34:54 -0800764 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000765 if(ct && ct->range_bits >= 0) {
Lev Walkincc6a76b2017-10-07 16:23:16 -0700766 unsigned long v;
Lev Walkin6c527842014-02-09 04:34:54 -0800767 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800768 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
769 value, value - ct->lower_bound, ct->range_bits);
Lev Walkincc6a76b2017-10-07 16:23:16 -0700770 if(per_long_range_rebase(value, ct->lower_bound, ct->upper_bound, &v)) {
771 ASN__ENCODE_FAILED;
772 }
773 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
774 ASN__ENCODE_FAILED;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700775 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000776 }
777
778 if(ct && ct->lower_bound) {
779 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
780 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700781 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000782 }
783
784 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
Lev Walkin5d947a82017-10-03 01:04:03 -0700785 int need_eom = 0;
786 ssize_t mayEncode = uper_put_length(po, end - buf, &need_eom);
787 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700788 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000789 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700790 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000791 buf += mayEncode;
Lev Walkin5d947a82017-10-03 01:04:03 -0700792 if(need_eom && uper_put_length(po, 0, 0)) ASN__ENCODE_FAILED;
793 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000794
Lev Walkin7c1dc052016-03-14 03:08:15 -0700795 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000796}
797
Lev Walkind7703cf2012-09-03 01:45:03 -0700798#endif /* ASN_DISABLE_PER_SUPPORT */
799
Lev Walkinc346f902017-10-09 20:24:40 -0700800static intmax_t
801asn__integer_convert(const uint8_t *b, const uint8_t *end) {
802 uintmax_t value;
Lev Walkin642b92f2017-09-17 22:16:02 -0700803
Lev Walkinc346f902017-10-09 20:24:40 -0700804 /* Perform the sign initialization */
805 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
806 if((*b >> 7)) {
807 value = (uintmax_t)(-1);
Lev Walkin642b92f2017-09-17 22:16:02 -0700808 } else {
809 value = 0;
810 }
811
812 /* Conversion engine */
Lev Walkinc346f902017-10-09 20:24:40 -0700813 for(; b < end; b++) {
814 value = (value << 8) | *b;
Lev Walkin642b92f2017-09-17 22:16:02 -0700815 }
816
817 return value;
818}
819
Lev Walkinf15320b2004-06-03 03:38:44 +0000820int
Lev Walkin72ec9092017-07-05 05:49:12 -0700821asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000822 uint8_t *b, *end;
823 size_t size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000824
825 /* Sanity checking */
826 if(!iptr || !iptr->buf || !lptr) {
827 errno = EINVAL;
828 return -1;
829 }
830
831 /* Cache the begin/end of the buffer */
832 b = iptr->buf; /* Start of the INTEGER buffer */
833 size = iptr->size;
834 end = b + size; /* Where to stop */
835
Lev Walkin642b92f2017-09-17 22:16:02 -0700836 if(size > sizeof(intmax_t)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000837 uint8_t *end1 = end - 1;
838 /*
839 * Slightly more advanced processing,
Lev Walkin642b92f2017-09-17 22:16:02 -0700840 * able to process INTEGERs with >sizeof(intmax_t) bytes
Lev Walkin72ec9092017-07-05 05:49:12 -0700841 * when the actual value is small, e.g. for intmax_t == int32_t
842 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000843 */
844 /* Skip out the insignificant leading bytes */
845 for(; b < end1; b++) {
846 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700847 case 0x00: if((b[1] & 0x80) == 0) continue; break;
848 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000849 }
850 break;
851 }
852
853 size = end - b;
Lev Walkin642b92f2017-09-17 22:16:02 -0700854 if(size > sizeof(intmax_t)) {
855 /* Still cannot fit the sizeof(intmax_t) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000856 errno = ERANGE;
857 return -1;
858 }
859 }
860
861 /* Shortcut processing of a corner case */
862 if(end == b) {
863 *lptr = 0;
864 return 0;
865 }
866
Lev Walkinc346f902017-10-09 20:24:40 -0700867 *lptr = asn__integer_convert(b, end);
Lev Walkinf15320b2004-06-03 03:38:44 +0000868 return 0;
869}
Lev Walkind703ff42004-10-21 11:21:25 +0000870
Lev Walkin72ec9092017-07-05 05:49:12 -0700871/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000872int
Lev Walkin72ec9092017-07-05 05:49:12 -0700873asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000874 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700875 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000876 size_t size;
877
878 if(!iptr || !iptr->buf || !lptr) {
879 errno = EINVAL;
880 return -1;
881 }
882
883 b = iptr->buf;
884 size = iptr->size;
885 end = b + size;
886
887 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700888 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000889 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700890 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000891 errno = ERANGE;
892 return -1;
893 }
894 }
895
896 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700897 for(value = 0; b < end; b++)
898 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000899
Lev Walkin72ec9092017-07-05 05:49:12 -0700900 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000901 return 0;
902}
903
904int
Lev Walkin72ec9092017-07-05 05:49:12 -0700905asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
906 uint8_t *buf;
907 uint8_t *end;
908 uint8_t *b;
909 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000910
Lev Walkinaa067a92017-10-03 16:09:05 -0700911 if(value <= ((~(uintmax_t)0) >> 1)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700912 return asn_imax2INTEGER(st, value);
913 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000914
Lev Walkin72ec9092017-07-05 05:49:12 -0700915 buf = (uint8_t *)MALLOC(1 + sizeof(value));
916 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000917
Lev Walkin72ec9092017-07-05 05:49:12 -0700918 end = buf + (sizeof(value) + 1);
919 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
920 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
921 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000922
Lev Walkin72ec9092017-07-05 05:49:12 -0700923 if(st->buf) FREEMEM(st->buf);
924 st->buf = buf;
925 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000926
927 return 0;
928}
929
930int
Lev Walkin72ec9092017-07-05 05:49:12 -0700931asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000932 uint8_t *buf, *bp;
933 uint8_t *p;
934 uint8_t *pstart;
935 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000936 int littleEndian = 1; /* Run-time detection */
937 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000938
939 if(!st) {
940 errno = EINVAL;
941 return -1;
942 }
943
Lev Walkin290b4d62017-08-28 17:48:35 -0700944 buf = (uint8_t *)(long *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000945 if(!buf) return -1;
946
Lev Walkind7ad5612004-10-26 08:20:46 +0000947 if(*(char *)&littleEndian) {
948 pstart = (uint8_t *)&value + sizeof(value) - 1;
949 pend1 = (uint8_t *)&value;
950 add = -1;
951 } else {
952 pstart = (uint8_t *)&value;
953 pend1 = pstart + sizeof(value) - 1;
954 add = 1;
955 }
956
Lev Walkind703ff42004-10-21 11:21:25 +0000957 /*
958 * If the contents octet consists of more than one octet,
959 * then bits of the first octet and bit 8 of the second octet:
960 * a) shall not all be ones; and
961 * b) shall not all be zero.
962 */
Lev Walkin33700162004-10-26 09:03:31 +0000963 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000964 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000965 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000966 continue;
967 break;
Lev Walkin33700162004-10-26 09:03:31 +0000968 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000969 continue;
970 break;
971 }
972 break;
973 }
974 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700975 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000976 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000977
978 if(st->buf) FREEMEM(st->buf);
979 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000980 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000981
982 return 0;
983}
Lev Walkinb3751942012-09-02 19:36:47 -0700984
Lev Walkin72ec9092017-07-05 05:49:12 -0700985int
986asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
987 intmax_t v;
988 if(asn_INTEGER2imax(iptr, &v) == 0) {
989 if(v < LONG_MIN || v > LONG_MAX) {
990 errno = ERANGE;
991 return -1;
992 }
993 *l = v;
994 return 0;
995 } else {
996 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700997 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700998}
Lev Walkincad560a2013-03-16 07:00:58 -0700999
Lev Walkin72ec9092017-07-05 05:49:12 -07001000int
1001asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
1002 uintmax_t v;
1003 if(asn_INTEGER2umax(iptr, &v) == 0) {
1004 if(v > ULONG_MAX) {
1005 errno = ERANGE;
1006 return -1;
1007 }
1008 *l = v;
1009 return 0;
1010 } else {
1011 return -1;
1012 }
1013}
1014
1015int
1016asn_long2INTEGER(INTEGER_t *st, long value) {
1017 return asn_imax2INTEGER(st, value);
1018}
1019
1020int
1021asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1022 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -07001023}
1024
1025/*
1026 * Parse the number in the given string until the given *end position,
1027 * returning the position after the last parsed character back using the
1028 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -07001029 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -07001030 */
Lev Walkin72ec9092017-07-05 05:49:12 -07001031enum asn_strtox_result_e
1032asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001033 int sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001034 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -07001035
Lev Walkinaa067a92017-10-03 16:09:05 -07001036#define ASN1_INTMAX_MAX ((~(uintmax_t)0) >> 1)
Lev Walkinaa067a92017-10-03 16:09:05 -07001037 const intmax_t upper_boundary = ASN1_INTMAX_MAX / 10;
1038 intmax_t last_digit_max = ASN1_INTMAX_MAX % 10;
Lev Walkin588bf0f2017-10-13 23:51:16 -07001039#undef ASN1_INTMAX_MAX
Lev Walkine09f9f12012-09-02 23:06:35 -07001040
Lev Walkin72ec9092017-07-05 05:49:12 -07001041 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001042
1043 switch(*str) {
1044 case '-':
1045 last_digit_max++;
1046 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001047 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001048 case '+':
1049 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001050 if(str >= *end) {
1051 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001052 return ASN_STRTOX_EXPECT_MORE;
Lev Walkincad560a2013-03-16 07:00:58 -07001053 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001054 }
1055
Lev Walkin72ec9092017-07-05 05:49:12 -07001056 for(value = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001057 switch(*str) {
1058 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1059 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1060 int d = *str - '0';
Lev Walkin72ec9092017-07-05 05:49:12 -07001061 if(value < upper_boundary) {
1062 value = value * 10 + d;
1063 } else if(value == upper_boundary) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001064 if(d <= last_digit_max) {
1065 if(sign > 0) {
Lev Walkin72ec9092017-07-05 05:49:12 -07001066 value = value * 10 + d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001067 } else {
1068 sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001069 value = -value * 10 - d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001070 }
1071 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001072 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001073 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001074 }
1075 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001076 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001077 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001078 }
1079 }
1080 continue;
1081 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001082 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001083 *intp = sign * value;
1084 return ASN_STRTOX_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001085 }
1086 }
1087
Lev Walkincad560a2013-03-16 07:00:58 -07001088 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001089 *intp = sign * value;
1090 return ASN_STRTOX_OK;
1091}
1092
Lev Walkin588bf0f2017-10-13 23:51:16 -07001093/*
1094 * Parse the number in the given string until the given *end position,
1095 * returning the position after the last parsed character back using the
1096 * same (*end) pointer.
1097 * WARNING: This behavior is different from the standard strtoul/strtoumax(3).
1098 */
1099enum asn_strtox_result_e
1100asn_strtoumax_lim(const char *str, const char **end, uintmax_t *uintp) {
1101 uintmax_t value;
1102
1103#define ASN1_UINTMAX_MAX ((~(uintmax_t)0))
1104 const uintmax_t upper_boundary = ASN1_UINTMAX_MAX / 10;
1105 uintmax_t last_digit_max = ASN1_UINTMAX_MAX % 10;
1106#undef ASN1_UINTMAX_MAX
1107
1108 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
1109
1110 switch(*str) {
1111 case '-':
1112 return ASN_STRTOX_ERROR_INVAL;
1113 case '+':
1114 str++;
1115 if(str >= *end) {
1116 *end = str;
1117 return ASN_STRTOX_EXPECT_MORE;
1118 }
1119 }
1120
1121 for(value = 0; str < (*end); str++) {
1122 switch(*str) {
1123 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1124 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1125 unsigned int d = *str - '0';
1126 if(value < upper_boundary) {
1127 value = value * 10 + d;
1128 } else if(value == upper_boundary) {
1129 if(d <= last_digit_max) {
1130 value = value * 10 + d;
1131 } else {
1132 *end = str;
1133 return ASN_STRTOX_ERROR_RANGE;
1134 }
1135 } else {
1136 *end = str;
1137 return ASN_STRTOX_ERROR_RANGE;
1138 }
1139 }
1140 continue;
1141 default:
1142 *end = str;
1143 *uintp = value;
1144 return ASN_STRTOX_EXTRA_DATA;
1145 }
1146 }
1147
1148 *end = str;
1149 *uintp = value;
1150 return ASN_STRTOX_OK;
1151}
1152
Lev Walkin72ec9092017-07-05 05:49:12 -07001153enum asn_strtox_result_e
1154asn_strtol_lim(const char *str, const char **end, long *lp) {
1155 intmax_t value;
1156 switch(asn_strtoimax_lim(str, end, &value)) {
1157 case ASN_STRTOX_ERROR_RANGE:
1158 return ASN_STRTOX_ERROR_RANGE;
1159 case ASN_STRTOX_ERROR_INVAL:
1160 return ASN_STRTOX_ERROR_INVAL;
1161 case ASN_STRTOX_EXPECT_MORE:
1162 return ASN_STRTOX_EXPECT_MORE;
1163 case ASN_STRTOX_OK:
1164 if(value >= LONG_MIN && value <= LONG_MAX) {
1165 *lp = value;
1166 return ASN_STRTOX_OK;
1167 } else {
1168 return ASN_STRTOX_ERROR_RANGE;
1169 }
1170 case ASN_STRTOX_EXTRA_DATA:
1171 if(value >= LONG_MIN && value <= LONG_MAX) {
1172 *lp = value;
1173 return ASN_STRTOX_EXTRA_DATA;
1174 } else {
1175 return ASN_STRTOX_ERROR_RANGE;
1176 }
1177 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001178
1179 assert(!"Unreachable");
1180 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001181}
1182
Lev Walkin588bf0f2017-10-13 23:51:16 -07001183enum asn_strtox_result_e
1184asn_strtoul_lim(const char *str, const char **end, unsigned long *ulp) {
1185 uintmax_t value;
1186 switch(asn_strtoumax_lim(str, end, &value)) {
1187 case ASN_STRTOX_ERROR_RANGE:
1188 return ASN_STRTOX_ERROR_RANGE;
1189 case ASN_STRTOX_ERROR_INVAL:
1190 return ASN_STRTOX_ERROR_INVAL;
1191 case ASN_STRTOX_EXPECT_MORE:
1192 return ASN_STRTOX_EXPECT_MORE;
1193 case ASN_STRTOX_OK:
1194 if(value <= ULONG_MAX) {
1195 *ulp = value;
1196 return ASN_STRTOX_OK;
1197 } else {
1198 return ASN_STRTOX_ERROR_RANGE;
1199 }
1200 case ASN_STRTOX_EXTRA_DATA:
1201 if(value <= ULONG_MAX) {
1202 *ulp = value;
1203 return ASN_STRTOX_EXTRA_DATA;
1204 } else {
1205 return ASN_STRTOX_ERROR_RANGE;
1206 }
1207 }
1208
1209 assert(!"Unreachable");
1210 return ASN_STRTOX_ERROR_INVAL;
1211}
1212
Lev Walkincd2f48e2017-08-10 02:14:59 -07001213int
1214INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1215 const void *bptr) {
1216 const INTEGER_t *a = aptr;
1217 const INTEGER_t *b = bptr;
1218
1219 (void)td;
1220
1221 if(a && b) {
1222 if(a->size && b->size) {
1223 int sign_a = (a->buf[0] & 0x80) ? -1 : 1;
1224 int sign_b = (b->buf[0] & 0x80) ? -1 : 1;
1225
1226 if(sign_a < sign_b) return -1;
1227 if(sign_a > sign_b) return 1;
1228
1229 /* The shortest integer wins, unless comparing negatives */
1230 if(a->size < b->size) {
1231 return -1 * sign_a;
1232 } else if(a->size > b->size) {
1233 return 1 * sign_b;
1234 }
1235
1236 return sign_a * memcmp(a->buf, b->buf, a->size);
1237 } else if(a->size) {
1238 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1239 return (1) * sign;
1240 } else if(b->size) {
1241 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1242 return (-1) * sign;
1243 } else {
1244 return 0;
1245 }
1246 } else if(!a && !b) {
1247 return 0;
1248 } else if(!a) {
1249 return -1;
1250 } else {
1251 return 1;
1252 }
1253
1254}
1255
Lev Walkina5972be2017-09-29 23:15:58 -07001256asn_random_fill_result_t
1257INTEGER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1258 const asn_encoding_constraints_t *constraints,
1259 size_t max_length) {
1260 const asn_INTEGER_specifics_t *specs =
1261 (const asn_INTEGER_specifics_t *)td->specifics;
1262 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
1263 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1264 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1265 INTEGER_t *st = *sptr;
1266 const asn_INTEGER_enum_map_t *emap;
1267 size_t emap_len;
1268 intmax_t value;
1269 int find_inside_map;
1270
1271 if(max_length == 0) return result_skipped;
1272
1273 if(st == NULL) {
1274 st = (INTEGER_t *)CALLOC(1, sizeof(*st));
1275 if(st == NULL) {
1276 return result_failed;
1277 }
1278 }
1279
1280 if(specs) {
1281 emap = specs->value2enum;
1282 emap_len = specs->map_count;
1283 if(specs->strict_enumeration) {
1284 find_inside_map = emap_len > 0;
1285 } else {
1286 find_inside_map = emap_len ? asn_random_between(0, 1) : 0;
1287 }
1288 } else {
1289 emap = 0;
1290 emap_len = 0;
1291 find_inside_map = 0;
1292 }
1293
1294 if(find_inside_map) {
1295 assert(emap_len > 0);
1296 value = emap[asn_random_between(0, emap_len - 1)].nat_value;
1297 } else {
Lev Walkin18741a92017-10-01 12:40:19 -07001298 const asn_per_constraints_t *ct;
Lev Walkina5972be2017-09-29 23:15:58 -07001299
1300 static const long variants[] = {
1301 -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384,
1302 -16383, -257, -256, -255, -254, -129, -128, -127,
1303 -126, -1, 0, 1, 126, 127, 128, 129,
1304 254, 255, 256, 257, 16383, 16384, 16385, 32767,
1305 32768, 32769, 65534, 65535, 65536, 65537};
1306 if(specs && specs->field_unsigned) {
1307 assert(variants[18] == 0);
1308 value = variants[asn_random_between(
1309 18, sizeof(variants) / sizeof(variants[0]) - 1)];
1310 } else {
1311 value = variants[asn_random_between(
1312 0, sizeof(variants) / sizeof(variants[0]) - 1)];
1313 }
1314
1315 if(!constraints) constraints = &td->encoding_constraints;
Lev Walkin18741a92017-10-01 12:40:19 -07001316 ct = constraints ? constraints->per_constraints : 0;
1317 if(ct && (ct->value.flags & APC_CONSTRAINED)) {
1318 if(value < ct->value.lower_bound || value > ct->value.upper_bound) {
1319 value = asn_random_between(ct->value.lower_bound,
1320 ct->value.upper_bound);
1321 }
1322 }
Lev Walkina5972be2017-09-29 23:15:58 -07001323 }
1324
Lev Walkina5972be2017-09-29 23:15:58 -07001325 if(asn_imax2INTEGER(st, value)) {
1326 if(st == *sptr) {
1327 ASN_STRUCT_RESET(*td, st);
1328 } else {
1329 ASN_STRUCT_FREE(*td, st);
1330 }
1331 return result_failed;
1332 } else {
Lev Walkin18741a92017-10-01 12:40:19 -07001333 *sptr = st;
Lev Walkina5972be2017-09-29 23:15:58 -07001334 result_ok.length = st->size;
1335 return result_ok;
1336 }
1337}