blob: 9ba14bc77e7b30875c9a26dec9640bc58cdd21ea [file] [log] [blame]
Harald Welte92c45f32010-06-12 18:59:38 +02001/*-
Harald Welte41b85d52015-08-31 08:56:53 +02002 * Copyright (c) 2003-2014 Lev Walkin <vlm@lionet.info>.
Harald Welte92c45f32010-06-12 18:59:38 +02003 * All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
5 */
6#include <asn_internal.h>
7#include <INTEGER.h>
8#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
9#include <errno.h>
10
11/*
12 * INTEGER basic type description.
13 */
Harald Welte41b85d52015-08-31 08:56:53 +020014static const ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
Harald Welte92c45f32010-06-12 18:59:38 +020015 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
16};
17asn_TYPE_descriptor_t asn_DEF_INTEGER = {
18 "INTEGER",
19 "INTEGER",
20 ASN__PRIMITIVE_TYPE_free,
21 INTEGER_print,
22 asn_generic_no_constraint,
23 ber_decode_primitive,
24 INTEGER_encode_der,
25 INTEGER_decode_xer,
26 INTEGER_encode_xer,
Harald Welte41b85d52015-08-31 08:56:53 +020027#ifdef ASN_DISABLE_PER_SUPPORT
28 0,
29 0,
30 0,
31 0,
32#else
Harald Welte92c45f32010-06-12 18:59:38 +020033 INTEGER_decode_uper, /* Unaligned PER decoder */
34 INTEGER_encode_uper, /* Unaligned PER encoder */
Harald Welte41b85d52015-08-31 08:56:53 +020035 INTEGER_decode_aper,
36 INTEGER_encode_aper,
37#endif /* ASN_DISABLE_PER_SUPPORT */
Harald Welte92c45f32010-06-12 18:59:38 +020038 0, /* Use generic outmost tag fetcher */
39 asn_DEF_INTEGER_tags,
40 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
41 asn_DEF_INTEGER_tags, /* Same as above */
42 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
43 0, /* No PER visible constraints */
44 0, 0, /* No members */
45 0 /* No specifics */
46};
47
48/*
49 * Encode INTEGER type using DER.
50 */
51asn_enc_rval_t
52INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
53 int tag_mode, ber_tlv_tag_t tag,
54 asn_app_consume_bytes_f *cb, void *app_key) {
55 INTEGER_t *st = (INTEGER_t *)sptr;
56
57 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
58 cb?"Encoding":"Estimating", td->name, tag_mode);
59
60 /*
61 * Canonicalize integer in the buffer.
62 * (Remove too long sign extension, remove some first 0x00 bytes)
63 */
64 if(st->buf) {
65 uint8_t *buf = st->buf;
66 uint8_t *end1 = buf + st->size - 1;
67 int shift;
68
69 /* Compute the number of superfluous leading bytes */
70 for(; buf < end1; buf++) {
71 /*
72 * If the contents octets of an integer value encoding
73 * consist of more than one octet, then the bits of the
74 * first octet and bit 8 of the second octet:
75 * a) shall not all be ones; and
76 * b) shall not all be zero.
77 */
78 switch(*buf) {
79 case 0x00: if((buf[1] & 0x80) == 0)
80 continue;
81 break;
82 case 0xff: if((buf[1] & 0x80))
83 continue;
84 break;
85 }
86 break;
87 }
88
89 /* Remove leading superfluous bytes from the integer */
90 shift = buf - st->buf;
91 if(shift) {
92 uint8_t *nb = st->buf;
93 uint8_t *end;
94
95 st->size -= shift; /* New size, minus bad bytes */
96 end = nb + st->size;
97
98 for(; nb < end; nb++, buf++)
99 *nb = *buf;
100 }
101
102 } /* if(1) */
103
104 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
105}
106
107static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop);
108
109/*
110 * INTEGER specific human-readable output.
111 */
112static ssize_t
Harald Welte41b85d52015-08-31 08:56:53 +0200113INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
Harald Welte92c45f32010-06-12 18:59:38 +0200114 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
115 char scratch[32]; /* Enough for 64-bit integer */
116 uint8_t *buf = st->buf;
117 uint8_t *buf_end = st->buf + st->size;
Harald Welte41b85d52015-08-31 08:56:53 +0200118 signed long value;
Harald Welte92c45f32010-06-12 18:59:38 +0200119 ssize_t wrote = 0;
120 char *p;
121 int ret;
122
Harald Welte41b85d52015-08-31 08:56:53 +0200123 if(specs && specs->field_unsigned)
124 ret = asn_INTEGER2ulong(st, (unsigned long *)&value);
125 else
126 ret = asn_INTEGER2long(st, &value);
Harald Welte92c45f32010-06-12 18:59:38 +0200127
128 /* Simple case: the integer size is small */
Harald Welte41b85d52015-08-31 08:56:53 +0200129 if(ret == 0) {
Harald Welte92c45f32010-06-12 18:59:38 +0200130 const asn_INTEGER_enum_map_t *el;
131 size_t scrsize;
132 char *scr;
133
Harald Welte41b85d52015-08-31 08:56:53 +0200134 el = (value >= 0 || !specs || !specs->field_unsigned)
135 ? INTEGER_map_value2enum(specs, value) : 0;
Harald Welte92c45f32010-06-12 18:59:38 +0200136 if(el) {
137 scrsize = el->enum_len + 32;
138 scr = (char *)alloca(scrsize);
139 if(plainOrXER == 0)
140 ret = snprintf(scr, scrsize,
Harald Welte41b85d52015-08-31 08:56:53 +0200141 "%ld (%s)", value, el->enum_name);
Harald Welte92c45f32010-06-12 18:59:38 +0200142 else
143 ret = snprintf(scr, scrsize,
144 "<%s/>", el->enum_name);
145 } else if(plainOrXER && specs && specs->strict_enumeration) {
146 ASN_DEBUG("ASN.1 forbids dealing with "
147 "unknown value of ENUMERATED type");
148 errno = EPERM;
149 return -1;
150 } else {
151 scrsize = sizeof(scratch);
152 scr = scratch;
Harald Welteec0e2172010-07-20 00:03:44 +0200153 ret = snprintf(scr, scrsize,
154 (specs && specs->field_unsigned)
Harald Welte41b85d52015-08-31 08:56:53 +0200155 ?"%lu":"%ld", value);
Harald Welte92c45f32010-06-12 18:59:38 +0200156 }
157 assert(ret > 0 && (size_t)ret < scrsize);
158 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
159 } else if(plainOrXER && specs && specs->strict_enumeration) {
160 /*
161 * Here and earlier, we cannot encode the ENUMERATED values
162 * if there is no corresponding identifier.
163 */
164 ASN_DEBUG("ASN.1 forbids dealing with "
165 "unknown value of ENUMERATED type");
166 errno = EPERM;
167 return -1;
168 }
169
170 /* Output in the long xx:yy:zz... format */
171 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
172 for(p = scratch; buf < buf_end; buf++) {
Harald Welte41b85d52015-08-31 08:56:53 +0200173 const char * const h2c = "0123456789ABCDEF";
Harald Welte92c45f32010-06-12 18:59:38 +0200174 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
175 /* Flush buffer */
176 if(cb(scratch, p - scratch, app_key) < 0)
177 return -1;
178 wrote += p - scratch;
179 p = scratch;
180 }
181 *p++ = h2c[*buf >> 4];
182 *p++ = h2c[*buf & 0x0F];
183 *p++ = 0x3a; /* ":" */
184 }
185 if(p != scratch)
186 p--; /* Remove the last ":" */
187
188 wrote += p - scratch;
189 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
190}
191
192/*
193 * INTEGER specific human-readable output.
194 */
195int
196INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
197 asn_app_consume_bytes_f *cb, void *app_key) {
198 const INTEGER_t *st = (const INTEGER_t *)sptr;
199 ssize_t ret;
200
201 (void)td;
202 (void)ilevel;
203
204 if(!st || !st->buf)
205 ret = cb("<absent>", 8, app_key);
206 else
207 ret = INTEGER__dump(td, st, cb, app_key, 0);
208
209 return (ret < 0) ? -1 : 0;
210}
211
212struct e2v_key {
213 const char *start;
214 const char *stop;
Harald Welte41b85d52015-08-31 08:56:53 +0200215 const asn_INTEGER_enum_map_t *vemap;
216 const unsigned int *evmap;
Harald Welte92c45f32010-06-12 18:59:38 +0200217};
218static int
219INTEGER__compar_enum2value(const void *kp, const void *am) {
220 const struct e2v_key *key = (const struct e2v_key *)kp;
221 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
222 const char *ptr, *end, *name;
223
224 /* Remap the element (sort by different criterion) */
225 el = key->vemap + key->evmap[el - key->vemap];
226
227 /* Compare strings */
228 for(ptr = key->start, end = key->stop, name = el->enum_name;
229 ptr < end; ptr++, name++) {
230 if(*ptr != *name)
231 return *(const unsigned char *)ptr
232 - *(const unsigned char *)name;
233 }
234 return name[0] ? -1 : 0;
235}
236
237static const asn_INTEGER_enum_map_t *
238INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Harald Welte41b85d52015-08-31 08:56:53 +0200239 const asn_INTEGER_enum_map_t *el_found;
Harald Welte92c45f32010-06-12 18:59:38 +0200240 int count = specs ? specs->map_count : 0;
241 struct e2v_key key;
242 const char *lp;
243
244 if(!count) return NULL;
245
246 /* Guaranteed: assert(lstart < lstop); */
247 /* Figure out the tag name */
248 for(lstart++, lp = lstart; lp < lstop; lp++) {
249 switch(*lp) {
250 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
251 case 0x2f: /* '/' */ case 0x3e: /* '>' */
252 break;
253 default:
254 continue;
255 }
256 break;
257 }
258 if(lp == lstop) return NULL; /* No tag found */
259 lstop = lp;
260
261 key.start = lstart;
262 key.stop = lstop;
263 key.vemap = specs->value2enum;
264 key.evmap = specs->enum2value;
265 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
266 specs->value2enum, count, sizeof(specs->value2enum[0]),
267 INTEGER__compar_enum2value);
268 if(el_found) {
269 /* Remap enum2value into value2enum */
270 el_found = key.vemap + key.evmap[el_found - key.vemap];
271 }
272 return el_found;
273}
274
275static int
276INTEGER__compar_value2enum(const void *kp, const void *am) {
277 long a = *(const long *)kp;
278 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
279 long b = el->nat_value;
280 if(a < b) return -1;
281 else if(a == b) return 0;
282 else return 1;
283}
284
285const asn_INTEGER_enum_map_t *
286INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
287 int count = specs ? specs->map_count : 0;
288 if(!count) return 0;
289 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
290 count, sizeof(specs->value2enum[0]),
291 INTEGER__compar_value2enum);
292}
293
294static int
295INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
296 void *p = MALLOC(min_size + 1);
297 if(p) {
298 void *b = st->buf;
299 st->size = 0;
300 st->buf = p;
301 FREEMEM(b);
302 return 0;
303 } else {
304 return -1;
305 }
306}
307
308/*
309 * Decode the chunk of XML text encoding INTEGER.
310 */
311static enum xer_pbd_rval
312INTEGER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
313 INTEGER_t *st = (INTEGER_t *)sptr;
Harald Welte41b85d52015-08-31 08:56:53 +0200314 long dec_value;
315 long hex_value = 0;
Harald Welte92c45f32010-06-12 18:59:38 +0200316 const char *lp;
317 const char *lstart = (const char *)chunk_buf;
318 const char *lstop = lstart + chunk_size;
319 enum {
Harald Welte41b85d52015-08-31 08:56:53 +0200320 ST_LEADSPACE,
Harald Welte92c45f32010-06-12 18:59:38 +0200321 ST_SKIPSPHEX,
322 ST_WAITDIGITS,
323 ST_DIGITS,
Harald Welte41b85d52015-08-31 08:56:53 +0200324 ST_DIGITS_TRAILSPACE,
Harald Welte92c45f32010-06-12 18:59:38 +0200325 ST_HEXDIGIT1,
326 ST_HEXDIGIT2,
Harald Welte41b85d52015-08-31 08:56:53 +0200327 ST_HEXDIGITS_TRAILSPACE,
Harald Welte92c45f32010-06-12 18:59:38 +0200328 ST_HEXCOLON,
Harald Welte41b85d52015-08-31 08:56:53 +0200329 ST_END_ENUM,
330 ST_UNEXPECTED
331 } state = ST_LEADSPACE;
332 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
333 const char *dec_value_end = 0;
Harald Welte92c45f32010-06-12 18:59:38 +0200334
335 if(chunk_size)
Harald Welteec0e2172010-07-20 00:03:44 +0200336 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
337 (long)chunk_size, *lstart, lstop[-1]);
Harald Welte92c45f32010-06-12 18:59:38 +0200338
Harald Welte41b85d52015-08-31 08:56:53 +0200339 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
340 return XPBD_SYSTEM_FAILURE;
341
Harald Welte92c45f32010-06-12 18:59:38 +0200342 /*
343 * We may have received a tag here. It will be processed inline.
344 * Use strtoul()-like code and serialize the result.
345 */
Harald Welte41b85d52015-08-31 08:56:53 +0200346 for(lp = lstart; lp < lstop; lp++) {
Harald Welte92c45f32010-06-12 18:59:38 +0200347 int lv = *lp;
348 switch(lv) {
349 case 0x09: case 0x0a: case 0x0d: case 0x20:
350 switch(state) {
Harald Welte41b85d52015-08-31 08:56:53 +0200351 case ST_LEADSPACE:
352 case ST_DIGITS_TRAILSPACE:
353 case ST_HEXDIGITS_TRAILSPACE:
Harald Welte92c45f32010-06-12 18:59:38 +0200354 case ST_SKIPSPHEX:
355 continue;
Harald Welte41b85d52015-08-31 08:56:53 +0200356 case ST_DIGITS:
357 dec_value_end = lp;
358 state = ST_DIGITS_TRAILSPACE;
359 continue;
Harald Welte92c45f32010-06-12 18:59:38 +0200360 case ST_HEXCOLON:
Harald Welte41b85d52015-08-31 08:56:53 +0200361 state = ST_HEXDIGITS_TRAILSPACE;
362 continue;
Harald Welte92c45f32010-06-12 18:59:38 +0200363 default:
364 break;
365 }
366 break;
367 case 0x2d: /* '-' */
Harald Welte41b85d52015-08-31 08:56:53 +0200368 if(state == ST_LEADSPACE) {
369 dec_value = 0;
370 dec_value_start = lp;
Harald Welte92c45f32010-06-12 18:59:38 +0200371 state = ST_WAITDIGITS;
372 continue;
373 }
374 break;
375 case 0x2b: /* '+' */
Harald Welte41b85d52015-08-31 08:56:53 +0200376 if(state == ST_LEADSPACE) {
377 dec_value = 0;
378 dec_value_start = lp;
Harald Welte92c45f32010-06-12 18:59:38 +0200379 state = ST_WAITDIGITS;
380 continue;
381 }
382 break;
383 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
384 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
385 switch(state) {
Harald Welte41b85d52015-08-31 08:56:53 +0200386 case ST_DIGITS: continue;
Harald Welte92c45f32010-06-12 18:59:38 +0200387 case ST_SKIPSPHEX: /* Fall through */
388 case ST_HEXDIGIT1:
Harald Welte41b85d52015-08-31 08:56:53 +0200389 hex_value = (lv - 0x30) << 4;
Harald Welte92c45f32010-06-12 18:59:38 +0200390 state = ST_HEXDIGIT2;
391 continue;
392 case ST_HEXDIGIT2:
Harald Welte41b85d52015-08-31 08:56:53 +0200393 hex_value += (lv - 0x30);
Harald Welte92c45f32010-06-12 18:59:38 +0200394 state = ST_HEXCOLON;
Harald Welte41b85d52015-08-31 08:56:53 +0200395 st->buf[st->size++] = (uint8_t)hex_value;
Harald Welte92c45f32010-06-12 18:59:38 +0200396 continue;
397 case ST_HEXCOLON:
398 return XPBD_BROKEN_ENCODING;
Harald Welte41b85d52015-08-31 08:56:53 +0200399 case ST_LEADSPACE:
400 dec_value = 0;
401 dec_value_start = lp;
402 /* FALL THROUGH */
403 case ST_WAITDIGITS:
Harald Welte92c45f32010-06-12 18:59:38 +0200404 state = ST_DIGITS;
Harald Welte41b85d52015-08-31 08:56:53 +0200405 continue;
406 default:
Harald Welte92c45f32010-06-12 18:59:38 +0200407 break;
408 }
Harald Welte41b85d52015-08-31 08:56:53 +0200409 break;
410 case 0x3c: /* '<', start of XML encoded enumeration */
411 if(state == ST_LEADSPACE) {
Harald Welte92c45f32010-06-12 18:59:38 +0200412 const asn_INTEGER_enum_map_t *el;
413 el = INTEGER_map_enum2value(
414 (asn_INTEGER_specifics_t *)
415 td->specifics, lstart, lstop);
416 if(el) {
Harald Welte41b85d52015-08-31 08:56:53 +0200417 ASN_DEBUG("Found \"%s\" => %lld",
Harald Welte92c45f32010-06-12 18:59:38 +0200418 el->enum_name, el->nat_value);
Harald Welte41b85d52015-08-31 08:56:53 +0200419 dec_value = el->nat_value;
420 state = ST_END_ENUM;
Harald Welte92c45f32010-06-12 18:59:38 +0200421 lp = lstop - 1;
422 continue;
423 }
424 ASN_DEBUG("Unknown identifier for INTEGER");
425 }
426 return XPBD_BROKEN_ENCODING;
427 case 0x3a: /* ':' */
428 if(state == ST_HEXCOLON) {
429 /* This colon is expected */
430 state = ST_HEXDIGIT1;
431 continue;
432 } else if(state == ST_DIGITS) {
433 /* The colon here means that we have
434 * decoded the first two hexadecimal
435 * places as a decimal value.
436 * Switch decoding mode. */
437 ASN_DEBUG("INTEGER re-evaluate as hex form");
Harald Welte92c45f32010-06-12 18:59:38 +0200438 state = ST_SKIPSPHEX;
Harald Welte41b85d52015-08-31 08:56:53 +0200439 dec_value_start = 0;
Harald Welte92c45f32010-06-12 18:59:38 +0200440 lp = lstart - 1;
441 continue;
442 } else {
Harald Welte41b85d52015-08-31 08:56:53 +0200443 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Harald Welte92c45f32010-06-12 18:59:38 +0200444 break;
445 }
446 /* [A-Fa-f] */
447 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
448 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
449 switch(state) {
450 case ST_SKIPSPHEX:
Harald Welte41b85d52015-08-31 08:56:53 +0200451 case ST_LEADSPACE: /* Fall through */
Harald Welte92c45f32010-06-12 18:59:38 +0200452 case ST_HEXDIGIT1:
Harald Welte41b85d52015-08-31 08:56:53 +0200453 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
454 hex_value += 10;
455 hex_value <<= 4;
Harald Welte92c45f32010-06-12 18:59:38 +0200456 state = ST_HEXDIGIT2;
457 continue;
458 case ST_HEXDIGIT2:
Harald Welte41b85d52015-08-31 08:56:53 +0200459 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
460 hex_value += 10;
461 st->buf[st->size++] = (uint8_t)hex_value;
Harald Welte92c45f32010-06-12 18:59:38 +0200462 state = ST_HEXCOLON;
463 continue;
464 case ST_DIGITS:
465 ASN_DEBUG("INTEGER re-evaluate as hex form");
Harald Welte92c45f32010-06-12 18:59:38 +0200466 state = ST_SKIPSPHEX;
Harald Welte41b85d52015-08-31 08:56:53 +0200467 dec_value_start = 0;
Harald Welte92c45f32010-06-12 18:59:38 +0200468 lp = lstart - 1;
469 continue;
470 default:
471 break;
472 }
473 break;
474 }
475
476 /* Found extra non-numeric stuff */
Harald Welte41b85d52015-08-31 08:56:53 +0200477 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
478 lv, (long)(lp - lstart));
479 state = ST_UNEXPECTED;
Harald Welte92c45f32010-06-12 18:59:38 +0200480 break;
481 }
482
483 switch(state) {
Harald Welte41b85d52015-08-31 08:56:53 +0200484 case ST_END_ENUM:
485 /* Got a complete and valid enumeration encoded as a tag. */
486 break;
Harald Welte92c45f32010-06-12 18:59:38 +0200487 case ST_DIGITS:
Harald Welte41b85d52015-08-31 08:56:53 +0200488 dec_value_end = lstop;
489 /* FALL THROUGH */
490 case ST_DIGITS_TRAILSPACE:
491 /* The last symbol encountered was a digit. */
492 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
493 case ASN_STRTOL_OK:
494 break;
495 case ASN_STRTOL_ERROR_RANGE:
496 return XPBD_DECODER_LIMIT;
497 case ASN_STRTOL_ERROR_INVAL:
498 case ASN_STRTOL_EXPECT_MORE:
499 case ASN_STRTOL_EXTRA_DATA:
500 return XPBD_BROKEN_ENCODING;
501 }
Harald Welte92c45f32010-06-12 18:59:38 +0200502 break;
503 case ST_HEXCOLON:
Harald Welte41b85d52015-08-31 08:56:53 +0200504 case ST_HEXDIGITS_TRAILSPACE:
Harald Welte92c45f32010-06-12 18:59:38 +0200505 st->buf[st->size] = 0; /* Just in case termination */
506 return XPBD_BODY_CONSUMED;
507 case ST_HEXDIGIT1:
508 case ST_HEXDIGIT2:
509 case ST_SKIPSPHEX:
510 return XPBD_BROKEN_ENCODING;
Harald Welte41b85d52015-08-31 08:56:53 +0200511 case ST_LEADSPACE:
512 /* Content not found */
513 return XPBD_NOT_BODY_IGNORE;
514 case ST_WAITDIGITS:
515 case ST_UNEXPECTED:
516 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
517 return XPBD_BROKEN_ENCODING; /* No digits */
Harald Welte92c45f32010-06-12 18:59:38 +0200518 }
519
Harald Welte41b85d52015-08-31 08:56:53 +0200520 /*
521 * Convert the result of parsing of enumeration or a straight
522 * decimal value into a BER representation.
523 */
524 if(asn_long2INTEGER(st, dec_value))
Harald Welte92c45f32010-06-12 18:59:38 +0200525 return XPBD_SYSTEM_FAILURE;
526
527 return XPBD_BODY_CONSUMED;
528}
529
530asn_dec_rval_t
531INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
532 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
533 const void *buf_ptr, size_t size) {
534
535 return xer_decode_primitive(opt_codec_ctx, td,
536 sptr, sizeof(INTEGER_t), opt_mname,
537 buf_ptr, size, INTEGER__xer_body_decode);
538}
539
540asn_enc_rval_t
541INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
542 int ilevel, enum xer_encoder_flags_e flags,
543 asn_app_consume_bytes_f *cb, void *app_key) {
544 const INTEGER_t *st = (const INTEGER_t *)sptr;
545 asn_enc_rval_t er;
546
547 (void)ilevel;
548 (void)flags;
549
550 if(!st || !st->buf)
551 _ASN_ENCODE_FAILED;
552
553 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
554 if(er.encoded < 0) _ASN_ENCODE_FAILED;
555
556 _ASN_ENCODED_OK(er);
557}
558
Harald Welte41b85d52015-08-31 08:56:53 +0200559#ifndef ASN_DISABLE_PER_SUPPORT
560
Harald Welte92c45f32010-06-12 18:59:38 +0200561asn_dec_rval_t
562INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
563 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Harald Welteec0e2172010-07-20 00:03:44 +0200564 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Harald Welte92c45f32010-06-12 18:59:38 +0200565 asn_dec_rval_t rval = { RC_OK, 0 };
566 INTEGER_t *st = (INTEGER_t *)*sptr;
567 asn_per_constraint_t *ct;
568 int repeat;
569
570 (void)opt_codec_ctx;
571
572 if(!st) {
573 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
574 if(!st) _ASN_DECODE_FAILED;
575 }
576
577 if(!constraints) constraints = td->per_constraints;
578 ct = constraints ? &constraints->value : 0;
579
580 if(ct && ct->flags & APC_EXTENSIBLE) {
581 int inext = per_get_few_bits(pd, 1);
582 if(inext < 0) _ASN_DECODE_STARVED;
583 if(inext) ct = 0;
584 }
585
586 FREEMEM(st->buf);
Harald Welteec0e2172010-07-20 00:03:44 +0200587 st->buf = 0;
588 st->size = 0;
Harald Welte92c45f32010-06-12 18:59:38 +0200589 if(ct) {
590 if(ct->flags & APC_SEMI_CONSTRAINED) {
591 st->buf = (uint8_t *)CALLOC(1, 2);
592 if(!st->buf) _ASN_DECODE_FAILED;
593 st->size = 1;
594 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
595 size_t size = (ct->range_bits + 7) >> 3;
596 st->buf = (uint8_t *)MALLOC(1 + size + 1);
597 if(!st->buf) _ASN_DECODE_FAILED;
598 st->size = size;
Harald Welte92c45f32010-06-12 18:59:38 +0200599 }
Harald Welte92c45f32010-06-12 18:59:38 +0200600 }
601
Harald Welte41b85d52015-08-31 08:56:53 +0200602 /* X.691-2008/11, #13.2.2, constrained whole number */
603 if(ct && ct->flags != APC_UNCONSTRAINED) {
604 /* #11.5.6 */
605 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
606 if(ct->range_bits >= 0) {
607 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
608 _ASN_DECODE_FAILED;
609
610 if(specs && specs->field_unsigned) {
611 unsigned long uvalue;
612 if(uper_get_constrained_whole_number(pd,
613 &uvalue, ct->range_bits))
614 _ASN_DECODE_STARVED;
615 ASN_DEBUG("Got value %lu + low %lld",
616 uvalue, ct->lower_bound);
617 uvalue += ct->lower_bound;
618 if(asn_ulong2INTEGER(st, uvalue))
619 _ASN_DECODE_FAILED;
620 } else {
621 unsigned long svalue;
622 if(uper_get_constrained_whole_number(pd,
623 &svalue, ct->range_bits))
624 _ASN_DECODE_STARVED;
625 ASN_DEBUG("Got value %ld + low %ld",
626 svalue, ct->lower_bound);
627 svalue += ct->lower_bound;
628 if(asn_long2INTEGER(st, svalue))
629 _ASN_DECODE_FAILED;
630 }
631 return rval;
632 }
633 } else {
634 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
635 }
636
637 /* X.691, #12.2.3, #12.2.4 */
638 do {
639 ssize_t len;
640 void *p;
641 int ret;
642
643 /* Get the PER length */
644 len = uper_get_length(pd, -1, &repeat);
645 if(len < 0) _ASN_DECODE_STARVED;
646
647 p = REALLOC(st->buf, st->size + len + 1);
648 if(!p) _ASN_DECODE_FAILED;
649 st->buf = (uint8_t *)p;
650
651 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
652 if(ret < 0) _ASN_DECODE_STARVED;
653 st->size += len;
654 } while(repeat);
655 st->buf[st->size] = 0; /* JIC */
656
657 /* #12.2.3 */
658 if(ct && ct->lower_bound) {
659 /*
660 * TODO: replace by in-place arithmetics.
661 */
662 long value;
663 if(asn_INTEGER2long(st, &value))
664 _ASN_DECODE_FAILED;
665 if(asn_long2INTEGER(st, value + ct->lower_bound))
666 _ASN_DECODE_FAILED;
667 }
668
669 return rval;
670}
671
672asn_dec_rval_t
673INTEGER_decode_aper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
674 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
675 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
676 asn_dec_rval_t rval = { RC_OK, 0 };
677 INTEGER_t *st = (INTEGER_t *)*sptr;
678 asn_per_constraint_t *ct;
679 int repeat;
680
681 (void)opt_codec_ctx;
682
683 if(!st) {
684 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
685 if(!st) _ASN_DECODE_FAILED;
686 }
687
688 if(!constraints) constraints = td->per_constraints;
689 ct = constraints ? &constraints->value : 0;
690
691 if(ct && ct->flags & APC_EXTENSIBLE) {
692 int inext = per_get_few_bits(pd, 1);
693 if(inext < 0) _ASN_DECODE_STARVED;
694 if(inext) ct = 0;
695 }
696
697 FREEMEM(st->buf);
698 st->buf = 0;
699 st->size = 0;
700 if(ct) {
701 if(ct->flags & APC_SEMI_CONSTRAINED) {
702 st->buf = (uint8_t *)CALLOC(1, 2);
703 if(!st->buf) _ASN_DECODE_FAILED;
704 st->size = 1;
705 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
706 size_t size = (ct->range_bits + 7) >> 3;
707 st->buf = (uint8_t *)MALLOC(1 + size + 1);
708 if(!st->buf) _ASN_DECODE_FAILED;
709 st->size = size;
710 }
711 }
712
Harald Welte92c45f32010-06-12 18:59:38 +0200713 /* X.691, #12.2.2 */
714 if(ct && ct->flags != APC_UNCONSTRAINED) {
715 /* #10.5.6 */
716 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
717 if(ct->range_bits >= 0) {
Harald Welte41b85d52015-08-31 08:56:53 +0200718 if (ct->range_bits > 16) {
719 int max_range_bytes = (ct->range_bits >> 3) + 1;
720 int length, i;
721 int64_t value = 0;
722
723 for (i = 0; i < max_range_bytes; i++) {
724 int upper = 1 << (i + 1);
725 if (upper > max_range_bytes)
726 break;
727 }
728 if ((length = per_get_few_bits(pd, i + 1)) < 0)
729 _ASN_DECODE_STARVED;
730 if (aper_get_align(pd) != 0)
731 _ASN_DECODE_STARVED;
732 ASN_DEBUG("Got length %d", length + 1);
733 for (i = 0; i < length + 1; i++) {
734 int buf = per_get_few_bits(pd, 8);
735 if (buf < 0)
736 _ASN_DECODE_STARVED;
737 value += (((int64_t)buf) << (8 * i));
738 }
739
740 if((specs && specs->field_unsigned)
741 ? asn_uint642INTEGER(st, value)
742 : asn_int642INTEGER(st, value))
743 _ASN_DECODE_FAILED;
744 ASN_DEBUG("Got value %lld + low %lld",
745 value, ct->lower_bound);
746 value += ct->lower_bound;
Harald Welteec0e2172010-07-20 00:03:44 +0200747 } else {
Harald Welte41b85d52015-08-31 08:56:53 +0200748 long value = 0;
749 if (ct->range_bits < 8) {
750 value = per_get_few_bits(pd, ct->range_bits);
751 if(value < 0) _ASN_DECODE_STARVED;
752 } else if (ct->range_bits == 8) {
753 if (aper_get_align(pd) < 0)
754 _ASN_DECODE_FAILED;
755 value = per_get_few_bits(pd, ct->range_bits);
756 if(value < 0) _ASN_DECODE_STARVED;
757 } else {
758 /* Align */
759 if (aper_get_align(pd) < 0)
760 _ASN_DECODE_FAILED;
761 value = per_get_few_bits(pd, 16);
762 if(value < 0) _ASN_DECODE_STARVED;
763 }
764 if((specs && specs->field_unsigned)
765 ? asn_ulong2INTEGER(st, value)
766 : asn_long2INTEGER(st, value))
767 _ASN_DECODE_FAILED;
768 ASN_DEBUG("Got value %ld + low %lld",
769 value, ct->lower_bound);
770 value += ct->lower_bound;
Harald Welteec0e2172010-07-20 00:03:44 +0200771 }
Harald Welte92c45f32010-06-12 18:59:38 +0200772 return rval;
Harald Welte41b85d52015-08-31 08:56:53 +0200773 } else {
774 _ASN_DECODE_FAILED;
Harald Welte92c45f32010-06-12 18:59:38 +0200775 }
776 } else {
777 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
778 }
779
780 /* X.691, #12.2.3, #12.2.4 */
781 do {
782 ssize_t len;
783 void *p;
784 int ret;
785
786 /* Get the PER length */
Harald Welte41b85d52015-08-31 08:56:53 +0200787 len = aper_get_length(pd, -1, -1, &repeat);
Harald Welte92c45f32010-06-12 18:59:38 +0200788 if(len < 0) _ASN_DECODE_STARVED;
789
790 p = REALLOC(st->buf, st->size + len + 1);
791 if(!p) _ASN_DECODE_FAILED;
792 st->buf = (uint8_t *)p;
793
794 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
795 if(ret < 0) _ASN_DECODE_STARVED;
796 st->size += len;
797 } while(repeat);
798 st->buf[st->size] = 0; /* JIC */
799
800 /* #12.2.3 */
801 if(ct && ct->lower_bound) {
802 /*
803 * TODO: replace by in-place arithmetics.
804 */
805 long value;
806 if(asn_INTEGER2long(st, &value))
807 _ASN_DECODE_FAILED;
808 if(asn_long2INTEGER(st, value + ct->lower_bound))
809 _ASN_DECODE_FAILED;
810 }
811
812 return rval;
813}
814
815asn_enc_rval_t
816INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
817 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Harald Welteec0e2172010-07-20 00:03:44 +0200818 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Harald Welte92c45f32010-06-12 18:59:38 +0200819 asn_enc_rval_t er;
820 INTEGER_t *st = (INTEGER_t *)sptr;
821 const uint8_t *buf;
822 const uint8_t *end;
823 asn_per_constraint_t *ct;
824 long value = 0;
825
826 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
827
828 if(!constraints) constraints = td->per_constraints;
829 ct = constraints ? &constraints->value : 0;
830
831 er.encoded = 0;
832
833 if(ct) {
834 int inext = 0;
Harald Welteec0e2172010-07-20 00:03:44 +0200835 if(specs && specs->field_unsigned) {
836 unsigned long uval;
837 if(asn_INTEGER2ulong(st, &uval))
838 _ASN_ENCODE_FAILED;
839 /* Check proper range */
840 if(ct->flags & APC_SEMI_CONSTRAINED) {
841 if(uval < (unsigned long)ct->lower_bound)
842 inext = 1;
843 } else if(ct->range_bits >= 0) {
844 if(uval < (unsigned long)ct->lower_bound
845 || uval > (unsigned long)ct->upper_bound)
846 inext = 1;
847 }
Harald Welte41b85d52015-08-31 08:56:53 +0200848 ASN_DEBUG("Value %lu (%02x/%d) lb %llu ub %llu %s",
Harald Welteec0e2172010-07-20 00:03:44 +0200849 uval, st->buf[0], st->size,
850 ct->lower_bound, ct->upper_bound,
851 inext ? "ext" : "fix");
852 value = uval;
853 } else {
854 if(asn_INTEGER2long(st, &value))
855 _ASN_ENCODE_FAILED;
856 /* Check proper range */
857 if(ct->flags & APC_SEMI_CONSTRAINED) {
858 if(value < ct->lower_bound)
859 inext = 1;
860 } else if(ct->range_bits >= 0) {
861 if(value < ct->lower_bound
862 || value > ct->upper_bound)
863 inext = 1;
864 }
Harald Welte41b85d52015-08-31 08:56:53 +0200865 ASN_DEBUG("Value %ld (%02x/%d) lb %lld ub %lld %s",
Harald Welteec0e2172010-07-20 00:03:44 +0200866 value, st->buf[0], st->size,
867 ct->lower_bound, ct->upper_bound,
868 inext ? "ext" : "fix");
Harald Welte92c45f32010-06-12 18:59:38 +0200869 }
Harald Welte92c45f32010-06-12 18:59:38 +0200870 if(ct->flags & APC_EXTENSIBLE) {
871 if(per_put_few_bits(po, inext, 1))
872 _ASN_ENCODE_FAILED;
873 if(inext) ct = 0;
874 } else if(inext) {
875 _ASN_ENCODE_FAILED;
876 }
877 }
878
879
Harald Welte41b85d52015-08-31 08:56:53 +0200880 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Harald Welte92c45f32010-06-12 18:59:38 +0200881 if(ct && ct->range_bits >= 0) {
Harald Welte41b85d52015-08-31 08:56:53 +0200882 /* #11.5.6 -> #11.3 */
883 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
884 value, value - ct->lower_bound, ct->range_bits);
885 unsigned long v = value - ct->lower_bound;
886 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
887 _ASN_ENCODE_FAILED;
Harald Welte92c45f32010-06-12 18:59:38 +0200888 _ASN_ENCODED_OK(er);
889 }
890
891 if(ct && ct->lower_bound) {
Harald Welte41b85d52015-08-31 08:56:53 +0200892 ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound);
Harald Welte92c45f32010-06-12 18:59:38 +0200893 /* TODO: adjust lower bound */
894 _ASN_ENCODE_FAILED;
895 }
896
897 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
898 ssize_t mayEncode = uper_put_length(po, end - buf);
899 if(mayEncode < 0)
900 _ASN_ENCODE_FAILED;
901 if(per_put_many_bits(po, buf, 8 * mayEncode))
902 _ASN_ENCODE_FAILED;
903 buf += mayEncode;
904 }
905
906 _ASN_ENCODED_OK(er);
907}
908
Harald Welte41b85d52015-08-31 08:56:53 +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 */
Harald Welte667d7582015-12-19 13:37:02 +0100975 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
976 value, value - ct->lower_bound, ct->range_bits);
977 unsigned long v = value - ct->lower_bound;
Harald Welte41b85d52015-08-31 08:56:53 +0200978
979 /* #12 <= 8 -> alignment ? */
980 if (ct->range_bits < 8) {
Harald Welte667d7582015-12-19 13:37:02 +0100981 if(per_put_few_bits(po, 0x00 | v, ct->range_bits))
Harald Welte41b85d52015-08-31 08:56:53 +0200982 _ASN_ENCODE_FAILED;
983 } else if (ct->range_bits == 8) {
984 if(aper_put_align(po) < 0)
985 _ASN_ENCODE_FAILED;
Harald Welte667d7582015-12-19 13:37:02 +0100986 if(per_put_few_bits(po, 0x00 | v, ct->range_bits))
Harald Welte41b85d52015-08-31 08:56:53 +0200987 _ASN_ENCODE_FAILED;
988 } else if (ct->range_bits <= 16) {
989 // Consume the bytes to align on octet
990 if(aper_put_align(po) < 0)
991 _ASN_ENCODE_FAILED;
Harald Welte667d7582015-12-19 13:37:02 +0100992 if(per_put_few_bits(po, 0x0000 | v,
Harald Welte41b85d52015-08-31 08:56:53 +0200993 16))
994 _ASN_ENCODE_FAILED;
995 } else {
996 /* TODO: extend to >64 bits */
Harald Welte667d7582015-12-19 13:37:02 +0100997 int64_t v64 = v;
Harald Welte41b85d52015-08-31 08:56:53 +0200998 int i;
999
1000 /* Putting length - 1 in the minimum number of bits ex: 5 = 3bits */
1001 if (per_put_few_bits(po, st->size - 1, (ct->range_bits >> 3)-1))
1002 _ASN_ENCODE_FAILED;
1003
1004 // Consume the bits to align on octet
1005 if (aper_put_align(po) < 0)
1006 _ASN_ENCODE_FAILED;
1007 /* Put the value */
1008 for (i = 0; i < st->size; i++) {
Harald Welte667d7582015-12-19 13:37:02 +01001009 if(per_put_few_bits(po, (v64 >> (8 * (st->size - i - 1))) & 0xff, 8)) _ASN_ENCODE_FAILED;
Harald Welte41b85d52015-08-31 08:56:53 +02001010 }
1011 }
1012 _ASN_ENCODED_OK(er);
1013 }
1014
1015 if(ct && ct->lower_bound) {
1016 ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound);
1017 /* TODO: adjust lower bound */
1018 _ASN_ENCODE_FAILED;
1019 }
1020
1021 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
1022 ssize_t mayEncode = aper_put_length(po, -1, end - buf);
1023 if(mayEncode < 0)
1024 _ASN_ENCODE_FAILED;
1025 if(per_put_many_bits(po, buf, 8 * mayEncode))
1026 _ASN_ENCODE_FAILED;
1027 buf += mayEncode;
1028 }
1029
1030 _ASN_ENCODED_OK(er);
1031}
1032
1033#endif /* ASN_DISABLE_PER_SUPPORT */
1034
Harald Welte92c45f32010-06-12 18:59:38 +02001035int
1036asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
1037 uint8_t *b, *end;
1038 size_t size;
1039 long l;
1040
1041 /* Sanity checking */
1042 if(!iptr || !iptr->buf || !lptr) {
1043 errno = EINVAL;
1044 return -1;
1045 }
1046
1047 /* Cache the begin/end of the buffer */
1048 b = iptr->buf; /* Start of the INTEGER buffer */
1049 size = iptr->size;
1050 end = b + size; /* Where to stop */
1051
1052 if(size > sizeof(long)) {
1053 uint8_t *end1 = end - 1;
1054 /*
1055 * Slightly more advanced processing,
1056 * able to >sizeof(long) bytes,
1057 * when the actual value is small
1058 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1059 */
1060 /* Skip out the insignificant leading bytes */
1061 for(; b < end1; b++) {
1062 switch(*b) {
1063 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1064 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1065 }
1066 break;
1067 }
1068
1069 size = end - b;
1070 if(size > sizeof(long)) {
1071 /* Still cannot fit the long */
1072 errno = ERANGE;
1073 return -1;
1074 }
1075 }
1076
1077 /* Shortcut processing of a corner case */
1078 if(end == b) {
1079 *lptr = 0;
1080 return 0;
1081 }
1082
1083 /* Perform the sign initialization */
1084 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1085 if((*b >> 7)) l = -1; else l = 0;
1086
1087 /* Conversion engine */
1088 for(; b < end; b++)
1089 l = (l << 8) | *b;
1090
1091 *lptr = l;
1092 return 0;
1093}
1094
1095int
Harald Welte41b85d52015-08-31 08:56:53 +02001096asn_INTEGER2int64(const INTEGER_t *iptr, int64_t *lptr) {
1097 uint8_t *b, *end;
1098 size_t size;
1099 int64_t l;
1100
1101 /* Sanity checking */
1102 if(!iptr || !iptr->buf || !lptr) {
1103 errno = EINVAL;
1104 return -1;
1105 }
1106
1107 /* Cache the begin/end of the buffer */
1108 b = iptr->buf; /* Start of the INTEGER buffer */
1109 size = iptr->size;
1110 end = b + size; /* Where to stop */
1111
1112 if(size > sizeof(int64_t)) {
1113 uint8_t *end1 = end - 1;
1114 /*
1115 * Slightly more advanced processing,
1116 * able to >sizeof(int64_t) bytes,
1117 * when the actual value is small
1118 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1119 */
1120 /* Skip out the insignificant leading bytes */
1121 for(; b < end1; b++) {
1122 switch(*b) {
1123 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1124 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1125 }
1126 break;
1127 }
1128
1129 size = end - b;
1130 if(size > sizeof(int64_t)) {
1131 /* Still cannot fit the int64_t */
1132 errno = ERANGE;
1133 return -1;
1134 }
1135 }
1136
1137 /* Shortcut processing of a corner case */
1138 if(end == b) {
1139 *lptr = 0;
1140 return 0;
1141 }
1142
1143 /* Perform the sign initialization */
1144 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1145 if((*b >> 7)) l = -1; else l = 0;
1146
1147 /* Conversion engine */
1148 for(; b < end; b++)
1149 l = (l << 8) | *b;
1150
1151 *lptr = l;
1152 return 0;
1153}
1154
1155int
Harald Welteec0e2172010-07-20 00:03:44 +02001156asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
1157 uint8_t *b, *end;
1158 unsigned long l;
1159 size_t size;
1160
1161 if(!iptr || !iptr->buf || !lptr) {
1162 errno = EINVAL;
1163 return -1;
1164 }
1165
1166 b = iptr->buf;
1167 size = iptr->size;
1168 end = b + size;
1169
1170 /* If all extra leading bytes are zeroes, ignore them */
1171 for(; size > sizeof(unsigned long); b++, size--) {
1172 if(*b) {
1173 /* Value won't fit unsigned long */
1174 errno = ERANGE;
1175 return -1;
1176 }
1177 }
1178
1179 /* Conversion engine */
1180 for(l = 0; b < end; b++)
1181 l = (l << 8) | *b;
1182
1183 *lptr = l;
1184 return 0;
1185}
1186
1187int
Harald Welte41b85d52015-08-31 08:56:53 +02001188asn_INTEGER2uint64(const INTEGER_t *iptr, uint64_t *lptr) {
1189 uint8_t *b, *end;
1190 uint64_t l;
1191 size_t size;
1192
1193 if(!iptr || !iptr->buf || !lptr) {
1194 errno = EINVAL;
1195 return -1;
1196 }
1197
1198 b = iptr->buf;
1199 size = iptr->size;
1200 end = b + size;
1201
1202 /* If all extra leading bytes are zeroes, ignore them */
1203 for(; size > sizeof(uint64_t); b++, size--) {
1204 if(*b) {
1205 /* Value won't fit unsigned long */
1206 errno = ERANGE;
1207 return -1;
1208 }
1209 }
1210
1211 /* Conversion engine */
1212 for(l = 0; b < end; b++)
1213 l = (l << 8) | *b;
1214
1215 *lptr = l;
1216 return 0;
1217}
1218
1219int
Harald Welteec0e2172010-07-20 00:03:44 +02001220asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1221 uint8_t *buf;
1222 uint8_t *end;
1223 uint8_t *b;
1224 int shr;
1225
1226 if(value <= LONG_MAX)
1227 return asn_long2INTEGER(st, value);
1228
1229 buf = (uint8_t *)MALLOC(1 + sizeof(value));
1230 if(!buf) return -1;
1231
1232 end = buf + (sizeof(value) + 1);
1233 buf[0] = 0;
1234 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
1235 *b = (uint8_t)(value >> shr);
1236
1237 if(st->buf) FREEMEM(st->buf);
1238 st->buf = buf;
1239 st->size = 1 + sizeof(value);
1240
1241 return 0;
1242}
1243
1244int
Harald Welte41b85d52015-08-31 08:56:53 +02001245asn_uint642INTEGER(INTEGER_t *st, uint64_t value) {
1246 uint8_t *buf;
1247 uint8_t *end;
1248 uint8_t *b;
1249 int shr;
1250
1251 if(value <= INT64_MAX)
1252 return asn_int642INTEGER(st, value);
1253
1254 buf = (uint8_t *)MALLOC(1 + sizeof(value));
1255 if(!buf) return -1;
1256
1257 end = buf + (sizeof(value) + 1);
1258 buf[0] = 0;
1259 for(b = buf + 1, shr = (sizeof(value)-1)*8; b < end; shr -= 8, b++)
1260 *b = (uint8_t)(value >> shr);
1261
1262 if(st->buf) FREEMEM(st->buf);
1263 st->buf = buf;
1264 st->size = 1 + sizeof(value);
1265
1266 return 0;
1267}
1268
1269int
1270asn_int642INTEGER(INTEGER_t *st, int64_t value) {
1271 uint8_t *buf, *bp;
1272 uint8_t *p;
1273 uint8_t *pstart;
1274 uint8_t *pend1;
1275 int littleEndian = 1; /* Run-time detection */
1276 int add;
1277
1278 if(!st) {
1279 errno = EINVAL;
1280 return -1;
1281 }
1282
1283 buf = (uint8_t *)MALLOC(sizeof(value));
1284 if(!buf) return -1;
1285
1286 if(*(char *)&littleEndian) {
1287 pstart = (uint8_t *)&value + sizeof(value) - 1;
1288 pend1 = (uint8_t *)&value;
1289 add = -1;
1290 } else {
1291 pstart = (uint8_t *)&value;
1292 pend1 = pstart + sizeof(value) - 1;
1293 add = 1;
1294 }
1295
1296 /*
1297 * If the contents octet consists of more than one octet,
1298 * then bits of the first octet and bit 8 of the second octet:
1299 * a) shall not all be ones; and
1300 * b) shall not all be zero.
1301 */
1302 for(p = pstart; p != pend1; p += add) {
1303 switch(*p) {
1304 case 0x00: if((*(p+add) & 0x80) == 0)
1305 continue;
1306 break;
1307 case 0xff: if((*(p+add) & 0x80))
1308 continue;
1309 break;
1310 }
1311 break;
1312 }
1313 /* Copy the integer body */
1314 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1315 *bp++ = *p;
1316
1317 if(st->buf) FREEMEM(st->buf);
1318 st->buf = buf;
1319 st->size = bp - buf;
1320
1321 return 0;
1322}
1323
1324int
Harald Welte92c45f32010-06-12 18:59:38 +02001325asn_long2INTEGER(INTEGER_t *st, long value) {
1326 uint8_t *buf, *bp;
1327 uint8_t *p;
1328 uint8_t *pstart;
1329 uint8_t *pend1;
1330 int littleEndian = 1; /* Run-time detection */
1331 int add;
1332
1333 if(!st) {
1334 errno = EINVAL;
1335 return -1;
1336 }
1337
1338 buf = (uint8_t *)MALLOC(sizeof(value));
1339 if(!buf) return -1;
1340
1341 if(*(char *)&littleEndian) {
1342 pstart = (uint8_t *)&value + sizeof(value) - 1;
1343 pend1 = (uint8_t *)&value;
1344 add = -1;
1345 } else {
1346 pstart = (uint8_t *)&value;
1347 pend1 = pstart + sizeof(value) - 1;
1348 add = 1;
1349 }
1350
1351 /*
1352 * If the contents octet consists of more than one octet,
1353 * then bits of the first octet and bit 8 of the second octet:
1354 * a) shall not all be ones; and
1355 * b) shall not all be zero.
1356 */
1357 for(p = pstart; p != pend1; p += add) {
1358 switch(*p) {
1359 case 0x00: if((*(p+add) & 0x80) == 0)
1360 continue;
1361 break;
1362 case 0xff: if((*(p+add) & 0x80))
1363 continue;
1364 break;
1365 }
1366 break;
1367 }
1368 /* Copy the integer body */
1369 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1370 *bp++ = *p;
1371
1372 if(st->buf) FREEMEM(st->buf);
1373 st->buf = buf;
1374 st->size = bp - buf;
1375
1376 return 0;
1377}
Harald Welte41b85d52015-08-31 08:56:53 +02001378
1379/*
1380 * This function is going to be DEPRECATED soon.
1381 */
1382enum asn_strtol_result_e
1383asn_strtol(const char *str, const char *end, long *lp) {
1384 const char *endp = end;
1385
1386 switch(asn_strtol_lim(str, &endp, lp)) {
1387 case ASN_STRTOL_ERROR_RANGE:
1388 return ASN_STRTOL_ERROR_RANGE;
1389 case ASN_STRTOL_ERROR_INVAL:
1390 return ASN_STRTOL_ERROR_INVAL;
1391 case ASN_STRTOL_EXPECT_MORE:
1392 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1393 case ASN_STRTOL_OK:
1394 return ASN_STRTOL_OK;
1395 case ASN_STRTOL_EXTRA_DATA:
1396 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1397 }
1398
1399 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1400}
1401
1402/*
1403 * Parse the number in the given string until the given *end position,
1404 * returning the position after the last parsed character back using the
1405 * same (*end) pointer.
1406 * WARNING: This behavior is different from the standard strtol(3).
1407 */
1408enum asn_strtol_result_e
1409asn_strtol_lim(const char *str, const char **end, long *lp) {
1410 int sign = 1;
1411 long l;
1412
1413 const long upper_boundary = LONG_MAX / 10;
1414 long last_digit_max = LONG_MAX % 10;
1415
1416 if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
1417
1418 switch(*str) {
1419 case '-':
1420 last_digit_max++;
1421 sign = -1;
1422 case '+':
1423 str++;
1424 if(str >= *end) {
1425 *end = str;
1426 return ASN_STRTOL_EXPECT_MORE;
1427 }
1428 }
1429
1430 for(l = 0; str < (*end); str++) {
1431 switch(*str) {
1432 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1433 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1434 int d = *str - '0';
1435 if(l < upper_boundary) {
1436 l = l * 10 + d;
1437 } else if(l == upper_boundary) {
1438 if(d <= last_digit_max) {
1439 if(sign > 0) {
1440 l = l * 10 + d;
1441 } else {
1442 sign = 1;
1443 l = -l * 10 - d;
1444 }
1445 } else {
1446 *end = str;
1447 return ASN_STRTOL_ERROR_RANGE;
1448 }
1449 } else {
1450 *end = str;
1451 return ASN_STRTOL_ERROR_RANGE;
1452 }
1453 }
1454 continue;
1455 default:
1456 *end = str;
1457 *lp = sign * l;
1458 return ASN_STRTOL_EXTRA_DATA;
1459 }
1460 }
1461
1462 *end = str;
1463 *lp = sign * l;
1464 return ASN_STRTOL_OK;
1465}
1466