blob: e9f61ac018ce162e795059562a44bd05612f968e [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 Walkind703ff42004-10-21 11:21:25 +0000300/*
301 * Decode the chunk of XML text encoding INTEGER.
302 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000303static enum xer_pbd_rval
304INTEGER__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 +0000305 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkind703ff42004-10-21 11:21:25 +0000306 long sign = 1;
307 long value;
Lev Walkine0b56e02005-02-25 12:10:27 +0000308 const char *lp;
309 const char *lstart = (const char *)chunk_buf;
310 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000311 enum {
312 ST_SKIPSPACE,
313 ST_WAITDIGITS,
314 ST_DIGITS,
Lev Walkinfdb25922005-07-21 09:32:49 +0000315 ST_EXTRASTUFF
Lev Walkind703ff42004-10-21 11:21:25 +0000316 } state = ST_SKIPSPACE;
Lev Walkin0be3a992004-10-21 12:11:57 +0000317
Lev Walkind703ff42004-10-21 11:21:25 +0000318 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000319 * We may have received a tag here. It will be processed inline.
320 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000321 */
Lev Walkin0be3a992004-10-21 12:11:57 +0000322 for(value = 0, lp = lstart; lp < lstop; lp++) {
323 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000324 switch(lv) {
325 case 0x09: case 0x0a: case 0x0d: case 0x20:
326 if(state == ST_SKIPSPACE) continue;
327 break;
328 case 0x2d: /* '-' */
329 if(state == ST_SKIPSPACE) {
330 sign = -1;
331 state = ST_WAITDIGITS;
332 continue;
333 }
334 break;
335 case 0x2b: /* '+' */
336 if(state == ST_SKIPSPACE) {
337 state = ST_WAITDIGITS;
338 continue;
339 }
340 break;
341 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
342 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
343 if(state != ST_DIGITS) state = ST_DIGITS;
344
Lev Walkin0be3a992004-10-21 12:11:57 +0000345 {
346 long new_value = value * 10;
347
348 if(new_value / 10 != value)
349 /* Overflow */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000350 return XPBD_DECODER_LIMIT;
Lev Walkin0be3a992004-10-21 12:11:57 +0000351
352 value = new_value + (lv - 0x30);
Lev Walkind703ff42004-10-21 11:21:25 +0000353 /* Check for two's complement overflow */
354 if(value < 0) {
355 /* Check whether it is a LONG_MIN */
356 if(sign == -1
Lev Walkin8471cec2004-10-21 14:02:19 +0000357 && (unsigned long)value
358 == ~((unsigned long)-1 >> 1)) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000359 sign = 1;
Lev Walkind703ff42004-10-21 11:21:25 +0000360 } else {
361 /* Overflow */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000362 return XPBD_DECODER_LIMIT;
Lev Walkind703ff42004-10-21 11:21:25 +0000363 }
364 }
Lev Walkin0be3a992004-10-21 12:11:57 +0000365 }
Lev Walkind703ff42004-10-21 11:21:25 +0000366 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000367 case 0x3c: /* '<' */
368 if(state == ST_SKIPSPACE) {
369 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000370 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000371 (asn_INTEGER_specifics_t *)
372 td->specifics, lstart, lstop);
373 if(el) {
374 ASN_DEBUG("Found \"%s\" => %ld",
375 el->enum_name, el->nat_value);
376 state = ST_DIGITS;
377 value = el->nat_value;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000378 lp = lstop - 1;
379 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000380 }
381 ASN_DEBUG("Unknown identifier for INTEGER");
382 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000383 return XPBD_BROKEN_ENCODING;
Lev Walkind703ff42004-10-21 11:21:25 +0000384 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000385
386 /* Found extra non-numeric stuff */
387 state = ST_EXTRASTUFF;
Lev Walkin0be3a992004-10-21 12:11:57 +0000388 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000389 }
390
Lev Walkin0fab1a62005-03-09 22:19:25 +0000391 if(state != ST_DIGITS) {
Lev Walkin806b0bb2005-03-09 22:31:22 +0000392 if(xer_is_whitespace(lp, lstop - lp)) {
393 if(state != ST_EXTRASTUFF)
394 return XPBD_NOT_BODY_IGNORE;
395 /* Fall through */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000396 } else {
397 ASN_DEBUG("No useful digits in output");
398 return XPBD_BROKEN_ENCODING; /* No digits */
399 }
400 }
Lev Walkind703ff42004-10-21 11:21:25 +0000401
402 value *= sign; /* Change sign, if needed */
403
404 if(asn_long2INTEGER(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000405 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000406
Lev Walkin0fab1a62005-03-09 22:19:25 +0000407 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000408}
409
410asn_dec_rval_t
411INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
412 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000413 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000414
415 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000416 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000417 buf_ptr, size, INTEGER__xer_body_decode);
418}
419
Lev Walkina9cc46e2004-09-22 16:06:28 +0000420asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000421INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000422 int ilevel, enum xer_encoder_flags_e flags,
423 asn_app_consume_bytes_f *cb, void *app_key) {
424 const INTEGER_t *st = (const INTEGER_t *)sptr;
425 asn_enc_rval_t er;
426
427 (void)ilevel;
428 (void)flags;
429
Lev Walkind500a962005-11-27 13:06:56 +0000430 if(!st || !st->buf)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000431 _ASN_ENCODE_FAILED;
432
Lev Walkine0b56e02005-02-25 12:10:27 +0000433 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000434 if(er.encoded < 0) _ASN_ENCODE_FAILED;
435
Lev Walkin59b176e2005-11-26 11:25:14 +0000436 _ASN_ENCODED_OK(er);
437}
438
439asn_dec_rval_t
440INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
441 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
442 asn_dec_rval_t rval = { RC_OK, 0 };
443 INTEGER_t *st = (INTEGER_t *)*sptr;
444 asn_per_constraint_t *ct;
445 int repeat;
446
447 (void)opt_codec_ctx;
448
449 if(!st) {
450 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
451 if(!st) _ASN_DECODE_FAILED;
452 }
453
454 if(!constraints) constraints = td->per_constraints;
455 ct = constraints ? &constraints->value : 0;
456
457 if(ct && ct->flags & APC_EXTENSIBLE) {
458 int inext = per_get_few_bits(pd, 1);
459 if(inext < 0) _ASN_DECODE_FAILED;
460 if(inext) ct = 0;
461 }
462
463 FREEMEM(st->buf);
464 if(ct) {
465 if(ct->flags & APC_SEMI_CONSTRAINED) {
466 st->buf = (uint8_t *)CALLOC(1, 2);
467 if(!st->buf) _ASN_DECODE_FAILED;
468 st->size = 1;
469 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
470 size_t size = (ct->range_bits + 7) >> 3;
471 st->buf = (uint8_t *)MALLOC(1 + size + 1);
472 if(!st->buf) _ASN_DECODE_FAILED;
473 st->size = size;
474 } else {
475 st->size = 0;
476 }
477 } else {
478 st->size = 0;
479 }
480
481 /* X.691, #12.2.2 */
482 if(ct && ct->flags != APC_UNCONSTRAINED) {
483 /* #10.5.6 */
484 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
485 if(ct->range_bits >= 0) {
486 long value = per_get_few_bits(pd, ct->range_bits);
487 if(value < 0) _ASN_DECODE_FAILED;
488 ASN_DEBUG("Got value %ld + low %ld",
489 value, ct->lower_bound);
490 value += ct->lower_bound;
491 if(asn_long2INTEGER(st, value))
492 _ASN_DECODE_FAILED;
493 return rval;
494 }
495 } else {
496 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
497 }
498
499 /* X.691, #12.2.3, #12.2.4 */
500 do {
501 ssize_t len;
502 void *p;
503 int ret;
504
505 /* Get the PER length */
506 len = uper_get_length(pd, -1, &repeat);
507 if(len < 0) _ASN_DECODE_FAILED;
508
509 p = REALLOC(st->buf, st->size + len + 1);
510 if(!p) _ASN_DECODE_FAILED;
511 st->buf = (uint8_t *)p;
512
513 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
514 if(ret < 0) _ASN_DECODE_FAILED;
515 st->size += len;
516 } while(repeat);
517 st->buf[st->size] = 0; /* JIC */
518
519 /* #12.2.3 */
520 if(ct && ct->lower_bound) {
521 /*
522 * TODO: replace by in-place arithmetics.
523 */
524 long value;
525 if(asn_INTEGER2long(st, &value))
526 _ASN_DECODE_FAILED;
527 if(asn_long2INTEGER(st, value + ct->lower_bound))
528 _ASN_DECODE_FAILED;
529 }
530
531 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000532}
533
Lev Walkin523de9e2006-08-18 01:34:18 +0000534asn_enc_rval_t
535INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
536 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
537 asn_enc_rval_t er;
538 INTEGER_t *st = (INTEGER_t *)sptr;
539 const uint8_t *buf;
540 const uint8_t *end;
541 asn_per_constraint_t *ct;
542 long value = 0;
543
544 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
545
546 if(!constraints) constraints = td->per_constraints;
547 ct = constraints ? &constraints->value : 0;
548
549 er.encoded = 0;
550
551 if(ct) {
552 int inext = 0;
553 if(asn_INTEGER2long(st, &value))
554 _ASN_ENCODE_FAILED;
555 /* Check proper range */
556 if(ct->flags & APC_SEMI_CONSTRAINED) {
557 if(value < ct->lower_bound)
558 inext = 1;
559 } else if(ct->range_bits >= 0) {
560 if(value < ct->lower_bound
561 || value > ct->upper_bound)
562 inext = 1;
563 }
564 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
565 value, st->buf[0], st->size,
566 ct->lower_bound, ct->upper_bound,
567 inext ? "ext" : "fix");
568 if(ct->flags & APC_EXTENSIBLE) {
569 if(per_put_few_bits(po, inext, 1))
570 _ASN_ENCODE_FAILED;
571 if(inext) ct = 0;
572 } else if(inext) {
573 _ASN_ENCODE_FAILED;
574 }
575 }
576
577
578 /* X.691, #12.2.2 */
579 if(ct && ct->range_bits >= 0) {
580 /* #10.5.6 */
581 ASN_DEBUG("Encoding integer with range %d bits",
582 ct->range_bits);
583 if(per_put_few_bits(po, value - ct->lower_bound,
584 ct->range_bits))
585 _ASN_ENCODE_FAILED;
586 _ASN_ENCODED_OK(er);
587 }
588
589 if(ct && ct->lower_bound) {
590 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
591 /* TODO: adjust lower bound */
592 _ASN_ENCODE_FAILED;
593 }
594
595 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
596 ssize_t mayEncode = uper_put_length(po, end - buf);
597 if(mayEncode < 0)
598 _ASN_ENCODE_FAILED;
599 if(per_put_many_bits(po, buf, 8 * mayEncode))
600 _ASN_ENCODE_FAILED;
601 }
602
603 _ASN_ENCODED_OK(er);
604}
605
Lev Walkinf15320b2004-06-03 03:38:44 +0000606int
Lev Walkin5e033762004-09-29 13:26:15 +0000607asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000608 uint8_t *b, *end;
609 size_t size;
610 long l;
611
612 /* Sanity checking */
613 if(!iptr || !iptr->buf || !lptr) {
614 errno = EINVAL;
615 return -1;
616 }
617
618 /* Cache the begin/end of the buffer */
619 b = iptr->buf; /* Start of the INTEGER buffer */
620 size = iptr->size;
621 end = b + size; /* Where to stop */
622
623 if(size > sizeof(long)) {
624 uint8_t *end1 = end - 1;
625 /*
626 * Slightly more advanced processing,
627 * able to >sizeof(long) bytes,
628 * when the actual value is small
629 * (0x0000000000abcdef would yield a fine 0x00abcdef)
630 */
631 /* Skip out the insignificant leading bytes */
632 for(; b < end1; b++) {
633 switch(*b) {
634 case 0x00: if((b[1] & 0x80) == 0) continue; break;
635 case 0xff: if((b[1] & 0x80) != 0) continue; break;
636 }
637 break;
638 }
639
640 size = end - b;
641 if(size > sizeof(long)) {
642 /* Still cannot fit the long */
643 errno = ERANGE;
644 return -1;
645 }
646 }
647
648 /* Shortcut processing of a corner case */
649 if(end == b) {
650 *lptr = 0;
651 return 0;
652 }
653
654 /* Perform the sign initialization */
655 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
656 if((*b >> 7)) l = -1; else l = 0;
657
658 /* Conversion engine */
659 for(; b < end; b++)
660 l = (l << 8) | *b;
661
662 *lptr = l;
663 return 0;
664}
Lev Walkind703ff42004-10-21 11:21:25 +0000665
666int
667asn_long2INTEGER(INTEGER_t *st, long value) {
668 uint8_t *buf, *bp;
669 uint8_t *p;
670 uint8_t *pstart;
671 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000672 int littleEndian = 1; /* Run-time detection */
673 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000674
675 if(!st) {
676 errno = EINVAL;
677 return -1;
678 }
679
Lev Walkin8471cec2004-10-21 14:02:19 +0000680 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000681 if(!buf) return -1;
682
Lev Walkind7ad5612004-10-26 08:20:46 +0000683 if(*(char *)&littleEndian) {
684 pstart = (uint8_t *)&value + sizeof(value) - 1;
685 pend1 = (uint8_t *)&value;
686 add = -1;
687 } else {
688 pstart = (uint8_t *)&value;
689 pend1 = pstart + sizeof(value) - 1;
690 add = 1;
691 }
692
Lev Walkind703ff42004-10-21 11:21:25 +0000693 /*
694 * If the contents octet consists of more than one octet,
695 * then bits of the first octet and bit 8 of the second octet:
696 * a) shall not all be ones; and
697 * b) shall not all be zero.
698 */
Lev Walkin33700162004-10-26 09:03:31 +0000699 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000700 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000701 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000702 continue;
703 break;
Lev Walkin33700162004-10-26 09:03:31 +0000704 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000705 continue;
706 break;
707 }
708 break;
709 }
710 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000711 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
712 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000713
714 if(st->buf) FREEMEM(st->buf);
715 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000716 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000717
718 return 0;
719}