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