Harald Welte | 43ab79f | 2018-10-03 23:34:21 +0200 | [diff] [blame] | 1 | /*- |
| 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 | */ |
| 14 | static const ber_tlv_tag_t asn_DEF_INTEGER_tags[] = { |
| 15 | (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) |
| 16 | }; |
| 17 | asn_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 | */ |
| 51 | asn_enc_rval_t |
| 52 | INTEGER_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 | |
| 107 | static 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 | */ |
| 112 | static ssize_t |
| 113 | INTEGER__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 | */ |
| 195 | int |
| 196 | INTEGER_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 | |
| 212 | struct e2v_key { |
| 213 | const char *start; |
| 214 | const char *stop; |
| 215 | const asn_INTEGER_enum_map_t *vemap; |
| 216 | const unsigned int *evmap; |
| 217 | }; |
| 218 | static int |
| 219 | INTEGER__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 | |
| 237 | static const asn_INTEGER_enum_map_t * |
| 238 | INTEGER_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 | |
| 275 | static int |
| 276 | INTEGER__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 | |
| 285 | const asn_INTEGER_enum_map_t * |
| 286 | INTEGER_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 | |
| 294 | static int |
| 295 | INTEGER_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 | */ |
| 311 | static enum xer_pbd_rval |
| 312 | INTEGER__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 | |
| 530 | asn_dec_rval_t |
| 531 | INTEGER_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 | |
| 540 | asn_enc_rval_t |
| 541 | INTEGER_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 | |
| 561 | asn_dec_rval_t |
| 562 | INTEGER_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 | |
| 672 | asn_dec_rval_t |
| 673 | INTEGER_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) + |
| 720 | (((ct->range_bits % 8) > 0) ? 1 : 0); |
| 721 | int length = 0, i; |
| 722 | int64_t value = 0; |
| 723 | |
| 724 | for (i = 1; ; i++) { |
| 725 | int upper = 1 << i; |
| 726 | if (upper >= max_range_bytes) |
| 727 | break; |
| 728 | } |
| 729 | ASN_DEBUG("Can encode %d (%d bytes) in %d bits", ct->range_bits, |
| 730 | max_range_bytes, i); |
| 731 | |
| 732 | if ((length = per_get_few_bits(pd, i)) < 0) |
| 733 | _ASN_DECODE_FAILED; |
| 734 | |
| 735 | /* X.691 #12.2.6 length determinant + lb (1) */ |
| 736 | length += 1; |
| 737 | ASN_DEBUG("Got length %d", length); |
| 738 | |
| 739 | if (aper_get_align(pd) != 0) |
| 740 | _ASN_DECODE_STARVED; |
| 741 | |
| 742 | while (length--) { |
| 743 | int buf = per_get_few_bits(pd, 8); |
| 744 | if (buf < 0) |
| 745 | _ASN_DECODE_STARVED; |
| 746 | value += (((int64_t)buf) << (8 * length)); |
| 747 | } |
| 748 | |
| 749 | value += ct->lower_bound; |
| 750 | if((specs && specs->field_unsigned) |
| 751 | ? asn_uint642INTEGER(st, value) |
| 752 | : asn_int642INTEGER(st, value)) |
| 753 | _ASN_DECODE_FAILED; |
| 754 | ASN_DEBUG("Got value %lld + low %lld", |
| 755 | value, ct->lower_bound); |
| 756 | } else { |
| 757 | long value = 0; |
| 758 | if (ct->range_bits < 8) { |
| 759 | value = per_get_few_bits(pd, ct->range_bits); |
| 760 | if(value < 0) _ASN_DECODE_STARVED; |
| 761 | } else if (ct->range_bits == 8) { |
| 762 | if (aper_get_align(pd) < 0) |
| 763 | _ASN_DECODE_FAILED; |
| 764 | value = per_get_few_bits(pd, ct->range_bits); |
| 765 | if(value < 0) _ASN_DECODE_STARVED; |
| 766 | } else { |
| 767 | /* Align */ |
| 768 | if (aper_get_align(pd) < 0) |
| 769 | _ASN_DECODE_FAILED; |
| 770 | value = per_get_few_bits(pd, 16); |
| 771 | if(value < 0) _ASN_DECODE_STARVED; |
| 772 | } |
| 773 | value += ct->lower_bound; |
| 774 | if((specs && specs->field_unsigned) |
| 775 | ? asn_ulong2INTEGER(st, value) |
| 776 | : asn_long2INTEGER(st, value)) |
| 777 | _ASN_DECODE_FAILED; |
| 778 | ASN_DEBUG("Got value %ld + low %lld", |
| 779 | value, ct->lower_bound); |
| 780 | } |
| 781 | return rval; |
| 782 | } else { |
| 783 | _ASN_DECODE_FAILED; |
| 784 | } |
| 785 | } else { |
| 786 | ASN_DEBUG("Decoding unconstrained integer %s", td->name); |
| 787 | } |
| 788 | |
| 789 | /* X.691, #12.2.3, #12.2.4 */ |
| 790 | do { |
| 791 | ssize_t len; |
| 792 | void *p; |
| 793 | int ret; |
| 794 | |
| 795 | /* Get the PER length */ |
| 796 | len = aper_get_length(pd, -1, -1, &repeat); |
| 797 | if(len < 0) _ASN_DECODE_STARVED; |
| 798 | |
| 799 | p = REALLOC(st->buf, st->size + len + 1); |
| 800 | if(!p) _ASN_DECODE_FAILED; |
| 801 | st->buf = (uint8_t *)p; |
| 802 | |
| 803 | ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len); |
| 804 | if(ret < 0) _ASN_DECODE_STARVED; |
| 805 | st->size += len; |
| 806 | } while(repeat); |
| 807 | st->buf[st->size] = 0; /* JIC */ |
| 808 | |
| 809 | /* #12.2.3 */ |
| 810 | if(ct && ct->lower_bound) { |
| 811 | /* |
| 812 | * TODO: replace by in-place arithmetics. |
| 813 | */ |
| 814 | long value; |
| 815 | if(asn_INTEGER2long(st, &value)) |
| 816 | _ASN_DECODE_FAILED; |
| 817 | if(asn_long2INTEGER(st, value + ct->lower_bound)) |
| 818 | _ASN_DECODE_FAILED; |
| 819 | } |
| 820 | |
| 821 | return rval; |
| 822 | } |
| 823 | |
| 824 | asn_enc_rval_t |
| 825 | INTEGER_encode_uper(asn_TYPE_descriptor_t *td, |
| 826 | asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) { |
| 827 | asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics; |
| 828 | asn_enc_rval_t er; |
| 829 | INTEGER_t *st = (INTEGER_t *)sptr; |
| 830 | const uint8_t *buf; |
| 831 | const uint8_t *end; |
| 832 | asn_per_constraint_t *ct; |
| 833 | long value = 0; |
| 834 | |
| 835 | if(!st || st->size == 0) _ASN_ENCODE_FAILED; |
| 836 | |
| 837 | if(!constraints) constraints = td->per_constraints; |
| 838 | ct = constraints ? &constraints->value : 0; |
| 839 | |
| 840 | er.encoded = 0; |
| 841 | |
| 842 | if(ct) { |
| 843 | int inext = 0; |
| 844 | if(specs && specs->field_unsigned) { |
| 845 | unsigned long uval; |
| 846 | if(asn_INTEGER2ulong(st, &uval)) |
| 847 | _ASN_ENCODE_FAILED; |
| 848 | /* Check proper range */ |
| 849 | if(ct->flags & APC_SEMI_CONSTRAINED) { |
| 850 | if(uval < (unsigned long)ct->lower_bound) |
| 851 | inext = 1; |
| 852 | } else if(ct->range_bits >= 0) { |
| 853 | if(uval < (unsigned long)ct->lower_bound |
| 854 | || uval > (unsigned long)ct->upper_bound) |
| 855 | inext = 1; |
| 856 | } |
| 857 | ASN_DEBUG("Value %lu (%02x/%d) lb %llu ub %llu %s", |
| 858 | uval, st->buf[0], st->size, |
| 859 | ct->lower_bound, ct->upper_bound, |
| 860 | inext ? "ext" : "fix"); |
| 861 | value = uval; |
| 862 | } else { |
| 863 | if(asn_INTEGER2long(st, &value)) |
| 864 | _ASN_ENCODE_FAILED; |
| 865 | /* Check proper range */ |
| 866 | if(ct->flags & APC_SEMI_CONSTRAINED) { |
| 867 | if(value < ct->lower_bound) |
| 868 | inext = 1; |
| 869 | } else if(ct->range_bits >= 0) { |
| 870 | if(value < ct->lower_bound |
| 871 | || value > ct->upper_bound) |
| 872 | inext = 1; |
| 873 | } |
| 874 | ASN_DEBUG("Value %ld (%02x/%d) lb %lld ub %lld %s", |
| 875 | value, st->buf[0], st->size, |
| 876 | ct->lower_bound, ct->upper_bound, |
| 877 | inext ? "ext" : "fix"); |
| 878 | } |
| 879 | if(ct->flags & APC_EXTENSIBLE) { |
| 880 | if(per_put_few_bits(po, inext, 1)) |
| 881 | _ASN_ENCODE_FAILED; |
| 882 | if(inext) ct = 0; |
| 883 | } else if(inext) { |
| 884 | _ASN_ENCODE_FAILED; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | |
| 889 | /* X.691-11/2008, #13.2.2, test if constrained whole number */ |
| 890 | if(ct && ct->range_bits >= 0) { |
| 891 | /* #11.5.6 -> #11.3 */ |
| 892 | ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits", |
| 893 | value, value - ct->lower_bound, ct->range_bits); |
| 894 | unsigned long v = value - ct->lower_bound; |
| 895 | if(uper_put_constrained_whole_number_u(po, v, ct->range_bits)) |
| 896 | _ASN_ENCODE_FAILED; |
| 897 | _ASN_ENCODED_OK(er); |
| 898 | } |
| 899 | |
| 900 | if(ct && ct->lower_bound) { |
| 901 | ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound); |
| 902 | /* TODO: adjust lower bound */ |
| 903 | _ASN_ENCODE_FAILED; |
| 904 | } |
| 905 | |
| 906 | for(buf = st->buf, end = st->buf + st->size; buf < end;) { |
| 907 | ssize_t mayEncode = uper_put_length(po, end - buf); |
| 908 | if(mayEncode < 0) |
| 909 | _ASN_ENCODE_FAILED; |
| 910 | if(per_put_many_bits(po, buf, 8 * mayEncode)) |
| 911 | _ASN_ENCODE_FAILED; |
| 912 | buf += mayEncode; |
| 913 | } |
| 914 | |
| 915 | _ASN_ENCODED_OK(er); |
| 916 | } |
| 917 | |
| 918 | asn_enc_rval_t |
| 919 | INTEGER_encode_aper(asn_TYPE_descriptor_t *td, |
| 920 | asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) { |
| 921 | asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics; |
| 922 | asn_enc_rval_t er; |
| 923 | INTEGER_t *st = (INTEGER_t *)sptr; |
| 924 | const uint8_t *buf; |
| 925 | const uint8_t *end; |
| 926 | asn_per_constraint_t *ct; |
| 927 | int64_t value = 0; |
| 928 | |
| 929 | if(!st || st->size == 0) _ASN_ENCODE_FAILED; |
| 930 | |
| 931 | if(!constraints) constraints = td->per_constraints; |
| 932 | ct = constraints ? &constraints->value : 0; |
| 933 | |
| 934 | er.encoded = 0; |
| 935 | |
| 936 | if(ct) { |
| 937 | int inext = 0; |
| 938 | if(specs && specs->field_unsigned) { |
| 939 | uint64_t uval; |
| 940 | if(asn_INTEGER2uint64(st, &uval)) |
| 941 | _ASN_ENCODE_FAILED; |
| 942 | /* Check proper range */ |
| 943 | if(ct->flags & APC_SEMI_CONSTRAINED) { |
| 944 | if(uval < (unsigned long long)ct->lower_bound) |
| 945 | inext = 1; |
| 946 | } else if(ct->range_bits >= 0) { |
| 947 | if(uval < (unsigned long long)ct->lower_bound |
| 948 | || uval > (unsigned long long)ct->upper_bound) |
| 949 | inext = 1; |
| 950 | } |
| 951 | ASN_DEBUG("Value %llu (%02x/%d) lb %llu ub %llu %s", |
| 952 | uval, st->buf[0], st->size, |
| 953 | ct->lower_bound, ct->upper_bound, |
| 954 | inext ? "ext" : "fix"); |
| 955 | value = uval; |
| 956 | } else { |
| 957 | if(asn_INTEGER2int64(st, &value)) _ASN_ENCODE_FAILED; |
| 958 | /* Check proper range */ |
| 959 | if(ct->flags & APC_SEMI_CONSTRAINED) { |
| 960 | if(value < ct->lower_bound) |
| 961 | inext = 1; |
| 962 | } else if(ct->range_bits >= 0) { |
| 963 | if(value < ct->lower_bound |
| 964 | || value > ct->upper_bound) |
| 965 | inext = 1; |
| 966 | } |
| 967 | ASN_DEBUG("Value %lld (%02x/%d) lb %lld ub %lld %s", |
| 968 | value, st->buf[0], st->size, |
| 969 | ct->lower_bound, ct->upper_bound, |
| 970 | inext ? "ext" : "fix"); |
| 971 | } |
| 972 | if(ct->flags & APC_EXTENSIBLE) { |
| 973 | if(per_put_few_bits(po, inext, 1)) |
| 974 | _ASN_ENCODE_FAILED; |
| 975 | if(inext) ct = 0; |
| 976 | } else if(inext) { |
| 977 | _ASN_ENCODE_FAILED; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | /* X.691, #12.2.2 */ |
| 982 | if(ct && ct->range_bits >= 0) { |
| 983 | /* #10.5.6 */ |
| 984 | ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits", |
| 985 | value, value - ct->lower_bound, ct->range_bits); |
| 986 | unsigned long v = value - ct->lower_bound; |
| 987 | |
| 988 | /* #12 <= 8 -> alignment ? */ |
| 989 | if (ct->range_bits < 8) { |
| 990 | if(per_put_few_bits(po, 0x00 | v, ct->range_bits)) |
| 991 | _ASN_ENCODE_FAILED; |
| 992 | } else if (ct->range_bits == 8) { |
| 993 | if(aper_put_align(po) < 0) |
| 994 | _ASN_ENCODE_FAILED; |
| 995 | if(per_put_few_bits(po, 0x00 | v, ct->range_bits)) |
| 996 | _ASN_ENCODE_FAILED; |
| 997 | } else if (ct->range_bits <= 16) { |
| 998 | // Consume the bytes to align on octet |
| 999 | if(aper_put_align(po) < 0) |
| 1000 | _ASN_ENCODE_FAILED; |
| 1001 | if(per_put_few_bits(po, 0x0000 | v, |
| 1002 | 16)) |
| 1003 | _ASN_ENCODE_FAILED; |
| 1004 | } else { |
| 1005 | /* TODO: extend to >64 bits */ |
| 1006 | int64_t v64 = v; |
| 1007 | int i, j; |
| 1008 | int max_range_bytes = (ct->range_bits >> 3) + |
| 1009 | (((ct->range_bits % 8) > 0) ? 1 : 0); |
| 1010 | |
| 1011 | for (i = 1; ; i++) { |
| 1012 | int upper = 1 << i; |
| 1013 | if (upper >= max_range_bytes) |
| 1014 | break; |
| 1015 | } |
| 1016 | |
| 1017 | for (j = sizeof(int64_t) -1; j != 0; j--) { |
| 1018 | uint8_t val; |
| 1019 | val = v64 >> (j * 8); |
| 1020 | if (val != 0) |
| 1021 | break; |
| 1022 | } |
| 1023 | |
| 1024 | /* Putting length in the minimum number of bits ex: 5 = 3bits */ |
| 1025 | if (per_put_few_bits(po, j, i)) |
| 1026 | _ASN_ENCODE_FAILED; |
| 1027 | |
| 1028 | // Consume the bits to align on octet |
| 1029 | if (aper_put_align(po) < 0) |
| 1030 | _ASN_ENCODE_FAILED; |
| 1031 | |
| 1032 | /* Put the value */ |
| 1033 | for (i = 0; i <= j; i++) { |
| 1034 | if(per_put_few_bits(po, (v64 >> (8 * (j - i))) & 0xff, 8)) |
| 1035 | _ASN_ENCODE_FAILED; |
| 1036 | } |
| 1037 | } |
| 1038 | _ASN_ENCODED_OK(er); |
| 1039 | } |
| 1040 | |
| 1041 | if(ct && ct->lower_bound) { |
| 1042 | ASN_DEBUG("Adjust lower bound to %lld", ct->lower_bound); |
| 1043 | /* TODO: adjust lower bound */ |
| 1044 | _ASN_ENCODE_FAILED; |
| 1045 | } |
| 1046 | |
| 1047 | for(buf = st->buf, end = st->buf + st->size; buf < end;) { |
| 1048 | ssize_t mayEncode = aper_put_length(po, -1, end - buf); |
| 1049 | if(mayEncode < 0) |
| 1050 | _ASN_ENCODE_FAILED; |
| 1051 | if(per_put_many_bits(po, buf, 8 * mayEncode)) |
| 1052 | _ASN_ENCODE_FAILED; |
| 1053 | buf += mayEncode; |
| 1054 | } |
| 1055 | |
| 1056 | _ASN_ENCODED_OK(er); |
| 1057 | } |
| 1058 | |
| 1059 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
| 1060 | |
| 1061 | int |
| 1062 | asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) { |
| 1063 | uint8_t *b, *end; |
| 1064 | size_t size; |
| 1065 | long l; |
| 1066 | |
| 1067 | /* Sanity checking */ |
| 1068 | if(!iptr || !iptr->buf || !lptr) { |
| 1069 | errno = EINVAL; |
| 1070 | return -1; |
| 1071 | } |
| 1072 | |
| 1073 | /* Cache the begin/end of the buffer */ |
| 1074 | b = iptr->buf; /* Start of the INTEGER buffer */ |
| 1075 | size = iptr->size; |
| 1076 | end = b + size; /* Where to stop */ |
| 1077 | |
| 1078 | if(size > sizeof(long)) { |
| 1079 | uint8_t *end1 = end - 1; |
| 1080 | /* |
| 1081 | * Slightly more advanced processing, |
| 1082 | * able to >sizeof(long) bytes, |
| 1083 | * when the actual value is small |
| 1084 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 1085 | */ |
| 1086 | /* Skip out the insignificant leading bytes */ |
| 1087 | for(; b < end1; b++) { |
| 1088 | switch(*b) { |
| 1089 | case 0x00: if((b[1] & 0x80) == 0) continue; break; |
| 1090 | case 0xff: if((b[1] & 0x80) != 0) continue; break; |
| 1091 | } |
| 1092 | break; |
| 1093 | } |
| 1094 | |
| 1095 | size = end - b; |
| 1096 | if(size > sizeof(long)) { |
| 1097 | /* Still cannot fit the long */ |
| 1098 | errno = ERANGE; |
| 1099 | return -1; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | /* Shortcut processing of a corner case */ |
| 1104 | if(end == b) { |
| 1105 | *lptr = 0; |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | /* Perform the sign initialization */ |
| 1110 | /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */ |
| 1111 | if((*b >> 7)) l = -1; else l = 0; |
| 1112 | |
| 1113 | /* Conversion engine */ |
| 1114 | for(; b < end; b++) |
| 1115 | l = (l << 8) | *b; |
| 1116 | |
| 1117 | *lptr = l; |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | int |
| 1122 | asn_INTEGER2int64(const INTEGER_t *iptr, int64_t *lptr) { |
| 1123 | uint8_t *b, *end; |
| 1124 | size_t size; |
| 1125 | int64_t l; |
| 1126 | |
| 1127 | /* Sanity checking */ |
| 1128 | if(!iptr || !iptr->buf || !lptr) { |
| 1129 | errno = EINVAL; |
| 1130 | return -1; |
| 1131 | } |
| 1132 | |
| 1133 | /* Cache the begin/end of the buffer */ |
| 1134 | b = iptr->buf; /* Start of the INTEGER buffer */ |
| 1135 | size = iptr->size; |
| 1136 | end = b + size; /* Where to stop */ |
| 1137 | |
| 1138 | if(size > sizeof(int64_t)) { |
| 1139 | uint8_t *end1 = end - 1; |
| 1140 | /* |
| 1141 | * Slightly more advanced processing, |
| 1142 | * able to >sizeof(int64_t) bytes, |
| 1143 | * when the actual value is small |
| 1144 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 1145 | */ |
| 1146 | /* Skip out the insignificant leading bytes */ |
| 1147 | for(; b < end1; b++) { |
| 1148 | switch(*b) { |
| 1149 | case 0x00: if((b[1] & 0x80) == 0) continue; break; |
| 1150 | case 0xff: if((b[1] & 0x80) != 0) continue; break; |
| 1151 | } |
| 1152 | break; |
| 1153 | } |
| 1154 | |
| 1155 | size = end - b; |
| 1156 | if(size > sizeof(int64_t)) { |
| 1157 | /* Still cannot fit the int64_t */ |
| 1158 | errno = ERANGE; |
| 1159 | return -1; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | /* Shortcut processing of a corner case */ |
| 1164 | if(end == b) { |
| 1165 | *lptr = 0; |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | /* Perform the sign initialization */ |
| 1170 | /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */ |
| 1171 | if((*b >> 7)) l = -1; else l = 0; |
| 1172 | |
| 1173 | /* Conversion engine */ |
| 1174 | for(; b < end; b++) |
| 1175 | l = (l << 8) | *b; |
| 1176 | |
| 1177 | *lptr = l; |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | int |
| 1182 | asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) { |
| 1183 | uint8_t *b, *end; |
| 1184 | unsigned long l; |
| 1185 | size_t size; |
| 1186 | |
| 1187 | if(!iptr || !iptr->buf || !lptr) { |
| 1188 | errno = EINVAL; |
| 1189 | return -1; |
| 1190 | } |
| 1191 | |
| 1192 | b = iptr->buf; |
| 1193 | size = iptr->size; |
| 1194 | end = b + size; |
| 1195 | |
| 1196 | /* If all extra leading bytes are zeroes, ignore them */ |
| 1197 | for(; size > sizeof(unsigned long); b++, size--) { |
| 1198 | if(*b) { |
| 1199 | /* Value won't fit unsigned long */ |
| 1200 | errno = ERANGE; |
| 1201 | return -1; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | /* Conversion engine */ |
| 1206 | for(l = 0; b < end; b++) |
| 1207 | l = (l << 8) | *b; |
| 1208 | |
| 1209 | *lptr = l; |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | int |
| 1214 | asn_INTEGER2uint64(const INTEGER_t *iptr, uint64_t *lptr) { |
| 1215 | uint8_t *b, *end; |
| 1216 | uint64_t l; |
| 1217 | size_t size; |
| 1218 | |
| 1219 | if(!iptr || !iptr->buf || !lptr) { |
| 1220 | errno = EINVAL; |
| 1221 | return -1; |
| 1222 | } |
| 1223 | |
| 1224 | b = iptr->buf; |
| 1225 | size = iptr->size; |
| 1226 | end = b + size; |
| 1227 | |
| 1228 | /* If all extra leading bytes are zeroes, ignore them */ |
| 1229 | for(; size > sizeof(uint64_t); b++, size--) { |
| 1230 | if(*b) { |
| 1231 | /* Value won't fit unsigned long */ |
| 1232 | errno = ERANGE; |
| 1233 | return -1; |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | /* Conversion engine */ |
| 1238 | for(l = 0; b < end; b++) |
| 1239 | l = (l << 8) | *b; |
| 1240 | |
| 1241 | *lptr = l; |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | int |
| 1246 | asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) { |
| 1247 | uint8_t *buf; |
| 1248 | uint8_t *end; |
| 1249 | uint8_t *b; |
| 1250 | int shr; |
| 1251 | |
| 1252 | if(value <= LONG_MAX) |
| 1253 | return asn_long2INTEGER(st, value); |
| 1254 | |
| 1255 | buf = (uint8_t *)MALLOC(1 + sizeof(value)); |
| 1256 | if(!buf) return -1; |
| 1257 | |
| 1258 | end = buf + (sizeof(value) + 1); |
| 1259 | buf[0] = 0; |
| 1260 | for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++) |
| 1261 | *b = (uint8_t)(value >> shr); |
| 1262 | |
| 1263 | if(st->buf) FREEMEM(st->buf); |
| 1264 | st->buf = buf; |
| 1265 | st->size = 1 + sizeof(value); |
| 1266 | |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | int |
| 1271 | asn_uint642INTEGER(INTEGER_t *st, uint64_t value) { |
| 1272 | uint8_t *buf; |
| 1273 | uint8_t *end; |
| 1274 | uint8_t *b; |
| 1275 | int shr; |
| 1276 | |
| 1277 | if(value <= INT64_MAX) |
| 1278 | return asn_int642INTEGER(st, value); |
| 1279 | |
| 1280 | buf = (uint8_t *)MALLOC(1 + sizeof(value)); |
| 1281 | if(!buf) return -1; |
| 1282 | |
| 1283 | end = buf + (sizeof(value) + 1); |
| 1284 | buf[0] = 0; |
| 1285 | for(b = buf + 1, shr = (sizeof(value)-1)*8; b < end; shr -= 8, b++) |
| 1286 | *b = (uint8_t)(value >> shr); |
| 1287 | |
| 1288 | if(st->buf) FREEMEM(st->buf); |
| 1289 | st->buf = buf; |
| 1290 | st->size = 1 + sizeof(value); |
| 1291 | |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | int |
| 1296 | asn_int642INTEGER(INTEGER_t *st, int64_t value) { |
| 1297 | uint8_t *buf, *bp; |
| 1298 | uint8_t *p; |
| 1299 | uint8_t *pstart; |
| 1300 | uint8_t *pend1; |
| 1301 | int littleEndian = 1; /* Run-time detection */ |
| 1302 | int add; |
| 1303 | |
| 1304 | if(!st) { |
| 1305 | errno = EINVAL; |
| 1306 | return -1; |
| 1307 | } |
| 1308 | |
| 1309 | buf = (uint8_t *)MALLOC(sizeof(value)); |
| 1310 | if(!buf) return -1; |
| 1311 | |
| 1312 | if(*(char *)&littleEndian) { |
| 1313 | pstart = (uint8_t *)&value + sizeof(value) - 1; |
| 1314 | pend1 = (uint8_t *)&value; |
| 1315 | add = -1; |
| 1316 | } else { |
| 1317 | pstart = (uint8_t *)&value; |
| 1318 | pend1 = pstart + sizeof(value) - 1; |
| 1319 | add = 1; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
| 1323 | * If the contents octet consists of more than one octet, |
| 1324 | * then bits of the first octet and bit 8 of the second octet: |
| 1325 | * a) shall not all be ones; and |
| 1326 | * b) shall not all be zero. |
| 1327 | */ |
| 1328 | for(p = pstart; p != pend1; p += add) { |
| 1329 | switch(*p) { |
| 1330 | case 0x00: if((*(p+add) & 0x80) == 0) |
| 1331 | continue; |
| 1332 | break; |
| 1333 | case 0xff: if((*(p+add) & 0x80)) |
| 1334 | continue; |
| 1335 | break; |
| 1336 | } |
| 1337 | break; |
| 1338 | } |
| 1339 | /* Copy the integer body */ |
| 1340 | for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add) |
| 1341 | *bp++ = *p; |
| 1342 | |
| 1343 | if(st->buf) FREEMEM(st->buf); |
| 1344 | st->buf = buf; |
| 1345 | st->size = bp - buf; |
| 1346 | |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
| 1350 | int |
| 1351 | asn_long2INTEGER(INTEGER_t *st, long value) { |
| 1352 | uint8_t *buf, *bp; |
| 1353 | uint8_t *p; |
| 1354 | uint8_t *pstart; |
| 1355 | uint8_t *pend1; |
| 1356 | int littleEndian = 1; /* Run-time detection */ |
| 1357 | int add; |
| 1358 | |
| 1359 | if(!st) { |
| 1360 | errno = EINVAL; |
| 1361 | return -1; |
| 1362 | } |
| 1363 | |
| 1364 | buf = (uint8_t *)MALLOC(sizeof(value)); |
| 1365 | if(!buf) return -1; |
| 1366 | |
| 1367 | if(*(char *)&littleEndian) { |
| 1368 | pstart = (uint8_t *)&value + sizeof(value) - 1; |
| 1369 | pend1 = (uint8_t *)&value; |
| 1370 | add = -1; |
| 1371 | } else { |
| 1372 | pstart = (uint8_t *)&value; |
| 1373 | pend1 = pstart + sizeof(value) - 1; |
| 1374 | add = 1; |
| 1375 | } |
| 1376 | |
| 1377 | /* |
| 1378 | * If the contents octet consists of more than one octet, |
| 1379 | * then bits of the first octet and bit 8 of the second octet: |
| 1380 | * a) shall not all be ones; and |
| 1381 | * b) shall not all be zero. |
| 1382 | */ |
| 1383 | for(p = pstart; p != pend1; p += add) { |
| 1384 | switch(*p) { |
| 1385 | case 0x00: if((*(p+add) & 0x80) == 0) |
| 1386 | continue; |
| 1387 | break; |
| 1388 | case 0xff: if((*(p+add) & 0x80)) |
| 1389 | continue; |
| 1390 | break; |
| 1391 | } |
| 1392 | break; |
| 1393 | } |
| 1394 | /* Copy the integer body */ |
| 1395 | for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add) |
| 1396 | *bp++ = *p; |
| 1397 | |
| 1398 | if(st->buf) FREEMEM(st->buf); |
| 1399 | st->buf = buf; |
| 1400 | st->size = bp - buf; |
| 1401 | |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | /* |
| 1406 | * This function is going to be DEPRECATED soon. |
| 1407 | */ |
| 1408 | enum asn_strtol_result_e |
| 1409 | asn_strtol(const char *str, const char *end, long *lp) { |
| 1410 | const char *endp = end; |
| 1411 | |
| 1412 | switch(asn_strtol_lim(str, &endp, lp)) { |
| 1413 | case ASN_STRTOL_ERROR_RANGE: |
| 1414 | return ASN_STRTOL_ERROR_RANGE; |
| 1415 | case ASN_STRTOL_ERROR_INVAL: |
| 1416 | return ASN_STRTOL_ERROR_INVAL; |
| 1417 | case ASN_STRTOL_EXPECT_MORE: |
| 1418 | return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */ |
| 1419 | case ASN_STRTOL_OK: |
| 1420 | return ASN_STRTOL_OK; |
| 1421 | case ASN_STRTOL_EXTRA_DATA: |
| 1422 | return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */ |
| 1423 | } |
| 1424 | |
| 1425 | return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */ |
| 1426 | } |
| 1427 | |
| 1428 | /* |
| 1429 | * Parse the number in the given string until the given *end position, |
| 1430 | * returning the position after the last parsed character back using the |
| 1431 | * same (*end) pointer. |
| 1432 | * WARNING: This behavior is different from the standard strtol(3). |
| 1433 | */ |
| 1434 | enum asn_strtol_result_e |
| 1435 | asn_strtol_lim(const char *str, const char **end, long *lp) { |
| 1436 | int sign = 1; |
| 1437 | long l; |
| 1438 | |
| 1439 | const long upper_boundary = LONG_MAX / 10; |
| 1440 | long last_digit_max = LONG_MAX % 10; |
| 1441 | |
| 1442 | if(str >= *end) return ASN_STRTOL_ERROR_INVAL; |
| 1443 | |
| 1444 | switch(*str) { |
| 1445 | case '-': |
| 1446 | last_digit_max++; |
| 1447 | sign = -1; |
| 1448 | case '+': |
| 1449 | str++; |
| 1450 | if(str >= *end) { |
| 1451 | *end = str; |
| 1452 | return ASN_STRTOL_EXPECT_MORE; |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | for(l = 0; str < (*end); str++) { |
| 1457 | switch(*str) { |
| 1458 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 1459 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: { |
| 1460 | int d = *str - '0'; |
| 1461 | if(l < upper_boundary) { |
| 1462 | l = l * 10 + d; |
| 1463 | } else if(l == upper_boundary) { |
| 1464 | if(d <= last_digit_max) { |
| 1465 | if(sign > 0) { |
| 1466 | l = l * 10 + d; |
| 1467 | } else { |
| 1468 | sign = 1; |
| 1469 | l = -l * 10 - d; |
| 1470 | } |
| 1471 | } else { |
| 1472 | *end = str; |
| 1473 | return ASN_STRTOL_ERROR_RANGE; |
| 1474 | } |
| 1475 | } else { |
| 1476 | *end = str; |
| 1477 | return ASN_STRTOL_ERROR_RANGE; |
| 1478 | } |
| 1479 | } |
| 1480 | continue; |
| 1481 | default: |
| 1482 | *end = str; |
| 1483 | *lp = sign * l; |
| 1484 | return ASN_STRTOL_EXTRA_DATA; |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | *end = str; |
| 1489 | *lp = sign * l; |
| 1490 | return ASN_STRTOL_OK; |
| 1491 | } |
| 1492 | |