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