blob: c4fb972101a25d5aecbfa4fa60e0c4229f9e6aa3 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
vlma4799a62004-10-21 11:21:25 +00002 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
vlmfa67ddc2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
vlm39ba4c42004-09-22 16:06:28 +00005#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00006#include <INTEGER.h>
vlma4799a62004-10-21 11:21:25 +00007#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
vlmfa67ddc2004-06-03 03:38:44 +00008#include <assert.h>
9#include <errno.h>
10
11/*
12 * INTEGER basic type description.
13 */
vlmef6355b2004-09-29 13:26:15 +000014static ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
vlmfa67ddc2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
16};
vlmef6355b2004-09-29 13:26:15 +000017asn_TYPE_descriptor_t asn_DEF_INTEGER = {
vlmfa67ddc2004-06-03 03:38:44 +000018 "INTEGER",
vlm9de248e2004-10-20 15:50:55 +000019 "INTEGER",
vlm6678cb12004-09-26 13:10:40 +000020 ASN__PRIMITIVE_TYPE_free,
vlm39ba4c42004-09-22 16:06:28 +000021 INTEGER_print,
vlmfa67ddc2004-06-03 03:38:44 +000022 asn_generic_no_constraint,
vlm6678cb12004-09-26 13:10:40 +000023 ber_decode_primitive,
vlmfa67ddc2004-06-03 03:38:44 +000024 INTEGER_encode_der,
vlma4799a62004-10-21 11:21:25 +000025 INTEGER_decode_xer,
vlm39ba4c42004-09-22 16:06:28 +000026 INTEGER_encode_xer,
vlmfa67ddc2004-06-03 03:38:44 +000027 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-09-29 13:26:15 +000028 asn_DEF_INTEGER_tags,
29 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
30 asn_DEF_INTEGER_tags, /* Same as above */
31 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
vlme413c122004-08-20 13:23:42 +000032 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000033 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000034};
35
36/*
vlmfa67ddc2004-06-03 03:38:44 +000037 * Encode INTEGER type using DER.
38 */
vlm39ba4c42004-09-22 16:06:28 +000039asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +000040INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +000041 int tag_mode, ber_tlv_tag_t tag,
42 asn_app_consume_bytes_f *cb, void *app_key) {
vlm6678cb12004-09-26 13:10:40 +000043 INTEGER_t *st = (INTEGER_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000044
45 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
vlm6678cb12004-09-26 13:10:40 +000046 cb?"Encoding":"Estimating", td->name, tag_mode);
vlmfa67ddc2004-06-03 03:38:44 +000047
48 /*
49 * Canonicalize integer in the buffer.
50 * (Remove too long sign extension, remove some first 0x00 bytes)
51 */
52 if(st->buf) {
53 uint8_t *buf = st->buf;
54 uint8_t *end1 = buf + st->size - 1;
55 int shift;
56
57 /* Compute the number of superfluous leading bytes */
58 for(; buf < end1; buf++) {
59 /*
60 * If the contents octets of an integer value encoding
61 * consist of more than one octet, then the bits of the
62 * first octet and bit 8 of the second octet:
63 * a) shall not all be ones; and
64 * b) shall not all be zero.
65 */
66 switch(*buf) {
67 case 0x00: if((buf[1] & 0x80) == 0)
68 continue;
69 break;
70 case 0xff: if((buf[1] & 0x80))
71 continue;
72 break;
73 }
74 break;
75 }
76
77 /* Remove leading superfluous bytes from the integer */
78 shift = buf - st->buf;
79 if(shift) {
80 uint8_t *nb = st->buf;
81 uint8_t *end;
82
83 st->size -= shift; /* New size, minus bad bytes */
84 end = nb + st->size;
85
86 for(; nb < end; nb++, buf++)
87 *nb = *buf;
88 }
89
90 } /* if(1) */
91
vlm6678cb12004-09-26 13:10:40 +000092 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
vlmfa67ddc2004-06-03 03:38:44 +000093}
94
95/*
96 * INTEGER specific human-readable output.
97 */
vlm39ba4c42004-09-22 16:06:28 +000098static ssize_t
99INTEGER__dump(const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
vlm7d278c42004-07-19 17:30:25 +0000100 char scratch[32]; /* Enough for 64-bit integer */
vlmfa67ddc2004-06-03 03:38:44 +0000101 uint8_t *buf = st->buf;
102 uint8_t *buf_end = st->buf + st->size;
103 signed long accum;
vlm39ba4c42004-09-22 16:06:28 +0000104 ssize_t wrote = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000105 char *p;
106 int ret;
107
vlm39ba4c42004-09-22 16:06:28 +0000108 if(st->size == 0) {
109 return (cb("0", 1, app_key) < 0) ? -1 : 1;
110 }
vlmfa67ddc2004-06-03 03:38:44 +0000111
vlm7d278c42004-07-19 17:30:25 +0000112 /*
113 * Advance buf pointer until the start of the value's body.
114 * This will make us able to process large integers using simple case,
115 * when the actual value is small
116 * (0x0000000000abcdef would yield a fine 0x00abcdef)
117 */
118 /* Skip the insignificant leading bytes */
119 for(; buf < buf_end-1; buf++) {
120 switch(*buf) {
121 case 0x00: if((buf[1] & 0x80) == 0) continue; break;
122 case 0xff: if((buf[1] & 0x80) != 0) continue; break;
123 }
124 break;
125 }
126
vlmfa67ddc2004-06-03 03:38:44 +0000127 /* Simple case: the integer size is small */
vlm7d278c42004-07-19 17:30:25 +0000128 if((size_t)(buf_end - buf) <= sizeof(accum)) {
129 accum = (*buf & 0x80) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000130 for(; buf < buf_end; buf++)
131 accum = (accum << 8) | *buf;
132 ret = snprintf(scratch, sizeof(scratch), "%ld", accum);
vlmb42843a2004-06-05 08:17:50 +0000133 assert(ret > 0 && ret < (int)sizeof(scratch));
vlm39ba4c42004-09-22 16:06:28 +0000134 return (cb(scratch, ret, app_key) < 0) ? -1 : ret;
vlmfa67ddc2004-06-03 03:38:44 +0000135 }
136
137 /* Output in the long xx:yy:zz... format */
vlm7d278c42004-07-19 17:30:25 +0000138 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
vlmfa67ddc2004-06-03 03:38:44 +0000139 for(p = scratch; buf < buf_end; buf++) {
vlm1ff928d2004-08-11 08:10:13 +0000140 static const char *h2c = "0123456789ABCDEF";
vlm7d278c42004-07-19 17:30:25 +0000141 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
vlmfa67ddc2004-06-03 03:38:44 +0000142 /* Flush buffer */
vlm39ba4c42004-09-22 16:06:28 +0000143 if(cb(scratch, p - scratch, app_key) < 0)
vlmfa67ddc2004-06-03 03:38:44 +0000144 return -1;
vlm39ba4c42004-09-22 16:06:28 +0000145 wrote += p - scratch;
vlmfa67ddc2004-06-03 03:38:44 +0000146 p = scratch;
147 }
148 *p++ = h2c[*buf >> 4];
149 *p++ = h2c[*buf & 0x0F];
vlmef6355b2004-09-29 13:26:15 +0000150 *p++ = 0x3a; /* ":" */
vlmfa67ddc2004-06-03 03:38:44 +0000151 }
vlm7d278c42004-07-19 17:30:25 +0000152 if(p != scratch)
vlmef6355b2004-09-29 13:26:15 +0000153 p--; /* Remove the last ":" */
vlmfa67ddc2004-06-03 03:38:44 +0000154
vlm39ba4c42004-09-22 16:06:28 +0000155 wrote += p - scratch;
156 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
157}
158
159/*
160 * INTEGER specific human-readable output.
161 */
162int
vlmef6355b2004-09-29 13:26:15 +0000163INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlm39ba4c42004-09-22 16:06:28 +0000164 asn_app_consume_bytes_f *cb, void *app_key) {
165 const INTEGER_t *st = (const INTEGER_t *)sptr;
vlm6678cb12004-09-26 13:10:40 +0000166 ssize_t ret;
vlm39ba4c42004-09-22 16:06:28 +0000167
168 (void)td;
169 (void)ilevel;
170
vlm6678cb12004-09-26 13:10:40 +0000171 if(!st && !st->buf)
172 ret = cb("<absent>", 8, app_key);
173 else
174 ret = INTEGER__dump(st, cb, app_key);
vlm39ba4c42004-09-22 16:06:28 +0000175
vlm6678cb12004-09-26 13:10:40 +0000176 return (ret < 0) ? -1 : 0;
vlm39ba4c42004-09-22 16:06:28 +0000177}
178
vlma4799a62004-10-21 11:21:25 +0000179/*
180 * Decode the chunk of XML text encoding INTEGER.
181 */
182static ssize_t
183INTEGER__xer_body_decode(INTEGER_t *st, void *chunk_buf, size_t chunk_size) {
184 long sign = 1;
185 long value;
186 char *lstart = (char *)chunk_buf;
187 char *lstop = chunk_buf + chunk_size;
188 enum {
189 ST_SKIPSPACE,
190 ST_WAITDIGITS,
191 ST_DIGITS,
192 } state = ST_SKIPSPACE;
193 /*
194 * We may receive a tag here. But we aren't ready to deal with it yet.
195 * So, just use stroul()-like code and serialize the result.
196 */
197 for(value = 0; lstart < lstop; lstart++) {
198 int lv = *lstart;
199 switch(lv) {
200 case 0x09: case 0x0a: case 0x0d: case 0x20:
201 if(state == ST_SKIPSPACE) continue;
202 break;
203 case 0x2d: /* '-' */
204 if(state == ST_SKIPSPACE) {
205 sign = -1;
206 state = ST_WAITDIGITS;
207 continue;
208 }
209 break;
210 case 0x2b: /* '+' */
211 if(state == ST_SKIPSPACE) {
212 state = ST_WAITDIGITS;
213 continue;
214 }
215 break;
216 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
217 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
218 if(state != ST_DIGITS) state = ST_DIGITS;
219
220 value = value * 10 + (lv - 0x30);
221 /* Check for two's complement overflow */
222 if(value < 0) {
223 /* Check whether it is a LONG_MIN */
224 if(sign == -1
225 && value == ~((unsigned long)-1 >> 1)) {
226 sign = 0;
227 } else {
228 /* Overflow */
229 return -1;
230 }
231 }
232 continue;
233 }
234 }
235
236 if(state != ST_DIGITS)
237 return -1; /* No digits */
238
239 value *= sign; /* Change sign, if needed */
240
241 if(asn_long2INTEGER(st, value))
242 return -1;
243
244 return lstop - lstart;
245}
246
247asn_dec_rval_t
248INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
249 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
250 void *buf_ptr, size_t size) {
251
252 return xer_decode_primitive(opt_codec_ctx, td,
253 (ASN__PRIMITIVE_TYPE_t **)sptr, opt_mname,
254 buf_ptr, size, INTEGER__xer_body_decode);
255}
256
vlm39ba4c42004-09-22 16:06:28 +0000257asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000258INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000259 int ilevel, enum xer_encoder_flags_e flags,
260 asn_app_consume_bytes_f *cb, void *app_key) {
261 const INTEGER_t *st = (const INTEGER_t *)sptr;
262 asn_enc_rval_t er;
263
264 (void)ilevel;
265 (void)flags;
266
267 if(!st && !st->buf)
268 _ASN_ENCODE_FAILED;
269
270 er.encoded = INTEGER__dump(st, cb, app_key);
271 if(er.encoded < 0) _ASN_ENCODE_FAILED;
272
273 return er;
vlmfa67ddc2004-06-03 03:38:44 +0000274}
275
vlmfa67ddc2004-06-03 03:38:44 +0000276int
vlmef6355b2004-09-29 13:26:15 +0000277asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
vlmfa67ddc2004-06-03 03:38:44 +0000278 uint8_t *b, *end;
279 size_t size;
280 long l;
281
282 /* Sanity checking */
283 if(!iptr || !iptr->buf || !lptr) {
284 errno = EINVAL;
285 return -1;
286 }
287
288 /* Cache the begin/end of the buffer */
289 b = iptr->buf; /* Start of the INTEGER buffer */
290 size = iptr->size;
291 end = b + size; /* Where to stop */
292
293 if(size > sizeof(long)) {
294 uint8_t *end1 = end - 1;
295 /*
296 * Slightly more advanced processing,
297 * able to >sizeof(long) bytes,
298 * when the actual value is small
299 * (0x0000000000abcdef would yield a fine 0x00abcdef)
300 */
301 /* Skip out the insignificant leading bytes */
302 for(; b < end1; b++) {
303 switch(*b) {
304 case 0x00: if((b[1] & 0x80) == 0) continue; break;
305 case 0xff: if((b[1] & 0x80) != 0) continue; break;
306 }
307 break;
308 }
309
310 size = end - b;
311 if(size > sizeof(long)) {
312 /* Still cannot fit the long */
313 errno = ERANGE;
314 return -1;
315 }
316 }
317
318 /* Shortcut processing of a corner case */
319 if(end == b) {
320 *lptr = 0;
321 return 0;
322 }
323
324 /* Perform the sign initialization */
325 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
326 if((*b >> 7)) l = -1; else l = 0;
327
328 /* Conversion engine */
329 for(; b < end; b++)
330 l = (l << 8) | *b;
331
332 *lptr = l;
333 return 0;
334}
vlma4799a62004-10-21 11:21:25 +0000335
336int
337asn_long2INTEGER(INTEGER_t *st, long value) {
338 uint8_t *buf, *bp;
339 uint8_t *p;
340 uint8_t *pstart;
341 uint8_t *pend1;
342
343 if(!st) {
344 errno = EINVAL;
345 return -1;
346 }
347
348 buf = MALLOC(sizeof(value));
349 if(!buf) return -1;
350
351 pstart = (uint8_t *)&value;
352 pend1 = pstart + sizeof(value) - 1;
353 /*
354 * If the contents octet consists of more than one octet,
355 * then bits of the first octet and bit 8 of the second octet:
356 * a) shall not all be ones; and
357 * b) shall not all be zero.
358 */
359 for(p = pstart; p < pend1; p++) {
360 switch(*p) {
361 case 0x00: if((p[1] & 0x80) == 0)
362 continue;
363 break;
364 case 0xff: if((p[1] & 0x80))
365 continue;
366 break;
367 }
368 break;
369 }
370 /* Copy the integer body */
371 for(pstart = p, bp = buf; p <= pend1;)
372 *bp++ = *p++;
373
374 if(st->buf) FREEMEM(st->buf);
375 st->buf = buf;
376 st->size = p - pstart;
377
378 return 0;
379}