blob: 01990edad0a8d2bca7394f5f307ee952e92bf64d [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 Walkin59b176e2005-11-26 11:25:14 +000027 INTEGER_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000028 INTEGER_encode_uper, /* Unaligned PER encoder */
Lev Walkinf15320b2004-06-03 03:38:44 +000029 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000030 asn_DEF_INTEGER_tags,
31 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
32 asn_DEF_INTEGER_tags, /* Same as above */
33 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000034 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000035 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000036 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000037};
38
39/*
Lev Walkinf15320b2004-06-03 03:38:44 +000040 * Encode INTEGER type using DER.
41 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000042asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000043INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000044 int tag_mode, ber_tlv_tag_t tag,
45 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000046 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000047
48 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000049 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000050
51 /*
52 * Canonicalize integer in the buffer.
53 * (Remove too long sign extension, remove some first 0x00 bytes)
54 */
55 if(st->buf) {
56 uint8_t *buf = st->buf;
57 uint8_t *end1 = buf + st->size - 1;
58 int shift;
59
60 /* Compute the number of superfluous leading bytes */
61 for(; buf < end1; buf++) {
62 /*
63 * If the contents octets of an integer value encoding
64 * consist of more than one octet, then the bits of the
65 * first octet and bit 8 of the second octet:
66 * a) shall not all be ones; and
67 * b) shall not all be zero.
68 */
69 switch(*buf) {
70 case 0x00: if((buf[1] & 0x80) == 0)
71 continue;
72 break;
73 case 0xff: if((buf[1] & 0x80))
74 continue;
75 break;
76 }
77 break;
78 }
79
80 /* Remove leading superfluous bytes from the integer */
81 shift = buf - st->buf;
82 if(shift) {
83 uint8_t *nb = st->buf;
84 uint8_t *end;
85
86 st->size -= shift; /* New size, minus bad bytes */
87 end = nb + st->size;
88
89 for(; nb < end; nb++, buf++)
90 *nb = *buf;
91 }
92
93 } /* if(1) */
94
Lev Walkin8e8078a2004-09-26 13:10:40 +000095 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +000096}
97
Lev Walkinc2350112005-03-29 17:19:53 +000098static const asn_INTEGER_enum_map_t *INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop);
Lev Walkine0b56e02005-02-25 12:10:27 +000099
Lev Walkinf15320b2004-06-03 03:38:44 +0000100/*
101 * INTEGER specific human-readable output.
102 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000103static ssize_t
Lev Walkine0b56e02005-02-25 12:10:27 +0000104INTEGER__dump(asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
105 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000106 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 uint8_t *buf = st->buf;
108 uint8_t *buf_end = st->buf + st->size;
109 signed long accum;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000110 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000111 char *p;
112 int ret;
113
Lev Walkindb13f512004-07-19 17:30:25 +0000114 /*
115 * Advance buf pointer until the start of the value's body.
116 * This will make us able to process large integers using simple case,
117 * when the actual value is small
118 * (0x0000000000abcdef would yield a fine 0x00abcdef)
119 */
120 /* Skip the insignificant leading bytes */
121 for(; buf < buf_end-1; buf++) {
122 switch(*buf) {
123 case 0x00: if((buf[1] & 0x80) == 0) continue; break;
124 case 0xff: if((buf[1] & 0x80) != 0) continue; break;
125 }
126 break;
127 }
128
Lev Walkinf15320b2004-06-03 03:38:44 +0000129 /* Simple case: the integer size is small */
Lev Walkindb13f512004-07-19 17:30:25 +0000130 if((size_t)(buf_end - buf) <= sizeof(accum)) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000131 const asn_INTEGER_enum_map_t *el;
132 size_t scrsize;
133 char *scr;
134
135 if(buf == buf_end) {
136 accum = 0;
137 } else {
138 accum = (*buf & 0x80) ? -1 : 0;
139 for(; buf < buf_end; buf++)
140 accum = (accum << 8) | *buf;
141 }
142
Lev Walkinc2350112005-03-29 17:19:53 +0000143 el = INTEGER_map_value2enum(specs, accum);
Lev Walkine0b56e02005-02-25 12:10:27 +0000144 if(el) {
145 scrsize = el->enum_len + 32;
146 scr = (char *)alloca(scrsize);
147 if(plainOrXER == 0)
148 ret = snprintf(scr, scrsize,
149 "%ld (%s)", accum, el->enum_name);
150 else
151 ret = snprintf(scr, scrsize,
152 "<%s/>", el->enum_name);
153 } else if(plainOrXER && specs && specs->strict_enumeration) {
154 ASN_DEBUG("ASN.1 forbids dealing with "
155 "unknown value of ENUMERATED type");
156 errno = EPERM;
157 return -1;
158 } else {
159 scrsize = sizeof(scratch);
160 scr = scratch;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000161 ret = snprintf(scr, scrsize,
162 (specs && specs->field_unsigned)
163 ?"%lu":"%ld", accum);
Lev Walkine0b56e02005-02-25 12:10:27 +0000164 }
165 assert(ret > 0 && (size_t)ret < scrsize);
166 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
167 } else if(plainOrXER && specs && specs->strict_enumeration) {
168 /*
169 * Here and earlier, we cannot encode the ENUMERATED values
170 * if there is no corresponding identifier.
171 */
172 ASN_DEBUG("ASN.1 forbids dealing with "
173 "unknown value of ENUMERATED type");
174 errno = EPERM;
175 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 }
177
178 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000179 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 for(p = scratch; buf < buf_end; buf++) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000181 static const char *h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000182 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000184 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000186 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 p = scratch;
188 }
189 *p++ = h2c[*buf >> 4];
190 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000191 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 }
Lev Walkindb13f512004-07-19 17:30:25 +0000193 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000194 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000195
Lev Walkina9cc46e2004-09-22 16:06:28 +0000196 wrote += p - scratch;
197 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
198}
199
200/*
201 * INTEGER specific human-readable output.
202 */
203int
Lev Walkin5e033762004-09-29 13:26:15 +0000204INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000205 asn_app_consume_bytes_f *cb, void *app_key) {
206 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000207 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208
209 (void)td;
210 (void)ilevel;
211
Lev Walkind500a962005-11-27 13:06:56 +0000212 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000213 ret = cb("<absent>", 8, app_key);
214 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000215 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000216
Lev Walkin8e8078a2004-09-26 13:10:40 +0000217 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000218}
219
Lev Walkine0b56e02005-02-25 12:10:27 +0000220struct e2v_key {
221 const char *start;
222 const char *stop;
223 asn_INTEGER_enum_map_t *vemap;
224 unsigned int *evmap;
225};
226static int
227INTEGER__compar_enum2value(const void *kp, const void *am) {
228 const struct e2v_key *key = (const struct e2v_key *)kp;
229 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
230 const char *ptr, *end, *name;
231
232 /* Remap the element (sort by different criterion) */
233 el = key->vemap + key->evmap[el - key->vemap];
234
235 /* Compare strings */
236 for(ptr = key->start, end = key->stop, name = el->enum_name;
237 ptr < end; ptr++, name++) {
238 if(*ptr != *name)
239 return *(const unsigned char *)ptr
240 - *(const unsigned char *)name;
241 }
242 return name[0] ? -1 : 0;
243}
244
245static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000246INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Lev Walkin9332b652005-03-04 11:18:44 +0000247 asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000248 int count = specs ? specs->map_count : 0;
249 struct e2v_key key;
250 const char *lp;
251
252 if(!count) return NULL;
253
254 /* Guaranteed: assert(lstart < lstop); */
255 /* Figure out the tag name */
256 for(lstart++, lp = lstart; lp < lstop; lp++) {
257 switch(*lp) {
258 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
259 case 0x2f: /* '/' */ case 0x3e: /* '>' */
260 break;
261 default:
262 continue;
263 }
264 break;
265 }
266 if(lp == lstop) return NULL; /* No tag found */
267 lstop = lp;
268
269 key.start = lstart;
270 key.stop = lstop;
271 key.vemap = specs->value2enum;
272 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000273 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
274 specs->value2enum, count, sizeof(specs->value2enum[0]),
275 INTEGER__compar_enum2value);
276 if(el_found) {
277 /* Remap enum2value into value2enum */
278 el_found = key.vemap + key.evmap[el_found - key.vemap];
279 }
280 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000281}
282
283static int
284INTEGER__compar_value2enum(const void *kp, const void *am) {
285 long a = *(const long *)kp;
286 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
287 long b = el->nat_value;
288 if(a < b) return -1;
289 else if(a == b) return 0;
290 else return 1;
291}
292
Lev Walkinc2350112005-03-29 17:19:53 +0000293const asn_INTEGER_enum_map_t *
294INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000295 int count = specs ? specs->map_count : 0;
296 if(!count) return 0;
297 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
298 count, sizeof(specs->value2enum[0]),
299 INTEGER__compar_value2enum);
300}
301
Lev Walkinc744a022006-09-15 18:33:25 +0000302static int
303INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
304 void *p = MALLOC(min_size + 1);
305 if(p) {
306 void *b = st->buf;
307 st->size = 0;
308 st->buf = p;
309 FREEMEM(b);
310 return 0;
311 } else {
312 return -1;
313 }
314}
315
Lev Walkind703ff42004-10-21 11:21:25 +0000316/*
317 * Decode the chunk of XML text encoding INTEGER.
318 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000319static enum xer_pbd_rval
320INTEGER__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 +0000321 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinb3751942012-09-02 19:36:47 -0700322 long dec_value;
Lev Walkine09f9f12012-09-02 23:06:35 -0700323 long hex_value;
Lev Walkine0b56e02005-02-25 12:10:27 +0000324 const char *lp;
325 const char *lstart = (const char *)chunk_buf;
326 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000327 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700328 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000329 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000330 ST_WAITDIGITS,
331 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700332 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000333 ST_HEXDIGIT1,
334 ST_HEXDIGIT2,
335 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700336 ST_END_ENUM,
337 ST_UNEXPECTED
338 } state = ST_LEADSPACE;
339 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
340 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000341
Lev Walkinc744a022006-09-15 18:33:25 +0000342 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000343 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
344 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000345
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000346 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
347 return XPBD_SYSTEM_FAILURE;
348
Lev Walkind703ff42004-10-21 11:21:25 +0000349 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000350 * We may have received a tag here. It will be processed inline.
351 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000352 */
Lev Walkinb3751942012-09-02 19:36:47 -0700353 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000354 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000355 switch(lv) {
356 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000357 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700358 case ST_LEADSPACE:
359 case ST_DIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000360 case ST_SKIPSPHEX:
361 continue;
362 case ST_HEXCOLON:
363 if(xer_is_whitespace(lp, lstop - lp)) {
364 lp = lstop - 1;
365 continue;
366 }
367 break;
Lev Walkine09f9f12012-09-02 23:06:35 -0700368 case ST_DIGITS:
369 dec_value_end = lp;
370 state = ST_DIGITS_TRAILSPACE;
371 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000372 default:
373 break;
374 }
Lev Walkind703ff42004-10-21 11:21:25 +0000375 break;
376 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700377 if(state == ST_LEADSPACE) {
378 dec_value = 0;
379 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000380 state = ST_WAITDIGITS;
381 continue;
382 }
383 break;
384 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700385 if(state == ST_LEADSPACE) {
386 dec_value = 0;
387 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000388 state = ST_WAITDIGITS;
389 continue;
390 }
391 break;
392 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
393 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000394 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700395 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000396 case ST_SKIPSPHEX: /* Fall through */
397 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700398 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000399 state = ST_HEXDIGIT2;
400 continue;
401 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700402 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000403 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700404 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000405 continue;
406 case ST_HEXCOLON:
407 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700408 case ST_LEADSPACE:
409 dec_value = 0;
410 dec_value_start = lp;
411 /* FALL THROUGH */
412 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000413 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700414 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700415 default:
416 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000417 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700418 break;
419 case 0x3c: /* '<', start of XML encoded enumeration */
420 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000421 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000422 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000423 (asn_INTEGER_specifics_t *)
424 td->specifics, lstart, lstop);
425 if(el) {
426 ASN_DEBUG("Found \"%s\" => %ld",
427 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700428 dec_value = el->nat_value;
429 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000430 lp = lstop - 1;
431 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000432 }
433 ASN_DEBUG("Unknown identifier for INTEGER");
434 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000435 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000436 case 0x3a: /* ':' */
437 if(state == ST_HEXCOLON) {
438 /* This colon is expected */
439 state = ST_HEXDIGIT1;
440 continue;
441 } else if(state == ST_DIGITS) {
442 /* The colon here means that we have
443 * decoded the first two hexadecimal
444 * places as a decimal value.
445 * Switch decoding mode. */
446 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000447 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700448 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000449 lp = lstart - 1;
450 continue;
451 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400452 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000453 break;
454 }
455 /* [A-Fa-f] */
456 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
457 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
458 switch(state) {
459 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700460 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000461 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700462 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
463 hex_value += 10;
464 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000465 state = ST_HEXDIGIT2;
466 continue;
467 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700468 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
469 hex_value += 10;
470 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000471 state = ST_HEXCOLON;
472 continue;
473 case ST_DIGITS:
474 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000475 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700476 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000477 lp = lstart - 1;
478 continue;
479 default:
480 break;
481 }
482 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000483 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000484
485 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700486 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400487 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700488 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000489 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000490 }
491
Lev Walkinc744a022006-09-15 18:33:25 +0000492 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700493 case ST_END_ENUM:
494 /* Got a complete and valid enumeration encoded as a tag. */
495 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000496 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700497 dec_value_end = lstop;
498 case ST_DIGITS_TRAILSPACE:
499 /* The last symbol encountered was a digit. */
500 switch(asn_strtol(dec_value_start, dec_value_end, &dec_value)) {
501 case ASN_STRTOL_OK:
502 break;
503 case ASN_STRTOL_ERROR_RANGE:
504 return XPBD_DECODER_LIMIT;
505 case ASN_STRTOL_ERROR_INVAL:
506 return XPBD_BROKEN_ENCODING;
507 }
Lev Walkinc744a022006-09-15 18:33:25 +0000508 break;
509 case ST_HEXCOLON:
510 st->buf[st->size] = 0; /* Just in case termination */
511 return XPBD_BODY_CONSUMED;
512 case ST_HEXDIGIT1:
513 case ST_HEXDIGIT2:
514 case ST_SKIPSPHEX:
515 return XPBD_BROKEN_ENCODING;
516 default:
Lev Walkine09f9f12012-09-02 23:06:35 -0700517 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
518 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000519 }
Lev Walkind703ff42004-10-21 11:21:25 +0000520
Lev Walkine09f9f12012-09-02 23:06:35 -0700521 /*
522 * Convert the result of parsing of enumeration or a straight
523 * decimal value into a BER representation.
524 */
525 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000526 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000527
Lev Walkin0fab1a62005-03-09 22:19:25 +0000528 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000529}
530
531asn_dec_rval_t
532INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
533 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000534 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000535
536 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000537 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000538 buf_ptr, size, INTEGER__xer_body_decode);
539}
540
Lev Walkina9cc46e2004-09-22 16:06:28 +0000541asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000542INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000543 int ilevel, enum xer_encoder_flags_e flags,
544 asn_app_consume_bytes_f *cb, void *app_key) {
545 const INTEGER_t *st = (const INTEGER_t *)sptr;
546 asn_enc_rval_t er;
547
548 (void)ilevel;
549 (void)flags;
550
Lev Walkind500a962005-11-27 13:06:56 +0000551 if(!st || !st->buf)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000552 _ASN_ENCODE_FAILED;
553
Lev Walkine0b56e02005-02-25 12:10:27 +0000554 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000555 if(er.encoded < 0) _ASN_ENCODE_FAILED;
556
Lev Walkin59b176e2005-11-26 11:25:14 +0000557 _ASN_ENCODED_OK(er);
558}
559
560asn_dec_rval_t
561INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
562 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000563 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000564 asn_dec_rval_t rval = { RC_OK, 0 };
565 INTEGER_t *st = (INTEGER_t *)*sptr;
566 asn_per_constraint_t *ct;
567 int repeat;
568
569 (void)opt_codec_ctx;
570
571 if(!st) {
572 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
573 if(!st) _ASN_DECODE_FAILED;
574 }
575
576 if(!constraints) constraints = td->per_constraints;
577 ct = constraints ? &constraints->value : 0;
578
579 if(ct && ct->flags & APC_EXTENSIBLE) {
580 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000581 if(inext < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000582 if(inext) ct = 0;
583 }
584
585 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000586 st->buf = 0;
587 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000588 if(ct) {
589 if(ct->flags & APC_SEMI_CONSTRAINED) {
590 st->buf = (uint8_t *)CALLOC(1, 2);
591 if(!st->buf) _ASN_DECODE_FAILED;
592 st->size = 1;
593 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
594 size_t size = (ct->range_bits + 7) >> 3;
595 st->buf = (uint8_t *)MALLOC(1 + size + 1);
596 if(!st->buf) _ASN_DECODE_FAILED;
597 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000598 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000599 }
600
601 /* X.691, #12.2.2 */
602 if(ct && ct->flags != APC_UNCONSTRAINED) {
603 /* #10.5.6 */
604 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
605 if(ct->range_bits >= 0) {
Lev Walkina105cbc2007-11-06 02:35:13 +0000606 long value;
607 if(ct->range_bits == 32) {
608 long lhalf;
609 value = per_get_few_bits(pd, 16);
610 if(value < 0) _ASN_DECODE_STARVED;
611 lhalf = per_get_few_bits(pd, 16);
612 if(lhalf < 0) _ASN_DECODE_STARVED;
613 value = (value << 16) | lhalf;
614 } else {
615 value = per_get_few_bits(pd, ct->range_bits);
616 if(value < 0) _ASN_DECODE_STARVED;
617 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000618 ASN_DEBUG("Got value %ld + low %ld",
619 value, ct->lower_bound);
620 value += ct->lower_bound;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000621 if((specs && specs->field_unsigned)
622 ? asn_ulong2INTEGER(st, value)
623 : asn_long2INTEGER(st, value))
Lev Walkin59b176e2005-11-26 11:25:14 +0000624 _ASN_DECODE_FAILED;
625 return rval;
626 }
627 } else {
628 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
629 }
630
631 /* X.691, #12.2.3, #12.2.4 */
632 do {
633 ssize_t len;
634 void *p;
635 int ret;
636
637 /* Get the PER length */
638 len = uper_get_length(pd, -1, &repeat);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000639 if(len < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000640
641 p = REALLOC(st->buf, st->size + len + 1);
642 if(!p) _ASN_DECODE_FAILED;
643 st->buf = (uint8_t *)p;
644
645 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000646 if(ret < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000647 st->size += len;
648 } while(repeat);
649 st->buf[st->size] = 0; /* JIC */
650
651 /* #12.2.3 */
652 if(ct && ct->lower_bound) {
653 /*
654 * TODO: replace by in-place arithmetics.
655 */
656 long value;
657 if(asn_INTEGER2long(st, &value))
658 _ASN_DECODE_FAILED;
659 if(asn_long2INTEGER(st, value + ct->lower_bound))
660 _ASN_DECODE_FAILED;
661 }
662
663 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000664}
665
Lev Walkin523de9e2006-08-18 01:34:18 +0000666asn_enc_rval_t
667INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
668 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000669 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000670 asn_enc_rval_t er;
671 INTEGER_t *st = (INTEGER_t *)sptr;
672 const uint8_t *buf;
673 const uint8_t *end;
674 asn_per_constraint_t *ct;
675 long value = 0;
676
677 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
678
679 if(!constraints) constraints = td->per_constraints;
680 ct = constraints ? &constraints->value : 0;
681
682 er.encoded = 0;
683
684 if(ct) {
685 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000686 if(specs && specs->field_unsigned) {
687 unsigned long uval;
688 if(asn_INTEGER2ulong(st, &uval))
689 _ASN_ENCODE_FAILED;
690 /* Check proper range */
691 if(ct->flags & APC_SEMI_CONSTRAINED) {
692 if(uval < (unsigned long)ct->lower_bound)
693 inext = 1;
694 } else if(ct->range_bits >= 0) {
695 if(uval < (unsigned long)ct->lower_bound
696 || uval > (unsigned long)ct->upper_bound)
697 inext = 1;
698 }
699 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
700 uval, st->buf[0], st->size,
701 ct->lower_bound, ct->upper_bound,
702 inext ? "ext" : "fix");
703 value = uval;
704 } else {
705 if(asn_INTEGER2long(st, &value))
706 _ASN_ENCODE_FAILED;
707 /* Check proper range */
708 if(ct->flags & APC_SEMI_CONSTRAINED) {
709 if(value < ct->lower_bound)
710 inext = 1;
711 } else if(ct->range_bits >= 0) {
712 if(value < ct->lower_bound
713 || value > ct->upper_bound)
714 inext = 1;
715 }
716 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
717 value, st->buf[0], st->size,
718 ct->lower_bound, ct->upper_bound,
719 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000720 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000721 if(ct->flags & APC_EXTENSIBLE) {
722 if(per_put_few_bits(po, inext, 1))
723 _ASN_ENCODE_FAILED;
724 if(inext) ct = 0;
725 } else if(inext) {
726 _ASN_ENCODE_FAILED;
727 }
728 }
729
730
731 /* X.691, #12.2.2 */
732 if(ct && ct->range_bits >= 0) {
733 /* #10.5.6 */
734 ASN_DEBUG("Encoding integer with range %d bits",
735 ct->range_bits);
Lev Walkina105cbc2007-11-06 02:35:13 +0000736 if(ct->range_bits == 32) {
737 /* TODO: extend to >32 bits */
738 long v = value - ct->lower_bound;
739 if(per_put_few_bits(po, v >> 1, 31)
740 || per_put_few_bits(po, v, 1))
741 _ASN_ENCODE_FAILED;
742 } else {
743 if(per_put_few_bits(po, value - ct->lower_bound,
Lev Walkin523de9e2006-08-18 01:34:18 +0000744 ct->range_bits))
Lev Walkina105cbc2007-11-06 02:35:13 +0000745 _ASN_ENCODE_FAILED;
746 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000747 _ASN_ENCODED_OK(er);
748 }
749
750 if(ct && ct->lower_bound) {
751 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
752 /* TODO: adjust lower bound */
753 _ASN_ENCODE_FAILED;
754 }
755
756 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
757 ssize_t mayEncode = uper_put_length(po, end - buf);
758 if(mayEncode < 0)
759 _ASN_ENCODE_FAILED;
760 if(per_put_many_bits(po, buf, 8 * mayEncode))
761 _ASN_ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000762 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000763 }
764
765 _ASN_ENCODED_OK(er);
766}
767
Lev Walkinf15320b2004-06-03 03:38:44 +0000768int
Lev Walkin5e033762004-09-29 13:26:15 +0000769asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000770 uint8_t *b, *end;
771 size_t size;
772 long l;
773
774 /* Sanity checking */
775 if(!iptr || !iptr->buf || !lptr) {
776 errno = EINVAL;
777 return -1;
778 }
779
780 /* Cache the begin/end of the buffer */
781 b = iptr->buf; /* Start of the INTEGER buffer */
782 size = iptr->size;
783 end = b + size; /* Where to stop */
784
785 if(size > sizeof(long)) {
786 uint8_t *end1 = end - 1;
787 /*
788 * Slightly more advanced processing,
789 * able to >sizeof(long) bytes,
790 * when the actual value is small
791 * (0x0000000000abcdef would yield a fine 0x00abcdef)
792 */
793 /* Skip out the insignificant leading bytes */
794 for(; b < end1; b++) {
795 switch(*b) {
796 case 0x00: if((b[1] & 0x80) == 0) continue; break;
797 case 0xff: if((b[1] & 0x80) != 0) continue; break;
798 }
799 break;
800 }
801
802 size = end - b;
803 if(size > sizeof(long)) {
804 /* Still cannot fit the long */
805 errno = ERANGE;
806 return -1;
807 }
808 }
809
810 /* Shortcut processing of a corner case */
811 if(end == b) {
812 *lptr = 0;
813 return 0;
814 }
815
816 /* Perform the sign initialization */
817 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
818 if((*b >> 7)) l = -1; else l = 0;
819
820 /* Conversion engine */
821 for(; b < end; b++)
822 l = (l << 8) | *b;
823
824 *lptr = l;
825 return 0;
826}
Lev Walkind703ff42004-10-21 11:21:25 +0000827
828int
Lev Walkin5c879db2007-11-06 06:23:31 +0000829asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
830 uint8_t *b, *end;
831 unsigned long l;
832 size_t size;
833
834 if(!iptr || !iptr->buf || !lptr) {
835 errno = EINVAL;
836 return -1;
837 }
838
839 b = iptr->buf;
840 size = iptr->size;
841 end = b + size;
842
843 /* If all extra leading bytes are zeroes, ignore them */
844 for(; size > sizeof(unsigned long); b++, size--) {
845 if(*b) {
846 /* Value won't fit unsigned long */
847 errno = ERANGE;
848 return -1;
849 }
850 }
851
852 /* Conversion engine */
853 for(l = 0; b < end; b++)
854 l = (l << 8) | *b;
855
856 *lptr = l;
857 return 0;
858}
859
860int
861asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
862 uint8_t *buf;
863 uint8_t *end;
864 uint8_t *b;
865 int shr;
866
867 if(value <= LONG_MAX)
868 return asn_long2INTEGER(st, value);
869
870 buf = (uint8_t *)MALLOC(1 + sizeof(value));
871 if(!buf) return -1;
872
873 end = buf + (sizeof(value) + 1);
874 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +0000875 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
876 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000877
878 if(st->buf) FREEMEM(st->buf);
879 st->buf = buf;
880 st->size = 1 + sizeof(value);
881
882 return 0;
883}
884
885int
Lev Walkind703ff42004-10-21 11:21:25 +0000886asn_long2INTEGER(INTEGER_t *st, long value) {
887 uint8_t *buf, *bp;
888 uint8_t *p;
889 uint8_t *pstart;
890 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000891 int littleEndian = 1; /* Run-time detection */
892 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000893
894 if(!st) {
895 errno = EINVAL;
896 return -1;
897 }
898
Lev Walkin8471cec2004-10-21 14:02:19 +0000899 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000900 if(!buf) return -1;
901
Lev Walkind7ad5612004-10-26 08:20:46 +0000902 if(*(char *)&littleEndian) {
903 pstart = (uint8_t *)&value + sizeof(value) - 1;
904 pend1 = (uint8_t *)&value;
905 add = -1;
906 } else {
907 pstart = (uint8_t *)&value;
908 pend1 = pstart + sizeof(value) - 1;
909 add = 1;
910 }
911
Lev Walkind703ff42004-10-21 11:21:25 +0000912 /*
913 * If the contents octet consists of more than one octet,
914 * then bits of the first octet and bit 8 of the second octet:
915 * a) shall not all be ones; and
916 * b) shall not all be zero.
917 */
Lev Walkin33700162004-10-26 09:03:31 +0000918 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000919 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000920 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000921 continue;
922 break;
Lev Walkin33700162004-10-26 09:03:31 +0000923 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000924 continue;
925 break;
926 }
927 break;
928 }
929 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000930 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
931 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000932
933 if(st->buf) FREEMEM(st->buf);
934 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000935 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000936
937 return 0;
938}
Lev Walkinb3751942012-09-02 19:36:47 -0700939
Lev Walkine09f9f12012-09-02 23:06:35 -0700940enum asn_strtol_result_e
941asn_strtol(const char *str, const char *end, long *lp) {
942 int sign = 1;
943 long l;
Lev Walkinb3751942012-09-02 19:36:47 -0700944
Lev Walkine09f9f12012-09-02 23:06:35 -0700945 const long upper_boundary = LONG_MAX / 10;
946 long last_digit_max = LONG_MAX % 10;
947
948 if(str >= end) return ASN_STRTOL_ERROR_INVAL;
949
950 switch(*str) {
951 case '-':
952 last_digit_max++;
953 sign = -1;
954 case '+':
955 str++;
956 }
957
958 if(str >= end) return ASN_STRTOL_ERROR_INVAL;
959
960 for(l = 0; str < end; str++) {
961 switch(*str) {
962 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
963 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
964 int d = *str - '0';
965 if(l < upper_boundary) {
966 l = l * 10 + d;
967 } else if(l == upper_boundary) {
968 if(d <= last_digit_max) {
969 if(sign > 0) {
970 l = l * 10 + d;
971 } else {
972 sign = 1;
973 l = -l * 10 - d;
974 }
975 } else {
976 return ASN_STRTOL_ERROR_RANGE;
977 }
978 } else {
979 return ASN_STRTOL_ERROR_RANGE;
980 }
981 }
982 continue;
983 default:
984 return ASN_STRTOL_ERROR_INVAL;
985 }
986 }
987
988 *lp = sign * l;
989 return ASN_STRTOL_OK;
Lev Walkinb3751942012-09-02 19:36:47 -0700990}
991