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