2  * Copyright (c) 2003-2014 Lev Walkin <vlm@lionet.info>.
 
   4  * Redistribution and modifications are permitted subject to BSD license.
 
   6 #include <asn_internal.h>
 
   8 #include <asn_codecs_prim.h>    /* Encoder and decoder of a primitive type */
 
  12  * INTEGER basic type description.
 
  14 static const ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
 
  15         (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
 
  17 asn_TYPE_descriptor_t asn_DEF_INTEGER = {
 
  20         ASN__PRIMITIVE_TYPE_free,
 
  22         asn_generic_no_constraint,
 
  27 #ifdef  ASN_DISABLE_PER_SUPPORT
 
  31         INTEGER_decode_uper,    /* Unaligned PER decoder */
 
  32         INTEGER_encode_uper,    /* Unaligned PER encoder */
 
  33 #endif  /* ASN_DISABLE_PER_SUPPORT */
 
  34         0, /* Use generic outmost tag fetcher */
 
  36         sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
 
  37         asn_DEF_INTEGER_tags,   /* Same as above */
 
  38         sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
 
  39         0,      /* No PER visible constraints */
 
  40         0, 0,   /* No members */
 
  45  * Encode INTEGER type using DER.
 
  48 INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
 
  49         int tag_mode, ber_tlv_tag_t tag,
 
  50         asn_app_consume_bytes_f *cb, void *app_key) {
 
  51         INTEGER_t *st = (INTEGER_t *)sptr;
 
  53         ASN_DEBUG("%s %s as INTEGER (tm=%d)",
 
  54                 cb?"Encoding":"Estimating", td->name, tag_mode);
 
  57          * Canonicalize integer in the buffer.
 
  58          * (Remove too long sign extension, remove some first 0x00 bytes)
 
  61                 uint8_t *buf = st->buf;
 
  62                 uint8_t *end1 = buf + st->size - 1;
 
  65                 /* Compute the number of superfluous leading bytes */
 
  66                 for(; buf < end1; buf++) {
 
  68                          * If the contents octets of an integer value encoding
 
  69                          * consist of more than one octet, then the bits of the
 
  70                          * first octet and bit 8 of the second octet:
 
  71                          * a) shall not all be ones; and
 
  72                          * b) shall not all be zero.
 
  75                         case 0x00: if((buf[1] & 0x80) == 0)
 
  78                         case 0xff: if((buf[1] & 0x80))
 
  85                 /* Remove leading superfluous bytes from the integer */
 
  86                 shift = buf - st->buf;
 
  88                         uint8_t *nb = st->buf;
 
  91                         st->size -= shift;      /* New size, minus bad bytes */
 
  94                         for(; nb < end; nb++, buf++)
 
 100         return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
 
 103 static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop);
 
 106  * INTEGER specific human-readable output.
 
 109 INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
 
 110         asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
 
 111         char scratch[32];       /* Enough for 64-bit integer */
 
 112         uint8_t *buf = st->buf;
 
 113         uint8_t *buf_end = st->buf + st->size;
 
 119         if(specs && specs->field_unsigned)
 
 120                 ret = asn_INTEGER2ulong(st, (unsigned long *)&value);
 
 122                 ret = asn_INTEGER2long(st, &value);
 
 124         /* Simple case: the integer size is small */
 
 126                 const asn_INTEGER_enum_map_t *el;
 
 130                 el = (value >= 0 || !specs || !specs->field_unsigned)
 
 131                         ? INTEGER_map_value2enum(specs, value) : 0;
 
 133                         scrsize = el->enum_len + 32;
 
 134                         scr = (char *)alloca(scrsize);
 
 136                                 ret = snprintf(scr, scrsize,
 
 137                                         "%ld (%s)", value, el->enum_name);
 
 139                                 ret = snprintf(scr, scrsize,
 
 140                                         "<%s/>", el->enum_name);
 
 141                 } else if(plainOrXER && specs && specs->strict_enumeration) {
 
 142                         ASN_DEBUG("ASN.1 forbids dealing with "
 
 143                                 "unknown value of ENUMERATED type");
 
 147                         scrsize = sizeof(scratch);
 
 149                         ret = snprintf(scr, scrsize,
 
 150                                 (specs && specs->field_unsigned)
 
 151                                 ?"%lu":"%ld", value);
 
 153                 assert(ret > 0 && (size_t)ret < scrsize);
 
 154                 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
 
 155         } else if(plainOrXER && specs && specs->strict_enumeration) {
 
 157                  * Here and earlier, we cannot encode the ENUMERATED values
 
 158                  * if there is no corresponding identifier.
 
 160                 ASN_DEBUG("ASN.1 forbids dealing with "
 
 161                         "unknown value of ENUMERATED type");
 
 166         /* Output in the long xx:yy:zz... format */
 
 167         /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
 
 168         for(p = scratch; buf < buf_end; buf++) {
 
 169                 const char * const h2c = "0123456789ABCDEF";
 
 170                 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
 
 172                         if(cb(scratch, p - scratch, app_key) < 0)
 
 174                         wrote += p - scratch;
 
 177                 *p++ = h2c[*buf >> 4];
 
 178                 *p++ = h2c[*buf & 0x0F];
 
 179                 *p++ = 0x3a;    /* ":" */
 
 182                 p--;    /* Remove the last ":" */
 
 184         wrote += p - scratch;
 
 185         return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
 
 189  * INTEGER specific human-readable output.
 
 192 INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
 
 193         asn_app_consume_bytes_f *cb, void *app_key) {
 
 194         const INTEGER_t *st = (const INTEGER_t *)sptr;
 
 201                 ret = cb("<absent>", 8, app_key);
 
 203                 ret = INTEGER__dump(td, st, cb, app_key, 0);
 
 205         return (ret < 0) ? -1 : 0;
 
 211         const asn_INTEGER_enum_map_t *vemap;
 
 212         const unsigned int *evmap;
 
 215 INTEGER__compar_enum2value(const void *kp, const void *am) {
 
 216         const struct e2v_key *key = (const struct e2v_key *)kp;
 
 217         const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
 
 218         const char *ptr, *end, *name;
 
 220         /* Remap the element (sort by different criterion) */
 
 221         el = key->vemap + key->evmap[el - key->vemap];
 
 223         /* Compare strings */
 
 224         for(ptr = key->start, end = key->stop, name = el->enum_name;
 
 225                         ptr < end; ptr++, name++) {
 
 227                         return *(const unsigned char *)ptr
 
 228                                 - *(const unsigned char *)name;
 
 230         return name[0] ? -1 : 0;
 
 233 static const asn_INTEGER_enum_map_t *
 
 234 INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
 
 235         const asn_INTEGER_enum_map_t *el_found;
 
 236         int count = specs ? specs->map_count : 0;
 
 240         if(!count) return NULL;
 
 242         /* Guaranteed: assert(lstart < lstop); */
 
 243         /* Figure out the tag name */
 
 244         for(lstart++, lp = lstart; lp < lstop; lp++) {
 
 246                 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
 
 247                 case 0x2f: /* '/' */ case 0x3e: /* '>' */
 
 254         if(lp == lstop) return NULL;    /* No tag found */
 
 259         key.vemap = specs->value2enum;
 
 260         key.evmap = specs->enum2value;
 
 261         el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
 
 262                 specs->value2enum, count, sizeof(specs->value2enum[0]),
 
 263                 INTEGER__compar_enum2value);
 
 265                 /* Remap enum2value into value2enum */
 
 266                 el_found = key.vemap + key.evmap[el_found - key.vemap];
 
 272 INTEGER__compar_value2enum(const void *kp, const void *am) {
 
 273         long a = *(const long *)kp;
 
 274         const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
 
 275         long b = el->nat_value;
 
 277         else if(a == b) return 0;
 
 281 const asn_INTEGER_enum_map_t *
 
 282 INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
 
 283         int count = specs ? specs->map_count : 0;
 
 285         return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
 
 286                 count, sizeof(specs->value2enum[0]),
 
 287                 INTEGER__compar_value2enum);
 
 291 INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
 
 292         void *p = MALLOC(min_size + 1);
 
 305  * Decode the chunk of XML text encoding INTEGER.
 
 307 static enum xer_pbd_rval
 
 308 INTEGER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
 
 309         INTEGER_t *st = (INTEGER_t *)sptr;
 
 313         const char *lstart = (const char *)chunk_buf;
 
 314         const char *lstop = lstart + chunk_size;
 
 320                 ST_DIGITS_TRAILSPACE,
 
 323                 ST_HEXDIGITS_TRAILSPACE,
 
 327         } state = ST_LEADSPACE;
 
 328         const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
 
 329         const char *dec_value_end = 0;
 
 332                 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
 
 333                         (long)chunk_size, *lstart, lstop[-1]);
 
 335         if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
 
 336                 return XPBD_SYSTEM_FAILURE;
 
 339          * We may have received a tag here. It will be processed inline.
 
 340          * Use strtoul()-like code and serialize the result.
 
 342         for(lp = lstart; lp < lstop; lp++) {
 
 345                 case 0x09: case 0x0a: case 0x0d: case 0x20:
 
 348                         case ST_DIGITS_TRAILSPACE:
 
 349                         case ST_HEXDIGITS_TRAILSPACE:
 
 354                                 state = ST_DIGITS_TRAILSPACE;
 
 357                                 state = ST_HEXDIGITS_TRAILSPACE;
 
 364                         if(state == ST_LEADSPACE) {
 
 366                                 dec_value_start = lp;
 
 367                                 state = ST_WAITDIGITS;
 
 372                         if(state == ST_LEADSPACE) {
 
 374                                 dec_value_start = lp;
 
 375                                 state = ST_WAITDIGITS;
 
 379                 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
 
 380                 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
 
 382                         case ST_DIGITS: continue;
 
 383                         case ST_SKIPSPHEX:      /* Fall through */
 
 385                                 hex_value = (lv - 0x30) << 4;
 
 386                                 state = ST_HEXDIGIT2;
 
 389                                 hex_value += (lv - 0x30);
 
 391                                 st->buf[st->size++] = (uint8_t)hex_value;
 
 394                                 return XPBD_BROKEN_ENCODING;
 
 397                                 dec_value_start = lp;
 
 406                 case 0x3c:      /* '<', start of XML encoded enumeration */
 
 407                         if(state == ST_LEADSPACE) {
 
 408                                 const asn_INTEGER_enum_map_t *el;
 
 409                                 el = INTEGER_map_enum2value(
 
 410                                         (asn_INTEGER_specifics_t *)
 
 411                                         td->specifics, lstart, lstop);
 
 413                                         ASN_DEBUG("Found \"%s\" => %ld",
 
 414                                                 el->enum_name, el->nat_value);
 
 415                                         dec_value = el->nat_value;
 
 420                                 ASN_DEBUG("Unknown identifier for INTEGER");
 
 422                         return XPBD_BROKEN_ENCODING;
 
 424                         if(state == ST_HEXCOLON) {
 
 425                                 /* This colon is expected */
 
 426                                 state = ST_HEXDIGIT1;
 
 428                         } else if(state == ST_DIGITS) {
 
 429                                 /* The colon here means that we have
 
 430                                  * decoded the first two hexadecimal
 
 431                                  * places as a decimal value.
 
 432                                  * Switch decoding mode. */
 
 433                                 ASN_DEBUG("INTEGER re-evaluate as hex form");
 
 434                                 state = ST_SKIPSPHEX;
 
 439                                 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
 
 443                 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
 
 444                 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
 
 447                         case ST_LEADSPACE: /* Fall through */
 
 449                                 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
 
 452                                 state = ST_HEXDIGIT2;
 
 455                                 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
 
 457                                 st->buf[st->size++] = (uint8_t)hex_value;
 
 461                                 ASN_DEBUG("INTEGER re-evaluate as hex form");
 
 462                                 state = ST_SKIPSPHEX;
 
 472                 /* Found extra non-numeric stuff */
 
 473                 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
 
 474                         lv, (long)(lp - lstart));
 
 475                 state = ST_UNEXPECTED;
 
 481                 /* Got a complete and valid enumeration encoded as a tag. */
 
 484                 dec_value_end = lstop;
 
 486         case ST_DIGITS_TRAILSPACE:
 
 487                 /* The last symbol encountered was a digit. */
 
 488                 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
 
 491                 case ASN_STRTOL_ERROR_RANGE:
 
 492                         return XPBD_DECODER_LIMIT;
 
 493                 case ASN_STRTOL_ERROR_INVAL:
 
 494                 case ASN_STRTOL_EXPECT_MORE:
 
 495                 case ASN_STRTOL_EXTRA_DATA:
 
 496                         return XPBD_BROKEN_ENCODING;
 
 500         case ST_HEXDIGITS_TRAILSPACE:
 
 501                 st->buf[st->size] = 0;  /* Just in case termination */
 
 502                 return XPBD_BODY_CONSUMED;
 
 506                 return XPBD_BROKEN_ENCODING;
 
 508                 /* Content not found */
 
 509                 return XPBD_NOT_BODY_IGNORE;
 
 512                 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
 
 513                 return XPBD_BROKEN_ENCODING;    /* No digits */
 
 517          * Convert the result of parsing of enumeration or a straight
 
 518          * decimal value into a BER representation.
 
 520         if(asn_long2INTEGER(st, dec_value))
 
 521                 return XPBD_SYSTEM_FAILURE;
 
 523         return XPBD_BODY_CONSUMED;
 
 527 INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
 
 528         asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
 
 529                 const void *buf_ptr, size_t size) {
 
 531         return xer_decode_primitive(opt_codec_ctx, td,
 
 532                 sptr, sizeof(INTEGER_t), opt_mname,
 
 533                 buf_ptr, size, INTEGER__xer_body_decode);
 
 537 INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
 
 538         int ilevel, enum xer_encoder_flags_e flags,
 
 539                 asn_app_consume_bytes_f *cb, void *app_key) {
 
 540         const INTEGER_t *st = (const INTEGER_t *)sptr;
 
 549         er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
 
 550         if(er.encoded < 0) ASN__ENCODE_FAILED;
 
 555 #ifndef ASN_DISABLE_PER_SUPPORT
 
 558 INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
 
 559         asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
 
 560         asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
 
 561         asn_dec_rval_t rval = { RC_OK, 0 };
 
 562         INTEGER_t *st = (INTEGER_t *)*sptr;
 
 563         asn_per_constraint_t *ct;
 
 569                 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
 
 570                 if(!st) ASN__DECODE_FAILED;
 
 573         if(!constraints) constraints = td->per_constraints;
 
 574         ct = constraints ? &constraints->value : 0;
 
 576         if(ct && ct->flags & APC_EXTENSIBLE) {
 
 577                 int inext = per_get_few_bits(pd, 1);
 
 578                 if(inext < 0) ASN__DECODE_STARVED;
 
 586                 if(ct->flags & APC_SEMI_CONSTRAINED) {
 
 587                         st->buf = (uint8_t *)CALLOC(1, 2);
 
 588                         if(!st->buf) ASN__DECODE_FAILED;
 
 590                 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
 
 591                         size_t size = (ct->range_bits + 7) >> 3;
 
 592                         st->buf = (uint8_t *)MALLOC(1 + size + 1);
 
 593                         if(!st->buf) ASN__DECODE_FAILED;
 
 598         /* X.691-2008/11, #13.2.2, constrained whole number */
 
 599         if(ct && ct->flags != APC_UNCONSTRAINED) {
 
 601                 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
 
 602                 if(ct->range_bits >= 0) {
 
 603                         if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
 
 606                         if(specs && specs->field_unsigned) {
 
 607                                 unsigned long uvalue;
 
 608                                 if(uper_get_constrained_whole_number(pd,
 
 609                                         &uvalue, ct->range_bits))
 
 611                                 ASN_DEBUG("Got value %lu + low %ld",
 
 612                                         uvalue, ct->lower_bound);
 
 613                                 uvalue += ct->lower_bound;
 
 614                                 if(asn_ulong2INTEGER(st, uvalue))
 
 617                                 unsigned long svalue;
 
 618                                 if(uper_get_constrained_whole_number(pd,
 
 619                                         &svalue, ct->range_bits))
 
 621                                 ASN_DEBUG("Got value %ld + low %ld",
 
 622                                         svalue, ct->lower_bound);
 
 623                                 svalue += ct->lower_bound;
 
 624                                 if(asn_long2INTEGER(st, svalue))
 
 630                 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
 
 633         /* X.691, #12.2.3, #12.2.4 */
 
 639                 /* Get the PER length */
 
 640                 len = uper_get_length(pd, -1, &repeat);
 
 641                 if(len < 0) ASN__DECODE_STARVED;
 
 643                 p = REALLOC(st->buf, st->size + len + 1);
 
 644                 if(!p) ASN__DECODE_FAILED;
 
 645                 st->buf = (uint8_t *)p;
 
 647                 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
 
 648                 if(ret < 0) ASN__DECODE_STARVED;
 
 651         st->buf[st->size] = 0;  /* JIC */
 
 654         if(ct && ct->lower_bound) {
 
 656                  * TODO: replace by in-place arithmetics.
 
 659                 if(asn_INTEGER2long(st, &value))
 
 661                 if(asn_long2INTEGER(st, value + ct->lower_bound))
 
 669 INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
 
 670         asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
 
 671         asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
 
 673         INTEGER_t *st = (INTEGER_t *)sptr;
 
 676         asn_per_constraint_t *ct;
 
 680         if(!st || st->size == 0) ASN__ENCODE_FAILED;
 
 682         if(!constraints) constraints = td->per_constraints;
 
 683         ct = constraints ? &constraints->value : 0;
 
 689                 if(specs && specs->field_unsigned) {
 
 691                         if(asn_INTEGER2ulong(st, &uval))
 
 693                         /* Check proper range */
 
 694                         if(ct->flags & APC_SEMI_CONSTRAINED) {
 
 695                                 if(uval < (unsigned long)ct->lower_bound)
 
 697                         } else if(ct->range_bits >= 0) {
 
 698                                 if(uval < (unsigned long)ct->lower_bound
 
 699                                 || uval > (unsigned long)ct->upper_bound)
 
 702                         ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
 
 703                                 uval, st->buf[0], st->size,
 
 704                                 ct->lower_bound, ct->upper_bound,
 
 705                                 inext ? "ext" : "fix");
 
 708                         if(asn_INTEGER2long(st, &value))
 
 710                         /* Check proper range */
 
 711                         if(ct->flags & APC_SEMI_CONSTRAINED) {
 
 712                                 if(value < ct->lower_bound)
 
 714                         } else if(ct->range_bits >= 0) {
 
 715                                 if(value < ct->lower_bound
 
 716                                 || value > ct->upper_bound)
 
 719                         ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
 
 720                                 value, st->buf[0], st->size,
 
 721                                 ct->lower_bound, ct->upper_bound,
 
 722                                 inext ? "ext" : "fix");
 
 724                 if(ct->flags & APC_EXTENSIBLE) {
 
 725                         if(per_put_few_bits(po, inext, 1))
 
 734         /* X.691-11/2008, #13.2.2, test if constrained whole number */
 
 735         if(ct && ct->range_bits >= 0) {
 
 736                 /* #11.5.6 -> #11.3 */
 
 737                 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
 
 738                         value, value - ct->lower_bound, ct->range_bits);
 
 739                 v = value - ct->lower_bound;
 
 740                 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
 
 745         if(ct && ct->lower_bound) {
 
 746                 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
 
 747                 /* TODO: adjust lower bound */
 
 751         for(buf = st->buf, end = st->buf + st->size; buf < end;) {
 
 752                 ssize_t mayEncode = uper_put_length(po, end - buf);
 
 755                 if(per_put_many_bits(po, buf, 8 * mayEncode))
 
 763 #endif  /* ASN_DISABLE_PER_SUPPORT */
 
 766 asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
 
 771         /* Sanity checking */
 
 772         if(!iptr || !iptr->buf || !lptr) {
 
 777         /* Cache the begin/end of the buffer */
 
 778         b = iptr->buf;  /* Start of the INTEGER buffer */
 
 780         end = b + size; /* Where to stop */
 
 782         if(size > sizeof(long)) {
 
 783                 uint8_t *end1 = end - 1;
 
 785                  * Slightly more advanced processing,
 
 786                  * able to >sizeof(long) bytes,
 
 787                  * when the actual value is small
 
 788                  * (0x0000000000abcdef would yield a fine 0x00abcdef)
 
 790                 /* Skip out the insignificant leading bytes */
 
 791                 for(; b < end1; b++) {
 
 793                         case 0x00: if((b[1] & 0x80) == 0) continue; break;
 
 794                         case 0xff: if((b[1] & 0x80) != 0) continue; break;
 
 800                 if(size > sizeof(long)) {
 
 801                         /* Still cannot fit the long */
 
 807         /* Shortcut processing of a corner case */
 
 813         /* Perform the sign initialization */
 
 814         /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
 
 815         if((*b >> 7)) l = -1; else l = 0;
 
 817         /* Conversion engine */
 
 826 asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
 
 831         if(!iptr || !iptr->buf || !lptr) {
 
 840         /* If all extra leading bytes are zeroes, ignore them */
 
 841         for(; size > sizeof(unsigned long); b++, size--) {
 
 843                         /* Value won't fit unsigned long */
 
 849         /* Conversion engine */
 
 850         for(l = 0; b < end; b++)
 
 858 asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
 
 864         if(value <= LONG_MAX)
 
 865                 return asn_long2INTEGER(st, value);
 
 867         buf = (uint8_t *)MALLOC(1 + sizeof(value));
 
 870         end = buf + (sizeof(value) + 1);
 
 872         for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
 
 873                 *b = (uint8_t)(value >> shr);
 
 875         if(st->buf) FREEMEM(st->buf);
 
 877         st->size = 1 + sizeof(value);
 
 883 asn_long2INTEGER(INTEGER_t *st, long value) {
 
 888         int littleEndian = 1;   /* Run-time detection */
 
 896         buf = (uint8_t *)MALLOC(sizeof(value));
 
 899         if(*(char *)&littleEndian) {
 
 900                 pstart = (uint8_t *)&value + sizeof(value) - 1;
 
 901                 pend1 = (uint8_t *)&value;
 
 904                 pstart = (uint8_t *)&value;
 
 905                 pend1 = pstart + sizeof(value) - 1;
 
 910          * If the contents octet consists of more than one octet,
 
 911          * then bits of the first octet and bit 8 of the second octet:
 
 912          * a) shall not all be ones; and
 
 913          * b) shall not all be zero.
 
 915         for(p = pstart; p != pend1; p += add) {
 
 917                 case 0x00: if((*(p+add) & 0x80) == 0)
 
 920                 case 0xff: if((*(p+add) & 0x80))
 
 926         /* Copy the integer body */
 
 927         for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
 
 930         if(st->buf) FREEMEM(st->buf);
 
 938  * This function is going to be DEPRECATED soon.
 
 940 enum asn_strtol_result_e
 
 941 asn_strtol(const char *str, const char *end, long *lp) {
 
 942     const char *endp = end;
 
 944     switch(asn_strtol_lim(str, &endp, lp)) {
 
 945     case ASN_STRTOL_ERROR_RANGE:
 
 946         return ASN_STRTOL_ERROR_RANGE;
 
 947     case ASN_STRTOL_ERROR_INVAL:
 
 948         return ASN_STRTOL_ERROR_INVAL;
 
 949     case ASN_STRTOL_EXPECT_MORE:
 
 950         return ASN_STRTOL_ERROR_INVAL;  /* Retain old behavior */
 
 952         return ASN_STRTOL_OK;
 
 953     case ASN_STRTOL_EXTRA_DATA:
 
 954         return ASN_STRTOL_ERROR_INVAL;  /* Retain old behavior */
 
 957     return ASN_STRTOL_ERROR_INVAL;  /* Retain old behavior */
 
 961  * Parse the number in the given string until the given *end position,
 
 962  * returning the position after the last parsed character back using the
 
 963  * same (*end) pointer.
 
 964  * WARNING: This behavior is different from the standard strtol(3).
 
 966 enum asn_strtol_result_e
 
 967 asn_strtol_lim(const char *str, const char **end, long *lp) {
 
 971         const long upper_boundary = LONG_MAX / 10;
 
 972         long last_digit_max = LONG_MAX % 10;
 
 974         if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
 
 985                         return ASN_STRTOL_EXPECT_MORE;
 
 989         for(l = 0; str < (*end); str++) {
 
 991                 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
 
 992                 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
 
 994                         if(l < upper_boundary) {
 
 996                         } else if(l == upper_boundary) {
 
 997                                 if(d <= last_digit_max) {
 
1006                                         return ASN_STRTOL_ERROR_RANGE;
 
1010                                 return ASN_STRTOL_ERROR_RANGE;
 
1017                     return ASN_STRTOL_EXTRA_DATA;
 
1023         return ASN_STRTOL_OK;