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