2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
12 const asn_INTEGER_enum_map_t *vemap;
13 const unsigned int *evmap;
16 INTEGER__compar_enum2value(const void *kp, const void *am) {
17 const struct e2v_key *key = (const struct e2v_key *)kp;
18 const asn_INTEGER_enum_map_t *el = (const asn_INTEGER_enum_map_t *)am;
19 const char *ptr, *end, *name;
21 /* Remap the element (sort by different criterion) */
22 el = key->vemap + key->evmap[el - key->vemap];
25 for(ptr = key->start, end = key->stop, name = el->enum_name;
26 ptr < end; ptr++, name++) {
27 if(*ptr != *name || !*name)
28 return *(const unsigned char *)ptr - *(const unsigned char *)name;
30 return name[0] ? -1 : 0;
33 static const asn_INTEGER_enum_map_t *
34 INTEGER_map_enum2value(const asn_INTEGER_specifics_t *specs, const char *lstart,
36 const asn_INTEGER_enum_map_t *el_found;
37 int count = specs ? specs->map_count : 0;
41 if(!count) return NULL;
43 /* Guaranteed: assert(lstart < lstop); */
44 /* Figure out the tag name */
45 for(lstart++, lp = lstart; lp < lstop; lp++) {
47 case 9: case 10: case 11: case 12: case 13: case 32: /* WSP */
48 case 0x2f: /* '/' */ case 0x3e: /* '>' */
55 if(lp == lstop) return NULL; /* No tag found */
60 key.vemap = specs->value2enum;
61 key.evmap = specs->enum2value;
62 el_found = (asn_INTEGER_enum_map_t *)bsearch(&key,
63 specs->value2enum, count, sizeof(specs->value2enum[0]),
64 INTEGER__compar_enum2value);
66 /* Remap enum2value into value2enum */
67 el_found = key.vemap + key.evmap[el_found - key.vemap];
73 INTEGER_st_prealloc(INTEGER_t *st, int min_size) {
74 void *p = MALLOC(min_size + 1);
87 * Decode the chunk of XML text encoding INTEGER.
89 static enum xer_pbd_rval
90 INTEGER__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
91 const void *chunk_buf, size_t chunk_size) {
92 const asn_INTEGER_specifics_t *specs =
93 (const asn_INTEGER_specifics_t *)td->specifics;
94 INTEGER_t *st = (INTEGER_t *)sptr;
96 intmax_t hex_value = 0;
98 const char *lstart = (const char *)chunk_buf;
99 const char *lstop = lstart + chunk_size;
105 ST_DIGITS_TRAILSPACE,
108 ST_HEXDIGITS_TRAILSPACE,
112 } state = ST_LEADSPACE;
113 const char *dec_value_start = 0; /* INVARIANT: always !0 in ST_DIGITS */
114 const char *dec_value_end = 0;
117 ASN_DEBUG("INTEGER body %ld 0x%2x..0x%2x",
118 (long)chunk_size, *lstart, lstop[-1]);
120 if(INTEGER_st_prealloc(st, (chunk_size/3) + 1))
121 return XPBD_SYSTEM_FAILURE;
124 * We may have received a tag here. It will be processed inline.
125 * Use strtoul()-like code and serialize the result.
127 for(lp = lstart; lp < lstop; lp++) {
130 case 0x09: case 0x0a: case 0x0d: case 0x20:
133 case ST_DIGITS_TRAILSPACE:
134 case ST_HEXDIGITS_TRAILSPACE:
139 state = ST_DIGITS_TRAILSPACE;
142 state = ST_HEXDIGITS_TRAILSPACE;
149 if(state == ST_LEADSPACE) {
151 dec_value_start = lp;
152 state = ST_WAITDIGITS;
157 if(state == ST_LEADSPACE) {
159 dec_value_start = lp;
160 state = ST_WAITDIGITS;
164 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
165 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
167 case ST_DIGITS: continue;
168 case ST_SKIPSPHEX: /* Fall through */
170 hex_value = (lv - 0x30) << 4;
171 state = ST_HEXDIGIT2;
174 hex_value += (lv - 0x30);
176 st->buf[st->size++] = (uint8_t)hex_value;
179 return XPBD_BROKEN_ENCODING;
182 dec_value_start = lp;
191 case 0x3c: /* '<', start of XML encoded enumeration */
192 if(state == ST_LEADSPACE) {
193 const asn_INTEGER_enum_map_t *el;
194 el = INTEGER_map_enum2value(
195 (const asn_INTEGER_specifics_t *)
196 td->specifics, lstart, lstop);
198 ASN_DEBUG("Found \"%s\" => %ld",
199 el->enum_name, el->nat_value);
200 dec_value = el->nat_value;
205 ASN_DEBUG("Unknown identifier for INTEGER");
207 return XPBD_BROKEN_ENCODING;
209 if(state == ST_HEXCOLON) {
210 /* This colon is expected */
211 state = ST_HEXDIGIT1;
213 } else if(state == ST_DIGITS) {
214 /* The colon here means that we have
215 * decoded the first two hexadecimal
216 * places as a decimal value.
217 * Switch decoding mode. */
218 ASN_DEBUG("INTEGER re-evaluate as hex form");
219 state = ST_SKIPSPHEX;
224 ASN_DEBUG("state %d at %ld", state, (long)(lp - lstart));
228 case 0x41:case 0x42:case 0x43:case 0x44:case 0x45:case 0x46:
229 case 0x61:case 0x62:case 0x63:case 0x64:case 0x65:case 0x66:
232 case ST_LEADSPACE: /* Fall through */
234 hex_value = lv - ((lv < 0x61) ? 0x41 : 0x61);
237 state = ST_HEXDIGIT2;
240 hex_value += lv - ((lv < 0x61) ? 0x41 : 0x61);
242 st->buf[st->size++] = (uint8_t)hex_value;
246 ASN_DEBUG("INTEGER re-evaluate as hex form");
247 state = ST_SKIPSPHEX;
257 /* Found extra non-numeric stuff */
258 ASN_DEBUG("INTEGER :: Found non-numeric 0x%2x at %ld",
259 lv, (long)(lp - lstart));
260 state = ST_UNEXPECTED;
266 /* Got a complete and valid enumeration encoded as a tag. */
269 dec_value_end = lstop;
271 case ST_DIGITS_TRAILSPACE:
272 /* The last symbol encountered was a digit. */
273 switch(asn_strtoimax_lim(dec_value_start, &dec_value_end, &dec_value)) {
275 if(specs && specs->field_unsigned && (uintmax_t) dec_value <= ULONG_MAX) {
277 } else if(dec_value >= LONG_MIN && dec_value <= LONG_MAX) {
281 * We model INTEGER on long for XER,
282 * to avoid rewriting all the tests at once.
284 ASN_DEBUG("INTEGER exceeds long range");
287 case ASN_STRTOX_ERROR_RANGE:
288 ASN_DEBUG("INTEGER decode %s hit range limit", td->name);
289 return XPBD_DECODER_LIMIT;
290 case ASN_STRTOX_ERROR_INVAL:
291 case ASN_STRTOX_EXPECT_MORE:
292 case ASN_STRTOX_EXTRA_DATA:
293 return XPBD_BROKEN_ENCODING;
297 case ST_HEXDIGITS_TRAILSPACE:
298 st->buf[st->size] = 0; /* Just in case termination */
299 return XPBD_BODY_CONSUMED;
303 return XPBD_BROKEN_ENCODING;
305 /* Content not found */
306 return XPBD_NOT_BODY_IGNORE;
309 ASN_DEBUG("INTEGER: No useful digits (state %d)", state);
310 return XPBD_BROKEN_ENCODING; /* No digits */
314 * Convert the result of parsing of enumeration or a straight
315 * decimal value into a BER representation.
317 if(asn_imax2INTEGER(st, dec_value)) {
318 ASN_DEBUG("INTEGER decode %s conversion failed", td->name);
319 return XPBD_SYSTEM_FAILURE;
322 return XPBD_BODY_CONSUMED;
326 INTEGER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
327 const asn_TYPE_descriptor_t *td, void **sptr,
328 const char *opt_mname, const void *buf_ptr, size_t size) {
329 return xer_decode_primitive(opt_codec_ctx, td,
330 sptr, sizeof(INTEGER_t), opt_mname,
331 buf_ptr, size, INTEGER__xer_body_decode);
335 INTEGER_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
336 int ilevel, enum xer_encoder_flags_e flags,
337 asn_app_consume_bytes_f *cb, void *app_key) {
338 const INTEGER_t *st = (const INTEGER_t *)sptr;
339 asn_enc_rval_t er = {0,0,0};
347 er.encoded = INTEGER__dump(td, st, cb, app_key, 1);
348 if(er.encoded < 0) ASN__ENCODE_FAILED;