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