blob: 1a00064fa269acd54771d8f6107ad1e30eb1f5cd [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,
Lev Walkincd2f48e2017-08-10 02:14:59 -070022 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 Walkinc2350112005-03-29 17:19:53 +0000112static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop);
Lev Walkine0b56e02005-02-25 12:10:27 +0000113
Lev Walkinf15320b2004-06-03 03:38:44 +0000114/*
115 * INTEGER specific human-readable output.
116 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000117static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700118INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000119 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000120 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000121 uint8_t *buf = st->buf;
122 uint8_t *buf_end = st->buf + st->size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700123 intmax_t value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000124 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 char *p;
126 int ret;
127
Lev Walkin97f8edc2013-03-28 04:38:41 -0700128 if(specs && specs->field_unsigned)
Lev Walkin72ec9092017-07-05 05:49:12 -0700129 ret = asn_INTEGER2umax(st, (uintmax_t *)&value);
Lev Walkin97f8edc2013-03-28 04:38:41 -0700130 else
Lev Walkin72ec9092017-07-05 05:49:12 -0700131 ret = asn_INTEGER2imax(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000132
Lev Walkinf15320b2004-06-03 03:38:44 +0000133 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700134 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000135 const asn_INTEGER_enum_map_t *el;
136 size_t scrsize;
137 char *scr;
138
Lev Walkin97f8edc2013-03-28 04:38:41 -0700139 el = (value >= 0 || !specs || !specs->field_unsigned)
140 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000141 if(el) {
142 scrsize = el->enum_len + 32;
143 scr = (char *)alloca(scrsize);
144 if(plainOrXER == 0)
145 ret = snprintf(scr, scrsize,
Lev Walkin72ec9092017-07-05 05:49:12 -0700146 "%" PRIdMAX " (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000147 else
148 ret = snprintf(scr, scrsize,
149 "<%s/>", el->enum_name);
150 } else if(plainOrXER && specs && specs->strict_enumeration) {
151 ASN_DEBUG("ASN.1 forbids dealing with "
152 "unknown value of ENUMERATED type");
153 errno = EPERM;
154 return -1;
155 } else {
156 scrsize = sizeof(scratch);
157 scr = scratch;
Lev Walkin72ec9092017-07-05 05:49:12 -0700158 ret = snprintf(
159 scr, scrsize,
160 (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX,
161 value);
162 }
Lev Walkine0b56e02005-02-25 12:10:27 +0000163 assert(ret > 0 && (size_t)ret < scrsize);
164 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
165 } else if(plainOrXER && specs && specs->strict_enumeration) {
166 /*
167 * Here and earlier, we cannot encode the ENUMERATED values
168 * if there is no corresponding identifier.
169 */
170 ASN_DEBUG("ASN.1 forbids dealing with "
171 "unknown value of ENUMERATED type");
172 errno = EPERM;
173 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 }
175
176 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000177 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000178 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700179 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000180 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000182 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000184 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 p = scratch;
186 }
187 *p++ = h2c[*buf >> 4];
188 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000189 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 }
Lev Walkindb13f512004-07-19 17:30:25 +0000191 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000192 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000193
Lev Walkina9cc46e2004-09-22 16:06:28 +0000194 wrote += p - scratch;
195 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
196}
197
198/*
199 * INTEGER specific human-readable output.
200 */
201int
Lev Walkin5e033762004-09-29 13:26:15 +0000202INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000203 asn_app_consume_bytes_f *cb, void *app_key) {
204 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000205 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000206
207 (void)td;
208 (void)ilevel;
209
Lev Walkind500a962005-11-27 13:06:56 +0000210 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000211 ret = cb("<absent>", 8, app_key);
212 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000213 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000214
Lev Walkin8e8078a2004-09-26 13:10:40 +0000215 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000216}
217
Lev Walkine0b56e02005-02-25 12:10:27 +0000218struct e2v_key {
219 const char *start;
220 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700221 const asn_INTEGER_enum_map_t *vemap;
222 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000223};
224static int
225INTEGER__compar_enum2value(const void *kp, const void *am) {
226 const struct e2v_key *key = (const struct e2v_key *)kp;
227 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
228 const char *ptr, *end, *name;
229
230 /* Remap the element (sort by different criterion) */
231 el = key->vemap + key->evmap[el - key->vemap];
232
233 /* Compare strings */
234 for(ptr = key->start, end = key->stop, name = el->enum_name;
235 ptr < end; ptr++, name++) {
236 if(*ptr != *name)
237 return *(const unsigned char *)ptr
238 - *(const unsigned char *)name;
239 }
240 return name[0] ? -1 : 0;
241}
242
243static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000244INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700245 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000246 int count = specs ? specs->map_count : 0;
247 struct e2v_key key;
248 const char *lp;
249
250 if(!count) return NULL;
251
252 /* Guaranteed: assert(lstart < lstop); */
253 /* Figure out the tag name */
254 for(lstart++, lp = lstart; lp < lstop; lp++) {
255 switch(*lp) {
256 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
257 case 0x2f: /* '/' */ case 0x3e: /* '>' */
258 break;
259 default:
260 continue;
261 }
262 break;
263 }
264 if(lp == lstop) return NULL; /* No tag found */
265 lstop = lp;
266
267 key.start = lstart;
268 key.stop = lstop;
269 key.vemap = specs->value2enum;
270 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000271 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
272 specs->value2enum, count, sizeof(specs->value2enum[0]),
273 INTEGER__compar_enum2value);
274 if(el_found) {
275 /* Remap enum2value into value2enum */
276 el_found = key.vemap + key.evmap[el_found - key.vemap];
277 }
278 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000279}
280
281static int
282INTEGER__compar_value2enum(const void *kp, const void *am) {
283 long a = *(const long *)kp;
284 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
285 long b = el->nat_value;
286 if(a < b) return -1;
287 else if(a == b) return 0;
288 else return 1;
289}
290
Lev Walkinc2350112005-03-29 17:19:53 +0000291const asn_INTEGER_enum_map_t *
292INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000293 int count = specs ? specs->map_count : 0;
294 if(!count) return 0;
295 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
296 count, sizeof(specs->value2enum[0]),
297 INTEGER__compar_value2enum);
298}
299
Lev Walkinc744a022006-09-15 18:33:25 +0000300static int
301INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
302 void *p = MALLOC(min_size + 1);
303 if(p) {
304 void *b = st->buf;
305 st->size = 0;
306 st->buf = p;
307 FREEMEM(b);
308 return 0;
309 } else {
310 return -1;
311 }
312}
313
Lev Walkind703ff42004-10-21 11:21:25 +0000314/*
315 * Decode the chunk of XML text encoding INTEGER.
316 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000317static enum xer_pbd_rval
318INTEGER__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 +0000319 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkin72ec9092017-07-05 05:49:12 -0700320 intmax_t dec_value;
321 intmax_t hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000322 const char *lp;
323 const char *lstart = (const char *)chunk_buf;
324 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000325 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700326 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000327 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000328 ST_WAITDIGITS,
329 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700330 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000331 ST_HEXDIGIT1,
332 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700333 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000334 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700335 ST_END_ENUM,
336 ST_UNEXPECTED
337 } state = ST_LEADSPACE;
338 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
339 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000340
Lev Walkinc744a022006-09-15 18:33:25 +0000341 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000342 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
343 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000344
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000345 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
346 return XPBD_SYSTEM_FAILURE;
347
Lev Walkind703ff42004-10-21 11:21:25 +0000348 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000349 * We may have received a tag here. It will be processed inline.
350 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000351 */
Lev Walkinb3751942012-09-02 19:36:47 -0700352 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000353 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000354 switch(lv) {
355 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000356 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700357 case ST_LEADSPACE:
358 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700359 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000360 case ST_SKIPSPHEX:
361 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700362 case ST_DIGITS:
363 dec_value_end = lp;
364 state = ST_DIGITS_TRAILSPACE;
365 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700366 case ST_HEXCOLON:
367 state = ST_HEXDIGITS_TRAILSPACE;
368 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000369 default:
370 break;
371 }
Lev Walkind703ff42004-10-21 11:21:25 +0000372 break;
373 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700374 if(state == ST_LEADSPACE) {
375 dec_value = 0;
376 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000377 state = ST_WAITDIGITS;
378 continue;
379 }
380 break;
381 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700382 if(state == ST_LEADSPACE) {
383 dec_value = 0;
384 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000385 state = ST_WAITDIGITS;
386 continue;
387 }
388 break;
389 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
390 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000391 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700392 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000393 case ST_SKIPSPHEX: /* Fall through */
394 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700395 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000396 state = ST_HEXDIGIT2;
397 continue;
398 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700399 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000400 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700401 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000402 continue;
403 case ST_HEXCOLON:
404 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700405 case ST_LEADSPACE:
406 dec_value = 0;
407 dec_value_start = lp;
408 /* FALL THROUGH */
409 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000410 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700411 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700412 default:
413 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000414 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700415 break;
416 case 0x3c: /* '<', start of XML encoded enumeration */
417 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000418 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000419 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000420 (asn_INTEGER_specifics_t *)
421 td->specifics, lstart, lstop);
422 if(el) {
423 ASN_DEBUG("Found \"%s\" => %ld",
424 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700425 dec_value = el->nat_value;
426 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000427 lp = lstop - 1;
428 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000429 }
430 ASN_DEBUG("Unknown identifier for INTEGER");
431 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000432 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000433 case 0x3a: /* ':' */
434 if(state == ST_HEXCOLON) {
435 /* This colon is expected */
436 state = ST_HEXDIGIT1;
437 continue;
438 } else if(state == ST_DIGITS) {
439 /* The colon here means that we have
440 * decoded the first two hexadecimal
441 * places as a decimal value.
442 * Switch decoding mode. */
443 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000444 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700445 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000446 lp = lstart - 1;
447 continue;
448 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400449 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000450 break;
451 }
452 /* [A-Fa-f] */
453 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
454 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
455 switch(state) {
456 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700457 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000458 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700459 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
460 hex_value += 10;
461 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000462 state = ST_HEXDIGIT2;
463 continue;
464 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700465 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
466 hex_value += 10;
467 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000468 state = ST_HEXCOLON;
469 continue;
470 case ST_DIGITS:
471 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000472 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700473 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000474 lp = lstart - 1;
475 continue;
476 default:
477 break;
478 }
479 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000480 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000481
482 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700483 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400484 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700485 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000486 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000487 }
488
Lev Walkinc744a022006-09-15 18:33:25 +0000489 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700490 case ST_END_ENUM:
491 /* Got a complete and valid enumeration encoded as a tag. */
492 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000493 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700494 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700495 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700496 case ST_DIGITS_TRAILSPACE:
497 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700498 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700499 case ASN_STRTOX_OK:
Lev Walkine09f9f12012-09-02 23:06:35 -0700500 break;
Lev Walkin72ec9092017-07-05 05:49:12 -0700501 case ASN_STRTOX_ERROR_RANGE:
Lev Walkine09f9f12012-09-02 23:06:35 -0700502 return XPBD_DECODER_LIMIT;
Lev Walkin72ec9092017-07-05 05:49:12 -0700503 case ASN_STRTOX_ERROR_INVAL:
504 case ASN_STRTOX_EXPECT_MORE:
505 case ASN_STRTOX_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700506 return XPBD_BROKEN_ENCODING;
507 }
Lev Walkinc744a022006-09-15 18:33:25 +0000508 break;
509 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700510 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000511 st->buf[st->size] = 0; /* Just in case termination */
512 return XPBD_BODY_CONSUMED;
513 case ST_HEXDIGIT1:
514 case ST_HEXDIGIT2:
515 case ST_SKIPSPHEX:
516 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700517 case ST_LEADSPACE:
518 /* Content not found */
519 return XPBD_NOT_BODY_IGNORE;
520 case ST_WAITDIGITS:
521 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700522 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
523 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000524 }
Lev Walkind703ff42004-10-21 11:21:25 +0000525
Lev Walkine09f9f12012-09-02 23:06:35 -0700526 /*
527 * Convert the result of parsing of enumeration or a straight
528 * decimal value into a BER representation.
529 */
530 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000531 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000532
Lev Walkin0fab1a62005-03-09 22:19:25 +0000533 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000534}
535
536asn_dec_rval_t
537INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
538 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000539 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000540
541 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000542 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000543 buf_ptr, size, INTEGER__xer_body_decode);
544}
545
Lev Walkina9cc46e2004-09-22 16:06:28 +0000546asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000547INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000548 int ilevel, enum xer_encoder_flags_e flags,
549 asn_app_consume_bytes_f *cb, void *app_key) {
550 const INTEGER_t *st = (const INTEGER_t *)sptr;
551 asn_enc_rval_t er;
552
553 (void)ilevel;
554 (void)flags;
555
Lev Walkind500a962005-11-27 13:06:56 +0000556 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700557 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000558
Lev Walkine0b56e02005-02-25 12:10:27 +0000559 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700560 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000561
Lev Walkin7c1dc052016-03-14 03:08:15 -0700562 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000563}
564
Lev Walkind7703cf2012-09-03 01:45:03 -0700565#ifndef ASN_DISABLE_PER_SUPPORT
566
Lev Walkin59b176e2005-11-26 11:25:14 +0000567asn_dec_rval_t
568INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700569 const asn_per_constraints_t *constraints, void **sptr,
570 asn_per_data_t *pd) {
571 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000572 asn_dec_rval_t rval = { RC_OK, 0 };
573 INTEGER_t *st = (INTEGER_t *)*sptr;
Lev Walkin494fb702017-08-07 20:07:00 -0700574 const asn_per_constraint_t *ct;
Lev Walkin59b176e2005-11-26 11:25:14 +0000575 int repeat;
576
577 (void)opt_codec_ctx;
578
579 if(!st) {
580 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700581 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000582 }
583
584 if(!constraints) constraints = td->per_constraints;
585 ct = constraints ? &constraints->value : 0;
586
587 if(ct && ct->flags & APC_EXTENSIBLE) {
588 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700589 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000590 if(inext) ct = 0;
591 }
592
593 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000594 st->buf = 0;
595 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000596 if(ct) {
597 if(ct->flags & APC_SEMI_CONSTRAINED) {
598 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700599 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000600 st->size = 1;
601 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
602 size_t size = (ct->range_bits + 7) >> 3;
603 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700604 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000606 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000607 }
608
Lev Walkin6c527842014-02-09 04:34:54 -0800609 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000610 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800611 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000612 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
613 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800614 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700615 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800616
617 if(specs && specs->field_unsigned) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700618 unsigned long uvalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800619 if(uper_get_constrained_whole_number(pd,
620 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700621 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800622 ASN_DEBUG("Got value %lu + low %ld",
623 uvalue, ct->lower_bound);
624 uvalue += ct->lower_bound;
625 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700626 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800627 } else {
Lev Walkin72ec9092017-07-05 05:49:12 -0700628 unsigned long svalue = 0;
Lev Walkin6c527842014-02-09 04:34:54 -0800629 if(uper_get_constrained_whole_number(pd,
630 &svalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700631 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800632 ASN_DEBUG("Got value %ld + low %ld",
633 svalue, ct->lower_bound);
634 svalue += ct->lower_bound;
635 if(asn_long2INTEGER(st, svalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700636 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800637 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000638 return rval;
639 }
640 } else {
641 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
642 }
643
644 /* X.691, #12.2.3, #12.2.4 */
645 do {
Lev Walkin72ec9092017-07-05 05:49:12 -0700646 ssize_t len = 0;
647 void *p = NULL;
648 int ret = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000649
650 /* Get the PER length */
651 len = uper_get_length(pd, -1, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700652 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000653
654 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700655 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000656 st->buf = (uint8_t *)p;
657
658 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700659 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000660 st->size += len;
661 } while(repeat);
662 st->buf[st->size] = 0; /* JIC */
663
664 /* #12.2.3 */
665 if(ct && ct->lower_bound) {
666 /*
667 * TODO: replace by in-place arithmetics.
668 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700669 long value = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000670 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700671 ASN__DECODE_FAILED;
Lev Walkin72ec9092017-07-05 05:49:12 -0700672 if(asn_imax2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700673 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000674 }
675
676 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000677}
678
Lev Walkin523de9e2006-08-18 01:34:18 +0000679asn_enc_rval_t
680INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700681 const asn_per_constraints_t *constraints, void *sptr,
682 asn_per_outp_t *po) {
683 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000684 asn_enc_rval_t er;
685 INTEGER_t *st = (INTEGER_t *)sptr;
686 const uint8_t *buf;
687 const uint8_t *end;
Lev Walkin494fb702017-08-07 20:07:00 -0700688 const asn_per_constraint_t *ct;
Lev Walkin523de9e2006-08-18 01:34:18 +0000689 long value = 0;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100690 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000691
Lev Walkin7c1dc052016-03-14 03:08:15 -0700692 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000693
694 if(!constraints) constraints = td->per_constraints;
695 ct = constraints ? &constraints->value : 0;
696
697 er.encoded = 0;
698
699 if(ct) {
700 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000701 if(specs && specs->field_unsigned) {
702 unsigned long uval;
703 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700704 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000705 /* Check proper range */
706 if(ct->flags & APC_SEMI_CONSTRAINED) {
707 if(uval < (unsigned long)ct->lower_bound)
708 inext = 1;
709 } else if(ct->range_bits >= 0) {
710 if(uval < (unsigned long)ct->lower_bound
711 || uval > (unsigned long)ct->upper_bound)
712 inext = 1;
713 }
714 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
715 uval, st->buf[0], st->size,
716 ct->lower_bound, ct->upper_bound,
717 inext ? "ext" : "fix");
718 value = uval;
719 } else {
720 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700721 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000722 /* Check proper range */
723 if(ct->flags & APC_SEMI_CONSTRAINED) {
724 if(value < ct->lower_bound)
725 inext = 1;
726 } else if(ct->range_bits >= 0) {
727 if(value < ct->lower_bound
728 || value > ct->upper_bound)
729 inext = 1;
730 }
731 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
732 value, st->buf[0], st->size,
733 ct->lower_bound, ct->upper_bound,
734 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000735 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000736 if(ct->flags & APC_EXTENSIBLE) {
737 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700738 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000739 if(inext) ct = 0;
740 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700741 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000742 }
743 }
744
745
Lev Walkin6c527842014-02-09 04:34:54 -0800746 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000747 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800748 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800749 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
750 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100751 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800752 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700753 ASN__ENCODE_FAILED;
754 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000755 }
756
757 if(ct && ct->lower_bound) {
758 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
759 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700760 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000761 }
762
763 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
764 ssize_t mayEncode = uper_put_length(po, end - buf);
765 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700766 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000767 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700768 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000769 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000770 }
771
Lev Walkin7c1dc052016-03-14 03:08:15 -0700772 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000773}
774
Lev Walkind7703cf2012-09-03 01:45:03 -0700775#endif /* ASN_DISABLE_PER_SUPPORT */
776
Lev Walkinf15320b2004-06-03 03:38:44 +0000777int
Lev Walkin72ec9092017-07-05 05:49:12 -0700778asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000779 uint8_t *b, *end;
780 size_t size;
Lev Walkin72ec9092017-07-05 05:49:12 -0700781 intmax_t value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000782
783 /* Sanity checking */
784 if(!iptr || !iptr->buf || !lptr) {
785 errno = EINVAL;
786 return -1;
787 }
788
789 /* Cache the begin/end of the buffer */
790 b = iptr->buf; /* Start of the INTEGER buffer */
791 size = iptr->size;
792 end = b + size; /* Where to stop */
793
Lev Walkin72ec9092017-07-05 05:49:12 -0700794 if(size > sizeof(value)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000795 uint8_t *end1 = end - 1;
796 /*
797 * Slightly more advanced processing,
Lev Walkin72ec9092017-07-05 05:49:12 -0700798 * able to process INTEGERs with >sizeof(value) bytes
799 * when the actual value is small, e.g. for intmax_t == int32_t
800 * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t)
Lev Walkinf15320b2004-06-03 03:38:44 +0000801 */
802 /* Skip out the insignificant leading bytes */
803 for(; b < end1; b++) {
804 switch(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700805 case 0x00: if((b[1] & 0x80) == 0) continue; break;
806 case 0xff: if((b[1] & 0x80) != 0) continue; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000807 }
808 break;
809 }
810
811 size = end - b;
Lev Walkin72ec9092017-07-05 05:49:12 -0700812 if(size > sizeof(value)) {
813 /* Still cannot fit the sizeof(value) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000814 errno = ERANGE;
815 return -1;
816 }
817 }
818
819 /* Shortcut processing of a corner case */
820 if(end == b) {
821 *lptr = 0;
822 return 0;
823 }
824
825 /* Perform the sign initialization */
Lev Walkin72ec9092017-07-05 05:49:12 -0700826 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
827 if((*b >> 7)) value = -1; else value = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000828
829 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700830 for(; b < end; b++) {
831 value = (value << 8) | *b;
832 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000833
Lev Walkin72ec9092017-07-05 05:49:12 -0700834 *lptr = value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000835 return 0;
836}
Lev Walkind703ff42004-10-21 11:21:25 +0000837
Lev Walkin72ec9092017-07-05 05:49:12 -0700838/* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */
Lev Walkind703ff42004-10-21 11:21:25 +0000839int
Lev Walkin72ec9092017-07-05 05:49:12 -0700840asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000841 uint8_t *b, *end;
Lev Walkin72ec9092017-07-05 05:49:12 -0700842 uintmax_t value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000843 size_t size;
844
845 if(!iptr || !iptr->buf || !lptr) {
846 errno = EINVAL;
847 return -1;
848 }
849
850 b = iptr->buf;
851 size = iptr->size;
852 end = b + size;
853
854 /* If all extra leading bytes are zeroes, ignore them */
Lev Walkin72ec9092017-07-05 05:49:12 -0700855 for(; size > sizeof(value); b++, size--) {
Lev Walkin5c879db2007-11-06 06:23:31 +0000856 if(*b) {
Lev Walkin72ec9092017-07-05 05:49:12 -0700857 /* Value won't fit into uintmax_t */
Lev Walkin5c879db2007-11-06 06:23:31 +0000858 errno = ERANGE;
859 return -1;
860 }
861 }
862
863 /* Conversion engine */
Lev Walkin72ec9092017-07-05 05:49:12 -0700864 for(value = 0; b < end; b++)
865 value = (value << 8) | *b;
Lev Walkin5c879db2007-11-06 06:23:31 +0000866
Lev Walkin72ec9092017-07-05 05:49:12 -0700867 *lptr = value;
Lev Walkin5c879db2007-11-06 06:23:31 +0000868 return 0;
869}
870
871int
Lev Walkin72ec9092017-07-05 05:49:12 -0700872asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) {
873 uint8_t *buf;
874 uint8_t *end;
875 uint8_t *b;
876 int shr;
Lev Walkin5c879db2007-11-06 06:23:31 +0000877
Lev Walkin72ec9092017-07-05 05:49:12 -0700878 if(value <= INTMAX_MAX) {
879 return asn_imax2INTEGER(st, value);
880 }
Lev Walkin5c879db2007-11-06 06:23:31 +0000881
Lev Walkin72ec9092017-07-05 05:49:12 -0700882 buf = (uint8_t *)MALLOC(1 + sizeof(value));
883 if(!buf) return -1;
Lev Walkin5c879db2007-11-06 06:23:31 +0000884
Lev Walkin72ec9092017-07-05 05:49:12 -0700885 end = buf + (sizeof(value) + 1);
886 buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */
887 for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++)
888 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000889
Lev Walkin72ec9092017-07-05 05:49:12 -0700890 if(st->buf) FREEMEM(st->buf);
891 st->buf = buf;
892 st->size = 1 + sizeof(value);
Lev Walkin5c879db2007-11-06 06:23:31 +0000893
894 return 0;
895}
896
897int
Lev Walkin72ec9092017-07-05 05:49:12 -0700898asn_imax2INTEGER(INTEGER_t *st, intmax_t value) {
Lev Walkind703ff42004-10-21 11:21:25 +0000899 uint8_t *buf, *bp;
900 uint8_t *p;
901 uint8_t *pstart;
902 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000903 int littleEndian = 1; /* Run-time detection */
904 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000905
906 if(!st) {
907 errno = EINVAL;
908 return -1;
909 }
910
Lev Walkin8471cec2004-10-21 14:02:19 +0000911 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000912 if(!buf) return -1;
913
Lev Walkind7ad5612004-10-26 08:20:46 +0000914 if(*(char *)&littleEndian) {
915 pstart = (uint8_t *)&value + sizeof(value) - 1;
916 pend1 = (uint8_t *)&value;
917 add = -1;
918 } else {
919 pstart = (uint8_t *)&value;
920 pend1 = pstart + sizeof(value) - 1;
921 add = 1;
922 }
923
Lev Walkind703ff42004-10-21 11:21:25 +0000924 /*
925 * If the contents octet consists of more than one octet,
926 * then bits of the first octet and bit 8 of the second octet:
927 * a) shall not all be ones; and
928 * b) shall not all be zero.
929 */
Lev Walkin33700162004-10-26 09:03:31 +0000930 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000931 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000932 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000933 continue;
934 break;
Lev Walkin33700162004-10-26 09:03:31 +0000935 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000936 continue;
937 break;
938 }
939 break;
940 }
941 /* Copy the integer body */
Lev Walkin7e0ab882017-06-28 08:47:47 -0700942 for(bp = buf, pend1 += add; p != pend1; p += add)
Lev Walkin33700162004-10-26 09:03:31 +0000943 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000944
945 if(st->buf) FREEMEM(st->buf);
946 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000947 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000948
949 return 0;
950}
Lev Walkinb3751942012-09-02 19:36:47 -0700951
Lev Walkin72ec9092017-07-05 05:49:12 -0700952int
953asn_INTEGER2long(const INTEGER_t *iptr, long *l) {
954 intmax_t v;
955 if(asn_INTEGER2imax(iptr, &v) == 0) {
956 if(v < LONG_MIN || v > LONG_MAX) {
957 errno = ERANGE;
958 return -1;
959 }
960 *l = v;
961 return 0;
962 } else {
963 return -1;
Lev Walkincad560a2013-03-16 07:00:58 -0700964 }
Lev Walkin72ec9092017-07-05 05:49:12 -0700965}
Lev Walkincad560a2013-03-16 07:00:58 -0700966
Lev Walkin72ec9092017-07-05 05:49:12 -0700967int
968asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) {
969 uintmax_t v;
970 if(asn_INTEGER2umax(iptr, &v) == 0) {
971 if(v > ULONG_MAX) {
972 errno = ERANGE;
973 return -1;
974 }
975 *l = v;
976 return 0;
977 } else {
978 return -1;
979 }
980}
981
982int
983asn_long2INTEGER(INTEGER_t *st, long value) {
984 return asn_imax2INTEGER(st, value);
985}
986
987int
988asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
989 return asn_imax2INTEGER(st, value);
Lev Walkincad560a2013-03-16 07:00:58 -0700990}
991
992/*
993 * Parse the number in the given string until the given *end position,
994 * returning the position after the last parsed character back using the
995 * same (*end) pointer.
Lev Walkin72ec9092017-07-05 05:49:12 -0700996 * WARNING: This behavior is different from the standard strtol/strtoimax(3).
Lev Walkincad560a2013-03-16 07:00:58 -0700997 */
Lev Walkin72ec9092017-07-05 05:49:12 -0700998enum asn_strtox_result_e
999asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001000 int sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001001 intmax_t value;
Lev Walkinb3751942012-09-02 19:36:47 -07001002
Lev Walkin72ec9092017-07-05 05:49:12 -07001003 const intmax_t upper_boundary = INTMAX_MAX / 10;
1004 intmax_t last_digit_max = INTMAX_MAX % 10;
Lev Walkine09f9f12012-09-02 23:06:35 -07001005
Lev Walkin72ec9092017-07-05 05:49:12 -07001006 if(str >= *end) return ASN_STRTOX_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001007
1008 switch(*str) {
1009 case '-':
1010 last_digit_max++;
1011 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -04001012 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -07001013 case '+':
1014 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001015 if(str >= *end) {
1016 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001017 return ASN_STRTOX_EXPECT_MORE;
Lev Walkincad560a2013-03-16 07:00:58 -07001018 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001019 }
1020
Lev Walkin72ec9092017-07-05 05:49:12 -07001021 for(value = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001022 switch(*str) {
1023 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1024 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1025 int d = *str - '0';
Lev Walkin72ec9092017-07-05 05:49:12 -07001026 if(value < upper_boundary) {
1027 value = value * 10 + d;
1028 } else if(value == upper_boundary) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001029 if(d <= last_digit_max) {
1030 if(sign > 0) {
Lev Walkin72ec9092017-07-05 05:49:12 -07001031 value = value * 10 + d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001032 } else {
1033 sign = 1;
Lev Walkin72ec9092017-07-05 05:49:12 -07001034 value = -value * 10 - d;
Lev Walkine09f9f12012-09-02 23:06:35 -07001035 }
1036 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001037 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001038 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001039 }
1040 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001041 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001042 return ASN_STRTOX_ERROR_RANGE;
Lev Walkine09f9f12012-09-02 23:06:35 -07001043 }
1044 }
1045 continue;
1046 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001047 *end = str;
Lev Walkin72ec9092017-07-05 05:49:12 -07001048 *intp = sign * value;
1049 return ASN_STRTOX_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001050 }
1051 }
1052
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_OK;
1056}
1057
1058enum asn_strtox_result_e
1059asn_strtol_lim(const char *str, const char **end, long *lp) {
1060 intmax_t value;
1061 switch(asn_strtoimax_lim(str, end, &value)) {
1062 case ASN_STRTOX_ERROR_RANGE:
1063 return ASN_STRTOX_ERROR_RANGE;
1064 case ASN_STRTOX_ERROR_INVAL:
1065 return ASN_STRTOX_ERROR_INVAL;
1066 case ASN_STRTOX_EXPECT_MORE:
1067 return ASN_STRTOX_EXPECT_MORE;
1068 case ASN_STRTOX_OK:
1069 if(value >= LONG_MIN && value <= LONG_MAX) {
1070 *lp = value;
1071 return ASN_STRTOX_OK;
1072 } else {
1073 return ASN_STRTOX_ERROR_RANGE;
1074 }
1075 case ASN_STRTOX_EXTRA_DATA:
1076 if(value >= LONG_MIN && value <= LONG_MAX) {
1077 *lp = value;
1078 return ASN_STRTOX_EXTRA_DATA;
1079 } else {
1080 return ASN_STRTOX_ERROR_RANGE;
1081 }
1082 }
Lev Walkin5d9e3c52017-07-05 16:25:24 -07001083
1084 assert(!"Unreachable");
1085 return ASN_STRTOX_ERROR_INVAL;
Lev Walkinb3751942012-09-02 19:36:47 -07001086}
1087
Lev Walkincd2f48e2017-08-10 02:14:59 -07001088int
1089INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1090 const void *bptr) {
1091 const INTEGER_t *a = aptr;
1092 const INTEGER_t *b = bptr;
1093
1094 (void)td;
1095
1096 if(a && b) {
1097 if(a->size && b->size) {
1098 int sign_a = (a->buf[0] & 0x80) ? -1 : 1;
1099 int sign_b = (b->buf[0] & 0x80) ? -1 : 1;
1100
1101 if(sign_a < sign_b) return -1;
1102 if(sign_a > sign_b) return 1;
1103
1104 /* The shortest integer wins, unless comparing negatives */
1105 if(a->size < b->size) {
1106 return -1 * sign_a;
1107 } else if(a->size > b->size) {
1108 return 1 * sign_b;
1109 }
1110
1111 return sign_a * memcmp(a->buf, b->buf, a->size);
1112 } else if(a->size) {
1113 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1114 return (1) * sign;
1115 } else if(b->size) {
1116 int sign = (a->buf[0] & 0x80) ? -1 : 1;
1117 return (-1) * sign;
1118 } else {
1119 return 0;
1120 }
1121 } else if(!a && !b) {
1122 return 0;
1123 } else if(!a) {
1124 return -1;
1125 } else {
1126 return 1;
1127 }
1128
1129}
1130