blob: 0331e1b50ceaed3ae1fc526b809ef4e9edbeff86 [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 Walkina6040112016-07-02 23:01:30 -07006#define _BSD_SOURCE /* for snprintf() on some linux systems */
Lev Walkin0ec6b192016-03-14 05:17:34 -07007#include <stdio.h>
Lev Walkina9cc46e2004-09-22 16:06:28 +00008#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <INTEGER.h>
Lev Walkind703ff42004-10-21 11:21:25 +000010#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
Lev Walkinf15320b2004-06-03 03:38:44 +000011#include <errno.h>
12
13/*
14 * INTEGER basic type description.
15 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070016static const ber_tlv_tag_t asn_DEF_INTEGER_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000017 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
18};
Lev Walkin5e033762004-09-29 13:26:15 +000019asn_TYPE_descriptor_t asn_DEF_INTEGER = {
Lev Walkinf15320b2004-06-03 03:38:44 +000020 "INTEGER",
Lev Walkindc06f6b2004-10-20 15:50:55 +000021 "INTEGER",
Lev Walkin8e8078a2004-09-26 13:10:40 +000022 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000023 INTEGER_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000024 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000025 ber_decode_primitive,
Lev Walkinf15320b2004-06-03 03:38:44 +000026 INTEGER_encode_der,
Lev Walkind703ff42004-10-21 11:21:25 +000027 INTEGER_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000028 INTEGER_encode_xer,
Lev Walkind7703cf2012-09-03 01:45:03 -070029#ifdef ASN_DISABLE_PER_SUPPORT
30 0,
31 0,
32#else
Lev Walkin59b176e2005-11-26 11:25:14 +000033 INTEGER_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000034 INTEGER_encode_uper, /* Unaligned PER encoder */
Lev Walkind7703cf2012-09-03 01:45:03 -070035#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000036 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000037 asn_DEF_INTEGER_tags,
38 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
39 asn_DEF_INTEGER_tags, /* Same as above */
40 sizeof(asn_DEF_INTEGER_tags) / sizeof(asn_DEF_INTEGER_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000041 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000042 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000043 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000044};
45
46/*
Lev Walkinf15320b2004-06-03 03:38:44 +000047 * Encode INTEGER type using DER.
48 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000049asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000050INTEGER_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000051 int tag_mode, ber_tlv_tag_t tag,
52 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000053 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000054
55 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
Lev Walkin8e8078a2004-09-26 13:10:40 +000056 cb?"Encoding":"Estimating", td->name, tag_mode);
Lev Walkinf15320b2004-06-03 03:38:44 +000057
58 /*
59 * Canonicalize integer in the buffer.
60 * (Remove too long sign extension, remove some first 0x00 bytes)
61 */
62 if(st->buf) {
63 uint8_t *buf = st->buf;
64 uint8_t *end1 = buf + st->size - 1;
65 int shift;
66
67 /* Compute the number of superfluous leading bytes */
68 for(; buf < end1; buf++) {
69 /*
70 * If the contents octets of an integer value encoding
71 * consist of more than one octet, then the bits of the
72 * first octet and bit 8 of the second octet:
73 * a) shall not all be ones; and
74 * b) shall not all be zero.
75 */
76 switch(*buf) {
77 case 0x00: if((buf[1] & 0x80) == 0)
78 continue;
79 break;
80 case 0xff: if((buf[1] & 0x80))
81 continue;
82 break;
83 }
84 break;
85 }
86
87 /* Remove leading superfluous bytes from the integer */
88 shift = buf - st->buf;
89 if(shift) {
90 uint8_t *nb = st->buf;
91 uint8_t *end;
92
93 st->size -= shift; /* New size, minus bad bytes */
94 end = nb + st->size;
95
96 for(; nb < end; nb++, buf++)
97 *nb = *buf;
98 }
99
100 } /* if(1) */
101
Lev Walkin8e8078a2004-09-26 13:10:40 +0000102 return der_encode_primitive(td, sptr, tag_mode, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000103}
104
Lev Walkinc2350112005-03-29 17:19:53 +0000105static 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 +0000106
Lev Walkinf15320b2004-06-03 03:38:44 +0000107/*
108 * INTEGER specific human-readable output.
109 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000110static ssize_t
Wim Lewis14e6b162014-07-23 16:06:01 -0700111INTEGER__dump(const asn_TYPE_descriptor_t *td, const INTEGER_t *st, asn_app_consume_bytes_f *cb, void *app_key, int plainOrXER) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000112 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkindb13f512004-07-19 17:30:25 +0000113 char scratch[32]; /* Enough for 64-bit integer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 uint8_t *buf = st->buf;
115 uint8_t *buf_end = st->buf + st->size;
Lev Walkin97f8edc2013-03-28 04:38:41 -0700116 signed long value;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000117 ssize_t wrote = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 char *p;
119 int ret;
120
Lev Walkin97f8edc2013-03-28 04:38:41 -0700121 if(specs && specs->field_unsigned)
122 ret = asn_INTEGER2ulong(st, (unsigned long *)&value);
123 else
124 ret = asn_INTEGER2long(st, &value);
Lev Walkindb13f512004-07-19 17:30:25 +0000125
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 /* Simple case: the integer size is small */
Lev Walkin97f8edc2013-03-28 04:38:41 -0700127 if(ret == 0) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000128 const asn_INTEGER_enum_map_t *el;
129 size_t scrsize;
130 char *scr;
131
Lev Walkin97f8edc2013-03-28 04:38:41 -0700132 el = (value >= 0 || !specs || !specs->field_unsigned)
133 ? INTEGER_map_value2enum(specs, value) : 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000134 if(el) {
135 scrsize = el->enum_len + 32;
136 scr = (char *)alloca(scrsize);
137 if(plainOrXER == 0)
138 ret = snprintf(scr, scrsize,
Lev Walkin97f8edc2013-03-28 04:38:41 -0700139 "%ld (%s)", value, el->enum_name);
Lev Walkine0b56e02005-02-25 12:10:27 +0000140 else
141 ret = snprintf(scr, scrsize,
142 "<%s/>", el->enum_name);
143 } else if(plainOrXER && specs && specs->strict_enumeration) {
144 ASN_DEBUG("ASN.1 forbids dealing with "
145 "unknown value of ENUMERATED type");
146 errno = EPERM;
147 return -1;
148 } else {
149 scrsize = sizeof(scratch);
150 scr = scratch;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000151 ret = snprintf(scr, scrsize,
152 (specs && specs->field_unsigned)
Lev Walkin97f8edc2013-03-28 04:38:41 -0700153 ?"%lu":"%ld", value);
Lev Walkine0b56e02005-02-25 12:10:27 +0000154 }
155 assert(ret > 0 && (size_t)ret < scrsize);
156 return (cb(scr, ret, app_key) < 0) ? -1 : ret;
157 } else if(plainOrXER && specs && specs->strict_enumeration) {
158 /*
159 * Here and earlier, we cannot encode the ENUMERATED values
160 * if there is no corresponding identifier.
161 */
162 ASN_DEBUG("ASN.1 forbids dealing with "
163 "unknown value of ENUMERATED type");
164 errno = EPERM;
165 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 }
167
168 /* Output in the long xx:yy:zz... format */
Lev Walkindb13f512004-07-19 17:30:25 +0000169 /* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 for(p = scratch; buf < buf_end; buf++) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700171 const char * const h2c = "0123456789ABCDEF";
Lev Walkindb13f512004-07-19 17:30:25 +0000172 if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 /* Flush buffer */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000174 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 return -1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000176 wrote += p - scratch;
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 p = scratch;
178 }
179 *p++ = h2c[*buf >> 4];
180 *p++ = h2c[*buf & 0x0F];
Lev Walkin5e033762004-09-29 13:26:15 +0000181 *p++ = 0x3a; /* ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 }
Lev Walkindb13f512004-07-19 17:30:25 +0000183 if(p != scratch)
Lev Walkin5e033762004-09-29 13:26:15 +0000184 p--; /* Remove the last ":" */
Lev Walkinf15320b2004-06-03 03:38:44 +0000185
Lev Walkina9cc46e2004-09-22 16:06:28 +0000186 wrote += p - scratch;
187 return (cb(scratch, p - scratch, app_key) < 0) ? -1 : wrote;
188}
189
190/*
191 * INTEGER specific human-readable output.
192 */
193int
Lev Walkin5e033762004-09-29 13:26:15 +0000194INTEGER_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195 asn_app_consume_bytes_f *cb, void *app_key) {
196 const INTEGER_t *st = (const INTEGER_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000197 ssize_t ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000198
199 (void)td;
200 (void)ilevel;
201
Lev Walkind500a962005-11-27 13:06:56 +0000202 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000203 ret = cb("<absent>", 8, app_key);
204 else
Lev Walkine0b56e02005-02-25 12:10:27 +0000205 ret = INTEGER__dump(td, st, cb, app_key, 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000206
Lev Walkin8e8078a2004-09-26 13:10:40 +0000207 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208}
209
Lev Walkine0b56e02005-02-25 12:10:27 +0000210struct e2v_key {
211 const char *start;
212 const char *stop;
Wim Lewisfb6344e2014-07-28 12:16:01 -0700213 const asn_INTEGER_enum_map_t *vemap;
214 const unsigned int *evmap;
Lev Walkine0b56e02005-02-25 12:10:27 +0000215};
216static int
217INTEGER__compar_enum2value(const void *kp, const void *am) {
218 const struct e2v_key *key = (const struct e2v_key *)kp;
219 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
220 const char *ptr, *end, *name;
221
222 /* Remap the element (sort by different criterion) */
223 el = key->vemap + key->evmap[el - key->vemap];
224
225 /* Compare strings */
226 for(ptr = key->start, end = key->stop, name = el->enum_name;
227 ptr < end; ptr++, name++) {
228 if(*ptr != *name)
229 return *(const unsigned char *)ptr
230 - *(const unsigned char *)name;
231 }
232 return name[0] ? -1 : 0;
233}
234
235static const asn_INTEGER_enum_map_t *
Lev Walkinc2350112005-03-29 17:19:53 +0000236INTEGER_map_enum2value(asn_INTEGER_specifics_t *specs, const char *lstart, const char *lstop) {
Wim Lewisfb6344e2014-07-28 12:16:01 -0700237 const asn_INTEGER_enum_map_t *el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000238 int count = specs ? specs->map_count : 0;
239 struct e2v_key key;
240 const char *lp;
241
242 if(!count) return NULL;
243
244 /* Guaranteed: assert(lstart < lstop); */
245 /* Figure out the tag name */
246 for(lstart++, lp = lstart; lp < lstop; lp++) {
247 switch(*lp) {
248 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
249 case 0x2f: /* '/' */ case 0x3e: /* '>' */
250 break;
251 default:
252 continue;
253 }
254 break;
255 }
256 if(lp == lstop) return NULL; /* No tag found */
257 lstop = lp;
258
259 key.start = lstart;
260 key.stop = lstop;
261 key.vemap = specs->value2enum;
262 key.evmap = specs->enum2value;
Lev Walkin9332b652005-03-04 11:18:44 +0000263 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
264 specs->value2enum, count, sizeof(specs->value2enum[0]),
265 INTEGER__compar_enum2value);
266 if(el_found) {
267 /* Remap enum2value into value2enum */
268 el_found = key.vemap + key.evmap[el_found - key.vemap];
269 }
270 return el_found;
Lev Walkine0b56e02005-02-25 12:10:27 +0000271}
272
273static int
274INTEGER__compar_value2enum(const void *kp, const void *am) {
275 long a = *(const long *)kp;
276 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
277 long b = el->nat_value;
278 if(a < b) return -1;
279 else if(a == b) return 0;
280 else return 1;
281}
282
Lev Walkinc2350112005-03-29 17:19:53 +0000283const asn_INTEGER_enum_map_t *
284INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000285 int count = specs ? specs->map_count : 0;
286 if(!count) return 0;
287 return (asn_INTEGER_enum_map_t *)bsearch(&value, specs->value2enum,
288 count, sizeof(specs->value2enum[0]),
289 INTEGER__compar_value2enum);
290}
291
Lev Walkinc744a022006-09-15 18:33:25 +0000292static int
293INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
294 void *p = MALLOC(min_size + 1);
295 if(p) {
296 void *b = st->buf;
297 st->size = 0;
298 st->buf = p;
299 FREEMEM(b);
300 return 0;
301 } else {
302 return -1;
303 }
304}
305
Lev Walkind703ff42004-10-21 11:21:25 +0000306/*
307 * Decode the chunk of XML text encoding INTEGER.
308 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000309static enum xer_pbd_rval
310INTEGER__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 +0000311 INTEGER_t *st = (INTEGER_t *)sptr;
Lev Walkinb3751942012-09-02 19:36:47 -0700312 long dec_value;
Lev Walkin0ff71292013-03-25 18:51:51 -0700313 long hex_value = 0;
Lev Walkine0b56e02005-02-25 12:10:27 +0000314 const char *lp;
315 const char *lstart = (const char *)chunk_buf;
316 const char *lstop = lstart + chunk_size;
Lev Walkind703ff42004-10-21 11:21:25 +0000317 enum {
Lev Walkine09f9f12012-09-02 23:06:35 -0700318 ST_LEADSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000319 ST_SKIPSPHEX,
Lev Walkind703ff42004-10-21 11:21:25 +0000320 ST_WAITDIGITS,
321 ST_DIGITS,
Lev Walkine09f9f12012-09-02 23:06:35 -0700322 ST_DIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000323 ST_HEXDIGIT1,
324 ST_HEXDIGIT2,
Lev Walkinf6da1792012-09-04 14:40:57 -0700325 ST_HEXDIGITS_TRAILSPACE,
Lev Walkinc744a022006-09-15 18:33:25 +0000326 ST_HEXCOLON,
Lev Walkine09f9f12012-09-02 23:06:35 -0700327 ST_END_ENUM,
328 ST_UNEXPECTED
329 } state = ST_LEADSPACE;
330 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
331 const char *dec_value_end = 0;
Lev Walkin0be3a992004-10-21 12:11:57 +0000332
Lev Walkinc744a022006-09-15 18:33:25 +0000333 if(chunk_size)
Lev Walkin345e4c62006-10-19 02:44:08 +0000334 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
335 (long)chunk_size, *lstart, lstop[-1]);
Lev Walkinc744a022006-09-15 18:33:25 +0000336
Lev Walkin3d6fcfe2012-01-23 04:05:13 +0000337 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
338 return XPBD_SYSTEM_FAILURE;
339
Lev Walkind703ff42004-10-21 11:21:25 +0000340 /*
Lev Walkine0b56e02005-02-25 12:10:27 +0000341 * We may have received a tag here. It will be processed inline.
342 * Use strtoul()-like code and serialize the result.
Lev Walkind703ff42004-10-21 11:21:25 +0000343 */
Lev Walkinb3751942012-09-02 19:36:47 -0700344 for(lp = lstart; lp < lstop; lp++) {
Lev Walkin0be3a992004-10-21 12:11:57 +0000345 int lv = *lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000346 switch(lv) {
347 case 0x09: case 0x0a: case 0x0d: case 0x20:
Lev Walkinc744a022006-09-15 18:33:25 +0000348 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700349 case ST_LEADSPACE:
350 case ST_DIGITS_TRAILSPACE:
Lev Walkinf6da1792012-09-04 14:40:57 -0700351 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000352 case ST_SKIPSPHEX:
353 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700354 case ST_DIGITS:
355 dec_value_end = lp;
356 state = ST_DIGITS_TRAILSPACE;
357 continue;
Lev Walkinf6da1792012-09-04 14:40:57 -0700358 case ST_HEXCOLON:
359 state = ST_HEXDIGITS_TRAILSPACE;
360 continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000361 default:
362 break;
363 }
Lev Walkind703ff42004-10-21 11:21:25 +0000364 break;
365 case 0x2d: /* '-' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700366 if(state == ST_LEADSPACE) {
367 dec_value = 0;
368 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000369 state = ST_WAITDIGITS;
370 continue;
371 }
372 break;
373 case 0x2b: /* '+' */
Lev Walkine09f9f12012-09-02 23:06:35 -0700374 if(state == ST_LEADSPACE) {
375 dec_value = 0;
376 dec_value_start = lp;
Lev Walkind703ff42004-10-21 11:21:25 +0000377 state = ST_WAITDIGITS;
378 continue;
379 }
380 break;
381 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
382 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinc744a022006-09-15 18:33:25 +0000383 switch(state) {
Lev Walkinb3751942012-09-02 19:36:47 -0700384 case ST_DIGITS: continue;
Lev Walkinc744a022006-09-15 18:33:25 +0000385 case ST_SKIPSPHEX: /* Fall through */
386 case ST_HEXDIGIT1:
Lev Walkinb3751942012-09-02 19:36:47 -0700387 hex_value = (lv - 0x30) << 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000388 state = ST_HEXDIGIT2;
389 continue;
390 case ST_HEXDIGIT2:
Lev Walkinb3751942012-09-02 19:36:47 -0700391 hex_value += (lv - 0x30);
Lev Walkinc744a022006-09-15 18:33:25 +0000392 state = ST_HEXCOLON;
Lev Walkinb3751942012-09-02 19:36:47 -0700393 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000394 continue;
395 case ST_HEXCOLON:
396 return XPBD_BROKEN_ENCODING;
Lev Walkine09f9f12012-09-02 23:06:35 -0700397 case ST_LEADSPACE:
398 dec_value = 0;
399 dec_value_start = lp;
400 /* FALL THROUGH */
401 case ST_WAITDIGITS:
Lev Walkinc744a022006-09-15 18:33:25 +0000402 state = ST_DIGITS;
Lev Walkinb3751942012-09-02 19:36:47 -0700403 continue;
Lev Walkine09f9f12012-09-02 23:06:35 -0700404 default:
405 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000406 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700407 break;
408 case 0x3c: /* '<', start of XML encoded enumeration */
409 if(state == ST_LEADSPACE) {
Lev Walkine0b56e02005-02-25 12:10:27 +0000410 const asn_INTEGER_enum_map_t *el;
Lev Walkinc2350112005-03-29 17:19:53 +0000411 el = INTEGER_map_enum2value(
Lev Walkine0b56e02005-02-25 12:10:27 +0000412 (asn_INTEGER_specifics_t *)
413 td->specifics, lstart, lstop);
414 if(el) {
415 ASN_DEBUG("Found \"%s\" => %ld",
416 el->enum_name, el->nat_value);
Lev Walkine09f9f12012-09-02 23:06:35 -0700417 dec_value = el->nat_value;
418 state = ST_END_ENUM;
Lev Walkin806b0bb2005-03-09 22:31:22 +0000419 lp = lstop - 1;
420 continue;
Lev Walkine0b56e02005-02-25 12:10:27 +0000421 }
422 ASN_DEBUG("Unknown identifier for INTEGER");
423 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000424 return XPBD_BROKEN_ENCODING;
Lev Walkinc744a022006-09-15 18:33:25 +0000425 case 0x3a: /* ':' */
426 if(state == ST_HEXCOLON) {
427 /* This colon is expected */
428 state = ST_HEXDIGIT1;
429 continue;
430 } else if(state == ST_DIGITS) {
431 /* The colon here means that we have
432 * decoded the first two hexadecimal
433 * places as a decimal value.
434 * Switch decoding mode. */
435 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000436 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700437 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000438 lp = lstart - 1;
439 continue;
440 } else {
Lev Walkin2e763992011-07-21 01:17:37 +0400441 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
Lev Walkinc744a022006-09-15 18:33:25 +0000442 break;
443 }
444 /* [A-Fa-f] */
445 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
446 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
447 switch(state) {
448 case ST_SKIPSPHEX:
Lev Walkine09f9f12012-09-02 23:06:35 -0700449 case ST_LEADSPACE: /* Fall through */
Lev Walkinc744a022006-09-15 18:33:25 +0000450 case ST_HEXDIGIT1:
Lev Walkine09f9f12012-09-02 23:06:35 -0700451 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
452 hex_value += 10;
453 hex_value <<= 4;
Lev Walkinc744a022006-09-15 18:33:25 +0000454 state = ST_HEXDIGIT2;
455 continue;
456 case ST_HEXDIGIT2:
Lev Walkine09f9f12012-09-02 23:06:35 -0700457 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
458 hex_value += 10;
459 st->buf[st->size++] = (uint8_t)hex_value;
Lev Walkinc744a022006-09-15 18:33:25 +0000460 state = ST_HEXCOLON;
461 continue;
462 case ST_DIGITS:
463 ASN_DEBUG("INTEGER re-evaluate as hex form");
Lev Walkinc744a022006-09-15 18:33:25 +0000464 state = ST_SKIPSPHEX;
Lev Walkine09f9f12012-09-02 23:06:35 -0700465 dec_value_start = 0;
Lev Walkinc744a022006-09-15 18:33:25 +0000466 lp = lstart - 1;
467 continue;
468 default:
469 break;
470 }
471 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000472 }
Lev Walkin806b0bb2005-03-09 22:31:22 +0000473
474 /* Found extra non-numeric stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -0700475 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
Lev Walkin2e763992011-07-21 01:17:37 +0400476 lv, (long)(lp - lstart));
Lev Walkine09f9f12012-09-02 23:06:35 -0700477 state = ST_UNEXPECTED;
Lev Walkin0be3a992004-10-21 12:11:57 +0000478 break;
Lev Walkind703ff42004-10-21 11:21:25 +0000479 }
480
Lev Walkinc744a022006-09-15 18:33:25 +0000481 switch(state) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700482 case ST_END_ENUM:
483 /* Got a complete and valid enumeration encoded as a tag. */
484 break;
Lev Walkinc744a022006-09-15 18:33:25 +0000485 case ST_DIGITS:
Lev Walkine09f9f12012-09-02 23:06:35 -0700486 dec_value_end = lstop;
Lev Walkincad560a2013-03-16 07:00:58 -0700487 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700488 case ST_DIGITS_TRAILSPACE:
489 /* The last symbol encountered was a digit. */
Lev Walkincad560a2013-03-16 07:00:58 -0700490 switch(asn_strtol_lim(dec_value_start, &dec_value_end, &dec_value)) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700491 case ASN_STRTOL_OK:
492 break;
493 case ASN_STRTOL_ERROR_RANGE:
494 return XPBD_DECODER_LIMIT;
495 case ASN_STRTOL_ERROR_INVAL:
Lev Walkincad560a2013-03-16 07:00:58 -0700496 case ASN_STRTOL_EXPECT_MORE:
497 case ASN_STRTOL_EXTRA_DATA:
Lev Walkine09f9f12012-09-02 23:06:35 -0700498 return XPBD_BROKEN_ENCODING;
499 }
Lev Walkinc744a022006-09-15 18:33:25 +0000500 break;
501 case ST_HEXCOLON:
Lev Walkinf6da1792012-09-04 14:40:57 -0700502 case ST_HEXDIGITS_TRAILSPACE:
Lev Walkinc744a022006-09-15 18:33:25 +0000503 st->buf[st->size] = 0; /* Just in case termination */
504 return XPBD_BODY_CONSUMED;
505 case ST_HEXDIGIT1:
506 case ST_HEXDIGIT2:
507 case ST_SKIPSPHEX:
508 return XPBD_BROKEN_ENCODING;
Lev Walkin632f20b2012-09-04 13:14:29 -0700509 case ST_LEADSPACE:
510 /* Content not found */
511 return XPBD_NOT_BODY_IGNORE;
512 case ST_WAITDIGITS:
513 case ST_UNEXPECTED:
Lev Walkine09f9f12012-09-02 23:06:35 -0700514 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
515 return XPBD_BROKEN_ENCODING; /* No digits */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000516 }
Lev Walkind703ff42004-10-21 11:21:25 +0000517
Lev Walkine09f9f12012-09-02 23:06:35 -0700518 /*
519 * Convert the result of parsing of enumeration or a straight
520 * decimal value into a BER representation.
521 */
522 if(asn_long2INTEGER(st, dec_value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000523 return XPBD_SYSTEM_FAILURE;
Lev Walkind703ff42004-10-21 11:21:25 +0000524
Lev Walkin0fab1a62005-03-09 22:19:25 +0000525 return XPBD_BODY_CONSUMED;
Lev Walkind703ff42004-10-21 11:21:25 +0000526}
527
528asn_dec_rval_t
529INTEGER_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
530 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000531 const void *buf_ptr, size_t size) {
Lev Walkind703ff42004-10-21 11:21:25 +0000532
533 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000534 sptr, sizeof(INTEGER_t), opt_mname,
Lev Walkind703ff42004-10-21 11:21:25 +0000535 buf_ptr, size, INTEGER__xer_body_decode);
536}
537
Lev Walkina9cc46e2004-09-22 16:06:28 +0000538asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000539INTEGER_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000540 int ilevel, enum xer_encoder_flags_e flags,
541 asn_app_consume_bytes_f *cb, void *app_key) {
542 const INTEGER_t *st = (const INTEGER_t *)sptr;
543 asn_enc_rval_t er;
544
545 (void)ilevel;
546 (void)flags;
547
Lev Walkind500a962005-11-27 13:06:56 +0000548 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700549 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000550
Lev Walkine0b56e02005-02-25 12:10:27 +0000551 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700552 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000553
Lev Walkin7c1dc052016-03-14 03:08:15 -0700554 ASN__ENCODED_OK(er);
Lev Walkin59b176e2005-11-26 11:25:14 +0000555}
556
Lev Walkind7703cf2012-09-03 01:45:03 -0700557#ifndef ASN_DISABLE_PER_SUPPORT
558
Lev Walkin59b176e2005-11-26 11:25:14 +0000559asn_dec_rval_t
560INTEGER_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
561 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000562 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin59b176e2005-11-26 11:25:14 +0000563 asn_dec_rval_t rval = { RC_OK, 0 };
564 INTEGER_t *st = (INTEGER_t *)*sptr;
565 asn_per_constraint_t *ct;
566 int repeat;
567
568 (void)opt_codec_ctx;
569
570 if(!st) {
571 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
Lev Walkin7c1dc052016-03-14 03:08:15 -0700572 if(!st) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000573 }
574
575 if(!constraints) constraints = td->per_constraints;
576 ct = constraints ? &constraints->value : 0;
577
578 if(ct && ct->flags & APC_EXTENSIBLE) {
579 int inext = per_get_few_bits(pd, 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700580 if(inext < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000581 if(inext) ct = 0;
582 }
583
584 FREEMEM(st->buf);
Lev Walkin0b1eb0e2007-11-10 02:05:23 +0000585 st->buf = 0;
586 st->size = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +0000587 if(ct) {
588 if(ct->flags & APC_SEMI_CONSTRAINED) {
589 st->buf = (uint8_t *)CALLOC(1, 2);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700590 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000591 st->size = 1;
592 } else if(ct->flags & APC_CONSTRAINED && ct->range_bits >= 0) {
593 size_t size = (ct->range_bits + 7) >> 3;
594 st->buf = (uint8_t *)MALLOC(1 + size + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700595 if(!st->buf) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000596 st->size = size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000597 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000598 }
599
Lev Walkin6c527842014-02-09 04:34:54 -0800600 /* X.691-2008/11, #13.2.2, constrained whole number */
Lev Walkin59b176e2005-11-26 11:25:14 +0000601 if(ct && ct->flags != APC_UNCONSTRAINED) {
Lev Walkin6c527842014-02-09 04:34:54 -0800602 /* #11.5.6 */
Lev Walkin59b176e2005-11-26 11:25:14 +0000603 ASN_DEBUG("Integer with range %d bits", ct->range_bits);
604 if(ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800605 if((size_t)ct->range_bits > 8 * sizeof(unsigned long))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700606 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800607
608 if(specs && specs->field_unsigned) {
609 unsigned long uvalue;
610 if(uper_get_constrained_whole_number(pd,
611 &uvalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700612 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800613 ASN_DEBUG("Got value %lu + low %ld",
614 uvalue, ct->lower_bound);
615 uvalue += ct->lower_bound;
616 if(asn_ulong2INTEGER(st, uvalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700617 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800618 } else {
619 unsigned long svalue;
620 if(uper_get_constrained_whole_number(pd,
621 &svalue, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700622 ASN__DECODE_STARVED;
Lev Walkin6c527842014-02-09 04:34:54 -0800623 ASN_DEBUG("Got value %ld + low %ld",
624 svalue, ct->lower_bound);
625 svalue += ct->lower_bound;
626 if(asn_long2INTEGER(st, svalue))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700627 ASN__DECODE_FAILED;
Lev Walkin6c527842014-02-09 04:34:54 -0800628 }
Lev Walkin59b176e2005-11-26 11:25:14 +0000629 return rval;
630 }
631 } else {
632 ASN_DEBUG("Decoding unconstrained integer %s", td->name);
633 }
634
635 /* X.691, #12.2.3, #12.2.4 */
636 do {
637 ssize_t len;
638 void *p;
639 int ret;
640
641 /* Get the PER length */
642 len = uper_get_length(pd, -1, &repeat);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700643 if(len < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000644
645 p = REALLOC(st->buf, st->size + len + 1);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700646 if(!p) ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000647 st->buf = (uint8_t *)p;
648
649 ret = per_get_many_bits(pd, &st->buf[st->size], 0, 8 * len);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700650 if(ret < 0) ASN__DECODE_STARVED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000651 st->size += len;
652 } while(repeat);
653 st->buf[st->size] = 0; /* JIC */
654
655 /* #12.2.3 */
656 if(ct && ct->lower_bound) {
657 /*
658 * TODO: replace by in-place arithmetics.
659 */
660 long value;
661 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700662 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000663 if(asn_long2INTEGER(st, value + ct->lower_bound))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700664 ASN__DECODE_FAILED;
Lev Walkin59b176e2005-11-26 11:25:14 +0000665 }
666
667 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000668}
669
Lev Walkin523de9e2006-08-18 01:34:18 +0000670asn_enc_rval_t
671INTEGER_encode_uper(asn_TYPE_descriptor_t *td,
672 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
Lev Walkin8bb57a22007-12-03 13:41:36 +0000673 asn_INTEGER_specifics_t *specs=(asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin523de9e2006-08-18 01:34:18 +0000674 asn_enc_rval_t er;
675 INTEGER_t *st = (INTEGER_t *)sptr;
676 const uint8_t *buf;
677 const uint8_t *end;
678 asn_per_constraint_t *ct;
679 long value = 0;
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100680 unsigned long v = 0;
Lev Walkin523de9e2006-08-18 01:34:18 +0000681
Lev Walkin7c1dc052016-03-14 03:08:15 -0700682 if(!st || st->size == 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000683
684 if(!constraints) constraints = td->per_constraints;
685 ct = constraints ? &constraints->value : 0;
686
687 er.encoded = 0;
688
689 if(ct) {
690 int inext = 0;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000691 if(specs && specs->field_unsigned) {
692 unsigned long uval;
693 if(asn_INTEGER2ulong(st, &uval))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700694 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000695 /* Check proper range */
696 if(ct->flags & APC_SEMI_CONSTRAINED) {
697 if(uval < (unsigned long)ct->lower_bound)
698 inext = 1;
699 } else if(ct->range_bits >= 0) {
700 if(uval < (unsigned long)ct->lower_bound
701 || uval > (unsigned long)ct->upper_bound)
702 inext = 1;
703 }
704 ASN_DEBUG("Value %lu (%02x/%d) lb %lu ub %lu %s",
705 uval, st->buf[0], st->size,
706 ct->lower_bound, ct->upper_bound,
707 inext ? "ext" : "fix");
708 value = uval;
709 } else {
710 if(asn_INTEGER2long(st, &value))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700711 ASN__ENCODE_FAILED;
Lev Walkin8bb57a22007-12-03 13:41:36 +0000712 /* Check proper range */
713 if(ct->flags & APC_SEMI_CONSTRAINED) {
714 if(value < ct->lower_bound)
715 inext = 1;
716 } else if(ct->range_bits >= 0) {
717 if(value < ct->lower_bound
718 || value > ct->upper_bound)
719 inext = 1;
720 }
721 ASN_DEBUG("Value %ld (%02x/%d) lb %ld ub %ld %s",
722 value, st->buf[0], st->size,
723 ct->lower_bound, ct->upper_bound,
724 inext ? "ext" : "fix");
Lev Walkin523de9e2006-08-18 01:34:18 +0000725 }
Lev Walkin523de9e2006-08-18 01:34:18 +0000726 if(ct->flags & APC_EXTENSIBLE) {
727 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700728 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000729 if(inext) ct = 0;
730 } else if(inext) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700731 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000732 }
733 }
734
735
Lev Walkin6c527842014-02-09 04:34:54 -0800736 /* X.691-11/2008, #13.2.2, test if constrained whole number */
Lev Walkin523de9e2006-08-18 01:34:18 +0000737 if(ct && ct->range_bits >= 0) {
Lev Walkin6c527842014-02-09 04:34:54 -0800738 /* #11.5.6 -> #11.3 */
Lev Walkin58b74eb2014-02-10 09:24:39 -0800739 ASN_DEBUG("Encoding integer %ld (%lu) with range %d bits",
740 value, value - ct->lower_bound, ct->range_bits);
Jaroslav Imrich2253e6b2014-10-31 23:05:21 +0100741 v = value - ct->lower_bound;
Lev Walkin58b74eb2014-02-10 09:24:39 -0800742 if(uper_put_constrained_whole_number_u(po, v, ct->range_bits))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700743 ASN__ENCODE_FAILED;
744 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000745 }
746
747 if(ct && ct->lower_bound) {
748 ASN_DEBUG("Adjust lower bound to %ld", ct->lower_bound);
749 /* TODO: adjust lower bound */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700750 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000751 }
752
753 for(buf = st->buf, end = st->buf + st->size; buf < end;) {
754 ssize_t mayEncode = uper_put_length(po, end - buf);
755 if(mayEncode < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700756 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +0000757 if(per_put_many_bits(po, buf, 8 * mayEncode))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700758 ASN__ENCODE_FAILED;
Lev Walkin53827fd2006-09-08 01:56:32 +0000759 buf += mayEncode;
Lev Walkin523de9e2006-08-18 01:34:18 +0000760 }
761
Lev Walkin7c1dc052016-03-14 03:08:15 -0700762 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +0000763}
764
Lev Walkind7703cf2012-09-03 01:45:03 -0700765#endif /* ASN_DISABLE_PER_SUPPORT */
766
Lev Walkinf15320b2004-06-03 03:38:44 +0000767int
Lev Walkin5e033762004-09-29 13:26:15 +0000768asn_INTEGER2long(const INTEGER_t *iptr, long *lptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000769 uint8_t *b, *end;
770 size_t size;
771 long l;
772
773 /* Sanity checking */
774 if(!iptr || !iptr->buf || !lptr) {
775 errno = EINVAL;
776 return -1;
777 }
778
779 /* Cache the begin/end of the buffer */
780 b = iptr->buf; /* Start of the INTEGER buffer */
781 size = iptr->size;
782 end = b + size; /* Where to stop */
783
784 if(size > sizeof(long)) {
785 uint8_t *end1 = end - 1;
786 /*
787 * Slightly more advanced processing,
788 * able to >sizeof(long) bytes,
789 * when the actual value is small
790 * (0x0000000000abcdef would yield a fine 0x00abcdef)
791 */
792 /* Skip out the insignificant leading bytes */
793 for(; b < end1; b++) {
794 switch(*b) {
795 case 0x00: if((b[1] & 0x80) == 0) continue; break;
796 case 0xff: if((b[1] & 0x80) != 0) continue; break;
797 }
798 break;
799 }
800
801 size = end - b;
802 if(size > sizeof(long)) {
803 /* Still cannot fit the long */
804 errno = ERANGE;
805 return -1;
806 }
807 }
808
809 /* Shortcut processing of a corner case */
810 if(end == b) {
811 *lptr = 0;
812 return 0;
813 }
814
815 /* Perform the sign initialization */
816 /* Actually l = -(*b >> 7); gains nothing, yet unreadable! */
817 if((*b >> 7)) l = -1; else l = 0;
818
819 /* Conversion engine */
820 for(; b < end; b++)
821 l = (l << 8) | *b;
822
823 *lptr = l;
824 return 0;
825}
Lev Walkind703ff42004-10-21 11:21:25 +0000826
827int
Lev Walkin5c879db2007-11-06 06:23:31 +0000828asn_INTEGER2ulong(const INTEGER_t *iptr, unsigned long *lptr) {
829 uint8_t *b, *end;
830 unsigned long l;
831 size_t size;
832
833 if(!iptr || !iptr->buf || !lptr) {
834 errno = EINVAL;
835 return -1;
836 }
837
838 b = iptr->buf;
839 size = iptr->size;
840 end = b + size;
841
842 /* If all extra leading bytes are zeroes, ignore them */
843 for(; size > sizeof(unsigned long); b++, size--) {
844 if(*b) {
845 /* Value won't fit unsigned long */
846 errno = ERANGE;
847 return -1;
848 }
849 }
850
851 /* Conversion engine */
852 for(l = 0; b < end; b++)
853 l = (l << 8) | *b;
854
855 *lptr = l;
856 return 0;
857}
858
859int
860asn_ulong2INTEGER(INTEGER_t *st, unsigned long value) {
861 uint8_t *buf;
862 uint8_t *end;
863 uint8_t *b;
864 int shr;
865
866 if(value <= LONG_MAX)
867 return asn_long2INTEGER(st, value);
868
869 buf = (uint8_t *)MALLOC(1 + sizeof(value));
870 if(!buf) return -1;
871
872 end = buf + (sizeof(value) + 1);
873 buf[0] = 0;
Lev Walkinb2bfca52008-11-20 05:15:11 +0000874 for(b = buf + 1, shr = (sizeof(long)-1)*8; b < end; shr -= 8, b++)
875 *b = (uint8_t)(value >> shr);
Lev Walkin5c879db2007-11-06 06:23:31 +0000876
877 if(st->buf) FREEMEM(st->buf);
878 st->buf = buf;
879 st->size = 1 + sizeof(value);
880
881 return 0;
882}
883
884int
Lev Walkind703ff42004-10-21 11:21:25 +0000885asn_long2INTEGER(INTEGER_t *st, long value) {
886 uint8_t *buf, *bp;
887 uint8_t *p;
888 uint8_t *pstart;
889 uint8_t *pend1;
Lev Walkind7ad5612004-10-26 08:20:46 +0000890 int littleEndian = 1; /* Run-time detection */
891 int add;
Lev Walkind703ff42004-10-21 11:21:25 +0000892
893 if(!st) {
894 errno = EINVAL;
895 return -1;
896 }
897
Lev Walkin8471cec2004-10-21 14:02:19 +0000898 buf = (uint8_t *)MALLOC(sizeof(value));
Lev Walkind703ff42004-10-21 11:21:25 +0000899 if(!buf) return -1;
900
Lev Walkind7ad5612004-10-26 08:20:46 +0000901 if(*(char *)&littleEndian) {
902 pstart = (uint8_t *)&value + sizeof(value) - 1;
903 pend1 = (uint8_t *)&value;
904 add = -1;
905 } else {
906 pstart = (uint8_t *)&value;
907 pend1 = pstart + sizeof(value) - 1;
908 add = 1;
909 }
910
Lev Walkind703ff42004-10-21 11:21:25 +0000911 /*
912 * If the contents octet consists of more than one octet,
913 * then bits of the first octet and bit 8 of the second octet:
914 * a) shall not all be ones; and
915 * b) shall not all be zero.
916 */
Lev Walkin33700162004-10-26 09:03:31 +0000917 for(p = pstart; p != pend1; p += add) {
Lev Walkind703ff42004-10-21 11:21:25 +0000918 switch(*p) {
Lev Walkin33700162004-10-26 09:03:31 +0000919 case 0x00: if((*(p+add) & 0x80) == 0)
Lev Walkind703ff42004-10-21 11:21:25 +0000920 continue;
921 break;
Lev Walkin33700162004-10-26 09:03:31 +0000922 case 0xff: if((*(p+add) & 0x80))
Lev Walkind703ff42004-10-21 11:21:25 +0000923 continue;
924 break;
925 }
926 break;
927 }
928 /* Copy the integer body */
Lev Walkin33700162004-10-26 09:03:31 +0000929 for(pstart = p, bp = buf, pend1 += add; p != pend1; p += add)
930 *bp++ = *p;
Lev Walkind703ff42004-10-21 11:21:25 +0000931
932 if(st->buf) FREEMEM(st->buf);
933 st->buf = buf;
Lev Walkin33700162004-10-26 09:03:31 +0000934 st->size = bp - buf;
Lev Walkind703ff42004-10-21 11:21:25 +0000935
936 return 0;
937}
Lev Walkinb3751942012-09-02 19:36:47 -0700938
Lev Walkincad560a2013-03-16 07:00:58 -0700939/*
940 * This function is going to be DEPRECATED soon.
941 */
Lev Walkine09f9f12012-09-02 23:06:35 -0700942enum asn_strtol_result_e
943asn_strtol(const char *str, const char *end, long *lp) {
Lev Walkincad560a2013-03-16 07:00:58 -0700944 const char *endp = end;
945
946 switch(asn_strtol_lim(str, &endp, lp)) {
947 case ASN_STRTOL_ERROR_RANGE:
948 return ASN_STRTOL_ERROR_RANGE;
949 case ASN_STRTOL_ERROR_INVAL:
950 return ASN_STRTOL_ERROR_INVAL;
951 case ASN_STRTOL_EXPECT_MORE:
952 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
953 case ASN_STRTOL_OK:
954 return ASN_STRTOL_OK;
955 case ASN_STRTOL_EXTRA_DATA:
956 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
957 }
958
959 return ASN_STRTOL_ERROR_INVAL; /* Retain old behavior */
960}
961
962/*
963 * Parse the number in the given string until the given *end position,
964 * returning the position after the last parsed character back using the
965 * same (*end) pointer.
966 * WARNING: This behavior is different from the standard strtol(3).
967 */
968enum asn_strtol_result_e
969asn_strtol_lim(const char *str, const char **end, long *lp) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700970 int sign = 1;
971 long l;
Lev Walkinb3751942012-09-02 19:36:47 -0700972
Lev Walkine09f9f12012-09-02 23:06:35 -0700973 const long upper_boundary = LONG_MAX / 10;
974 long last_digit_max = LONG_MAX % 10;
975
Lev Walkincad560a2013-03-16 07:00:58 -0700976 if(str >= *end) return ASN_STRTOL_ERROR_INVAL;
Lev Walkine09f9f12012-09-02 23:06:35 -0700977
978 switch(*str) {
979 case '-':
980 last_digit_max++;
981 sign = -1;
Simo Sorce4f47bf52015-09-03 17:35:04 -0400982 /* FALL THROUGH */
Lev Walkine09f9f12012-09-02 23:06:35 -0700983 case '+':
984 str++;
Lev Walkincad560a2013-03-16 07:00:58 -0700985 if(str >= *end) {
986 *end = str;
987 return ASN_STRTOL_EXPECT_MORE;
988 }
Lev Walkine09f9f12012-09-02 23:06:35 -0700989 }
990
Lev Walkincad560a2013-03-16 07:00:58 -0700991 for(l = 0; str < (*end); str++) {
Lev Walkine09f9f12012-09-02 23:06:35 -0700992 switch(*str) {
993 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
994 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: {
995 int d = *str - '0';
996 if(l < upper_boundary) {
997 l = l * 10 + d;
998 } else if(l == upper_boundary) {
999 if(d <= last_digit_max) {
1000 if(sign > 0) {
1001 l = l * 10 + d;
1002 } else {
1003 sign = 1;
1004 l = -l * 10 - d;
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 } else {
Lev Walkincad560a2013-03-16 07:00:58 -07001011 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001012 return ASN_STRTOL_ERROR_RANGE;
1013 }
1014 }
1015 continue;
1016 default:
Lev Walkincad560a2013-03-16 07:00:58 -07001017 *end = str;
1018 *lp = sign * l;
1019 return ASN_STRTOL_EXTRA_DATA;
Lev Walkine09f9f12012-09-02 23:06:35 -07001020 }
1021 }
1022
Lev Walkincad560a2013-03-16 07:00:58 -07001023 *end = str;
Lev Walkine09f9f12012-09-02 23:06:35 -07001024 *lp = sign * l;
1025 return ASN_STRTOL_OK;
Lev Walkinb3751942012-09-02 19:36:47 -07001026}
1027