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