blob: 56d02b49ecf2acb7559f027d1b2ab1b6809da878 [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 Walkind7703cf2012-09-03 01:45:03 -070027#ifdef ASN_DISABLE_PER_SUPPORT
28 0,
29 0,
30#else
Lev Walkin59b176e2005-11-26 11:25:14 +000031 INTEGER_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000032 INTEGER_encode_uper, /* Unaligned PER encoder */
Lev Walkind7703cf2012-09-03 01:45:03 -070033#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000034 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000035 asn_DEF_INTEGER_tags,
36 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
37 asn_DEF_INTEGER_tags, /* Same as above */
38 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000039 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000040 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000041 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000042};
43
44/*
Lev Walkinf15320b2004-06-03 03:38:44 +000045 * Encode INTEGER type using DER.
46 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000047asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000048INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000049 int tag_mode, ber_tlv_tag_t tag,
50 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000051 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000052
53 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000054 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000055
56 /*
57 * Canonicalize integer in the buffer.
58 * (Remove too long sign extension, remove some first 0x00 bytes)
59 */
60 if(st->buf) {
61 uint8_t *buf = st->buf;
62 uint8_t *end1 = buf + st->size - 1;
63 int shift;
64
65 /* Compute the number of superfluous leading bytes */
66 for(; buf < end1; buf++) {
67 /*
68 * If the contents octets of an integer value encoding
69 * consist of more than one octet, then the bits of the
70 * first octet and bit 8 of the second octet:
71 * a) shall not all be ones; and
72 * b) shall not all be zero.
73 */
74 switch(*buf) {
75 case 0x00: if((buf[1] & 0x80) == 0)
76 continue;
77 break;
78 case 0xff: if((buf[1] & 0x80))
79 continue;
80 break;
81 }
82 break;
83 }
84
85 /* Remove leading superfluous bytes from the integer */
86 shift = buf - st->buf;
87 if(shift) {
88 uint8_t *nb = st->buf;
89 uint8_t *end;
90
91 st->size -= shift; /* New size, minus bad bytes */
92 end = nb + st->size;
93
94 for(; nb < end; nb++, buf++)
95 *nb = *buf;
96 }
97
98 } /* if(1) */
99
Lev Walkin8e8078a2004-09-26 13:10:40 +0000100 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000101}
102
Lev Walkinc2350112005-03-29 17:19:53 +0000103static 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 +0000104
Lev Walkinf15320b2004-06-03 03:38:44 +0000105/*
106 * INTEGER specific human-readable output.
107 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000108static ssize_t
Lev Walkine0b56e02005-02-25 12:10:27 +0000109INTEGER__dump(asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
110 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000111 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000112 uint8_t *buf = st->buf;
113 uint8_t *buf_end = st->buf + st->size;
114 signed long accum;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000115 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 char *p;
117 int ret;
118
Lev Walkindb13f512004-07-19 17:30:25 +0000119 /*
120 * Advance buf pointer until the start of the value's body.
121 * This will make us able to process large integers using simple case,
122 * when the actual value is small
123 * (0x0000000000abcdef would yield a fine 0x00abcdef)
124 */
125 /* Skip the insignificant leading bytes */
126 for(; buf < buf_end-1; buf++) {
127 switch(*buf) {
128 case 0x00: if((buf[1] & 0x80) == 0) continue; break;
129 case 0xff: if((buf[1] & 0x80) != 0) continue; break;
130 }
131 break;
132 }
133
Lev Walkinf15320b2004-06-03 03:38:44 +0000134 /* Simple case: the integer size is small */
Lev Walkindb13f512004-07-19 17:30:25 +0000135 if((size_t)(buf_end - buf) <= sizeof(accum)) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000136 const asn_INTEGER_enum_map_t *el;
137 size_t scrsize;
138 char *scr;
139
140 if(buf == buf_end) {
141 accum = 0;
142 } else {
143 accum = (*buf & 0x80) ? -1 : 0;
144 for(; buf < buf_end; buf++)
145 accum = (accum << 8) | *buf;
146 }
147
Lev Walkinc2350112005-03-29 17:19:53 +0000148 el = INTEGER_map_value2enum(specs, accum);
Lev Walkine0b56e02005-02-25 12:10:27 +0000149 if(el) {
150 scrsize = el->enum_len + 32;
151 scr = (char *)alloca(scrsize);
152 if(plainOrXER == 0)
153 ret = snprintf(scr, scrsize,
154 "%ld (%s)", accum, el->enum_name);
155 else
156 ret = snprintf(scr, scrsize,
157 "<%s/>", el->enum_name);
158 } else if(plainOrXER && specs && specs->strict_enumeration) {
159 ASN_DEBUG("ASN.1 forbids dealing with "
160 "unknown value of ENUMERATED type");
161 errno = EPERM;
162 return -1;
163 } else {
164 scrsize = sizeof(scratch);
165 scr = scratch;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000166 ret = snprintf(scr, scrsize,
167 (specs && specs->field_unsigned)
168 ?"%lu":"%ld", accum);
Lev Walkine0b56e02005-02-25 12:10:27 +0000169 }
170 assert(ret > 0 && (size_t)ret < scrsize);
171 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
172 } else if(plainOrXER && specs && specs->strict_enumeration) {
173 /*
174 * Here and earlier, we cannot encode the ENUMERATED values
175 * if there is no corresponding identifier.
176 */
177 ASN_DEBUG("ASN.1 forbids dealing with "
178 "unknown value of ENUMERATED type");
179 errno = EPERM;
180 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 }
182
183 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000184 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 for(p = scratch; buf < buf_end; buf++) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000186 static const char *h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000187 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000189 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000191 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 p = scratch;
193 }
194 *p++ = h2c[*buf >> 4];
195 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000196 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 }
Lev Walkindb13f512004-07-19 17:30:25 +0000198 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000199 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000200
Lev Walkina9cc46e2004-09-22 16:06:28 +0000201 wrote += p - scratch;
202 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
203}
204
205/*
206 * INTEGER specific human-readable output.
207 */
208int
Lev Walkin5e033762004-09-29 13:26:15 +0000209INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000210 asn_app_consume_bytes_f *cb, void *app_key) {
211 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000212 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000213
214 (void)td;
215 (void)ilevel;
216
Lev Walkind500a962005-11-27 13:06:56 +0000217 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000218 ret = cb("<absent>", 8, app_key);
219 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000220 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000221
Lev Walkin8e8078a2004-09-26 13:10:40 +0000222 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000223}
224
Lev Walkine0b56e02005-02-25 12:10:27 +0000225struct e2v_key {
226 const char *start;
227 const char *stop;
228 asn_INTEGER_enum_map_t *vemap;
229 unsigned int *evmap;
230};
231static int
232INTEGER__compar_enum2value(const void *kp, const void *am) {
233 const struct e2v_key *key = (const struct e2v_key *)kp;
234 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
235 const char *ptr, *end, *name;
236
237 /* Remap the element (sort by different criterion) */
238 el = key->vemap + key->evmap[el - key->vemap];
239
240 /* Compare strings */
241 for(ptr = key->start, end = key->stop, name = el->enum_name;
242 ptr < end; ptr++, name++) {
243 if(*ptr != *name)
244 return *(const unsigned char *)ptr
245 - *(const unsigned char *)name;
246 }
247 return name[0] ? -1 : 0;
248}
249
250static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000251INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Lev Walkin9332b652005-03-04 11:18:44 +0000252 asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000253 int count = specs ? specs->map_count : 0;
254 struct e2v_key key;
255 const char *lp;
256
257 if(!count) return NULL;
258
259 /* Guaranteed: assert(lstart < lstop); */
260 /* Figure out the tag name */
261 for(lstart++, lp = lstart; lp < lstop; lp++) {
262 switch(*lp) {
263 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
264 case 0x2f: /* '/' */ case 0x3e: /* '>' */
265 break;
266 default:
267 continue;
268 }
269 break;
270 }
271 if(lp == lstop) return NULL; /* No tag found */
272 lstop = lp;
273
274 key.start = lstart;
275 key.stop = lstop;
276 key.vemap = specs->value2enum;
277 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000278 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
279 specs->value2enum, count, sizeof(specs->value2enum[0]),
280 INTEGER__compar_enum2value);
281 if(el_found) {
282 /* Remap enum2value into value2enum */
283 el_found = key.vemap + key.evmap[el_found - key.vemap];
284 }
285 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000286}
287
288static int
289INTEGER__compar_value2enum(const void *kp, const void *am) {
290 long a = *(const long *)kp;
291 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
292 long b = el->nat_value;
293 if(a < b) return -1;
294 else if(a == b) return 0;
295 else return 1;
296}
297
Lev Walkinc2350112005-03-29 17:19:53 +0000298const asn_INTEGER_enum_map_t *
299INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000300 int count = specs ? specs->map_count : 0;
301 if(!count) return 0;
302 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
303 count, sizeof(specs->value2enum[0]),
304 INTEGER__compar_value2enum);
305}
306
Lev Walkinc744a022006-09-15 18:33:25 +0000307static int
308INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
309 void *p = MALLOC(min_size + 1);
310 if(p) {
311 void *b = st->buf;
312 st->size = 0;
313 st->buf = p;
314 FREEMEM(b);
315 return 0;
316 } else {
317 return -1;
318 }
319}
320
Lev Walkind703ff42004-10-21 11:21:25 +0000321/*
322 * Decode the chunk of XML text encoding INTEGER.
323 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000324static enum xer_pbd_rval
325INTEGER__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 +0000326 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinb3751942012-09-02 19:36:47 -0700327 long dec_value;
Lev Walkine09f9f12012-09-02 23:06:35 -0700328 long hex_value;
Lev Walkine0b56e02005-02-25 12:10:27 +0000329 const char *lp;
330 const char *lstart = (const char *)chunk_buf;
331 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000332 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700333 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000334 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000335 ST_WAITDIGITS,
336 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700337 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000338 ST_HEXDIGIT1,
339 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700340 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000341 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700342 ST_END_ENUM,
343 ST_UNEXPECTED
344 } state = ST_LEADSPACE;
345 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
346 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000347
Lev Walkinc744a022006-09-15 18:33:25 +0000348 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000349 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
350 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000351
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000352 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
353 return XPBD_SYSTEM_FAILURE;
354
Lev Walkind703ff42004-10-21 11:21:25 +0000355 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000356 * We may have received a tag here. It will be processed inline.
357 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000358 */
Lev Walkinb3751942012-09-02 19:36:47 -0700359 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000360 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000361 switch(lv) {
362 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000363 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700364 case ST_LEADSPACE:
365 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700366 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000367 case ST_SKIPSPHEX:
368 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700369 case ST_DIGITS:
370 dec_value_end = lp;
371 state = ST_DIGITS_TRAILSPACE;
372 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700373 case ST_HEXCOLON:
374 state = ST_HEXDIGITS_TRAILSPACE;
375 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000376 default:
377 break;
378 }
Lev Walkind703ff42004-10-21 11:21:25 +0000379 break;
380 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700381 if(state == ST_LEADSPACE) {
382 dec_value = 0;
383 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000384 state = ST_WAITDIGITS;
385 continue;
386 }
387 break;
388 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700389 if(state == ST_LEADSPACE) {
390 dec_value = 0;
391 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000392 state = ST_WAITDIGITS;
393 continue;
394 }
395 break;
396 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
397 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000398 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700399 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000400 case ST_SKIPSPHEX: /* Fall through */
401 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700402 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000403 state = ST_HEXDIGIT2;
404 continue;
405 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700406 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000407 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700408 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000409 continue;
410 case ST_HEXCOLON:
411 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700412 case ST_LEADSPACE:
413 dec_value = 0;
414 dec_value_start = lp;
415 /* FALL THROUGH */
416 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000417 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700418 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700419 default:
420 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000421 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700422 break;
423 case 0x3c: /* '<', start of XML encoded enumeration */
424 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000425 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000426 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000427 (asn_INTEGER_specifics_t *)
428 td->specifics, lstart, lstop);
429 if(el) {
430 ASN_DEBUG("Found \"%s\" => %ld",
431 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700432 dec_value = el->nat_value;
433 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000434 lp = lstop - 1;
435 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000436 }
437 ASN_DEBUG("Unknown identifier for INTEGER");
438 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000439 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000440 case 0x3a: /* ':' */
441 if(state == ST_HEXCOLON) {
442 /* This colon is expected */
443 state = ST_HEXDIGIT1;
444 continue;
445 } else if(state == ST_DIGITS) {
446 /* The colon here means that we have
447 * decoded the first two hexadecimal
448 * places as a decimal value.
449 * Switch decoding mode. */
450 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000451 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700452 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000453 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:
Lev Walkine09f9f12012-09-02 23:06:35 -0700464 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000465 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700466 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
467 hex_value += 10;
468 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000469 state = ST_HEXDIGIT2;
470 continue;
471 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700472 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
473 hex_value += 10;
474 st->buf[st->size++] = (uint8_t)hex_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");
Lev Walkinc744a022006-09-15 18:33:25 +0000479 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700480 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000481 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 Walkine09f9f12012-09-02 23:06:35 -0700490 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400491 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700492 state = ST_UNEXPECTED;
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) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700497 case ST_END_ENUM:
498 /* Got a complete and valid enumeration encoded as a tag. */
499 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000500 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700501 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700502 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700503 case ST_DIGITS_TRAILSPACE:
504 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700505 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700506 case ASN_STRTOL_OK:
507 break;
508 case ASN_STRTOL_ERROR_RANGE:
509 return XPBD_DECODER_LIMIT;
510 case ASN_STRTOL_ERROR_INVAL:
Lev Walkincad560a2013-03-16 07:00:58 -0700511 case ASN_STRTOL_EXPECT_MORE:
512 case ASN_STRTOL_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700513 return XPBD_BROKEN_ENCODING;
514 }
Lev Walkinc744a022006-09-15 18:33:25 +0000515 break;
516 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700517 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000518 st->buf[st->size] = 0; /* Just in case termination */
519 return XPBD_BODY_CONSUMED;
520 case ST_HEXDIGIT1:
521 case ST_HEXDIGIT2:
522 case ST_SKIPSPHEX:
523 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700524 case ST_LEADSPACE:
525 /* Content not found */
526 return XPBD_NOT_BODY_IGNORE;
527 case ST_WAITDIGITS:
528 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700529 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
530 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000531 }
Lev Walkind703ff42004-10-21 11:21:25 +0000532
Lev Walkine09f9f12012-09-02 23:06:35 -0700533 /*
534 * Convert the result of parsing of enumeration or a straight
535 * decimal value into a BER representation.
536 */
537 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000538 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000539
Lev Walkin0fab1a62005-03-09 22:19:25 +0000540 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000541}
542
543asn_dec_rval_t
544INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
545 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000546 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000547
548 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000549 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000550 buf_ptr, size, INTEGER__xer_body_decode);
551}
552
Lev Walkina9cc46e2004-09-22 16:06:28 +0000553asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000554INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000555 int ilevel, enum xer_encoder_flags_e flags,
556 asn_app_consume_bytes_f *cb, void *app_key) {
557 const INTEGER_t *st = (const INTEGER_t *)sptr;
558 asn_enc_rval_t er;
559
560 (void)ilevel;
561 (void)flags;
562
Lev Walkind500a962005-11-27 13:06:56 +0000563 if(!st || !st->buf)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000564 _ASN_ENCODE_FAILED;
565
Lev Walkine0b56e02005-02-25 12:10:27 +0000566 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000567 if(er.encoded < 0) _ASN_ENCODE_FAILED;
568
Lev Walkin59b176e2005-11-26 11:25:14 +0000569 _ASN_ENCODED_OK(er);
570}
571
Lev Walkind7703cf2012-09-03 01:45:03 -0700572#ifndef ASN_DISABLE_PER_SUPPORT
573
Lev Walkin59b176e2005-11-26 11:25:14 +0000574asn_dec_rval_t
575INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
576 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000577 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000578 asn_dec_rval_t rval = { RC_OK, 0 };
579 INTEGER_t *st = (INTEGER_t *)*sptr;
580 asn_per_constraint_t *ct;
581 int repeat;
582
583 (void)opt_codec_ctx;
584
585 if(!st) {
586 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
587 if(!st) _ASN_DECODE_FAILED;
588 }
589
590 if(!constraints) constraints = td->per_constraints;
591 ct = constraints ? &constraints->value : 0;
592
593 if(ct && ct->flags & APC_EXTENSIBLE) {
594 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000595 if(inext < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000596 if(inext) ct = 0;
597 }
598
599 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000600 st->buf = 0;
601 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000602 if(ct) {
603 if(ct->flags & APC_SEMI_CONSTRAINED) {
604 st->buf = (uint8_t *)CALLOC(1, 2);
605 if(!st->buf) _ASN_DECODE_FAILED;
606 st->size = 1;
607 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
608 size_t size = (ct->range_bits + 7) >> 3;
609 st->buf = (uint8_t *)MALLOC(1 + size + 1);
610 if(!st->buf) _ASN_DECODE_FAILED;
611 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000612 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000613 }
614
615 /* X.691, #12.2.2 */
616 if(ct && ct->flags != APC_UNCONSTRAINED) {
617 /* #10.5.6 */
618 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
619 if(ct->range_bits >= 0) {
Lev Walkina105cbc2007-11-06 02:35:13 +0000620 long value;
621 if(ct->range_bits == 32) {
622 long lhalf;
623 value = per_get_few_bits(pd, 16);
624 if(value < 0) _ASN_DECODE_STARVED;
625 lhalf = per_get_few_bits(pd, 16);
626 if(lhalf < 0) _ASN_DECODE_STARVED;
627 value = (value << 16) | lhalf;
628 } else {
629 value = per_get_few_bits(pd, ct->range_bits);
630 if(value < 0) _ASN_DECODE_STARVED;
631 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000632 ASN_DEBUG("Got value %ld + low %ld",
633 value, ct->lower_bound);
634 value += ct->lower_bound;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000635 if((specs && specs->field_unsigned)
636 ? asn_ulong2INTEGER(st, value)
637 : asn_long2INTEGER(st, value))
Lev Walkin59b176e2005-11-26 11:25:14 +0000638 _ASN_DECODE_FAILED;
639 return rval;
640 }
641 } else {
642 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
643 }
644
645 /* X.691, #12.2.3, #12.2.4 */
646 do {
647 ssize_t len;
648 void *p;
649 int ret;
650
651 /* Get the PER length */
652 len = uper_get_length(pd, -1, &repeat);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000653 if(len < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000654
655 p = REALLOC(st->buf, st->size + len + 1);
656 if(!p) _ASN_DECODE_FAILED;
657 st->buf = (uint8_t *)p;
658
659 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000660 if(ret < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000661 st->size += len;
662 } while(repeat);
663 st->buf[st->size] = 0; /* JIC */
664
665 /* #12.2.3 */
666 if(ct && ct->lower_bound) {
667 /*
668 * TODO: replace by in-place arithmetics.
669 */
670 long value;
671 if(asn_INTEGER2long(st, &value))
672 _ASN_DECODE_FAILED;
673 if(asn_long2INTEGER(st, value + ct->lower_bound))
674 _ASN_DECODE_FAILED;
675 }
676
677 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000678}
679
Lev Walkin523de9e2006-08-18 01:34:18 +0000680asn_enc_rval_t
681INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
682 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000683 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000684 asn_enc_rval_t er;
685 INTEGER_t *st = (INTEGER_t *)sptr;
686 const uint8_t *buf;
687 const uint8_t *end;
688 asn_per_constraint_t *ct;
689 long value = 0;
690
691 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
692
693 if(!constraints) constraints = td->per_constraints;
694 ct = constraints ? &constraints->value : 0;
695
696 er.encoded = 0;
697
698 if(ct) {
699 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000700 if(specs && specs->field_unsigned) {
701 unsigned long uval;
702 if(asn_INTEGER2ulong(st, &uval))
703 _ASN_ENCODE_FAILED;
704 /* Check proper range */
705 if(ct->flags & APC_SEMI_CONSTRAINED) {
706 if(uval < (unsigned long)ct->lower_bound)
707 inext = 1;
708 } else if(ct->range_bits >= 0) {
709 if(uval < (unsigned long)ct->lower_bound
710 || uval > (unsigned long)ct->upper_bound)
711 inext = 1;
712 }
713 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
714 uval, st->buf[0], st->size,
715 ct->lower_bound, ct->upper_bound,
716 inext ? "ext" : "fix");
717 value = uval;
718 } else {
719 if(asn_INTEGER2long(st, &value))
720 _ASN_ENCODE_FAILED;
721 /* Check proper range */
722 if(ct->flags & APC_SEMI_CONSTRAINED) {
723 if(value < ct->lower_bound)
724 inext = 1;
725 } else if(ct->range_bits >= 0) {
726 if(value < ct->lower_bound
727 || value > ct->upper_bound)
728 inext = 1;
729 }
730 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
731 value, st->buf[0], st->size,
732 ct->lower_bound, ct->upper_bound,
733 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000734 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000735 if(ct->flags & APC_EXTENSIBLE) {
736 if(per_put_few_bits(po, inext, 1))
737 _ASN_ENCODE_FAILED;
738 if(inext) ct = 0;
739 } else if(inext) {
740 _ASN_ENCODE_FAILED;
741 }
742 }
743
744
745 /* X.691, #12.2.2 */
746 if(ct && ct->range_bits >= 0) {
747 /* #10.5.6 */
748 ASN_DEBUG("Encoding integer with range %d bits",
749 ct->range_bits);
Lev Walkina105cbc2007-11-06 02:35:13 +0000750 if(ct->range_bits == 32) {
751 /* TODO: extend to >32 bits */
752 long v = value - ct->lower_bound;
753 if(per_put_few_bits(po, v >> 1, 31)
754 || per_put_few_bits(po, v, 1))
755 _ASN_ENCODE_FAILED;
756 } else {
757 if(per_put_few_bits(po, value - ct->lower_bound,
Lev Walkin523de9e2006-08-18 01:34:18 +0000758 ct->range_bits))
Lev Walkina105cbc2007-11-06 02:35:13 +0000759 _ASN_ENCODE_FAILED;
760 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000761 _ASN_ENCODED_OK(er);
762 }
763
764 if(ct && ct->lower_bound) {
765 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
766 /* TODO: adjust lower bound */
767 _ASN_ENCODE_FAILED;
768 }
769
770 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
771 ssize_t mayEncode = uper_put_length(po, end - buf);
772 if(mayEncode < 0)
773 _ASN_ENCODE_FAILED;
774 if(per_put_many_bits(po, buf, 8 * mayEncode))
775 _ASN_ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000776 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000777 }
778
779 _ASN_ENCODED_OK(er);
780}
781
Lev Walkind7703cf2012-09-03 01:45:03 -0700782#endif /* ASN_DISABLE_PER_SUPPORT */
783
Lev Walkinf15320b2004-06-03 03:38:44 +0000784int
Lev Walkin5e033762004-09-29 13:26:15 +0000785asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000786 uint8_t *b, *end;
787 size_t size;
788 long l;
789
790 /* Sanity checking */
791 if(!iptr || !iptr->buf || !lptr) {
792 errno = EINVAL;
793 return -1;
794 }
795
796 /* Cache the begin/end of the buffer */
797 b = iptr->buf; /* Start of the INTEGER buffer */
798 size = iptr->size;
799 end = b + size; /* Where to stop */
800
801 if(size > sizeof(long)) {
802 uint8_t *end1 = end - 1;
803 /*
804 * Slightly more advanced processing,
805 * able to >sizeof(long) bytes,
806 * when the actual value is small
807 * (0x0000000000abcdef would yield a fine 0x00abcdef)
808 */
809 /* Skip out the insignificant leading bytes */
810 for(; b < end1; b++) {
811 switch(*b) {
812 case 0x00: if((b[1] & 0x80) == 0) continue; break;
813 case 0xff: if((b[1] & 0x80) != 0) continue; break;
814 }
815 break;
816 }
817
818 size = end - b;
819 if(size > sizeof(long)) {
820 /* Still cannot fit the long */
821 errno = ERANGE;
822 return -1;
823 }
824 }
825
826 /* Shortcut processing of a corner case */
827 if(end == b) {
828 *lptr = 0;
829 return 0;
830 }
831
832 /* Perform the sign initialization */
833 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
834 if((*b >> 7)) l = -1; else l = 0;
835
836 /* Conversion engine */
837 for(; b < end; b++)
838 l = (l << 8) | *b;
839
840 *lptr = l;
841 return 0;
842}
Lev Walkind703ff42004-10-21 11:21:25 +0000843
844int
Lev Walkin5c879db2007-11-06 06:23:31 +0000845asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
846 uint8_t *b, *end;
847 unsigned long l;
848 size_t size;
849
850 if(!iptr || !iptr->buf || !lptr) {
851 errno = EINVAL;
852 return -1;
853 }
854
855 b = iptr->buf;
856 size = iptr->size;
857 end = b + size;
858
859 /* If all extra leading bytes are zeroes, ignore them */
860 for(; size > sizeof(unsigned long); b++, size--) {
861 if(*b) {
862 /* Value won't fit unsigned long */
863 errno = ERANGE;
864 return -1;
865 }
866 }
867
868 /* Conversion engine */
869 for(l = 0; b < end; b++)
870 l = (l << 8) | *b;
871
872 *lptr = l;
873 return 0;
874}
875
876int
877asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
878 uint8_t *buf;
879 uint8_t *end;
880 uint8_t *b;
881 int shr;
882
883 if(value <= LONG_MAX)
884 return asn_long2INTEGER(st, value);
885
886 buf = (uint8_t *)MALLOC(1 + sizeof(value));
887 if(!buf) return -1;
888
889 end = buf + (sizeof(value) + 1);
890 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +0000891 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
892 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000893
894 if(st->buf) FREEMEM(st->buf);
895 st->buf = buf;
896 st->size = 1 + sizeof(value);
897
898 return 0;
899}
900
901int
Lev Walkind703ff42004-10-21 11:21:25 +0000902asn_long2INTEGER(INTEGER_t *st, long value) {
903 uint8_t *buf, *bp;
904 uint8_t *p;
905 uint8_t *pstart;
906 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000907 int littleEndian = 1; /* Run-time detection */
908 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000909
910 if(!st) {
911 errno = EINVAL;
912 return -1;
913 }
914
Lev Walkin8471cec2004-10-21 14:02:19 +0000915 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000916 if(!buf) return -1;
917
Lev Walkind7ad5612004-10-26 08:20:46 +0000918 if(*(char *)&littleEndian) {
919 pstart = (uint8_t *)&value + sizeof(value) - 1;
920 pend1 = (uint8_t *)&value;
921 add = -1;
922 } else {
923 pstart = (uint8_t *)&value;
924 pend1 = pstart + sizeof(value) - 1;
925 add = 1;
926 }
927
Lev Walkind703ff42004-10-21 11:21:25 +0000928 /*
929 * If the contents octet consists of more than one octet,
930 * then bits of the first octet and bit 8 of the second octet:
931 * a) shall not all be ones; and
932 * b) shall not all be zero.
933 */
Lev Walkin33700162004-10-26 09:03:31 +0000934 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000935 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000936 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000937 continue;
938 break;
Lev Walkin33700162004-10-26 09:03:31 +0000939 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000940 continue;
941 break;
942 }
943 break;
944 }
945 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000946 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
947 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000948
949 if(st->buf) FREEMEM(st->buf);
950 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000951 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000952
953 return 0;
954}
Lev Walkinb3751942012-09-02 19:36:47 -0700955
Lev Walkincad560a2013-03-16 07:00:58 -0700956/*
957 * This function is going to be DEPRECATED soon.
958 */
Lev Walkine09f9f12012-09-02 23:06:35 -0700959enum asn_strtol_result_e
960asn_strtol(const char *str, const char *end, long *lp) {
Lev Walkincad560a2013-03-16 07:00:58 -0700961 const char *endp = end;
962
963 switch(asn_strtol_lim(str, &endp, lp)) {
964 case ASN_STRTOL_ERROR_RANGE:
965 return ASN_STRTOL_ERROR_RANGE;
966 case ASN_STRTOL_ERROR_INVAL:
967 return ASN_STRTOL_ERROR_INVAL;
968 case ASN_STRTOL_EXPECT_MORE:
969 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
970 case ASN_STRTOL_OK:
971 return ASN_STRTOL_OK;
972 case ASN_STRTOL_EXTRA_DATA:
973 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
974 }
975
976 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
977}
978
979/*
980 * Parse the number in the given string until the given *end position,
981 * returning the position after the last parsed character back using the
982 * same (*end) pointer.
983 * WARNING: This behavior is different from the standard strtol(3).
984 */
985enum asn_strtol_result_e
986asn_strtol_lim(const char *str, const char **end, long *lp) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700987 int sign = 1;
988 long l;
Lev Walkinb3751942012-09-02 19:36:47 -0700989
Lev Walkine09f9f12012-09-02 23:06:35 -0700990 const long upper_boundary = LONG_MAX / 10;
991 long last_digit_max = LONG_MAX % 10;
992
Lev Walkincad560a2013-03-16 07:00:58 -0700993 if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -0700994
995 switch(*str) {
996 case '-':
997 last_digit_max++;
998 sign = -1;
999 case '+':
1000 str++;
Lev Walkincad560a2013-03-16 07:00:58 -07001001 if(str >= *end) {
1002 *end = str;
1003 return ASN_STRTOL_EXPECT_MORE;
1004 }
Lev Walkine09f9f12012-09-02 23:06:35 -07001005 }
1006
Lev Walkincad560a2013-03-16 07:00:58 -07001007 for(l = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -07001008 switch(*str) {
1009 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
1010 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
1011 int d = *str - '0';
1012 if(l < upper_boundary) {
1013 l = l * 10 + d;
1014 } else if(l == upper_boundary) {
1015 if(d <= last_digit_max) {
1016 if(sign > 0) {
1017 l = l * 10 + d;
1018 } else {
1019 sign = 1;
1020 l = -l * 10 - d;
1021 }
1022 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001023 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001024 return ASN_STRTOL_ERROR_RANGE;
1025 }
1026 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001027 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001028 return ASN_STRTOL_ERROR_RANGE;
1029 }
1030 }
1031 continue;
1032 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001033 *end = str;
1034 *lp = sign * l;
1035 return ASN_STRTOL_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001036 }
1037 }
1038
Lev Walkincad560a2013-03-16 07:00:58 -07001039 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001040 *lp = sign * l;
1041 return ASN_STRTOL_OK;
Lev Walkinb3751942012-09-02 19:36:47 -07001042}
1043