Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 2 | * Copyright (c) 2003-2014 Lev Walkin <vlm@lionet.info>. |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 3 | * All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 6 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | #include <INTEGER.h> |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 8 | #include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | |
| 11 | /* |
| 12 | * INTEGER basic type description. |
| 13 | */ |
Wim Lewis | 18c2ec9 | 2014-07-29 11:30:10 -0700 | [diff] [blame] | 14 | static const ber_tlv_tag_t asn_DEF_INTEGER_tags[] = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) |
| 16 | }; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 17 | asn_TYPE_operation_t asn_OP_INTEGER = { |
Lev Walkin | 8077f93 | 2017-08-07 17:27:43 -0700 | [diff] [blame] | 18 | INTEGER_free, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 19 | INTEGER_print, |
Bi-Ruei, Chiu | e1bf219 | 2017-08-19 00:55:25 +0800 | [diff] [blame] | 20 | INTEGER_compare, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 21 | ber_decode_primitive, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 22 | INTEGER_encode_der, |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 23 | INTEGER_decode_xer, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 24 | INTEGER_encode_xer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 25 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 26 | 0, |
| 27 | 0, |
| 28 | #else |
Lev Walkin | 527497f | 2017-07-14 08:56:36 +0400 | [diff] [blame] | 29 | INTEGER_decode_oer, /* OER decoder */ |
| 30 | INTEGER_encode_oer, /* Canonical OER encoder */ |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 31 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 32 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 33 | 0, |
| 34 | 0, |
| 35 | #else |
| 36 | INTEGER_decode_uper, /* Unaligned PER decoder */ |
| 37 | INTEGER_encode_uper, /* Unaligned PER encoder */ |
| 38 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 39 | INTEGER_random_fill, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 40 | 0 /* Use generic outmost tag fetcher */ |
| 41 | }; |
| 42 | asn_TYPE_descriptor_t asn_DEF_INTEGER = { |
| 43 | "INTEGER", |
| 44 | "INTEGER", |
| 45 | &asn_OP_INTEGER, |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 46 | asn_DEF_INTEGER_tags, |
| 47 | sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]), |
| 48 | asn_DEF_INTEGER_tags, /* Same as above */ |
| 49 | sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]), |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 50 | { 0, 0, asn_generic_no_constraint }, |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 51 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 52 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 56 | * Encode INTEGER type using DER. |
| 57 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 58 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 59 | INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | int tag_mode, ber_tlv_tag_t tag, |
| 61 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 62 | INTEGER_t *st = (INTEGER_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 63 | |
| 64 | ASN_DEBUG("%s %s as INTEGER (tm=%d)", |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 65 | cb?"Encoding":"Estimating", td->name, tag_mode); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * Canonicalize integer in the buffer. |
| 69 | * (Remove too long sign extension, remove some first 0x00 bytes) |
| 70 | */ |
| 71 | if(st->buf) { |
| 72 | uint8_t *buf = st->buf; |
| 73 | uint8_t *end1 = buf + st->size - 1; |
| 74 | int shift; |
| 75 | |
| 76 | /* Compute the number of superfluous leading bytes */ |
| 77 | for(; buf < end1; buf++) { |
| 78 | /* |
| 79 | * If the contents octets of an integer value encoding |
| 80 | * consist of more than one octet, then the bits of the |
| 81 | * first octet and bit 8 of the second octet: |
| 82 | * a) shall not all be ones; and |
| 83 | * b) shall not all be zero. |
| 84 | */ |
| 85 | switch(*buf) { |
| 86 | case 0x00: if((buf[1] & 0x80) == 0) |
| 87 | continue; |
| 88 | break; |
| 89 | case 0xff: if((buf[1] & 0x80)) |
| 90 | continue; |
| 91 | break; |
| 92 | } |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | /* Remove leading superfluous bytes from the integer */ |
| 97 | shift = buf - st->buf; |
| 98 | if(shift) { |
| 99 | uint8_t *nb = st->buf; |
| 100 | uint8_t *end; |
| 101 | |
| 102 | st->size -= shift; /* New size, minus bad bytes */ |
| 103 | end = nb + st->size; |
| 104 | |
| 105 | for(; nb < end; nb++, buf++) |
| 106 | *nb = *buf; |
| 107 | } |
| 108 | |
| 109 | } /* if(1) */ |
| 110 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 111 | return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 114 | static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value( |
| 115 | const asn_INTEGER_specifics_t *specs, const char *lstart, |
| 116 | const char *lstop); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 117 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 118 | /* |
| 119 | * INTEGER specific human-readable output. |
| 120 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 121 | static ssize_t |
Wim Lewis | 14e6b16 | 2014-07-23 16:06:01 -0700 | [diff] [blame] | 122 | INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 123 | const asn_INTEGER_specifics_t *specs = |
| 124 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 125 | char scratch[32]; /* Enough for 64-bit integer */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 126 | uint8_t *buf = st->buf; |
| 127 | uint8_t *buf_end = st->buf + st->size; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 128 | intmax_t value; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 129 | ssize_t wrote = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 130 | char *p; |
| 131 | int ret; |
| 132 | |
Lev Walkin | 97f8edc | 2013-03-28 04:38:41 -0700 | [diff] [blame] | 133 | if(specs && specs->field_unsigned) |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 134 | ret = asn_INTEGER2umax(st, (uintmax_t *)&value); |
Lev Walkin | 97f8edc | 2013-03-28 04:38:41 -0700 | [diff] [blame] | 135 | else |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 136 | ret = asn_INTEGER2imax(st, &value); |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 137 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 138 | /* Simple case: the integer size is small */ |
Lev Walkin | 97f8edc | 2013-03-28 04:38:41 -0700 | [diff] [blame] | 139 | if(ret == 0) { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 140 | const asn_INTEGER_enum_map_t *el; |
| 141 | size_t scrsize; |
| 142 | char *scr; |
| 143 | |
Lev Walkin | 97f8edc | 2013-03-28 04:38:41 -0700 | [diff] [blame] | 144 | el = (value >= 0 || !specs || !specs->field_unsigned) |
| 145 | ? INTEGER_map_value2enum(specs, value) : 0; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 146 | if(el) { |
| 147 | scrsize = el->enum_len + 32; |
| 148 | scr = (char *)alloca(scrsize); |
| 149 | if(plainOrXER == 0) |
| 150 | ret = snprintf(scr, scrsize, |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 151 | "%" PRIdMAX " (%s)", value, el->enum_name); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 152 | else |
| 153 | ret = snprintf(scr, scrsize, |
| 154 | "<%s/>", el->enum_name); |
| 155 | } else if(plainOrXER && specs && specs->strict_enumeration) { |
| 156 | ASN_DEBUG("ASN.1 forbids dealing with " |
| 157 | "unknown value of ENUMERATED type"); |
| 158 | errno = EPERM; |
| 159 | return -1; |
| 160 | } else { |
| 161 | scrsize = sizeof(scratch); |
| 162 | scr = scratch; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 163 | ret = snprintf( |
| 164 | scr, scrsize, |
| 165 | (specs && specs->field_unsigned) ? "%" PRIuMAX : "%" PRIdMAX, |
| 166 | value); |
| 167 | } |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 168 | assert(ret > 0 && (size_t)ret < scrsize); |
| 169 | return (cb(scr, ret, app_key) < 0) ? -1 : ret; |
| 170 | } else if(plainOrXER && specs && specs->strict_enumeration) { |
| 171 | /* |
| 172 | * Here and earlier, we cannot encode the ENUMERATED values |
| 173 | * if there is no corresponding identifier. |
| 174 | */ |
| 175 | ASN_DEBUG("ASN.1 forbids dealing with " |
| 176 | "unknown value of ENUMERATED type"); |
| 177 | errno = EPERM; |
| 178 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* Output in the long xx:yy:zz... format */ |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 182 | /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 183 | for(p = scratch; buf < buf_end; buf++) { |
Wim Lewis | 59e8d28 | 2014-08-04 12:39:35 -0700 | [diff] [blame] | 184 | const char * const h2c = "0123456789ABCDEF"; |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 185 | if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 186 | /* Flush buffer */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 187 | if(cb(scratch, p - scratch, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 188 | return -1; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 189 | wrote += p - scratch; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 190 | p = scratch; |
| 191 | } |
| 192 | *p++ = h2c[*buf >> 4]; |
| 193 | *p++ = h2c[*buf & 0x0F]; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 194 | *p++ = 0x3a; /* ":" */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 195 | } |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 196 | if(p != scratch) |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 197 | p--; /* Remove the last ":" */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 198 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 199 | wrote += p - scratch; |
| 200 | return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * INTEGER specific human-readable output. |
| 205 | */ |
| 206 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 207 | INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 208 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 209 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 210 | ssize_t ret; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 211 | |
| 212 | (void)td; |
| 213 | (void)ilevel; |
| 214 | |
Lev Walkin | d500a96 | 2005-11-27 13:06:56 +0000 | [diff] [blame] | 215 | if(!st || !st->buf) |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 216 | ret = cb("<absent>", 8, app_key); |
| 217 | else |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 218 | ret = INTEGER__dump(td, st, cb, app_key, 0); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 219 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 220 | return (ret < 0) ? -1 : 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 223 | struct e2v_key { |
| 224 | const char *start; |
| 225 | const char *stop; |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 226 | const asn_INTEGER_enum_map_t *vemap; |
| 227 | const unsigned int *evmap; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 228 | }; |
| 229 | static int |
| 230 | INTEGER__compar_enum2value(const void *kp, const void *am) { |
| 231 | const struct e2v_key *key = (const struct e2v_key *)kp; |
| 232 | const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am; |
| 233 | const char *ptr, *end, *name; |
| 234 | |
| 235 | /* Remap the element (sort by different criterion) */ |
| 236 | el = key->vemap + key->evmap[el - key->vemap]; |
| 237 | |
| 238 | /* Compare strings */ |
| 239 | for(ptr = key->start, end = key->stop, name = el->enum_name; |
| 240 | ptr < end; ptr++, name++) { |
Lev Walkin | 793982a | 2017-10-02 14:12:51 -0700 | [diff] [blame] | 241 | if(*ptr != *name || !*name) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 242 | return *(const unsigned char *)ptr |
| 243 | - *(const unsigned char *)name; |
| 244 | } |
| 245 | return name[0] ? -1 : 0; |
| 246 | } |
| 247 | |
| 248 | static const asn_INTEGER_enum_map_t * |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 249 | INTEGER_map_enum2value(const asn_INTEGER_specifics_t *specs, const char *lstart, |
| 250 | const char *lstop) { |
| 251 | const asn_INTEGER_enum_map_t *el_found; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 252 | int count = specs ? specs->map_count : 0; |
| 253 | struct e2v_key key; |
| 254 | const char *lp; |
| 255 | |
| 256 | if(!count) return NULL; |
| 257 | |
| 258 | /* Guaranteed: assert(lstart < lstop); */ |
| 259 | /* Figure out the tag name */ |
| 260 | for(lstart++, lp = lstart; lp < lstop; lp++) { |
| 261 | switch(*lp) { |
| 262 | case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */ |
| 263 | case 0x2f: /* '/' */ case 0x3e: /* '>' */ |
| 264 | break; |
| 265 | default: |
| 266 | continue; |
| 267 | } |
| 268 | break; |
| 269 | } |
| 270 | if(lp == lstop) return NULL; /* No tag found */ |
| 271 | lstop = lp; |
| 272 | |
| 273 | key.start = lstart; |
| 274 | key.stop = lstop; |
| 275 | key.vemap = specs->value2enum; |
| 276 | key.evmap = specs->enum2value; |
Lev Walkin | 9332b65 | 2005-03-04 11:18:44 +0000 | [diff] [blame] | 277 | el_found = (asn_INTEGER_enum_map_t *)bsearch(&key, |
| 278 | specs->value2enum, count, sizeof(specs->value2enum[0]), |
| 279 | INTEGER__compar_enum2value); |
| 280 | if(el_found) { |
| 281 | /* Remap enum2value into value2enum */ |
| 282 | el_found = key.vemap + key.evmap[el_found - key.vemap]; |
| 283 | } |
| 284 | return el_found; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static int |
| 288 | INTEGER__compar_value2enum(const void *kp, const void *am) { |
| 289 | long a = *(const long *)kp; |
| 290 | const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am; |
| 291 | long b = el->nat_value; |
| 292 | if(a < b) return -1; |
| 293 | else if(a == b) return 0; |
| 294 | else return 1; |
| 295 | } |
| 296 | |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 297 | const asn_INTEGER_enum_map_t * |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 298 | INTEGER_map_value2enum(const asn_INTEGER_specifics_t *specs, long value) { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 299 | int count = specs ? specs->map_count : 0; |
| 300 | if(!count) return 0; |
| 301 | return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum, |
| 302 | count, sizeof(specs->value2enum[0]), |
| 303 | INTEGER__compar_value2enum); |
| 304 | } |
| 305 | |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 306 | static int |
| 307 | INTEGER_st_prealloc(INTEGER_t *st, int min_size) { |
| 308 | void *p = MALLOC(min_size + 1); |
| 309 | if(p) { |
| 310 | void *b = st->buf; |
| 311 | st->size = 0; |
| 312 | st->buf = p; |
| 313 | FREEMEM(b); |
| 314 | return 0; |
| 315 | } else { |
| 316 | return -1; |
| 317 | } |
| 318 | } |
| 319 | |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 320 | /* |
| 321 | * Decode the chunk of XML text encoding INTEGER. |
| 322 | */ |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 323 | static enum xer_pbd_rval |
| 324 | INTEGER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) { |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 325 | INTEGER_t *st = (INTEGER_t *)sptr; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 326 | intmax_t dec_value; |
| 327 | intmax_t hex_value = 0; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 328 | const char *lp; |
| 329 | const char *lstart = (const char *)chunk_buf; |
| 330 | const char *lstop = lstart + chunk_size; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 331 | enum { |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 332 | ST_LEADSPACE, |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 333 | ST_SKIPSPHEX, |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 334 | ST_WAITDIGITS, |
| 335 | ST_DIGITS, |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 336 | ST_DIGITS_TRAILSPACE, |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 337 | ST_HEXDIGIT1, |
| 338 | ST_HEXDIGIT2, |
Lev Walkin | f6da179 | 2012-09-04 14:40:57 -0700 | [diff] [blame] | 339 | ST_HEXDIGITS_TRAILSPACE, |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 340 | ST_HEXCOLON, |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 341 | ST_END_ENUM, |
| 342 | ST_UNEXPECTED |
| 343 | } state = ST_LEADSPACE; |
| 344 | const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */ |
| 345 | const char *dec_value_end = 0; |
Lev Walkin | 0be3a99 | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 346 | |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 347 | if(chunk_size) |
Lev Walkin | 345e4c6 | 2006-10-19 02:44:08 +0000 | [diff] [blame] | 348 | ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x", |
| 349 | (long)chunk_size, *lstart, lstop[-1]); |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 350 | |
Lev Walkin | 3d6fcfe | 2012-01-23 04:05:13 +0000 | [diff] [blame] | 351 | if(INTEGER_st_prealloc(st, (chunk_size/3) + 1)) |
| 352 | return XPBD_SYSTEM_FAILURE; |
| 353 | |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 354 | /* |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 355 | * We may have received a tag here. It will be processed inline. |
| 356 | * Use strtoul()-like code and serialize the result. |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 357 | */ |
Lev Walkin | b375194 | 2012-09-02 19:36:47 -0700 | [diff] [blame] | 358 | for(lp = lstart; lp < lstop; lp++) { |
Lev Walkin | 0be3a99 | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 359 | int lv = *lp; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 360 | switch(lv) { |
| 361 | case 0x09: case 0x0a: case 0x0d: case 0x20: |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 362 | switch(state) { |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 363 | case ST_LEADSPACE: |
| 364 | case ST_DIGITS_TRAILSPACE: |
Lev Walkin | f6da179 | 2012-09-04 14:40:57 -0700 | [diff] [blame] | 365 | case ST_HEXDIGITS_TRAILSPACE: |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 366 | case ST_SKIPSPHEX: |
| 367 | continue; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 368 | case ST_DIGITS: |
| 369 | dec_value_end = lp; |
| 370 | state = ST_DIGITS_TRAILSPACE; |
| 371 | continue; |
Lev Walkin | f6da179 | 2012-09-04 14:40:57 -0700 | [diff] [blame] | 372 | case ST_HEXCOLON: |
| 373 | state = ST_HEXDIGITS_TRAILSPACE; |
| 374 | continue; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 375 | default: |
| 376 | break; |
| 377 | } |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 378 | break; |
| 379 | case 0x2d: /* '-' */ |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 380 | if(state == ST_LEADSPACE) { |
| 381 | dec_value = 0; |
| 382 | dec_value_start = lp; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 383 | state = ST_WAITDIGITS; |
| 384 | continue; |
| 385 | } |
| 386 | break; |
| 387 | case 0x2b: /* '+' */ |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 388 | if(state == ST_LEADSPACE) { |
| 389 | dec_value = 0; |
| 390 | dec_value_start = lp; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 391 | state = ST_WAITDIGITS; |
| 392 | continue; |
| 393 | } |
| 394 | break; |
| 395 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 396 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 397 | switch(state) { |
Lev Walkin | b375194 | 2012-09-02 19:36:47 -0700 | [diff] [blame] | 398 | case ST_DIGITS: continue; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 399 | case ST_SKIPSPHEX: /* Fall through */ |
| 400 | case ST_HEXDIGIT1: |
Lev Walkin | b375194 | 2012-09-02 19:36:47 -0700 | [diff] [blame] | 401 | hex_value = (lv - 0x30) << 4; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 402 | state = ST_HEXDIGIT2; |
| 403 | continue; |
| 404 | case ST_HEXDIGIT2: |
Lev Walkin | b375194 | 2012-09-02 19:36:47 -0700 | [diff] [blame] | 405 | hex_value += (lv - 0x30); |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 406 | state = ST_HEXCOLON; |
Lev Walkin | b375194 | 2012-09-02 19:36:47 -0700 | [diff] [blame] | 407 | st->buf[st->size++] = (uint8_t)hex_value; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 408 | continue; |
| 409 | case ST_HEXCOLON: |
| 410 | return XPBD_BROKEN_ENCODING; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 411 | case ST_LEADSPACE: |
| 412 | dec_value = 0; |
| 413 | dec_value_start = lp; |
| 414 | /* FALL THROUGH */ |
| 415 | case ST_WAITDIGITS: |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 416 | state = ST_DIGITS; |
Lev Walkin | b375194 | 2012-09-02 19:36:47 -0700 | [diff] [blame] | 417 | continue; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 418 | default: |
| 419 | break; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 420 | } |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 421 | break; |
| 422 | case 0x3c: /* '<', start of XML encoded enumeration */ |
| 423 | if(state == ST_LEADSPACE) { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 424 | const asn_INTEGER_enum_map_t *el; |
Lev Walkin | c235011 | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 425 | el = INTEGER_map_enum2value( |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 426 | (const asn_INTEGER_specifics_t *) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 427 | td->specifics, lstart, lstop); |
| 428 | if(el) { |
| 429 | ASN_DEBUG("Found \"%s\" => %ld", |
| 430 | el->enum_name, el->nat_value); |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 431 | dec_value = el->nat_value; |
| 432 | state = ST_END_ENUM; |
Lev Walkin | 806b0bb | 2005-03-09 22:31:22 +0000 | [diff] [blame] | 433 | lp = lstop - 1; |
| 434 | continue; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 435 | } |
| 436 | ASN_DEBUG("Unknown identifier for INTEGER"); |
| 437 | } |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 438 | return XPBD_BROKEN_ENCODING; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 439 | case 0x3a: /* ':' */ |
| 440 | if(state == ST_HEXCOLON) { |
| 441 | /* This colon is expected */ |
| 442 | state = ST_HEXDIGIT1; |
| 443 | continue; |
| 444 | } else if(state == ST_DIGITS) { |
| 445 | /* The colon here means that we have |
| 446 | * decoded the first two hexadecimal |
| 447 | * places as a decimal value. |
| 448 | * Switch decoding mode. */ |
| 449 | ASN_DEBUG("INTEGER re-evaluate as hex form"); |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 450 | state = ST_SKIPSPHEX; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 451 | dec_value_start = 0; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 452 | lp = lstart - 1; |
| 453 | continue; |
| 454 | } else { |
Lev Walkin | 2e76399 | 2011-07-21 01:17:37 +0400 | [diff] [blame] | 455 | ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart)); |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 456 | break; |
| 457 | } |
| 458 | /* [A-Fa-f] */ |
| 459 | case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46: |
| 460 | case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66: |
| 461 | switch(state) { |
| 462 | case ST_SKIPSPHEX: |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 463 | case ST_LEADSPACE: /* Fall through */ |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 464 | case ST_HEXDIGIT1: |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 465 | hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61); |
| 466 | hex_value += 10; |
| 467 | hex_value <<= 4; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 468 | state = ST_HEXDIGIT2; |
| 469 | continue; |
| 470 | case ST_HEXDIGIT2: |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 471 | hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61); |
| 472 | hex_value += 10; |
| 473 | st->buf[st->size++] = (uint8_t)hex_value; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 474 | state = ST_HEXCOLON; |
| 475 | continue; |
| 476 | case ST_DIGITS: |
| 477 | ASN_DEBUG("INTEGER re-evaluate as hex form"); |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 478 | state = ST_SKIPSPHEX; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 479 | dec_value_start = 0; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 480 | lp = lstart - 1; |
| 481 | continue; |
| 482 | default: |
| 483 | break; |
| 484 | } |
| 485 | break; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 486 | } |
Lev Walkin | 806b0bb | 2005-03-09 22:31:22 +0000 | [diff] [blame] | 487 | |
| 488 | /* Found extra non-numeric stuff */ |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 489 | ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld", |
Lev Walkin | 2e76399 | 2011-07-21 01:17:37 +0400 | [diff] [blame] | 490 | lv, (long)(lp - lstart)); |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 491 | state = ST_UNEXPECTED; |
Lev Walkin | 0be3a99 | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 492 | break; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 495 | switch(state) { |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 496 | case ST_END_ENUM: |
| 497 | /* Got a complete and valid enumeration encoded as a tag. */ |
| 498 | break; |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 499 | case ST_DIGITS: |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 500 | dec_value_end = lstop; |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 501 | /* FALL THROUGH */ |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 502 | case ST_DIGITS_TRAILSPACE: |
| 503 | /* The last symbol encountered was a digit. */ |
Lev Walkin | b7c5899 | 2017-10-06 16:36:32 -0700 | [diff] [blame] | 504 | switch(asn_strtoimax_lim(dec_value_start, &dec_value_end, &dec_value)) { |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 505 | case ASN_STRTOX_OK: |
Lev Walkin | b7c5899 | 2017-10-06 16:36:32 -0700 | [diff] [blame] | 506 | if(dec_value >= LONG_MIN && dec_value <= LONG_MAX) { |
| 507 | break; |
| 508 | } else { |
| 509 | /* |
| 510 | * We model INTEGER on long for XER, |
| 511 | * to avoid rewriting all the tests at once. |
| 512 | */ |
| 513 | ASN_DEBUG("INTEGER exceeds long range"); |
| 514 | /* Fall through */ |
| 515 | } |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 516 | case ASN_STRTOX_ERROR_RANGE: |
Lev Walkin | b7c5899 | 2017-10-06 16:36:32 -0700 | [diff] [blame] | 517 | ASN_DEBUG("INTEGER decode %s hit range limit", td->name); |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 518 | return XPBD_DECODER_LIMIT; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 519 | case ASN_STRTOX_ERROR_INVAL: |
| 520 | case ASN_STRTOX_EXPECT_MORE: |
| 521 | case ASN_STRTOX_EXTRA_DATA: |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 522 | return XPBD_BROKEN_ENCODING; |
| 523 | } |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 524 | break; |
| 525 | case ST_HEXCOLON: |
Lev Walkin | f6da179 | 2012-09-04 14:40:57 -0700 | [diff] [blame] | 526 | case ST_HEXDIGITS_TRAILSPACE: |
Lev Walkin | c744a02 | 2006-09-15 18:33:25 +0000 | [diff] [blame] | 527 | st->buf[st->size] = 0; /* Just in case termination */ |
| 528 | return XPBD_BODY_CONSUMED; |
| 529 | case ST_HEXDIGIT1: |
| 530 | case ST_HEXDIGIT2: |
| 531 | case ST_SKIPSPHEX: |
| 532 | return XPBD_BROKEN_ENCODING; |
Lev Walkin | 632f20b | 2012-09-04 13:14:29 -0700 | [diff] [blame] | 533 | case ST_LEADSPACE: |
| 534 | /* Content not found */ |
| 535 | return XPBD_NOT_BODY_IGNORE; |
| 536 | case ST_WAITDIGITS: |
| 537 | case ST_UNEXPECTED: |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 538 | ASN_DEBUG("INTEGER: No useful digits (state %d)", state); |
| 539 | return XPBD_BROKEN_ENCODING; /* No digits */ |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 540 | } |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 541 | |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 542 | /* |
| 543 | * Convert the result of parsing of enumeration or a straight |
| 544 | * decimal value into a BER representation. |
| 545 | */ |
Lev Walkin | b7c5899 | 2017-10-06 16:36:32 -0700 | [diff] [blame] | 546 | if(asn_imax2INTEGER(st, dec_value)) { |
| 547 | ASN_DEBUG("INTEGER decode %s conversion failed", td->name); |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 548 | return XPBD_SYSTEM_FAILURE; |
Lev Walkin | b7c5899 | 2017-10-06 16:36:32 -0700 | [diff] [blame] | 549 | } |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 550 | |
Lev Walkin | 0fab1a6 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 551 | return XPBD_BODY_CONSUMED; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 555 | INTEGER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 556 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 557 | const void *buf_ptr, size_t size) { |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 558 | |
| 559 | return xer_decode_primitive(opt_codec_ctx, td, |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 560 | sptr, sizeof(INTEGER_t), opt_mname, |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 561 | buf_ptr, size, INTEGER__xer_body_decode); |
| 562 | } |
| 563 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 564 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 565 | INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 566 | int ilevel, enum xer_encoder_flags_e flags, |
| 567 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 568 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
| 569 | asn_enc_rval_t er; |
| 570 | |
| 571 | (void)ilevel; |
| 572 | (void)flags; |
| 573 | |
Lev Walkin | d500a96 | 2005-11-27 13:06:56 +0000 | [diff] [blame] | 574 | if(!st || !st->buf) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 575 | ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 576 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 577 | er.encoded = INTEGER__dump(td, st, cb, app_key, 1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 578 | if(er.encoded < 0) ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 579 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 580 | ASN__ENCODED_OK(er); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Lev Walkin | d7703cf | 2012-09-03 01:45:03 -0700 | [diff] [blame] | 583 | #ifndef ASN_DISABLE_PER_SUPPORT |
| 584 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 585 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 586 | INTEGER_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 587 | const asn_per_constraints_t *constraints, void **sptr, |
| 588 | asn_per_data_t *pd) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 589 | const asn_INTEGER_specifics_t *specs = |
| 590 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 591 | asn_dec_rval_t rval = { RC_OK, 0 }; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 592 | INTEGER_t *st = (INTEGER_t *)*sptr; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 593 | const asn_per_constraint_t *ct; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 594 | int repeat; |
| 595 | |
| 596 | (void)opt_codec_ctx; |
| 597 | |
| 598 | if(!st) { |
| 599 | st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st))); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 600 | if(!st) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 603 | if(!constraints) constraints = td->encoding_constraints.per_constraints; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 604 | ct = constraints ? &constraints->value : 0; |
| 605 | |
| 606 | if(ct && ct->flags & APC_EXTENSIBLE) { |
| 607 | int inext = per_get_few_bits(pd, 1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 608 | if(inext < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 609 | if(inext) ct = 0; |
| 610 | } |
| 611 | |
| 612 | FREEMEM(st->buf); |
Lev Walkin | 0b1eb0e | 2007-11-10 02:05:23 +0000 | [diff] [blame] | 613 | st->buf = 0; |
| 614 | st->size = 0; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 615 | if(ct) { |
| 616 | if(ct->flags & APC_SEMI_CONSTRAINED) { |
| 617 | st->buf = (uint8_t *)CALLOC(1, 2); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 618 | if(!st->buf) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 619 | st->size = 1; |
| 620 | } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) { |
| 621 | size_t size = (ct->range_bits + 7) >> 3; |
| 622 | st->buf = (uint8_t *)MALLOC(1 + size + 1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 623 | if(!st->buf) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 624 | st->size = size; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 625 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 628 | /* X.691-2008/11, #13.2.2, constrained whole number */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 629 | if(ct && ct->flags != APC_UNCONSTRAINED) { |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 630 | /* #11.5.6 */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 631 | ASN_DEBUG("Integer with range %d bits", ct->range_bits); |
| 632 | if(ct->range_bits >= 0) { |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 633 | if((size_t)ct->range_bits > 8 * sizeof(unsigned long)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 634 | ASN__DECODE_FAILED; |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 635 | |
| 636 | if(specs && specs->field_unsigned) { |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 637 | unsigned long uvalue = 0; |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 638 | if(uper_get_constrained_whole_number(pd, |
| 639 | &uvalue, ct->range_bits)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 640 | ASN__DECODE_STARVED; |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 641 | ASN_DEBUG("Got value %lu + low %ld", |
| 642 | uvalue, ct->lower_bound); |
| 643 | uvalue += ct->lower_bound; |
| 644 | if(asn_ulong2INTEGER(st, uvalue)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 645 | ASN__DECODE_FAILED; |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 646 | } else { |
Lev Walkin | 38a91df | 2017-09-17 23:52:33 -0700 | [diff] [blame] | 647 | unsigned long uvalue = 0; |
| 648 | long svalue; |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 649 | if(uper_get_constrained_whole_number(pd, |
Lev Walkin | 38a91df | 2017-09-17 23:52:33 -0700 | [diff] [blame] | 650 | &uvalue, ct->range_bits)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 651 | ASN__DECODE_STARVED; |
Lev Walkin | 38a91df | 2017-09-17 23:52:33 -0700 | [diff] [blame] | 652 | ASN_DEBUG("Got value %lu + low %ld", |
| 653 | uvalue, ct->lower_bound); |
| 654 | svalue = ct->lower_bound + (long)uvalue; |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 655 | if(asn_long2INTEGER(st, svalue)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 656 | ASN__DECODE_FAILED; |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 657 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 658 | return rval; |
| 659 | } |
| 660 | } else { |
| 661 | ASN_DEBUG("Decoding unconstrained integer %s", td->name); |
| 662 | } |
| 663 | |
| 664 | /* X.691, #12.2.3, #12.2.4 */ |
| 665 | do { |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 666 | ssize_t len = 0; |
| 667 | void *p = NULL; |
| 668 | int ret = 0; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 669 | |
| 670 | /* Get the PER length */ |
Lev Walkin | 9d1b45f | 2017-10-01 17:04:48 -0700 | [diff] [blame] | 671 | len = uper_get_length(pd, -1, 0, &repeat); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 672 | if(len < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 673 | |
| 674 | p = REALLOC(st->buf, st->size + len + 1); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 675 | if(!p) ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 676 | st->buf = (uint8_t *)p; |
| 677 | |
| 678 | ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 679 | if(ret < 0) ASN__DECODE_STARVED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 680 | st->size += len; |
| 681 | } while(repeat); |
| 682 | st->buf[st->size] = 0; /* JIC */ |
| 683 | |
| 684 | /* #12.2.3 */ |
| 685 | if(ct && ct->lower_bound) { |
| 686 | /* |
| 687 | * TODO: replace by in-place arithmetics. |
| 688 | */ |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 689 | long value = 0; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 690 | if(asn_INTEGER2long(st, &value)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 691 | ASN__DECODE_FAILED; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 692 | if(asn_imax2INTEGER(st, value + ct->lower_bound)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 693 | ASN__DECODE_FAILED; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | return rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 699 | asn_enc_rval_t |
| 700 | INTEGER_encode_uper(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 701 | const asn_per_constraints_t *constraints, void *sptr, |
| 702 | asn_per_outp_t *po) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 703 | const asn_INTEGER_specifics_t *specs = |
| 704 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 705 | asn_enc_rval_t er; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 706 | INTEGER_t *st = (INTEGER_t *)sptr; |
| 707 | const uint8_t *buf; |
| 708 | const uint8_t *end; |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 709 | const asn_per_constraint_t *ct; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 710 | long value = 0; |
Jaroslav Imrich | 2253e6b | 2014-10-31 23:05:21 +0100 | [diff] [blame] | 711 | unsigned long v = 0; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 712 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 713 | if(!st || st->size == 0) ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 714 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 715 | if(!constraints) constraints = td->encoding_constraints.per_constraints; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 716 | ct = constraints ? &constraints->value : 0; |
| 717 | |
| 718 | er.encoded = 0; |
| 719 | |
| 720 | if(ct) { |
| 721 | int inext = 0; |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 722 | if(specs && specs->field_unsigned) { |
| 723 | unsigned long uval; |
| 724 | if(asn_INTEGER2ulong(st, &uval)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 725 | ASN__ENCODE_FAILED; |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 726 | /* Check proper range */ |
| 727 | if(ct->flags & APC_SEMI_CONSTRAINED) { |
| 728 | if(uval < (unsigned long)ct->lower_bound) |
| 729 | inext = 1; |
| 730 | } else if(ct->range_bits >= 0) { |
| 731 | if(uval < (unsigned long)ct->lower_bound |
| 732 | || uval > (unsigned long)ct->upper_bound) |
| 733 | inext = 1; |
| 734 | } |
Lev Walkin | 67afc36 | 2017-10-03 15:47:26 -0700 | [diff] [blame] | 735 | ASN_DEBUG("Value %lu (%02x/%zu) lb %lu ub %lu %s", |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 736 | uval, st->buf[0], st->size, |
| 737 | ct->lower_bound, ct->upper_bound, |
| 738 | inext ? "ext" : "fix"); |
| 739 | value = uval; |
| 740 | } else { |
| 741 | if(asn_INTEGER2long(st, &value)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 742 | ASN__ENCODE_FAILED; |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 743 | /* Check proper range */ |
| 744 | if(ct->flags & APC_SEMI_CONSTRAINED) { |
| 745 | if(value < ct->lower_bound) |
| 746 | inext = 1; |
| 747 | } else if(ct->range_bits >= 0) { |
| 748 | if(value < ct->lower_bound |
| 749 | || value > ct->upper_bound) |
| 750 | inext = 1; |
| 751 | } |
Lev Walkin | 67afc36 | 2017-10-03 15:47:26 -0700 | [diff] [blame] | 752 | ASN_DEBUG("Value %ld (%02x/%zu) lb %ld ub %ld %s", |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 753 | value, st->buf[0], st->size, |
| 754 | ct->lower_bound, ct->upper_bound, |
| 755 | inext ? "ext" : "fix"); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 756 | } |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 757 | if(ct->flags & APC_EXTENSIBLE) { |
| 758 | if(per_put_few_bits(po, inext, 1)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 759 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 760 | if(inext) ct = 0; |
| 761 | } else if(inext) { |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 762 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
| 766 | |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 767 | /* X.691-11/2008, #13.2.2, test if constrained whole number */ |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 768 | if(ct && ct->range_bits >= 0) { |
Lev Walkin | 6c52784 | 2014-02-09 04:34:54 -0800 | [diff] [blame] | 769 | /* #11.5.6 -> #11.3 */ |
Lev Walkin | 58b74eb | 2014-02-10 09:24:39 -0800 | [diff] [blame] | 770 | ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits", |
| 771 | value, value - ct->lower_bound, ct->range_bits); |
Jaroslav Imrich | 2253e6b | 2014-10-31 23:05:21 +0100 | [diff] [blame] | 772 | v = value - ct->lower_bound; |
Lev Walkin | 58b74eb | 2014-02-10 09:24:39 -0800 | [diff] [blame] | 773 | if(uper_put_constrained_whole_number_u(po, v, ct->range_bits)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 774 | ASN__ENCODE_FAILED; |
| 775 | ASN__ENCODED_OK(er); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | if(ct && ct->lower_bound) { |
| 779 | ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound); |
| 780 | /* TODO: adjust lower bound */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 781 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | for(buf = st->buf, end = st->buf + st->size; buf < end;) { |
Lev Walkin | 5d947a8 | 2017-10-03 01:04:03 -0700 | [diff] [blame] | 785 | int need_eom = 0; |
| 786 | ssize_t mayEncode = uper_put_length(po, end - buf, &need_eom); |
| 787 | if(mayEncode < 0) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 788 | ASN__ENCODE_FAILED; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 789 | if(per_put_many_bits(po, buf, 8 * mayEncode)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 790 | ASN__ENCODE_FAILED; |
Lev Walkin | 53827fd | 2006-09-08 01:56:32 +0000 | [diff] [blame] | 791 | buf += mayEncode; |
Lev Walkin | 5d947a8 | 2017-10-03 01:04:03 -0700 | [diff] [blame] | 792 | if(need_eom && uper_put_length(po, 0, 0)) ASN__ENCODE_FAILED; |
| 793 | } |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 794 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 795 | ASN__ENCODED_OK(er); |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Lev Walkin | d7703cf | 2012-09-03 01:45:03 -0700 | [diff] [blame] | 798 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
| 799 | |
Lev Walkin | 642b92f | 2017-09-17 22:16:02 -0700 | [diff] [blame] | 800 | |
| 801 | /* |
| 802 | * This function is only to get rid of Undefined Behavior Sanitizer warning. |
| 803 | */ |
Lev Walkin | a4f6891 | 2017-09-27 02:21:38 +0000 | [diff] [blame] | 804 | static intmax_t CC_ATTR_NO_SANITIZE("shift-base") |
Lev Walkin | 642b92f | 2017-09-17 22:16:02 -0700 | [diff] [blame] | 805 | asn__safe_integer_convert_helper(const uint8_t *b, const uint8_t *end) { |
| 806 | intmax_t value; |
| 807 | |
| 808 | /* Perform the sign initialization */ |
| 809 | /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */ |
| 810 | if((*b >> 7)) { |
| 811 | value = -1; |
| 812 | } else { |
| 813 | value = 0; |
| 814 | } |
| 815 | |
| 816 | /* Conversion engine */ |
| 817 | for(; b < end; b++) { |
| 818 | value = (value << 8) | *b; |
| 819 | } |
| 820 | |
| 821 | return value; |
| 822 | } |
| 823 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 824 | int |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 825 | asn_INTEGER2imax(const INTEGER_t *iptr, intmax_t *lptr) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 826 | uint8_t *b, *end; |
| 827 | size_t size; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 828 | |
| 829 | /* Sanity checking */ |
| 830 | if(!iptr || !iptr->buf || !lptr) { |
| 831 | errno = EINVAL; |
| 832 | return -1; |
| 833 | } |
| 834 | |
| 835 | /* Cache the begin/end of the buffer */ |
| 836 | b = iptr->buf; /* Start of the INTEGER buffer */ |
| 837 | size = iptr->size; |
| 838 | end = b + size; /* Where to stop */ |
| 839 | |
Lev Walkin | 642b92f | 2017-09-17 22:16:02 -0700 | [diff] [blame] | 840 | if(size > sizeof(intmax_t)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 841 | uint8_t *end1 = end - 1; |
| 842 | /* |
| 843 | * Slightly more advanced processing, |
Lev Walkin | 642b92f | 2017-09-17 22:16:02 -0700 | [diff] [blame] | 844 | * able to process INTEGERs with >sizeof(intmax_t) bytes |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 845 | * when the actual value is small, e.g. for intmax_t == int32_t |
| 846 | * (0x0000000000abcdef INTEGER would yield a fine 0x00abcdef int32_t) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 847 | */ |
| 848 | /* Skip out the insignificant leading bytes */ |
| 849 | for(; b < end1; b++) { |
| 850 | switch(*b) { |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 851 | case 0x00: if((b[1] & 0x80) == 0) continue; break; |
| 852 | case 0xff: if((b[1] & 0x80) != 0) continue; break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 853 | } |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | size = end - b; |
Lev Walkin | 642b92f | 2017-09-17 22:16:02 -0700 | [diff] [blame] | 858 | if(size > sizeof(intmax_t)) { |
| 859 | /* Still cannot fit the sizeof(intmax_t) */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 860 | errno = ERANGE; |
| 861 | return -1; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | /* Shortcut processing of a corner case */ |
| 866 | if(end == b) { |
| 867 | *lptr = 0; |
| 868 | return 0; |
| 869 | } |
| 870 | |
Lev Walkin | 642b92f | 2017-09-17 22:16:02 -0700 | [diff] [blame] | 871 | *lptr = asn__safe_integer_convert_helper(b, end); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 872 | return 0; |
| 873 | } |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 874 | |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 875 | /* FIXME: negative INTEGER values are silently interpreted as large unsigned ones. */ |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 876 | int |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 877 | asn_INTEGER2umax(const INTEGER_t *iptr, uintmax_t *lptr) { |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 878 | uint8_t *b, *end; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 879 | uintmax_t value; |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 880 | size_t size; |
| 881 | |
| 882 | if(!iptr || !iptr->buf || !lptr) { |
| 883 | errno = EINVAL; |
| 884 | return -1; |
| 885 | } |
| 886 | |
| 887 | b = iptr->buf; |
| 888 | size = iptr->size; |
| 889 | end = b + size; |
| 890 | |
| 891 | /* If all extra leading bytes are zeroes, ignore them */ |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 892 | for(; size > sizeof(value); b++, size--) { |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 893 | if(*b) { |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 894 | /* Value won't fit into uintmax_t */ |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 895 | errno = ERANGE; |
| 896 | return -1; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | /* Conversion engine */ |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 901 | for(value = 0; b < end; b++) |
| 902 | value = (value << 8) | *b; |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 903 | |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 904 | *lptr = value; |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | int |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 909 | asn_umax2INTEGER(INTEGER_t *st, uintmax_t value) { |
| 910 | uint8_t *buf; |
| 911 | uint8_t *end; |
| 912 | uint8_t *b; |
| 913 | int shr; |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 914 | |
Lev Walkin | aa067a9 | 2017-10-03 16:09:05 -0700 | [diff] [blame] | 915 | if(value <= ((~(uintmax_t)0) >> 1)) { |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 916 | return asn_imax2INTEGER(st, value); |
| 917 | } |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 918 | |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 919 | buf = (uint8_t *)MALLOC(1 + sizeof(value)); |
| 920 | if(!buf) return -1; |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 921 | |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 922 | end = buf + (sizeof(value) + 1); |
| 923 | buf[0] = 0; /* INTEGERs are signed. 0-byte indicates positive. */ |
| 924 | for(b = buf + 1, shr = (sizeof(value) - 1) * 8; b < end; shr -= 8, b++) |
| 925 | *b = (uint8_t)(value >> shr); |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 926 | |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 927 | if(st->buf) FREEMEM(st->buf); |
| 928 | st->buf = buf; |
| 929 | st->size = 1 + sizeof(value); |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 930 | |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | int |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 935 | asn_imax2INTEGER(INTEGER_t *st, intmax_t value) { |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 936 | uint8_t *buf, *bp; |
| 937 | uint8_t *p; |
| 938 | uint8_t *pstart; |
| 939 | uint8_t *pend1; |
Lev Walkin | d7ad561 | 2004-10-26 08:20:46 +0000 | [diff] [blame] | 940 | int littleEndian = 1; /* Run-time detection */ |
| 941 | int add; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 942 | |
| 943 | if(!st) { |
| 944 | errno = EINVAL; |
| 945 | return -1; |
| 946 | } |
| 947 | |
Lev Walkin | 290b4d6 | 2017-08-28 17:48:35 -0700 | [diff] [blame] | 948 | buf = (uint8_t *)(long *)MALLOC(sizeof(value)); |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 949 | if(!buf) return -1; |
| 950 | |
Lev Walkin | d7ad561 | 2004-10-26 08:20:46 +0000 | [diff] [blame] | 951 | if(*(char *)&littleEndian) { |
| 952 | pstart = (uint8_t *)&value + sizeof(value) - 1; |
| 953 | pend1 = (uint8_t *)&value; |
| 954 | add = -1; |
| 955 | } else { |
| 956 | pstart = (uint8_t *)&value; |
| 957 | pend1 = pstart + sizeof(value) - 1; |
| 958 | add = 1; |
| 959 | } |
| 960 | |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 961 | /* |
| 962 | * If the contents octet consists of more than one octet, |
| 963 | * then bits of the first octet and bit 8 of the second octet: |
| 964 | * a) shall not all be ones; and |
| 965 | * b) shall not all be zero. |
| 966 | */ |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 967 | for(p = pstart; p != pend1; p += add) { |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 968 | switch(*p) { |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 969 | case 0x00: if((*(p+add) & 0x80) == 0) |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 970 | continue; |
| 971 | break; |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 972 | case 0xff: if((*(p+add) & 0x80)) |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 973 | continue; |
| 974 | break; |
| 975 | } |
| 976 | break; |
| 977 | } |
| 978 | /* Copy the integer body */ |
Lev Walkin | 7e0ab88 | 2017-06-28 08:47:47 -0700 | [diff] [blame] | 979 | for(bp = buf, pend1 += add; p != pend1; p += add) |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 980 | *bp++ = *p; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 981 | |
| 982 | if(st->buf) FREEMEM(st->buf); |
| 983 | st->buf = buf; |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 984 | st->size = bp - buf; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 985 | |
| 986 | return 0; |
| 987 | } |
Lev Walkin | b375194 | 2012-09-02 19:36:47 -0700 | [diff] [blame] | 988 | |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 989 | int |
| 990 | asn_INTEGER2long(const INTEGER_t *iptr, long *l) { |
| 991 | intmax_t v; |
| 992 | if(asn_INTEGER2imax(iptr, &v) == 0) { |
| 993 | if(v < LONG_MIN || v > LONG_MAX) { |
| 994 | errno = ERANGE; |
| 995 | return -1; |
| 996 | } |
| 997 | *l = v; |
| 998 | return 0; |
| 999 | } else { |
| 1000 | return -1; |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 1001 | } |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1002 | } |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 1003 | |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1004 | int |
| 1005 | asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *l) { |
| 1006 | uintmax_t v; |
| 1007 | if(asn_INTEGER2umax(iptr, &v) == 0) { |
| 1008 | if(v > ULONG_MAX) { |
| 1009 | errno = ERANGE; |
| 1010 | return -1; |
| 1011 | } |
| 1012 | *l = v; |
| 1013 | return 0; |
| 1014 | } else { |
| 1015 | return -1; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | int |
| 1020 | asn_long2INTEGER(INTEGER_t *st, long value) { |
| 1021 | return asn_imax2INTEGER(st, value); |
| 1022 | } |
| 1023 | |
| 1024 | int |
| 1025 | asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) { |
| 1026 | return asn_imax2INTEGER(st, value); |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * Parse the number in the given string until the given *end position, |
| 1031 | * returning the position after the last parsed character back using the |
| 1032 | * same (*end) pointer. |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1033 | * WARNING: This behavior is different from the standard strtol/strtoimax(3). |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 1034 | */ |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1035 | enum asn_strtox_result_e |
| 1036 | asn_strtoimax_lim(const char *str, const char **end, intmax_t *intp) { |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1037 | int sign = 1; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1038 | intmax_t value; |
Lev Walkin | b375194 | 2012-09-02 19:36:47 -0700 | [diff] [blame] | 1039 | |
Lev Walkin | aa067a9 | 2017-10-03 16:09:05 -0700 | [diff] [blame] | 1040 | #define ASN1_INTMAX_MAX ((~(uintmax_t)0) >> 1) |
| 1041 | |
| 1042 | const intmax_t upper_boundary = ASN1_INTMAX_MAX / 10; |
| 1043 | intmax_t last_digit_max = ASN1_INTMAX_MAX % 10; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1044 | |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1045 | if(str >= *end) return ASN_STRTOX_ERROR_INVAL; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1046 | |
| 1047 | switch(*str) { |
| 1048 | case '-': |
| 1049 | last_digit_max++; |
| 1050 | sign = -1; |
Simo Sorce | 4f47bf5 | 2015-09-03 17:35:04 -0400 | [diff] [blame] | 1051 | /* FALL THROUGH */ |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1052 | case '+': |
| 1053 | str++; |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 1054 | if(str >= *end) { |
| 1055 | *end = str; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1056 | return ASN_STRTOX_EXPECT_MORE; |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 1057 | } |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1060 | for(value = 0; str < (*end); str++) { |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1061 | switch(*str) { |
| 1062 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 1063 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: { |
| 1064 | int d = *str - '0'; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1065 | if(value < upper_boundary) { |
| 1066 | value = value * 10 + d; |
| 1067 | } else if(value == upper_boundary) { |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1068 | if(d <= last_digit_max) { |
| 1069 | if(sign > 0) { |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1070 | value = value * 10 + d; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1071 | } else { |
| 1072 | sign = 1; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1073 | value = -value * 10 - d; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1074 | } |
| 1075 | } else { |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 1076 | *end = str; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1077 | return ASN_STRTOX_ERROR_RANGE; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1078 | } |
| 1079 | } else { |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 1080 | *end = str; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1081 | return ASN_STRTOX_ERROR_RANGE; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | continue; |
| 1085 | default: |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 1086 | *end = str; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1087 | *intp = sign * value; |
| 1088 | return ASN_STRTOX_EXTRA_DATA; |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | |
Lev Walkin | cad560a | 2013-03-16 07:00:58 -0700 | [diff] [blame] | 1092 | *end = str; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 1093 | *intp = sign * value; |
| 1094 | return ASN_STRTOX_OK; |
| 1095 | } |
| 1096 | |
| 1097 | enum asn_strtox_result_e |
| 1098 | asn_strtol_lim(const char *str, const char **end, long *lp) { |
| 1099 | intmax_t value; |
| 1100 | switch(asn_strtoimax_lim(str, end, &value)) { |
| 1101 | case ASN_STRTOX_ERROR_RANGE: |
| 1102 | return ASN_STRTOX_ERROR_RANGE; |
| 1103 | case ASN_STRTOX_ERROR_INVAL: |
| 1104 | return ASN_STRTOX_ERROR_INVAL; |
| 1105 | case ASN_STRTOX_EXPECT_MORE: |
| 1106 | return ASN_STRTOX_EXPECT_MORE; |
| 1107 | case ASN_STRTOX_OK: |
| 1108 | if(value >= LONG_MIN && value <= LONG_MAX) { |
| 1109 | *lp = value; |
| 1110 | return ASN_STRTOX_OK; |
| 1111 | } else { |
| 1112 | return ASN_STRTOX_ERROR_RANGE; |
| 1113 | } |
| 1114 | case ASN_STRTOX_EXTRA_DATA: |
| 1115 | if(value >= LONG_MIN && value <= LONG_MAX) { |
| 1116 | *lp = value; |
| 1117 | return ASN_STRTOX_EXTRA_DATA; |
| 1118 | } else { |
| 1119 | return ASN_STRTOX_ERROR_RANGE; |
| 1120 | } |
| 1121 | } |
Lev Walkin | 5d9e3c5 | 2017-07-05 16:25:24 -0700 | [diff] [blame] | 1122 | |
| 1123 | assert(!"Unreachable"); |
| 1124 | return ASN_STRTOX_ERROR_INVAL; |
Lev Walkin | b375194 | 2012-09-02 19:36:47 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 1127 | int |
| 1128 | INTEGER_compare(const asn_TYPE_descriptor_t *td, const void *aptr, |
| 1129 | const void *bptr) { |
| 1130 | const INTEGER_t *a = aptr; |
| 1131 | const INTEGER_t *b = bptr; |
| 1132 | |
| 1133 | (void)td; |
| 1134 | |
| 1135 | if(a && b) { |
| 1136 | if(a->size && b->size) { |
| 1137 | int sign_a = (a->buf[0] & 0x80) ? -1 : 1; |
| 1138 | int sign_b = (b->buf[0] & 0x80) ? -1 : 1; |
| 1139 | |
| 1140 | if(sign_a < sign_b) return -1; |
| 1141 | if(sign_a > sign_b) return 1; |
| 1142 | |
| 1143 | /* The shortest integer wins, unless comparing negatives */ |
| 1144 | if(a->size < b->size) { |
| 1145 | return -1 * sign_a; |
| 1146 | } else if(a->size > b->size) { |
| 1147 | return 1 * sign_b; |
| 1148 | } |
| 1149 | |
| 1150 | return sign_a * memcmp(a->buf, b->buf, a->size); |
| 1151 | } else if(a->size) { |
| 1152 | int sign = (a->buf[0] & 0x80) ? -1 : 1; |
| 1153 | return (1) * sign; |
| 1154 | } else if(b->size) { |
| 1155 | int sign = (a->buf[0] & 0x80) ? -1 : 1; |
| 1156 | return (-1) * sign; |
| 1157 | } else { |
| 1158 | return 0; |
| 1159 | } |
| 1160 | } else if(!a && !b) { |
| 1161 | return 0; |
| 1162 | } else if(!a) { |
| 1163 | return -1; |
| 1164 | } else { |
| 1165 | return 1; |
| 1166 | } |
| 1167 | |
| 1168 | } |
| 1169 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1170 | asn_random_fill_result_t |
| 1171 | INTEGER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, |
| 1172 | const asn_encoding_constraints_t *constraints, |
| 1173 | size_t max_length) { |
| 1174 | const asn_INTEGER_specifics_t *specs = |
| 1175 | (const asn_INTEGER_specifics_t *)td->specifics; |
| 1176 | asn_random_fill_result_t result_ok = {ARFILL_OK, 1}; |
| 1177 | asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0}; |
| 1178 | asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0}; |
| 1179 | INTEGER_t *st = *sptr; |
| 1180 | const asn_INTEGER_enum_map_t *emap; |
| 1181 | size_t emap_len; |
| 1182 | intmax_t value; |
| 1183 | int find_inside_map; |
| 1184 | |
| 1185 | if(max_length == 0) return result_skipped; |
| 1186 | |
| 1187 | if(st == NULL) { |
| 1188 | st = (INTEGER_t *)CALLOC(1, sizeof(*st)); |
| 1189 | if(st == NULL) { |
| 1190 | return result_failed; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | if(specs) { |
| 1195 | emap = specs->value2enum; |
| 1196 | emap_len = specs->map_count; |
| 1197 | if(specs->strict_enumeration) { |
| 1198 | find_inside_map = emap_len > 0; |
| 1199 | } else { |
| 1200 | find_inside_map = emap_len ? asn_random_between(0, 1) : 0; |
| 1201 | } |
| 1202 | } else { |
| 1203 | emap = 0; |
| 1204 | emap_len = 0; |
| 1205 | find_inside_map = 0; |
| 1206 | } |
| 1207 | |
| 1208 | if(find_inside_map) { |
| 1209 | assert(emap_len > 0); |
| 1210 | value = emap[asn_random_between(0, emap_len - 1)].nat_value; |
| 1211 | } else { |
Lev Walkin | 18741a9 | 2017-10-01 12:40:19 -0700 | [diff] [blame] | 1212 | const asn_per_constraints_t *ct; |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1213 | |
| 1214 | static const long variants[] = { |
| 1215 | -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384, |
| 1216 | -16383, -257, -256, -255, -254, -129, -128, -127, |
| 1217 | -126, -1, 0, 1, 126, 127, 128, 129, |
| 1218 | 254, 255, 256, 257, 16383, 16384, 16385, 32767, |
| 1219 | 32768, 32769, 65534, 65535, 65536, 65537}; |
| 1220 | if(specs && specs->field_unsigned) { |
| 1221 | assert(variants[18] == 0); |
| 1222 | value = variants[asn_random_between( |
| 1223 | 18, sizeof(variants) / sizeof(variants[0]) - 1)]; |
| 1224 | } else { |
| 1225 | value = variants[asn_random_between( |
| 1226 | 0, sizeof(variants) / sizeof(variants[0]) - 1)]; |
| 1227 | } |
| 1228 | |
| 1229 | if(!constraints) constraints = &td->encoding_constraints; |
Lev Walkin | 18741a9 | 2017-10-01 12:40:19 -0700 | [diff] [blame] | 1230 | ct = constraints ? constraints->per_constraints : 0; |
| 1231 | if(ct && (ct->value.flags & APC_CONSTRAINED)) { |
| 1232 | if(value < ct->value.lower_bound || value > ct->value.upper_bound) { |
| 1233 | value = asn_random_between(ct->value.lower_bound, |
| 1234 | ct->value.upper_bound); |
| 1235 | } |
| 1236 | } |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1239 | if(asn_imax2INTEGER(st, value)) { |
| 1240 | if(st == *sptr) { |
| 1241 | ASN_STRUCT_RESET(*td, st); |
| 1242 | } else { |
| 1243 | ASN_STRUCT_FREE(*td, st); |
| 1244 | } |
| 1245 | return result_failed; |
| 1246 | } else { |
Lev Walkin | 18741a9 | 2017-10-01 12:40:19 -0700 | [diff] [blame] | 1247 | *sptr = st; |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 1248 | result_ok.length = st->size; |
| 1249 | return result_ok; |
| 1250 | } |
| 1251 | } |