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