blob: 5650368ed9bb287fa585139850be031c2b6bafe3 [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;
Lev Walkine2bbbf82017-10-08 18:52:37 -0700125 char scratch[32];
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;
Lev Walkin97f8edc2013-03-28 04:38:41 -0700141 el = (value >= 0 || !specs || !specs->field_unsigned)
142 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000143 if(el) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000144 if(plainOrXER == 0)
Lev Walkine2bbbf82017-10-08 18:52:37 -0700145 return asn__format_to_callback(cb, app_key,
Lev Walkin72ec9092017-07-05 05:49:12 -0700146 "%" PRIdMAX " (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000147 else
Lev Walkine2bbbf82017-10-08 18:52:37 -0700148 return asn__format_to_callback(cb, app_key,
Lev Walkine0b56e02005-02-25 12:10:27 +0000149 "<%s/>", el->enum_name);
150 } else if(plainOrXER && specs && specs->strict_enumeration) {
151 ASN_DEBUG("ASN.1 forbids dealing with "
152 "unknown value of ENUMERATED type");
153 errno = EPERM;
154 return -1;
155 } else {
Lev Walkine2bbbf82017-10-08 18:52:37 -0700156 return asn__format_to_callback(cb, app_key,
Lev Walkin72ec9092017-07-05 05:49:12 -0700157 (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX,
158 value);
159 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000160 } else if(plainOrXER && specs && specs->strict_enumeration) {
161 /*
162 * Here and earlier, we cannot encode the ENUMERATED values
163 * if there is no corresponding identifier.
164 */
165 ASN_DEBUG("ASN.1 forbids dealing with "
166 "unknown value of ENUMERATED type");
167 errno = EPERM;
168 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 }
170
171 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000172 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700174 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000175 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000177 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000178 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000179 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 p = scratch;
181 }
182 *p++ = h2c[*buf >> 4];
183 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000184 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 }
Lev Walkindb13f512004-07-19 17:30:25 +0000186 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000187 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000188
Lev Walkina9cc46e2004-09-22 16:06:28 +0000189 wrote += p - scratch;
190 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
191}
192
193/*
194 * INTEGER specific human-readable output.
195 */
196int
Lev Walkin5e033762004-09-29 13:26:15 +0000197INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000198 asn_app_consume_bytes_f *cb, void *app_key) {
199 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000200 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000201
202 (void)td;
203 (void)ilevel;
204
Lev Walkind500a962005-11-27 13:06:56 +0000205 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000206 ret = cb("<absent>", 8, app_key);
207 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000208 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000209
Lev Walkin8e8078a2004-09-26 13:10:40 +0000210 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000211}
212
Lev Walkine0b56e02005-02-25 12:10:27 +0000213struct e2v_key {
214 const char *start;
215 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700216 const asn_INTEGER_enum_map_t *vemap;
217 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000218};
219static int
220INTEGER__compar_enum2value(const void *kp, const void *am) {
221 const struct e2v_key *key = (const struct e2v_key *)kp;
222 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
223 const char *ptr, *end, *name;
224
225 /* Remap the element (sort by different criterion) */
226 el = key->vemap + key->evmap[el - key->vemap];
227
228 /* Compare strings */
229 for(ptr = key->start, end = key->stop, name = el->enum_name;
230 ptr < end; ptr++, name++) {
Lev Walkin793982a2017-10-02 14:12:51 -0700231 if(*ptr != *name || !*name)
Lev Walkine0b56e02005-02-25 12:10:27 +0000232 return *(const unsigned char *)ptr
233 - *(const unsigned char *)name;
234 }
235 return name[0] ? -1 : 0;
236}
237
238static const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700239INTEGER_map_enum2value(const asn_INTEGER_specifics_t *specs, const char *lstart,
240 const char *lstop) {
241 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000242 int count = specs ? specs->map_count : 0;
243 struct e2v_key key;
244 const char *lp;
245
246 if(!count) return NULL;
247
248 /* Guaranteed: assert(lstart < lstop); */
249 /* Figure out the tag name */
250 for(lstart++, lp = lstart; lp < lstop; lp++) {
251 switch(*lp) {
252 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
253 case 0x2f: /* '/' */ case 0x3e: /* '>' */
254 break;
255 default:
256 continue;
257 }
258 break;
259 }
260 if(lp == lstop) return NULL; /* No tag found */
261 lstop = lp;
262
263 key.start = lstart;
264 key.stop = lstop;
265 key.vemap = specs->value2enum;
266 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000267 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
268 specs->value2enum, count, sizeof(specs->value2enum[0]),
269 INTEGER__compar_enum2value);
270 if(el_found) {
271 /* Remap enum2value into value2enum */
272 el_found = key.vemap + key.evmap[el_found - key.vemap];
273 }
274 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000275}
276
277static int
278INTEGER__compar_value2enum(const void *kp, const void *am) {
279 long a = *(const long *)kp;
280 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
281 long b = el->nat_value;
282 if(a < b) return -1;
283 else if(a == b) return 0;
284 else return 1;
285}
286
Lev Walkinc2350112005-03-29 17:19:53 +0000287const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700288INTEGER_map_value2enum(const asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000289 int count = specs ? specs->map_count : 0;
290 if(!count) return 0;
291 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
292 count, sizeof(specs->value2enum[0]),
293 INTEGER__compar_value2enum);
294}
295
Lev Walkinc744a022006-09-15 18:33:25 +0000296static int
297INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
298 void *p = MALLOC(min_size + 1);
299 if(p) {
300 void *b = st->buf;
301 st->size = 0;
302 st->buf = p;
303 FREEMEM(b);
304 return 0;
305 } else {
306 return -1;
307 }
308}
309
Lev Walkind703ff42004-10-21 11:21:25 +0000310/*
311 * Decode the chunk of XML text encoding INTEGER.
312 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000313static enum xer_pbd_rval
314INTEGER__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 +0000315 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700316 intmax_t dec_value;
317 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000318 const char *lp;
319 const char *lstart = (const char *)chunk_buf;
320 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000321 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700322 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000323 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000324 ST_WAITDIGITS,
325 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700326 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000327 ST_HEXDIGIT1,
328 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700329 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000330 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700331 ST_END_ENUM,
332 ST_UNEXPECTED
333 } state = ST_LEADSPACE;
334 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
335 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000336
Lev Walkinc744a022006-09-15 18:33:25 +0000337 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000338 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
339 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000340
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000341 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
342 return XPBD_SYSTEM_FAILURE;
343
Lev Walkind703ff42004-10-21 11:21:25 +0000344 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000345 * We may have received a tag here. It will be processed inline.
346 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000347 */
Lev Walkinb3751942012-09-02 19:36:47 -0700348 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000349 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000350 switch(lv) {
351 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000352 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700353 case ST_LEADSPACE:
354 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700355 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000356 case ST_SKIPSPHEX:
357 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700358 case ST_DIGITS:
359 dec_value_end = lp;
360 state = ST_DIGITS_TRAILSPACE;
361 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700362 case ST_HEXCOLON:
363 state = ST_HEXDIGITS_TRAILSPACE;
364 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000365 default:
366 break;
367 }
Lev Walkind703ff42004-10-21 11:21:25 +0000368 break;
369 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700370 if(state == ST_LEADSPACE) {
371 dec_value = 0;
372 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000373 state = ST_WAITDIGITS;
374 continue;
375 }
376 break;
377 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700378 if(state == ST_LEADSPACE) {
379 dec_value = 0;
380 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000381 state = ST_WAITDIGITS;
382 continue;
383 }
384 break;
385 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
386 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000387 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700388 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000389 case ST_SKIPSPHEX: /* Fall through */
390 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700391 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000392 state = ST_HEXDIGIT2;
393 continue;
394 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700395 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000396 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700397 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000398 continue;
399 case ST_HEXCOLON:
400 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700401 case ST_LEADSPACE:
402 dec_value = 0;
403 dec_value_start = lp;
404 /* FALL THROUGH */
405 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000406 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700407 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700408 default:
409 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000410 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700411 break;
412 case 0x3c: /* '<', start of XML encoded enumeration */
413 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000414 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000415 el = INTEGER_map_enum2value(
Lev Walkind62a6622017-08-22 02:31:02 -0700416 (const asn_INTEGER_specifics_t *)
Lev Walkine0b56e02005-02-25 12:10:27 +0000417 td->specifics, lstart, lstop);
418 if(el) {
419 ASN_DEBUG("Found \"%s\" => %ld",
420 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700421 dec_value = el->nat_value;
422 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000423 lp = lstop - 1;
424 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000425 }
426 ASN_DEBUG("Unknown identifier for INTEGER");
427 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000428 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000429 case 0x3a: /* ':' */
430 if(state == ST_HEXCOLON) {
431 /* This colon is expected */
432 state = ST_HEXDIGIT1;
433 continue;
434 } else if(state == ST_DIGITS) {
435 /* The colon here means that we have
436 * decoded the first two hexadecimal
437 * places as a decimal value.
438 * Switch decoding mode. */
439 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000440 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700441 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000442 lp = lstart - 1;
443 continue;
444 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400445 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000446 break;
447 }
448 /* [A-Fa-f] */
449 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
450 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
451 switch(state) {
452 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700453 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000454 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700455 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
456 hex_value += 10;
457 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000458 state = ST_HEXDIGIT2;
459 continue;
460 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700461 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
462 hex_value += 10;
463 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000464 state = ST_HEXCOLON;
465 continue;
466 case ST_DIGITS:
467 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000468 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700469 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000470 lp = lstart - 1;
471 continue;
472 default:
473 break;
474 }
475 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000476 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000477
478 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700479 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400480 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700481 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000482 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000483 }
484
Lev Walkinc744a022006-09-15 18:33:25 +0000485 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700486 case ST_END_ENUM:
487 /* Got a complete and valid enumeration encoded as a tag. */
488 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000489 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700490 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700491 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700492 case ST_DIGITS_TRAILSPACE:
493 /* The last symbol encountered was a digit. */
Lev Walkinb7c58992017-10-06 16:36:32 -0700494 switch(asn_strtoimax_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700495 case ASN_STRTOX_OK:
Lev Walkinb7c58992017-10-06 16:36:32 -0700496 if(dec_value >= LONG_MIN && dec_value <= LONG_MAX) {
497 break;
498 } else {
499 /*
500 * We model INTEGER on long for XER,
501 * to avoid rewriting all the tests at once.
502 */
503 ASN_DEBUG("INTEGER exceeds long range");
504 /* Fall through */
505 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700506 case ASN_STRTOX_ERROR_RANGE:
Lev Walkinb7c58992017-10-06 16:36:32 -0700507 ASN_DEBUG("INTEGER decode %s hit range limit", td->name);
Lev Walkine09f9f12012-09-02 23:06:35 -0700508 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700509 case ASN_STRTOX_ERROR_INVAL:
510 case ASN_STRTOX_EXPECT_MORE:
511 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700512 return XPBD_BROKEN_ENCODING;
513 }
Lev Walkinc744a022006-09-15 18:33:25 +0000514 break;
515 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700516 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000517 st->buf[st->size] = 0; /* Just in case termination */
518 return XPBD_BODY_CONSUMED;
519 case ST_HEXDIGIT1:
520 case ST_HEXDIGIT2:
521 case ST_SKIPSPHEX:
522 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700523 case ST_LEADSPACE:
524 /* Content not found */
525 return XPBD_NOT_BODY_IGNORE;
526 case ST_WAITDIGITS:
527 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700528 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
529 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000530 }
Lev Walkind703ff42004-10-21 11:21:25 +0000531
Lev Walkine09f9f12012-09-02 23:06:35 -0700532 /*
533 * Convert the result of parsing of enumeration or a straight
534 * decimal value into a BER representation.
535 */
Lev Walkinb7c58992017-10-06 16:36:32 -0700536 if(asn_imax2INTEGER(st, dec_value)) {
537 ASN_DEBUG("INTEGER decode %s conversion failed", td->name);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000538 return XPBD_SYSTEM_FAILURE;
Lev Walkinb7c58992017-10-06 16:36:32 -0700539 }
Lev Walkind703ff42004-10-21 11:21:25 +0000540
Lev Walkin0fab1a62005-03-09 22:19:25 +0000541 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000542}
543
544asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700545INTEGER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkind703ff42004-10-21 11:21:25 +0000546 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000547 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000548
549 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000550 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000551 buf_ptr, size, INTEGER__xer_body_decode);
552}
553
Lev Walkina9cc46e2004-09-22 16:06:28 +0000554asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000555INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000556 int ilevel, enum xer_encoder_flags_e flags,
557 asn_app_consume_bytes_f *cb, void *app_key) {
558 const INTEGER_t *st = (const INTEGER_t *)sptr;
559 asn_enc_rval_t er;
560
561 (void)ilevel;
562 (void)flags;
563
Lev Walkind500a962005-11-27 13:06:56 +0000564 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700565 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000566
Lev Walkine0b56e02005-02-25 12:10:27 +0000567 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700568 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000569
Lev Walkin7c1dc052016-03-14 03:08:15 -0700570 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000571}
572
Lev Walkind7703cf2012-09-03 01:45:03 -0700573#ifndef ASN_DISABLE_PER_SUPPORT
574
Lev Walkin59b176e2005-11-26 11:25:14 +0000575asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700576INTEGER_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700577 const asn_per_constraints_t *constraints, void **sptr,
578 asn_per_data_t *pd) {
Lev Walkind62a6622017-08-22 02:31:02 -0700579 const asn_INTEGER_specifics_t *specs =
580 (const asn_INTEGER_specifics_t *)td->specifics;
581 asn_dec_rval_t rval = { RC_OK, 0 };
Lev Walkin59b176e2005-11-26 11:25:14 +0000582 INTEGER_t *st = (INTEGER_t *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -0700583 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000584 int repeat;
585
586 (void)opt_codec_ctx;
587
588 if(!st) {
589 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700590 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000591 }
592
Lev Walkina5972be2017-09-29 23:15:58 -0700593 if(!constraints) constraints = td->encoding_constraints.per_constraints;
Lev Walkin59b176e2005-11-26 11:25:14 +0000594 ct = constraints ? &constraints->value : 0;
595
596 if(ct && ct->flags & APC_EXTENSIBLE) {
597 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700598 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000599 if(inext) ct = 0;
600 }
601
602 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000603 st->buf = 0;
604 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 if(ct) {
606 if(ct->flags & APC_SEMI_CONSTRAINED) {
607 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700608 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000609 st->size = 1;
610 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
611 size_t size = (ct->range_bits + 7) >> 3;
612 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700613 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000614 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000615 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000616 }
617
Lev Walkin6c527842014-02-09 04:34:54 -0800618 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000619 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800620 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000621 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
622 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800623 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700624 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800625
626 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700627 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800628 if(uper_get_constrained_whole_number(pd,
629 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700630 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800631 ASN_DEBUG("Got value %lu + low %ld",
632 uvalue, ct->lower_bound);
633 uvalue += ct->lower_bound;
634 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700635 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800636 } else {
Lev Walkin38a91df2017-09-17 23:52:33 -0700637 unsigned long uvalue = 0;
638 long svalue;
Lev Walkin6c527842014-02-09 04:34:54 -0800639 if(uper_get_constrained_whole_number(pd,
Lev Walkin38a91df2017-09-17 23:52:33 -0700640 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700641 ASN__DECODE_STARVED;
Lev Walkin38a91df2017-09-17 23:52:33 -0700642 ASN_DEBUG("Got value %lu + low %ld",
643 uvalue, ct->lower_bound);
Lev Walkincc6a76b2017-10-07 16:23:16 -0700644 if(per_long_range_unrebase(uvalue, ct->lower_bound,
645 ct->upper_bound, &svalue)
646 || asn_long2INTEGER(st, svalue)) {
647 ASN__DECODE_FAILED;
648 }
Lev Walkin6c527842014-02-09 04:34:54 -0800649 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000650 return rval;
651 }
652 } else {
653 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
654 }
655
656 /* X.691, #12.2.3, #12.2.4 */
657 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700658 ssize_t len = 0;
659 void *p = NULL;
660 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000661
662 /* Get the PER length */
Lev Walkin9d1b45f2017-10-01 17:04:48 -0700663 len = uper_get_length(pd, -1, 0, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700664 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000665
666 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700667 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000668 st->buf = (uint8_t *)p;
669
670 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700671 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000672 st->size += len;
673 } while(repeat);
674 st->buf[st->size] = 0; /* JIC */
675
676 /* #12.2.3 */
677 if(ct && ct->lower_bound) {
678 /*
679 * TODO: replace by in-place arithmetics.
680 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700681 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000682 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700683 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700684 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700685 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000686 }
687
688 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000689}
690
Lev Walkin523de9e2006-08-18 01:34:18 +0000691asn_enc_rval_t
692INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700693 const asn_per_constraints_t *constraints, void *sptr,
694 asn_per_outp_t *po) {
Lev Walkind62a6622017-08-22 02:31:02 -0700695 const asn_INTEGER_specifics_t *specs =
696 (const asn_INTEGER_specifics_t *)td->specifics;
697 asn_enc_rval_t er;
Lev Walkin523de9e2006-08-18 01:34:18 +0000698 INTEGER_t *st = (INTEGER_t *)sptr;
699 const uint8_t *buf;
700 const uint8_t *end;
Lev Walkin494fb702017-08-07 20:07:00 -0700701 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000702 long value = 0;
703
Lev Walkin7c1dc052016-03-14 03:08:15 -0700704 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000705
Lev Walkina5972be2017-09-29 23:15:58 -0700706 if(!constraints) constraints = td->encoding_constraints.per_constraints;
Lev Walkin523de9e2006-08-18 01:34:18 +0000707 ct = constraints ? &constraints->value : 0;
708
709 er.encoded = 0;
710
711 if(ct) {
712 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000713 if(specs && specs->field_unsigned) {
714 unsigned long uval;
715 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700716 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000717 /* Check proper range */
718 if(ct->flags & APC_SEMI_CONSTRAINED) {
719 if(uval < (unsigned long)ct->lower_bound)
720 inext = 1;
721 } else if(ct->range_bits >= 0) {
722 if(uval < (unsigned long)ct->lower_bound
723 || uval > (unsigned long)ct->upper_bound)
724 inext = 1;
725 }
Lev Walkin67afc362017-10-03 15:47:26 -0700726 ASN_DEBUG("Value %lu (%02x/%zu) lb %lu ub %lu %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000727 uval, st->buf[0], st->size,
728 ct->lower_bound, ct->upper_bound,
729 inext ? "ext" : "fix");
730 value = uval;
731 } else {
732 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700733 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000734 /* Check proper range */
735 if(ct->flags & APC_SEMI_CONSTRAINED) {
736 if(value < ct->lower_bound)
737 inext = 1;
738 } else if(ct->range_bits >= 0) {
739 if(value < ct->lower_bound
740 || value > ct->upper_bound)
741 inext = 1;
742 }
Lev Walkin67afc362017-10-03 15:47:26 -0700743 ASN_DEBUG("Value %ld (%02x/%zu) lb %ld ub %ld %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000744 value, st->buf[0], st->size,
745 ct->lower_bound, ct->upper_bound,
746 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000747 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000748 if(ct->flags & APC_EXTENSIBLE) {
749 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700750 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000751 if(inext) ct = 0;
752 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700753 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000754 }
755 }
756
757
Lev Walkin6c527842014-02-09 04:34:54 -0800758 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000759 if(ct && ct->range_bits >= 0) {
Lev Walkincc6a76b2017-10-07 16:23:16 -0700760 unsigned long v;
Lev Walkin6c527842014-02-09 04:34:54 -0800761 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800762 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
763 value, value - ct->lower_bound, ct->range_bits);
Lev Walkincc6a76b2017-10-07 16:23:16 -0700764 if(per_long_range_rebase(value, ct->lower_bound, ct->upper_bound, &v)) {
765 ASN__ENCODE_FAILED;
766 }
767 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
768 ASN__ENCODE_FAILED;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700769 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000770 }
771
772 if(ct && ct->lower_bound) {
773 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
774 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700775 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000776 }
777
778 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
Lev Walkin5d947a82017-10-03 01:04:03 -0700779 int need_eom = 0;
780 ssize_t mayEncode = uper_put_length(po, end - buf, &need_eom);
781 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700782 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000783 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700784 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000785 buf += mayEncode;
Lev Walkin5d947a82017-10-03 01:04:03 -0700786 if(need_eom && uper_put_length(po, 0, 0)) ASN__ENCODE_FAILED;
787 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000788
Lev Walkin7c1dc052016-03-14 03:08:15 -0700789 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000790}
791
Lev Walkind7703cf2012-09-03 01:45:03 -0700792#endif /* ASN_DISABLE_PER_SUPPORT */
793
Lev Walkinc346f902017-10-09 20:24:40 -0700794static intmax_t
795asn__integer_convert(const uint8_t *b, const uint8_t *end) {
796 uintmax_t value;
Lev Walkin642b92f2017-09-17 22:16:02 -0700797
Lev Walkinc346f902017-10-09 20:24:40 -0700798 /* Perform the sign initialization */
799 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
800 if((*b >> 7)) {
801 value = (uintmax_t)(-1);
Lev Walkin642b92f2017-09-17 22:16:02 -0700802 } else {
803 value = 0;
804 }
805
806 /* Conversion engine */
Lev Walkinc346f902017-10-09 20:24:40 -0700807 for(; b < end; b++) {
808 value = (value << 8) | *b;
Lev Walkin642b92f2017-09-17 22:16:02 -0700809 }
810
811 return value;
812}
813
Lev Walkinf15320b2004-06-03 03:38:44 +0000814int
Lev Walkin72ec9092017-07-05 05:49:12 -0700815asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000816 uint8_t *b, *end;
817 size_t size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000818
819 /* Sanity checking */
820 if(!iptr || !iptr->buf || !lptr) {
821 errno = EINVAL;
822 return -1;
823 }
824
825 /* Cache the begin/end of the buffer */
826 b = iptr->buf; /* Start of the INTEGER buffer */
827 size = iptr->size;
828 end = b + size; /* Where to stop */
829
Lev Walkin642b92f2017-09-17 22:16:02 -0700830 if(size > sizeof(intmax_t)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000831 uint8_t *end1 = end - 1;
832 /*
833 * Slightly more advanced processing,
Lev Walkin642b92f2017-09-17 22:16:02 -0700834 * able to process INTEGERs with >sizeof(intmax_t) bytes
Lev Walkin72ec9092017-07-05 05:49:12 -0700835 * when the actual value is small, e.g. for intmax_t == int32_t
836 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000837 */
838 /* Skip out the insignificant leading bytes */
839 for(; b < end1; b++) {
840 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700841 case 0x00: if((b[1] & 0x80) == 0) continue; break;
842 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000843 }
844 break;
845 }
846
847 size = end - b;
Lev Walkin642b92f2017-09-17 22:16:02 -0700848 if(size > sizeof(intmax_t)) {
849 /* Still cannot fit the sizeof(intmax_t) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000850 errno = ERANGE;
851 return -1;
852 }
853 }
854
855 /* Shortcut processing of a corner case */
856 if(end == b) {
857 *lptr = 0;
858 return 0;
859 }
860
Lev Walkinc346f902017-10-09 20:24:40 -0700861 *lptr = asn__integer_convert(b, end);
Lev Walkinf15320b2004-06-03 03:38:44 +0000862 return 0;
863}
Lev Walkind703ff42004-10-21 11:21:25 +0000864
Lev Walkin72ec9092017-07-05 05:49:12 -0700865/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000866int
Lev Walkin72ec9092017-07-05 05:49:12 -0700867asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000868 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700869 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000870 size_t size;
871
872 if(!iptr || !iptr->buf || !lptr) {
873 errno = EINVAL;
874 return -1;
875 }
876
877 b = iptr->buf;
878 size = iptr->size;
879 end = b + size;
880
881 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700882 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000883 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700884 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000885 errno = ERANGE;
886 return -1;
887 }
888 }
889
890 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700891 for(value = 0; b < end; b++)
892 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000893
Lev Walkin72ec9092017-07-05 05:49:12 -0700894 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000895 return 0;
896}
897
898int
Lev Walkin72ec9092017-07-05 05:49:12 -0700899asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
900 uint8_t *buf;
901 uint8_t *end;
902 uint8_t *b;
903 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000904
Lev Walkinaa067a92017-10-03 16:09:05 -0700905 if(value <= ((~(uintmax_t)0) >> 1)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700906 return asn_imax2INTEGER(st, value);
907 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000908
Lev Walkin72ec9092017-07-05 05:49:12 -0700909 buf = (uint8_t *)MALLOC(1 + sizeof(value));
910 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000911
Lev Walkin72ec9092017-07-05 05:49:12 -0700912 end = buf + (sizeof(value) + 1);
913 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
914 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
915 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000916
Lev Walkin72ec9092017-07-05 05:49:12 -0700917 if(st->buf) FREEMEM(st->buf);
918 st->buf = buf;
919 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000920
921 return 0;
922}
923
924int
Lev Walkin72ec9092017-07-05 05:49:12 -0700925asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000926 uint8_t *buf, *bp;
927 uint8_t *p;
928 uint8_t *pstart;
929 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000930 int littleEndian = 1; /* Run-time detection */
931 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000932
933 if(!st) {
934 errno = EINVAL;
935 return -1;
936 }
937
Lev Walkin290b4d62017-08-28 17:48:35 -0700938 buf = (uint8_t *)(long *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000939 if(!buf) return -1;
940
Lev Walkind7ad5612004-10-26 08:20:46 +0000941 if(*(char *)&littleEndian) {
942 pstart = (uint8_t *)&value + sizeof(value) - 1;
943 pend1 = (uint8_t *)&value;
944 add = -1;
945 } else {
946 pstart = (uint8_t *)&value;
947 pend1 = pstart + sizeof(value) - 1;
948 add = 1;
949 }
950
Lev Walkind703ff42004-10-21 11:21:25 +0000951 /*
952 * If the contents octet consists of more than one octet,
953 * then bits of the first octet and bit 8 of the second octet:
954 * a) shall not all be ones; and
955 * b) shall not all be zero.
956 */
Lev Walkin33700162004-10-26 09:03:31 +0000957 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000958 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000959 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000960 continue;
961 break;
Lev Walkin33700162004-10-26 09:03:31 +0000962 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000963 continue;
964 break;
965 }
966 break;
967 }
968 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700969 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000970 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000971
972 if(st->buf) FREEMEM(st->buf);
973 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000974 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000975
976 return 0;
977}
Lev Walkinb3751942012-09-02 19:36:47 -0700978
Lev Walkin72ec9092017-07-05 05:49:12 -0700979int
980asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
981 intmax_t v;
982 if(asn_INTEGER2imax(iptr, &v) == 0) {
983 if(v < LONG_MIN || v > LONG_MAX) {
984 errno = ERANGE;
985 return -1;
986 }
987 *l = v;
988 return 0;
989 } else {
990 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700991 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700992}
Lev Walkincad560a2013-03-16 07:00:58 -0700993
Lev Walkin72ec9092017-07-05 05:49:12 -0700994int
995asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
996 uintmax_t v;
997 if(asn_INTEGER2umax(iptr, &v) == 0) {
998 if(v > ULONG_MAX) {
999 errno = ERANGE;
1000 return -1;
1001 }
1002 *l = v;
1003 return 0;
1004 } else {
1005 return -1;
1006 }
1007}
1008
1009int
1010asn_long2INTEGER(INTEGER_t *st, long value) {
1011 return asn_imax2INTEGER(st, value);
1012}
1013
1014int
1015asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1016 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -07001017}
1018
1019/*
1020 * Parse the number in the given string until the given *end position,
1021 * returning the position after the last parsed character back using the
1022 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -07001023 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -07001024 */
Lev Walkin72ec9092017-07-05 05:49:12 -07001025enum asn_strtox_result_e
1026asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001027 int sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001028 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -07001029
Lev Walkinaa067a92017-10-03 16:09:05 -07001030#define ASN1_INTMAX_MAX ((~(uintmax_t)0) >> 1)
1031
1032 const intmax_t upper_boundary = ASN1_INTMAX_MAX / 10;
1033 intmax_t last_digit_max = ASN1_INTMAX_MAX % 10;
Lev Walkine09f9f12012-09-02 23:06:35 -07001034
Lev Walkin72ec9092017-07-05 05:49:12 -07001035 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001036
1037 switch(*str) {
1038 case '-':
1039 last_digit_max++;
1040 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001041 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001042 case '+':
1043 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001044 if(str >= *end) {
1045 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001046 return ASN_STRTOX_EXPECT_MORE;
Lev Walkincad560a2013-03-16 07:00:58 -07001047 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001048 }
1049
Lev Walkin72ec9092017-07-05 05:49:12 -07001050 for(value = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001051 switch(*str) {
1052 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1053 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1054 int d = *str - '0';
Lev Walkin72ec9092017-07-05 05:49:12 -07001055 if(value < upper_boundary) {
1056 value = value * 10 + d;
1057 } else if(value == upper_boundary) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001058 if(d <= last_digit_max) {
1059 if(sign > 0) {
Lev Walkin72ec9092017-07-05 05:49:12 -07001060 value = value * 10 + d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001061 } else {
1062 sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001063 value = -value * 10 - d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001064 }
1065 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001066 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001067 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001068 }
1069 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001070 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001071 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001072 }
1073 }
1074 continue;
1075 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001076 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001077 *intp = sign * value;
1078 return ASN_STRTOX_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001079 }
1080 }
1081
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_OK;
1085}
1086
1087enum asn_strtox_result_e
1088asn_strtol_lim(const char *str, const char **end, long *lp) {
1089 intmax_t value;
1090 switch(asn_strtoimax_lim(str, end, &value)) {
1091 case ASN_STRTOX_ERROR_RANGE:
1092 return ASN_STRTOX_ERROR_RANGE;
1093 case ASN_STRTOX_ERROR_INVAL:
1094 return ASN_STRTOX_ERROR_INVAL;
1095 case ASN_STRTOX_EXPECT_MORE:
1096 return ASN_STRTOX_EXPECT_MORE;
1097 case ASN_STRTOX_OK:
1098 if(value >= LONG_MIN && value <= LONG_MAX) {
1099 *lp = value;
1100 return ASN_STRTOX_OK;
1101 } else {
1102 return ASN_STRTOX_ERROR_RANGE;
1103 }
1104 case ASN_STRTOX_EXTRA_DATA:
1105 if(value >= LONG_MIN && value <= LONG_MAX) {
1106 *lp = value;
1107 return ASN_STRTOX_EXTRA_DATA;
1108 } else {
1109 return ASN_STRTOX_ERROR_RANGE;
1110 }
1111 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001112
1113 assert(!"Unreachable");
1114 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001115}
1116
Lev Walkincd2f48e2017-08-10 02:14:59 -07001117int
1118INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1119 const void *bptr) {
1120 const INTEGER_t *a = aptr;
1121 const INTEGER_t *b = bptr;
1122
1123 (void)td;
1124
1125 if(a && b) {
1126 if(a->size && b->size) {
1127 int sign_a = (a->buf[0] & 0x80) ? -1 : 1;
1128 int sign_b = (b->buf[0] & 0x80) ? -1 : 1;
1129
1130 if(sign_a < sign_b) return -1;
1131 if(sign_a > sign_b) return 1;
1132
1133 /* The shortest integer wins, unless comparing negatives */
1134 if(a->size < b->size) {
1135 return -1 * sign_a;
1136 } else if(a->size > b->size) {
1137 return 1 * sign_b;
1138 }
1139
1140 return sign_a * memcmp(a->buf, b->buf, a->size);
1141 } else if(a->size) {
1142 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1143 return (1) * sign;
1144 } else if(b->size) {
1145 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1146 return (-1) * sign;
1147 } else {
1148 return 0;
1149 }
1150 } else if(!a && !b) {
1151 return 0;
1152 } else if(!a) {
1153 return -1;
1154 } else {
1155 return 1;
1156 }
1157
1158}
1159
Lev Walkina5972be2017-09-29 23:15:58 -07001160asn_random_fill_result_t
1161INTEGER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1162 const asn_encoding_constraints_t *constraints,
1163 size_t max_length) {
1164 const asn_INTEGER_specifics_t *specs =
1165 (const asn_INTEGER_specifics_t *)td->specifics;
1166 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
1167 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1168 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1169 INTEGER_t *st = *sptr;
1170 const asn_INTEGER_enum_map_t *emap;
1171 size_t emap_len;
1172 intmax_t value;
1173 int find_inside_map;
1174
1175 if(max_length == 0) return result_skipped;
1176
1177 if(st == NULL) {
1178 st = (INTEGER_t *)CALLOC(1, sizeof(*st));
1179 if(st == NULL) {
1180 return result_failed;
1181 }
1182 }
1183
1184 if(specs) {
1185 emap = specs->value2enum;
1186 emap_len = specs->map_count;
1187 if(specs->strict_enumeration) {
1188 find_inside_map = emap_len > 0;
1189 } else {
1190 find_inside_map = emap_len ? asn_random_between(0, 1) : 0;
1191 }
1192 } else {
1193 emap = 0;
1194 emap_len = 0;
1195 find_inside_map = 0;
1196 }
1197
1198 if(find_inside_map) {
1199 assert(emap_len > 0);
1200 value = emap[asn_random_between(0, emap_len - 1)].nat_value;
1201 } else {
Lev Walkin18741a92017-10-01 12:40:19 -07001202 const asn_per_constraints_t *ct;
Lev Walkina5972be2017-09-29 23:15:58 -07001203
1204 static const long variants[] = {
1205 -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384,
1206 -16383, -257, -256, -255, -254, -129, -128, -127,
1207 -126, -1, 0, 1, 126, 127, 128, 129,
1208 254, 255, 256, 257, 16383, 16384, 16385, 32767,
1209 32768, 32769, 65534, 65535, 65536, 65537};
1210 if(specs && specs->field_unsigned) {
1211 assert(variants[18] == 0);
1212 value = variants[asn_random_between(
1213 18, sizeof(variants) / sizeof(variants[0]) - 1)];
1214 } else {
1215 value = variants[asn_random_between(
1216 0, sizeof(variants) / sizeof(variants[0]) - 1)];
1217 }
1218
1219 if(!constraints) constraints = &td->encoding_constraints;
Lev Walkin18741a92017-10-01 12:40:19 -07001220 ct = constraints ? constraints->per_constraints : 0;
1221 if(ct && (ct->value.flags & APC_CONSTRAINED)) {
1222 if(value < ct->value.lower_bound || value > ct->value.upper_bound) {
1223 value = asn_random_between(ct->value.lower_bound,
1224 ct->value.upper_bound);
1225 }
1226 }
Lev Walkina5972be2017-09-29 23:15:58 -07001227 }
1228
Lev Walkina5972be2017-09-29 23:15:58 -07001229 if(asn_imax2INTEGER(st, value)) {
1230 if(st == *sptr) {
1231 ASN_STRUCT_RESET(*td, st);
1232 } else {
1233 ASN_STRUCT_FREE(*td, st);
1234 }
1235 return result_failed;
1236 } else {
Lev Walkin18741a92017-10-01 12:40:19 -07001237 *sptr = st;
Lev Walkina5972be2017-09-29 23:15:58 -07001238 result_ok.length = st->size;
1239 return result_ok;
1240 }
1241}