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