blob: 57fc8bb75be191d7afb620bd92ff9985330e54c2 [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,
340 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700341 ST_END_ENUM,
342 ST_UNEXPECTED
343 } state = ST_LEADSPACE;
344 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
345 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000346
Lev Walkinc744a022006-09-15 18:33:25 +0000347 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000348 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
349 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000350
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000351 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
352 return XPBD_SYSTEM_FAILURE;
353
Lev Walkind703ff42004-10-21 11:21:25 +0000354 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000355 * We may have received a tag here. It will be processed inline.
356 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000357 */
Lev Walkinb3751942012-09-02 19:36:47 -0700358 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000359 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000360 switch(lv) {
361 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000362 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700363 case ST_LEADSPACE:
364 case ST_DIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000365 case ST_SKIPSPHEX:
366 continue;
367 case ST_HEXCOLON:
368 if(xer_is_whitespace(lp, lstop - lp)) {
369 lp = lstop - 1;
370 continue;
371 }
372 break;
Lev Walkine09f9f12012-09-02 23:06:35 -0700373 case ST_DIGITS:
374 dec_value_end = lp;
375 state = ST_DIGITS_TRAILSPACE;
376 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000377 default:
378 break;
379 }
Lev Walkind703ff42004-10-21 11:21:25 +0000380 break;
381 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700382 if(state == ST_LEADSPACE) {
383 dec_value = 0;
384 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000385 state = ST_WAITDIGITS;
386 continue;
387 }
388 break;
389 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700390 if(state == ST_LEADSPACE) {
391 dec_value = 0;
392 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000393 state = ST_WAITDIGITS;
394 continue;
395 }
396 break;
397 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
398 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000399 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700400 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000401 case ST_SKIPSPHEX: /* Fall through */
402 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700403 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000404 state = ST_HEXDIGIT2;
405 continue;
406 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700407 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000408 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700409 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000410 continue;
411 case ST_HEXCOLON:
412 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700413 case ST_LEADSPACE:
414 dec_value = 0;
415 dec_value_start = lp;
416 /* FALL THROUGH */
417 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000418 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700419 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700420 default:
421 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000422 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700423 break;
424 case 0x3c: /* '<', start of XML encoded enumeration */
425 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000426 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000427 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000428 (asn_INTEGER_specifics_t *)
429 td->specifics, lstart, lstop);
430 if(el) {
431 ASN_DEBUG("Found \"%s\" => %ld",
432 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700433 dec_value = el->nat_value;
434 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000435 lp = lstop - 1;
436 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000437 }
438 ASN_DEBUG("Unknown identifier for INTEGER");
439 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000440 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000441 case 0x3a: /* ':' */
442 if(state == ST_HEXCOLON) {
443 /* This colon is expected */
444 state = ST_HEXDIGIT1;
445 continue;
446 } else if(state == ST_DIGITS) {
447 /* The colon here means that we have
448 * decoded the first two hexadecimal
449 * places as a decimal value.
450 * Switch decoding mode. */
451 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000452 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700453 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000454 lp = lstart - 1;
455 continue;
456 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400457 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000458 break;
459 }
460 /* [A-Fa-f] */
461 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
462 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
463 switch(state) {
464 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700465 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000466 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700467 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
468 hex_value += 10;
469 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000470 state = ST_HEXDIGIT2;
471 continue;
472 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700473 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
474 hex_value += 10;
475 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000476 state = ST_HEXCOLON;
477 continue;
478 case ST_DIGITS:
479 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000480 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700481 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000482 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 Walkine09f9f12012-09-02 23:06:35 -0700491 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400492 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700493 state = ST_UNEXPECTED;
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) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700498 case ST_END_ENUM:
499 /* Got a complete and valid enumeration encoded as a tag. */
500 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000501 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700502 dec_value_end = lstop;
503 case ST_DIGITS_TRAILSPACE:
504 /* The last symbol encountered was a digit. */
505 switch(asn_strtol(dec_value_start, dec_value_end, &dec_value)) {
506 case ASN_STRTOL_OK:
507 break;
508 case ASN_STRTOL_ERROR_RANGE:
509 return XPBD_DECODER_LIMIT;
510 case ASN_STRTOL_ERROR_INVAL:
511 return XPBD_BROKEN_ENCODING;
512 }
Lev Walkinc744a022006-09-15 18:33:25 +0000513 break;
514 case ST_HEXCOLON:
515 st->buf[st->size] = 0; /* Just in case termination */
516 return XPBD_BODY_CONSUMED;
517 case ST_HEXDIGIT1:
518 case ST_HEXDIGIT2:
519 case ST_SKIPSPHEX:
520 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700521 case ST_LEADSPACE:
522 /* Content not found */
523 return XPBD_NOT_BODY_IGNORE;
524 case ST_WAITDIGITS:
525 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700526 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
527 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000528 }
Lev Walkind703ff42004-10-21 11:21:25 +0000529
Lev Walkine09f9f12012-09-02 23:06:35 -0700530 /*
531 * Convert the result of parsing of enumeration or a straight
532 * decimal value into a BER representation.
533 */
534 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000535 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000536
Lev Walkin0fab1a62005-03-09 22:19:25 +0000537 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000538}
539
540asn_dec_rval_t
541INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
542 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000543 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000544
545 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000546 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000547 buf_ptr, size, INTEGER__xer_body_decode);
548}
549
Lev Walkina9cc46e2004-09-22 16:06:28 +0000550asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000551INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000552 int ilevel, enum xer_encoder_flags_e flags,
553 asn_app_consume_bytes_f *cb, void *app_key) {
554 const INTEGER_t *st = (const INTEGER_t *)sptr;
555 asn_enc_rval_t er;
556
557 (void)ilevel;
558 (void)flags;
559
Lev Walkind500a962005-11-27 13:06:56 +0000560 if(!st || !st->buf)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000561 _ASN_ENCODE_FAILED;
562
Lev Walkine0b56e02005-02-25 12:10:27 +0000563 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000564 if(er.encoded < 0) _ASN_ENCODE_FAILED;
565
Lev Walkin59b176e2005-11-26 11:25:14 +0000566 _ASN_ENCODED_OK(er);
567}
568
Lev Walkind7703cf2012-09-03 01:45:03 -0700569#ifndef ASN_DISABLE_PER_SUPPORT
570
Lev Walkin59b176e2005-11-26 11:25:14 +0000571asn_dec_rval_t
572INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
573 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000574 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000575 asn_dec_rval_t rval = { RC_OK, 0 };
576 INTEGER_t *st = (INTEGER_t *)*sptr;
577 asn_per_constraint_t *ct;
578 int repeat;
579
580 (void)opt_codec_ctx;
581
582 if(!st) {
583 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
584 if(!st) _ASN_DECODE_FAILED;
585 }
586
587 if(!constraints) constraints = td->per_constraints;
588 ct = constraints ? &constraints->value : 0;
589
590 if(ct && ct->flags & APC_EXTENSIBLE) {
591 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000592 if(inext < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000593 if(inext) ct = 0;
594 }
595
596 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000597 st->buf = 0;
598 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000599 if(ct) {
600 if(ct->flags & APC_SEMI_CONSTRAINED) {
601 st->buf = (uint8_t *)CALLOC(1, 2);
602 if(!st->buf) _ASN_DECODE_FAILED;
603 st->size = 1;
604 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
605 size_t size = (ct->range_bits + 7) >> 3;
606 st->buf = (uint8_t *)MALLOC(1 + size + 1);
607 if(!st->buf) _ASN_DECODE_FAILED;
608 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000609 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000610 }
611
612 /* X.691, #12.2.2 */
613 if(ct && ct->flags != APC_UNCONSTRAINED) {
614 /* #10.5.6 */
615 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
616 if(ct->range_bits >= 0) {
Lev Walkina105cbc2007-11-06 02:35:13 +0000617 long value;
618 if(ct->range_bits == 32) {
619 long lhalf;
620 value = per_get_few_bits(pd, 16);
621 if(value < 0) _ASN_DECODE_STARVED;
622 lhalf = per_get_few_bits(pd, 16);
623 if(lhalf < 0) _ASN_DECODE_STARVED;
624 value = (value << 16) | lhalf;
625 } else {
626 value = per_get_few_bits(pd, ct->range_bits);
627 if(value < 0) _ASN_DECODE_STARVED;
628 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000629 ASN_DEBUG("Got value %ld + low %ld",
630 value, ct->lower_bound);
631 value += ct->lower_bound;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000632 if((specs && specs->field_unsigned)
633 ? asn_ulong2INTEGER(st, value)
634 : asn_long2INTEGER(st, value))
Lev Walkin59b176e2005-11-26 11:25:14 +0000635 _ASN_DECODE_FAILED;
636 return rval;
637 }
638 } else {
639 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
640 }
641
642 /* X.691, #12.2.3, #12.2.4 */
643 do {
644 ssize_t len;
645 void *p;
646 int ret;
647
648 /* Get the PER length */
649 len = uper_get_length(pd, -1, &repeat);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000650 if(len < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000651
652 p = REALLOC(st->buf, st->size + len + 1);
653 if(!p) _ASN_DECODE_FAILED;
654 st->buf = (uint8_t *)p;
655
656 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000657 if(ret < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000658 st->size += len;
659 } while(repeat);
660 st->buf[st->size] = 0; /* JIC */
661
662 /* #12.2.3 */
663 if(ct && ct->lower_bound) {
664 /*
665 * TODO: replace by in-place arithmetics.
666 */
667 long value;
668 if(asn_INTEGER2long(st, &value))
669 _ASN_DECODE_FAILED;
670 if(asn_long2INTEGER(st, value + ct->lower_bound))
671 _ASN_DECODE_FAILED;
672 }
673
674 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000675}
676
Lev Walkin523de9e2006-08-18 01:34:18 +0000677asn_enc_rval_t
678INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
679 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000680 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000681 asn_enc_rval_t er;
682 INTEGER_t *st = (INTEGER_t *)sptr;
683 const uint8_t *buf;
684 const uint8_t *end;
685 asn_per_constraint_t *ct;
686 long value = 0;
687
688 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
689
690 if(!constraints) constraints = td->per_constraints;
691 ct = constraints ? &constraints->value : 0;
692
693 er.encoded = 0;
694
695 if(ct) {
696 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000697 if(specs && specs->field_unsigned) {
698 unsigned long uval;
699 if(asn_INTEGER2ulong(st, &uval))
700 _ASN_ENCODE_FAILED;
701 /* Check proper range */
702 if(ct->flags & APC_SEMI_CONSTRAINED) {
703 if(uval < (unsigned long)ct->lower_bound)
704 inext = 1;
705 } else if(ct->range_bits >= 0) {
706 if(uval < (unsigned long)ct->lower_bound
707 || uval > (unsigned long)ct->upper_bound)
708 inext = 1;
709 }
710 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
711 uval, st->buf[0], st->size,
712 ct->lower_bound, ct->upper_bound,
713 inext ? "ext" : "fix");
714 value = uval;
715 } else {
716 if(asn_INTEGER2long(st, &value))
717 _ASN_ENCODE_FAILED;
718 /* Check proper range */
719 if(ct->flags & APC_SEMI_CONSTRAINED) {
720 if(value < ct->lower_bound)
721 inext = 1;
722 } else if(ct->range_bits >= 0) {
723 if(value < ct->lower_bound
724 || value > ct->upper_bound)
725 inext = 1;
726 }
727 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
728 value, st->buf[0], st->size,
729 ct->lower_bound, ct->upper_bound,
730 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000731 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000732 if(ct->flags & APC_EXTENSIBLE) {
733 if(per_put_few_bits(po, inext, 1))
734 _ASN_ENCODE_FAILED;
735 if(inext) ct = 0;
736 } else if(inext) {
737 _ASN_ENCODE_FAILED;
738 }
739 }
740
741
742 /* X.691, #12.2.2 */
743 if(ct && ct->range_bits >= 0) {
744 /* #10.5.6 */
745 ASN_DEBUG("Encoding integer with range %d bits",
746 ct->range_bits);
Lev Walkina105cbc2007-11-06 02:35:13 +0000747 if(ct->range_bits == 32) {
748 /* TODO: extend to >32 bits */
749 long v = value - ct->lower_bound;
750 if(per_put_few_bits(po, v >> 1, 31)
751 || per_put_few_bits(po, v, 1))
752 _ASN_ENCODE_FAILED;
753 } else {
754 if(per_put_few_bits(po, value - ct->lower_bound,
Lev Walkin523de9e2006-08-18 01:34:18 +0000755 ct->range_bits))
Lev Walkina105cbc2007-11-06 02:35:13 +0000756 _ASN_ENCODE_FAILED;
757 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000758 _ASN_ENCODED_OK(er);
759 }
760
761 if(ct && ct->lower_bound) {
762 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
763 /* TODO: adjust lower bound */
764 _ASN_ENCODE_FAILED;
765 }
766
767 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
768 ssize_t mayEncode = uper_put_length(po, end - buf);
769 if(mayEncode < 0)
770 _ASN_ENCODE_FAILED;
771 if(per_put_many_bits(po, buf, 8 * mayEncode))
772 _ASN_ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000773 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000774 }
775
776 _ASN_ENCODED_OK(er);
777}
778
Lev Walkind7703cf2012-09-03 01:45:03 -0700779#endif /* ASN_DISABLE_PER_SUPPORT */
780
Lev Walkinf15320b2004-06-03 03:38:44 +0000781int
Lev Walkin5e033762004-09-29 13:26:15 +0000782asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000783 uint8_t *b, *end;
784 size_t size;
785 long l;
786
787 /* Sanity checking */
788 if(!iptr || !iptr->buf || !lptr) {
789 errno = EINVAL;
790 return -1;
791 }
792
793 /* Cache the begin/end of the buffer */
794 b = iptr->buf; /* Start of the INTEGER buffer */
795 size = iptr->size;
796 end = b + size; /* Where to stop */
797
798 if(size > sizeof(long)) {
799 uint8_t *end1 = end - 1;
800 /*
801 * Slightly more advanced processing,
802 * able to >sizeof(long) bytes,
803 * when the actual value is small
804 * (0x0000000000abcdef would yield a fine 0x00abcdef)
805 */
806 /* Skip out the insignificant leading bytes */
807 for(; b < end1; b++) {
808 switch(*b) {
809 case 0x00: if((b[1] & 0x80) == 0) continue; break;
810 case 0xff: if((b[1] & 0x80) != 0) continue; break;
811 }
812 break;
813 }
814
815 size = end - b;
816 if(size > sizeof(long)) {
817 /* Still cannot fit the long */
818 errno = ERANGE;
819 return -1;
820 }
821 }
822
823 /* Shortcut processing of a corner case */
824 if(end == b) {
825 *lptr = 0;
826 return 0;
827 }
828
829 /* Perform the sign initialization */
830 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
831 if((*b >> 7)) l = -1; else l = 0;
832
833 /* Conversion engine */
834 for(; b < end; b++)
835 l = (l << 8) | *b;
836
837 *lptr = l;
838 return 0;
839}
Lev Walkind703ff42004-10-21 11:21:25 +0000840
841int
Lev Walkin5c879db2007-11-06 06:23:31 +0000842asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
843 uint8_t *b, *end;
844 unsigned long l;
845 size_t size;
846
847 if(!iptr || !iptr->buf || !lptr) {
848 errno = EINVAL;
849 return -1;
850 }
851
852 b = iptr->buf;
853 size = iptr->size;
854 end = b + size;
855
856 /* If all extra leading bytes are zeroes, ignore them */
857 for(; size > sizeof(unsigned long); b++, size--) {
858 if(*b) {
859 /* Value won't fit unsigned long */
860 errno = ERANGE;
861 return -1;
862 }
863 }
864
865 /* Conversion engine */
866 for(l = 0; b < end; b++)
867 l = (l << 8) | *b;
868
869 *lptr = l;
870 return 0;
871}
872
873int
874asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
875 uint8_t *buf;
876 uint8_t *end;
877 uint8_t *b;
878 int shr;
879
880 if(value <= LONG_MAX)
881 return asn_long2INTEGER(st, value);
882
883 buf = (uint8_t *)MALLOC(1 + sizeof(value));
884 if(!buf) return -1;
885
886 end = buf + (sizeof(value) + 1);
887 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +0000888 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
889 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000890
891 if(st->buf) FREEMEM(st->buf);
892 st->buf = buf;
893 st->size = 1 + sizeof(value);
894
895 return 0;
896}
897
898int
Lev Walkind703ff42004-10-21 11:21:25 +0000899asn_long2INTEGER(INTEGER_t *st, long value) {
900 uint8_t *buf, *bp;
901 uint8_t *p;
902 uint8_t *pstart;
903 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000904 int littleEndian = 1; /* Run-time detection */
905 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000906
907 if(!st) {
908 errno = EINVAL;
909 return -1;
910 }
911
Lev Walkin8471cec2004-10-21 14:02:19 +0000912 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000913 if(!buf) return -1;
914
Lev Walkind7ad5612004-10-26 08:20:46 +0000915 if(*(char *)&littleEndian) {
916 pstart = (uint8_t *)&value + sizeof(value) - 1;
917 pend1 = (uint8_t *)&value;
918 add = -1;
919 } else {
920 pstart = (uint8_t *)&value;
921 pend1 = pstart + sizeof(value) - 1;
922 add = 1;
923 }
924
Lev Walkind703ff42004-10-21 11:21:25 +0000925 /*
926 * If the contents octet consists of more than one octet,
927 * then bits of the first octet and bit 8 of the second octet:
928 * a) shall not all be ones; and
929 * b) shall not all be zero.
930 */
Lev Walkin33700162004-10-26 09:03:31 +0000931 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000932 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000933 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000934 continue;
935 break;
Lev Walkin33700162004-10-26 09:03:31 +0000936 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000937 continue;
938 break;
939 }
940 break;
941 }
942 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000943 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
944 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000945
946 if(st->buf) FREEMEM(st->buf);
947 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000948 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000949
950 return 0;
951}
Lev Walkinb3751942012-09-02 19:36:47 -0700952
Lev Walkine09f9f12012-09-02 23:06:35 -0700953enum asn_strtol_result_e
954asn_strtol(const char *str, const char *end, long *lp) {
955 int sign = 1;
956 long l;
Lev Walkinb3751942012-09-02 19:36:47 -0700957
Lev Walkine09f9f12012-09-02 23:06:35 -0700958 const long upper_boundary = LONG_MAX / 10;
959 long last_digit_max = LONG_MAX % 10;
960
961 if(str >= end) return ASN_STRTOL_ERROR_INVAL;
962
963 switch(*str) {
964 case '-':
965 last_digit_max++;
966 sign = -1;
967 case '+':
968 str++;
969 }
970
971 if(str >= end) return ASN_STRTOL_ERROR_INVAL;
972
973 for(l = 0; str < end; str++) {
974 switch(*str) {
975 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
976 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
977 int d = *str - '0';
978 if(l < upper_boundary) {
979 l = l * 10 + d;
980 } else if(l == upper_boundary) {
981 if(d <= last_digit_max) {
982 if(sign > 0) {
983 l = l * 10 + d;
984 } else {
985 sign = 1;
986 l = -l * 10 - d;
987 }
988 } else {
989 return ASN_STRTOL_ERROR_RANGE;
990 }
991 } else {
992 return ASN_STRTOL_ERROR_RANGE;
993 }
994 }
995 continue;
996 default:
997 return ASN_STRTOL_ERROR_INVAL;
998 }
999 }
1000
1001 *lp = sign * l;
1002 return ASN_STRTOL_OK;
Lev Walkinb3751942012-09-02 19:36:47 -07001003}
1004