blob: 7b3d10a2ff01326feb14e289e9e033de876f6a4b [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 Walkin8e8078a2004-09-26 13:10:40 +000020 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000021 INTEGER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000022 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000023 ber_decode_primitive,
Lev Walkinf15320b2004-06-03 03:38:44 +000024 INTEGER_encode_der,
Lev Walkind703ff42004-10-21 11:21:25 +000025 INTEGER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000026 INTEGER_encode_xer,
Lev Walkind7703cf2012-09-03 01:45:03 -070027#ifdef ASN_DISABLE_PER_SUPPORT
28 0,
29 0,
30#else
Lev Walkin59b176e2005-11-26 11:25:14 +000031 INTEGER_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000032 INTEGER_encode_uper, /* Unaligned PER encoder */
Harald Welte498c9712015-08-30 16:33:07 +020033 INTEGER_decode_aper,
34 INTEGER_encode_aper,
Lev Walkind7703cf2012-09-03 01:45:03 -070035#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000036 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000037 asn_DEF_INTEGER_tags,
38 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
39 asn_DEF_INTEGER_tags, /* Same as above */
40 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000041 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000042 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000043 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000044};
45
46/*
Lev Walkinf15320b2004-06-03 03:38:44 +000047 * Encode INTEGER type using DER.
48 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000049asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000050INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000051 int tag_mode, ber_tlv_tag_t tag,
52 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000053 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000054
55 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000056 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000057
58 /*
59 * Canonicalize integer in the buffer.
60 * (Remove too long sign extension, remove some first 0x00 bytes)
61 */
62 if(st->buf) {
63 uint8_t *buf = st->buf;
64 uint8_t *end1 = buf + st->size - 1;
65 int shift;
66
67 /* Compute the number of superfluous leading bytes */
68 for(; buf < end1; buf++) {
69 /*
70 * If the contents octets of an integer value encoding
71 * consist of more than one octet, then the bits of the
72 * first octet and bit 8 of the second octet:
73 * a) shall not all be ones; and
74 * b) shall not all be zero.
75 */
76 switch(*buf) {
77 case 0x00: if((buf[1] & 0x80) == 0)
78 continue;
79 break;
80 case 0xff: if((buf[1] & 0x80))
81 continue;
82 break;
83 }
84 break;
85 }
86
87 /* Remove leading superfluous bytes from the integer */
88 shift = buf - st->buf;
89 if(shift) {
90 uint8_t *nb = st->buf;
91 uint8_t *end;
92
93 st->size -= shift; /* New size, minus bad bytes */
94 end = nb + st->size;
95
96 for(; nb < end; nb++, buf++)
97 *nb = *buf;
98 }
99
100 } /* if(1) */
101
Lev Walkin8e8078a2004-09-26 13:10:40 +0000102 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000103}
104
Lev Walkinc2350112005-03-29 17:19:53 +0000105static 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 +0000106
Lev Walkinf15320b2004-06-03 03:38:44 +0000107/*
108 * INTEGER specific human-readable output.
109 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000110static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700111INTEGER__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 +0000112 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000113 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 uint8_t *buf = st->buf;
115 uint8_t *buf_end = st->buf + st->size;
Lev Walkin97f8edc2013-03-28 04:38:41 -0700116 signed long value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000117 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 char *p;
119 int ret;
120
Lev Walkin97f8edc2013-03-28 04:38:41 -0700121 if(specs && specs->field_unsigned)
122 ret = asn_INTEGER2ulong(st, (unsigned long *)&value);
123 else
124 ret = asn_INTEGER2long(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000125
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700127 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000128 const asn_INTEGER_enum_map_t *el;
129 size_t scrsize;
130 char *scr;
131
Lev Walkin97f8edc2013-03-28 04:38:41 -0700132 el = (value >= 0 || !specs || !specs->field_unsigned)
133 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000134 if(el) {
135 scrsize = el->enum_len + 32;
136 scr = (char *)alloca(scrsize);
137 if(plainOrXER == 0)
138 ret = snprintf(scr, scrsize,
Lev Walkin97f8edc2013-03-28 04:38:41 -0700139 "%ld (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000140 else
141 ret = snprintf(scr, scrsize,
142 "<%s/>", el->enum_name);
143 } else if(plainOrXER && specs && specs->strict_enumeration) {
144 ASN_DEBUG("ASN.1 forbids dealing with "
145 "unknown value of ENUMERATED type");
146 errno = EPERM;
147 return -1;
148 } else {
149 scrsize = sizeof(scratch);
150 scr = scratch;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000151 ret = snprintf(scr, scrsize,
152 (specs && specs->field_unsigned)
Lev Walkin97f8edc2013-03-28 04:38:41 -0700153 ?"%lu":"%ld", value);
Lev Walkine0b56e02005-02-25 12:10:27 +0000154 }
155 assert(ret > 0 && (size_t)ret < scrsize);
156 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
157 } else if(plainOrXER && specs && specs->strict_enumeration) {
158 /*
159 * Here and earlier, we cannot encode the ENUMERATED values
160 * if there is no corresponding identifier.
161 */
162 ASN_DEBUG("ASN.1 forbids dealing with "
163 "unknown value of ENUMERATED type");
164 errno = EPERM;
165 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 }
167
168 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000169 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700171 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000172 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000174 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000176 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 p = scratch;
178 }
179 *p++ = h2c[*buf >> 4];
180 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000181 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 }
Lev Walkindb13f512004-07-19 17:30:25 +0000183 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000184 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000185
Lev Walkina9cc46e2004-09-22 16:06:28 +0000186 wrote += p - scratch;
187 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
188}
189
190/*
191 * INTEGER specific human-readable output.
192 */
193int
Lev Walkin5e033762004-09-29 13:26:15 +0000194INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195 asn_app_consume_bytes_f *cb, void *app_key) {
196 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000197 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000198
199 (void)td;
200 (void)ilevel;
201
Lev Walkind500a962005-11-27 13:06:56 +0000202 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000203 ret = cb("<absent>", 8, app_key);
204 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000205 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000206
Lev Walkin8e8078a2004-09-26 13:10:40 +0000207 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208}
209
Lev Walkine0b56e02005-02-25 12:10:27 +0000210struct e2v_key {
211 const char *start;
212 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700213 const asn_INTEGER_enum_map_t *vemap;
214 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000215};
216static int
217INTEGER__compar_enum2value(const void *kp, const void *am) {
218 const struct e2v_key *key = (const struct e2v_key *)kp;
219 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
220 const char *ptr, *end, *name;
221
222 /* Remap the element (sort by different criterion) */
223 el = key->vemap + key->evmap[el - key->vemap];
224
225 /* Compare strings */
226 for(ptr = key->start, end = key->stop, name = el->enum_name;
227 ptr < end; ptr++, name++) {
228 if(*ptr != *name)
229 return *(const unsigned char *)ptr
230 - *(const unsigned char *)name;
231 }
232 return name[0] ? -1 : 0;
233}
234
235static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000236INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700237 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000238 int count = specs ? specs->map_count : 0;
239 struct e2v_key key;
240 const char *lp;
241
242 if(!count) return NULL;
243
244 /* Guaranteed: assert(lstart < lstop); */
245 /* Figure out the tag name */
246 for(lstart++, lp = lstart; lp < lstop; lp++) {
247 switch(*lp) {
248 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
249 case 0x2f: /* '/' */ case 0x3e: /* '>' */
250 break;
251 default:
252 continue;
253 }
254 break;
255 }
256 if(lp == lstop) return NULL; /* No tag found */
257 lstop = lp;
258
259 key.start = lstart;
260 key.stop = lstop;
261 key.vemap = specs->value2enum;
262 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000263 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
264 specs->value2enum, count, sizeof(specs->value2enum[0]),
265 INTEGER__compar_enum2value);
266 if(el_found) {
267 /* Remap enum2value into value2enum */
268 el_found = key.vemap + key.evmap[el_found - key.vemap];
269 }
270 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000271}
272
273static int
274INTEGER__compar_value2enum(const void *kp, const void *am) {
275 long a = *(const long *)kp;
276 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
277 long b = el->nat_value;
278 if(a < b) return -1;
279 else if(a == b) return 0;
280 else return 1;
281}
282
Lev Walkinc2350112005-03-29 17:19:53 +0000283const asn_INTEGER_enum_map_t *
284INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000285 int count = specs ? specs->map_count : 0;
286 if(!count) return 0;
287 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
288 count, sizeof(specs->value2enum[0]),
289 INTEGER__compar_value2enum);
290}
291
Lev Walkinc744a022006-09-15 18:33:25 +0000292static int
293INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
294 void *p = MALLOC(min_size + 1);
295 if(p) {
296 void *b = st->buf;
297 st->size = 0;
298 st->buf = p;
299 FREEMEM(b);
300 return 0;
301 } else {
302 return -1;
303 }
304}
305
Lev Walkind703ff42004-10-21 11:21:25 +0000306/*
307 * Decode the chunk of XML text encoding INTEGER.
308 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000309static enum xer_pbd_rval
310INTEGER__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 +0000311 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinb3751942012-09-02 19:36:47 -0700312 long dec_value;
Lev Walkin0ff71292013-03-25 18:51:51 -0700313 long hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000314 const char *lp;
315 const char *lstart = (const char *)chunk_buf;
316 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000317 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700318 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000319 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000320 ST_WAITDIGITS,
321 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700322 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000323 ST_HEXDIGIT1,
324 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700325 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000326 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700327 ST_END_ENUM,
328 ST_UNEXPECTED
329 } state = ST_LEADSPACE;
330 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
331 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000332
Lev Walkinc744a022006-09-15 18:33:25 +0000333 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000334 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
335 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000336
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000337 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
338 return XPBD_SYSTEM_FAILURE;
339
Lev Walkind703ff42004-10-21 11:21:25 +0000340 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000341 * We may have received a tag here. It will be processed inline.
342 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000343 */
Lev Walkinb3751942012-09-02 19:36:47 -0700344 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000345 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000346 switch(lv) {
347 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000348 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700349 case ST_LEADSPACE:
350 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700351 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000352 case ST_SKIPSPHEX:
353 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700354 case ST_DIGITS:
355 dec_value_end = lp;
356 state = ST_DIGITS_TRAILSPACE;
357 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700358 case ST_HEXCOLON:
359 state = ST_HEXDIGITS_TRAILSPACE;
360 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000361 default:
362 break;
363 }
Lev Walkind703ff42004-10-21 11:21:25 +0000364 break;
365 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700366 if(state == ST_LEADSPACE) {
367 dec_value = 0;
368 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000369 state = ST_WAITDIGITS;
370 continue;
371 }
372 break;
373 case 0x2b: /* '+' */
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 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
382 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000383 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700384 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000385 case ST_SKIPSPHEX: /* Fall through */
386 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700387 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000388 state = ST_HEXDIGIT2;
389 continue;
390 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700391 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000392 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700393 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000394 continue;
395 case ST_HEXCOLON:
396 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700397 case ST_LEADSPACE:
398 dec_value = 0;
399 dec_value_start = lp;
400 /* FALL THROUGH */
401 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000402 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700403 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700404 default:
405 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000406 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700407 break;
408 case 0x3c: /* '<', start of XML encoded enumeration */
409 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000410 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000411 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000412 (asn_INTEGER_specifics_t *)
413 td->specifics, lstart, lstop);
414 if(el) {
Harald Welte498c9712015-08-30 16:33:07 +0200415 ASN_DEBUG("Found \"%s\" => %lld",
Lev Walkine0b56e02005-02-25 12:10:27 +0000416 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700417 dec_value = el->nat_value;
418 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000419 lp = lstop - 1;
420 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000421 }
422 ASN_DEBUG("Unknown identifier for INTEGER");
423 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000424 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000425 case 0x3a: /* ':' */
426 if(state == ST_HEXCOLON) {
427 /* This colon is expected */
428 state = ST_HEXDIGIT1;
429 continue;
430 } else if(state == ST_DIGITS) {
431 /* The colon here means that we have
432 * decoded the first two hexadecimal
433 * places as a decimal value.
434 * Switch decoding mode. */
435 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000436 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700437 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000438 lp = lstart - 1;
439 continue;
440 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400441 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000442 break;
443 }
444 /* [A-Fa-f] */
445 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
446 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
447 switch(state) {
448 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700449 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000450 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700451 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
452 hex_value += 10;
453 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000454 state = ST_HEXDIGIT2;
455 continue;
456 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700457 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
458 hex_value += 10;
459 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000460 state = ST_HEXCOLON;
461 continue;
462 case ST_DIGITS:
463 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000464 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700465 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000466 lp = lstart - 1;
467 continue;
468 default:
469 break;
470 }
471 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000472 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000473
474 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700475 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400476 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700477 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000478 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000479 }
480
Lev Walkinc744a022006-09-15 18:33:25 +0000481 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700482 case ST_END_ENUM:
483 /* Got a complete and valid enumeration encoded as a tag. */
484 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000485 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700486 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700487 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700488 case ST_DIGITS_TRAILSPACE:
489 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700490 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700491 case ASN_STRTOL_OK:
492 break;
493 case ASN_STRTOL_ERROR_RANGE:
494 return XPBD_DECODER_LIMIT;
495 case ASN_STRTOL_ERROR_INVAL:
Lev Walkincad560a2013-03-16 07:00:58 -0700496 case ASN_STRTOL_EXPECT_MORE:
497 case ASN_STRTOL_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700498 return XPBD_BROKEN_ENCODING;
499 }
Lev Walkinc744a022006-09-15 18:33:25 +0000500 break;
501 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700502 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000503 st->buf[st->size] = 0; /* Just in case termination */
504 return XPBD_BODY_CONSUMED;
505 case ST_HEXDIGIT1:
506 case ST_HEXDIGIT2:
507 case ST_SKIPSPHEX:
508 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700509 case ST_LEADSPACE:
510 /* Content not found */
511 return XPBD_NOT_BODY_IGNORE;
512 case ST_WAITDIGITS:
513 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700514 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
515 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000516 }
Lev Walkind703ff42004-10-21 11:21:25 +0000517
Lev Walkine09f9f12012-09-02 23:06:35 -0700518 /*
519 * Convert the result of parsing of enumeration or a straight
520 * decimal value into a BER representation.
521 */
522 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000523 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000524
Lev Walkin0fab1a62005-03-09 22:19:25 +0000525 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000526}
527
528asn_dec_rval_t
529INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
530 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000531 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000532
533 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000534 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000535 buf_ptr, size, INTEGER__xer_body_decode);
536}
537
Lev Walkina9cc46e2004-09-22 16:06:28 +0000538asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000539INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000540 int ilevel, enum xer_encoder_flags_e flags,
541 asn_app_consume_bytes_f *cb, void *app_key) {
542 const INTEGER_t *st = (const INTEGER_t *)sptr;
543 asn_enc_rval_t er;
544
545 (void)ilevel;
546 (void)flags;
547
Lev Walkind500a962005-11-27 13:06:56 +0000548 if(!st || !st->buf)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000549 _ASN_ENCODE_FAILED;
550
Lev Walkine0b56e02005-02-25 12:10:27 +0000551 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000552 if(er.encoded < 0) _ASN_ENCODE_FAILED;
553
Lev Walkin59b176e2005-11-26 11:25:14 +0000554 _ASN_ENCODED_OK(er);
555}
556
Lev Walkind7703cf2012-09-03 01:45:03 -0700557#ifndef ASN_DISABLE_PER_SUPPORT
558
Lev Walkin59b176e2005-11-26 11:25:14 +0000559asn_dec_rval_t
560INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
561 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000562 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000563 asn_dec_rval_t rval = { RC_OK, 0 };
564 INTEGER_t *st = (INTEGER_t *)*sptr;
565 asn_per_constraint_t *ct;
566 int repeat;
567
568 (void)opt_codec_ctx;
569
570 if(!st) {
571 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
572 if(!st) _ASN_DECODE_FAILED;
573 }
574
575 if(!constraints) constraints = td->per_constraints;
576 ct = constraints ? &constraints->value : 0;
577
578 if(ct && ct->flags & APC_EXTENSIBLE) {
579 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000580 if(inext < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000581 if(inext) ct = 0;
582 }
583
584 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000585 st->buf = 0;
586 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000587 if(ct) {
588 if(ct->flags & APC_SEMI_CONSTRAINED) {
589 st->buf = (uint8_t *)CALLOC(1, 2);
590 if(!st->buf) _ASN_DECODE_FAILED;
591 st->size = 1;
592 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
593 size_t size = (ct->range_bits + 7) >> 3;
594 st->buf = (uint8_t *)MALLOC(1 + size + 1);
595 if(!st->buf) _ASN_DECODE_FAILED;
596 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000597 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000598 }
599
Lev Walkin6c527842014-02-09 04:34:54 -0800600 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000601 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800602 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000603 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
604 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800605 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin59b176e2005-11-26 11:25:14 +0000606 _ASN_DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800607
608 if(specs && specs->field_unsigned) {
609 unsigned long uvalue;
610 if(uper_get_constrained_whole_number(pd,
611 &uvalue, ct->range_bits))
612 _ASN_DECODE_STARVED;
Harald Welte498c9712015-08-30 16:33:07 +0200613 ASN_DEBUG("Got value %lu + low %lld",
Lev Walkin6c527842014-02-09 04:34:54 -0800614 uvalue, ct->lower_bound);
615 uvalue += ct->lower_bound;
616 if(asn_ulong2INTEGER(st, uvalue))
617 _ASN_DECODE_FAILED;
618 } else {
619 unsigned long svalue;
620 if(uper_get_constrained_whole_number(pd,
621 &svalue, ct->range_bits))
622 _ASN_DECODE_STARVED;
623 ASN_DEBUG("Got value %ld + low %ld",
624 svalue, ct->lower_bound);
625 svalue += ct->lower_bound;
626 if(asn_long2INTEGER(st, svalue))
627 _ASN_DECODE_FAILED;
628 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000629 return rval;
630 }
631 } else {
632 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
633 }
634
635 /* X.691, #12.2.3, #12.2.4 */
636 do {
637 ssize_t len;
638 void *p;
639 int ret;
640
641 /* Get the PER length */
642 len = uper_get_length(pd, -1, &repeat);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000643 if(len < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000644
645 p = REALLOC(st->buf, st->size + len + 1);
646 if(!p) _ASN_DECODE_FAILED;
647 st->buf = (uint8_t *)p;
648
649 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000650 if(ret < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000651 st->size += len;
652 } while(repeat);
653 st->buf[st->size] = 0; /* JIC */
654
655 /* #12.2.3 */
656 if(ct && ct->lower_bound) {
657 /*
658 * TODO: replace by in-place arithmetics.
659 */
660 long value;
661 if(asn_INTEGER2long(st, &value))
662 _ASN_DECODE_FAILED;
663 if(asn_long2INTEGER(st, value + ct->lower_bound))
664 _ASN_DECODE_FAILED;
665 }
666
667 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000668}
669
Harald Welte498c9712015-08-30 16:33:07 +0200670asn_dec_rval_t
671INTEGER_decode_aper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
672 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
673 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
674 asn_dec_rval_t rval = { RC_OK, 0 };
675 INTEGER_t *st = (INTEGER_t *)*sptr;
676 asn_per_constraint_t *ct;
677 int repeat;
678
679 (void)opt_codec_ctx;
680
681 if(!st) {
682 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
683 if(!st) _ASN_DECODE_FAILED;
684 }
685
686 if(!constraints) constraints = td->per_constraints;
687 ct = constraints ? &constraints->value : 0;
688
689 if(ct && ct->flags & APC_EXTENSIBLE) {
690 int inext = per_get_few_bits(pd, 1);
691 if(inext < 0) _ASN_DECODE_STARVED;
692 if(inext) ct = 0;
693 }
694
695 FREEMEM(st->buf);
696 st->buf = 0;
697 st->size = 0;
698 if(ct) {
699 if(ct->flags & APC_SEMI_CONSTRAINED) {
700 st->buf = (uint8_t *)CALLOC(1, 2);
701 if(!st->buf) _ASN_DECODE_FAILED;
702 st->size = 1;
703 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
704 size_t size = (ct->range_bits + 7) >> 3;
705 st->buf = (uint8_t *)MALLOC(1 + size + 1);
706 if(!st->buf) _ASN_DECODE_FAILED;
707 st->size = size;
708 }
709 }
710
711 /* X.691, #12.2.2 */
712 if(ct && ct->flags != APC_UNCONSTRAINED) {
713 /* #10.5.6 */
714 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
715 if(ct->range_bits >= 0) {
716 if (ct->range_bits > 16) {
717 int max_range_bytes = (ct->range_bits >> 3) + 1;
718 int length, i;
719 int64_t value = 0;
720
721 for (i = 0; i < max_range_bytes; i++) {
722 int upper = 1 << (i + 1);
723 if (upper > max_range_bytes)
724 break;
725 }
726 if ((length = per_get_few_bits(pd, i + 1)) < 0)
727 _ASN_DECODE_STARVED;
728 if (aper_get_align(pd) != 0)
729 _ASN_DECODE_STARVED;
730 ASN_DEBUG("Got length %d", length + 1);
731 for (i = 0; i < length + 1; i++) {
732 int buf = per_get_few_bits(pd, 8);
733 if (buf < 0)
734 _ASN_DECODE_STARVED;
735 value += (((int64_t)buf) << (8 * i));
736 }
737
738 if((specs && specs->field_unsigned)
739 ? asn_uint642INTEGER(st, value)
740 : asn_int642INTEGER(st, value))
741 _ASN_DECODE_FAILED;
742 ASN_DEBUG("Got value %lld + low %lld",
743 value, ct->lower_bound);
744 value += ct->lower_bound;
745 } else {
746 long value = 0;
747 if (ct->range_bits < 8) {
748 value = per_get_few_bits(pd, ct->range_bits);
749 if(value < 0) _ASN_DECODE_STARVED;
750 } else if (ct->range_bits == 8) {
751 if (aper_get_align(pd) < 0)
752 _ASN_DECODE_FAILED;
753 value = per_get_few_bits(pd, ct->range_bits);
754 if(value < 0) _ASN_DECODE_STARVED;
755 } else {
756 /* Align */
757 if (aper_get_align(pd) < 0)
758 _ASN_DECODE_FAILED;
759 value = per_get_few_bits(pd, 16);
760 if(value < 0) _ASN_DECODE_STARVED;
761 }
762 if((specs && specs->field_unsigned)
763 ? asn_ulong2INTEGER(st, value)
764 : asn_long2INTEGER(st, value))
765 _ASN_DECODE_FAILED;
766 ASN_DEBUG("Got value %ld + low %lld",
767 value, ct->lower_bound);
768 value += ct->lower_bound;
769 }
770 return rval;
771 } else {
772 _ASN_DECODE_FAILED;
773 }
774 } else {
775 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
776 }
777
778 /* X.691, #12.2.3, #12.2.4 */
779 do {
780 ssize_t len;
781 void *p;
782 int ret;
783
784 /* Get the PER length */
785 len = aper_get_length(pd, -1, -1, &repeat);
786 if(len < 0) _ASN_DECODE_STARVED;
787
788 p = REALLOC(st->buf, st->size + len + 1);
789 if(!p) _ASN_DECODE_FAILED;
790 st->buf = (uint8_t *)p;
791
792 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
793 if(ret < 0) _ASN_DECODE_STARVED;
794 st->size += len;
795 } while(repeat);
796 st->buf[st->size] = 0; /* JIC */
797
798 /* #12.2.3 */
799 if(ct && ct->lower_bound) {
800 /*
801 * TODO: replace by in-place arithmetics.
802 */
803 long value;
804 if(asn_INTEGER2long(st, &value))
805 _ASN_DECODE_FAILED;
806 if(asn_long2INTEGER(st, value + ct->lower_bound))
807 _ASN_DECODE_FAILED;
808 }
809
810 return rval;
811}
812
Lev Walkin523de9e2006-08-18 01:34:18 +0000813asn_enc_rval_t
814INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
815 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000816 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000817 asn_enc_rval_t er;
818 INTEGER_t *st = (INTEGER_t *)sptr;
819 const uint8_t *buf;
820 const uint8_t *end;
821 asn_per_constraint_t *ct;
822 long value = 0;
823
824 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
825
826 if(!constraints) constraints = td->per_constraints;
827 ct = constraints ? &constraints->value : 0;
828
829 er.encoded = 0;
830
831 if(ct) {
832 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000833 if(specs && specs->field_unsigned) {
834 unsigned long uval;
835 if(asn_INTEGER2ulong(st, &uval))
836 _ASN_ENCODE_FAILED;
837 /* Check proper range */
838 if(ct->flags & APC_SEMI_CONSTRAINED) {
839 if(uval < (unsigned long)ct->lower_bound)
840 inext = 1;
841 } else if(ct->range_bits >= 0) {
842 if(uval < (unsigned long)ct->lower_bound
843 || uval > (unsigned long)ct->upper_bound)
844 inext = 1;
845 }
Harald Welte498c9712015-08-30 16:33:07 +0200846 ASN_DEBUG("Value %lu (%02x/%d) lb %llu ub %llu %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000847 uval, st->buf[0], st->size,
848 ct->lower_bound, ct->upper_bound,
849 inext ? "ext" : "fix");
850 value = uval;
851 } else {
852 if(asn_INTEGER2long(st, &value))
853 _ASN_ENCODE_FAILED;
854 /* Check proper range */
855 if(ct->flags & APC_SEMI_CONSTRAINED) {
856 if(value < ct->lower_bound)
857 inext = 1;
858 } else if(ct->range_bits >= 0) {
859 if(value < ct->lower_bound
860 || value > ct->upper_bound)
861 inext = 1;
862 }
Harald Welte498c9712015-08-30 16:33:07 +0200863 ASN_DEBUG("Value %ld (%02x/%d) lb %lld ub %lld %s",
Lev Walkin8bb57a22007-12-03 13:41:36 +0000864 value, st->buf[0], st->size,
865 ct->lower_bound, ct->upper_bound,
866 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000867 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000868 if(ct->flags & APC_EXTENSIBLE) {
869 if(per_put_few_bits(po, inext, 1))
870 _ASN_ENCODE_FAILED;
871 if(inext) ct = 0;
872 } else if(inext) {
873 _ASN_ENCODE_FAILED;
874 }
875 }
876
877
Lev Walkin6c527842014-02-09 04:34:54 -0800878 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000879 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800880 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800881 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
882 value, value - ct->lower_bound, ct->range_bits);
883 unsigned long v = value - ct->lower_bound;
884 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin6c527842014-02-09 04:34:54 -0800885 _ASN_ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000886 _ASN_ENCODED_OK(er);
887 }
888
889 if(ct && ct->lower_bound) {
Harald Welte498c9712015-08-30 16:33:07 +0200890 ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound);
Lev Walkin523de9e2006-08-18 01:34:18 +0000891 /* TODO: adjust lower bound */
892 _ASN_ENCODE_FAILED;
893 }
894
895 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
896 ssize_t mayEncode = uper_put_length(po, end - buf);
897 if(mayEncode < 0)
898 _ASN_ENCODE_FAILED;
899 if(per_put_many_bits(po, buf, 8 * mayEncode))
900 _ASN_ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000901 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000902 }
903
904 _ASN_ENCODED_OK(er);
905}
906
Lev Walkind7703cf2012-09-03 01:45:03 -0700907#endif /* ASN_DISABLE_PER_SUPPORT */
908
Harald Welte498c9712015-08-30 16:33:07 +0200909asn_enc_rval_t
910INTEGER_encode_aper(asn_TYPE_descriptor_t *td,
911 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
912 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
913 asn_enc_rval_t er;
914 INTEGER_t *st = (INTEGER_t *)sptr;
915 const uint8_t *buf;
916 const uint8_t *end;
917 asn_per_constraint_t *ct;
918 int64_t value = 0;
919
920 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
921
922 if(!constraints) constraints = td->per_constraints;
923 ct = constraints ? &constraints->value : 0;
924
925 er.encoded = 0;
926
927 if(ct) {
928 int inext = 0;
929 if(specs && specs->field_unsigned) {
930 uint64_t uval;
931 if(asn_INTEGER2uint64(st, &uval))
932 _ASN_ENCODE_FAILED;
933 /* Check proper range */
934 if(ct->flags & APC_SEMI_CONSTRAINED) {
935 if(uval < (unsigned long long)ct->lower_bound)
936 inext = 1;
937 } else if(ct->range_bits >= 0) {
938 if(uval < (unsigned long long)ct->lower_bound
939 || uval > (unsigned long long)ct->upper_bound)
940 inext = 1;
941 }
942 ASN_DEBUG("Value %llu (%02x/%d) lb %llu ub %llu %s",
943 uval, st->buf[0], st->size,
944 ct->lower_bound, ct->upper_bound,
945 inext ? "ext" : "fix");
946 value = uval;
947 } else {
948 if(asn_INTEGER2int64(st, &value)) _ASN_ENCODE_FAILED;
949 /* Check proper range */
950 if(ct->flags & APC_SEMI_CONSTRAINED) {
951 if(value < ct->lower_bound)
952 inext = 1;
953 } else if(ct->range_bits >= 0) {
954 if(value < ct->lower_bound
955 || value > ct->upper_bound)
956 inext = 1;
957 }
958 ASN_DEBUG("Value %lld (%02x/%d) lb %lld ub %lld %s",
959 value, st->buf[0], st->size,
960 ct->lower_bound, ct->upper_bound,
961 inext ? "ext" : "fix");
962 }
963 if(ct->flags & APC_EXTENSIBLE) {
964 if(per_put_few_bits(po, inext, 1))
965 _ASN_ENCODE_FAILED;
966 if(inext) ct = 0;
967 } else if(inext) {
968 _ASN_ENCODE_FAILED;
969 }
970 }
971
972 /* X.691, #12.2.2 */
973 if(ct && ct->range_bits >= 0) {
974 /* #10.5.6 */
975 ASN_DEBUG("Encoding integer with range %d bits",
976 ct->range_bits);
977
978 /* #12 <= 8 -> alignment ? */
979 if (ct->range_bits < 8) {
980 if(per_put_few_bits(po, 0x00 | value, ct->range_bits))
981 _ASN_ENCODE_FAILED;
982 } else if (ct->range_bits == 8) {
983 if(aper_put_align(po) < 0)
984 _ASN_ENCODE_FAILED;
985 if(per_put_few_bits(po, 0x00 | value, ct->range_bits))
986 _ASN_ENCODE_FAILED;
987 } else if (ct->range_bits <= 16) {
988 // Consume the bytes to align on octet
989 if(aper_put_align(po) < 0)
990 _ASN_ENCODE_FAILED;
991 if(per_put_few_bits(po, 0x0000 | value,
992 16))
993 _ASN_ENCODE_FAILED;
994 } else {
995 /* TODO: extend to >64 bits */
996 int64_t v = value;
997 int i;
998
999 /* Putting length - 1 in the minimum number of bits ex: 5 = 3bits */
1000 if (per_put_few_bits(po, st->size - 1, (ct->range_bits >> 3)-1))
1001 _ASN_ENCODE_FAILED;
1002
1003 // Consume the bits to align on octet
1004 if (aper_put_align(po) < 0)
1005 _ASN_ENCODE_FAILED;
1006 /* Put the value */
1007 for (i = 0; i < st->size; i++) {
1008 if(per_put_few_bits(po, (v >> (8 * (st->size - i - 1))) & 0xff, 8)) _ASN_ENCODE_FAILED;
1009 }
1010 }
1011 _ASN_ENCODED_OK(er);
1012 }
1013
1014 if(ct && ct->lower_bound) {
1015 ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound);
1016 /* TODO: adjust lower bound */
1017 _ASN_ENCODE_FAILED;
1018 }
1019
1020 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
1021 ssize_t mayEncode = aper_put_length(po, -1, end - buf);
1022 if(mayEncode < 0)
1023 _ASN_ENCODE_FAILED;
1024 if(per_put_many_bits(po, buf, 8 * mayEncode))
1025 _ASN_ENCODE_FAILED;
1026 buf += mayEncode;
1027 }
1028
1029 _ASN_ENCODED_OK(er);
1030}
1031
Lev Walkinf15320b2004-06-03 03:38:44 +00001032int
Lev Walkin5e033762004-09-29 13:26:15 +00001033asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +00001034 uint8_t *b, *end;
1035 size_t size;
1036 long l;
1037
1038 /* Sanity checking */
1039 if(!iptr || !iptr->buf || !lptr) {
1040 errno = EINVAL;
1041 return -1;
1042 }
1043
1044 /* Cache the begin/end of the buffer */
1045 b = iptr->buf; /* Start of the INTEGER buffer */
1046 size = iptr->size;
1047 end = b + size; /* Where to stop */
1048
1049 if(size > sizeof(long)) {
1050 uint8_t *end1 = end - 1;
1051 /*
1052 * Slightly more advanced processing,
1053 * able to >sizeof(long) bytes,
1054 * when the actual value is small
1055 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1056 */
1057 /* Skip out the insignificant leading bytes */
1058 for(; b < end1; b++) {
1059 switch(*b) {
1060 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1061 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1062 }
1063 break;
1064 }
1065
1066 size = end - b;
1067 if(size > sizeof(long)) {
1068 /* Still cannot fit the long */
1069 errno = ERANGE;
1070 return -1;
1071 }
1072 }
1073
1074 /* Shortcut processing of a corner case */
1075 if(end == b) {
1076 *lptr = 0;
1077 return 0;
1078 }
1079
1080 /* Perform the sign initialization */
1081 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1082 if((*b >> 7)) l = -1; else l = 0;
1083
1084 /* Conversion engine */
1085 for(; b < end; b++)
1086 l = (l << 8) | *b;
1087
1088 *lptr = l;
1089 return 0;
1090}
Lev Walkind703ff42004-10-21 11:21:25 +00001091
1092int
Harald Welte498c9712015-08-30 16:33:07 +02001093asn_INTEGER2int64(const INTEGER_t *iptr, int64_t *lptr) {
1094 uint8_t *b, *end;
1095 size_t size;
1096 int64_t l;
1097
1098 /* Sanity checking */
1099 if(!iptr || !iptr->buf || !lptr) {
1100 errno = EINVAL;
1101 return -1;
1102 }
1103
1104 /* Cache the begin/end of the buffer */
1105 b = iptr->buf; /* Start of the INTEGER buffer */
1106 size = iptr->size;
1107 end = b + size; /* Where to stop */
1108
1109 if(size > sizeof(int64_t)) {
1110 uint8_t *end1 = end - 1;
1111 /*
1112 * Slightly more advanced processing,
1113 * able to >sizeof(int64_t) bytes,
1114 * when the actual value is small
1115 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1116 */
1117 /* Skip out the insignificant leading bytes */
1118 for(; b < end1; b++) {
1119 switch(*b) {
1120 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1121 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1122 }
1123 break;
1124 }
1125
1126 size = end - b;
1127 if(size > sizeof(int64_t)) {
1128 /* Still cannot fit the int64_t */
1129 errno = ERANGE;
1130 return -1;
1131 }
1132 }
1133
1134 /* Shortcut processing of a corner case */
1135 if(end == b) {
1136 *lptr = 0;
1137 return 0;
1138 }
1139
1140 /* Perform the sign initialization */
1141 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1142 if((*b >> 7)) l = -1; else l = 0;
1143
1144 /* Conversion engine */
1145 for(; b < end; b++)
1146 l = (l << 8) | *b;
1147
1148 *lptr = l;
1149 return 0;
1150}
1151
1152int
Lev Walkin5c879db2007-11-06 06:23:31 +00001153asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
1154 uint8_t *b, *end;
1155 unsigned long l;
1156 size_t size;
1157
1158 if(!iptr || !iptr->buf || !lptr) {
1159 errno = EINVAL;
1160 return -1;
1161 }
1162
1163 b = iptr->buf;
1164 size = iptr->size;
1165 end = b + size;
1166
1167 /* If all extra leading bytes are zeroes, ignore them */
1168 for(; size > sizeof(unsigned long); b++, size--) {
1169 if(*b) {
1170 /* Value won't fit unsigned long */
1171 errno = ERANGE;
1172 return -1;
1173 }
1174 }
1175
1176 /* Conversion engine */
1177 for(l = 0; b < end; b++)
1178 l = (l << 8) | *b;
1179
1180 *lptr = l;
1181 return 0;
1182}
1183
1184int
Harald Welte498c9712015-08-30 16:33:07 +02001185asn_INTEGER2uint64(const INTEGER_t *iptr, uint64_t *lptr) {
1186 uint8_t *b, *end;
1187 uint64_t l;
1188 size_t size;
1189
1190 if(!iptr || !iptr->buf || !lptr) {
1191 errno = EINVAL;
1192 return -1;
1193 }
1194
1195 b = iptr->buf;
1196 size = iptr->size;
1197 end = b + size;
1198
1199 /* If all extra leading bytes are zeroes, ignore them */
1200 for(; size > sizeof(uint64_t); b++, size--) {
1201 if(*b) {
1202 /* Value won't fit unsigned long */
1203 errno = ERANGE;
1204 return -1;
1205 }
1206 }
1207
1208 /* Conversion engine */
1209 for(l = 0; b < end; b++)
1210 l = (l << 8) | *b;
1211
1212 *lptr = l;
1213 return 0;
1214}
1215
1216int
Lev Walkin5c879db2007-11-06 06:23:31 +00001217asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1218 uint8_t *buf;
1219 uint8_t *end;
1220 uint8_t *b;
1221 int shr;
1222
1223 if(value <= LONG_MAX)
1224 return asn_long2INTEGER(st, value);
1225
1226 buf = (uint8_t *)MALLOC(1 + sizeof(value));
1227 if(!buf) return -1;
1228
1229 end = buf + (sizeof(value) + 1);
1230 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +00001231 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
1232 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +00001233
1234 if(st->buf) FREEMEM(st->buf);
1235 st->buf = buf;
1236 st->size = 1 + sizeof(value);
1237
1238 return 0;
1239}
1240
1241int
Harald Welte498c9712015-08-30 16:33:07 +02001242asn_uint642INTEGER(INTEGER_t *st, uint64_t value) {
1243 uint8_t *buf;
1244 uint8_t *end;
1245 uint8_t *b;
1246 int shr;
1247
1248 if(value <= INT64_MAX)
1249 return asn_int642INTEGER(st, value);
1250
1251 buf = (uint8_t *)MALLOC(1 + sizeof(value));
1252 if(!buf) return -1;
1253
1254 end = buf + (sizeof(value) + 1);
1255 buf[0] = 0;
1256 for(b = buf + 1, shr = (sizeof(value)-1)*8; b < end; shr -= 8, b++)
1257 *b = (uint8_t)(value >> shr);
1258
1259 if(st->buf) FREEMEM(st->buf);
1260 st->buf = buf;
1261 st->size = 1 + sizeof(value);
1262
1263 return 0;
1264}
1265
1266int
1267asn_int642INTEGER(INTEGER_t *st, int64_t value) {
1268 uint8_t *buf, *bp;
1269 uint8_t *p;
1270 uint8_t *pstart;
1271 uint8_t *pend1;
1272 int littleEndian = 1; /* Run-time detection */
1273 int add;
1274
1275 if(!st) {
1276 errno = EINVAL;
1277 return -1;
1278 }
1279
1280 buf = (uint8_t *)MALLOC(sizeof(value));
1281 if(!buf) return -1;
1282
1283 if(*(char *)&littleEndian) {
1284 pstart = (uint8_t *)&value + sizeof(value) - 1;
1285 pend1 = (uint8_t *)&value;
1286 add = -1;
1287 } else {
1288 pstart = (uint8_t *)&value;
1289 pend1 = pstart + sizeof(value) - 1;
1290 add = 1;
1291 }
1292
1293 /*
1294 * If the contents octet consists of more than one octet,
1295 * then bits of the first octet and bit 8 of the second octet:
1296 * a) shall not all be ones; and
1297 * b) shall not all be zero.
1298 */
1299 for(p = pstart; p != pend1; p += add) {
1300 switch(*p) {
1301 case 0x00: if((*(p+add) & 0x80) == 0)
1302 continue;
1303 break;
1304 case 0xff: if((*(p+add) & 0x80))
1305 continue;
1306 break;
1307 }
1308 break;
1309 }
1310 /* Copy the integer body */
1311 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1312 *bp++ = *p;
1313
1314 if(st->buf) FREEMEM(st->buf);
1315 st->buf = buf;
1316 st->size = bp - buf;
1317
1318 return 0;
1319}
1320
1321int
Lev Walkind703ff42004-10-21 11:21:25 +00001322asn_long2INTEGER(INTEGER_t *st, long value) {
1323 uint8_t *buf, *bp;
1324 uint8_t *p;
1325 uint8_t *pstart;
1326 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +00001327 int littleEndian = 1; /* Run-time detection */
1328 int add;
Lev Walkind703ff42004-10-21 11:21:25 +00001329
1330 if(!st) {
1331 errno = EINVAL;
1332 return -1;
1333 }
1334
Lev Walkin8471cec2004-10-21 14:02:19 +00001335 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +00001336 if(!buf) return -1;
1337
Lev Walkind7ad5612004-10-26 08:20:46 +00001338 if(*(char *)&littleEndian) {
1339 pstart = (uint8_t *)&value + sizeof(value) - 1;
1340 pend1 = (uint8_t *)&value;
1341 add = -1;
1342 } else {
1343 pstart = (uint8_t *)&value;
1344 pend1 = pstart + sizeof(value) - 1;
1345 add = 1;
1346 }
1347
Lev Walkind703ff42004-10-21 11:21:25 +00001348 /*
1349 * If the contents octet consists of more than one octet,
1350 * then bits of the first octet and bit 8 of the second octet:
1351 * a) shall not all be ones; and
1352 * b) shall not all be zero.
1353 */
Lev Walkin33700162004-10-26 09:03:31 +00001354 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +00001355 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +00001356 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +00001357 continue;
1358 break;
Lev Walkin33700162004-10-26 09:03:31 +00001359 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +00001360 continue;
1361 break;
1362 }
1363 break;
1364 }
1365 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +00001366 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1367 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +00001368
1369 if(st->buf) FREEMEM(st->buf);
1370 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +00001371 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +00001372
1373 return 0;
1374}
Lev Walkinb3751942012-09-02 19:36:47 -07001375
Lev Walkincad560a2013-03-16 07:00:58 -07001376/*
1377 * This function is going to be DEPRECATED soon.
1378 */
Lev Walkine09f9f12012-09-02 23:06:35 -07001379enum asn_strtol_result_e
1380asn_strtol(const char *str, const char *end, long *lp) {
Lev Walkincad560a2013-03-16 07:00:58 -07001381 const char *endp = end;
1382
1383 switch(asn_strtol_lim(str, &endp, lp)) {
1384 case ASN_STRTOL_ERROR_RANGE:
1385 return ASN_STRTOL_ERROR_RANGE;
1386 case ASN_STRTOL_ERROR_INVAL:
1387 return ASN_STRTOL_ERROR_INVAL;
1388 case ASN_STRTOL_EXPECT_MORE:
1389 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1390 case ASN_STRTOL_OK:
1391 return ASN_STRTOL_OK;
1392 case ASN_STRTOL_EXTRA_DATA:
1393 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1394 }
1395
1396 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1397}
1398
1399/*
1400 * Parse the number in the given string until the given *end position,
1401 * returning the position after the last parsed character back using the
1402 * same (*end) pointer.
1403 * WARNING: This behavior is different from the standard strtol(3).
1404 */
1405enum asn_strtol_result_e
1406asn_strtol_lim(const char *str, const char **end, long *lp) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001407 int sign = 1;
1408 long l;
Lev Walkinb3751942012-09-02 19:36:47 -07001409
Lev Walkine09f9f12012-09-02 23:06:35 -07001410 const long upper_boundary = LONG_MAX / 10;
1411 long last_digit_max = LONG_MAX % 10;
1412
Lev Walkincad560a2013-03-16 07:00:58 -07001413 if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -07001414
1415 switch(*str) {
1416 case '-':
1417 last_digit_max++;
1418 sign = -1;
1419 case '+':
1420 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001421 if(str >= *end) {
1422 *end = str;
1423 return ASN_STRTOL_EXPECT_MORE;
1424 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001425 }
1426
Lev Walkincad560a2013-03-16 07:00:58 -07001427 for(l = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001428 switch(*str) {
1429 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1430 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1431 int d = *str - '0';
1432 if(l < upper_boundary) {
1433 l = l * 10 + d;
1434 } else if(l == upper_boundary) {
1435 if(d <= last_digit_max) {
1436 if(sign > 0) {
1437 l = l * 10 + d;
1438 } else {
1439 sign = 1;
1440 l = -l * 10 - d;
1441 }
1442 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001443 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001444 return ASN_STRTOL_ERROR_RANGE;
1445 }
1446 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001447 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001448 return ASN_STRTOL_ERROR_RANGE;
1449 }
1450 }
1451 continue;
1452 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001453 *end = str;
1454 *lp = sign * l;
1455 return ASN_STRTOL_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001456 }
1457 }
1458
Lev Walkincad560a2013-03-16 07:00:58 -07001459 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001460 *lp = sign * l;
1461 return ASN_STRTOL_OK;
Lev Walkinb3751942012-09-02 19:36:47 -07001462}
1463