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