Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>. |
| 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 <assert.h> |
| 10 | #include <errno.h> |
| 11 | |
| 12 | /* |
| 13 | * INTEGER basic type description. |
| 14 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 15 | static ber_tlv_tag_t asn_DEF_INTEGER_tags[] = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 16 | (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)) |
| 17 | }; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 18 | asn_TYPE_descriptor_t asn_DEF_INTEGER = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | "INTEGER", |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 20 | "INTEGER", |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 21 | ASN__PRIMITIVE_TYPE_free, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 22 | INTEGER_print, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 23 | asn_generic_no_constraint, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 24 | ber_decode_primitive, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | INTEGER_encode_der, |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 26 | INTEGER_decode_xer, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 27 | INTEGER_encode_xer, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 29 | asn_DEF_INTEGER_tags, |
| 30 | sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]), |
| 31 | asn_DEF_INTEGER_tags, /* Same as above */ |
| 32 | sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]), |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 33 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 34 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | * Encode INTEGER type using DER. |
| 39 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 40 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 41 | INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | int tag_mode, ber_tlv_tag_t tag, |
| 43 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 44 | INTEGER_t *st = (INTEGER_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | |
| 46 | ASN_DEBUG("%s %s as INTEGER (tm=%d)", |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 47 | cb?"Encoding":"Estimating", td->name, tag_mode); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Canonicalize integer in the buffer. |
| 51 | * (Remove too long sign extension, remove some first 0x00 bytes) |
| 52 | */ |
| 53 | if(st->buf) { |
| 54 | uint8_t *buf = st->buf; |
| 55 | uint8_t *end1 = buf + st->size - 1; |
| 56 | int shift; |
| 57 | |
| 58 | /* Compute the number of superfluous leading bytes */ |
| 59 | for(; buf < end1; buf++) { |
| 60 | /* |
| 61 | * If the contents octets of an integer value encoding |
| 62 | * consist of more than one octet, then the bits of the |
| 63 | * first octet and bit 8 of the second octet: |
| 64 | * a) shall not all be ones; and |
| 65 | * b) shall not all be zero. |
| 66 | */ |
| 67 | switch(*buf) { |
| 68 | case 0x00: if((buf[1] & 0x80) == 0) |
| 69 | continue; |
| 70 | break; |
| 71 | case 0xff: if((buf[1] & 0x80)) |
| 72 | continue; |
| 73 | break; |
| 74 | } |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | /* Remove leading superfluous bytes from the integer */ |
| 79 | shift = buf - st->buf; |
| 80 | if(shift) { |
| 81 | uint8_t *nb = st->buf; |
| 82 | uint8_t *end; |
| 83 | |
| 84 | st->size -= shift; /* New size, minus bad bytes */ |
| 85 | end = nb + st->size; |
| 86 | |
| 87 | for(; nb < end; nb++, buf++) |
| 88 | *nb = *buf; |
| 89 | } |
| 90 | |
| 91 | } /* if(1) */ |
| 92 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 93 | return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 96 | static const asn_INTEGER_enum_map_t *INTEGER__map_value2enum(asn_INTEGER_specifics_t *specs, long value); |
| 97 | static const asn_INTEGER_enum_map_t *INTEGER__map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop); |
| 98 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 99 | /* |
| 100 | * INTEGER specific human-readable output. |
| 101 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 102 | static ssize_t |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 103 | INTEGER__dump(asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) { |
| 104 | asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics; |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 105 | char scratch[32]; /* Enough for 64-bit integer */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | uint8_t *buf = st->buf; |
| 107 | uint8_t *buf_end = st->buf + st->size; |
| 108 | signed long accum; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 109 | ssize_t wrote = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | char *p; |
| 111 | int ret; |
| 112 | |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 113 | /* |
| 114 | * Advance buf pointer until the start of the value's body. |
| 115 | * This will make us able to process large integers using simple case, |
| 116 | * when the actual value is small |
| 117 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 118 | */ |
| 119 | /* Skip the insignificant leading bytes */ |
| 120 | for(; buf < buf_end-1; buf++) { |
| 121 | switch(*buf) { |
| 122 | case 0x00: if((buf[1] & 0x80) == 0) continue; break; |
| 123 | case 0xff: if((buf[1] & 0x80) != 0) continue; break; |
| 124 | } |
| 125 | break; |
| 126 | } |
| 127 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 128 | /* Simple case: the integer size is small */ |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 129 | if((size_t)(buf_end - buf) <= sizeof(accum)) { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 130 | const asn_INTEGER_enum_map_t *el; |
| 131 | size_t scrsize; |
| 132 | char *scr; |
| 133 | |
| 134 | if(buf == buf_end) { |
| 135 | accum = 0; |
| 136 | } else { |
| 137 | accum = (*buf & 0x80) ? -1 : 0; |
| 138 | for(; buf < buf_end; buf++) |
| 139 | accum = (accum << 8) | *buf; |
| 140 | } |
| 141 | |
| 142 | el = INTEGER__map_value2enum(specs, accum); |
| 143 | if(el) { |
| 144 | scrsize = el->enum_len + 32; |
| 145 | scr = (char *)alloca(scrsize); |
| 146 | if(plainOrXER == 0) |
| 147 | ret = snprintf(scr, scrsize, |
| 148 | "%ld (%s)", accum, el->enum_name); |
| 149 | else |
| 150 | ret = snprintf(scr, scrsize, |
| 151 | "<%s/>", el->enum_name); |
| 152 | } else if(plainOrXER && specs && specs->strict_enumeration) { |
| 153 | ASN_DEBUG("ASN.1 forbids dealing with " |
| 154 | "unknown value of ENUMERATED type"); |
| 155 | errno = EPERM; |
| 156 | return -1; |
| 157 | } else { |
| 158 | scrsize = sizeof(scratch); |
| 159 | scr = scratch; |
| 160 | ret = snprintf(scr, scrsize, "%ld", accum); |
| 161 | } |
| 162 | assert(ret > 0 && (size_t)ret < scrsize); |
| 163 | return (cb(scr, ret, app_key) < 0) ? -1 : ret; |
| 164 | } else if(plainOrXER && specs && specs->strict_enumeration) { |
| 165 | /* |
| 166 | * Here and earlier, we cannot encode the ENUMERATED values |
| 167 | * if there is no corresponding identifier. |
| 168 | */ |
| 169 | ASN_DEBUG("ASN.1 forbids dealing with " |
| 170 | "unknown value of ENUMERATED type"); |
| 171 | errno = EPERM; |
| 172 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* Output in the long xx:yy:zz... format */ |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 176 | /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 177 | for(p = scratch; buf < buf_end; buf++) { |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 178 | static const char *h2c = "0123456789ABCDEF"; |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 179 | if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 180 | /* Flush buffer */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 181 | if(cb(scratch, p - scratch, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 182 | return -1; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 183 | wrote += p - scratch; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 184 | p = scratch; |
| 185 | } |
| 186 | *p++ = h2c[*buf >> 4]; |
| 187 | *p++ = h2c[*buf & 0x0F]; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 188 | *p++ = 0x3a; /* ":" */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 189 | } |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 190 | if(p != scratch) |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 191 | p--; /* Remove the last ":" */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 193 | wrote += p - scratch; |
| 194 | return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * INTEGER specific human-readable output. |
| 199 | */ |
| 200 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 201 | INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 202 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 203 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 204 | ssize_t ret; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 205 | |
| 206 | (void)td; |
| 207 | (void)ilevel; |
| 208 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 209 | if(!st && !st->buf) |
| 210 | ret = cb("<absent>", 8, app_key); |
| 211 | else |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 212 | ret = INTEGER__dump(td, st, cb, app_key, 0); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 213 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 214 | return (ret < 0) ? -1 : 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 217 | struct e2v_key { |
| 218 | const char *start; |
| 219 | const char *stop; |
| 220 | asn_INTEGER_enum_map_t *vemap; |
| 221 | unsigned int *evmap; |
| 222 | }; |
| 223 | static int |
| 224 | INTEGER__compar_enum2value(const void *kp, const void *am) { |
| 225 | const struct e2v_key *key = (const struct e2v_key *)kp; |
| 226 | const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am; |
| 227 | const char *ptr, *end, *name; |
| 228 | |
| 229 | /* Remap the element (sort by different criterion) */ |
| 230 | el = key->vemap + key->evmap[el - key->vemap]; |
| 231 | |
| 232 | /* Compare strings */ |
| 233 | for(ptr = key->start, end = key->stop, name = el->enum_name; |
| 234 | ptr < end; ptr++, name++) { |
| 235 | if(*ptr != *name) |
| 236 | return *(const unsigned char *)ptr |
| 237 | - *(const unsigned char *)name; |
| 238 | } |
| 239 | return name[0] ? -1 : 0; |
| 240 | } |
| 241 | |
| 242 | static const asn_INTEGER_enum_map_t * |
| 243 | 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] | 244 | asn_INTEGER_enum_map_t *el_found; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 245 | int count = specs ? specs->map_count : 0; |
| 246 | struct e2v_key key; |
| 247 | const char *lp; |
| 248 | |
| 249 | if(!count) return NULL; |
| 250 | |
| 251 | /* Guaranteed: assert(lstart < lstop); */ |
| 252 | /* Figure out the tag name */ |
| 253 | for(lstart++, lp = lstart; lp < lstop; lp++) { |
| 254 | switch(*lp) { |
| 255 | case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */ |
| 256 | case 0x2f: /* '/' */ case 0x3e: /* '>' */ |
| 257 | break; |
| 258 | default: |
| 259 | continue; |
| 260 | } |
| 261 | break; |
| 262 | } |
| 263 | if(lp == lstop) return NULL; /* No tag found */ |
| 264 | lstop = lp; |
| 265 | |
| 266 | key.start = lstart; |
| 267 | key.stop = lstop; |
| 268 | key.vemap = specs->value2enum; |
| 269 | key.evmap = specs->enum2value; |
Lev Walkin | 9332b65 | 2005-03-04 11:18:44 +0000 | [diff] [blame] | 270 | el_found = (asn_INTEGER_enum_map_t *)bsearch(&key, |
| 271 | specs->value2enum, count, sizeof(specs->value2enum[0]), |
| 272 | INTEGER__compar_enum2value); |
| 273 | if(el_found) { |
| 274 | /* Remap enum2value into value2enum */ |
| 275 | el_found = key.vemap + key.evmap[el_found - key.vemap]; |
| 276 | } |
| 277 | return el_found; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static int |
| 281 | INTEGER__compar_value2enum(const void *kp, const void *am) { |
| 282 | long a = *(const long *)kp; |
| 283 | const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am; |
| 284 | long b = el->nat_value; |
| 285 | if(a < b) return -1; |
| 286 | else if(a == b) return 0; |
| 287 | else return 1; |
| 288 | } |
| 289 | |
| 290 | static const asn_INTEGER_enum_map_t * |
| 291 | INTEGER__map_value2enum(asn_INTEGER_specifics_t *specs, long value) { |
| 292 | int count = specs ? specs->map_count : 0; |
| 293 | if(!count) return 0; |
| 294 | return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum, |
| 295 | count, sizeof(specs->value2enum[0]), |
| 296 | INTEGER__compar_value2enum); |
| 297 | } |
| 298 | |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 299 | /* |
| 300 | * Decode the chunk of XML text encoding INTEGER. |
| 301 | */ |
| 302 | static ssize_t |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 303 | INTEGER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, void *chunk_buf, size_t chunk_size) { |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 304 | INTEGER_t *st = (INTEGER_t *)sptr; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 305 | long sign = 1; |
| 306 | long value; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 307 | const char *lp; |
| 308 | const char *lstart = (const char *)chunk_buf; |
| 309 | const char *lstop = lstart + chunk_size; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 310 | enum { |
| 311 | ST_SKIPSPACE, |
| 312 | ST_WAITDIGITS, |
| 313 | ST_DIGITS, |
| 314 | } state = ST_SKIPSPACE; |
Lev Walkin | 0be3a99 | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 315 | |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 316 | /* |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 317 | * We may have received a tag here. It will be processed inline. |
| 318 | * Use strtoul()-like code and serialize the result. |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 319 | */ |
Lev Walkin | 0be3a99 | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 320 | for(value = 0, lp = lstart; lp < lstop; lp++) { |
| 321 | int lv = *lp; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 322 | switch(lv) { |
| 323 | case 0x09: case 0x0a: case 0x0d: case 0x20: |
| 324 | if(state == ST_SKIPSPACE) continue; |
| 325 | break; |
| 326 | case 0x2d: /* '-' */ |
| 327 | if(state == ST_SKIPSPACE) { |
| 328 | sign = -1; |
| 329 | state = ST_WAITDIGITS; |
| 330 | continue; |
| 331 | } |
| 332 | break; |
| 333 | case 0x2b: /* '+' */ |
| 334 | if(state == ST_SKIPSPACE) { |
| 335 | state = ST_WAITDIGITS; |
| 336 | continue; |
| 337 | } |
| 338 | break; |
| 339 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 340 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 341 | if(state != ST_DIGITS) state = ST_DIGITS; |
| 342 | |
Lev Walkin | 0be3a99 | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 343 | { |
| 344 | long new_value = value * 10; |
| 345 | |
| 346 | if(new_value / 10 != value) |
| 347 | /* Overflow */ |
| 348 | return -1; |
| 349 | |
| 350 | value = new_value + (lv - 0x30); |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 351 | /* Check for two's complement overflow */ |
| 352 | if(value < 0) { |
| 353 | /* Check whether it is a LONG_MIN */ |
| 354 | if(sign == -1 |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 355 | && (unsigned long)value |
| 356 | == ~((unsigned long)-1 >> 1)) { |
Lev Walkin | 0be3a99 | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 357 | sign = 1; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 358 | } else { |
| 359 | /* Overflow */ |
| 360 | return -1; |
| 361 | } |
| 362 | } |
Lev Walkin | 0be3a99 | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 363 | } |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 364 | continue; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 365 | case 0x3c: /* '<' */ |
| 366 | if(state == ST_SKIPSPACE) { |
| 367 | const asn_INTEGER_enum_map_t *el; |
| 368 | el = INTEGER__map_enum2value( |
| 369 | (asn_INTEGER_specifics_t *) |
| 370 | td->specifics, lstart, lstop); |
| 371 | if(el) { |
| 372 | ASN_DEBUG("Found \"%s\" => %ld", |
| 373 | el->enum_name, el->nat_value); |
| 374 | state = ST_DIGITS; |
| 375 | value = el->nat_value; |
| 376 | break; |
| 377 | } |
| 378 | ASN_DEBUG("Unknown identifier for INTEGER"); |
| 379 | } |
| 380 | return -1; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 381 | } |
Lev Walkin | 0be3a99 | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 382 | break; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | if(state != ST_DIGITS) |
| 386 | return -1; /* No digits */ |
| 387 | |
| 388 | value *= sign; /* Change sign, if needed */ |
| 389 | |
| 390 | if(asn_long2INTEGER(st, value)) |
| 391 | return -1; |
| 392 | |
Lev Walkin | 0be3a99 | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 393 | return lp - lstart; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | asn_dec_rval_t |
| 397 | INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 398 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
| 399 | void *buf_ptr, size_t size) { |
| 400 | |
| 401 | return xer_decode_primitive(opt_codec_ctx, td, |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 402 | sptr, sizeof(INTEGER_t), opt_mname, |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 403 | buf_ptr, size, INTEGER__xer_body_decode); |
| 404 | } |
| 405 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 406 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 407 | INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 408 | int ilevel, enum xer_encoder_flags_e flags, |
| 409 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 410 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
| 411 | asn_enc_rval_t er; |
| 412 | |
| 413 | (void)ilevel; |
| 414 | (void)flags; |
| 415 | |
| 416 | if(!st && !st->buf) |
| 417 | _ASN_ENCODE_FAILED; |
| 418 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 419 | er.encoded = INTEGER__dump(td, st, cb, app_key, 1); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 420 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 421 | |
| 422 | return er; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 425 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 426 | asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 427 | uint8_t *b, *end; |
| 428 | size_t size; |
| 429 | long l; |
| 430 | |
| 431 | /* Sanity checking */ |
| 432 | if(!iptr || !iptr->buf || !lptr) { |
| 433 | errno = EINVAL; |
| 434 | return -1; |
| 435 | } |
| 436 | |
| 437 | /* Cache the begin/end of the buffer */ |
| 438 | b = iptr->buf; /* Start of the INTEGER buffer */ |
| 439 | size = iptr->size; |
| 440 | end = b + size; /* Where to stop */ |
| 441 | |
| 442 | if(size > sizeof(long)) { |
| 443 | uint8_t *end1 = end - 1; |
| 444 | /* |
| 445 | * Slightly more advanced processing, |
| 446 | * able to >sizeof(long) bytes, |
| 447 | * when the actual value is small |
| 448 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 449 | */ |
| 450 | /* Skip out the insignificant leading bytes */ |
| 451 | for(; b < end1; b++) { |
| 452 | switch(*b) { |
| 453 | case 0x00: if((b[1] & 0x80) == 0) continue; break; |
| 454 | case 0xff: if((b[1] & 0x80) != 0) continue; break; |
| 455 | } |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | size = end - b; |
| 460 | if(size > sizeof(long)) { |
| 461 | /* Still cannot fit the long */ |
| 462 | errno = ERANGE; |
| 463 | return -1; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /* Shortcut processing of a corner case */ |
| 468 | if(end == b) { |
| 469 | *lptr = 0; |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | /* Perform the sign initialization */ |
| 474 | /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */ |
| 475 | if((*b >> 7)) l = -1; else l = 0; |
| 476 | |
| 477 | /* Conversion engine */ |
| 478 | for(; b < end; b++) |
| 479 | l = (l << 8) | *b; |
| 480 | |
| 481 | *lptr = l; |
| 482 | return 0; |
| 483 | } |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 484 | |
| 485 | int |
| 486 | asn_long2INTEGER(INTEGER_t *st, long value) { |
| 487 | uint8_t *buf, *bp; |
| 488 | uint8_t *p; |
| 489 | uint8_t *pstart; |
| 490 | uint8_t *pend1; |
Lev Walkin | d7ad561 | 2004-10-26 08:20:46 +0000 | [diff] [blame] | 491 | int littleEndian = 1; /* Run-time detection */ |
| 492 | int add; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 493 | |
| 494 | if(!st) { |
| 495 | errno = EINVAL; |
| 496 | return -1; |
| 497 | } |
| 498 | |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 499 | buf = (uint8_t *)MALLOC(sizeof(value)); |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 500 | if(!buf) return -1; |
| 501 | |
Lev Walkin | d7ad561 | 2004-10-26 08:20:46 +0000 | [diff] [blame] | 502 | if(*(char *)&littleEndian) { |
| 503 | pstart = (uint8_t *)&value + sizeof(value) - 1; |
| 504 | pend1 = (uint8_t *)&value; |
| 505 | add = -1; |
| 506 | } else { |
| 507 | pstart = (uint8_t *)&value; |
| 508 | pend1 = pstart + sizeof(value) - 1; |
| 509 | add = 1; |
| 510 | } |
| 511 | |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 512 | /* |
| 513 | * If the contents octet consists of more than one octet, |
| 514 | * then bits of the first octet and bit 8 of the second octet: |
| 515 | * a) shall not all be ones; and |
| 516 | * b) shall not all be zero. |
| 517 | */ |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 518 | for(p = pstart; p != pend1; p += add) { |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 519 | switch(*p) { |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 520 | case 0x00: if((*(p+add) & 0x80) == 0) |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 521 | continue; |
| 522 | break; |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 523 | case 0xff: if((*(p+add) & 0x80)) |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 524 | continue; |
| 525 | break; |
| 526 | } |
| 527 | break; |
| 528 | } |
| 529 | /* Copy the integer body */ |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 530 | for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add) |
| 531 | *bp++ = *p; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 532 | |
| 533 | if(st->buf) FREEMEM(st->buf); |
| 534 | st->buf = buf; |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 535 | st->size = bp - buf; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 536 | |
| 537 | return 0; |
| 538 | } |