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