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