blob: 38ddb60bd19bf59e491636d53e01df24aef6be58 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin6c527842014-02-09 04:34:54 -08002 * Copyright (c) 2003-2014 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;
Lev Walkin97f8edc2013-03-28 04:38:41 -0700114 signed long value;
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 Walkin97f8edc2013-03-28 04:38:41 -0700119 if(specs && specs->field_unsigned)
120 ret = asn_INTEGER2ulong(st, (unsigned long *)&value);
121 else
122 ret = asn_INTEGER2long(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000123
Lev Walkinf15320b2004-06-03 03:38:44 +0000124 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700125 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000126 const asn_INTEGER_enum_map_t *el;
127 size_t scrsize;
128 char *scr;
129
Lev Walkin97f8edc2013-03-28 04:38:41 -0700130 el = (value >= 0 || !specs || !specs->field_unsigned)
131 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000132 if(el) {
133 scrsize = el->enum_len + 32;
134 scr = (char *)alloca(scrsize);
135 if(plainOrXER == 0)
136 ret = snprintf(scr, scrsize,
Lev Walkin97f8edc2013-03-28 04:38:41 -0700137 "%ld (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000138 else
139 ret = snprintf(scr, scrsize,
140 "<%s/>", el->enum_name);
141 } else if(plainOrXER && specs && specs->strict_enumeration) {
142 ASN_DEBUG("ASN.1 forbids dealing with "
143 "unknown value of ENUMERATED type");
144 errno = EPERM;
145 return -1;
146 } else {
147 scrsize = sizeof(scratch);
148 scr = scratch;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000149 ret = snprintf(scr, scrsize,
150 (specs && specs->field_unsigned)
Lev Walkin97f8edc2013-03-28 04:38:41 -0700151 ?"%lu":"%ld", value);
Lev Walkine0b56e02005-02-25 12:10:27 +0000152 }
153 assert(ret > 0 && (size_t)ret < scrsize);
154 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
155 } else if(plainOrXER && specs && specs->strict_enumeration) {
156 /*
157 * Here and earlier, we cannot encode the ENUMERATED values
158 * if there is no corresponding identifier.
159 */
160 ASN_DEBUG("ASN.1 forbids dealing with "
161 "unknown value of ENUMERATED type");
162 errno = EPERM;
163 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000164 }
165
166 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000167 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000168 for(p = scratch; buf < buf_end; buf++) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000169 static const char *h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000170 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000172 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000174 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 p = scratch;
176 }
177 *p++ = h2c[*buf >> 4];
178 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000179 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 }
Lev Walkindb13f512004-07-19 17:30:25 +0000181 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000182 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000183
Lev Walkina9cc46e2004-09-22 16:06:28 +0000184 wrote += p - scratch;
185 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
186}
187
188/*
189 * INTEGER specific human-readable output.
190 */
191int
Lev Walkin5e033762004-09-29 13:26:15 +0000192INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000193 asn_app_consume_bytes_f *cb, void *app_key) {
194 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000195 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000196
197 (void)td;
198 (void)ilevel;
199
Lev Walkind500a962005-11-27 13:06:56 +0000200 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000201 ret = cb("<absent>", 8, app_key);
202 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000203 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000204
Lev Walkin8e8078a2004-09-26 13:10:40 +0000205 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000206}
207
Lev Walkine0b56e02005-02-25 12:10:27 +0000208struct e2v_key {
209 const char *start;
210 const char *stop;
211 asn_INTEGER_enum_map_t *vemap;
212 unsigned int *evmap;
213};
214static int
215INTEGER__compar_enum2value(const void *kp, const void *am) {
216 const struct e2v_key *key = (const struct e2v_key *)kp;
217 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
218 const char *ptr, *end, *name;
219
220 /* Remap the element (sort by different criterion) */
221 el = key->vemap + key->evmap[el - key->vemap];
222
223 /* Compare strings */
224 for(ptr = key->start, end = key->stop, name = el->enum_name;
225 ptr < end; ptr++, name++) {
226 if(*ptr != *name)
227 return *(const unsigned char *)ptr
228 - *(const unsigned char *)name;
229 }
230 return name[0] ? -1 : 0;
231}
232
233static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000234INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Lev Walkin9332b652005-03-04 11:18:44 +0000235 asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000236 int count = specs ? specs->map_count : 0;
237 struct e2v_key key;
238 const char *lp;
239
240 if(!count) return NULL;
241
242 /* Guaranteed: assert(lstart < lstop); */
243 /* Figure out the tag name */
244 for(lstart++, lp = lstart; lp < lstop; lp++) {
245 switch(*lp) {
246 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
247 case 0x2f: /* '/' */ case 0x3e: /* '>' */
248 break;
249 default:
250 continue;
251 }
252 break;
253 }
254 if(lp == lstop) return NULL; /* No tag found */
255 lstop = lp;
256
257 key.start = lstart;
258 key.stop = lstop;
259 key.vemap = specs->value2enum;
260 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000261 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
262 specs->value2enum, count, sizeof(specs->value2enum[0]),
263 INTEGER__compar_enum2value);
264 if(el_found) {
265 /* Remap enum2value into value2enum */
266 el_found = key.vemap + key.evmap[el_found - key.vemap];
267 }
268 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000269}
270
271static int
272INTEGER__compar_value2enum(const void *kp, const void *am) {
273 long a = *(const long *)kp;
274 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
275 long b = el->nat_value;
276 if(a < b) return -1;
277 else if(a == b) return 0;
278 else return 1;
279}
280
Lev Walkinc2350112005-03-29 17:19:53 +0000281const asn_INTEGER_enum_map_t *
282INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000283 int count = specs ? specs->map_count : 0;
284 if(!count) return 0;
285 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
286 count, sizeof(specs->value2enum[0]),
287 INTEGER__compar_value2enum);
288}
289
Lev Walkinc744a022006-09-15 18:33:25 +0000290static int
291INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
292 void *p = MALLOC(min_size + 1);
293 if(p) {
294 void *b = st->buf;
295 st->size = 0;
296 st->buf = p;
297 FREEMEM(b);
298 return 0;
299 } else {
300 return -1;
301 }
302}
303
Lev Walkind703ff42004-10-21 11:21:25 +0000304/*
305 * Decode the chunk of XML text encoding INTEGER.
306 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000307static enum xer_pbd_rval
308INTEGER__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 +0000309 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinb3751942012-09-02 19:36:47 -0700310 long dec_value;
Lev Walkin0ff71292013-03-25 18:51:51 -0700311 long hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000312 const char *lp;
313 const char *lstart = (const char *)chunk_buf;
314 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000315 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700316 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000317 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000318 ST_WAITDIGITS,
319 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700320 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000321 ST_HEXDIGIT1,
322 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700323 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000324 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700325 ST_END_ENUM,
326 ST_UNEXPECTED
327 } state = ST_LEADSPACE;
328 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
329 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000330
Lev Walkinc744a022006-09-15 18:33:25 +0000331 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000332 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
333 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000334
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000335 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
336 return XPBD_SYSTEM_FAILURE;
337
Lev Walkind703ff42004-10-21 11:21:25 +0000338 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000339 * We may have received a tag here. It will be processed inline.
340 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000341 */
Lev Walkinb3751942012-09-02 19:36:47 -0700342 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000343 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000344 switch(lv) {
345 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000346 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700347 case ST_LEADSPACE:
348 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700349 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000350 case ST_SKIPSPHEX:
351 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700352 case ST_DIGITS:
353 dec_value_end = lp;
354 state = ST_DIGITS_TRAILSPACE;
355 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700356 case ST_HEXCOLON:
357 state = ST_HEXDIGITS_TRAILSPACE;
358 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000359 default:
360 break;
361 }
Lev Walkind703ff42004-10-21 11:21:25 +0000362 break;
363 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700364 if(state == ST_LEADSPACE) {
365 dec_value = 0;
366 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000367 state = ST_WAITDIGITS;
368 continue;
369 }
370 break;
371 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700372 if(state == ST_LEADSPACE) {
373 dec_value = 0;
374 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000375 state = ST_WAITDIGITS;
376 continue;
377 }
378 break;
379 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
380 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000381 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700382 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000383 case ST_SKIPSPHEX: /* Fall through */
384 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700385 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000386 state = ST_HEXDIGIT2;
387 continue;
388 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700389 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000390 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700391 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000392 continue;
393 case ST_HEXCOLON:
394 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700395 case ST_LEADSPACE:
396 dec_value = 0;
397 dec_value_start = lp;
398 /* FALL THROUGH */
399 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000400 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700401 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700402 default:
403 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000404 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700405 break;
406 case 0x3c: /* '<', start of XML encoded enumeration */
407 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000408 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000409 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000410 (asn_INTEGER_specifics_t *)
411 td->specifics, lstart, lstop);
412 if(el) {
413 ASN_DEBUG("Found \"%s\" => %ld",
414 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700415 dec_value = el->nat_value;
416 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000417 lp = lstop - 1;
418 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000419 }
420 ASN_DEBUG("Unknown identifier for INTEGER");
421 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000422 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000423 case 0x3a: /* ':' */
424 if(state == ST_HEXCOLON) {
425 /* This colon is expected */
426 state = ST_HEXDIGIT1;
427 continue;
428 } else if(state == ST_DIGITS) {
429 /* The colon here means that we have
430 * decoded the first two hexadecimal
431 * places as a decimal value.
432 * Switch decoding mode. */
433 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000434 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700435 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000436 lp = lstart - 1;
437 continue;
438 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400439 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000440 break;
441 }
442 /* [A-Fa-f] */
443 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
444 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
445 switch(state) {
446 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700447 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000448 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700449 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
450 hex_value += 10;
451 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000452 state = ST_HEXDIGIT2;
453 continue;
454 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700455 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
456 hex_value += 10;
457 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000458 state = ST_HEXCOLON;
459 continue;
460 case ST_DIGITS:
461 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000462 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700463 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000464 lp = lstart - 1;
465 continue;
466 default:
467 break;
468 }
469 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000470 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000471
472 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700473 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400474 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700475 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000476 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000477 }
478
Lev Walkinc744a022006-09-15 18:33:25 +0000479 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700480 case ST_END_ENUM:
481 /* Got a complete and valid enumeration encoded as a tag. */
482 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000483 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700484 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700485 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700486 case ST_DIGITS_TRAILSPACE:
487 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700488 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700489 case ASN_STRTOL_OK:
490 break;
491 case ASN_STRTOL_ERROR_RANGE:
492 return XPBD_DECODER_LIMIT;
493 case ASN_STRTOL_ERROR_INVAL:
Lev Walkincad560a2013-03-16 07:00:58 -0700494 case ASN_STRTOL_EXPECT_MORE:
495 case ASN_STRTOL_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700496 return XPBD_BROKEN_ENCODING;
497 }
Lev Walkinc744a022006-09-15 18:33:25 +0000498 break;
499 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700500 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000501 st->buf[st->size] = 0; /* Just in case termination */
502 return XPBD_BODY_CONSUMED;
503 case ST_HEXDIGIT1:
504 case ST_HEXDIGIT2:
505 case ST_SKIPSPHEX:
506 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700507 case ST_LEADSPACE:
508 /* Content not found */
509 return XPBD_NOT_BODY_IGNORE;
510 case ST_WAITDIGITS:
511 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700512 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
513 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000514 }
Lev Walkind703ff42004-10-21 11:21:25 +0000515
Lev Walkine09f9f12012-09-02 23:06:35 -0700516 /*
517 * Convert the result of parsing of enumeration or a straight
518 * decimal value into a BER representation.
519 */
520 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000521 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000522
Lev Walkin0fab1a62005-03-09 22:19:25 +0000523 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000524}
525
526asn_dec_rval_t
527INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
528 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000529 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000530
531 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000532 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000533 buf_ptr, size, INTEGER__xer_body_decode);
534}
535
Lev Walkina9cc46e2004-09-22 16:06:28 +0000536asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000537INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000538 int ilevel, enum xer_encoder_flags_e flags,
539 asn_app_consume_bytes_f *cb, void *app_key) {
540 const INTEGER_t *st = (const INTEGER_t *)sptr;
541 asn_enc_rval_t er;
542
543 (void)ilevel;
544 (void)flags;
545
Lev Walkind500a962005-11-27 13:06:56 +0000546 if(!st || !st->buf)
Lev Walkina9cc46e2004-09-22 16:06:28 +0000547 _ASN_ENCODE_FAILED;
548
Lev Walkine0b56e02005-02-25 12:10:27 +0000549 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000550 if(er.encoded < 0) _ASN_ENCODE_FAILED;
551
Lev Walkin59b176e2005-11-26 11:25:14 +0000552 _ASN_ENCODED_OK(er);
553}
554
Lev Walkind7703cf2012-09-03 01:45:03 -0700555#ifndef ASN_DISABLE_PER_SUPPORT
556
Lev Walkin59b176e2005-11-26 11:25:14 +0000557asn_dec_rval_t
558INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
559 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000560 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000561 asn_dec_rval_t rval = { RC_OK, 0 };
562 INTEGER_t *st = (INTEGER_t *)*sptr;
563 asn_per_constraint_t *ct;
564 int repeat;
565
566 (void)opt_codec_ctx;
567
568 if(!st) {
569 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
570 if(!st) _ASN_DECODE_FAILED;
571 }
572
573 if(!constraints) constraints = td->per_constraints;
574 ct = constraints ? &constraints->value : 0;
575
576 if(ct && ct->flags & APC_EXTENSIBLE) {
577 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000578 if(inext < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000579 if(inext) ct = 0;
580 }
581
582 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000583 st->buf = 0;
584 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000585 if(ct) {
586 if(ct->flags & APC_SEMI_CONSTRAINED) {
587 st->buf = (uint8_t *)CALLOC(1, 2);
588 if(!st->buf) _ASN_DECODE_FAILED;
589 st->size = 1;
590 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
591 size_t size = (ct->range_bits + 7) >> 3;
592 st->buf = (uint8_t *)MALLOC(1 + size + 1);
593 if(!st->buf) _ASN_DECODE_FAILED;
594 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000595 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000596 }
597
Lev Walkin6c527842014-02-09 04:34:54 -0800598 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000599 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800600 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000601 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
602 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800603 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin59b176e2005-11-26 11:25:14 +0000604 _ASN_DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800605
606 if(specs && specs->field_unsigned) {
607 unsigned long uvalue;
608 if(uper_get_constrained_whole_number(pd,
609 &uvalue, ct->range_bits))
610 _ASN_DECODE_STARVED;
611 ASN_DEBUG("Got value %lu + low %ld",
612 uvalue, ct->lower_bound);
613 uvalue += ct->lower_bound;
614 if(asn_ulong2INTEGER(st, uvalue))
615 _ASN_DECODE_FAILED;
616 } else {
617 unsigned long svalue;
618 if(uper_get_constrained_whole_number(pd,
619 &svalue, ct->range_bits))
620 _ASN_DECODE_STARVED;
621 ASN_DEBUG("Got value %ld + low %ld",
622 svalue, ct->lower_bound);
623 svalue += ct->lower_bound;
624 if(asn_long2INTEGER(st, svalue))
625 _ASN_DECODE_FAILED;
626 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000627 return rval;
628 }
629 } else {
630 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
631 }
632
633 /* X.691, #12.2.3, #12.2.4 */
634 do {
635 ssize_t len;
636 void *p;
637 int ret;
638
639 /* Get the PER length */
640 len = uper_get_length(pd, -1, &repeat);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000641 if(len < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000642
643 p = REALLOC(st->buf, st->size + len + 1);
644 if(!p) _ASN_DECODE_FAILED;
645 st->buf = (uint8_t *)p;
646
647 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin0a8aa602006-09-18 20:05:55 +0000648 if(ret < 0) _ASN_DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000649 st->size += len;
650 } while(repeat);
651 st->buf[st->size] = 0; /* JIC */
652
653 /* #12.2.3 */
654 if(ct && ct->lower_bound) {
655 /*
656 * TODO: replace by in-place arithmetics.
657 */
658 long value;
659 if(asn_INTEGER2long(st, &value))
660 _ASN_DECODE_FAILED;
661 if(asn_long2INTEGER(st, value + ct->lower_bound))
662 _ASN_DECODE_FAILED;
663 }
664
665 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000666}
667
Lev Walkin523de9e2006-08-18 01:34:18 +0000668asn_enc_rval_t
669INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
670 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000671 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000672 asn_enc_rval_t er;
673 INTEGER_t *st = (INTEGER_t *)sptr;
674 const uint8_t *buf;
675 const uint8_t *end;
676 asn_per_constraint_t *ct;
677 long value = 0;
678
679 if(!st || st->size == 0) _ASN_ENCODE_FAILED;
680
681 if(!constraints) constraints = td->per_constraints;
682 ct = constraints ? &constraints->value : 0;
683
684 er.encoded = 0;
685
686 if(ct) {
687 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000688 if(specs && specs->field_unsigned) {
689 unsigned long uval;
690 if(asn_INTEGER2ulong(st, &uval))
691 _ASN_ENCODE_FAILED;
692 /* Check proper range */
693 if(ct->flags & APC_SEMI_CONSTRAINED) {
694 if(uval < (unsigned long)ct->lower_bound)
695 inext = 1;
696 } else if(ct->range_bits >= 0) {
697 if(uval < (unsigned long)ct->lower_bound
698 || uval > (unsigned long)ct->upper_bound)
699 inext = 1;
700 }
701 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
702 uval, st->buf[0], st->size,
703 ct->lower_bound, ct->upper_bound,
704 inext ? "ext" : "fix");
705 value = uval;
706 } else {
707 if(asn_INTEGER2long(st, &value))
708 _ASN_ENCODE_FAILED;
709 /* Check proper range */
710 if(ct->flags & APC_SEMI_CONSTRAINED) {
711 if(value < ct->lower_bound)
712 inext = 1;
713 } else if(ct->range_bits >= 0) {
714 if(value < ct->lower_bound
715 || value > ct->upper_bound)
716 inext = 1;
717 }
718 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
719 value, st->buf[0], st->size,
720 ct->lower_bound, ct->upper_bound,
721 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000722 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000723 if(ct->flags & APC_EXTENSIBLE) {
724 if(per_put_few_bits(po, inext, 1))
725 _ASN_ENCODE_FAILED;
726 if(inext) ct = 0;
727 } else if(inext) {
728 _ASN_ENCODE_FAILED;
729 }
730 }
731
732
Lev Walkin6c527842014-02-09 04:34:54 -0800733 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000734 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800735 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800736 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
737 value, value - ct->lower_bound, ct->range_bits);
738 unsigned long v = value - ct->lower_bound;
739 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin6c527842014-02-09 04:34:54 -0800740 _ASN_ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000741 _ASN_ENCODED_OK(er);
742 }
743
744 if(ct && ct->lower_bound) {
745 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
746 /* TODO: adjust lower bound */
747 _ASN_ENCODE_FAILED;
748 }
749
750 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
751 ssize_t mayEncode = uper_put_length(po, end - buf);
752 if(mayEncode < 0)
753 _ASN_ENCODE_FAILED;
754 if(per_put_many_bits(po, buf, 8 * mayEncode))
755 _ASN_ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000756 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000757 }
758
759 _ASN_ENCODED_OK(er);
760}
761
Lev Walkind7703cf2012-09-03 01:45:03 -0700762#endif /* ASN_DISABLE_PER_SUPPORT */
763
Lev Walkinf15320b2004-06-03 03:38:44 +0000764int
Lev Walkin5e033762004-09-29 13:26:15 +0000765asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000766 uint8_t *b, *end;
767 size_t size;
768 long l;
769
770 /* Sanity checking */
771 if(!iptr || !iptr->buf || !lptr) {
772 errno = EINVAL;
773 return -1;
774 }
775
776 /* Cache the begin/end of the buffer */
777 b = iptr->buf; /* Start of the INTEGER buffer */
778 size = iptr->size;
779 end = b + size; /* Where to stop */
780
781 if(size > sizeof(long)) {
782 uint8_t *end1 = end - 1;
783 /*
784 * Slightly more advanced processing,
785 * able to >sizeof(long) bytes,
786 * when the actual value is small
787 * (0x0000000000abcdef would yield a fine 0x00abcdef)
788 */
789 /* Skip out the insignificant leading bytes */
790 for(; b < end1; b++) {
791 switch(*b) {
792 case 0x00: if((b[1] & 0x80) == 0) continue; break;
793 case 0xff: if((b[1] & 0x80) != 0) continue; break;
794 }
795 break;
796 }
797
798 size = end - b;
799 if(size > sizeof(long)) {
800 /* Still cannot fit the long */
801 errno = ERANGE;
802 return -1;
803 }
804 }
805
806 /* Shortcut processing of a corner case */
807 if(end == b) {
808 *lptr = 0;
809 return 0;
810 }
811
812 /* Perform the sign initialization */
813 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
814 if((*b >> 7)) l = -1; else l = 0;
815
816 /* Conversion engine */
817 for(; b < end; b++)
818 l = (l << 8) | *b;
819
820 *lptr = l;
821 return 0;
822}
Lev Walkind703ff42004-10-21 11:21:25 +0000823
824int
Lev Walkin5c879db2007-11-06 06:23:31 +0000825asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
826 uint8_t *b, *end;
827 unsigned long l;
828 size_t size;
829
830 if(!iptr || !iptr->buf || !lptr) {
831 errno = EINVAL;
832 return -1;
833 }
834
835 b = iptr->buf;
836 size = iptr->size;
837 end = b + size;
838
839 /* If all extra leading bytes are zeroes, ignore them */
840 for(; size > sizeof(unsigned long); b++, size--) {
841 if(*b) {
842 /* Value won't fit unsigned long */
843 errno = ERANGE;
844 return -1;
845 }
846 }
847
848 /* Conversion engine */
849 for(l = 0; b < end; b++)
850 l = (l << 8) | *b;
851
852 *lptr = l;
853 return 0;
854}
855
856int
857asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
858 uint8_t *buf;
859 uint8_t *end;
860 uint8_t *b;
861 int shr;
862
863 if(value <= LONG_MAX)
864 return asn_long2INTEGER(st, value);
865
866 buf = (uint8_t *)MALLOC(1 + sizeof(value));
867 if(!buf) return -1;
868
869 end = buf + (sizeof(value) + 1);
870 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +0000871 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
872 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000873
874 if(st->buf) FREEMEM(st->buf);
875 st->buf = buf;
876 st->size = 1 + sizeof(value);
877
878 return 0;
879}
880
881int
Lev Walkind703ff42004-10-21 11:21:25 +0000882asn_long2INTEGER(INTEGER_t *st, long value) {
883 uint8_t *buf, *bp;
884 uint8_t *p;
885 uint8_t *pstart;
886 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000887 int littleEndian = 1; /* Run-time detection */
888 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000889
890 if(!st) {
891 errno = EINVAL;
892 return -1;
893 }
894
Lev Walkin8471cec2004-10-21 14:02:19 +0000895 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000896 if(!buf) return -1;
897
Lev Walkind7ad5612004-10-26 08:20:46 +0000898 if(*(char *)&littleEndian) {
899 pstart = (uint8_t *)&value + sizeof(value) - 1;
900 pend1 = (uint8_t *)&value;
901 add = -1;
902 } else {
903 pstart = (uint8_t *)&value;
904 pend1 = pstart + sizeof(value) - 1;
905 add = 1;
906 }
907
Lev Walkind703ff42004-10-21 11:21:25 +0000908 /*
909 * If the contents octet consists of more than one octet,
910 * then bits of the first octet and bit 8 of the second octet:
911 * a) shall not all be ones; and
912 * b) shall not all be zero.
913 */
Lev Walkin33700162004-10-26 09:03:31 +0000914 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000915 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000916 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000917 continue;
918 break;
Lev Walkin33700162004-10-26 09:03:31 +0000919 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000920 continue;
921 break;
922 }
923 break;
924 }
925 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000926 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
927 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000928
929 if(st->buf) FREEMEM(st->buf);
930 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000931 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000932
933 return 0;
934}
Lev Walkinb3751942012-09-02 19:36:47 -0700935
Lev Walkincad560a2013-03-16 07:00:58 -0700936/*
937 * This function is going to be DEPRECATED soon.
938 */
Lev Walkine09f9f12012-09-02 23:06:35 -0700939enum asn_strtol_result_e
940asn_strtol(const char *str, const char *end, long *lp) {
Lev Walkincad560a2013-03-16 07:00:58 -0700941 const char *endp = end;
942
943 switch(asn_strtol_lim(str, &endp, lp)) {
944 case ASN_STRTOL_ERROR_RANGE:
945 return ASN_STRTOL_ERROR_RANGE;
946 case ASN_STRTOL_ERROR_INVAL:
947 return ASN_STRTOL_ERROR_INVAL;
948 case ASN_STRTOL_EXPECT_MORE:
949 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
950 case ASN_STRTOL_OK:
951 return ASN_STRTOL_OK;
952 case ASN_STRTOL_EXTRA_DATA:
953 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
954 }
955
956 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
957}
958
959/*
960 * Parse the number in the given string until the given *end position,
961 * returning the position after the last parsed character back using the
962 * same (*end) pointer.
963 * WARNING: This behavior is different from the standard strtol(3).
964 */
965enum asn_strtol_result_e
966asn_strtol_lim(const char *str, const char **end, long *lp) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700967 int sign = 1;
968 long l;
Lev Walkinb3751942012-09-02 19:36:47 -0700969
Lev Walkine09f9f12012-09-02 23:06:35 -0700970 const long upper_boundary = LONG_MAX / 10;
971 long last_digit_max = LONG_MAX % 10;
972
Lev Walkincad560a2013-03-16 07:00:58 -0700973 if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -0700974
975 switch(*str) {
976 case '-':
977 last_digit_max++;
978 sign = -1;
979 case '+':
980 str++;
Lev Walkincad560a2013-03-16 07:00:58 -0700981 if(str >= *end) {
982 *end = str;
983 return ASN_STRTOL_EXPECT_MORE;
984 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700985 }
986
Lev Walkincad560a2013-03-16 07:00:58 -0700987 for(l = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700988 switch(*str) {
989 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
990 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
991 int d = *str - '0';
992 if(l < upper_boundary) {
993 l = l * 10 + d;
994 } else if(l == upper_boundary) {
995 if(d <= last_digit_max) {
996 if(sign > 0) {
997 l = l * 10 + d;
998 } else {
999 sign = 1;
1000 l = -l * 10 - d;
1001 }
1002 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001003 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001004 return ASN_STRTOL_ERROR_RANGE;
1005 }
1006 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001007 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001008 return ASN_STRTOL_ERROR_RANGE;
1009 }
1010 }
1011 continue;
1012 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001013 *end = str;
1014 *lp = sign * l;
1015 return ASN_STRTOL_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001016 }
1017 }
1018
Lev Walkincad560a2013-03-16 07:00:58 -07001019 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001020 *lp = sign * l;
1021 return ASN_STRTOL_OK;
Lev Walkinb3751942012-09-02 19:36:47 -07001022}
1023