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