blob: c6e1d9107afa6a02e24dded0f843d39c4baea230 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin5c879db2007-11-06 06:23:31 +00002 * Copyright (c) 2003, 2004, 2005, 2006, 2007 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;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000161 ret = snprintf(scr, scrsize,
162 (specs && specs->field_unsigned)
163 ?"%lu":"%ld", accum);
Lev Walkine0b56e02005-02-25 12:10:27 +0000164 }
165 assert(ret > 0 && (size_t)ret < scrsize);
166 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
167 } else if(plainOrXER && specs && specs->strict_enumeration) {
168 /*
169 * Here and earlier, we cannot encode the ENUMERATED values
170 * if there is no corresponding identifier.
171 */
172 ASN_DEBUG("ASN.1 forbids dealing with "
173 "unknown value of ENUMERATED type");
174 errno = EPERM;
175 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 }
177
178 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000179 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 for(p = scratch; buf < buf_end; buf++) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000181 static const char *h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000182 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000184 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000186 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 p = scratch;
188 }
189 *p++ = h2c[*buf >> 4];
190 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000191 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 }
Lev Walkindb13f512004-07-19 17:30:25 +0000193 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000194 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000195
Lev Walkina9cc46e2004-09-22 16:06:28 +0000196 wrote += p - scratch;
197 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
198}
199
200/*
201 * INTEGER specific human-readable output.
202 */
203int
Lev Walkin5e033762004-09-29 13:26:15 +0000204INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000205 asn_app_consume_bytes_f *cb, void *app_key) {
206 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000207 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208
209 (void)td;
210 (void)ilevel;
211
Lev Walkind500a962005-11-27 13:06:56 +0000212 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000213 ret = cb("<absent>", 8, app_key);
214 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000215 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000216
Lev Walkin8e8078a2004-09-26 13:10:40 +0000217 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000218}
219
Lev Walkine0b56e02005-02-25 12:10:27 +0000220struct e2v_key {
221 const char *start;
222 const char *stop;
223 asn_INTEGER_enum_map_t *vemap;
224 unsigned int *evmap;
225};
226static int
227INTEGER__compar_enum2value(const void *kp, const void *am) {
228 const struct e2v_key *key = (const struct e2v_key *)kp;
229 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
230 const char *ptr, *end, *name;
231
232 /* Remap the element (sort by different criterion) */
233 el = key->vemap + key->evmap[el - key->vemap];
234
235 /* Compare strings */
236 for(ptr = key->start, end = key->stop, name = el->enum_name;
237 ptr < end; ptr++, name++) {
238 if(*ptr != *name)
239 return *(const unsigned char *)ptr
240 - *(const unsigned char *)name;
241 }
242 return name[0] ? -1 : 0;
243}
244
245static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000246INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Lev Walkin9332b652005-03-04 11:18:44 +0000247 asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000248 int count = specs ? specs->map_count : 0;
249 struct e2v_key key;
250 const char *lp;
251
252 if(!count) return NULL;
253
254 /* Guaranteed: assert(lstart < lstop); */
255 /* Figure out the tag name */
256 for(lstart++, lp = lstart; lp < lstop; lp++) {
257 switch(*lp) {
258 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
259 case 0x2f: /* '/' */ case 0x3e: /* '>' */
260 break;
261 default:
262 continue;
263 }
264 break;
265 }
266 if(lp == lstop) return NULL; /* No tag found */
267 lstop = lp;
268
269 key.start = lstart;
270 key.stop = lstop;
271 key.vemap = specs->value2enum;
272 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000273 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
274 specs->value2enum, count, sizeof(specs->value2enum[0]),
275 INTEGER__compar_enum2value);
276 if(el_found) {
277 /* Remap enum2value into value2enum */
278 el_found = key.vemap + key.evmap[el_found - key.vemap];
279 }
280 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000281}
282
283static int
284INTEGER__compar_value2enum(const void *kp, const void *am) {
285 long a = *(const long *)kp;
286 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
287 long b = el->nat_value;
288 if(a < b) return -1;
289 else if(a == b) return 0;
290 else return 1;
291}
292
Lev Walkinc2350112005-03-29 17:19:53 +0000293const asn_INTEGER_enum_map_t *
294INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000295 int count = specs ? specs->map_count : 0;
296 if(!count) return 0;
297 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
298 count, sizeof(specs->value2enum[0]),
299 INTEGER__compar_value2enum);
300}
301
Lev Walkinc744a022006-09-15 18:33:25 +0000302static int
303INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
304 void *p = MALLOC(min_size + 1);
305 if(p) {
306 void *b = st->buf;
307 st->size = 0;
308 st->buf = p;
309 FREEMEM(b);
310 return 0;
311 } else {
312 return -1;
313 }
314}
315
Lev Walkind703ff42004-10-21 11:21:25 +0000316/*
317 * Decode the chunk of XML text encoding INTEGER.
318 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000319static enum xer_pbd_rval
320INTEGER__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 +0000321 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkind703ff42004-10-21 11:21:25 +0000322 long sign = 1;
323 long value;
Lev Walkine0b56e02005-02-25 12:10:27 +0000324 const char *lp;
325 const char *lstart = (const char *)chunk_buf;
326 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000327 enum {
328 ST_SKIPSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000329 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000330 ST_WAITDIGITS,
331 ST_DIGITS,
Lev Walkinc744a022006-09-15 18:33:25 +0000332 ST_HEXDIGIT1,
333 ST_HEXDIGIT2,
334 ST_HEXCOLON,
Lev Walkinfdb25922005-07-21 09:32:49 +0000335 ST_EXTRASTUFF
Lev Walkind703ff42004-10-21 11:21:25 +0000336 } state = ST_SKIPSPACE;
Lev Walkin0be3a992004-10-21 12:11:57 +0000337
Lev Walkinc744a022006-09-15 18:33:25 +0000338 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000339 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
340 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000341
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000342 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
343 return XPBD_SYSTEM_FAILURE;
344
Lev Walkind703ff42004-10-21 11:21:25 +0000345 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000346 * We may have received a tag here. It will be processed inline.
347 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000348 */
Lev Walkin0be3a992004-10-21 12:11:57 +0000349 for(value = 0, lp = lstart; lp < lstop; lp++) {
350 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000351 switch(lv) {
352 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000353 switch(state) {
354 case ST_SKIPSPACE:
355 case ST_SKIPSPHEX:
356 continue;
357 case ST_HEXCOLON:
358 if(xer_is_whitespace(lp, lstop - lp)) {
359 lp = lstop - 1;
360 continue;
361 }
362 break;
363 default:
364 break;
365 }
Lev Walkind703ff42004-10-21 11:21:25 +0000366 break;
367 case 0x2d: /* '-' */
368 if(state == ST_SKIPSPACE) {
369 sign = -1;
370 state = ST_WAITDIGITS;
371 continue;
372 }
373 break;
374 case 0x2b: /* '+' */
375 if(state == ST_SKIPSPACE) {
376 state = ST_WAITDIGITS;
377 continue;
378 }
379 break;
380 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
381 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000382 switch(state) {
383 case ST_DIGITS: break;
384 case ST_SKIPSPHEX: /* Fall through */
385 case ST_HEXDIGIT1:
386 value = (lv - 0x30) << 4;
387 state = ST_HEXDIGIT2;
388 continue;
389 case ST_HEXDIGIT2:
390 value += (lv - 0x30);
391 state = ST_HEXCOLON;
Lev Walkinf3c089e2007-11-13 23:53:13 +0000392 st->buf[st->size++] = (uint8_t)value;
Lev Walkinc744a022006-09-15 18:33:25 +0000393 continue;
394 case ST_HEXCOLON:
395 return XPBD_BROKEN_ENCODING;
396 default:
397 state = ST_DIGITS;
398 break;
399 }
Lev Walkind703ff42004-10-21 11:21:25 +0000400
Lev Walkin0be3a992004-10-21 12:11:57 +0000401 {
Lev Walkin9a338f82012-01-22 16:38:37 -0800402 long volatile new_value = value * 10;
403 /* GCC 4.x optimizes (new_value) without `volatile'
404 * so the following check does not detect overflow. */
Lev Walkin0be3a992004-10-21 12:11:57 +0000405
406 if(new_value / 10 != value)
407 /* Overflow */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000408 return XPBD_DECODER_LIMIT;
Lev Walkin0be3a992004-10-21 12:11:57 +0000409
410 value = new_value + (lv - 0x30);
Lev Walkind703ff42004-10-21 11:21:25 +0000411 /* Check for two's complement overflow */
412 if(value < 0) {
413 /* Check whether it is a LONG_MIN */
414 if(sign == -1
Lev Walkin8471cec2004-10-21 14:02:19 +0000415 && (unsigned long)value
416 == ~((unsigned long)-1 >> 1)) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000417 sign = 1;
Lev Walkind703ff42004-10-21 11:21:25 +0000418 } else {
419 /* Overflow */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000420 return XPBD_DECODER_LIMIT;
Lev Walkind703ff42004-10-21 11:21:25 +0000421 }
422 }
Lev Walkin0be3a992004-10-21 12:11:57 +0000423 }
Lev Walkind703ff42004-10-21 11:21:25 +0000424 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000425 case 0x3c: /* '<' */
426 if(state == ST_SKIPSPACE) {
427 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000428 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000429 (asn_INTEGER_specifics_t *)
430 td->specifics, lstart, lstop);
431 if(el) {
432 ASN_DEBUG("Found \"%s\" => %ld",
433 el->enum_name, el->nat_value);
434 state = ST_DIGITS;
435 value = el->nat_value;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000436 lp = lstop - 1;
437 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000438 }
439 ASN_DEBUG("Unknown identifier for INTEGER");
440 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000441 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000442 case 0x3a: /* ':' */
443 if(state == ST_HEXCOLON) {
444 /* This colon is expected */
445 state = ST_HEXDIGIT1;
446 continue;
447 } else if(state == ST_DIGITS) {
448 /* The colon here means that we have
449 * decoded the first two hexadecimal
450 * places as a decimal value.
451 * Switch decoding mode. */
452 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000453 state = ST_SKIPSPHEX;
454 lp = lstart - 1;
455 continue;
456 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400457 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000458 break;
459 }
460 /* [A-Fa-f] */
461 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
462 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
463 switch(state) {
464 case ST_SKIPSPHEX:
465 case ST_SKIPSPACE: /* Fall through */
466 case ST_HEXDIGIT1:
467 value = lv - ((lv < 0x61) ? 0x41 : 0x61);
468 value += 10;
469 value <<= 4;
470 state = ST_HEXDIGIT2;
471 continue;
472 case ST_HEXDIGIT2:
473 value += lv - ((lv < 0x61) ? 0x41 : 0x61);
474 value += 10;
Lev Walkinf3c089e2007-11-13 23:53:13 +0000475 st->buf[st->size++] = (uint8_t)value;
Lev Walkinc744a022006-09-15 18:33:25 +0000476 state = ST_HEXCOLON;
477 continue;
478 case ST_DIGITS:
479 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000480 state = ST_SKIPSPHEX;
481 lp = lstart - 1;
482 continue;
483 default:
484 break;
485 }
486 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000487 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000488
489 /* Found extra non-numeric stuff */
Lev Walkin2e763992011-07-21 01:17:37 +0400490 ASN_DEBUG("Found non-numeric 0x%2x at %ld",
491 lv, (long)(lp - lstart));
Lev Walkin806b0bb2005-03-09 22:31:22 +0000492 state = ST_EXTRASTUFF;
Lev Walkin0be3a992004-10-21 12:11:57 +0000493 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000494 }
495
Lev Walkinc744a022006-09-15 18:33:25 +0000496 switch(state) {
497 case ST_DIGITS:
498 /* Everything is cool */
499 break;
500 case ST_HEXCOLON:
501 st->buf[st->size] = 0; /* Just in case termination */
502 return XPBD_BODY_CONSUMED;
503 case ST_HEXDIGIT1:
504 case ST_HEXDIGIT2:
505 case ST_SKIPSPHEX:
506 return XPBD_BROKEN_ENCODING;
507 default:
Lev Walkin806b0bb2005-03-09 22:31:22 +0000508 if(xer_is_whitespace(lp, lstop - lp)) {
509 if(state != ST_EXTRASTUFF)
510 return XPBD_NOT_BODY_IGNORE;
Lev Walkinc744a022006-09-15 18:33:25 +0000511 break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000512 } else {
Lev Walkinc744a022006-09-15 18:33:25 +0000513 ASN_DEBUG("INTEGER: No useful digits (state %d)",
514 state);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000515 return XPBD_BROKEN_ENCODING; /* No digits */
516 }
Lev Walkinc744a022006-09-15 18:33:25 +0000517 break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000518 }
Lev Walkind703ff42004-10-21 11:21:25 +0000519
520 value *= sign; /* Change sign, if needed */
521
522 if(asn_long2INTEGER(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000523 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000524
Lev Walkin0fab1a62005-03-09 22:19:25 +0000525 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000526}
527
528asn_dec_rval_t
529INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
530 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000531 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000532
533 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000534 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000535 buf_ptr, size, INTEGER__xer_body_decode);
536}
537
Lev Walkina9cc46e2004-09-22 16:06:28 +0000538asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000539INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000540 int ilevel, enum xer_encoder_flags_e flags,
541 asn_app_consume_bytes_f *cb, void *app_key) {
542 const INTEGER_t *st = (const INTEGER_t *)sptr;
543 asn_enc_rval_t er;
544
545 (void)ilevel;
546 (void)flags;
547
Lev Walkind500a962005-11-27 13:06:56 +0000548 if(!st || !st->buf)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000549 _ASN_ENCODE_FAILED;
550
Lev Walkine0b56e02005-02-25 12:10:27 +0000551 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000552 if(er.encoded < 0) _ASN_ENCODE_FAILED;
553
Lev Walkin59b176e2005-11-26 11:25:14 +0000554 _ASN_ENCODED_OK(er);
555}
556
557asn_dec_rval_t
558INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
559 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000560 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000561 asn_dec_rval_t rval = { RC_OK, 0 };
562 INTEGER_t *st = (INTEGER_t *)*sptr;
563 asn_per_constraint_t *ct;
564 int repeat;
565
566 (void)opt_codec_ctx;
567
568 if(!st) {
569 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
570 if(!st) _ASN_DECODE_FAILED;
571 }
572
573 if(!constraints) constraints = td->per_constraints;
574 ct = constraints ? &constraints->value : 0;
575
576 if(ct && ct->flags & APC_EXTENSIBLE) {
577 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000578 if(inext < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000579 if(inext) ct = 0;
580 }
581
582 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000583 st->buf = 0;
584 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000585 if(ct) {
586 if(ct->flags & APC_SEMI_CONSTRAINED) {
587 st->buf = (uint8_t *)CALLOC(1, 2);
588 if(!st->buf) _ASN_DECODE_FAILED;
589 st->size = 1;
590 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
591 size_t size = (ct->range_bits + 7) >> 3;
592 st->buf = (uint8_t *)MALLOC(1 + size + 1);
593 if(!st->buf) _ASN_DECODE_FAILED;
594 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000595 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000596 }
597
598 /* X.691, #12.2.2 */
599 if(ct && ct->flags != APC_UNCONSTRAINED) {
600 /* #10.5.6 */
601 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
602 if(ct->range_bits >= 0) {
Lev Walkina105cbc2007-11-06 02:35:13 +0000603 long value;
604 if(ct->range_bits == 32) {
605 long lhalf;
606 value = per_get_few_bits(pd, 16);
607 if(value < 0) _ASN_DECODE_STARVED;
608 lhalf = per_get_few_bits(pd, 16);
609 if(lhalf < 0) _ASN_DECODE_STARVED;
610 value = (value << 16) | lhalf;
611 } else {
612 value = per_get_few_bits(pd, ct->range_bits);
613 if(value < 0) _ASN_DECODE_STARVED;
614 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000615 ASN_DEBUG("Got value %ld + low %ld",
616 value, ct->lower_bound);
617 value += ct->lower_bound;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000618 if((specs && specs->field_unsigned)
619 ? asn_ulong2INTEGER(st, value)
620 : asn_long2INTEGER(st, value))
Lev Walkin59b176e2005-11-26 11:25:14 +0000621 _ASN_DECODE_FAILED;
622 return rval;
623 }
624 } else {
625 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
626 }
627
628 /* X.691, #12.2.3, #12.2.4 */
629 do {
630 ssize_t len;
631 void *p;
632 int ret;
633
634 /* Get the PER length */
635 len = uper_get_length(pd, -1, &repeat);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000636 if(len < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000637
638 p = REALLOC(st->buf, st->size + len + 1);
639 if(!p) _ASN_DECODE_FAILED;
640 st->buf = (uint8_t *)p;
641
642 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000643 if(ret < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000644 st->size += len;
645 } while(repeat);
646 st->buf[st->size] = 0; /* JIC */
647
648 /* #12.2.3 */
649 if(ct && ct->lower_bound) {
650 /*
651 * TODO: replace by in-place arithmetics.
652 */
653 long value;
654 if(asn_INTEGER2long(st, &value))
655 _ASN_DECODE_FAILED;
656 if(asn_long2INTEGER(st, value + ct->lower_bound))
657 _ASN_DECODE_FAILED;
658 }
659
660 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000661}
662
Lev Walkin523de9e2006-08-18 01:34:18 +0000663asn_enc_rval_t
664INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
665 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000666 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000667 asn_enc_rval_t er;
668 INTEGER_t *st = (INTEGER_t *)sptr;
669 const uint8_t *buf;
670 const uint8_t *end;
671 asn_per_constraint_t *ct;
672 long value = 0;
673
674 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
675
676 if(!constraints) constraints = td->per_constraints;
677 ct = constraints ? &constraints->value : 0;
678
679 er.encoded = 0;
680
681 if(ct) {
682 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000683 if(specs && specs->field_unsigned) {
684 unsigned long uval;
685 if(asn_INTEGER2ulong(st, &uval))
686 _ASN_ENCODE_FAILED;
687 /* Check proper range */
688 if(ct->flags & APC_SEMI_CONSTRAINED) {
689 if(uval < (unsigned long)ct->lower_bound)
690 inext = 1;
691 } else if(ct->range_bits >= 0) {
692 if(uval < (unsigned long)ct->lower_bound
693 || uval > (unsigned long)ct->upper_bound)
694 inext = 1;
695 }
696 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
697 uval, st->buf[0], st->size,
698 ct->lower_bound, ct->upper_bound,
699 inext ? "ext" : "fix");
700 value = uval;
701 } else {
702 if(asn_INTEGER2long(st, &value))
703 _ASN_ENCODE_FAILED;
704 /* Check proper range */
705 if(ct->flags & APC_SEMI_CONSTRAINED) {
706 if(value < ct->lower_bound)
707 inext = 1;
708 } else if(ct->range_bits >= 0) {
709 if(value < ct->lower_bound
710 || value > ct->upper_bound)
711 inext = 1;
712 }
713 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
714 value, st->buf[0], st->size,
715 ct->lower_bound, ct->upper_bound,
716 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000717 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000718 if(ct->flags & APC_EXTENSIBLE) {
719 if(per_put_few_bits(po, inext, 1))
720 _ASN_ENCODE_FAILED;
721 if(inext) ct = 0;
722 } else if(inext) {
723 _ASN_ENCODE_FAILED;
724 }
725 }
726
727
728 /* X.691, #12.2.2 */
729 if(ct && ct->range_bits >= 0) {
730 /* #10.5.6 */
731 ASN_DEBUG("Encoding integer with range %d bits",
732 ct->range_bits);
Lev Walkina105cbc2007-11-06 02:35:13 +0000733 if(ct->range_bits == 32) {
734 /* TODO: extend to >32 bits */
735 long v = value - ct->lower_bound;
736 if(per_put_few_bits(po, v >> 1, 31)
737 || per_put_few_bits(po, v, 1))
738 _ASN_ENCODE_FAILED;
739 } else {
740 if(per_put_few_bits(po, value - ct->lower_bound,
Lev Walkin523de9e2006-08-18 01:34:18 +0000741 ct->range_bits))
Lev Walkina105cbc2007-11-06 02:35:13 +0000742 _ASN_ENCODE_FAILED;
743 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000744 _ASN_ENCODED_OK(er);
745 }
746
747 if(ct && ct->lower_bound) {
748 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
749 /* TODO: adjust lower bound */
750 _ASN_ENCODE_FAILED;
751 }
752
753 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
754 ssize_t mayEncode = uper_put_length(po, end - buf);
755 if(mayEncode < 0)
756 _ASN_ENCODE_FAILED;
757 if(per_put_many_bits(po, buf, 8 * mayEncode))
758 _ASN_ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000759 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000760 }
761
762 _ASN_ENCODED_OK(er);
763}
764
Lev Walkinf15320b2004-06-03 03:38:44 +0000765int
Lev Walkin5e033762004-09-29 13:26:15 +0000766asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000767 uint8_t *b, *end;
768 size_t size;
769 long l;
770
771 /* Sanity checking */
772 if(!iptr || !iptr->buf || !lptr) {
773 errno = EINVAL;
774 return -1;
775 }
776
777 /* Cache the begin/end of the buffer */
778 b = iptr->buf; /* Start of the INTEGER buffer */
779 size = iptr->size;
780 end = b + size; /* Where to stop */
781
782 if(size > sizeof(long)) {
783 uint8_t *end1 = end - 1;
784 /*
785 * Slightly more advanced processing,
786 * able to >sizeof(long) bytes,
787 * when the actual value is small
788 * (0x0000000000abcdef would yield a fine 0x00abcdef)
789 */
790 /* Skip out the insignificant leading bytes */
791 for(; b < end1; b++) {
792 switch(*b) {
793 case 0x00: if((b[1] & 0x80) == 0) continue; break;
794 case 0xff: if((b[1] & 0x80) != 0) continue; break;
795 }
796 break;
797 }
798
799 size = end - b;
800 if(size > sizeof(long)) {
801 /* Still cannot fit the long */
802 errno = ERANGE;
803 return -1;
804 }
805 }
806
807 /* Shortcut processing of a corner case */
808 if(end == b) {
809 *lptr = 0;
810 return 0;
811 }
812
813 /* Perform the sign initialization */
814 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
815 if((*b >> 7)) l = -1; else l = 0;
816
817 /* Conversion engine */
818 for(; b < end; b++)
819 l = (l << 8) | *b;
820
821 *lptr = l;
822 return 0;
823}
Lev Walkind703ff42004-10-21 11:21:25 +0000824
825int
Lev Walkin5c879db2007-11-06 06:23:31 +0000826asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
827 uint8_t *b, *end;
828 unsigned long l;
829 size_t size;
830
831 if(!iptr || !iptr->buf || !lptr) {
832 errno = EINVAL;
833 return -1;
834 }
835
836 b = iptr->buf;
837 size = iptr->size;
838 end = b + size;
839
840 /* If all extra leading bytes are zeroes, ignore them */
841 for(; size > sizeof(unsigned long); b++, size--) {
842 if(*b) {
843 /* Value won't fit unsigned long */
844 errno = ERANGE;
845 return -1;
846 }
847 }
848
849 /* Conversion engine */
850 for(l = 0; b < end; b++)
851 l = (l << 8) | *b;
852
853 *lptr = l;
854 return 0;
855}
856
857int
858asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
859 uint8_t *buf;
860 uint8_t *end;
861 uint8_t *b;
862 int shr;
863
864 if(value <= LONG_MAX)
865 return asn_long2INTEGER(st, value);
866
867 buf = (uint8_t *)MALLOC(1 + sizeof(value));
868 if(!buf) return -1;
869
870 end = buf + (sizeof(value) + 1);
871 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +0000872 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
873 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000874
875 if(st->buf) FREEMEM(st->buf);
876 st->buf = buf;
877 st->size = 1 + sizeof(value);
878
879 return 0;
880}
881
882int
Lev Walkind703ff42004-10-21 11:21:25 +0000883asn_long2INTEGER(INTEGER_t *st, long value) {
884 uint8_t *buf, *bp;
885 uint8_t *p;
886 uint8_t *pstart;
887 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000888 int littleEndian = 1; /* Run-time detection */
889 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000890
891 if(!st) {
892 errno = EINVAL;
893 return -1;
894 }
895
Lev Walkin8471cec2004-10-21 14:02:19 +0000896 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000897 if(!buf) return -1;
898
Lev Walkind7ad5612004-10-26 08:20:46 +0000899 if(*(char *)&littleEndian) {
900 pstart = (uint8_t *)&value + sizeof(value) - 1;
901 pend1 = (uint8_t *)&value;
902 add = -1;
903 } else {
904 pstart = (uint8_t *)&value;
905 pend1 = pstart + sizeof(value) - 1;
906 add = 1;
907 }
908
Lev Walkind703ff42004-10-21 11:21:25 +0000909 /*
910 * If the contents octet consists of more than one octet,
911 * then bits of the first octet and bit 8 of the second octet:
912 * a) shall not all be ones; and
913 * b) shall not all be zero.
914 */
Lev Walkin33700162004-10-26 09:03:31 +0000915 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000916 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000917 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000918 continue;
919 break;
Lev Walkin33700162004-10-26 09:03:31 +0000920 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000921 continue;
922 break;
923 }
924 break;
925 }
926 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000927 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
928 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000929
930 if(st->buf) FREEMEM(st->buf);
931 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000932 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000933
934 return 0;
935}