blob: e7a5ba670889c3e42785825259f1f6eb72dd4d6f [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 Walkind703ff42004-10-21 11:21:25 +0000342 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000343 * We may have received a tag here. It will be processed inline.
344 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000345 */
Lev Walkin0be3a992004-10-21 12:11:57 +0000346 for(value = 0, lp = lstart; lp < lstop; lp++) {
347 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000348 switch(lv) {
349 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000350 switch(state) {
351 case ST_SKIPSPACE:
352 case ST_SKIPSPHEX:
353 continue;
354 case ST_HEXCOLON:
355 if(xer_is_whitespace(lp, lstop - lp)) {
356 lp = lstop - 1;
357 continue;
358 }
359 break;
360 default:
361 break;
362 }
Lev Walkind703ff42004-10-21 11:21:25 +0000363 break;
364 case 0x2d: /* '-' */
365 if(state == ST_SKIPSPACE) {
366 sign = -1;
367 state = ST_WAITDIGITS;
368 continue;
369 }
370 break;
371 case 0x2b: /* '+' */
372 if(state == ST_SKIPSPACE) {
373 state = ST_WAITDIGITS;
374 continue;
375 }
376 break;
377 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
378 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000379 switch(state) {
380 case ST_DIGITS: break;
381 case ST_SKIPSPHEX: /* Fall through */
382 case ST_HEXDIGIT1:
383 value = (lv - 0x30) << 4;
384 state = ST_HEXDIGIT2;
385 continue;
386 case ST_HEXDIGIT2:
387 value += (lv - 0x30);
388 state = ST_HEXCOLON;
Lev Walkinf3c089e2007-11-13 23:53:13 +0000389 st->buf[st->size++] = (uint8_t)value;
Lev Walkinc744a022006-09-15 18:33:25 +0000390 continue;
391 case ST_HEXCOLON:
392 return XPBD_BROKEN_ENCODING;
393 default:
394 state = ST_DIGITS;
395 break;
396 }
Lev Walkind703ff42004-10-21 11:21:25 +0000397
Lev Walkin0be3a992004-10-21 12:11:57 +0000398 {
Lev Walkin9a338f82012-01-22 16:38:37 -0800399 long volatile new_value = value * 10;
400 /* GCC 4.x optimizes (new_value) without `volatile'
401 * so the following check does not detect overflow. */
Lev Walkin0be3a992004-10-21 12:11:57 +0000402
403 if(new_value / 10 != value)
404 /* Overflow */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000405 return XPBD_DECODER_LIMIT;
Lev Walkin0be3a992004-10-21 12:11:57 +0000406
407 value = new_value + (lv - 0x30);
Lev Walkind703ff42004-10-21 11:21:25 +0000408 /* Check for two's complement overflow */
409 if(value < 0) {
410 /* Check whether it is a LONG_MIN */
411 if(sign == -1
Lev Walkin8471cec2004-10-21 14:02:19 +0000412 && (unsigned long)value
413 == ~((unsigned long)-1 >> 1)) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000414 sign = 1;
Lev Walkind703ff42004-10-21 11:21:25 +0000415 } else {
416 /* Overflow */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000417 return XPBD_DECODER_LIMIT;
Lev Walkind703ff42004-10-21 11:21:25 +0000418 }
419 }
Lev Walkin0be3a992004-10-21 12:11:57 +0000420 }
Lev Walkind703ff42004-10-21 11:21:25 +0000421 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000422 case 0x3c: /* '<' */
423 if(state == ST_SKIPSPACE) {
424 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000425 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000426 (asn_INTEGER_specifics_t *)
427 td->specifics, lstart, lstop);
428 if(el) {
429 ASN_DEBUG("Found \"%s\" => %ld",
430 el->enum_name, el->nat_value);
431 state = ST_DIGITS;
432 value = el->nat_value;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000433 lp = lstop - 1;
434 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000435 }
436 ASN_DEBUG("Unknown identifier for INTEGER");
437 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000438 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000439 case 0x3a: /* ':' */
440 if(state == ST_HEXCOLON) {
441 /* This colon is expected */
442 state = ST_HEXDIGIT1;
443 continue;
444 } else if(state == ST_DIGITS) {
445 /* The colon here means that we have
446 * decoded the first two hexadecimal
447 * places as a decimal value.
448 * Switch decoding mode. */
449 ASN_DEBUG("INTEGER re-evaluate as hex form");
450 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
451 return XPBD_SYSTEM_FAILURE;
452 state = ST_SKIPSPHEX;
453 lp = lstart - 1;
454 continue;
455 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400456 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000457 break;
458 }
459 /* [A-Fa-f] */
460 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
461 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
462 switch(state) {
463 case ST_SKIPSPHEX:
464 case ST_SKIPSPACE: /* Fall through */
465 case ST_HEXDIGIT1:
466 value = lv - ((lv < 0x61) ? 0x41 : 0x61);
467 value += 10;
468 value <<= 4;
469 state = ST_HEXDIGIT2;
470 continue;
471 case ST_HEXDIGIT2:
472 value += lv - ((lv < 0x61) ? 0x41 : 0x61);
473 value += 10;
Lev Walkinf3c089e2007-11-13 23:53:13 +0000474 st->buf[st->size++] = (uint8_t)value;
Lev Walkinc744a022006-09-15 18:33:25 +0000475 state = ST_HEXCOLON;
476 continue;
477 case ST_DIGITS:
478 ASN_DEBUG("INTEGER re-evaluate as hex form");
479 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
480 return XPBD_SYSTEM_FAILURE;
481 state = ST_SKIPSPHEX;
482 lp = lstart - 1;
483 continue;
484 default:
485 break;
486 }
487 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000488 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000489
490 /* Found extra non-numeric stuff */
Lev Walkin2e763992011-07-21 01:17:37 +0400491 ASN_DEBUG("Found non-numeric 0x%2x at %ld",
492 lv, (long)(lp - lstart));
Lev Walkin806b0bb2005-03-09 22:31:22 +0000493 state = ST_EXTRASTUFF;
Lev Walkin0be3a992004-10-21 12:11:57 +0000494 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000495 }
496
Lev Walkinc744a022006-09-15 18:33:25 +0000497 switch(state) {
498 case ST_DIGITS:
499 /* Everything is cool */
500 break;
501 case ST_HEXCOLON:
502 st->buf[st->size] = 0; /* Just in case termination */
503 return XPBD_BODY_CONSUMED;
504 case ST_HEXDIGIT1:
505 case ST_HEXDIGIT2:
506 case ST_SKIPSPHEX:
507 return XPBD_BROKEN_ENCODING;
508 default:
Lev Walkin806b0bb2005-03-09 22:31:22 +0000509 if(xer_is_whitespace(lp, lstop - lp)) {
510 if(state != ST_EXTRASTUFF)
511 return XPBD_NOT_BODY_IGNORE;
Lev Walkinc744a022006-09-15 18:33:25 +0000512 break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000513 } else {
Lev Walkinc744a022006-09-15 18:33:25 +0000514 ASN_DEBUG("INTEGER: No useful digits (state %d)",
515 state);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000516 return XPBD_BROKEN_ENCODING; /* No digits */
517 }
Lev Walkinc744a022006-09-15 18:33:25 +0000518 break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000519 }
Lev Walkind703ff42004-10-21 11:21:25 +0000520
521 value *= sign; /* Change sign, if needed */
522
523 if(asn_long2INTEGER(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000524 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000525
Lev Walkin0fab1a62005-03-09 22:19:25 +0000526 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000527}
528
529asn_dec_rval_t
530INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
531 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000532 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000533
534 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000535 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000536 buf_ptr, size, INTEGER__xer_body_decode);
537}
538
Lev Walkina9cc46e2004-09-22 16:06:28 +0000539asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000540INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000541 int ilevel, enum xer_encoder_flags_e flags,
542 asn_app_consume_bytes_f *cb, void *app_key) {
543 const INTEGER_t *st = (const INTEGER_t *)sptr;
544 asn_enc_rval_t er;
545
546 (void)ilevel;
547 (void)flags;
548
Lev Walkind500a962005-11-27 13:06:56 +0000549 if(!st || !st->buf)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000550 _ASN_ENCODE_FAILED;
551
Lev Walkine0b56e02005-02-25 12:10:27 +0000552 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000553 if(er.encoded < 0) _ASN_ENCODE_FAILED;
554
Lev Walkin59b176e2005-11-26 11:25:14 +0000555 _ASN_ENCODED_OK(er);
556}
557
558asn_dec_rval_t
559INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
560 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000561 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000562 asn_dec_rval_t rval = { RC_OK, 0 };
563 INTEGER_t *st = (INTEGER_t *)*sptr;
564 asn_per_constraint_t *ct;
565 int repeat;
566
567 (void)opt_codec_ctx;
568
569 if(!st) {
570 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
571 if(!st) _ASN_DECODE_FAILED;
572 }
573
574 if(!constraints) constraints = td->per_constraints;
575 ct = constraints ? &constraints->value : 0;
576
577 if(ct && ct->flags & APC_EXTENSIBLE) {
578 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000579 if(inext < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000580 if(inext) ct = 0;
581 }
582
583 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000584 st->buf = 0;
585 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000586 if(ct) {
587 if(ct->flags & APC_SEMI_CONSTRAINED) {
588 st->buf = (uint8_t *)CALLOC(1, 2);
589 if(!st->buf) _ASN_DECODE_FAILED;
590 st->size = 1;
591 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
592 size_t size = (ct->range_bits + 7) >> 3;
593 st->buf = (uint8_t *)MALLOC(1 + size + 1);
594 if(!st->buf) _ASN_DECODE_FAILED;
595 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000596 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000597 }
598
599 /* X.691, #12.2.2 */
600 if(ct && ct->flags != APC_UNCONSTRAINED) {
601 /* #10.5.6 */
602 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
603 if(ct->range_bits >= 0) {
Lev Walkina105cbc2007-11-06 02:35:13 +0000604 long value;
605 if(ct->range_bits == 32) {
606 long lhalf;
607 value = per_get_few_bits(pd, 16);
608 if(value < 0) _ASN_DECODE_STARVED;
609 lhalf = per_get_few_bits(pd, 16);
610 if(lhalf < 0) _ASN_DECODE_STARVED;
611 value = (value << 16) | lhalf;
612 } else {
613 value = per_get_few_bits(pd, ct->range_bits);
614 if(value < 0) _ASN_DECODE_STARVED;
615 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000616 ASN_DEBUG("Got value %ld + low %ld",
617 value, ct->lower_bound);
618 value += ct->lower_bound;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000619 if((specs && specs->field_unsigned)
620 ? asn_ulong2INTEGER(st, value)
621 : asn_long2INTEGER(st, value))
Lev Walkin59b176e2005-11-26 11:25:14 +0000622 _ASN_DECODE_FAILED;
623 return rval;
624 }
625 } else {
626 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
627 }
628
629 /* X.691, #12.2.3, #12.2.4 */
630 do {
631 ssize_t len;
632 void *p;
633 int ret;
634
635 /* Get the PER length */
636 len = uper_get_length(pd, -1, &repeat);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000637 if(len < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000638
639 p = REALLOC(st->buf, st->size + len + 1);
640 if(!p) _ASN_DECODE_FAILED;
641 st->buf = (uint8_t *)p;
642
643 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000644 if(ret < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000645 st->size += len;
646 } while(repeat);
647 st->buf[st->size] = 0; /* JIC */
648
649 /* #12.2.3 */
650 if(ct && ct->lower_bound) {
651 /*
652 * TODO: replace by in-place arithmetics.
653 */
654 long value;
655 if(asn_INTEGER2long(st, &value))
656 _ASN_DECODE_FAILED;
657 if(asn_long2INTEGER(st, value + ct->lower_bound))
658 _ASN_DECODE_FAILED;
659 }
660
661 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000662}
663
Lev Walkin523de9e2006-08-18 01:34:18 +0000664asn_enc_rval_t
665INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
666 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000667 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000668 asn_enc_rval_t er;
669 INTEGER_t *st = (INTEGER_t *)sptr;
670 const uint8_t *buf;
671 const uint8_t *end;
672 asn_per_constraint_t *ct;
673 long value = 0;
674
675 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
676
677 if(!constraints) constraints = td->per_constraints;
678 ct = constraints ? &constraints->value : 0;
679
680 er.encoded = 0;
681
682 if(ct) {
683 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000684 if(specs && specs->field_unsigned) {
685 unsigned long uval;
686 if(asn_INTEGER2ulong(st, &uval))
687 _ASN_ENCODE_FAILED;
688 /* Check proper range */
689 if(ct->flags & APC_SEMI_CONSTRAINED) {
690 if(uval < (unsigned long)ct->lower_bound)
691 inext = 1;
692 } else if(ct->range_bits >= 0) {
693 if(uval < (unsigned long)ct->lower_bound
694 || uval > (unsigned long)ct->upper_bound)
695 inext = 1;
696 }
697 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
698 uval, st->buf[0], st->size,
699 ct->lower_bound, ct->upper_bound,
700 inext ? "ext" : "fix");
701 value = uval;
702 } else {
703 if(asn_INTEGER2long(st, &value))
704 _ASN_ENCODE_FAILED;
705 /* Check proper range */
706 if(ct->flags & APC_SEMI_CONSTRAINED) {
707 if(value < ct->lower_bound)
708 inext = 1;
709 } else if(ct->range_bits >= 0) {
710 if(value < ct->lower_bound
711 || value > ct->upper_bound)
712 inext = 1;
713 }
714 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
715 value, st->buf[0], st->size,
716 ct->lower_bound, ct->upper_bound,
717 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000718 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000719 if(ct->flags & APC_EXTENSIBLE) {
720 if(per_put_few_bits(po, inext, 1))
721 _ASN_ENCODE_FAILED;
722 if(inext) ct = 0;
723 } else if(inext) {
724 _ASN_ENCODE_FAILED;
725 }
726 }
727
728
729 /* X.691, #12.2.2 */
730 if(ct && ct->range_bits >= 0) {
731 /* #10.5.6 */
732 ASN_DEBUG("Encoding integer with range %d bits",
733 ct->range_bits);
Lev Walkina105cbc2007-11-06 02:35:13 +0000734 if(ct->range_bits == 32) {
735 /* TODO: extend to >32 bits */
736 long v = value - ct->lower_bound;
737 if(per_put_few_bits(po, v >> 1, 31)
738 || per_put_few_bits(po, v, 1))
739 _ASN_ENCODE_FAILED;
740 } else {
741 if(per_put_few_bits(po, value - ct->lower_bound,
Lev Walkin523de9e2006-08-18 01:34:18 +0000742 ct->range_bits))
Lev Walkina105cbc2007-11-06 02:35:13 +0000743 _ASN_ENCODE_FAILED;
744 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000745 _ASN_ENCODED_OK(er);
746 }
747
748 if(ct && ct->lower_bound) {
749 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
750 /* TODO: adjust lower bound */
751 _ASN_ENCODE_FAILED;
752 }
753
754 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
755 ssize_t mayEncode = uper_put_length(po, end - buf);
756 if(mayEncode < 0)
757 _ASN_ENCODE_FAILED;
758 if(per_put_many_bits(po, buf, 8 * mayEncode))
759 _ASN_ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000760 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000761 }
762
763 _ASN_ENCODED_OK(er);
764}
765
Lev Walkinf15320b2004-06-03 03:38:44 +0000766int
Lev Walkin5e033762004-09-29 13:26:15 +0000767asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000768 uint8_t *b, *end;
769 size_t size;
770 long l;
771
772 /* Sanity checking */
773 if(!iptr || !iptr->buf || !lptr) {
774 errno = EINVAL;
775 return -1;
776 }
777
778 /* Cache the begin/end of the buffer */
779 b = iptr->buf; /* Start of the INTEGER buffer */
780 size = iptr->size;
781 end = b + size; /* Where to stop */
782
783 if(size > sizeof(long)) {
784 uint8_t *end1 = end - 1;
785 /*
786 * Slightly more advanced processing,
787 * able to >sizeof(long) bytes,
788 * when the actual value is small
789 * (0x0000000000abcdef would yield a fine 0x00abcdef)
790 */
791 /* Skip out the insignificant leading bytes */
792 for(; b < end1; b++) {
793 switch(*b) {
794 case 0x00: if((b[1] & 0x80) == 0) continue; break;
795 case 0xff: if((b[1] & 0x80) != 0) continue; break;
796 }
797 break;
798 }
799
800 size = end - b;
801 if(size > sizeof(long)) {
802 /* Still cannot fit the long */
803 errno = ERANGE;
804 return -1;
805 }
806 }
807
808 /* Shortcut processing of a corner case */
809 if(end == b) {
810 *lptr = 0;
811 return 0;
812 }
813
814 /* Perform the sign initialization */
815 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
816 if((*b >> 7)) l = -1; else l = 0;
817
818 /* Conversion engine */
819 for(; b < end; b++)
820 l = (l << 8) | *b;
821
822 *lptr = l;
823 return 0;
824}
Lev Walkind703ff42004-10-21 11:21:25 +0000825
826int
Lev Walkin5c879db2007-11-06 06:23:31 +0000827asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
828 uint8_t *b, *end;
829 unsigned long l;
830 size_t size;
831
832 if(!iptr || !iptr->buf || !lptr) {
833 errno = EINVAL;
834 return -1;
835 }
836
837 b = iptr->buf;
838 size = iptr->size;
839 end = b + size;
840
841 /* If all extra leading bytes are zeroes, ignore them */
842 for(; size > sizeof(unsigned long); b++, size--) {
843 if(*b) {
844 /* Value won't fit unsigned long */
845 errno = ERANGE;
846 return -1;
847 }
848 }
849
850 /* Conversion engine */
851 for(l = 0; b < end; b++)
852 l = (l << 8) | *b;
853
854 *lptr = l;
855 return 0;
856}
857
858int
859asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
860 uint8_t *buf;
861 uint8_t *end;
862 uint8_t *b;
863 int shr;
864
865 if(value <= LONG_MAX)
866 return asn_long2INTEGER(st, value);
867
868 buf = (uint8_t *)MALLOC(1 + sizeof(value));
869 if(!buf) return -1;
870
871 end = buf + (sizeof(value) + 1);
872 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +0000873 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
874 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000875
876 if(st->buf) FREEMEM(st->buf);
877 st->buf = buf;
878 st->size = 1 + sizeof(value);
879
880 return 0;
881}
882
883int
Lev Walkind703ff42004-10-21 11:21:25 +0000884asn_long2INTEGER(INTEGER_t *st, long value) {
885 uint8_t *buf, *bp;
886 uint8_t *p;
887 uint8_t *pstart;
888 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000889 int littleEndian = 1; /* Run-time detection */
890 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000891
892 if(!st) {
893 errno = EINVAL;
894 return -1;
895 }
896
Lev Walkin8471cec2004-10-21 14:02:19 +0000897 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000898 if(!buf) return -1;
899
Lev Walkind7ad5612004-10-26 08:20:46 +0000900 if(*(char *)&littleEndian) {
901 pstart = (uint8_t *)&value + sizeof(value) - 1;
902 pend1 = (uint8_t *)&value;
903 add = -1;
904 } else {
905 pstart = (uint8_t *)&value;
906 pend1 = pstart + sizeof(value) - 1;
907 add = 1;
908 }
909
Lev Walkind703ff42004-10-21 11:21:25 +0000910 /*
911 * If the contents octet consists of more than one octet,
912 * then bits of the first octet and bit 8 of the second octet:
913 * a) shall not all be ones; and
914 * b) shall not all be zero.
915 */
Lev Walkin33700162004-10-26 09:03:31 +0000916 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000917 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000918 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000919 continue;
920 break;
Lev Walkin33700162004-10-26 09:03:31 +0000921 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000922 continue;
923 break;
924 }
925 break;
926 }
927 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000928 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
929 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000930
931 if(st->buf) FREEMEM(st->buf);
932 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000933 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000934
935 return 0;
936}