blob: 798fb28feb04c7cad3d634e2fe6df56d6b4afb94 [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 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080039 0 /* Use generic outmost tag fetcher */
40};
41asn_TYPE_descriptor_t asn_DEF_INTEGER = {
42 "INTEGER",
43 "INTEGER",
44 &asn_OP_INTEGER,
45 asn_generic_no_constraint,
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 Walkin76780762017-07-07 10:07:30 -070050 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000051 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000052 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000053 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000054};
55
56/*
Lev Walkinf15320b2004-06-03 03:38:44 +000057 * Encode INTEGER type using DER.
58 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000059asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000060INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000061 int tag_mode, ber_tlv_tag_t tag,
62 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000063 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000064
65 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000066 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000067
68 /*
69 * Canonicalize integer in the buffer.
70 * (Remove too long sign extension, remove some first 0x00 bytes)
71 */
72 if(st->buf) {
73 uint8_t *buf = st->buf;
74 uint8_t *end1 = buf + st->size - 1;
75 int shift;
76
77 /* Compute the number of superfluous leading bytes */
78 for(; buf < end1; buf++) {
79 /*
80 * If the contents octets of an integer value encoding
81 * consist of more than one octet, then the bits of the
82 * first octet and bit 8 of the second octet:
83 * a) shall not all be ones; and
84 * b) shall not all be zero.
85 */
86 switch(*buf) {
87 case 0x00: if((buf[1] & 0x80) == 0)
88 continue;
89 break;
90 case 0xff: if((buf[1] & 0x80))
91 continue;
92 break;
93 }
94 break;
95 }
96
97 /* Remove leading superfluous bytes from the integer */
98 shift = buf - st->buf;
99 if(shift) {
100 uint8_t *nb = st->buf;
101 uint8_t *end;
102
103 st->size -= shift; /* New size, minus bad bytes */
104 end = nb + st->size;
105
106 for(; nb < end; nb++, buf++)
107 *nb = *buf;
108 }
109
110 } /* if(1) */
111
Lev Walkin8e8078a2004-09-26 13:10:40 +0000112 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000113}
114
Lev Walkind62a6622017-08-22 02:31:02 -0700115static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(
116 const asn_INTEGER_specifics_t *specs, const char *lstart,
117 const char *lstop);
Lev Walkine0b56e02005-02-25 12:10:27 +0000118
Lev Walkinf15320b2004-06-03 03:38:44 +0000119/*
120 * INTEGER specific human-readable output.
121 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000122static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700123INTEGER__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 -0700124 const asn_INTEGER_specifics_t *specs =
125 (const asn_INTEGER_specifics_t *)td->specifics;
126 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000127 uint8_t *buf = st->buf;
128 uint8_t *buf_end = st->buf + st->size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700129 intmax_t value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000130 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000131 char *p;
132 int ret;
133
Lev Walkin97f8edc2013-03-28 04:38:41 -0700134 if(specs && specs->field_unsigned)
Lev Walkin72ec9092017-07-05 05:49:12 -0700135 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
Lev Walkin97f8edc2013-03-28 04:38:41 -0700136 else
Lev Walkin72ec9092017-07-05 05:49:12 -0700137 ret = asn_INTEGER2imax(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000138
Lev Walkinf15320b2004-06-03 03:38:44 +0000139 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700140 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000141 const asn_INTEGER_enum_map_t *el;
142 size_t scrsize;
143 char *scr;
144
Lev Walkin97f8edc2013-03-28 04:38:41 -0700145 el = (value >= 0 || !specs || !specs->field_unsigned)
146 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000147 if(el) {
148 scrsize = el->enum_len + 32;
149 scr = (char *)alloca(scrsize);
150 if(plainOrXER == 0)
151 ret = snprintf(scr, scrsize,
Lev Walkin72ec9092017-07-05 05:49:12 -0700152 "%" PRIdMAX " (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000153 else
154 ret = snprintf(scr, scrsize,
155 "<%s/>", el->enum_name);
156 } else if(plainOrXER && specs && specs->strict_enumeration) {
157 ASN_DEBUG("ASN.1 forbids dealing with "
158 "unknown value of ENUMERATED type");
159 errno = EPERM;
160 return -1;
161 } else {
162 scrsize = sizeof(scratch);
163 scr = scratch;
Lev Walkin72ec9092017-07-05 05:49:12 -0700164 ret = snprintf(
165 scr, scrsize,
166 (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX,
167 value);
168 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000169 assert(ret > 0 && (size_t)ret < scrsize);
170 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
171 } else if(plainOrXER && specs && specs->strict_enumeration) {
172 /*
173 * Here and earlier, we cannot encode the ENUMERATED values
174 * if there is no corresponding identifier.
175 */
176 ASN_DEBUG("ASN.1 forbids dealing with "
177 "unknown value of ENUMERATED type");
178 errno = EPERM;
179 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 }
181
182 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000183 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700185 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000186 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000188 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000190 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 p = scratch;
192 }
193 *p++ = h2c[*buf >> 4];
194 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000195 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000196 }
Lev Walkindb13f512004-07-19 17:30:25 +0000197 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000198 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000199
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200 wrote += p - scratch;
201 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
202}
203
204/*
205 * INTEGER specific human-readable output.
206 */
207int
Lev Walkin5e033762004-09-29 13:26:15 +0000208INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000209 asn_app_consume_bytes_f *cb, void *app_key) {
210 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000211 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000212
213 (void)td;
214 (void)ilevel;
215
Lev Walkind500a962005-11-27 13:06:56 +0000216 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000217 ret = cb("<absent>", 8, app_key);
218 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000219 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000220
Lev Walkin8e8078a2004-09-26 13:10:40 +0000221 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000222}
223
Lev Walkine0b56e02005-02-25 12:10:27 +0000224struct e2v_key {
225 const char *start;
226 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700227 const asn_INTEGER_enum_map_t *vemap;
228 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000229};
230static int
231INTEGER__compar_enum2value(const void *kp, const void *am) {
232 const struct e2v_key *key = (const struct e2v_key *)kp;
233 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
234 const char *ptr, *end, *name;
235
236 /* Remap the element (sort by different criterion) */
237 el = key->vemap + key->evmap[el - key->vemap];
238
239 /* Compare strings */
240 for(ptr = key->start, end = key->stop, name = el->enum_name;
241 ptr < end; ptr++, name++) {
242 if(*ptr != *name)
243 return *(const unsigned char *)ptr
244 - *(const unsigned char *)name;
245 }
246 return name[0] ? -1 : 0;
247}
248
249static const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700250INTEGER_map_enum2value(const asn_INTEGER_specifics_t *specs, const char *lstart,
251 const char *lstop) {
252 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000253 int count = specs ? specs->map_count : 0;
254 struct e2v_key key;
255 const char *lp;
256
257 if(!count) return NULL;
258
259 /* Guaranteed: assert(lstart < lstop); */
260 /* Figure out the tag name */
261 for(lstart++, lp = lstart; lp < lstop; lp++) {
262 switch(*lp) {
263 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
264 case 0x2f: /* '/' */ case 0x3e: /* '>' */
265 break;
266 default:
267 continue;
268 }
269 break;
270 }
271 if(lp == lstop) return NULL; /* No tag found */
272 lstop = lp;
273
274 key.start = lstart;
275 key.stop = lstop;
276 key.vemap = specs->value2enum;
277 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000278 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
279 specs->value2enum, count, sizeof(specs->value2enum[0]),
280 INTEGER__compar_enum2value);
281 if(el_found) {
282 /* Remap enum2value into value2enum */
283 el_found = key.vemap + key.evmap[el_found - key.vemap];
284 }
285 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000286}
287
288static int
289INTEGER__compar_value2enum(const void *kp, const void *am) {
290 long a = *(const long *)kp;
291 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
292 long b = el->nat_value;
293 if(a < b) return -1;
294 else if(a == b) return 0;
295 else return 1;
296}
297
Lev Walkinc2350112005-03-29 17:19:53 +0000298const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700299INTEGER_map_value2enum(const asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000300 int count = specs ? specs->map_count : 0;
301 if(!count) return 0;
302 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
303 count, sizeof(specs->value2enum[0]),
304 INTEGER__compar_value2enum);
305}
306
Lev Walkinc744a022006-09-15 18:33:25 +0000307static int
308INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
309 void *p = MALLOC(min_size + 1);
310 if(p) {
311 void *b = st->buf;
312 st->size = 0;
313 st->buf = p;
314 FREEMEM(b);
315 return 0;
316 } else {
317 return -1;
318 }
319}
320
Lev Walkind703ff42004-10-21 11:21:25 +0000321/*
322 * Decode the chunk of XML text encoding INTEGER.
323 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000324static enum xer_pbd_rval
325INTEGER__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 +0000326 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700327 intmax_t dec_value;
328 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000329 const char *lp;
330 const char *lstart = (const char *)chunk_buf;
331 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000332 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700333 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000334 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000335 ST_WAITDIGITS,
336 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700337 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000338 ST_HEXDIGIT1,
339 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700340 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000341 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700342 ST_END_ENUM,
343 ST_UNEXPECTED
344 } state = ST_LEADSPACE;
345 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
346 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000347
Lev Walkinc744a022006-09-15 18:33:25 +0000348 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000349 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
350 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000351
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000352 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
353 return XPBD_SYSTEM_FAILURE;
354
Lev Walkind703ff42004-10-21 11:21:25 +0000355 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000356 * We may have received a tag here. It will be processed inline.
357 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000358 */
Lev Walkinb3751942012-09-02 19:36:47 -0700359 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000360 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000361 switch(lv) {
362 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000363 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700364 case ST_LEADSPACE:
365 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700366 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000367 case ST_SKIPSPHEX:
368 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700369 case ST_DIGITS:
370 dec_value_end = lp;
371 state = ST_DIGITS_TRAILSPACE;
372 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700373 case ST_HEXCOLON:
374 state = ST_HEXDIGITS_TRAILSPACE;
375 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000376 default:
377 break;
378 }
Lev Walkind703ff42004-10-21 11:21:25 +0000379 break;
380 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700381 if(state == ST_LEADSPACE) {
382 dec_value = 0;
383 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000384 state = ST_WAITDIGITS;
385 continue;
386 }
387 break;
388 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700389 if(state == ST_LEADSPACE) {
390 dec_value = 0;
391 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000392 state = ST_WAITDIGITS;
393 continue;
394 }
395 break;
396 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
397 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000398 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700399 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000400 case ST_SKIPSPHEX: /* Fall through */
401 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700402 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000403 state = ST_HEXDIGIT2;
404 continue;
405 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700406 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000407 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700408 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000409 continue;
410 case ST_HEXCOLON:
411 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700412 case ST_LEADSPACE:
413 dec_value = 0;
414 dec_value_start = lp;
415 /* FALL THROUGH */
416 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000417 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700418 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700419 default:
420 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000421 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700422 break;
423 case 0x3c: /* '<', start of XML encoded enumeration */
424 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000425 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000426 el = INTEGER_map_enum2value(
Lev Walkind62a6622017-08-22 02:31:02 -0700427 (const asn_INTEGER_specifics_t *)
Lev Walkine0b56e02005-02-25 12:10:27 +0000428 td->specifics, lstart, lstop);
429 if(el) {
430 ASN_DEBUG("Found \"%s\" => %ld",
431 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700432 dec_value = el->nat_value;
433 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000434 lp = lstop - 1;
435 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000436 }
437 ASN_DEBUG("Unknown identifier for INTEGER");
438 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000439 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000440 case 0x3a: /* ':' */
441 if(state == ST_HEXCOLON) {
442 /* This colon is expected */
443 state = ST_HEXDIGIT1;
444 continue;
445 } else if(state == ST_DIGITS) {
446 /* The colon here means that we have
447 * decoded the first two hexadecimal
448 * places as a decimal value.
449 * Switch decoding mode. */
450 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000451 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700452 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000453 lp = lstart - 1;
454 continue;
455 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400456 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000457 break;
458 }
459 /* [A-Fa-f] */
460 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
461 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
462 switch(state) {
463 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700464 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000465 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700466 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
467 hex_value += 10;
468 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000469 state = ST_HEXDIGIT2;
470 continue;
471 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700472 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
473 hex_value += 10;
474 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000475 state = ST_HEXCOLON;
476 continue;
477 case ST_DIGITS:
478 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000479 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700480 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000481 lp = lstart - 1;
482 continue;
483 default:
484 break;
485 }
486 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000487 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000488
489 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700490 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400491 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700492 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000493 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000494 }
495
Lev Walkinc744a022006-09-15 18:33:25 +0000496 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700497 case ST_END_ENUM:
498 /* Got a complete and valid enumeration encoded as a tag. */
499 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000500 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700501 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700502 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700503 case ST_DIGITS_TRAILSPACE:
504 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700505 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700506 case ASN_STRTOX_OK:
Lev Walkine09f9f12012-09-02 23:06:35 -0700507 break;
Lev Walkin72ec9092017-07-05 05:49:12 -0700508 case ASN_STRTOX_ERROR_RANGE:
Lev Walkine09f9f12012-09-02 23:06:35 -0700509 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700510 case ASN_STRTOX_ERROR_INVAL:
511 case ASN_STRTOX_EXPECT_MORE:
512 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700513 return XPBD_BROKEN_ENCODING;
514 }
Lev Walkinc744a022006-09-15 18:33:25 +0000515 break;
516 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700517 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000518 st->buf[st->size] = 0; /* Just in case termination */
519 return XPBD_BODY_CONSUMED;
520 case ST_HEXDIGIT1:
521 case ST_HEXDIGIT2:
522 case ST_SKIPSPHEX:
523 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700524 case ST_LEADSPACE:
525 /* Content not found */
526 return XPBD_NOT_BODY_IGNORE;
527 case ST_WAITDIGITS:
528 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700529 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
530 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000531 }
Lev Walkind703ff42004-10-21 11:21:25 +0000532
Lev Walkine09f9f12012-09-02 23:06:35 -0700533 /*
534 * Convert the result of parsing of enumeration or a straight
535 * decimal value into a BER representation.
536 */
537 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000538 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000539
Lev Walkin0fab1a62005-03-09 22:19:25 +0000540 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000541}
542
543asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700544INTEGER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkind703ff42004-10-21 11:21:25 +0000545 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000546 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000547
548 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000549 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000550 buf_ptr, size, INTEGER__xer_body_decode);
551}
552
Lev Walkina9cc46e2004-09-22 16:06:28 +0000553asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000554INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000555 int ilevel, enum xer_encoder_flags_e flags,
556 asn_app_consume_bytes_f *cb, void *app_key) {
557 const INTEGER_t *st = (const INTEGER_t *)sptr;
558 asn_enc_rval_t er;
559
560 (void)ilevel;
561 (void)flags;
562
Lev Walkind500a962005-11-27 13:06:56 +0000563 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700564 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000565
Lev Walkine0b56e02005-02-25 12:10:27 +0000566 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700567 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000568
Lev Walkin7c1dc052016-03-14 03:08:15 -0700569 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000570}
571
Lev Walkind7703cf2012-09-03 01:45:03 -0700572#ifndef ASN_DISABLE_PER_SUPPORT
573
Lev Walkin59b176e2005-11-26 11:25:14 +0000574asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700575INTEGER_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700576 const asn_per_constraints_t *constraints, void **sptr,
577 asn_per_data_t *pd) {
Lev Walkind62a6622017-08-22 02:31:02 -0700578 const asn_INTEGER_specifics_t *specs =
579 (const asn_INTEGER_specifics_t *)td->specifics;
580 asn_dec_rval_t rval = { RC_OK, 0 };
Lev Walkin59b176e2005-11-26 11:25:14 +0000581 INTEGER_t *st = (INTEGER_t *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -0700582 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000583 int repeat;
584
585 (void)opt_codec_ctx;
586
587 if(!st) {
588 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700589 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000590 }
591
592 if(!constraints) constraints = td->per_constraints;
593 ct = constraints ? &constraints->value : 0;
594
595 if(ct && ct->flags & APC_EXTENSIBLE) {
596 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700597 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000598 if(inext) ct = 0;
599 }
600
601 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000602 st->buf = 0;
603 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000604 if(ct) {
605 if(ct->flags & APC_SEMI_CONSTRAINED) {
606 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700607 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000608 st->size = 1;
609 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
610 size_t size = (ct->range_bits + 7) >> 3;
611 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700612 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000613 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000614 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000615 }
616
Lev Walkin6c527842014-02-09 04:34:54 -0800617 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000618 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800619 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000620 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
621 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800622 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700623 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800624
625 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700626 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800627 if(uper_get_constrained_whole_number(pd,
628 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700629 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800630 ASN_DEBUG("Got value %lu + low %ld",
631 uvalue, ct->lower_bound);
632 uvalue += ct->lower_bound;
633 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700634 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800635 } else {
Lev Walkin38a91df2017-09-17 23:52:33 -0700636 unsigned long uvalue = 0;
637 long svalue;
Lev Walkin6c527842014-02-09 04:34:54 -0800638 if(uper_get_constrained_whole_number(pd,
Lev Walkin38a91df2017-09-17 23:52:33 -0700639 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700640 ASN__DECODE_STARVED;
Lev Walkin38a91df2017-09-17 23:52:33 -0700641 ASN_DEBUG("Got value %lu + low %ld",
642 uvalue, ct->lower_bound);
643 svalue = ct->lower_bound + (long)uvalue;
Lev Walkin6c527842014-02-09 04:34:54 -0800644 if(asn_long2INTEGER(st, svalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700645 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800646 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000647 return rval;
648 }
649 } else {
650 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
651 }
652
653 /* X.691, #12.2.3, #12.2.4 */
654 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700655 ssize_t len = 0;
656 void *p = NULL;
657 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000658
659 /* Get the PER length */
660 len = uper_get_length(pd, -1, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700661 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000662
663 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700664 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000665 st->buf = (uint8_t *)p;
666
667 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700668 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000669 st->size += len;
670 } while(repeat);
671 st->buf[st->size] = 0; /* JIC */
672
673 /* #12.2.3 */
674 if(ct && ct->lower_bound) {
675 /*
676 * TODO: replace by in-place arithmetics.
677 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700678 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000679 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700680 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700681 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700682 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000683 }
684
685 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000686}
687
Lev Walkin523de9e2006-08-18 01:34:18 +0000688asn_enc_rval_t
689INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700690 const asn_per_constraints_t *constraints, void *sptr,
691 asn_per_outp_t *po) {
Lev Walkind62a6622017-08-22 02:31:02 -0700692 const asn_INTEGER_specifics_t *specs =
693 (const asn_INTEGER_specifics_t *)td->specifics;
694 asn_enc_rval_t er;
Lev Walkin523de9e2006-08-18 01:34:18 +0000695 INTEGER_t *st = (INTEGER_t *)sptr;
696 const uint8_t *buf;
697 const uint8_t *end;
Lev Walkin494fb702017-08-07 20:07:00 -0700698 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000699 long value = 0;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100700 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000701
Lev Walkin7c1dc052016-03-14 03:08:15 -0700702 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000703
704 if(!constraints) constraints = td->per_constraints;
705 ct = constraints ? &constraints->value : 0;
706
707 er.encoded = 0;
708
709 if(ct) {
710 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000711 if(specs && specs->field_unsigned) {
712 unsigned long uval;
713 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700714 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000715 /* Check proper range */
716 if(ct->flags & APC_SEMI_CONSTRAINED) {
717 if(uval < (unsigned long)ct->lower_bound)
718 inext = 1;
719 } else if(ct->range_bits >= 0) {
720 if(uval < (unsigned long)ct->lower_bound
721 || uval > (unsigned long)ct->upper_bound)
722 inext = 1;
723 }
724 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
725 uval, st->buf[0], st->size,
726 ct->lower_bound, ct->upper_bound,
727 inext ? "ext" : "fix");
728 value = uval;
729 } else {
730 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700731 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000732 /* Check proper range */
733 if(ct->flags & APC_SEMI_CONSTRAINED) {
734 if(value < ct->lower_bound)
735 inext = 1;
736 } else if(ct->range_bits >= 0) {
737 if(value < ct->lower_bound
738 || value > ct->upper_bound)
739 inext = 1;
740 }
741 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
742 value, st->buf[0], st->size,
743 ct->lower_bound, ct->upper_bound,
744 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000745 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000746 if(ct->flags & APC_EXTENSIBLE) {
747 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700748 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000749 if(inext) ct = 0;
750 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700751 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000752 }
753 }
754
755
Lev Walkin6c527842014-02-09 04:34:54 -0800756 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000757 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800758 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800759 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
760 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100761 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800762 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700763 ASN__ENCODE_FAILED;
764 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000765 }
766
767 if(ct && ct->lower_bound) {
768 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
769 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700770 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000771 }
772
773 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
774 ssize_t mayEncode = uper_put_length(po, end - buf);
775 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700776 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000777 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700778 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000779 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000780 }
781
Lev Walkin7c1dc052016-03-14 03:08:15 -0700782 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000783}
784
Lev Walkind7703cf2012-09-03 01:45:03 -0700785#endif /* ASN_DISABLE_PER_SUPPORT */
786
Lev Walkin642b92f2017-09-17 22:16:02 -0700787
788/*
789 * This function is only to get rid of Undefined Behavior Sanitizer warning.
790 */
791static intmax_t CLANG_NO_SANITIZE("shift-base")
792asn__safe_integer_convert_helper(const uint8_t *b, const uint8_t *end) {
793 intmax_t value;
794
795 /* Perform the sign initialization */
796 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
797 if((*b >> 7)) {
798 value = -1;
799 } else {
800 value = 0;
801 }
802
803 /* Conversion engine */
804 for(; b < end; b++) {
805 value = (value << 8) | *b;
806 }
807
808 return value;
809}
810
Lev Walkinf15320b2004-06-03 03:38:44 +0000811int
Lev Walkin72ec9092017-07-05 05:49:12 -0700812asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000813 uint8_t *b, *end;
814 size_t size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000815
816 /* Sanity checking */
817 if(!iptr || !iptr->buf || !lptr) {
818 errno = EINVAL;
819 return -1;
820 }
821
822 /* Cache the begin/end of the buffer */
823 b = iptr->buf; /* Start of the INTEGER buffer */
824 size = iptr->size;
825 end = b + size; /* Where to stop */
826
Lev Walkin642b92f2017-09-17 22:16:02 -0700827 if(size > sizeof(intmax_t)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000828 uint8_t *end1 = end - 1;
829 /*
830 * Slightly more advanced processing,
Lev Walkin642b92f2017-09-17 22:16:02 -0700831 * able to process INTEGERs with >sizeof(intmax_t) bytes
Lev Walkin72ec9092017-07-05 05:49:12 -0700832 * when the actual value is small, e.g. for intmax_t == int32_t
833 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000834 */
835 /* Skip out the insignificant leading bytes */
836 for(; b < end1; b++) {
837 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700838 case 0x00: if((b[1] & 0x80) == 0) continue; break;
839 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000840 }
841 break;
842 }
843
844 size = end - b;
Lev Walkin642b92f2017-09-17 22:16:02 -0700845 if(size > sizeof(intmax_t)) {
846 /* Still cannot fit the sizeof(intmax_t) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000847 errno = ERANGE;
848 return -1;
849 }
850 }
851
852 /* Shortcut processing of a corner case */
853 if(end == b) {
854 *lptr = 0;
855 return 0;
856 }
857
Lev Walkin642b92f2017-09-17 22:16:02 -0700858 *lptr = asn__safe_integer_convert_helper(b, end);
Lev Walkinf15320b2004-06-03 03:38:44 +0000859 return 0;
860}
Lev Walkind703ff42004-10-21 11:21:25 +0000861
Lev Walkin72ec9092017-07-05 05:49:12 -0700862/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000863int
Lev Walkin72ec9092017-07-05 05:49:12 -0700864asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000865 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700866 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000867 size_t size;
868
869 if(!iptr || !iptr->buf || !lptr) {
870 errno = EINVAL;
871 return -1;
872 }
873
874 b = iptr->buf;
875 size = iptr->size;
876 end = b + size;
877
878 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700879 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000880 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700881 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000882 errno = ERANGE;
883 return -1;
884 }
885 }
886
887 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700888 for(value = 0; b < end; b++)
889 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000890
Lev Walkin72ec9092017-07-05 05:49:12 -0700891 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000892 return 0;
893}
894
895int
Lev Walkin72ec9092017-07-05 05:49:12 -0700896asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
897 uint8_t *buf;
898 uint8_t *end;
899 uint8_t *b;
900 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000901
Lev Walkin72ec9092017-07-05 05:49:12 -0700902 if(value <= INTMAX_MAX) {
903 return asn_imax2INTEGER(st, value);
904 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000905
Lev Walkin72ec9092017-07-05 05:49:12 -0700906 buf = (uint8_t *)MALLOC(1 + sizeof(value));
907 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000908
Lev Walkin72ec9092017-07-05 05:49:12 -0700909 end = buf + (sizeof(value) + 1);
910 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
911 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
912 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000913
Lev Walkin72ec9092017-07-05 05:49:12 -0700914 if(st->buf) FREEMEM(st->buf);
915 st->buf = buf;
916 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000917
918 return 0;
919}
920
921int
Lev Walkin72ec9092017-07-05 05:49:12 -0700922asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000923 uint8_t *buf, *bp;
924 uint8_t *p;
925 uint8_t *pstart;
926 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000927 int littleEndian = 1; /* Run-time detection */
928 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000929
930 if(!st) {
931 errno = EINVAL;
932 return -1;
933 }
934
Lev Walkin290b4d62017-08-28 17:48:35 -0700935 buf = (uint8_t *)(long *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000936 if(!buf) return -1;
937
Lev Walkind7ad5612004-10-26 08:20:46 +0000938 if(*(char *)&littleEndian) {
939 pstart = (uint8_t *)&value + sizeof(value) - 1;
940 pend1 = (uint8_t *)&value;
941 add = -1;
942 } else {
943 pstart = (uint8_t *)&value;
944 pend1 = pstart + sizeof(value) - 1;
945 add = 1;
946 }
947
Lev Walkind703ff42004-10-21 11:21:25 +0000948 /*
949 * If the contents octet consists of more than one octet,
950 * then bits of the first octet and bit 8 of the second octet:
951 * a) shall not all be ones; and
952 * b) shall not all be zero.
953 */
Lev Walkin33700162004-10-26 09:03:31 +0000954 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000955 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000956 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000957 continue;
958 break;
Lev Walkin33700162004-10-26 09:03:31 +0000959 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000960 continue;
961 break;
962 }
963 break;
964 }
965 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700966 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000967 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000968
969 if(st->buf) FREEMEM(st->buf);
970 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000971 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000972
973 return 0;
974}
Lev Walkinb3751942012-09-02 19:36:47 -0700975
Lev Walkin72ec9092017-07-05 05:49:12 -0700976int
977asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
978 intmax_t v;
979 if(asn_INTEGER2imax(iptr, &v) == 0) {
980 if(v < LONG_MIN || v > LONG_MAX) {
981 errno = ERANGE;
982 return -1;
983 }
984 *l = v;
985 return 0;
986 } else {
987 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700988 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700989}
Lev Walkincad560a2013-03-16 07:00:58 -0700990
Lev Walkin72ec9092017-07-05 05:49:12 -0700991int
992asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
993 uintmax_t v;
994 if(asn_INTEGER2umax(iptr, &v) == 0) {
995 if(v > ULONG_MAX) {
996 errno = ERANGE;
997 return -1;
998 }
999 *l = v;
1000 return 0;
1001 } else {
1002 return -1;
1003 }
1004}
1005
1006int
1007asn_long2INTEGER(INTEGER_t *st, long value) {
1008 return asn_imax2INTEGER(st, value);
1009}
1010
1011int
1012asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1013 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -07001014}
1015
1016/*
1017 * Parse the number in the given string until the given *end position,
1018 * returning the position after the last parsed character back using the
1019 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -07001020 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -07001021 */
Lev Walkin72ec9092017-07-05 05:49:12 -07001022enum asn_strtox_result_e
1023asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001024 int sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001025 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -07001026
Lev Walkin72ec9092017-07-05 05:49:12 -07001027 const intmax_t upper_boundary = INTMAX_MAX / 10;
1028 intmax_t last_digit_max = INTMAX_MAX % 10;
Lev Walkine09f9f12012-09-02 23:06:35 -07001029
Lev Walkin72ec9092017-07-05 05:49:12 -07001030 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001031
1032 switch(*str) {
1033 case '-':
1034 last_digit_max++;
1035 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001036 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001037 case '+':
1038 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001039 if(str >= *end) {
1040 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001041 return ASN_STRTOX_EXPECT_MORE;
Lev Walkincad560a2013-03-16 07:00:58 -07001042 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001043 }
1044
Lev Walkin72ec9092017-07-05 05:49:12 -07001045 for(value = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001046 switch(*str) {
1047 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1048 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1049 int d = *str - '0';
Lev Walkin72ec9092017-07-05 05:49:12 -07001050 if(value < upper_boundary) {
1051 value = value * 10 + d;
1052 } else if(value == upper_boundary) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001053 if(d <= last_digit_max) {
1054 if(sign > 0) {
Lev Walkin72ec9092017-07-05 05:49:12 -07001055 value = value * 10 + d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001056 } else {
1057 sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001058 value = -value * 10 - d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001059 }
1060 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001061 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001062 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001063 }
1064 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001065 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001066 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001067 }
1068 }
1069 continue;
1070 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001071 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001072 *intp = sign * value;
1073 return ASN_STRTOX_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001074 }
1075 }
1076
Lev Walkincad560a2013-03-16 07:00:58 -07001077 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001078 *intp = sign * value;
1079 return ASN_STRTOX_OK;
1080}
1081
1082enum asn_strtox_result_e
1083asn_strtol_lim(const char *str, const char **end, long *lp) {
1084 intmax_t value;
1085 switch(asn_strtoimax_lim(str, end, &value)) {
1086 case ASN_STRTOX_ERROR_RANGE:
1087 return ASN_STRTOX_ERROR_RANGE;
1088 case ASN_STRTOX_ERROR_INVAL:
1089 return ASN_STRTOX_ERROR_INVAL;
1090 case ASN_STRTOX_EXPECT_MORE:
1091 return ASN_STRTOX_EXPECT_MORE;
1092 case ASN_STRTOX_OK:
1093 if(value >= LONG_MIN && value <= LONG_MAX) {
1094 *lp = value;
1095 return ASN_STRTOX_OK;
1096 } else {
1097 return ASN_STRTOX_ERROR_RANGE;
1098 }
1099 case ASN_STRTOX_EXTRA_DATA:
1100 if(value >= LONG_MIN && value <= LONG_MAX) {
1101 *lp = value;
1102 return ASN_STRTOX_EXTRA_DATA;
1103 } else {
1104 return ASN_STRTOX_ERROR_RANGE;
1105 }
1106 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001107
1108 assert(!"Unreachable");
1109 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001110}
1111
Lev Walkincd2f48e2017-08-10 02:14:59 -07001112int
1113INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1114 const void *bptr) {
1115 const INTEGER_t *a = aptr;
1116 const INTEGER_t *b = bptr;
1117
1118 (void)td;
1119
1120 if(a && b) {
1121 if(a->size && b->size) {
1122 int sign_a = (a->buf[0] & 0x80) ? -1 : 1;
1123 int sign_b = (b->buf[0] & 0x80) ? -1 : 1;
1124
1125 if(sign_a < sign_b) return -1;
1126 if(sign_a > sign_b) return 1;
1127
1128 /* The shortest integer wins, unless comparing negatives */
1129 if(a->size < b->size) {
1130 return -1 * sign_a;
1131 } else if(a->size > b->size) {
1132 return 1 * sign_b;
1133 }
1134
1135 return sign_a * memcmp(a->buf, b->buf, a->size);
1136 } else if(a->size) {
1137 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1138 return (1) * sign;
1139 } else if(b->size) {
1140 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1141 return (-1) * sign;
1142 } else {
1143 return 0;
1144 }
1145 } else if(!a && !b) {
1146 return 0;
1147 } else if(!a) {
1148 return -1;
1149 } else {
1150 return 1;
1151 }
1152
1153}
1154