vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>. |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 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 | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 28 | INTEGER_encode_uper, /* Unaligned PER encoder */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 29 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 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]), |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 34 | 0, /* No PER visible constraints */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 35 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 36 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /* |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | * Encode INTEGER type using DER. |
| 41 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 42 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 43 | INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | fa67ddc | 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) { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 46 | INTEGER_t *st = (INTEGER_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | |
| 48 | ASN_DEBUG("%s %s as INTEGER (tm=%d)", |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 49 | cb?"Encoding":"Estimating", td->name, tag_mode); |
vlm | fa67ddc | 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 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 95 | return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 96 | } |
| 97 | |
vlm | 75fddcc | 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); |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 99 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 100 | /* |
| 101 | * INTEGER specific human-readable output. |
| 102 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 103 | static ssize_t |
vlm | 80a4859 | 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; |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 106 | char scratch[32]; /* Enough for 64-bit integer */ |
vlm | fa67ddc | 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; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 110 | ssize_t wrote = 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | char *p; |
| 112 | int ret; |
| 113 | |
vlm | 7d278c4 | 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 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 129 | /* Simple case: the integer size is small */ |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 130 | if((size_t)(buf_end - buf) <= sizeof(accum)) { |
vlm | 80a4859 | 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 | |
vlm | 75fddcc | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 143 | el = INTEGER_map_value2enum(specs, accum); |
vlm | 80a4859 | 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; |
| 161 | ret = snprintf(scr, scrsize, "%ld", accum); |
| 162 | } |
| 163 | assert(ret > 0 && (size_t)ret < scrsize); |
| 164 | return (cb(scr, ret, app_key) < 0) ? -1 : ret; |
| 165 | } else if(plainOrXER && specs && specs->strict_enumeration) { |
| 166 | /* |
| 167 | * Here and earlier, we cannot encode the ENUMERATED values |
| 168 | * if there is no corresponding identifier. |
| 169 | */ |
| 170 | ASN_DEBUG("ASN.1 forbids dealing with " |
| 171 | "unknown value of ENUMERATED type"); |
| 172 | errno = EPERM; |
| 173 | return -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* Output in the long xx:yy:zz... format */ |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 177 | /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 178 | for(p = scratch; buf < buf_end; buf++) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 179 | static const char *h2c = "0123456789ABCDEF"; |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 180 | if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 181 | /* Flush buffer */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 182 | if(cb(scratch, p - scratch, app_key) < 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 183 | return -1; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 184 | wrote += p - scratch; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 185 | p = scratch; |
| 186 | } |
| 187 | *p++ = h2c[*buf >> 4]; |
| 188 | *p++ = h2c[*buf & 0x0F]; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 189 | *p++ = 0x3a; /* ":" */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 190 | } |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 191 | if(p != scratch) |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 192 | p--; /* Remove the last ":" */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 193 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 194 | wrote += p - scratch; |
| 195 | return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * INTEGER specific human-readable output. |
| 200 | */ |
| 201 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 202 | INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 203 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 204 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 205 | ssize_t ret; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 206 | |
| 207 | (void)td; |
| 208 | (void)ilevel; |
| 209 | |
vlm | 9747e67 | 2005-11-27 13:06:56 +0000 | [diff] [blame] | 210 | if(!st || !st->buf) |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 211 | ret = cb("<absent>", 8, app_key); |
| 212 | else |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 213 | ret = INTEGER__dump(td, st, cb, app_key, 0); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 214 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 215 | return (ret < 0) ? -1 : 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 216 | } |
| 217 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 218 | struct e2v_key { |
| 219 | const char *start; |
| 220 | const char *stop; |
| 221 | asn_INTEGER_enum_map_t *vemap; |
| 222 | unsigned int *evmap; |
| 223 | }; |
| 224 | static int |
| 225 | INTEGER__compar_enum2value(const void *kp, const void *am) { |
| 226 | const struct e2v_key *key = (const struct e2v_key *)kp; |
| 227 | const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am; |
| 228 | const char *ptr, *end, *name; |
| 229 | |
| 230 | /* Remap the element (sort by different criterion) */ |
| 231 | el = key->vemap + key->evmap[el - key->vemap]; |
| 232 | |
| 233 | /* Compare strings */ |
| 234 | for(ptr = key->start, end = key->stop, name = el->enum_name; |
| 235 | ptr < end; ptr++, name++) { |
| 236 | if(*ptr != *name) |
| 237 | return *(const unsigned char *)ptr |
| 238 | - *(const unsigned char *)name; |
| 239 | } |
| 240 | return name[0] ? -1 : 0; |
| 241 | } |
| 242 | |
| 243 | static const asn_INTEGER_enum_map_t * |
vlm | 75fddcc | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 244 | 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] | 245 | asn_INTEGER_enum_map_t *el_found; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 246 | int count = specs ? specs->map_count : 0; |
| 247 | struct e2v_key key; |
| 248 | const char *lp; |
| 249 | |
| 250 | if(!count) return NULL; |
| 251 | |
| 252 | /* Guaranteed: assert(lstart < lstop); */ |
| 253 | /* Figure out the tag name */ |
| 254 | for(lstart++, lp = lstart; lp < lstop; lp++) { |
| 255 | switch(*lp) { |
| 256 | case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */ |
| 257 | case 0x2f: /* '/' */ case 0x3e: /* '>' */ |
| 258 | break; |
| 259 | default: |
| 260 | continue; |
| 261 | } |
| 262 | break; |
| 263 | } |
| 264 | if(lp == lstop) return NULL; /* No tag found */ |
| 265 | lstop = lp; |
| 266 | |
| 267 | key.start = lstart; |
| 268 | key.stop = lstop; |
| 269 | key.vemap = specs->value2enum; |
| 270 | key.evmap = specs->enum2value; |
vlm | e26febf | 2005-03-04 11:18:44 +0000 | [diff] [blame] | 271 | el_found = (asn_INTEGER_enum_map_t *)bsearch(&key, |
| 272 | specs->value2enum, count, sizeof(specs->value2enum[0]), |
| 273 | INTEGER__compar_enum2value); |
| 274 | if(el_found) { |
| 275 | /* Remap enum2value into value2enum */ |
| 276 | el_found = key.vemap + key.evmap[el_found - key.vemap]; |
| 277 | } |
| 278 | return el_found; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static int |
| 282 | INTEGER__compar_value2enum(const void *kp, const void *am) { |
| 283 | long a = *(const long *)kp; |
| 284 | const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am; |
| 285 | long b = el->nat_value; |
| 286 | if(a < b) return -1; |
| 287 | else if(a == b) return 0; |
| 288 | else return 1; |
| 289 | } |
| 290 | |
vlm | 75fddcc | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 291 | const asn_INTEGER_enum_map_t * |
| 292 | INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) { |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 293 | int count = specs ? specs->map_count : 0; |
| 294 | if(!count) return 0; |
| 295 | return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum, |
| 296 | count, sizeof(specs->value2enum[0]), |
| 297 | INTEGER__compar_value2enum); |
| 298 | } |
| 299 | |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 300 | /* |
| 301 | * Decode the chunk of XML text encoding INTEGER. |
| 302 | */ |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 303 | static enum xer_pbd_rval |
| 304 | 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] | 305 | INTEGER_t *st = (INTEGER_t *)sptr; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 306 | long sign = 1; |
| 307 | long value; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 308 | const char *lp; |
| 309 | const char *lstart = (const char *)chunk_buf; |
| 310 | const char *lstop = lstart + chunk_size; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 311 | enum { |
| 312 | ST_SKIPSPACE, |
| 313 | ST_WAITDIGITS, |
| 314 | ST_DIGITS, |
vlm | e66fd8a | 2005-07-21 09:32:49 +0000 | [diff] [blame] | 315 | ST_EXTRASTUFF |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 316 | } state = ST_SKIPSPACE; |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 317 | |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 318 | /* |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 319 | * We may have received a tag here. It will be processed inline. |
| 320 | * Use strtoul()-like code and serialize the result. |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 321 | */ |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 322 | for(value = 0, lp = lstart; lp < lstop; lp++) { |
| 323 | int lv = *lp; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 324 | switch(lv) { |
| 325 | case 0x09: case 0x0a: case 0x0d: case 0x20: |
| 326 | if(state == ST_SKIPSPACE) continue; |
| 327 | break; |
| 328 | case 0x2d: /* '-' */ |
| 329 | if(state == ST_SKIPSPACE) { |
| 330 | sign = -1; |
| 331 | state = ST_WAITDIGITS; |
| 332 | continue; |
| 333 | } |
| 334 | break; |
| 335 | case 0x2b: /* '+' */ |
| 336 | if(state == ST_SKIPSPACE) { |
| 337 | state = ST_WAITDIGITS; |
| 338 | continue; |
| 339 | } |
| 340 | break; |
| 341 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 342 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 343 | if(state != ST_DIGITS) state = ST_DIGITS; |
| 344 | |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 345 | { |
| 346 | long new_value = value * 10; |
| 347 | |
| 348 | if(new_value / 10 != value) |
| 349 | /* Overflow */ |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 350 | return XPBD_DECODER_LIMIT; |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 351 | |
| 352 | value = new_value + (lv - 0x30); |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 353 | /* Check for two's complement overflow */ |
| 354 | if(value < 0) { |
| 355 | /* Check whether it is a LONG_MIN */ |
| 356 | if(sign == -1 |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 357 | && (unsigned long)value |
| 358 | == ~((unsigned long)-1 >> 1)) { |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 359 | sign = 1; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 360 | } else { |
| 361 | /* Overflow */ |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 362 | return XPBD_DECODER_LIMIT; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 365 | } |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 366 | continue; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 367 | case 0x3c: /* '<' */ |
| 368 | if(state == ST_SKIPSPACE) { |
| 369 | const asn_INTEGER_enum_map_t *el; |
vlm | 75fddcc | 2005-03-29 17:19:53 +0000 | [diff] [blame] | 370 | el = INTEGER_map_enum2value( |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 371 | (asn_INTEGER_specifics_t *) |
| 372 | td->specifics, lstart, lstop); |
| 373 | if(el) { |
| 374 | ASN_DEBUG("Found \"%s\" => %ld", |
| 375 | el->enum_name, el->nat_value); |
| 376 | state = ST_DIGITS; |
| 377 | value = el->nat_value; |
vlm | 585e79f | 2005-03-09 22:31:22 +0000 | [diff] [blame] | 378 | lp = lstop - 1; |
| 379 | continue; |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 380 | } |
| 381 | ASN_DEBUG("Unknown identifier for INTEGER"); |
| 382 | } |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 383 | return XPBD_BROKEN_ENCODING; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 384 | } |
vlm | 585e79f | 2005-03-09 22:31:22 +0000 | [diff] [blame] | 385 | |
| 386 | /* Found extra non-numeric stuff */ |
| 387 | state = ST_EXTRASTUFF; |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 388 | break; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 389 | } |
| 390 | |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 391 | if(state != ST_DIGITS) { |
vlm | 585e79f | 2005-03-09 22:31:22 +0000 | [diff] [blame] | 392 | if(xer_is_whitespace(lp, lstop - lp)) { |
| 393 | if(state != ST_EXTRASTUFF) |
| 394 | return XPBD_NOT_BODY_IGNORE; |
| 395 | /* Fall through */ |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 396 | } else { |
| 397 | ASN_DEBUG("No useful digits in output"); |
| 398 | return XPBD_BROKEN_ENCODING; /* No digits */ |
| 399 | } |
| 400 | } |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 401 | |
| 402 | value *= sign; /* Change sign, if needed */ |
| 403 | |
| 404 | if(asn_long2INTEGER(st, value)) |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 405 | return XPBD_SYSTEM_FAILURE; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 406 | |
vlm | 4df9cc1 | 2005-03-09 22:19:25 +0000 | [diff] [blame] | 407 | return XPBD_BODY_CONSUMED; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | asn_dec_rval_t |
| 411 | INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 412 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 413 | const void *buf_ptr, size_t size) { |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 414 | |
| 415 | return xer_decode_primitive(opt_codec_ctx, td, |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 416 | sptr, sizeof(INTEGER_t), opt_mname, |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 417 | buf_ptr, size, INTEGER__xer_body_decode); |
| 418 | } |
| 419 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 420 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 421 | INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 422 | int ilevel, enum xer_encoder_flags_e flags, |
| 423 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 424 | const INTEGER_t *st = (const INTEGER_t *)sptr; |
| 425 | asn_enc_rval_t er; |
| 426 | |
| 427 | (void)ilevel; |
| 428 | (void)flags; |
| 429 | |
vlm | 9747e67 | 2005-11-27 13:06:56 +0000 | [diff] [blame] | 430 | if(!st || !st->buf) |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 431 | _ASN_ENCODE_FAILED; |
| 432 | |
vlm | 80a4859 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 433 | er.encoded = INTEGER__dump(td, st, cb, app_key, 1); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 434 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 435 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 436 | _ASN_ENCODED_OK(er); |
| 437 | } |
| 438 | |
| 439 | asn_dec_rval_t |
| 440 | INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, |
| 441 | asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) { |
| 442 | asn_dec_rval_t rval = { RC_OK, 0 }; |
| 443 | INTEGER_t *st = (INTEGER_t *)*sptr; |
| 444 | asn_per_constraint_t *ct; |
| 445 | int repeat; |
| 446 | |
| 447 | (void)opt_codec_ctx; |
| 448 | |
| 449 | if(!st) { |
| 450 | st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st))); |
| 451 | if(!st) _ASN_DECODE_FAILED; |
| 452 | } |
| 453 | |
| 454 | if(!constraints) constraints = td->per_constraints; |
| 455 | ct = constraints ? &constraints->value : 0; |
| 456 | |
| 457 | if(ct && ct->flags & APC_EXTENSIBLE) { |
| 458 | int inext = per_get_few_bits(pd, 1); |
| 459 | if(inext < 0) _ASN_DECODE_FAILED; |
| 460 | if(inext) ct = 0; |
| 461 | } |
| 462 | |
| 463 | FREEMEM(st->buf); |
| 464 | if(ct) { |
| 465 | if(ct->flags & APC_SEMI_CONSTRAINED) { |
| 466 | st->buf = (uint8_t *)CALLOC(1, 2); |
| 467 | if(!st->buf) _ASN_DECODE_FAILED; |
| 468 | st->size = 1; |
| 469 | } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) { |
| 470 | size_t size = (ct->range_bits + 7) >> 3; |
| 471 | st->buf = (uint8_t *)MALLOC(1 + size + 1); |
| 472 | if(!st->buf) _ASN_DECODE_FAILED; |
| 473 | st->size = size; |
| 474 | } else { |
| 475 | st->size = 0; |
| 476 | } |
| 477 | } else { |
| 478 | st->size = 0; |
| 479 | } |
| 480 | |
| 481 | /* X.691, #12.2.2 */ |
| 482 | if(ct && ct->flags != APC_UNCONSTRAINED) { |
| 483 | /* #10.5.6 */ |
| 484 | ASN_DEBUG("Integer with range %d bits", ct->range_bits); |
| 485 | if(ct->range_bits >= 0) { |
| 486 | long value = per_get_few_bits(pd, ct->range_bits); |
| 487 | if(value < 0) _ASN_DECODE_FAILED; |
| 488 | ASN_DEBUG("Got value %ld + low %ld", |
| 489 | value, ct->lower_bound); |
| 490 | value += ct->lower_bound; |
| 491 | if(asn_long2INTEGER(st, value)) |
| 492 | _ASN_DECODE_FAILED; |
| 493 | return rval; |
| 494 | } |
| 495 | } else { |
| 496 | ASN_DEBUG("Decoding unconstrained integer %s", td->name); |
| 497 | } |
| 498 | |
| 499 | /* X.691, #12.2.3, #12.2.4 */ |
| 500 | do { |
| 501 | ssize_t len; |
| 502 | void *p; |
| 503 | int ret; |
| 504 | |
| 505 | /* Get the PER length */ |
| 506 | len = uper_get_length(pd, -1, &repeat); |
| 507 | if(len < 0) _ASN_DECODE_FAILED; |
| 508 | |
| 509 | p = REALLOC(st->buf, st->size + len + 1); |
| 510 | if(!p) _ASN_DECODE_FAILED; |
| 511 | st->buf = (uint8_t *)p; |
| 512 | |
| 513 | ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len); |
| 514 | if(ret < 0) _ASN_DECODE_FAILED; |
| 515 | st->size += len; |
| 516 | } while(repeat); |
| 517 | st->buf[st->size] = 0; /* JIC */ |
| 518 | |
| 519 | /* #12.2.3 */ |
| 520 | if(ct && ct->lower_bound) { |
| 521 | /* |
| 522 | * TODO: replace by in-place arithmetics. |
| 523 | */ |
| 524 | long value; |
| 525 | if(asn_INTEGER2long(st, &value)) |
| 526 | _ASN_DECODE_FAILED; |
| 527 | if(asn_long2INTEGER(st, value + ct->lower_bound)) |
| 528 | _ASN_DECODE_FAILED; |
| 529 | } |
| 530 | |
| 531 | return rval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 532 | } |
| 533 | |
vlm | 18dd82c | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 534 | asn_enc_rval_t |
| 535 | INTEGER_encode_uper(asn_TYPE_descriptor_t *td, |
| 536 | asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) { |
| 537 | asn_enc_rval_t er; |
| 538 | INTEGER_t *st = (INTEGER_t *)sptr; |
| 539 | const uint8_t *buf; |
| 540 | const uint8_t *end; |
| 541 | asn_per_constraint_t *ct; |
| 542 | long value = 0; |
| 543 | |
| 544 | if(!st || st->size == 0) _ASN_ENCODE_FAILED; |
| 545 | |
| 546 | if(!constraints) constraints = td->per_constraints; |
| 547 | ct = constraints ? &constraints->value : 0; |
| 548 | |
| 549 | er.encoded = 0; |
| 550 | |
| 551 | if(ct) { |
| 552 | int inext = 0; |
| 553 | if(asn_INTEGER2long(st, &value)) |
| 554 | _ASN_ENCODE_FAILED; |
| 555 | /* Check proper range */ |
| 556 | if(ct->flags & APC_SEMI_CONSTRAINED) { |
| 557 | if(value < ct->lower_bound) |
| 558 | inext = 1; |
| 559 | } else if(ct->range_bits >= 0) { |
| 560 | if(value < ct->lower_bound |
| 561 | || value > ct->upper_bound) |
| 562 | inext = 1; |
| 563 | } |
| 564 | ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s", |
| 565 | value, st->buf[0], st->size, |
| 566 | ct->lower_bound, ct->upper_bound, |
| 567 | inext ? "ext" : "fix"); |
| 568 | if(ct->flags & APC_EXTENSIBLE) { |
| 569 | if(per_put_few_bits(po, inext, 1)) |
| 570 | _ASN_ENCODE_FAILED; |
| 571 | if(inext) ct = 0; |
| 572 | } else if(inext) { |
| 573 | _ASN_ENCODE_FAILED; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | |
| 578 | /* X.691, #12.2.2 */ |
| 579 | if(ct && ct->range_bits >= 0) { |
| 580 | /* #10.5.6 */ |
| 581 | ASN_DEBUG("Encoding integer with range %d bits", |
| 582 | ct->range_bits); |
| 583 | if(per_put_few_bits(po, value - ct->lower_bound, |
| 584 | ct->range_bits)) |
| 585 | _ASN_ENCODE_FAILED; |
| 586 | _ASN_ENCODED_OK(er); |
| 587 | } |
| 588 | |
| 589 | if(ct && ct->lower_bound) { |
| 590 | ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound); |
| 591 | /* TODO: adjust lower bound */ |
| 592 | _ASN_ENCODE_FAILED; |
| 593 | } |
| 594 | |
| 595 | for(buf = st->buf, end = st->buf + st->size; buf < end;) { |
| 596 | ssize_t mayEncode = uper_put_length(po, end - buf); |
| 597 | if(mayEncode < 0) |
| 598 | _ASN_ENCODE_FAILED; |
| 599 | if(per_put_many_bits(po, buf, 8 * mayEncode)) |
| 600 | _ASN_ENCODE_FAILED; |
| 601 | } |
| 602 | |
| 603 | _ASN_ENCODED_OK(er); |
| 604 | } |
| 605 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 606 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 607 | asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 608 | uint8_t *b, *end; |
| 609 | size_t size; |
| 610 | long l; |
| 611 | |
| 612 | /* Sanity checking */ |
| 613 | if(!iptr || !iptr->buf || !lptr) { |
| 614 | errno = EINVAL; |
| 615 | return -1; |
| 616 | } |
| 617 | |
| 618 | /* Cache the begin/end of the buffer */ |
| 619 | b = iptr->buf; /* Start of the INTEGER buffer */ |
| 620 | size = iptr->size; |
| 621 | end = b + size; /* Where to stop */ |
| 622 | |
| 623 | if(size > sizeof(long)) { |
| 624 | uint8_t *end1 = end - 1; |
| 625 | /* |
| 626 | * Slightly more advanced processing, |
| 627 | * able to >sizeof(long) bytes, |
| 628 | * when the actual value is small |
| 629 | * (0x0000000000abcdef would yield a fine 0x00abcdef) |
| 630 | */ |
| 631 | /* Skip out the insignificant leading bytes */ |
| 632 | for(; b < end1; b++) { |
| 633 | switch(*b) { |
| 634 | case 0x00: if((b[1] & 0x80) == 0) continue; break; |
| 635 | case 0xff: if((b[1] & 0x80) != 0) continue; break; |
| 636 | } |
| 637 | break; |
| 638 | } |
| 639 | |
| 640 | size = end - b; |
| 641 | if(size > sizeof(long)) { |
| 642 | /* Still cannot fit the long */ |
| 643 | errno = ERANGE; |
| 644 | return -1; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | /* Shortcut processing of a corner case */ |
| 649 | if(end == b) { |
| 650 | *lptr = 0; |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | /* Perform the sign initialization */ |
| 655 | /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */ |
| 656 | if((*b >> 7)) l = -1; else l = 0; |
| 657 | |
| 658 | /* Conversion engine */ |
| 659 | for(; b < end; b++) |
| 660 | l = (l << 8) | *b; |
| 661 | |
| 662 | *lptr = l; |
| 663 | return 0; |
| 664 | } |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 665 | |
| 666 | int |
| 667 | asn_long2INTEGER(INTEGER_t *st, long value) { |
| 668 | uint8_t *buf, *bp; |
| 669 | uint8_t *p; |
| 670 | uint8_t *pstart; |
| 671 | uint8_t *pend1; |
vlm | 96a4e46 | 2004-10-26 08:20:46 +0000 | [diff] [blame] | 672 | int littleEndian = 1; /* Run-time detection */ |
| 673 | int add; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 674 | |
| 675 | if(!st) { |
| 676 | errno = EINVAL; |
| 677 | return -1; |
| 678 | } |
| 679 | |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 680 | buf = (uint8_t *)MALLOC(sizeof(value)); |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 681 | if(!buf) return -1; |
| 682 | |
vlm | 96a4e46 | 2004-10-26 08:20:46 +0000 | [diff] [blame] | 683 | if(*(char *)&littleEndian) { |
| 684 | pstart = (uint8_t *)&value + sizeof(value) - 1; |
| 685 | pend1 = (uint8_t *)&value; |
| 686 | add = -1; |
| 687 | } else { |
| 688 | pstart = (uint8_t *)&value; |
| 689 | pend1 = pstart + sizeof(value) - 1; |
| 690 | add = 1; |
| 691 | } |
| 692 | |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 693 | /* |
| 694 | * If the contents octet consists of more than one octet, |
| 695 | * then bits of the first octet and bit 8 of the second octet: |
| 696 | * a) shall not all be ones; and |
| 697 | * b) shall not all be zero. |
| 698 | */ |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 699 | for(p = pstart; p != pend1; p += add) { |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 700 | switch(*p) { |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 701 | case 0x00: if((*(p+add) & 0x80) == 0) |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 702 | continue; |
| 703 | break; |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 704 | case 0xff: if((*(p+add) & 0x80)) |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 705 | continue; |
| 706 | break; |
| 707 | } |
| 708 | break; |
| 709 | } |
| 710 | /* Copy the integer body */ |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 711 | for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add) |
| 712 | *bp++ = *p; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 713 | |
| 714 | if(st->buf) FREEMEM(st->buf); |
| 715 | st->buf = buf; |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 716 | st->size = bp - buf; |
vlm | a4799a6 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 717 | |
| 718 | return 0; |
| 719 | } |