blob: f5dd44cdc114a49523c8555e70a1403d84c8c158 [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 Walkin5e033762004-09-29 13:26:15 +000059INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000060 int tag_mode, ber_tlv_tag_t tag,
61 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000062 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000063
64 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000065 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000066
67 /*
68 * Canonicalize integer in the buffer.
69 * (Remove too long sign extension, remove some first 0x00 bytes)
70 */
71 if(st->buf) {
72 uint8_t *buf = st->buf;
73 uint8_t *end1 = buf + st->size - 1;
74 int shift;
75
76 /* Compute the number of superfluous leading bytes */
77 for(; buf < end1; buf++) {
78 /*
79 * If the contents octets of an integer value encoding
80 * consist of more than one octet, then the bits of the
81 * first octet and bit 8 of the second octet:
82 * a) shall not all be ones; and
83 * b) shall not all be zero.
84 */
85 switch(*buf) {
86 case 0x00: if((buf[1] & 0x80) == 0)
87 continue;
88 break;
89 case 0xff: if((buf[1] & 0x80))
90 continue;
91 break;
92 }
93 break;
94 }
95
96 /* Remove leading superfluous bytes from the integer */
97 shift = buf - st->buf;
98 if(shift) {
99 uint8_t *nb = st->buf;
100 uint8_t *end;
101
102 st->size -= shift; /* New size, minus bad bytes */
103 end = nb + st->size;
104
105 for(; nb < end; nb++, buf++)
106 *nb = *buf;
107 }
108
109 } /* if(1) */
110
Lev Walkin8e8078a2004-09-26 13:10:40 +0000111 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000112}
113
Lev Walkind62a6622017-08-22 02:31:02 -0700114static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(
115 const asn_INTEGER_specifics_t *specs, const char *lstart,
116 const char *lstop);
Lev Walkine0b56e02005-02-25 12:10:27 +0000117
Lev Walkinf15320b2004-06-03 03:38:44 +0000118/*
119 * INTEGER specific human-readable output.
120 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000121static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700122INTEGER__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 -0700123 const asn_INTEGER_specifics_t *specs =
124 (const asn_INTEGER_specifics_t *)td->specifics;
125 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 uint8_t *buf = st->buf;
127 uint8_t *buf_end = st->buf + st->size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700128 intmax_t value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000129 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 char *p;
131 int ret;
132
Lev Walkin97f8edc2013-03-28 04:38:41 -0700133 if(specs && specs->field_unsigned)
Lev Walkin72ec9092017-07-05 05:49:12 -0700134 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
Lev Walkin97f8edc2013-03-28 04:38:41 -0700135 else
Lev Walkin72ec9092017-07-05 05:49:12 -0700136 ret = asn_INTEGER2imax(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000137
Lev Walkinf15320b2004-06-03 03:38:44 +0000138 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700139 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000140 const asn_INTEGER_enum_map_t *el;
141 size_t scrsize;
142 char *scr;
143
Lev Walkin97f8edc2013-03-28 04:38:41 -0700144 el = (value >= 0 || !specs || !specs->field_unsigned)
145 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000146 if(el) {
147 scrsize = el->enum_len + 32;
148 scr = (char *)alloca(scrsize);
149 if(plainOrXER == 0)
150 ret = snprintf(scr, scrsize,
Lev Walkin72ec9092017-07-05 05:49:12 -0700151 "%" PRIdMAX " (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000152 else
153 ret = snprintf(scr, scrsize,
154 "<%s/>", el->enum_name);
155 } else if(plainOrXER && specs && specs->strict_enumeration) {
156 ASN_DEBUG("ASN.1 forbids dealing with "
157 "unknown value of ENUMERATED type");
158 errno = EPERM;
159 return -1;
160 } else {
161 scrsize = sizeof(scratch);
162 scr = scratch;
Lev Walkin72ec9092017-07-05 05:49:12 -0700163 ret = snprintf(
164 scr, scrsize,
165 (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX,
166 value);
167 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000168 assert(ret > 0 && (size_t)ret < scrsize);
169 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
170 } else if(plainOrXER && specs && specs->strict_enumeration) {
171 /*
172 * Here and earlier, we cannot encode the ENUMERATED values
173 * if there is no corresponding identifier.
174 */
175 ASN_DEBUG("ASN.1 forbids dealing with "
176 "unknown value of ENUMERATED type");
177 errno = EPERM;
178 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 }
180
181 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000182 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700184 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000185 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000187 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000189 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 p = scratch;
191 }
192 *p++ = h2c[*buf >> 4];
193 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000194 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 }
Lev Walkindb13f512004-07-19 17:30:25 +0000196 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000197 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000198
Lev Walkina9cc46e2004-09-22 16:06:28 +0000199 wrote += p - scratch;
200 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
201}
202
203/*
204 * INTEGER specific human-readable output.
205 */
206int
Lev Walkin5e033762004-09-29 13:26:15 +0000207INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208 asn_app_consume_bytes_f *cb, void *app_key) {
209 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000210 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000211
212 (void)td;
213 (void)ilevel;
214
Lev Walkind500a962005-11-27 13:06:56 +0000215 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000216 ret = cb("<absent>", 8, app_key);
217 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000218 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000219
Lev Walkin8e8078a2004-09-26 13:10:40 +0000220 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000221}
222
Lev Walkine0b56e02005-02-25 12:10:27 +0000223struct e2v_key {
224 const char *start;
225 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700226 const asn_INTEGER_enum_map_t *vemap;
227 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000228};
229static int
230INTEGER__compar_enum2value(const void *kp, const void *am) {
231 const struct e2v_key *key = (const struct e2v_key *)kp;
232 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
233 const char *ptr, *end, *name;
234
235 /* Remap the element (sort by different criterion) */
236 el = key->vemap + key->evmap[el - key->vemap];
237
238 /* Compare strings */
239 for(ptr = key->start, end = key->stop, name = el->enum_name;
240 ptr < end; ptr++, name++) {
Lev Walkin793982a2017-10-02 14:12:51 -0700241 if(*ptr != *name || !*name)
Lev Walkine0b56e02005-02-25 12:10:27 +0000242 return *(const unsigned char *)ptr
243 - *(const unsigned char *)name;
244 }
245 return name[0] ? -1 : 0;
246}
247
248static const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700249INTEGER_map_enum2value(const asn_INTEGER_specifics_t *specs, const char *lstart,
250 const char *lstop) {
251 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000252 int count = specs ? specs->map_count : 0;
253 struct e2v_key key;
254 const char *lp;
255
256 if(!count) return NULL;
257
258 /* Guaranteed: assert(lstart < lstop); */
259 /* Figure out the tag name */
260 for(lstart++, lp = lstart; lp < lstop; lp++) {
261 switch(*lp) {
262 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
263 case 0x2f: /* '/' */ case 0x3e: /* '>' */
264 break;
265 default:
266 continue;
267 }
268 break;
269 }
270 if(lp == lstop) return NULL; /* No tag found */
271 lstop = lp;
272
273 key.start = lstart;
274 key.stop = lstop;
275 key.vemap = specs->value2enum;
276 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000277 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
278 specs->value2enum, count, sizeof(specs->value2enum[0]),
279 INTEGER__compar_enum2value);
280 if(el_found) {
281 /* Remap enum2value into value2enum */
282 el_found = key.vemap + key.evmap[el_found - key.vemap];
283 }
284 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000285}
286
287static int
288INTEGER__compar_value2enum(const void *kp, const void *am) {
289 long a = *(const long *)kp;
290 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
291 long b = el->nat_value;
292 if(a < b) return -1;
293 else if(a == b) return 0;
294 else return 1;
295}
296
Lev Walkinc2350112005-03-29 17:19:53 +0000297const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700298INTEGER_map_value2enum(const asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000299 int count = specs ? specs->map_count : 0;
300 if(!count) return 0;
301 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
302 count, sizeof(specs->value2enum[0]),
303 INTEGER__compar_value2enum);
304}
305
Lev Walkinc744a022006-09-15 18:33:25 +0000306static int
307INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
308 void *p = MALLOC(min_size + 1);
309 if(p) {
310 void *b = st->buf;
311 st->size = 0;
312 st->buf = p;
313 FREEMEM(b);
314 return 0;
315 } else {
316 return -1;
317 }
318}
319
Lev Walkind703ff42004-10-21 11:21:25 +0000320/*
321 * Decode the chunk of XML text encoding INTEGER.
322 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000323static enum xer_pbd_rval
324INTEGER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000325 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700326 intmax_t dec_value;
327 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000328 const char *lp;
329 const char *lstart = (const char *)chunk_buf;
330 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000331 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700332 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000333 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000334 ST_WAITDIGITS,
335 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700336 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000337 ST_HEXDIGIT1,
338 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700339 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000340 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700341 ST_END_ENUM,
342 ST_UNEXPECTED
343 } state = ST_LEADSPACE;
344 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
345 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000346
Lev Walkinc744a022006-09-15 18:33:25 +0000347 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000348 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
349 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000350
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000351 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
352 return XPBD_SYSTEM_FAILURE;
353
Lev Walkind703ff42004-10-21 11:21:25 +0000354 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000355 * We may have received a tag here. It will be processed inline.
356 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000357 */
Lev Walkinb3751942012-09-02 19:36:47 -0700358 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000359 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000360 switch(lv) {
361 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000362 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700363 case ST_LEADSPACE:
364 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700365 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000366 case ST_SKIPSPHEX:
367 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700368 case ST_DIGITS:
369 dec_value_end = lp;
370 state = ST_DIGITS_TRAILSPACE;
371 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700372 case ST_HEXCOLON:
373 state = ST_HEXDIGITS_TRAILSPACE;
374 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000375 default:
376 break;
377 }
Lev Walkind703ff42004-10-21 11:21:25 +0000378 break;
379 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700380 if(state == ST_LEADSPACE) {
381 dec_value = 0;
382 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000383 state = ST_WAITDIGITS;
384 continue;
385 }
386 break;
387 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700388 if(state == ST_LEADSPACE) {
389 dec_value = 0;
390 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000391 state = ST_WAITDIGITS;
392 continue;
393 }
394 break;
395 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
396 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000397 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700398 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000399 case ST_SKIPSPHEX: /* Fall through */
400 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700401 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000402 state = ST_HEXDIGIT2;
403 continue;
404 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700405 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000406 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700407 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000408 continue;
409 case ST_HEXCOLON:
410 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700411 case ST_LEADSPACE:
412 dec_value = 0;
413 dec_value_start = lp;
414 /* FALL THROUGH */
415 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000416 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700417 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700418 default:
419 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000420 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700421 break;
422 case 0x3c: /* '<', start of XML encoded enumeration */
423 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000424 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000425 el = INTEGER_map_enum2value(
Lev Walkind62a6622017-08-22 02:31:02 -0700426 (const asn_INTEGER_specifics_t *)
Lev Walkine0b56e02005-02-25 12:10:27 +0000427 td->specifics, lstart, lstop);
428 if(el) {
429 ASN_DEBUG("Found \"%s\" => %ld",
430 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700431 dec_value = el->nat_value;
432 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000433 lp = lstop - 1;
434 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000435 }
436 ASN_DEBUG("Unknown identifier for INTEGER");
437 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000438 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000439 case 0x3a: /* ':' */
440 if(state == ST_HEXCOLON) {
441 /* This colon is expected */
442 state = ST_HEXDIGIT1;
443 continue;
444 } else if(state == ST_DIGITS) {
445 /* The colon here means that we have
446 * decoded the first two hexadecimal
447 * places as a decimal value.
448 * Switch decoding mode. */
449 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000450 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700451 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000452 lp = lstart - 1;
453 continue;
454 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400455 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000456 break;
457 }
458 /* [A-Fa-f] */
459 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
460 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
461 switch(state) {
462 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700463 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000464 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700465 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
466 hex_value += 10;
467 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000468 state = ST_HEXDIGIT2;
469 continue;
470 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700471 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
472 hex_value += 10;
473 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000474 state = ST_HEXCOLON;
475 continue;
476 case ST_DIGITS:
477 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000478 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700479 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000480 lp = lstart - 1;
481 continue;
482 default:
483 break;
484 }
485 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000486 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000487
488 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700489 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400490 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700491 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000492 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000493 }
494
Lev Walkinc744a022006-09-15 18:33:25 +0000495 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700496 case ST_END_ENUM:
497 /* Got a complete and valid enumeration encoded as a tag. */
498 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000499 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700500 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700501 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700502 case ST_DIGITS_TRAILSPACE:
503 /* The last symbol encountered was a digit. */
Lev Walkinb7c58992017-10-06 16:36:32 -0700504 switch(asn_strtoimax_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700505 case ASN_STRTOX_OK:
Lev Walkinb7c58992017-10-06 16:36:32 -0700506 if(dec_value >= LONG_MIN && dec_value <= LONG_MAX) {
507 break;
508 } else {
509 /*
510 * We model INTEGER on long for XER,
511 * to avoid rewriting all the tests at once.
512 */
513 ASN_DEBUG("INTEGER exceeds long range");
514 /* Fall through */
515 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700516 case ASN_STRTOX_ERROR_RANGE:
Lev Walkinb7c58992017-10-06 16:36:32 -0700517 ASN_DEBUG("INTEGER decode %s hit range limit", td->name);
Lev Walkine09f9f12012-09-02 23:06:35 -0700518 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700519 case ASN_STRTOX_ERROR_INVAL:
520 case ASN_STRTOX_EXPECT_MORE:
521 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700522 return XPBD_BROKEN_ENCODING;
523 }
Lev Walkinc744a022006-09-15 18:33:25 +0000524 break;
525 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700526 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000527 st->buf[st->size] = 0; /* Just in case termination */
528 return XPBD_BODY_CONSUMED;
529 case ST_HEXDIGIT1:
530 case ST_HEXDIGIT2:
531 case ST_SKIPSPHEX:
532 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700533 case ST_LEADSPACE:
534 /* Content not found */
535 return XPBD_NOT_BODY_IGNORE;
536 case ST_WAITDIGITS:
537 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700538 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
539 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000540 }
Lev Walkind703ff42004-10-21 11:21:25 +0000541
Lev Walkine09f9f12012-09-02 23:06:35 -0700542 /*
543 * Convert the result of parsing of enumeration or a straight
544 * decimal value into a BER representation.
545 */
Lev Walkinb7c58992017-10-06 16:36:32 -0700546 if(asn_imax2INTEGER(st, dec_value)) {
547 ASN_DEBUG("INTEGER decode %s conversion failed", td->name);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000548 return XPBD_SYSTEM_FAILURE;
Lev Walkinb7c58992017-10-06 16:36:32 -0700549 }
Lev Walkind703ff42004-10-21 11:21:25 +0000550
Lev Walkin0fab1a62005-03-09 22:19:25 +0000551 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000552}
553
554asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700555INTEGER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkind703ff42004-10-21 11:21:25 +0000556 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000557 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000558
559 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000560 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000561 buf_ptr, size, INTEGER__xer_body_decode);
562}
563
Lev Walkina9cc46e2004-09-22 16:06:28 +0000564asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000565INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000566 int ilevel, enum xer_encoder_flags_e flags,
567 asn_app_consume_bytes_f *cb, void *app_key) {
568 const INTEGER_t *st = (const INTEGER_t *)sptr;
569 asn_enc_rval_t er;
570
571 (void)ilevel;
572 (void)flags;
573
Lev Walkind500a962005-11-27 13:06:56 +0000574 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700575 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000576
Lev Walkine0b56e02005-02-25 12:10:27 +0000577 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700578 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000579
Lev Walkin7c1dc052016-03-14 03:08:15 -0700580 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000581}
582
Lev Walkind7703cf2012-09-03 01:45:03 -0700583#ifndef ASN_DISABLE_PER_SUPPORT
584
Lev Walkin59b176e2005-11-26 11:25:14 +0000585asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700586INTEGER_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700587 const asn_per_constraints_t *constraints, void **sptr,
588 asn_per_data_t *pd) {
Lev Walkind62a6622017-08-22 02:31:02 -0700589 const asn_INTEGER_specifics_t *specs =
590 (const asn_INTEGER_specifics_t *)td->specifics;
591 asn_dec_rval_t rval = { RC_OK, 0 };
Lev Walkin59b176e2005-11-26 11:25:14 +0000592 INTEGER_t *st = (INTEGER_t *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -0700593 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000594 int repeat;
595
596 (void)opt_codec_ctx;
597
598 if(!st) {
599 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700600 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000601 }
602
Lev Walkina5972be2017-09-29 23:15:58 -0700603 if(!constraints) constraints = td->encoding_constraints.per_constraints;
Lev Walkin59b176e2005-11-26 11:25:14 +0000604 ct = constraints ? &constraints->value : 0;
605
606 if(ct && ct->flags & APC_EXTENSIBLE) {
607 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700608 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000609 if(inext) ct = 0;
610 }
611
612 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000613 st->buf = 0;
614 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000615 if(ct) {
616 if(ct->flags & APC_SEMI_CONSTRAINED) {
617 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700618 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000619 st->size = 1;
620 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
621 size_t size = (ct->range_bits + 7) >> 3;
622 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700623 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000624 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000625 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000626 }
627
Lev Walkin6c527842014-02-09 04:34:54 -0800628 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000629 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800630 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000631 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
632 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800633 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700634 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800635
636 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700637 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800638 if(uper_get_constrained_whole_number(pd,
639 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700640 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800641 ASN_DEBUG("Got value %lu + low %ld",
642 uvalue, ct->lower_bound);
643 uvalue += ct->lower_bound;
644 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700645 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800646 } else {
Lev Walkin38a91df2017-09-17 23:52:33 -0700647 unsigned long uvalue = 0;
648 long svalue;
Lev Walkin6c527842014-02-09 04:34:54 -0800649 if(uper_get_constrained_whole_number(pd,
Lev Walkin38a91df2017-09-17 23:52:33 -0700650 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700651 ASN__DECODE_STARVED;
Lev Walkin38a91df2017-09-17 23:52:33 -0700652 ASN_DEBUG("Got value %lu + low %ld",
653 uvalue, ct->lower_bound);
Lev Walkincc6a76b2017-10-07 16:23:16 -0700654 if(per_long_range_unrebase(uvalue, ct->lower_bound,
655 ct->upper_bound, &svalue)
656 || asn_long2INTEGER(st, svalue)) {
657 ASN__DECODE_FAILED;
658 }
Lev Walkin6c527842014-02-09 04:34:54 -0800659 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000660 return rval;
661 }
662 } else {
663 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
664 }
665
666 /* X.691, #12.2.3, #12.2.4 */
667 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700668 ssize_t len = 0;
669 void *p = NULL;
670 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000671
672 /* Get the PER length */
Lev Walkin9d1b45f2017-10-01 17:04:48 -0700673 len = uper_get_length(pd, -1, 0, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700674 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000675
676 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700677 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000678 st->buf = (uint8_t *)p;
679
680 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700681 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000682 st->size += len;
683 } while(repeat);
684 st->buf[st->size] = 0; /* JIC */
685
686 /* #12.2.3 */
687 if(ct && ct->lower_bound) {
688 /*
689 * TODO: replace by in-place arithmetics.
690 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700691 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000692 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700693 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700694 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700695 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000696 }
697
698 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000699}
700
Lev Walkin523de9e2006-08-18 01:34:18 +0000701asn_enc_rval_t
702INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700703 const asn_per_constraints_t *constraints, void *sptr,
704 asn_per_outp_t *po) {
Lev Walkind62a6622017-08-22 02:31:02 -0700705 const asn_INTEGER_specifics_t *specs =
706 (const asn_INTEGER_specifics_t *)td->specifics;
707 asn_enc_rval_t er;
Lev Walkin523de9e2006-08-18 01:34:18 +0000708 INTEGER_t *st = (INTEGER_t *)sptr;
709 const uint8_t *buf;
710 const uint8_t *end;
Lev Walkin494fb702017-08-07 20:07:00 -0700711 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000712 long value = 0;
713
Lev Walkin7c1dc052016-03-14 03:08:15 -0700714 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000715
Lev Walkina5972be2017-09-29 23:15:58 -0700716 if(!constraints) constraints = td->encoding_constraints.per_constraints;
Lev Walkin523de9e2006-08-18 01:34:18 +0000717 ct = constraints ? &constraints->value : 0;
718
719 er.encoded = 0;
720
721 if(ct) {
722 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000723 if(specs && specs->field_unsigned) {
724 unsigned long uval;
725 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700726 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000727 /* Check proper range */
728 if(ct->flags & APC_SEMI_CONSTRAINED) {
729 if(uval < (unsigned long)ct->lower_bound)
730 inext = 1;
731 } else if(ct->range_bits >= 0) {
732 if(uval < (unsigned long)ct->lower_bound
733 || uval > (unsigned long)ct->upper_bound)
734 inext = 1;
735 }
Lev Walkin67afc362017-10-03 15:47:26 -0700736 ASN_DEBUG("Value %lu (%02x/%zu) lb %lu ub %lu %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000737 uval, st->buf[0], st->size,
738 ct->lower_bound, ct->upper_bound,
739 inext ? "ext" : "fix");
740 value = uval;
741 } else {
742 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700743 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000744 /* Check proper range */
745 if(ct->flags & APC_SEMI_CONSTRAINED) {
746 if(value < ct->lower_bound)
747 inext = 1;
748 } else if(ct->range_bits >= 0) {
749 if(value < ct->lower_bound
750 || value > ct->upper_bound)
751 inext = 1;
752 }
Lev Walkin67afc362017-10-03 15:47:26 -0700753 ASN_DEBUG("Value %ld (%02x/%zu) lb %ld ub %ld %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000754 value, st->buf[0], st->size,
755 ct->lower_bound, ct->upper_bound,
756 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000757 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000758 if(ct->flags & APC_EXTENSIBLE) {
759 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700760 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000761 if(inext) ct = 0;
762 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700763 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000764 }
765 }
766
767
Lev Walkin6c527842014-02-09 04:34:54 -0800768 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000769 if(ct && ct->range_bits >= 0) {
Lev Walkincc6a76b2017-10-07 16:23:16 -0700770 unsigned long v;
Lev Walkin6c527842014-02-09 04:34:54 -0800771 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800772 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
773 value, value - ct->lower_bound, ct->range_bits);
Lev Walkincc6a76b2017-10-07 16:23:16 -0700774 if(per_long_range_rebase(value, ct->lower_bound, ct->upper_bound, &v)) {
775 ASN__ENCODE_FAILED;
776 }
777 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
778 ASN__ENCODE_FAILED;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700779 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000780 }
781
782 if(ct && ct->lower_bound) {
783 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
784 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700785 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000786 }
787
788 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
Lev Walkin5d947a82017-10-03 01:04:03 -0700789 int need_eom = 0;
790 ssize_t mayEncode = uper_put_length(po, end - buf, &need_eom);
791 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700792 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000793 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700794 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000795 buf += mayEncode;
Lev Walkin5d947a82017-10-03 01:04:03 -0700796 if(need_eom && uper_put_length(po, 0, 0)) ASN__ENCODE_FAILED;
797 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000798
Lev Walkin7c1dc052016-03-14 03:08:15 -0700799 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000800}
801
Lev Walkind7703cf2012-09-03 01:45:03 -0700802#endif /* ASN_DISABLE_PER_SUPPORT */
803
Lev Walkin642b92f2017-09-17 22:16:02 -0700804
805/*
806 * This function is only to get rid of Undefined Behavior Sanitizer warning.
807 */
Lev Walkina4f68912017-09-27 02:21:38 +0000808static intmax_t CC_ATTR_NO_SANITIZE("shift-base")
Lev Walkin642b92f2017-09-17 22:16:02 -0700809asn__safe_integer_convert_helper(const uint8_t *b, const uint8_t *end) {
810 intmax_t value;
811
812 /* Perform the sign initialization */
813 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
814 if((*b >> 7)) {
815 value = -1;
816 } else {
817 value = 0;
818 }
819
820 /* Conversion engine */
821 for(; b < end; b++) {
822 value = (value << 8) | *b;
823 }
824
825 return value;
826}
827
Lev Walkinf15320b2004-06-03 03:38:44 +0000828int
Lev Walkin72ec9092017-07-05 05:49:12 -0700829asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000830 uint8_t *b, *end;
831 size_t size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000832
833 /* Sanity checking */
834 if(!iptr || !iptr->buf || !lptr) {
835 errno = EINVAL;
836 return -1;
837 }
838
839 /* Cache the begin/end of the buffer */
840 b = iptr->buf; /* Start of the INTEGER buffer */
841 size = iptr->size;
842 end = b + size; /* Where to stop */
843
Lev Walkin642b92f2017-09-17 22:16:02 -0700844 if(size > sizeof(intmax_t)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000845 uint8_t *end1 = end - 1;
846 /*
847 * Slightly more advanced processing,
Lev Walkin642b92f2017-09-17 22:16:02 -0700848 * able to process INTEGERs with >sizeof(intmax_t) bytes
Lev Walkin72ec9092017-07-05 05:49:12 -0700849 * when the actual value is small, e.g. for intmax_t == int32_t
850 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000851 */
852 /* Skip out the insignificant leading bytes */
853 for(; b < end1; b++) {
854 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700855 case 0x00: if((b[1] & 0x80) == 0) continue; break;
856 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000857 }
858 break;
859 }
860
861 size = end - b;
Lev Walkin642b92f2017-09-17 22:16:02 -0700862 if(size > sizeof(intmax_t)) {
863 /* Still cannot fit the sizeof(intmax_t) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000864 errno = ERANGE;
865 return -1;
866 }
867 }
868
869 /* Shortcut processing of a corner case */
870 if(end == b) {
871 *lptr = 0;
872 return 0;
873 }
874
Lev Walkin642b92f2017-09-17 22:16:02 -0700875 *lptr = asn__safe_integer_convert_helper(b, end);
Lev Walkinf15320b2004-06-03 03:38:44 +0000876 return 0;
877}
Lev Walkind703ff42004-10-21 11:21:25 +0000878
Lev Walkin72ec9092017-07-05 05:49:12 -0700879/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000880int
Lev Walkin72ec9092017-07-05 05:49:12 -0700881asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000882 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700883 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000884 size_t size;
885
886 if(!iptr || !iptr->buf || !lptr) {
887 errno = EINVAL;
888 return -1;
889 }
890
891 b = iptr->buf;
892 size = iptr->size;
893 end = b + size;
894
895 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700896 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000897 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700898 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000899 errno = ERANGE;
900 return -1;
901 }
902 }
903
904 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700905 for(value = 0; b < end; b++)
906 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000907
Lev Walkin72ec9092017-07-05 05:49:12 -0700908 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000909 return 0;
910}
911
912int
Lev Walkin72ec9092017-07-05 05:49:12 -0700913asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
914 uint8_t *buf;
915 uint8_t *end;
916 uint8_t *b;
917 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000918
Lev Walkinaa067a92017-10-03 16:09:05 -0700919 if(value <= ((~(uintmax_t)0) >> 1)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700920 return asn_imax2INTEGER(st, value);
921 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000922
Lev Walkin72ec9092017-07-05 05:49:12 -0700923 buf = (uint8_t *)MALLOC(1 + sizeof(value));
924 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000925
Lev Walkin72ec9092017-07-05 05:49:12 -0700926 end = buf + (sizeof(value) + 1);
927 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
928 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
929 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000930
Lev Walkin72ec9092017-07-05 05:49:12 -0700931 if(st->buf) FREEMEM(st->buf);
932 st->buf = buf;
933 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000934
935 return 0;
936}
937
938int
Lev Walkin72ec9092017-07-05 05:49:12 -0700939asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000940 uint8_t *buf, *bp;
941 uint8_t *p;
942 uint8_t *pstart;
943 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000944 int littleEndian = 1; /* Run-time detection */
945 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000946
947 if(!st) {
948 errno = EINVAL;
949 return -1;
950 }
951
Lev Walkin290b4d62017-08-28 17:48:35 -0700952 buf = (uint8_t *)(long *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000953 if(!buf) return -1;
954
Lev Walkind7ad5612004-10-26 08:20:46 +0000955 if(*(char *)&littleEndian) {
956 pstart = (uint8_t *)&value + sizeof(value) - 1;
957 pend1 = (uint8_t *)&value;
958 add = -1;
959 } else {
960 pstart = (uint8_t *)&value;
961 pend1 = pstart + sizeof(value) - 1;
962 add = 1;
963 }
964
Lev Walkind703ff42004-10-21 11:21:25 +0000965 /*
966 * If the contents octet consists of more than one octet,
967 * then bits of the first octet and bit 8 of the second octet:
968 * a) shall not all be ones; and
969 * b) shall not all be zero.
970 */
Lev Walkin33700162004-10-26 09:03:31 +0000971 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000972 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000973 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000974 continue;
975 break;
Lev Walkin33700162004-10-26 09:03:31 +0000976 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000977 continue;
978 break;
979 }
980 break;
981 }
982 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700983 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000984 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000985
986 if(st->buf) FREEMEM(st->buf);
987 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000988 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000989
990 return 0;
991}
Lev Walkinb3751942012-09-02 19:36:47 -0700992
Lev Walkin72ec9092017-07-05 05:49:12 -0700993int
994asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
995 intmax_t v;
996 if(asn_INTEGER2imax(iptr, &v) == 0) {
997 if(v < LONG_MIN || v > LONG_MAX) {
998 errno = ERANGE;
999 return -1;
1000 }
1001 *l = v;
1002 return 0;
1003 } else {
1004 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -07001005 }
Lev Walkin72ec9092017-07-05 05:49:12 -07001006}
Lev Walkincad560a2013-03-16 07:00:58 -07001007
Lev Walkin72ec9092017-07-05 05:49:12 -07001008int
1009asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
1010 uintmax_t v;
1011 if(asn_INTEGER2umax(iptr, &v) == 0) {
1012 if(v > ULONG_MAX) {
1013 errno = ERANGE;
1014 return -1;
1015 }
1016 *l = v;
1017 return 0;
1018 } else {
1019 return -1;
1020 }
1021}
1022
1023int
1024asn_long2INTEGER(INTEGER_t *st, long value) {
1025 return asn_imax2INTEGER(st, value);
1026}
1027
1028int
1029asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1030 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -07001031}
1032
1033/*
1034 * Parse the number in the given string until the given *end position,
1035 * returning the position after the last parsed character back using the
1036 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -07001037 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -07001038 */
Lev Walkin72ec9092017-07-05 05:49:12 -07001039enum asn_strtox_result_e
1040asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001041 int sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001042 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -07001043
Lev Walkinaa067a92017-10-03 16:09:05 -07001044#define ASN1_INTMAX_MAX ((~(uintmax_t)0) >> 1)
1045
1046 const intmax_t upper_boundary = ASN1_INTMAX_MAX / 10;
1047 intmax_t last_digit_max = ASN1_INTMAX_MAX % 10;
Lev Walkine09f9f12012-09-02 23:06:35 -07001048
Lev Walkin72ec9092017-07-05 05:49:12 -07001049 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001050
1051 switch(*str) {
1052 case '-':
1053 last_digit_max++;
1054 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001055 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001056 case '+':
1057 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001058 if(str >= *end) {
1059 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001060 return ASN_STRTOX_EXPECT_MORE;
Lev Walkincad560a2013-03-16 07:00:58 -07001061 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001062 }
1063
Lev Walkin72ec9092017-07-05 05:49:12 -07001064 for(value = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001065 switch(*str) {
1066 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1067 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1068 int d = *str - '0';
Lev Walkin72ec9092017-07-05 05:49:12 -07001069 if(value < upper_boundary) {
1070 value = value * 10 + d;
1071 } else if(value == upper_boundary) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001072 if(d <= last_digit_max) {
1073 if(sign > 0) {
Lev Walkin72ec9092017-07-05 05:49:12 -07001074 value = value * 10 + d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001075 } else {
1076 sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001077 value = -value * 10 - d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001078 }
1079 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001080 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001081 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001082 }
1083 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001084 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001085 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001086 }
1087 }
1088 continue;
1089 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001090 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001091 *intp = sign * value;
1092 return ASN_STRTOX_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001093 }
1094 }
1095
Lev Walkincad560a2013-03-16 07:00:58 -07001096 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001097 *intp = sign * value;
1098 return ASN_STRTOX_OK;
1099}
1100
1101enum asn_strtox_result_e
1102asn_strtol_lim(const char *str, const char **end, long *lp) {
1103 intmax_t value;
1104 switch(asn_strtoimax_lim(str, end, &value)) {
1105 case ASN_STRTOX_ERROR_RANGE:
1106 return ASN_STRTOX_ERROR_RANGE;
1107 case ASN_STRTOX_ERROR_INVAL:
1108 return ASN_STRTOX_ERROR_INVAL;
1109 case ASN_STRTOX_EXPECT_MORE:
1110 return ASN_STRTOX_EXPECT_MORE;
1111 case ASN_STRTOX_OK:
1112 if(value >= LONG_MIN && value <= LONG_MAX) {
1113 *lp = value;
1114 return ASN_STRTOX_OK;
1115 } else {
1116 return ASN_STRTOX_ERROR_RANGE;
1117 }
1118 case ASN_STRTOX_EXTRA_DATA:
1119 if(value >= LONG_MIN && value <= LONG_MAX) {
1120 *lp = value;
1121 return ASN_STRTOX_EXTRA_DATA;
1122 } else {
1123 return ASN_STRTOX_ERROR_RANGE;
1124 }
1125 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001126
1127 assert(!"Unreachable");
1128 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001129}
1130
Lev Walkincd2f48e2017-08-10 02:14:59 -07001131int
1132INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1133 const void *bptr) {
1134 const INTEGER_t *a = aptr;
1135 const INTEGER_t *b = bptr;
1136
1137 (void)td;
1138
1139 if(a && b) {
1140 if(a->size && b->size) {
1141 int sign_a = (a->buf[0] & 0x80) ? -1 : 1;
1142 int sign_b = (b->buf[0] & 0x80) ? -1 : 1;
1143
1144 if(sign_a < sign_b) return -1;
1145 if(sign_a > sign_b) return 1;
1146
1147 /* The shortest integer wins, unless comparing negatives */
1148 if(a->size < b->size) {
1149 return -1 * sign_a;
1150 } else if(a->size > b->size) {
1151 return 1 * sign_b;
1152 }
1153
1154 return sign_a * memcmp(a->buf, b->buf, a->size);
1155 } else if(a->size) {
1156 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1157 return (1) * sign;
1158 } else if(b->size) {
1159 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1160 return (-1) * sign;
1161 } else {
1162 return 0;
1163 }
1164 } else if(!a && !b) {
1165 return 0;
1166 } else if(!a) {
1167 return -1;
1168 } else {
1169 return 1;
1170 }
1171
1172}
1173
Lev Walkina5972be2017-09-29 23:15:58 -07001174asn_random_fill_result_t
1175INTEGER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1176 const asn_encoding_constraints_t *constraints,
1177 size_t max_length) {
1178 const asn_INTEGER_specifics_t *specs =
1179 (const asn_INTEGER_specifics_t *)td->specifics;
1180 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
1181 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1182 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1183 INTEGER_t *st = *sptr;
1184 const asn_INTEGER_enum_map_t *emap;
1185 size_t emap_len;
1186 intmax_t value;
1187 int find_inside_map;
1188
1189 if(max_length == 0) return result_skipped;
1190
1191 if(st == NULL) {
1192 st = (INTEGER_t *)CALLOC(1, sizeof(*st));
1193 if(st == NULL) {
1194 return result_failed;
1195 }
1196 }
1197
1198 if(specs) {
1199 emap = specs->value2enum;
1200 emap_len = specs->map_count;
1201 if(specs->strict_enumeration) {
1202 find_inside_map = emap_len > 0;
1203 } else {
1204 find_inside_map = emap_len ? asn_random_between(0, 1) : 0;
1205 }
1206 } else {
1207 emap = 0;
1208 emap_len = 0;
1209 find_inside_map = 0;
1210 }
1211
1212 if(find_inside_map) {
1213 assert(emap_len > 0);
1214 value = emap[asn_random_between(0, emap_len - 1)].nat_value;
1215 } else {
Lev Walkin18741a92017-10-01 12:40:19 -07001216 const asn_per_constraints_t *ct;
Lev Walkina5972be2017-09-29 23:15:58 -07001217
1218 static const long variants[] = {
1219 -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384,
1220 -16383, -257, -256, -255, -254, -129, -128, -127,
1221 -126, -1, 0, 1, 126, 127, 128, 129,
1222 254, 255, 256, 257, 16383, 16384, 16385, 32767,
1223 32768, 32769, 65534, 65535, 65536, 65537};
1224 if(specs && specs->field_unsigned) {
1225 assert(variants[18] == 0);
1226 value = variants[asn_random_between(
1227 18, sizeof(variants) / sizeof(variants[0]) - 1)];
1228 } else {
1229 value = variants[asn_random_between(
1230 0, sizeof(variants) / sizeof(variants[0]) - 1)];
1231 }
1232
1233 if(!constraints) constraints = &td->encoding_constraints;
Lev Walkin18741a92017-10-01 12:40:19 -07001234 ct = constraints ? constraints->per_constraints : 0;
1235 if(ct && (ct->value.flags & APC_CONSTRAINED)) {
1236 if(value < ct->value.lower_bound || value > ct->value.upper_bound) {
1237 value = asn_random_between(ct->value.lower_bound,
1238 ct->value.upper_bound);
1239 }
1240 }
Lev Walkina5972be2017-09-29 23:15:58 -07001241 }
1242
Lev Walkina5972be2017-09-29 23:15:58 -07001243 if(asn_imax2INTEGER(st, value)) {
1244 if(st == *sptr) {
1245 ASN_STRUCT_RESET(*td, st);
1246 } else {
1247 ASN_STRUCT_FREE(*td, st);
1248 }
1249 return result_failed;
1250 } else {
Lev Walkin18741a92017-10-01 12:40:19 -07001251 *sptr = st;
Lev Walkina5972be2017-09-29 23:15:58 -07001252 result_ok.length = st->size;
1253 return result_ok;
1254 }
1255}