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