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