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 | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 27 | INTEGER_decode_uper, /* Unaligned PER decoder */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 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]), |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 33 | 0, /* No PER visible constraints */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 34 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 35 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /* |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 39 | * Encode INTEGER type using DER. |
| 40 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 41 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 42 | INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 43 | int tag_mode, ber_tlv_tag_t tag, |
| 44 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 45 | INTEGER_t *st = (INTEGER_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 46 | |
| 47 | ASN_DEBUG("%s %s as INTEGER (tm=%d)", |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 48 | cb?"Encoding":"Estimating", td->name, tag_mode); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * Canonicalize integer in the buffer. |
| 52 | * (Remove too long sign extension, remove some first 0x00 bytes) |
| 53 | */ |
| 54 | if(st->buf) { |
| 55 | uint8_t *buf = st->buf; |
| 56 | uint8_t *end1 = buf + st->size - 1; |
| 57 | int shift; |
| 58 | |
| 59 | /* Compute the number of superfluous leading bytes */ |
| 60 | for(; buf < end1; buf++) { |
| 61 | /* |
| 62 | * If the contents octets of an integer value encoding |
| 63 | * consist of more than one octet, then the bits of the |
| 64 | * first octet and bit 8 of the second octet: |
| 65 | * a) shall not all be ones; and |
| 66 | * b) shall not all be zero. |
| 67 | */ |
| 68 | switch(*buf) { |
| 69 | case 0x00: if((buf[1] & 0x80) == 0) |
| 70 | continue; |
| 71 | break; |
| 72 | case 0xff: if((buf[1] & 0x80)) |
| 73 | continue; |
| 74 | break; |
| 75 | } |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | /* Remove leading superfluous bytes from the integer */ |
| 80 | shift = buf - st->buf; |
| 81 | if(shift) { |
| 82 | uint8_t *nb = st->buf; |
| 83 | uint8_t *end; |
| 84 | |
| 85 | st->size -= shift; /* New size, minus bad bytes */ |
| 86 | end = nb + st->size; |
| 87 | |
| 88 | for(; nb < end; nb++, buf++) |
| 89 | *nb = *buf; |
| 90 | } |
| 91 | |
| 92 | } /* if(1) */ |
| 93 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 94 | return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 95 | } |
| 96 | |
vlm | 75fddcc | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 97 | 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] | 98 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 99 | /* |
| 100 | * INTEGER specific human-readable output. |
| 101 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 102 | static ssize_t |
vlm | 80a4859 | 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; |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 105 | char scratch[32]; /* Enough for 64-bit integer */ |
vlm | fa67ddc | 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; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 109 | ssize_t wrote = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | char *p; |
| 111 | int ret; |
| 112 | |
vlm | 7d278c4 | 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 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 128 | /* Simple case: the integer size is small */ |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 129 | if((size_t)(buf_end - buf) <= sizeof(accum)) { |
vlm | 80a4859 | 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 | |
vlm | 75fddcc | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 142 | el = INTEGER_map_value2enum(specs, accum); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 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; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* Output in the long xx:yy:zz... format */ |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 176 | /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 177 | for(p = scratch; buf < buf_end; buf++) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 178 | static const char *h2c = "0123456789ABCDEF"; |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 179 | if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 180 | /* Flush buffer */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 181 | if(cb(scratch, p - scratch, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 182 | return -1; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 183 | wrote += p - scratch; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 184 | p = scratch; |
| 185 | } |
| 186 | *p++ = h2c[*buf >> 4]; |
| 187 | *p++ = h2c[*buf & 0x0F]; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 188 | *p++ = 0x3a; /* ":" */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 189 | } |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 190 | if(p != scratch) |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 191 | p--; /* Remove the last ":" */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | |
vlm | 39ba4c4 | 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 |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 201 | INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | 39ba4c4 | 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; |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 204 | ssize_t ret; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 205 | |
| 206 | (void)td; |
| 207 | (void)ilevel; |
| 208 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 209 | if(!st && !st->buf) |
| 210 | ret = cb("<absent>", 8, app_key); |
| 211 | else |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 212 | ret = INTEGER__dump(td, st, cb, app_key, 0); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 213 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 214 | return (ret < 0) ? -1 : 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 215 | } |
| 216 | |
vlm | 80a4859 | 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 * |
vlm | 75fddcc | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 243 | 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] | 244 | asn_INTEGER_enum_map_t *el_found; |
vlm | 80a4859 | 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; |
vlm | e26febf | 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; |
vlm | 80a4859 | 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 | |
vlm | 75fddcc | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 290 | const asn_INTEGER_enum_map_t * |
| 291 | INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) { |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 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 | |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 299 | /* |
| 300 | * Decode the chunk of XML text encoding INTEGER. |
| 301 | */ |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 302 | static enum xer_pbd_rval |
| 303 | 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] | 304 | INTEGER_t *st = (INTEGER_t *)sptr; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 305 | long sign = 1; |
| 306 | long value; |
vlm | 80a4859 | 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; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 310 | enum { |
| 311 | ST_SKIPSPACE, |
| 312 | ST_WAITDIGITS, |
| 313 | ST_DIGITS, |
vlm | e66fd8a | 2005-07-21 09:32:49 +0000 | [diff] [blame] | 314 | ST_EXTRASTUFF |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 315 | } state = ST_SKIPSPACE; |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 316 | |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 317 | /* |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 318 | * We may have received a tag here. It will be processed inline. |
| 319 | * Use strtoul()-like code and serialize the result. |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 320 | */ |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 321 | for(value = 0, lp = lstart; lp < lstop; lp++) { |
| 322 | int lv = *lp; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 323 | switch(lv) { |
| 324 | case 0x09: case 0x0a: case 0x0d: case 0x20: |
| 325 | if(state == ST_SKIPSPACE) continue; |
| 326 | break; |
| 327 | case 0x2d: /* '-' */ |
| 328 | if(state == ST_SKIPSPACE) { |
| 329 | sign = -1; |
| 330 | state = ST_WAITDIGITS; |
| 331 | continue; |
| 332 | } |
| 333 | break; |
| 334 | case 0x2b: /* '+' */ |
| 335 | if(state == ST_SKIPSPACE) { |
| 336 | state = ST_WAITDIGITS; |
| 337 | continue; |
| 338 | } |
| 339 | break; |
| 340 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 341 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 342 | if(state != ST_DIGITS) state = ST_DIGITS; |
| 343 | |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 344 | { |
| 345 | long new_value = value * 10; |
| 346 | |
| 347 | if(new_value / 10 != value) |
| 348 | /* Overflow */ |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 349 | return XPBD_DECODER_LIMIT; |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 350 | |
| 351 | value = new_value + (lv - 0x30); |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 352 | /* Check for two's complement overflow */ |
| 353 | if(value < 0) { |
| 354 | /* Check whether it is a LONG_MIN */ |
| 355 | if(sign == -1 |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 356 | && (unsigned long)value |
| 357 | == ~((unsigned long)-1 >> 1)) { |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 358 | sign = 1; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 359 | } else { |
| 360 | /* Overflow */ |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 361 | return XPBD_DECODER_LIMIT; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 364 | } |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 365 | continue; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 366 | case 0x3c: /* '<' */ |
| 367 | if(state == ST_SKIPSPACE) { |
| 368 | const asn_INTEGER_enum_map_t *el; |
vlm | 75fddcc | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 369 | el = INTEGER_map_enum2value( |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 370 | (asn_INTEGER_specifics_t *) |
| 371 | td->specifics, lstart, lstop); |
| 372 | if(el) { |
| 373 | ASN_DEBUG("Found \"%s\" => %ld", |
| 374 | el->enum_name, el->nat_value); |
| 375 | state = ST_DIGITS; |
| 376 | value = el->nat_value; |
vlm | 585e79f | 2005-03-09 22:31:22 +0000 | [diff] [blame] | 377 | lp = lstop - 1; |
| 378 | continue; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 379 | } |
| 380 | ASN_DEBUG("Unknown identifier for INTEGER"); |
| 381 | } |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 382 | return XPBD_BROKEN_ENCODING; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 383 | } |
vlm | 585e79f | 2005-03-09 22:31:22 +0000 | [diff] [blame] | 384 | |
| 385 | /* Found extra non-numeric stuff */ |
| 386 | state = ST_EXTRASTUFF; |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 387 | break; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 388 | } |
| 389 | |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 390 | if(state != ST_DIGITS) { |
vlm | 585e79f | 2005-03-09 22:31:22 +0000 | [diff] [blame] | 391 | if(xer_is_whitespace(lp, lstop - lp)) { |
| 392 | if(state != ST_EXTRASTUFF) |
| 393 | return XPBD_NOT_BODY_IGNORE; |
| 394 | /* Fall through */ |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 395 | } else { |
| 396 | ASN_DEBUG("No useful digits in output"); |
| 397 | return XPBD_BROKEN_ENCODING; /* No digits */ |
| 398 | } |
| 399 | } |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 400 | |
| 401 | value *= sign; /* Change sign, if needed */ |
| 402 | |
| 403 | if(asn_long2INTEGER(st, value)) |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 404 | return XPBD_SYSTEM_FAILURE; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 405 | |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 406 | return XPBD_BODY_CONSUMED; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | asn_dec_rval_t |
| 410 | INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 411 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 412 | const void *buf_ptr, size_t size) { |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 413 | |
| 414 | return xer_decode_primitive(opt_codec_ctx, td, |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 415 | sptr, sizeof(INTEGER_t), opt_mname, |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 416 | buf_ptr, size, INTEGER__xer_body_decode); |
| 417 | } |
| 418 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 419 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 420 | INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 421 | int ilevel, enum xer_encoder_flags_e flags, |
| 422 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 423 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
| 424 | asn_enc_rval_t er; |
| 425 | |
| 426 | (void)ilevel; |
| 427 | (void)flags; |
| 428 | |
| 429 | if(!st && !st->buf) |
| 430 | _ASN_ENCODE_FAILED; |
| 431 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 432 | er.encoded = INTEGER__dump(td, st, cb, app_key, 1); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 433 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 434 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 435 | _ASN_ENCODED_OK(er); |
| 436 | } |
| 437 | |
| 438 | asn_dec_rval_t |
| 439 | INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 440 | asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) { |
| 441 | asn_dec_rval_t rval = { RC_OK, 0 }; |
| 442 | INTEGER_t *st = (INTEGER_t *)*sptr; |
| 443 | asn_per_constraint_t *ct; |
| 444 | int repeat; |
| 445 | |
| 446 | (void)opt_codec_ctx; |
| 447 | |
| 448 | if(!st) { |
| 449 | st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st))); |
| 450 | if(!st) _ASN_DECODE_FAILED; |
| 451 | } |
| 452 | |
| 453 | if(!constraints) constraints = td->per_constraints; |
| 454 | ct = constraints ? &constraints->value : 0; |
| 455 | |
| 456 | if(ct && ct->flags & APC_EXTENSIBLE) { |
| 457 | int inext = per_get_few_bits(pd, 1); |
| 458 | if(inext < 0) _ASN_DECODE_FAILED; |
| 459 | if(inext) ct = 0; |
| 460 | } |
| 461 | |
| 462 | FREEMEM(st->buf); |
| 463 | if(ct) { |
| 464 | if(ct->flags & APC_SEMI_CONSTRAINED) { |
| 465 | st->buf = (uint8_t *)CALLOC(1, 2); |
| 466 | if(!st->buf) _ASN_DECODE_FAILED; |
| 467 | st->size = 1; |
| 468 | } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) { |
| 469 | size_t size = (ct->range_bits + 7) >> 3; |
| 470 | st->buf = (uint8_t *)MALLOC(1 + size + 1); |
| 471 | if(!st->buf) _ASN_DECODE_FAILED; |
| 472 | st->size = size; |
| 473 | } else { |
| 474 | st->size = 0; |
| 475 | } |
| 476 | } else { |
| 477 | st->size = 0; |
| 478 | } |
| 479 | |
| 480 | /* X.691, #12.2.2 */ |
| 481 | if(ct && ct->flags != APC_UNCONSTRAINED) { |
| 482 | /* #10.5.6 */ |
| 483 | ASN_DEBUG("Integer with range %d bits", ct->range_bits); |
| 484 | if(ct->range_bits >= 0) { |
| 485 | long value = per_get_few_bits(pd, ct->range_bits); |
| 486 | if(value < 0) _ASN_DECODE_FAILED; |
| 487 | ASN_DEBUG("Got value %ld + low %ld", |
| 488 | value, ct->lower_bound); |
| 489 | value += ct->lower_bound; |
| 490 | if(asn_long2INTEGER(st, value)) |
| 491 | _ASN_DECODE_FAILED; |
| 492 | return rval; |
| 493 | } |
| 494 | } else { |
| 495 | ASN_DEBUG("Decoding unconstrained integer %s", td->name); |
| 496 | } |
| 497 | |
| 498 | /* X.691, #12.2.3, #12.2.4 */ |
| 499 | do { |
| 500 | ssize_t len; |
| 501 | void *p; |
| 502 | int ret; |
| 503 | |
| 504 | /* Get the PER length */ |
| 505 | len = uper_get_length(pd, -1, &repeat); |
| 506 | if(len < 0) _ASN_DECODE_FAILED; |
| 507 | |
| 508 | p = REALLOC(st->buf, st->size + len + 1); |
| 509 | if(!p) _ASN_DECODE_FAILED; |
| 510 | st->buf = (uint8_t *)p; |
| 511 | |
| 512 | ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len); |
| 513 | if(ret < 0) _ASN_DECODE_FAILED; |
| 514 | st->size += len; |
| 515 | } while(repeat); |
| 516 | st->buf[st->size] = 0; /* JIC */ |
| 517 | |
| 518 | /* #12.2.3 */ |
| 519 | if(ct && ct->lower_bound) { |
| 520 | /* |
| 521 | * TODO: replace by in-place arithmetics. |
| 522 | */ |
| 523 | long value; |
| 524 | if(asn_INTEGER2long(st, &value)) |
| 525 | _ASN_DECODE_FAILED; |
| 526 | if(asn_long2INTEGER(st, value + ct->lower_bound)) |
| 527 | _ASN_DECODE_FAILED; |
| 528 | } |
| 529 | |
| 530 | return rval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 531 | } |
| 532 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 533 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 534 | asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 535 | uint8_t *b, *end; |
| 536 | size_t size; |
| 537 | long l; |
| 538 | |
| 539 | /* Sanity checking */ |
| 540 | if(!iptr || !iptr->buf || !lptr) { |
| 541 | errno = EINVAL; |
| 542 | return -1; |
| 543 | } |
| 544 | |
| 545 | /* Cache the begin/end of the buffer */ |
| 546 | b = iptr->buf; /* Start of the INTEGER buffer */ |
| 547 | size = iptr->size; |
| 548 | end = b + size; /* Where to stop */ |
| 549 | |
| 550 | if(size > sizeof(long)) { |
| 551 | uint8_t *end1 = end - 1; |
| 552 | /* |
| 553 | * Slightly more advanced processing, |
| 554 | * able to >sizeof(long) bytes, |
| 555 | * when the actual value is small |
| 556 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 557 | */ |
| 558 | /* Skip out the insignificant leading bytes */ |
| 559 | for(; b < end1; b++) { |
| 560 | switch(*b) { |
| 561 | case 0x00: if((b[1] & 0x80) == 0) continue; break; |
| 562 | case 0xff: if((b[1] & 0x80) != 0) continue; break; |
| 563 | } |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | size = end - b; |
| 568 | if(size > sizeof(long)) { |
| 569 | /* Still cannot fit the long */ |
| 570 | errno = ERANGE; |
| 571 | return -1; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /* Shortcut processing of a corner case */ |
| 576 | if(end == b) { |
| 577 | *lptr = 0; |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | /* Perform the sign initialization */ |
| 582 | /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */ |
| 583 | if((*b >> 7)) l = -1; else l = 0; |
| 584 | |
| 585 | /* Conversion engine */ |
| 586 | for(; b < end; b++) |
| 587 | l = (l << 8) | *b; |
| 588 | |
| 589 | *lptr = l; |
| 590 | return 0; |
| 591 | } |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 592 | |
| 593 | int |
| 594 | asn_long2INTEGER(INTEGER_t *st, long value) { |
| 595 | uint8_t *buf, *bp; |
| 596 | uint8_t *p; |
| 597 | uint8_t *pstart; |
| 598 | uint8_t *pend1; |
vlm | 96a4e46 | 2004-10-26 08:20:46 +0000 | [diff] [blame] | 599 | int littleEndian = 1; /* Run-time detection */ |
| 600 | int add; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 601 | |
| 602 | if(!st) { |
| 603 | errno = EINVAL; |
| 604 | return -1; |
| 605 | } |
| 606 | |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 607 | buf = (uint8_t *)MALLOC(sizeof(value)); |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 608 | if(!buf) return -1; |
| 609 | |
vlm | 96a4e46 | 2004-10-26 08:20:46 +0000 | [diff] [blame] | 610 | if(*(char *)&littleEndian) { |
| 611 | pstart = (uint8_t *)&value + sizeof(value) - 1; |
| 612 | pend1 = (uint8_t *)&value; |
| 613 | add = -1; |
| 614 | } else { |
| 615 | pstart = (uint8_t *)&value; |
| 616 | pend1 = pstart + sizeof(value) - 1; |
| 617 | add = 1; |
| 618 | } |
| 619 | |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 620 | /* |
| 621 | * If the contents octet consists of more than one octet, |
| 622 | * then bits of the first octet and bit 8 of the second octet: |
| 623 | * a) shall not all be ones; and |
| 624 | * b) shall not all be zero. |
| 625 | */ |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 626 | for(p = pstart; p != pend1; p += add) { |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 627 | switch(*p) { |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 628 | case 0x00: if((*(p+add) & 0x80) == 0) |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 629 | continue; |
| 630 | break; |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 631 | case 0xff: if((*(p+add) & 0x80)) |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 632 | continue; |
| 633 | break; |
| 634 | } |
| 635 | break; |
| 636 | } |
| 637 | /* Copy the integer body */ |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 638 | for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add) |
| 639 | *bp++ = *p; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 640 | |
| 641 | if(st->buf) FREEMEM(st->buf); |
| 642 | st->buf = buf; |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 643 | st->size = bp - buf; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 644 | |
| 645 | return 0; |
| 646 | } |