blob: 60afa77f7c430a45a9886e8412983f7164791eb6 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin523de9e2006-08-18 01:34:18 +00002 * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
Lev Walkine0b56e02005-02-25 12:10:27 +00003 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <INTEGER.h>
Lev Walkind703ff42004-10-21 11:21:25 +00008#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <errno.h>
10
11/*
12 * INTEGER basic type description.
13 */
Lev Walkin5e033762004-09-29 13:26:15 +000014static ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
16};
Lev Walkin5e033762004-09-29 13:26:15 +000017asn_TYPE_descriptor_t asn_DEF_INTEGER = {
Lev Walkinf15320b2004-06-03 03:38:44 +000018 "INTEGER",
Lev Walkindc06f6b2004-10-20 15:50:55 +000019 "INTEGER",
Lev Walkin8e8078a2004-09-26 13:10:40 +000020 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000021 INTEGER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000022 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000023 ber_decode_primitive,
Lev Walkinf15320b2004-06-03 03:38:44 +000024 INTEGER_encode_der,
Lev Walkind703ff42004-10-21 11:21:25 +000025 INTEGER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000026 INTEGER_encode_xer,
Lev Walkin59b176e2005-11-26 11:25:14 +000027 INTEGER_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000028 INTEGER_encode_uper, /* Unaligned PER encoder */
Lev Walkinf15320b2004-06-03 03:38:44 +000029 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000030 asn_DEF_INTEGER_tags,
31 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
32 asn_DEF_INTEGER_tags, /* Same as above */
33 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000034 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000035 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000036 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000037};
38
39/*
Lev Walkinf15320b2004-06-03 03:38:44 +000040 * Encode INTEGER type using DER.
41 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000042asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000043INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000044 int tag_mode, ber_tlv_tag_t tag,
45 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000046 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000047
48 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000049 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000050
51 /*
52 * Canonicalize integer in the buffer.
53 * (Remove too long sign extension, remove some first 0x00 bytes)
54 */
55 if(st->buf) {
56 uint8_t *buf = st->buf;
57 uint8_t *end1 = buf + st->size - 1;
58 int shift;
59
60 /* Compute the number of superfluous leading bytes */
61 for(; buf < end1; buf++) {
62 /*
63 * If the contents octets of an integer value encoding
64 * consist of more than one octet, then the bits of the
65 * first octet and bit 8 of the second octet:
66 * a) shall not all be ones; and
67 * b) shall not all be zero.
68 */
69 switch(*buf) {
70 case 0x00: if((buf[1] & 0x80) == 0)
71 continue;
72 break;
73 case 0xff: if((buf[1] & 0x80))
74 continue;
75 break;
76 }
77 break;
78 }
79
80 /* Remove leading superfluous bytes from the integer */
81 shift = buf - st->buf;
82 if(shift) {
83 uint8_t *nb = st->buf;
84 uint8_t *end;
85
86 st->size -= shift; /* New size, minus bad bytes */
87 end = nb + st->size;
88
89 for(; nb < end; nb++, buf++)
90 *nb = *buf;
91 }
92
93 } /* if(1) */
94
Lev Walkin8e8078a2004-09-26 13:10:40 +000095 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +000096}
97
Lev Walkinc2350112005-03-29 17:19:53 +000098static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop);
Lev Walkine0b56e02005-02-25 12:10:27 +000099
Lev Walkinf15320b2004-06-03 03:38:44 +0000100/*
101 * INTEGER specific human-readable output.
102 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000103static ssize_t
Lev Walkine0b56e02005-02-25 12:10:27 +0000104INTEGER__dump(asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
105 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000106 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 uint8_t *buf = st->buf;
108 uint8_t *buf_end = st->buf + st->size;
109 signed long accum;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000110 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000111 char *p;
112 int ret;
113
Lev Walkindb13f512004-07-19 17:30:25 +0000114 /*
115 * Advance buf pointer until the start of the value's body.
116 * This will make us able to process large integers using simple case,
117 * when the actual value is small
118 * (0x0000000000abcdef would yield a fine 0x00abcdef)
119 */
120 /* Skip the insignificant leading bytes */
121 for(; buf < buf_end-1; buf++) {
122 switch(*buf) {
123 case 0x00: if((buf[1] & 0x80) == 0) continue; break;
124 case 0xff: if((buf[1] & 0x80) != 0) continue; break;
125 }
126 break;
127 }
128
Lev Walkinf15320b2004-06-03 03:38:44 +0000129 /* Simple case: the integer size is small */
Lev Walkindb13f512004-07-19 17:30:25 +0000130 if((size_t)(buf_end - buf) <= sizeof(accum)) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000131 const asn_INTEGER_enum_map_t *el;
132 size_t scrsize;
133 char *scr;
134
135 if(buf == buf_end) {
136 accum = 0;
137 } else {
138 accum = (*buf & 0x80) ? -1 : 0;
139 for(; buf < buf_end; buf++)
140 accum = (accum << 8) | *buf;
141 }
142
Lev Walkinc2350112005-03-29 17:19:53 +0000143 el = INTEGER_map_value2enum(specs, accum);
Lev Walkine0b56e02005-02-25 12:10:27 +0000144 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;
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 }
175
176 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000177 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000178 for(p = scratch; buf < buf_end; buf++) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000179 static const char *h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000180 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000182 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000184 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 p = scratch;
186 }
187 *p++ = h2c[*buf >> 4];
188 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000189 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 }
Lev Walkindb13f512004-07-19 17:30:25 +0000191 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000192 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000193
Lev Walkina9cc46e2004-09-22 16:06:28 +0000194 wrote += p - scratch;
195 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
196}
197
198/*
199 * INTEGER specific human-readable output.
200 */
201int
Lev Walkin5e033762004-09-29 13:26:15 +0000202INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000203 asn_app_consume_bytes_f *cb, void *app_key) {
204 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000205 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000206
207 (void)td;
208 (void)ilevel;
209
Lev Walkind500a962005-11-27 13:06:56 +0000210 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000211 ret = cb("<absent>", 8, app_key);
212 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000213 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000214
Lev Walkin8e8078a2004-09-26 13:10:40 +0000215 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000216}
217
Lev Walkine0b56e02005-02-25 12:10:27 +0000218struct e2v_key {
219 const char *start;
220 const char *stop;
221 asn_INTEGER_enum_map_t *vemap;
222 unsigned int *evmap;
223};
224static int
225INTEGER__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
243static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000244INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Lev Walkin9332b652005-03-04 11:18:44 +0000245 asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000246 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;
Lev Walkin9332b652005-03-04 11:18:44 +0000271 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;
Lev Walkine0b56e02005-02-25 12:10:27 +0000279}
280
281static int
282INTEGER__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
Lev Walkinc2350112005-03-29 17:19:53 +0000291const asn_INTEGER_enum_map_t *
292INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000293 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
Lev Walkinc744a022006-09-15 18:33:25 +0000300static int
301INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
302 void *p = MALLOC(min_size + 1);
303 if(p) {
304 void *b = st->buf;
305 st->size = 0;
306 st->buf = p;
307 FREEMEM(b);
308 return 0;
309 } else {
310 return -1;
311 }
312}
313
Lev Walkind703ff42004-10-21 11:21:25 +0000314/*
315 * Decode the chunk of XML text encoding INTEGER.
316 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000317static enum xer_pbd_rval
318INTEGER__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000319 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkind703ff42004-10-21 11:21:25 +0000320 long sign = 1;
321 long value;
Lev Walkine0b56e02005-02-25 12:10:27 +0000322 const char *lp;
323 const char *lstart = (const char *)chunk_buf;
324 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000325 enum {
326 ST_SKIPSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000327 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000328 ST_WAITDIGITS,
329 ST_DIGITS,
Lev Walkinc744a022006-09-15 18:33:25 +0000330 ST_HEXDIGIT1,
331 ST_HEXDIGIT2,
332 ST_HEXCOLON,
Lev Walkinfdb25922005-07-21 09:32:49 +0000333 ST_EXTRASTUFF
Lev Walkind703ff42004-10-21 11:21:25 +0000334 } state = ST_SKIPSPACE;
Lev Walkin0be3a992004-10-21 12:11:57 +0000335
Lev Walkinc744a022006-09-15 18:33:25 +0000336 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000337 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
338 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000339
Lev Walkind703ff42004-10-21 11:21:25 +0000340 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000341 * We may have received a tag here. It will be processed inline.
342 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000343 */
Lev Walkin0be3a992004-10-21 12:11:57 +0000344 for(value = 0, lp = lstart; lp < lstop; lp++) {
345 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000346 switch(lv) {
347 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000348 switch(state) {
349 case ST_SKIPSPACE:
350 case ST_SKIPSPHEX:
351 continue;
352 case ST_HEXCOLON:
353 if(xer_is_whitespace(lp, lstop - lp)) {
354 lp = lstop - 1;
355 continue;
356 }
357 break;
358 default:
359 break;
360 }
Lev Walkind703ff42004-10-21 11:21:25 +0000361 break;
362 case 0x2d: /* '-' */
363 if(state == ST_SKIPSPACE) {
364 sign = -1;
365 state = ST_WAITDIGITS;
366 continue;
367 }
368 break;
369 case 0x2b: /* '+' */
370 if(state == ST_SKIPSPACE) {
371 state = ST_WAITDIGITS;
372 continue;
373 }
374 break;
375 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
376 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000377 switch(state) {
378 case ST_DIGITS: break;
379 case ST_SKIPSPHEX: /* Fall through */
380 case ST_HEXDIGIT1:
381 value = (lv - 0x30) << 4;
382 state = ST_HEXDIGIT2;
383 continue;
384 case ST_HEXDIGIT2:
385 value += (lv - 0x30);
386 state = ST_HEXCOLON;
387 st->buf[st->size++] = value;
388 continue;
389 case ST_HEXCOLON:
390 return XPBD_BROKEN_ENCODING;
391 default:
392 state = ST_DIGITS;
393 break;
394 }
Lev Walkind703ff42004-10-21 11:21:25 +0000395
Lev Walkin0be3a992004-10-21 12:11:57 +0000396 {
397 long new_value = value * 10;
398
399 if(new_value / 10 != value)
400 /* Overflow */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000401 return XPBD_DECODER_LIMIT;
Lev Walkin0be3a992004-10-21 12:11:57 +0000402
403 value = new_value + (lv - 0x30);
Lev Walkind703ff42004-10-21 11:21:25 +0000404 /* Check for two's complement overflow */
405 if(value < 0) {
406 /* Check whether it is a LONG_MIN */
407 if(sign == -1
Lev Walkin8471cec2004-10-21 14:02:19 +0000408 && (unsigned long)value
409 == ~((unsigned long)-1 >> 1)) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000410 sign = 1;
Lev Walkind703ff42004-10-21 11:21:25 +0000411 } else {
412 /* Overflow */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000413 return XPBD_DECODER_LIMIT;
Lev Walkind703ff42004-10-21 11:21:25 +0000414 }
415 }
Lev Walkin0be3a992004-10-21 12:11:57 +0000416 }
Lev Walkind703ff42004-10-21 11:21:25 +0000417 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000418 case 0x3c: /* '<' */
419 if(state == ST_SKIPSPACE) {
420 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000421 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000422 (asn_INTEGER_specifics_t *)
423 td->specifics, lstart, lstop);
424 if(el) {
425 ASN_DEBUG("Found \"%s\" => %ld",
426 el->enum_name, el->nat_value);
427 state = ST_DIGITS;
428 value = el->nat_value;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000429 lp = lstop - 1;
430 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000431 }
432 ASN_DEBUG("Unknown identifier for INTEGER");
433 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000434 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000435 case 0x3a: /* ':' */
436 if(state == ST_HEXCOLON) {
437 /* This colon is expected */
438 state = ST_HEXDIGIT1;
439 continue;
440 } else if(state == ST_DIGITS) {
441 /* The colon here means that we have
442 * decoded the first two hexadecimal
443 * places as a decimal value.
444 * Switch decoding mode. */
445 ASN_DEBUG("INTEGER re-evaluate as hex form");
446 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
447 return XPBD_SYSTEM_FAILURE;
448 state = ST_SKIPSPHEX;
449 lp = lstart - 1;
450 continue;
451 } else {
452 ASN_DEBUG("state %d at %d", state, lp - lstart);
453 break;
454 }
455 /* [A-Fa-f] */
456 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
457 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
458 switch(state) {
459 case ST_SKIPSPHEX:
460 case ST_SKIPSPACE: /* Fall through */
461 case ST_HEXDIGIT1:
462 value = lv - ((lv < 0x61) ? 0x41 : 0x61);
463 value += 10;
464 value <<= 4;
465 state = ST_HEXDIGIT2;
466 continue;
467 case ST_HEXDIGIT2:
468 value += lv - ((lv < 0x61) ? 0x41 : 0x61);
469 value += 10;
470 st->buf[st->size++] = value;
471 state = ST_HEXCOLON;
472 continue;
473 case ST_DIGITS:
474 ASN_DEBUG("INTEGER re-evaluate as hex form");
475 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
476 return XPBD_SYSTEM_FAILURE;
477 state = ST_SKIPSPHEX;
478 lp = lstart - 1;
479 continue;
480 default:
481 break;
482 }
483 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000484 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000485
486 /* Found extra non-numeric stuff */
Lev Walkinc744a022006-09-15 18:33:25 +0000487 ASN_DEBUG("Found non-numeric 0x%2x at %d",
488 lv, lp - lstart);
Lev Walkin806b0bb2005-03-09 22:31:22 +0000489 state = ST_EXTRASTUFF;
Lev Walkin0be3a992004-10-21 12:11:57 +0000490 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000491 }
492
Lev Walkinc744a022006-09-15 18:33:25 +0000493 switch(state) {
494 case ST_DIGITS:
495 /* Everything is cool */
496 break;
497 case ST_HEXCOLON:
498 st->buf[st->size] = 0; /* Just in case termination */
499 return XPBD_BODY_CONSUMED;
500 case ST_HEXDIGIT1:
501 case ST_HEXDIGIT2:
502 case ST_SKIPSPHEX:
503 return XPBD_BROKEN_ENCODING;
504 default:
Lev Walkin806b0bb2005-03-09 22:31:22 +0000505 if(xer_is_whitespace(lp, lstop - lp)) {
506 if(state != ST_EXTRASTUFF)
507 return XPBD_NOT_BODY_IGNORE;
Lev Walkinc744a022006-09-15 18:33:25 +0000508 break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000509 } else {
Lev Walkinc744a022006-09-15 18:33:25 +0000510 ASN_DEBUG("INTEGER: No useful digits (state %d)",
511 state);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000512 return XPBD_BROKEN_ENCODING; /* No digits */
513 }
Lev Walkinc744a022006-09-15 18:33:25 +0000514 break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000515 }
Lev Walkind703ff42004-10-21 11:21:25 +0000516
517 value *= sign; /* Change sign, if needed */
518
519 if(asn_long2INTEGER(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000520 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000521
Lev Walkin0fab1a62005-03-09 22:19:25 +0000522 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000523}
524
525asn_dec_rval_t
526INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
527 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000528 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000529
530 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000531 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000532 buf_ptr, size, INTEGER__xer_body_decode);
533}
534
Lev Walkina9cc46e2004-09-22 16:06:28 +0000535asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000536INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000537 int ilevel, enum xer_encoder_flags_e flags,
538 asn_app_consume_bytes_f *cb, void *app_key) {
539 const INTEGER_t *st = (const INTEGER_t *)sptr;
540 asn_enc_rval_t er;
541
542 (void)ilevel;
543 (void)flags;
544
Lev Walkind500a962005-11-27 13:06:56 +0000545 if(!st || !st->buf)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000546 _ASN_ENCODE_FAILED;
547
Lev Walkine0b56e02005-02-25 12:10:27 +0000548 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000549 if(er.encoded < 0) _ASN_ENCODE_FAILED;
550
Lev Walkin59b176e2005-11-26 11:25:14 +0000551 _ASN_ENCODED_OK(er);
552}
553
554asn_dec_rval_t
555INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
556 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
557 asn_dec_rval_t rval = { RC_OK, 0 };
558 INTEGER_t *st = (INTEGER_t *)*sptr;
559 asn_per_constraint_t *ct;
560 int repeat;
561
562 (void)opt_codec_ctx;
563
564 if(!st) {
565 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
566 if(!st) _ASN_DECODE_FAILED;
567 }
568
569 if(!constraints) constraints = td->per_constraints;
570 ct = constraints ? &constraints->value : 0;
571
572 if(ct && ct->flags & APC_EXTENSIBLE) {
573 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000574 if(inext < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000575 if(inext) ct = 0;
576 }
577
578 FREEMEM(st->buf);
579 if(ct) {
580 if(ct->flags & APC_SEMI_CONSTRAINED) {
581 st->buf = (uint8_t *)CALLOC(1, 2);
582 if(!st->buf) _ASN_DECODE_FAILED;
583 st->size = 1;
584 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
585 size_t size = (ct->range_bits + 7) >> 3;
586 st->buf = (uint8_t *)MALLOC(1 + size + 1);
587 if(!st->buf) _ASN_DECODE_FAILED;
588 st->size = size;
589 } else {
590 st->size = 0;
591 }
592 } else {
593 st->size = 0;
594 }
595
596 /* X.691, #12.2.2 */
597 if(ct && ct->flags != APC_UNCONSTRAINED) {
598 /* #10.5.6 */
599 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
600 if(ct->range_bits >= 0) {
Lev Walkina105cbc2007-11-06 02:35:13 +0000601 long value;
602 if(ct->range_bits == 32) {
603 long lhalf;
604 value = per_get_few_bits(pd, 16);
605 if(value < 0) _ASN_DECODE_STARVED;
606 lhalf = per_get_few_bits(pd, 16);
607 if(lhalf < 0) _ASN_DECODE_STARVED;
608 value = (value << 16) | lhalf;
609 } else {
610 value = per_get_few_bits(pd, ct->range_bits);
611 if(value < 0) _ASN_DECODE_STARVED;
612 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000613 ASN_DEBUG("Got value %ld + low %ld",
614 value, ct->lower_bound);
615 value += ct->lower_bound;
616 if(asn_long2INTEGER(st, value))
617 _ASN_DECODE_FAILED;
618 return rval;
619 }
620 } else {
621 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
622 }
623
624 /* X.691, #12.2.3, #12.2.4 */
625 do {
626 ssize_t len;
627 void *p;
628 int ret;
629
630 /* Get the PER length */
631 len = uper_get_length(pd, -1, &repeat);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000632 if(len < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000633
634 p = REALLOC(st->buf, st->size + len + 1);
635 if(!p) _ASN_DECODE_FAILED;
636 st->buf = (uint8_t *)p;
637
638 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000639 if(ret < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000640 st->size += len;
641 } while(repeat);
642 st->buf[st->size] = 0; /* JIC */
643
644 /* #12.2.3 */
645 if(ct && ct->lower_bound) {
646 /*
647 * TODO: replace by in-place arithmetics.
648 */
649 long value;
650 if(asn_INTEGER2long(st, &value))
651 _ASN_DECODE_FAILED;
652 if(asn_long2INTEGER(st, value + ct->lower_bound))
653 _ASN_DECODE_FAILED;
654 }
655
656 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000657}
658
Lev Walkin523de9e2006-08-18 01:34:18 +0000659asn_enc_rval_t
660INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
661 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
662 asn_enc_rval_t er;
663 INTEGER_t *st = (INTEGER_t *)sptr;
664 const uint8_t *buf;
665 const uint8_t *end;
666 asn_per_constraint_t *ct;
667 long value = 0;
668
669 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
670
671 if(!constraints) constraints = td->per_constraints;
672 ct = constraints ? &constraints->value : 0;
673
674 er.encoded = 0;
675
676 if(ct) {
677 int inext = 0;
678 if(asn_INTEGER2long(st, &value))
679 _ASN_ENCODE_FAILED;
680 /* Check proper range */
681 if(ct->flags & APC_SEMI_CONSTRAINED) {
682 if(value < ct->lower_bound)
683 inext = 1;
684 } else if(ct->range_bits >= 0) {
685 if(value < ct->lower_bound
686 || value > ct->upper_bound)
687 inext = 1;
688 }
689 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
690 value, st->buf[0], st->size,
691 ct->lower_bound, ct->upper_bound,
692 inext ? "ext" : "fix");
693 if(ct->flags & APC_EXTENSIBLE) {
694 if(per_put_few_bits(po, inext, 1))
695 _ASN_ENCODE_FAILED;
696 if(inext) ct = 0;
697 } else if(inext) {
698 _ASN_ENCODE_FAILED;
699 }
700 }
701
702
703 /* X.691, #12.2.2 */
704 if(ct && ct->range_bits >= 0) {
705 /* #10.5.6 */
706 ASN_DEBUG("Encoding integer with range %d bits",
707 ct->range_bits);
Lev Walkina105cbc2007-11-06 02:35:13 +0000708 if(ct->range_bits == 32) {
709 /* TODO: extend to >32 bits */
710 long v = value - ct->lower_bound;
711 if(per_put_few_bits(po, v >> 1, 31)
712 || per_put_few_bits(po, v, 1))
713 _ASN_ENCODE_FAILED;
714 } else {
715 if(per_put_few_bits(po, value - ct->lower_bound,
Lev Walkin523de9e2006-08-18 01:34:18 +0000716 ct->range_bits))
Lev Walkina105cbc2007-11-06 02:35:13 +0000717 _ASN_ENCODE_FAILED;
718 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000719 _ASN_ENCODED_OK(er);
720 }
721
722 if(ct && ct->lower_bound) {
723 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
724 /* TODO: adjust lower bound */
725 _ASN_ENCODE_FAILED;
726 }
727
728 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
729 ssize_t mayEncode = uper_put_length(po, end - buf);
730 if(mayEncode < 0)
731 _ASN_ENCODE_FAILED;
732 if(per_put_many_bits(po, buf, 8 * mayEncode))
733 _ASN_ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000734 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000735 }
736
737 _ASN_ENCODED_OK(er);
738}
739
Lev Walkinf15320b2004-06-03 03:38:44 +0000740int
Lev Walkin5e033762004-09-29 13:26:15 +0000741asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000742 uint8_t *b, *end;
743 size_t size;
744 long l;
745
746 /* Sanity checking */
747 if(!iptr || !iptr->buf || !lptr) {
748 errno = EINVAL;
749 return -1;
750 }
751
752 /* Cache the begin/end of the buffer */
753 b = iptr->buf; /* Start of the INTEGER buffer */
754 size = iptr->size;
755 end = b + size; /* Where to stop */
756
757 if(size > sizeof(long)) {
758 uint8_t *end1 = end - 1;
759 /*
760 * Slightly more advanced processing,
761 * able to >sizeof(long) bytes,
762 * when the actual value is small
763 * (0x0000000000abcdef would yield a fine 0x00abcdef)
764 */
765 /* Skip out the insignificant leading bytes */
766 for(; b < end1; b++) {
767 switch(*b) {
768 case 0x00: if((b[1] & 0x80) == 0) continue; break;
769 case 0xff: if((b[1] & 0x80) != 0) continue; break;
770 }
771 break;
772 }
773
774 size = end - b;
775 if(size > sizeof(long)) {
776 /* Still cannot fit the long */
777 errno = ERANGE;
778 return -1;
779 }
780 }
781
782 /* Shortcut processing of a corner case */
783 if(end == b) {
784 *lptr = 0;
785 return 0;
786 }
787
788 /* Perform the sign initialization */
789 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
790 if((*b >> 7)) l = -1; else l = 0;
791
792 /* Conversion engine */
793 for(; b < end; b++)
794 l = (l << 8) | *b;
795
796 *lptr = l;
797 return 0;
798}
Lev Walkind703ff42004-10-21 11:21:25 +0000799
800int
801asn_long2INTEGER(INTEGER_t *st, long value) {
802 uint8_t *buf, *bp;
803 uint8_t *p;
804 uint8_t *pstart;
805 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000806 int littleEndian = 1; /* Run-time detection */
807 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000808
809 if(!st) {
810 errno = EINVAL;
811 return -1;
812 }
813
Lev Walkin8471cec2004-10-21 14:02:19 +0000814 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000815 if(!buf) return -1;
816
Lev Walkind7ad5612004-10-26 08:20:46 +0000817 if(*(char *)&littleEndian) {
818 pstart = (uint8_t *)&value + sizeof(value) - 1;
819 pend1 = (uint8_t *)&value;
820 add = -1;
821 } else {
822 pstart = (uint8_t *)&value;
823 pend1 = pstart + sizeof(value) - 1;
824 add = 1;
825 }
826
Lev Walkind703ff42004-10-21 11:21:25 +0000827 /*
828 * If the contents octet consists of more than one octet,
829 * then bits of the first octet and bit 8 of the second octet:
830 * a) shall not all be ones; and
831 * b) shall not all be zero.
832 */
Lev Walkin33700162004-10-26 09:03:31 +0000833 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000834 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000835 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000836 continue;
837 break;
Lev Walkin33700162004-10-26 09:03:31 +0000838 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000839 continue;
840 break;
841 }
842 break;
843 }
844 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000845 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
846 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000847
848 if(st->buf) FREEMEM(st->buf);
849 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000850 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000851
852 return 0;
853}