blob: 05eae8b057d7b207c4f35492d7c47f3896ba6a9d [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;
521 default:
Lev Walkine09f9f12012-09-02 23:06:35 -0700522 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
523 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000524 }
Lev Walkind703ff42004-10-21 11:21:25 +0000525
Lev Walkine09f9f12012-09-02 23:06:35 -0700526 /*
527 * Convert the result of parsing of enumeration or a straight
528 * decimal value into a BER representation.
529 */
530 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000531 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000532
Lev Walkin0fab1a62005-03-09 22:19:25 +0000533 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000534}
535
536asn_dec_rval_t
537INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
538 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000539 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000540
541 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000542 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000543 buf_ptr, size, INTEGER__xer_body_decode);
544}
545
Lev Walkina9cc46e2004-09-22 16:06:28 +0000546asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000547INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000548 int ilevel, enum xer_encoder_flags_e flags,
549 asn_app_consume_bytes_f *cb, void *app_key) {
550 const INTEGER_t *st = (const INTEGER_t *)sptr;
551 asn_enc_rval_t er;
552
553 (void)ilevel;
554 (void)flags;
555
Lev Walkind500a962005-11-27 13:06:56 +0000556 if(!st || !st->buf)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000557 _ASN_ENCODE_FAILED;
558
Lev Walkine0b56e02005-02-25 12:10:27 +0000559 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000560 if(er.encoded < 0) _ASN_ENCODE_FAILED;
561
Lev Walkin59b176e2005-11-26 11:25:14 +0000562 _ASN_ENCODED_OK(er);
563}
564
Lev Walkind7703cf2012-09-03 01:45:03 -0700565#ifndef ASN_DISABLE_PER_SUPPORT
566
Lev Walkin59b176e2005-11-26 11:25:14 +0000567asn_dec_rval_t
568INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
569 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000570 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000571 asn_dec_rval_t rval = { RC_OK, 0 };
572 INTEGER_t *st = (INTEGER_t *)*sptr;
573 asn_per_constraint_t *ct;
574 int repeat;
575
576 (void)opt_codec_ctx;
577
578 if(!st) {
579 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
580 if(!st) _ASN_DECODE_FAILED;
581 }
582
583 if(!constraints) constraints = td->per_constraints;
584 ct = constraints ? &constraints->value : 0;
585
586 if(ct && ct->flags & APC_EXTENSIBLE) {
587 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000588 if(inext < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000589 if(inext) ct = 0;
590 }
591
592 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000593 st->buf = 0;
594 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000595 if(ct) {
596 if(ct->flags & APC_SEMI_CONSTRAINED) {
597 st->buf = (uint8_t *)CALLOC(1, 2);
598 if(!st->buf) _ASN_DECODE_FAILED;
599 st->size = 1;
600 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
601 size_t size = (ct->range_bits + 7) >> 3;
602 st->buf = (uint8_t *)MALLOC(1 + size + 1);
603 if(!st->buf) _ASN_DECODE_FAILED;
604 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000606 }
607
608 /* X.691, #12.2.2 */
609 if(ct && ct->flags != APC_UNCONSTRAINED) {
610 /* #10.5.6 */
611 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
612 if(ct->range_bits >= 0) {
Lev Walkina105cbc2007-11-06 02:35:13 +0000613 long value;
614 if(ct->range_bits == 32) {
615 long lhalf;
616 value = per_get_few_bits(pd, 16);
617 if(value < 0) _ASN_DECODE_STARVED;
618 lhalf = per_get_few_bits(pd, 16);
619 if(lhalf < 0) _ASN_DECODE_STARVED;
620 value = (value << 16) | lhalf;
621 } else {
622 value = per_get_few_bits(pd, ct->range_bits);
623 if(value < 0) _ASN_DECODE_STARVED;
624 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000625 ASN_DEBUG("Got value %ld + low %ld",
626 value, ct->lower_bound);
627 value += ct->lower_bound;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000628 if((specs && specs->field_unsigned)
629 ? asn_ulong2INTEGER(st, value)
630 : asn_long2INTEGER(st, value))
Lev Walkin59b176e2005-11-26 11:25:14 +0000631 _ASN_DECODE_FAILED;
632 return rval;
633 }
634 } else {
635 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
636 }
637
638 /* X.691, #12.2.3, #12.2.4 */
639 do {
640 ssize_t len;
641 void *p;
642 int ret;
643
644 /* Get the PER length */
645 len = uper_get_length(pd, -1, &repeat);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000646 if(len < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000647
648 p = REALLOC(st->buf, st->size + len + 1);
649 if(!p) _ASN_DECODE_FAILED;
650 st->buf = (uint8_t *)p;
651
652 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000653 if(ret < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000654 st->size += len;
655 } while(repeat);
656 st->buf[st->size] = 0; /* JIC */
657
658 /* #12.2.3 */
659 if(ct && ct->lower_bound) {
660 /*
661 * TODO: replace by in-place arithmetics.
662 */
663 long value;
664 if(asn_INTEGER2long(st, &value))
665 _ASN_DECODE_FAILED;
666 if(asn_long2INTEGER(st, value + ct->lower_bound))
667 _ASN_DECODE_FAILED;
668 }
669
670 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000671}
672
Lev Walkin523de9e2006-08-18 01:34:18 +0000673asn_enc_rval_t
674INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
675 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000676 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000677 asn_enc_rval_t er;
678 INTEGER_t *st = (INTEGER_t *)sptr;
679 const uint8_t *buf;
680 const uint8_t *end;
681 asn_per_constraint_t *ct;
682 long value = 0;
683
684 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
685
686 if(!constraints) constraints = td->per_constraints;
687 ct = constraints ? &constraints->value : 0;
688
689 er.encoded = 0;
690
691 if(ct) {
692 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000693 if(specs && specs->field_unsigned) {
694 unsigned long uval;
695 if(asn_INTEGER2ulong(st, &uval))
696 _ASN_ENCODE_FAILED;
697 /* Check proper range */
698 if(ct->flags & APC_SEMI_CONSTRAINED) {
699 if(uval < (unsigned long)ct->lower_bound)
700 inext = 1;
701 } else if(ct->range_bits >= 0) {
702 if(uval < (unsigned long)ct->lower_bound
703 || uval > (unsigned long)ct->upper_bound)
704 inext = 1;
705 }
706 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
707 uval, st->buf[0], st->size,
708 ct->lower_bound, ct->upper_bound,
709 inext ? "ext" : "fix");
710 value = uval;
711 } else {
712 if(asn_INTEGER2long(st, &value))
713 _ASN_ENCODE_FAILED;
714 /* Check proper range */
715 if(ct->flags & APC_SEMI_CONSTRAINED) {
716 if(value < ct->lower_bound)
717 inext = 1;
718 } else if(ct->range_bits >= 0) {
719 if(value < ct->lower_bound
720 || value > ct->upper_bound)
721 inext = 1;
722 }
723 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
724 value, st->buf[0], st->size,
725 ct->lower_bound, ct->upper_bound,
726 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000727 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000728 if(ct->flags & APC_EXTENSIBLE) {
729 if(per_put_few_bits(po, inext, 1))
730 _ASN_ENCODE_FAILED;
731 if(inext) ct = 0;
732 } else if(inext) {
733 _ASN_ENCODE_FAILED;
734 }
735 }
736
737
738 /* X.691, #12.2.2 */
739 if(ct && ct->range_bits >= 0) {
740 /* #10.5.6 */
741 ASN_DEBUG("Encoding integer with range %d bits",
742 ct->range_bits);
Lev Walkina105cbc2007-11-06 02:35:13 +0000743 if(ct->range_bits == 32) {
744 /* TODO: extend to >32 bits */
745 long v = value - ct->lower_bound;
746 if(per_put_few_bits(po, v >> 1, 31)
747 || per_put_few_bits(po, v, 1))
748 _ASN_ENCODE_FAILED;
749 } else {
750 if(per_put_few_bits(po, value - ct->lower_bound,
Lev Walkin523de9e2006-08-18 01:34:18 +0000751 ct->range_bits))
Lev Walkina105cbc2007-11-06 02:35:13 +0000752 _ASN_ENCODE_FAILED;
753 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000754 _ASN_ENCODED_OK(er);
755 }
756
757 if(ct && ct->lower_bound) {
758 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
759 /* TODO: adjust lower bound */
760 _ASN_ENCODE_FAILED;
761 }
762
763 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
764 ssize_t mayEncode = uper_put_length(po, end - buf);
765 if(mayEncode < 0)
766 _ASN_ENCODE_FAILED;
767 if(per_put_many_bits(po, buf, 8 * mayEncode))
768 _ASN_ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000769 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000770 }
771
772 _ASN_ENCODED_OK(er);
773}
774
Lev Walkind7703cf2012-09-03 01:45:03 -0700775#endif /* ASN_DISABLE_PER_SUPPORT */
776
Lev Walkinf15320b2004-06-03 03:38:44 +0000777int
Lev Walkin5e033762004-09-29 13:26:15 +0000778asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000779 uint8_t *b, *end;
780 size_t size;
781 long l;
782
783 /* Sanity checking */
784 if(!iptr || !iptr->buf || !lptr) {
785 errno = EINVAL;
786 return -1;
787 }
788
789 /* Cache the begin/end of the buffer */
790 b = iptr->buf; /* Start of the INTEGER buffer */
791 size = iptr->size;
792 end = b + size; /* Where to stop */
793
794 if(size > sizeof(long)) {
795 uint8_t *end1 = end - 1;
796 /*
797 * Slightly more advanced processing,
798 * able to >sizeof(long) bytes,
799 * when the actual value is small
800 * (0x0000000000abcdef would yield a fine 0x00abcdef)
801 */
802 /* Skip out the insignificant leading bytes */
803 for(; b < end1; b++) {
804 switch(*b) {
805 case 0x00: if((b[1] & 0x80) == 0) continue; break;
806 case 0xff: if((b[1] & 0x80) != 0) continue; break;
807 }
808 break;
809 }
810
811 size = end - b;
812 if(size > sizeof(long)) {
813 /* Still cannot fit the long */
814 errno = ERANGE;
815 return -1;
816 }
817 }
818
819 /* Shortcut processing of a corner case */
820 if(end == b) {
821 *lptr = 0;
822 return 0;
823 }
824
825 /* Perform the sign initialization */
826 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
827 if((*b >> 7)) l = -1; else l = 0;
828
829 /* Conversion engine */
830 for(; b < end; b++)
831 l = (l << 8) | *b;
832
833 *lptr = l;
834 return 0;
835}
Lev Walkind703ff42004-10-21 11:21:25 +0000836
837int
Lev Walkin5c879db2007-11-06 06:23:31 +0000838asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
839 uint8_t *b, *end;
840 unsigned long l;
841 size_t size;
842
843 if(!iptr || !iptr->buf || !lptr) {
844 errno = EINVAL;
845 return -1;
846 }
847
848 b = iptr->buf;
849 size = iptr->size;
850 end = b + size;
851
852 /* If all extra leading bytes are zeroes, ignore them */
853 for(; size > sizeof(unsigned long); b++, size--) {
854 if(*b) {
855 /* Value won't fit unsigned long */
856 errno = ERANGE;
857 return -1;
858 }
859 }
860
861 /* Conversion engine */
862 for(l = 0; b < end; b++)
863 l = (l << 8) | *b;
864
865 *lptr = l;
866 return 0;
867}
868
869int
870asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
871 uint8_t *buf;
872 uint8_t *end;
873 uint8_t *b;
874 int shr;
875
876 if(value <= LONG_MAX)
877 return asn_long2INTEGER(st, value);
878
879 buf = (uint8_t *)MALLOC(1 + sizeof(value));
880 if(!buf) return -1;
881
882 end = buf + (sizeof(value) + 1);
883 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +0000884 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
885 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000886
887 if(st->buf) FREEMEM(st->buf);
888 st->buf = buf;
889 st->size = 1 + sizeof(value);
890
891 return 0;
892}
893
894int
Lev Walkind703ff42004-10-21 11:21:25 +0000895asn_long2INTEGER(INTEGER_t *st, long value) {
896 uint8_t *buf, *bp;
897 uint8_t *p;
898 uint8_t *pstart;
899 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000900 int littleEndian = 1; /* Run-time detection */
901 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000902
903 if(!st) {
904 errno = EINVAL;
905 return -1;
906 }
907
Lev Walkin8471cec2004-10-21 14:02:19 +0000908 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000909 if(!buf) return -1;
910
Lev Walkind7ad5612004-10-26 08:20:46 +0000911 if(*(char *)&littleEndian) {
912 pstart = (uint8_t *)&value + sizeof(value) - 1;
913 pend1 = (uint8_t *)&value;
914 add = -1;
915 } else {
916 pstart = (uint8_t *)&value;
917 pend1 = pstart + sizeof(value) - 1;
918 add = 1;
919 }
920
Lev Walkind703ff42004-10-21 11:21:25 +0000921 /*
922 * If the contents octet consists of more than one octet,
923 * then bits of the first octet and bit 8 of the second octet:
924 * a) shall not all be ones; and
925 * b) shall not all be zero.
926 */
Lev Walkin33700162004-10-26 09:03:31 +0000927 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000928 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000929 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000930 continue;
931 break;
Lev Walkin33700162004-10-26 09:03:31 +0000932 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000933 continue;
934 break;
935 }
936 break;
937 }
938 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000939 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
940 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000941
942 if(st->buf) FREEMEM(st->buf);
943 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000944 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000945
946 return 0;
947}
Lev Walkinb3751942012-09-02 19:36:47 -0700948
Lev Walkine09f9f12012-09-02 23:06:35 -0700949enum asn_strtol_result_e
950asn_strtol(const char *str, const char *end, long *lp) {
951 int sign = 1;
952 long l;
Lev Walkinb3751942012-09-02 19:36:47 -0700953
Lev Walkine09f9f12012-09-02 23:06:35 -0700954 const long upper_boundary = LONG_MAX / 10;
955 long last_digit_max = LONG_MAX % 10;
956
957 if(str >= end) return ASN_STRTOL_ERROR_INVAL;
958
959 switch(*str) {
960 case '-':
961 last_digit_max++;
962 sign = -1;
963 case '+':
964 str++;
965 }
966
967 if(str >= end) return ASN_STRTOL_ERROR_INVAL;
968
969 for(l = 0; str < end; str++) {
970 switch(*str) {
971 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
972 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
973 int d = *str - '0';
974 if(l < upper_boundary) {
975 l = l * 10 + d;
976 } else if(l == upper_boundary) {
977 if(d <= last_digit_max) {
978 if(sign > 0) {
979 l = l * 10 + d;
980 } else {
981 sign = 1;
982 l = -l * 10 - d;
983 }
984 } else {
985 return ASN_STRTOL_ERROR_RANGE;
986 }
987 } else {
988 return ASN_STRTOL_ERROR_RANGE;
989 }
990 }
991 continue;
992 default:
993 return ASN_STRTOL_ERROR_INVAL;
994 }
995 }
996
997 *lp = sign * l;
998 return ASN_STRTOL_OK;
Lev Walkinb3751942012-09-02 19:36:47 -0700999}
1000