blob: c74c5b4d90d2d855bb6705a0d0cf7f30a4702dae [file] [log] [blame]
Harald Welted523a692015-08-30 23:11:19 +02001/*-
2 * Copyright (c) 2003-2014 Lev Walkin <vlm@lionet.info>.
3 * 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 */
14static const ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
15 (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,
27#ifdef ASN_DISABLE_PER_SUPPORT
28 0,
29 0,
30 0,
31 0,
32#else
33 INTEGER_decode_uper, /* Unaligned PER decoder */
34 INTEGER_encode_uper, /* Unaligned PER encoder */
35 INTEGER_decode_aper,
36 INTEGER_encode_aper,
37#endif /* ASN_DISABLE_PER_SUPPORT */
38 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
113INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
114 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;
118 signed long value;
119 ssize_t wrote = 0;
120 char *p;
121 int ret;
122
123 if(specs && specs->field_unsigned)
124 ret = asn_INTEGER2ulong(st, (unsigned long *)&value);
125 else
126 ret = asn_INTEGER2long(st, &value);
127
128 /* Simple case: the integer size is small */
129 if(ret == 0) {
130 const asn_INTEGER_enum_map_t *el;
131 size_t scrsize;
132 char *scr;
133
134 el = (value >= 0 || !specs || !specs->field_unsigned)
135 ? INTEGER_map_value2enum(specs, value) : 0;
136 if(el) {
137 scrsize = el->enum_len + 32;
138 scr = (char *)alloca(scrsize);
139 if(plainOrXER == 0)
140 ret = snprintf(scr, scrsize,
141 "%ld (%s)", value, el->enum_name);
142 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;
153 ret = snprintf(scr, scrsize,
154 (specs && specs->field_unsigned)
155 ?"%lu":"%ld", value);
156 }
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++) {
173 const char * const h2c = "0123456789ABCDEF";
174 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;
215 const asn_INTEGER_enum_map_t *vemap;
216 const unsigned int *evmap;
217};
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) {
239 const asn_INTEGER_enum_map_t *el_found;
240 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;
314 long dec_value;
315 long hex_value = 0;
316 const char *lp;
317 const char *lstart = (const char *)chunk_buf;
318 const char *lstop = lstart + chunk_size;
319 enum {
320 ST_LEADSPACE,
321 ST_SKIPSPHEX,
322 ST_WAITDIGITS,
323 ST_DIGITS,
324 ST_DIGITS_TRAILSPACE,
325 ST_HEXDIGIT1,
326 ST_HEXDIGIT2,
327 ST_HEXDIGITS_TRAILSPACE,
328 ST_HEXCOLON,
329 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;
334
335 if(chunk_size)
336 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
337 (long)chunk_size, *lstart, lstop[-1]);
338
339 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
340 return XPBD_SYSTEM_FAILURE;
341
342 /*
343 * We may have received a tag here. It will be processed inline.
344 * Use strtoul()-like code and serialize the result.
345 */
346 for(lp = lstart; lp < lstop; lp++) {
347 int lv = *lp;
348 switch(lv) {
349 case 0x09: case 0x0a: case 0x0d: case 0x20:
350 switch(state) {
351 case ST_LEADSPACE:
352 case ST_DIGITS_TRAILSPACE:
353 case ST_HEXDIGITS_TRAILSPACE:
354 case ST_SKIPSPHEX:
355 continue;
356 case ST_DIGITS:
357 dec_value_end = lp;
358 state = ST_DIGITS_TRAILSPACE;
359 continue;
360 case ST_HEXCOLON:
361 state = ST_HEXDIGITS_TRAILSPACE;
362 continue;
363 default:
364 break;
365 }
366 break;
367 case 0x2d: /* '-' */
368 if(state == ST_LEADSPACE) {
369 dec_value = 0;
370 dec_value_start = lp;
371 state = ST_WAITDIGITS;
372 continue;
373 }
374 break;
375 case 0x2b: /* '+' */
376 if(state == ST_LEADSPACE) {
377 dec_value = 0;
378 dec_value_start = lp;
379 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) {
386 case ST_DIGITS: continue;
387 case ST_SKIPSPHEX: /* Fall through */
388 case ST_HEXDIGIT1:
389 hex_value = (lv - 0x30) << 4;
390 state = ST_HEXDIGIT2;
391 continue;
392 case ST_HEXDIGIT2:
393 hex_value += (lv - 0x30);
394 state = ST_HEXCOLON;
395 st->buf[st->size++] = (uint8_t)hex_value;
396 continue;
397 case ST_HEXCOLON:
398 return XPBD_BROKEN_ENCODING;
399 case ST_LEADSPACE:
400 dec_value = 0;
401 dec_value_start = lp;
402 /* FALL THROUGH */
403 case ST_WAITDIGITS:
404 state = ST_DIGITS;
405 continue;
406 default:
407 break;
408 }
409 break;
410 case 0x3c: /* '<', start of XML encoded enumeration */
411 if(state == ST_LEADSPACE) {
412 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) {
417 ASN_DEBUG("Found \"%s\" => %lld",
418 el->enum_name, el->nat_value);
419 dec_value = el->nat_value;
420 state = ST_END_ENUM;
421 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");
438 state = ST_SKIPSPHEX;
439 dec_value_start = 0;
440 lp = lstart - 1;
441 continue;
442 } else {
443 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
444 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:
451 case ST_LEADSPACE: /* Fall through */
452 case ST_HEXDIGIT1:
453 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
454 hex_value += 10;
455 hex_value <<= 4;
456 state = ST_HEXDIGIT2;
457 continue;
458 case ST_HEXDIGIT2:
459 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
460 hex_value += 10;
461 st->buf[st->size++] = (uint8_t)hex_value;
462 state = ST_HEXCOLON;
463 continue;
464 case ST_DIGITS:
465 ASN_DEBUG("INTEGER re-evaluate as hex form");
466 state = ST_SKIPSPHEX;
467 dec_value_start = 0;
468 lp = lstart - 1;
469 continue;
470 default:
471 break;
472 }
473 break;
474 }
475
476 /* Found extra non-numeric stuff */
477 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
478 lv, (long)(lp - lstart));
479 state = ST_UNEXPECTED;
480 break;
481 }
482
483 switch(state) {
484 case ST_END_ENUM:
485 /* Got a complete and valid enumeration encoded as a tag. */
486 break;
487 case ST_DIGITS:
488 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 }
502 break;
503 case ST_HEXCOLON:
504 case ST_HEXDIGITS_TRAILSPACE:
505 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;
511 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 */
518 }
519
520 /*
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))
525 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
559#ifndef ASN_DISABLE_PER_SUPPORT
560
561asn_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) {
564 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
565 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);
587 st->buf = 0;
588 st->size = 0;
589 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;
599 }
600 }
601
602 /* 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
713 /* 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) {
718 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;
747 } else {
748 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;
771 }
772 return rval;
773 } else {
774 _ASN_DECODE_FAILED;
775 }
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 */
787 len = aper_get_length(pd, -1, -1, &repeat);
788 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) {
818 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
819 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;
835 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 }
848 ASN_DEBUG("Value %lu (%02x/%d) lb %llu ub %llu %s",
849 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 }
865 ASN_DEBUG("Value %ld (%02x/%d) lb %lld ub %lld %s",
866 value, st->buf[0], st->size,
867 ct->lower_bound, ct->upper_bound,
868 inext ? "ext" : "fix");
869 }
870 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
880 /* X.691-11/2008, #13.2.2, test if constrained whole number */
881 if(ct && ct->range_bits >= 0) {
882 /* #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;
888 _ASN_ENCODED_OK(er);
889 }
890
891 if(ct && ct->lower_bound) {
892 ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound);
893 /* 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
909asn_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
1032#endif /* ASN_DISABLE_PER_SUPPORT */
1033
1034int
1035asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
1036 uint8_t *b, *end;
1037 size_t size;
1038 long l;
1039
1040 /* Sanity checking */
1041 if(!iptr || !iptr->buf || !lptr) {
1042 errno = EINVAL;
1043 return -1;
1044 }
1045
1046 /* Cache the begin/end of the buffer */
1047 b = iptr->buf; /* Start of the INTEGER buffer */
1048 size = iptr->size;
1049 end = b + size; /* Where to stop */
1050
1051 if(size > sizeof(long)) {
1052 uint8_t *end1 = end - 1;
1053 /*
1054 * Slightly more advanced processing,
1055 * able to >sizeof(long) bytes,
1056 * when the actual value is small
1057 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1058 */
1059 /* Skip out the insignificant leading bytes */
1060 for(; b < end1; b++) {
1061 switch(*b) {
1062 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1063 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1064 }
1065 break;
1066 }
1067
1068 size = end - b;
1069 if(size > sizeof(long)) {
1070 /* Still cannot fit the long */
1071 errno = ERANGE;
1072 return -1;
1073 }
1074 }
1075
1076 /* Shortcut processing of a corner case */
1077 if(end == b) {
1078 *lptr = 0;
1079 return 0;
1080 }
1081
1082 /* Perform the sign initialization */
1083 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1084 if((*b >> 7)) l = -1; else l = 0;
1085
1086 /* Conversion engine */
1087 for(; b < end; b++)
1088 l = (l << 8) | *b;
1089
1090 *lptr = l;
1091 return 0;
1092}
1093
1094int
1095asn_INTEGER2int64(const INTEGER_t *iptr, int64_t *lptr) {
1096 uint8_t *b, *end;
1097 size_t size;
1098 int64_t l;
1099
1100 /* Sanity checking */
1101 if(!iptr || !iptr->buf || !lptr) {
1102 errno = EINVAL;
1103 return -1;
1104 }
1105
1106 /* Cache the begin/end of the buffer */
1107 b = iptr->buf; /* Start of the INTEGER buffer */
1108 size = iptr->size;
1109 end = b + size; /* Where to stop */
1110
1111 if(size > sizeof(int64_t)) {
1112 uint8_t *end1 = end - 1;
1113 /*
1114 * Slightly more advanced processing,
1115 * able to >sizeof(int64_t) bytes,
1116 * when the actual value is small
1117 * (0x0000000000abcdef would yield a fine 0x00abcdef)
1118 */
1119 /* Skip out the insignificant leading bytes */
1120 for(; b < end1; b++) {
1121 switch(*b) {
1122 case 0x00: if((b[1] & 0x80) == 0) continue; break;
1123 case 0xff: if((b[1] & 0x80) != 0) continue; break;
1124 }
1125 break;
1126 }
1127
1128 size = end - b;
1129 if(size > sizeof(int64_t)) {
1130 /* Still cannot fit the int64_t */
1131 errno = ERANGE;
1132 return -1;
1133 }
1134 }
1135
1136 /* Shortcut processing of a corner case */
1137 if(end == b) {
1138 *lptr = 0;
1139 return 0;
1140 }
1141
1142 /* Perform the sign initialization */
1143 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
1144 if((*b >> 7)) l = -1; else l = 0;
1145
1146 /* Conversion engine */
1147 for(; b < end; b++)
1148 l = (l << 8) | *b;
1149
1150 *lptr = l;
1151 return 0;
1152}
1153
1154int
1155asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
1156 uint8_t *b, *end;
1157 unsigned long l;
1158 size_t size;
1159
1160 if(!iptr || !iptr->buf || !lptr) {
1161 errno = EINVAL;
1162 return -1;
1163 }
1164
1165 b = iptr->buf;
1166 size = iptr->size;
1167 end = b + size;
1168
1169 /* If all extra leading bytes are zeroes, ignore them */
1170 for(; size > sizeof(unsigned long); b++, size--) {
1171 if(*b) {
1172 /* Value won't fit unsigned long */
1173 errno = ERANGE;
1174 return -1;
1175 }
1176 }
1177
1178 /* Conversion engine */
1179 for(l = 0; b < end; b++)
1180 l = (l << 8) | *b;
1181
1182 *lptr = l;
1183 return 0;
1184}
1185
1186int
1187asn_INTEGER2uint64(const INTEGER_t *iptr, uint64_t *lptr) {
1188 uint8_t *b, *end;
1189 uint64_t l;
1190 size_t size;
1191
1192 if(!iptr || !iptr->buf || !lptr) {
1193 errno = EINVAL;
1194 return -1;
1195 }
1196
1197 b = iptr->buf;
1198 size = iptr->size;
1199 end = b + size;
1200
1201 /* If all extra leading bytes are zeroes, ignore them */
1202 for(; size > sizeof(uint64_t); b++, size--) {
1203 if(*b) {
1204 /* Value won't fit unsigned long */
1205 errno = ERANGE;
1206 return -1;
1207 }
1208 }
1209
1210 /* Conversion engine */
1211 for(l = 0; b < end; b++)
1212 l = (l << 8) | *b;
1213
1214 *lptr = l;
1215 return 0;
1216}
1217
1218int
1219asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
1220 uint8_t *buf;
1221 uint8_t *end;
1222 uint8_t *b;
1223 int shr;
1224
1225 if(value <= LONG_MAX)
1226 return asn_long2INTEGER(st, value);
1227
1228 buf = (uint8_t *)MALLOC(1 + sizeof(value));
1229 if(!buf) return -1;
1230
1231 end = buf + (sizeof(value) + 1);
1232 buf[0] = 0;
1233 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
1234 *b = (uint8_t)(value >> shr);
1235
1236 if(st->buf) FREEMEM(st->buf);
1237 st->buf = buf;
1238 st->size = 1 + sizeof(value);
1239
1240 return 0;
1241}
1242
1243int
1244asn_uint642INTEGER(INTEGER_t *st, uint64_t value) {
1245 uint8_t *buf;
1246 uint8_t *end;
1247 uint8_t *b;
1248 int shr;
1249
1250 if(value <= INT64_MAX)
1251 return asn_int642INTEGER(st, value);
1252
1253 buf = (uint8_t *)MALLOC(1 + sizeof(value));
1254 if(!buf) return -1;
1255
1256 end = buf + (sizeof(value) + 1);
1257 buf[0] = 0;
1258 for(b = buf + 1, shr = (sizeof(value)-1)*8; b < end; shr -= 8, b++)
1259 *b = (uint8_t)(value >> shr);
1260
1261 if(st->buf) FREEMEM(st->buf);
1262 st->buf = buf;
1263 st->size = 1 + sizeof(value);
1264
1265 return 0;
1266}
1267
1268int
1269asn_int642INTEGER(INTEGER_t *st, int64_t value) {
1270 uint8_t *buf, *bp;
1271 uint8_t *p;
1272 uint8_t *pstart;
1273 uint8_t *pend1;
1274 int littleEndian = 1; /* Run-time detection */
1275 int add;
1276
1277 if(!st) {
1278 errno = EINVAL;
1279 return -1;
1280 }
1281
1282 buf = (uint8_t *)MALLOC(sizeof(value));
1283 if(!buf) return -1;
1284
1285 if(*(char *)&littleEndian) {
1286 pstart = (uint8_t *)&value + sizeof(value) - 1;
1287 pend1 = (uint8_t *)&value;
1288 add = -1;
1289 } else {
1290 pstart = (uint8_t *)&value;
1291 pend1 = pstart + sizeof(value) - 1;
1292 add = 1;
1293 }
1294
1295 /*
1296 * If the contents octet consists of more than one octet,
1297 * then bits of the first octet and bit 8 of the second octet:
1298 * a) shall not all be ones; and
1299 * b) shall not all be zero.
1300 */
1301 for(p = pstart; p != pend1; p += add) {
1302 switch(*p) {
1303 case 0x00: if((*(p+add) & 0x80) == 0)
1304 continue;
1305 break;
1306 case 0xff: if((*(p+add) & 0x80))
1307 continue;
1308 break;
1309 }
1310 break;
1311 }
1312 /* Copy the integer body */
1313 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1314 *bp++ = *p;
1315
1316 if(st->buf) FREEMEM(st->buf);
1317 st->buf = buf;
1318 st->size = bp - buf;
1319
1320 return 0;
1321}
1322
1323int
1324asn_long2INTEGER(INTEGER_t *st, long value) {
1325 uint8_t *buf, *bp;
1326 uint8_t *p;
1327 uint8_t *pstart;
1328 uint8_t *pend1;
1329 int littleEndian = 1; /* Run-time detection */
1330 int add;
1331
1332 if(!st) {
1333 errno = EINVAL;
1334 return -1;
1335 }
1336
1337 buf = (uint8_t *)MALLOC(sizeof(value));
1338 if(!buf) return -1;
1339
1340 if(*(char *)&littleEndian) {
1341 pstart = (uint8_t *)&value + sizeof(value) - 1;
1342 pend1 = (uint8_t *)&value;
1343 add = -1;
1344 } else {
1345 pstart = (uint8_t *)&value;
1346 pend1 = pstart + sizeof(value) - 1;
1347 add = 1;
1348 }
1349
1350 /*
1351 * If the contents octet consists of more than one octet,
1352 * then bits of the first octet and bit 8 of the second octet:
1353 * a) shall not all be ones; and
1354 * b) shall not all be zero.
1355 */
1356 for(p = pstart; p != pend1; p += add) {
1357 switch(*p) {
1358 case 0x00: if((*(p+add) & 0x80) == 0)
1359 continue;
1360 break;
1361 case 0xff: if((*(p+add) & 0x80))
1362 continue;
1363 break;
1364 }
1365 break;
1366 }
1367 /* Copy the integer body */
1368 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
1369 *bp++ = *p;
1370
1371 if(st->buf) FREEMEM(st->buf);
1372 st->buf = buf;
1373 st->size = bp - buf;
1374
1375 return 0;
1376}
1377
1378/*
1379 * This function is going to be DEPRECATED soon.
1380 */
1381enum asn_strtol_result_e
1382asn_strtol(const char *str, const char *end, long *lp) {
1383 const char *endp = end;
1384
1385 switch(asn_strtol_lim(str, &endp, lp)) {
1386 case ASN_STRTOL_ERROR_RANGE:
1387 return ASN_STRTOL_ERROR_RANGE;
1388 case ASN_STRTOL_ERROR_INVAL:
1389 return ASN_STRTOL_ERROR_INVAL;
1390 case ASN_STRTOL_EXPECT_MORE:
1391 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1392 case ASN_STRTOL_OK:
1393 return ASN_STRTOL_OK;
1394 case ASN_STRTOL_EXTRA_DATA:
1395 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1396 }
1397
1398 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
1399}
1400
1401/*
1402 * Parse the number in the given string until the given *end position,
1403 * returning the position after the last parsed character back using the
1404 * same (*end) pointer.
1405 * WARNING: This behavior is different from the standard strtol(3).
1406 */
1407enum asn_strtol_result_e
1408asn_strtol_lim(const char *str, const char **end, long *lp) {
1409 int sign = 1;
1410 long l;
1411
1412 const long upper_boundary = LONG_MAX / 10;
1413 long last_digit_max = LONG_MAX % 10;
1414
1415 if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
1416
1417 switch(*str) {
1418 case '-':
1419 last_digit_max++;
1420 sign = -1;
1421 case '+':
1422 str++;
1423 if(str >= *end) {
1424 *end = str;
1425 return ASN_STRTOL_EXPECT_MORE;
1426 }
1427 }
1428
1429 for(l = 0; str < (*end); str++) {
1430 switch(*str) {
1431 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1432 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1433 int d = *str - '0';
1434 if(l < upper_boundary) {
1435 l = l * 10 + d;
1436 } else if(l == upper_boundary) {
1437 if(d <= last_digit_max) {
1438 if(sign > 0) {
1439 l = l * 10 + d;
1440 } else {
1441 sign = 1;
1442 l = -l * 10 - d;
1443 }
1444 } else {
1445 *end = str;
1446 return ASN_STRTOL_ERROR_RANGE;
1447 }
1448 } else {
1449 *end = str;
1450 return ASN_STRTOL_ERROR_RANGE;
1451 }
1452 }
1453 continue;
1454 default:
1455 *end = str;
1456 *lp = sign * l;
1457 return ASN_STRTOL_EXTRA_DATA;
1458 }
1459 }
1460
1461 *end = str;
1462 *lp = sign * l;
1463 return ASN_STRTOL_OK;
1464}
1465