blob: 86202b7bbc21d4fec338d1bad4b2c569a2fad8a2 [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};
Lev Walkin5e033762004-09-29 13:26:15 +000017asn_TYPE_descriptor_t asn_DEF_INTEGER = {
Lev Walkinf15320b2004-06-03 03:38:44 +000018 "INTEGER",
Lev Walkindc06f6b2004-10-20 15:50:55 +000019 "INTEGER",
Lev Walkin8077f932017-08-07 17:27:43 -070020 INTEGER_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000021 INTEGER_print,
Bi-Ruei, Chiue1bf2192017-08-19 00:55:25 +080022 INTEGER_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000023 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000024 ber_decode_primitive,
Lev Walkinf15320b2004-06-03 03:38:44 +000025 INTEGER_encode_der,
Lev Walkind703ff42004-10-21 11:21:25 +000026 INTEGER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000027 INTEGER_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070028#ifdef ASN_DISABLE_OER_SUPPORT
29 0,
30 0,
31#else
Lev Walkin527497f2017-07-14 08:56:36 +040032 INTEGER_decode_oer, /* OER decoder */
33 INTEGER_encode_oer, /* Canonical OER encoder */
Lev Walkincc159472017-07-06 08:26:36 -070034#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040035#ifdef ASN_DISABLE_PER_SUPPORT
36 0,
37 0,
38#else
39 INTEGER_decode_uper, /* Unaligned PER decoder */
40 INTEGER_encode_uper, /* Unaligned PER encoder */
41#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000042 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000043 asn_DEF_INTEGER_tags,
44 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
45 asn_DEF_INTEGER_tags, /* Same as above */
46 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070047 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000048 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000049 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000050 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000051};
52
53/*
Lev Walkinf15320b2004-06-03 03:38:44 +000054 * Encode INTEGER type using DER.
55 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000056asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000057INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000058 int tag_mode, ber_tlv_tag_t tag,
59 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000060 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000061
62 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000063 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000064
65 /*
66 * Canonicalize integer in the buffer.
67 * (Remove too long sign extension, remove some first 0x00 bytes)
68 */
69 if(st->buf) {
70 uint8_t *buf = st->buf;
71 uint8_t *end1 = buf + st->size - 1;
72 int shift;
73
74 /* Compute the number of superfluous leading bytes */
75 for(; buf < end1; buf++) {
76 /*
77 * If the contents octets of an integer value encoding
78 * consist of more than one octet, then the bits of the
79 * first octet and bit 8 of the second octet:
80 * a) shall not all be ones; and
81 * b) shall not all be zero.
82 */
83 switch(*buf) {
84 case 0x00: if((buf[1] & 0x80) == 0)
85 continue;
86 break;
87 case 0xff: if((buf[1] & 0x80))
88 continue;
89 break;
90 }
91 break;
92 }
93
94 /* Remove leading superfluous bytes from the integer */
95 shift = buf - st->buf;
96 if(shift) {
97 uint8_t *nb = st->buf;
98 uint8_t *end;
99
100 st->size -= shift; /* New size, minus bad bytes */
101 end = nb + st->size;
102
103 for(; nb < end; nb++, buf++)
104 *nb = *buf;
105 }
106
107 } /* if(1) */
108
Lev Walkin8e8078a2004-09-26 13:10:40 +0000109 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000110}
111
Lev Walkind62a6622017-08-22 02:31:02 -0700112static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(
113 const asn_INTEGER_specifics_t *specs, const char *lstart,
114 const char *lstop);
Lev Walkine0b56e02005-02-25 12:10:27 +0000115
Lev Walkinf15320b2004-06-03 03:38:44 +0000116/*
117 * INTEGER specific human-readable output.
118 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000119static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700120INTEGER__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 -0700121 const asn_INTEGER_specifics_t *specs =
122 (const asn_INTEGER_specifics_t *)td->specifics;
123 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000124 uint8_t *buf = st->buf;
125 uint8_t *buf_end = st->buf + st->size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700126 intmax_t value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000127 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000128 char *p;
129 int ret;
130
Lev Walkin97f8edc2013-03-28 04:38:41 -0700131 if(specs && specs->field_unsigned)
Lev Walkin72ec9092017-07-05 05:49:12 -0700132 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
Lev Walkin97f8edc2013-03-28 04:38:41 -0700133 else
Lev Walkin72ec9092017-07-05 05:49:12 -0700134 ret = asn_INTEGER2imax(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000135
Lev Walkinf15320b2004-06-03 03:38:44 +0000136 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700137 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000138 const asn_INTEGER_enum_map_t *el;
139 size_t scrsize;
140 char *scr;
141
Lev Walkin97f8edc2013-03-28 04:38:41 -0700142 el = (value >= 0 || !specs || !specs->field_unsigned)
143 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000144 if(el) {
145 scrsize = el->enum_len + 32;
146 scr = (char *)alloca(scrsize);
147 if(plainOrXER == 0)
148 ret = snprintf(scr, scrsize,
Lev Walkin72ec9092017-07-05 05:49:12 -0700149 "%" PRIdMAX " (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000150 else
151 ret = snprintf(scr, scrsize,
152 "<%s/>", el->enum_name);
153 } else if(plainOrXER && specs && specs->strict_enumeration) {
154 ASN_DEBUG("ASN.1 forbids dealing with "
155 "unknown value of ENUMERATED type");
156 errno = EPERM;
157 return -1;
158 } else {
159 scrsize = sizeof(scratch);
160 scr = scratch;
Lev Walkin72ec9092017-07-05 05:49:12 -0700161 ret = snprintf(
162 scr, scrsize,
163 (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX,
164 value);
165 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000166 assert(ret > 0 && (size_t)ret < scrsize);
167 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
168 } else if(plainOrXER && specs && specs->strict_enumeration) {
169 /*
170 * Here and earlier, we cannot encode the ENUMERATED values
171 * if there is no corresponding identifier.
172 */
173 ASN_DEBUG("ASN.1 forbids dealing with "
174 "unknown value of ENUMERATED type");
175 errno = EPERM;
176 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 }
178
179 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000180 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700182 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000183 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000185 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000187 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 p = scratch;
189 }
190 *p++ = h2c[*buf >> 4];
191 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000192 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 }
Lev Walkindb13f512004-07-19 17:30:25 +0000194 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000195 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000196
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197 wrote += p - scratch;
198 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
199}
200
201/*
202 * INTEGER specific human-readable output.
203 */
204int
Lev Walkin5e033762004-09-29 13:26:15 +0000205INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000206 asn_app_consume_bytes_f *cb, void *app_key) {
207 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000208 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000209
210 (void)td;
211 (void)ilevel;
212
Lev Walkind500a962005-11-27 13:06:56 +0000213 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000214 ret = cb("<absent>", 8, app_key);
215 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000216 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000217
Lev Walkin8e8078a2004-09-26 13:10:40 +0000218 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000219}
220
Lev Walkine0b56e02005-02-25 12:10:27 +0000221struct e2v_key {
222 const char *start;
223 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700224 const asn_INTEGER_enum_map_t *vemap;
225 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000226};
227static int
228INTEGER__compar_enum2value(const void *kp, const void *am) {
229 const struct e2v_key *key = (const struct e2v_key *)kp;
230 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
231 const char *ptr, *end, *name;
232
233 /* Remap the element (sort by different criterion) */
234 el = key->vemap + key->evmap[el - key->vemap];
235
236 /* Compare strings */
237 for(ptr = key->start, end = key->stop, name = el->enum_name;
238 ptr < end; ptr++, name++) {
239 if(*ptr != *name)
240 return *(const unsigned char *)ptr
241 - *(const unsigned char *)name;
242 }
243 return name[0] ? -1 : 0;
244}
245
246static const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700247INTEGER_map_enum2value(const asn_INTEGER_specifics_t *specs, const char *lstart,
248 const char *lstop) {
249 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000250 int count = specs ? specs->map_count : 0;
251 struct e2v_key key;
252 const char *lp;
253
254 if(!count) return NULL;
255
256 /* Guaranteed: assert(lstart < lstop); */
257 /* Figure out the tag name */
258 for(lstart++, lp = lstart; lp < lstop; lp++) {
259 switch(*lp) {
260 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
261 case 0x2f: /* '/' */ case 0x3e: /* '>' */
262 break;
263 default:
264 continue;
265 }
266 break;
267 }
268 if(lp == lstop) return NULL; /* No tag found */
269 lstop = lp;
270
271 key.start = lstart;
272 key.stop = lstop;
273 key.vemap = specs->value2enum;
274 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000275 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
276 specs->value2enum, count, sizeof(specs->value2enum[0]),
277 INTEGER__compar_enum2value);
278 if(el_found) {
279 /* Remap enum2value into value2enum */
280 el_found = key.vemap + key.evmap[el_found - key.vemap];
281 }
282 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000283}
284
285static int
286INTEGER__compar_value2enum(const void *kp, const void *am) {
287 long a = *(const long *)kp;
288 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
289 long b = el->nat_value;
290 if(a < b) return -1;
291 else if(a == b) return 0;
292 else return 1;
293}
294
Lev Walkinc2350112005-03-29 17:19:53 +0000295const asn_INTEGER_enum_map_t *
Lev Walkind62a6622017-08-22 02:31:02 -0700296INTEGER_map_value2enum(const asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000297 int count = specs ? specs->map_count : 0;
298 if(!count) return 0;
299 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
300 count, sizeof(specs->value2enum[0]),
301 INTEGER__compar_value2enum);
302}
303
Lev Walkinc744a022006-09-15 18:33:25 +0000304static int
305INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
306 void *p = MALLOC(min_size + 1);
307 if(p) {
308 void *b = st->buf;
309 st->size = 0;
310 st->buf = p;
311 FREEMEM(b);
312 return 0;
313 } else {
314 return -1;
315 }
316}
317
Lev Walkind703ff42004-10-21 11:21:25 +0000318/*
319 * Decode the chunk of XML text encoding INTEGER.
320 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000321static enum xer_pbd_rval
322INTEGER__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 +0000323 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700324 intmax_t dec_value;
325 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000326 const char *lp;
327 const char *lstart = (const char *)chunk_buf;
328 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000329 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700330 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000331 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000332 ST_WAITDIGITS,
333 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700334 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000335 ST_HEXDIGIT1,
336 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700337 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000338 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700339 ST_END_ENUM,
340 ST_UNEXPECTED
341 } state = ST_LEADSPACE;
342 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
343 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000344
Lev Walkinc744a022006-09-15 18:33:25 +0000345 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000346 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
347 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000348
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000349 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
350 return XPBD_SYSTEM_FAILURE;
351
Lev Walkind703ff42004-10-21 11:21:25 +0000352 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000353 * We may have received a tag here. It will be processed inline.
354 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000355 */
Lev Walkinb3751942012-09-02 19:36:47 -0700356 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000357 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000358 switch(lv) {
359 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000360 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700361 case ST_LEADSPACE:
362 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700363 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000364 case ST_SKIPSPHEX:
365 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700366 case ST_DIGITS:
367 dec_value_end = lp;
368 state = ST_DIGITS_TRAILSPACE;
369 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700370 case ST_HEXCOLON:
371 state = ST_HEXDIGITS_TRAILSPACE;
372 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000373 default:
374 break;
375 }
Lev Walkind703ff42004-10-21 11:21:25 +0000376 break;
377 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700378 if(state == ST_LEADSPACE) {
379 dec_value = 0;
380 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000381 state = ST_WAITDIGITS;
382 continue;
383 }
384 break;
385 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700386 if(state == ST_LEADSPACE) {
387 dec_value = 0;
388 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000389 state = ST_WAITDIGITS;
390 continue;
391 }
392 break;
393 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
394 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000395 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700396 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000397 case ST_SKIPSPHEX: /* Fall through */
398 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700399 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000400 state = ST_HEXDIGIT2;
401 continue;
402 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700403 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000404 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700405 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000406 continue;
407 case ST_HEXCOLON:
408 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700409 case ST_LEADSPACE:
410 dec_value = 0;
411 dec_value_start = lp;
412 /* FALL THROUGH */
413 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000414 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700415 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700416 default:
417 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000418 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700419 break;
420 case 0x3c: /* '<', start of XML encoded enumeration */
421 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000422 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000423 el = INTEGER_map_enum2value(
Lev Walkind62a6622017-08-22 02:31:02 -0700424 (const asn_INTEGER_specifics_t *)
Lev Walkine0b56e02005-02-25 12:10:27 +0000425 td->specifics, lstart, lstop);
426 if(el) {
427 ASN_DEBUG("Found \"%s\" => %ld",
428 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700429 dec_value = el->nat_value;
430 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000431 lp = lstop - 1;
432 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000433 }
434 ASN_DEBUG("Unknown identifier for INTEGER");
435 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000436 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000437 case 0x3a: /* ':' */
438 if(state == ST_HEXCOLON) {
439 /* This colon is expected */
440 state = ST_HEXDIGIT1;
441 continue;
442 } else if(state == ST_DIGITS) {
443 /* The colon here means that we have
444 * decoded the first two hexadecimal
445 * places as a decimal value.
446 * Switch decoding mode. */
447 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000448 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700449 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000450 lp = lstart - 1;
451 continue;
452 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400453 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000454 break;
455 }
456 /* [A-Fa-f] */
457 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
458 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
459 switch(state) {
460 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700461 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000462 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700463 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
464 hex_value += 10;
465 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000466 state = ST_HEXDIGIT2;
467 continue;
468 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700469 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
470 hex_value += 10;
471 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000472 state = ST_HEXCOLON;
473 continue;
474 case ST_DIGITS:
475 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000476 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700477 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000478 lp = lstart - 1;
479 continue;
480 default:
481 break;
482 }
483 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000484 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000485
486 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700487 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400488 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700489 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000490 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000491 }
492
Lev Walkinc744a022006-09-15 18:33:25 +0000493 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700494 case ST_END_ENUM:
495 /* Got a complete and valid enumeration encoded as a tag. */
496 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000497 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700498 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700499 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700500 case ST_DIGITS_TRAILSPACE:
501 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700502 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700503 case ASN_STRTOX_OK:
Lev Walkine09f9f12012-09-02 23:06:35 -0700504 break;
Lev Walkin72ec9092017-07-05 05:49:12 -0700505 case ASN_STRTOX_ERROR_RANGE:
Lev Walkine09f9f12012-09-02 23:06:35 -0700506 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700507 case ASN_STRTOX_ERROR_INVAL:
508 case ASN_STRTOX_EXPECT_MORE:
509 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700510 return XPBD_BROKEN_ENCODING;
511 }
Lev Walkinc744a022006-09-15 18:33:25 +0000512 break;
513 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700514 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000515 st->buf[st->size] = 0; /* Just in case termination */
516 return XPBD_BODY_CONSUMED;
517 case ST_HEXDIGIT1:
518 case ST_HEXDIGIT2:
519 case ST_SKIPSPHEX:
520 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700521 case ST_LEADSPACE:
522 /* Content not found */
523 return XPBD_NOT_BODY_IGNORE;
524 case ST_WAITDIGITS:
525 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700526 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
527 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000528 }
Lev Walkind703ff42004-10-21 11:21:25 +0000529
Lev Walkine09f9f12012-09-02 23:06:35 -0700530 /*
531 * Convert the result of parsing of enumeration or a straight
532 * decimal value into a BER representation.
533 */
534 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000535 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000536
Lev Walkin0fab1a62005-03-09 22:19:25 +0000537 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000538}
539
540asn_dec_rval_t
541INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
542 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000543 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000544
545 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000546 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000547 buf_ptr, size, INTEGER__xer_body_decode);
548}
549
Lev Walkina9cc46e2004-09-22 16:06:28 +0000550asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000551INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000552 int ilevel, enum xer_encoder_flags_e flags,
553 asn_app_consume_bytes_f *cb, void *app_key) {
554 const INTEGER_t *st = (const INTEGER_t *)sptr;
555 asn_enc_rval_t er;
556
557 (void)ilevel;
558 (void)flags;
559
Lev Walkind500a962005-11-27 13:06:56 +0000560 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700561 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000562
Lev Walkine0b56e02005-02-25 12:10:27 +0000563 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700564 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000565
Lev Walkin7c1dc052016-03-14 03:08:15 -0700566 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000567}
568
Lev Walkind7703cf2012-09-03 01:45:03 -0700569#ifndef ASN_DISABLE_PER_SUPPORT
570
Lev Walkin59b176e2005-11-26 11:25:14 +0000571asn_dec_rval_t
572INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700573 const asn_per_constraints_t *constraints, void **sptr,
574 asn_per_data_t *pd) {
Lev Walkind62a6622017-08-22 02:31:02 -0700575 const asn_INTEGER_specifics_t *specs =
576 (const asn_INTEGER_specifics_t *)td->specifics;
577 asn_dec_rval_t rval = { RC_OK, 0 };
Lev Walkin59b176e2005-11-26 11:25:14 +0000578 INTEGER_t *st = (INTEGER_t *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -0700579 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000580 int repeat;
581
582 (void)opt_codec_ctx;
583
584 if(!st) {
585 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700586 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000587 }
588
589 if(!constraints) constraints = td->per_constraints;
590 ct = constraints ? &constraints->value : 0;
591
592 if(ct && ct->flags & APC_EXTENSIBLE) {
593 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700594 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000595 if(inext) ct = 0;
596 }
597
598 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000599 st->buf = 0;
600 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000601 if(ct) {
602 if(ct->flags & APC_SEMI_CONSTRAINED) {
603 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700604 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 st->size = 1;
606 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
607 size_t size = (ct->range_bits + 7) >> 3;
608 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700609 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000610 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000611 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000612 }
613
Lev Walkin6c527842014-02-09 04:34:54 -0800614 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000615 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800616 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000617 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
618 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800619 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700620 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800621
622 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700623 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800624 if(uper_get_constrained_whole_number(pd,
625 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700626 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800627 ASN_DEBUG("Got value %lu + low %ld",
628 uvalue, ct->lower_bound);
629 uvalue += ct->lower_bound;
630 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700631 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800632 } else {
Lev Walkin72ec9092017-07-05 05:49:12 -0700633 unsigned long svalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800634 if(uper_get_constrained_whole_number(pd,
635 &svalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700636 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800637 ASN_DEBUG("Got value %ld + low %ld",
638 svalue, ct->lower_bound);
639 svalue += ct->lower_bound;
640 if(asn_long2INTEGER(st, svalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700641 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800642 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000643 return rval;
644 }
645 } else {
646 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
647 }
648
649 /* X.691, #12.2.3, #12.2.4 */
650 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700651 ssize_t len = 0;
652 void *p = NULL;
653 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000654
655 /* Get the PER length */
656 len = uper_get_length(pd, -1, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700657 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000658
659 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700660 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000661 st->buf = (uint8_t *)p;
662
663 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700664 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000665 st->size += len;
666 } while(repeat);
667 st->buf[st->size] = 0; /* JIC */
668
669 /* #12.2.3 */
670 if(ct && ct->lower_bound) {
671 /*
672 * TODO: replace by in-place arithmetics.
673 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700674 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000675 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700676 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700677 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700678 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000679 }
680
681 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000682}
683
Lev Walkin523de9e2006-08-18 01:34:18 +0000684asn_enc_rval_t
685INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700686 const asn_per_constraints_t *constraints, void *sptr,
687 asn_per_outp_t *po) {
Lev Walkind62a6622017-08-22 02:31:02 -0700688 const asn_INTEGER_specifics_t *specs =
689 (const asn_INTEGER_specifics_t *)td->specifics;
690 asn_enc_rval_t er;
Lev Walkin523de9e2006-08-18 01:34:18 +0000691 INTEGER_t *st = (INTEGER_t *)sptr;
692 const uint8_t *buf;
693 const uint8_t *end;
Lev Walkin494fb702017-08-07 20:07:00 -0700694 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000695 long value = 0;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100696 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000697
Lev Walkin7c1dc052016-03-14 03:08:15 -0700698 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000699
700 if(!constraints) constraints = td->per_constraints;
701 ct = constraints ? &constraints->value : 0;
702
703 er.encoded = 0;
704
705 if(ct) {
706 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000707 if(specs && specs->field_unsigned) {
708 unsigned long uval;
709 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700710 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000711 /* Check proper range */
712 if(ct->flags & APC_SEMI_CONSTRAINED) {
713 if(uval < (unsigned long)ct->lower_bound)
714 inext = 1;
715 } else if(ct->range_bits >= 0) {
716 if(uval < (unsigned long)ct->lower_bound
717 || uval > (unsigned long)ct->upper_bound)
718 inext = 1;
719 }
720 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
721 uval, st->buf[0], st->size,
722 ct->lower_bound, ct->upper_bound,
723 inext ? "ext" : "fix");
724 value = uval;
725 } else {
726 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700727 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000728 /* Check proper range */
729 if(ct->flags & APC_SEMI_CONSTRAINED) {
730 if(value < ct->lower_bound)
731 inext = 1;
732 } else if(ct->range_bits >= 0) {
733 if(value < ct->lower_bound
734 || value > ct->upper_bound)
735 inext = 1;
736 }
737 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
738 value, st->buf[0], st->size,
739 ct->lower_bound, ct->upper_bound,
740 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000741 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000742 if(ct->flags & APC_EXTENSIBLE) {
743 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700744 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000745 if(inext) ct = 0;
746 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700747 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000748 }
749 }
750
751
Lev Walkin6c527842014-02-09 04:34:54 -0800752 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000753 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800754 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800755 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
756 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100757 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800758 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700759 ASN__ENCODE_FAILED;
760 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000761 }
762
763 if(ct && ct->lower_bound) {
764 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
765 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700766 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000767 }
768
769 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
770 ssize_t mayEncode = uper_put_length(po, end - buf);
771 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700772 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000773 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700774 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000775 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000776 }
777
Lev Walkin7c1dc052016-03-14 03:08:15 -0700778 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000779}
780
Lev Walkind7703cf2012-09-03 01:45:03 -0700781#endif /* ASN_DISABLE_PER_SUPPORT */
782
Lev Walkinf15320b2004-06-03 03:38:44 +0000783int
Lev Walkin72ec9092017-07-05 05:49:12 -0700784asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000785 uint8_t *b, *end;
786 size_t size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700787 intmax_t value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000788
789 /* Sanity checking */
790 if(!iptr || !iptr->buf || !lptr) {
791 errno = EINVAL;
792 return -1;
793 }
794
795 /* Cache the begin/end of the buffer */
796 b = iptr->buf; /* Start of the INTEGER buffer */
797 size = iptr->size;
798 end = b + size; /* Where to stop */
799
Lev Walkin72ec9092017-07-05 05:49:12 -0700800 if(size > sizeof(value)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000801 uint8_t *end1 = end - 1;
802 /*
803 * Slightly more advanced processing,
Lev Walkin72ec9092017-07-05 05:49:12 -0700804 * able to process INTEGERs with >sizeof(value) bytes
805 * when the actual value is small, e.g. for intmax_t == int32_t
806 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000807 */
808 /* Skip out the insignificant leading bytes */
809 for(; b < end1; b++) {
810 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700811 case 0x00: if((b[1] & 0x80) == 0) continue; break;
812 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000813 }
814 break;
815 }
816
817 size = end - b;
Lev Walkin72ec9092017-07-05 05:49:12 -0700818 if(size > sizeof(value)) {
819 /* Still cannot fit the sizeof(value) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000820 errno = ERANGE;
821 return -1;
822 }
823 }
824
825 /* Shortcut processing of a corner case */
826 if(end == b) {
827 *lptr = 0;
828 return 0;
829 }
830
831 /* Perform the sign initialization */
Lev Walkin72ec9092017-07-05 05:49:12 -0700832 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
833 if((*b >> 7)) value = -1; else value = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000834
835 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700836 for(; b < end; b++) {
837 value = (value << 8) | *b;
838 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000839
Lev Walkin72ec9092017-07-05 05:49:12 -0700840 *lptr = value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000841 return 0;
842}
Lev Walkind703ff42004-10-21 11:21:25 +0000843
Lev Walkin72ec9092017-07-05 05:49:12 -0700844/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000845int
Lev Walkin72ec9092017-07-05 05:49:12 -0700846asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000847 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700848 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000849 size_t size;
850
851 if(!iptr || !iptr->buf || !lptr) {
852 errno = EINVAL;
853 return -1;
854 }
855
856 b = iptr->buf;
857 size = iptr->size;
858 end = b + size;
859
860 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700861 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000862 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700863 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000864 errno = ERANGE;
865 return -1;
866 }
867 }
868
869 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700870 for(value = 0; b < end; b++)
871 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000872
Lev Walkin72ec9092017-07-05 05:49:12 -0700873 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000874 return 0;
875}
876
877int
Lev Walkin72ec9092017-07-05 05:49:12 -0700878asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
879 uint8_t *buf;
880 uint8_t *end;
881 uint8_t *b;
882 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000883
Lev Walkin72ec9092017-07-05 05:49:12 -0700884 if(value <= INTMAX_MAX) {
885 return asn_imax2INTEGER(st, value);
886 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000887
Lev Walkin72ec9092017-07-05 05:49:12 -0700888 buf = (uint8_t *)MALLOC(1 + sizeof(value));
889 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000890
Lev Walkin72ec9092017-07-05 05:49:12 -0700891 end = buf + (sizeof(value) + 1);
892 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
893 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
894 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000895
Lev Walkin72ec9092017-07-05 05:49:12 -0700896 if(st->buf) FREEMEM(st->buf);
897 st->buf = buf;
898 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000899
900 return 0;
901}
902
903int
Lev Walkin72ec9092017-07-05 05:49:12 -0700904asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000905 uint8_t *buf, *bp;
906 uint8_t *p;
907 uint8_t *pstart;
908 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000909 int littleEndian = 1; /* Run-time detection */
910 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000911
912 if(!st) {
913 errno = EINVAL;
914 return -1;
915 }
916
Lev Walkin8471cec2004-10-21 14:02:19 +0000917 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000918 if(!buf) return -1;
919
Lev Walkind7ad5612004-10-26 08:20:46 +0000920 if(*(char *)&littleEndian) {
921 pstart = (uint8_t *)&value + sizeof(value) - 1;
922 pend1 = (uint8_t *)&value;
923 add = -1;
924 } else {
925 pstart = (uint8_t *)&value;
926 pend1 = pstart + sizeof(value) - 1;
927 add = 1;
928 }
929
Lev Walkind703ff42004-10-21 11:21:25 +0000930 /*
931 * If the contents octet consists of more than one octet,
932 * then bits of the first octet and bit 8 of the second octet:
933 * a) shall not all be ones; and
934 * b) shall not all be zero.
935 */
Lev Walkin33700162004-10-26 09:03:31 +0000936 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000937 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000938 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000939 continue;
940 break;
Lev Walkin33700162004-10-26 09:03:31 +0000941 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000942 continue;
943 break;
944 }
945 break;
946 }
947 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700948 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000949 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000950
951 if(st->buf) FREEMEM(st->buf);
952 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000953 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000954
955 return 0;
956}
Lev Walkinb3751942012-09-02 19:36:47 -0700957
Lev Walkin72ec9092017-07-05 05:49:12 -0700958int
959asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
960 intmax_t v;
961 if(asn_INTEGER2imax(iptr, &v) == 0) {
962 if(v < LONG_MIN || v > LONG_MAX) {
963 errno = ERANGE;
964 return -1;
965 }
966 *l = v;
967 return 0;
968 } else {
969 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700970 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700971}
Lev Walkincad560a2013-03-16 07:00:58 -0700972
Lev Walkin72ec9092017-07-05 05:49:12 -0700973int
974asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
975 uintmax_t v;
976 if(asn_INTEGER2umax(iptr, &v) == 0) {
977 if(v > ULONG_MAX) {
978 errno = ERANGE;
979 return -1;
980 }
981 *l = v;
982 return 0;
983 } else {
984 return -1;
985 }
986}
987
988int
989asn_long2INTEGER(INTEGER_t *st, long value) {
990 return asn_imax2INTEGER(st, value);
991}
992
993int
994asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
995 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -0700996}
997
998/*
999 * Parse the number in the given string until the given *end position,
1000 * returning the position after the last parsed character back using the
1001 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -07001002 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -07001003 */
Lev Walkin72ec9092017-07-05 05:49:12 -07001004enum asn_strtox_result_e
1005asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001006 int sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001007 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -07001008
Lev Walkin72ec9092017-07-05 05:49:12 -07001009 const intmax_t upper_boundary = INTMAX_MAX / 10;
1010 intmax_t last_digit_max = INTMAX_MAX % 10;
Lev Walkine09f9f12012-09-02 23:06:35 -07001011
Lev Walkin72ec9092017-07-05 05:49:12 -07001012 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001013
1014 switch(*str) {
1015 case '-':
1016 last_digit_max++;
1017 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001018 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001019 case '+':
1020 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001021 if(str >= *end) {
1022 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001023 return ASN_STRTOX_EXPECT_MORE;
Lev Walkincad560a2013-03-16 07:00:58 -07001024 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001025 }
1026
Lev Walkin72ec9092017-07-05 05:49:12 -07001027 for(value = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001028 switch(*str) {
1029 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1030 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1031 int d = *str - '0';
Lev Walkin72ec9092017-07-05 05:49:12 -07001032 if(value < upper_boundary) {
1033 value = value * 10 + d;
1034 } else if(value == upper_boundary) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001035 if(d <= last_digit_max) {
1036 if(sign > 0) {
Lev Walkin72ec9092017-07-05 05:49:12 -07001037 value = value * 10 + d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001038 } else {
1039 sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001040 value = -value * 10 - d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001041 }
1042 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001043 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001044 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001045 }
1046 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001047 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001048 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001049 }
1050 }
1051 continue;
1052 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001053 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001054 *intp = sign * value;
1055 return ASN_STRTOX_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001056 }
1057 }
1058
Lev Walkincad560a2013-03-16 07:00:58 -07001059 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001060 *intp = sign * value;
1061 return ASN_STRTOX_OK;
1062}
1063
1064enum asn_strtox_result_e
1065asn_strtol_lim(const char *str, const char **end, long *lp) {
1066 intmax_t value;
1067 switch(asn_strtoimax_lim(str, end, &value)) {
1068 case ASN_STRTOX_ERROR_RANGE:
1069 return ASN_STRTOX_ERROR_RANGE;
1070 case ASN_STRTOX_ERROR_INVAL:
1071 return ASN_STRTOX_ERROR_INVAL;
1072 case ASN_STRTOX_EXPECT_MORE:
1073 return ASN_STRTOX_EXPECT_MORE;
1074 case ASN_STRTOX_OK:
1075 if(value >= LONG_MIN && value <= LONG_MAX) {
1076 *lp = value;
1077 return ASN_STRTOX_OK;
1078 } else {
1079 return ASN_STRTOX_ERROR_RANGE;
1080 }
1081 case ASN_STRTOX_EXTRA_DATA:
1082 if(value >= LONG_MIN && value <= LONG_MAX) {
1083 *lp = value;
1084 return ASN_STRTOX_EXTRA_DATA;
1085 } else {
1086 return ASN_STRTOX_ERROR_RANGE;
1087 }
1088 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001089
1090 assert(!"Unreachable");
1091 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001092}
1093
Lev Walkincd2f48e2017-08-10 02:14:59 -07001094int
1095INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1096 const void *bptr) {
1097 const INTEGER_t *a = aptr;
1098 const INTEGER_t *b = bptr;
1099
1100 (void)td;
1101
1102 if(a && b) {
1103 if(a->size && b->size) {
1104 int sign_a = (a->buf[0] & 0x80) ? -1 : 1;
1105 int sign_b = (b->buf[0] & 0x80) ? -1 : 1;
1106
1107 if(sign_a < sign_b) return -1;
1108 if(sign_a > sign_b) return 1;
1109
1110 /* The shortest integer wins, unless comparing negatives */
1111 if(a->size < b->size) {
1112 return -1 * sign_a;
1113 } else if(a->size > b->size) {
1114 return 1 * sign_b;
1115 }
1116
1117 return sign_a * memcmp(a->buf, b->buf, a->size);
1118 } else if(a->size) {
1119 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1120 return (1) * sign;
1121 } else if(b->size) {
1122 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1123 return (-1) * sign;
1124 } else {
1125 return 0;
1126 }
1127 } else if(!a && !b) {
1128 return 0;
1129 } else if(!a) {
1130 return -1;
1131 } else {
1132 return 1;
1133 }
1134
1135}
1136