blob: 316e8720a0afff1b089b6690d831c02eca4a9f1c [file] [log] [blame]
Peter Szilagyifbc56f92019-07-23 19:29:46 +00001/*-
2 * Copyright (c) 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
5 */
6/*
7 * Read the NativeInteger.h for the explanation wrt. differences between
8 * INTEGER and NativeInteger.
9 * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this
10 * implementation deals with the standard (machine-specific) representation
11 * of them instead of using the platform-independent buffer.
12 */
13#include <asn_internal.h>
14#include <NativeInteger.h>
15
16/*
17 * NativeInteger basic type description.
18 */
19static const ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
20 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
21};
22asn_TYPE_operation_t asn_OP_NativeInteger = {
23 NativeInteger_free,
24 NativeInteger_print,
25 NativeInteger_compare,
26 NativeInteger_decode_ber,
27 NativeInteger_encode_der,
28 NativeInteger_decode_xer,
29 NativeInteger_encode_xer,
30#ifdef ASN_DISABLE_OER_SUPPORT
31 0,
32 0,
33#else
34 NativeInteger_decode_oer, /* OER decoder */
35 NativeInteger_encode_oer, /* Canonical OER encoder */
36#endif /* ASN_DISABLE_OER_SUPPORT */
37#ifdef ASN_DISABLE_PER_SUPPORT
38 0,
39 0,
40 0,
41 0,
42#else
43 NativeInteger_decode_uper, /* Unaligned PER decoder */
44 NativeInteger_encode_uper, /* Unaligned PER encoder */
45 NativeInteger_decode_aper, /* Aligned PER decoder */
46 NativeInteger_encode_aper, /* Aligned PER encoder */
47#endif /* ASN_DISABLE_PER_SUPPORT */
48 NativeInteger_random_fill,
49 0 /* Use generic outmost tag fetcher */
50};
51asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
52 "INTEGER", /* The ASN.1 type is still INTEGER */
53 "INTEGER",
54 &asn_OP_NativeInteger,
55 asn_DEF_NativeInteger_tags,
56 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
57 asn_DEF_NativeInteger_tags, /* Same as above */
58 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
59 { 0, 0, asn_generic_no_constraint },
60 0, 0, /* No members */
61 0 /* No specifics */
62};
63
64/*
65 * Decode INTEGER type.
66 */
67asn_dec_rval_t
68NativeInteger_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
69 const asn_TYPE_descriptor_t *td, void **nint_ptr,
70 const void *buf_ptr, size_t size, int tag_mode) {
71 const asn_INTEGER_specifics_t *specs =
72 (const asn_INTEGER_specifics_t *)td->specifics;
73 long *native = (long *)*nint_ptr;
74 asn_dec_rval_t rval;
75 ber_tlv_len_t length;
76
77 /*
78 * If the structure is not there, allocate it.
79 */
80 if(native == NULL) {
81 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
82 if(native == NULL) {
83 rval.code = RC_FAIL;
84 rval.consumed = 0;
85 return rval;
86 }
87 }
88
89 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
90 td->name, tag_mode);
91
92 /*
93 * Check tags.
94 */
95 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
96 tag_mode, 0, &length, 0);
97 if(rval.code != RC_OK)
98 return rval;
99
100 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
101
102 /*
103 * Make sure we have this length.
104 */
105 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
106 size -= rval.consumed;
107 if(length > (ber_tlv_len_t)size) {
108 rval.code = RC_WMORE;
109 rval.consumed = 0;
110 return rval;
111 }
112
113 /*
114 * ASN.1 encoded INTEGER: buf_ptr, length
115 * Fill the native, at the same time checking for overflow.
116 * If overflow occured, return with RC_FAIL.
117 */
118 {
119 INTEGER_t tmp;
120 union {
121 const void *constbuf;
122 void *nonconstbuf;
123 } unconst_buf;
124 long l;
125
126 unconst_buf.constbuf = buf_ptr;
127 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
128 tmp.size = length;
129
130 if((specs&&specs->field_unsigned)
131 ? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */
132 : asn_INTEGER2long(&tmp, &l)) {
133 rval.code = RC_FAIL;
134 rval.consumed = 0;
135 return rval;
136 }
137
138 *native = l;
139 }
140
141 rval.code = RC_OK;
142 rval.consumed += length;
143
144 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
145 (long)rval.consumed, (long)length, td->name, (long)*native);
146
147 return rval;
148}
149
150/*
151 * Encode the NativeInteger using the standard INTEGER type DER encoder.
152 */
153asn_enc_rval_t
154NativeInteger_encode_der(const asn_TYPE_descriptor_t *sd, const void *ptr,
155 int tag_mode, ber_tlv_tag_t tag,
156 asn_app_consume_bytes_f *cb, void *app_key) {
157 unsigned long native = *(const unsigned long *)ptr; /* Disable sign ext. */
158 asn_enc_rval_t erval = {0,0,0};
159 INTEGER_t tmp;
160
161#ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
162
163 tmp.buf = (uint8_t *)&native;
164 tmp.size = sizeof(native);
165
166#else /* Works even if WORDS_BIGENDIAN is not set where should've been */
167 uint8_t buf[sizeof(native)];
168 uint8_t *p;
169
170 /* Prepare a fake INTEGER */
171 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
172 *p = (uint8_t)native;
173
174 tmp.buf = buf;
175 tmp.size = sizeof(buf);
176#endif /* WORDS_BIGENDIAN */
177
178 /* Encode fake INTEGER */
179 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
180 if(erval.structure_ptr == &tmp) {
181 erval.structure_ptr = ptr;
182 }
183 return erval;
184}
185
186/*
187 * Decode the chunk of XML text encoding INTEGER.
188 */
189asn_dec_rval_t
190NativeInteger_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
191 const asn_TYPE_descriptor_t *td, void **sptr,
192 const char *opt_mname, const void *buf_ptr,
193 size_t size) {
194 const asn_INTEGER_specifics_t *specs =
195 (const asn_INTEGER_specifics_t *)td->specifics;
196 asn_dec_rval_t rval;
197 INTEGER_t st;
198 void *st_ptr = (void *)&st;
199 long *native = (long *)*sptr;
200
201 if(!native) {
202 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
203 if(!native) ASN__DECODE_FAILED;
204 }
205
206 memset(&st, 0, sizeof(st));
207 rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr,
208 opt_mname, buf_ptr, size);
209 if(rval.code == RC_OK) {
210 long l;
211 if((specs&&specs->field_unsigned)
212 ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
213 : asn_INTEGER2long(&st, &l)) {
214 rval.code = RC_FAIL;
215 rval.consumed = 0;
216 } else {
217 *native = l;
218 }
219 } else {
220 /*
221 * Cannot restart from the middle;
222 * there is no place to save state in the native type.
223 * Request a continuation from the very beginning.
224 */
225 rval.consumed = 0;
226 }
227 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
228 return rval;
229}
230
231
232asn_enc_rval_t
233NativeInteger_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
234 int ilevel, enum xer_encoder_flags_e flags,
235 asn_app_consume_bytes_f *cb, void *app_key) {
236 const asn_INTEGER_specifics_t *specs =
237 (const asn_INTEGER_specifics_t *)td->specifics;
238 char scratch[32]; /* Enough for 64-bit int */
239 asn_enc_rval_t er = {0,0,0};
240 const long *native = (const long *)sptr;
241
242 (void)ilevel;
243 (void)flags;
244
245 if(!native) ASN__ENCODE_FAILED;
246
247 er.encoded = snprintf(scratch, sizeof(scratch),
248 (specs && specs->field_unsigned)
249 ? "%lu" : "%ld", *native);
250 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
251 || cb(scratch, er.encoded, app_key) < 0)
252 ASN__ENCODE_FAILED;
253
254 ASN__ENCODED_OK(er);
255}
256
257#ifndef ASN_DISABLE_PER_SUPPORT
258
259asn_dec_rval_t
260NativeInteger_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
261 const asn_TYPE_descriptor_t *td,
262 const asn_per_constraints_t *constraints, void **sptr,
263 asn_per_data_t *pd) {
264 const asn_INTEGER_specifics_t *specs =
265 (const asn_INTEGER_specifics_t *)td->specifics;
266 asn_dec_rval_t rval;
267 long *native = (long *)*sptr;
268 INTEGER_t tmpint;
269 void *tmpintptr = &tmpint;
270
271 (void)opt_codec_ctx;
272 ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name);
273
274 if(!native) {
275 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
276 if(!native) ASN__DECODE_FAILED;
277 }
278
279 memset(&tmpint, 0, sizeof tmpint);
280 rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints,
281 &tmpintptr, pd);
282 if(rval.code == RC_OK) {
283 if((specs&&specs->field_unsigned)
284 ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
285 : asn_INTEGER2long(&tmpint, native))
286 rval.code = RC_FAIL;
287 else
288 ASN_DEBUG("NativeInteger %s got value %ld",
289 td->name, *native);
290 }
291 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
292
293 return rval;
294}
295
296asn_enc_rval_t
297NativeInteger_encode_uper(const asn_TYPE_descriptor_t *td,
298 const asn_per_constraints_t *constraints,
299 const void *sptr, asn_per_outp_t *po) {
300 const asn_INTEGER_specifics_t *specs =
301 (const asn_INTEGER_specifics_t *)td->specifics;
302 asn_enc_rval_t er = {0,0,0};
303 long native;
304 INTEGER_t tmpint;
305
306 if(!sptr) ASN__ENCODE_FAILED;
307
308 native = *(const long *)sptr;
309
310 ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native);
311
312 memset(&tmpint, 0, sizeof(tmpint));
313 if((specs&&specs->field_unsigned)
314 ? asn_ulong2INTEGER(&tmpint, native)
315 : asn_long2INTEGER(&tmpint, native))
316 ASN__ENCODE_FAILED;
317 er = INTEGER_encode_uper(td, constraints, &tmpint, po);
318 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
319 return er;
320}
321
322asn_dec_rval_t
323NativeInteger_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
324 const asn_TYPE_descriptor_t *td,
325 const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
326
327 const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics;
328 asn_dec_rval_t rval;
329 long *native = (long *)*sptr;
330 INTEGER_t tmpint;
331 void *tmpintptr = &tmpint;
332
333 (void)opt_codec_ctx;
334 ASN_DEBUG("Decoding NativeInteger %s (APER)", td->name);
335
336 if(!native) {
337 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
338 if(!native) ASN__DECODE_FAILED;
339 }
340
341 memset(&tmpint, 0, sizeof tmpint);
342 rval = INTEGER_decode_aper(opt_codec_ctx, td, constraints,
343 &tmpintptr, pd);
344 if(rval.code == RC_OK) {
345 if((specs&&specs->field_unsigned)
346 ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
347 : asn_INTEGER2long(&tmpint, native))
348 rval.code = RC_FAIL;
349 else
350 ASN_DEBUG("NativeInteger %s got value %ld",
351 td->name, *native);
352 }
353 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
354
355 return rval;
356}
357
358asn_enc_rval_t
359NativeInteger_encode_aper(const asn_TYPE_descriptor_t *td,
360 const asn_per_constraints_t *constraints,
361 const void *sptr, asn_per_outp_t *po) {
362
363 const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics;
364 asn_enc_rval_t er = {0,0,0};
365 long native;
366 INTEGER_t tmpint;
367
368 if(!sptr) ASN__ENCODE_FAILED;
369
370 native = *(const long *)sptr;
371
372 ASN_DEBUG("Encoding NativeInteger %s %ld (APER)", td->name, native);
373
374 memset(&tmpint, 0, sizeof(tmpint));
375 if((specs&&specs->field_unsigned)
376 ? asn_ulong2INTEGER(&tmpint, (unsigned long)native)
377 : asn_long2INTEGER(&tmpint, native))
378 ASN__ENCODE_FAILED;
379 er = INTEGER_encode_aper(td, constraints, &tmpint, po);
380 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
381 return er;
382}
383
384#endif /* ASN_DISABLE_PER_SUPPORT */
385
386/*
387 * INTEGER specific human-readable output.
388 */
389int
390NativeInteger_print(const asn_TYPE_descriptor_t *td, const void *sptr,
391 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
392 const asn_INTEGER_specifics_t *specs =
393 (const asn_INTEGER_specifics_t *)td->specifics;
394 const long *native = (const long *)sptr;
395 char scratch[32]; /* Enough for 64-bit int */
396 int ret;
397
398 (void)td; /* Unused argument */
399 (void)ilevel; /* Unused argument */
400
401 if(native) {
402 long value = *native;
403 ret = snprintf(scratch, sizeof(scratch),
404 (specs && specs->field_unsigned) ? "%lu" : "%ld", value);
405 assert(ret > 0 && (size_t)ret < sizeof(scratch));
406 if(cb(scratch, ret, app_key) < 0) return -1;
407 if(specs && (value >= 0 || !specs->field_unsigned)) {
408 const asn_INTEGER_enum_map_t *el =
409 INTEGER_map_value2enum(specs, value);
410 if(el) {
411 if(cb(" (", 2, app_key) < 0) return -1;
412 if(cb(el->enum_name, el->enum_len, app_key) < 0) return -1;
413 if(cb(")", 1, app_key) < 0) return -1;
414 }
415 }
416 return 0;
417 } else {
418 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
419 }
420}
421
422void
423NativeInteger_free(const asn_TYPE_descriptor_t *td, void *ptr,
424 enum asn_struct_free_method method) {
425 if(!td || !ptr)
426 return;
427
428 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
429 td->name, method, ptr);
430
431 switch(method) {
432 case ASFM_FREE_EVERYTHING:
433 FREEMEM(ptr);
434 break;
435 case ASFM_FREE_UNDERLYING:
436 break;
437 case ASFM_FREE_UNDERLYING_AND_RESET:
438 memset(ptr, 0, sizeof(long));
439 break;
440 }
441}
442
443int
444NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
445 (void)td;
446
447 if(aptr && bptr) {
448 const asn_INTEGER_specifics_t *specs =
449 (const asn_INTEGER_specifics_t *)td->specifics;
450 if(specs && specs->field_unsigned) {
451 const unsigned long *a = aptr;
452 const unsigned long *b = bptr;
453 if(*a < *b) {
454 return -1;
455 } else if(*a > *b) {
456 return 1;
457 } else {
458 return 0;
459 }
460 } else {
461 const long *a = aptr;
462 const long *b = bptr;
463 if(*a < *b) {
464 return -1;
465 } else if(*a > *b) {
466 return 1;
467 } else {
468 return 0;
469 }
470 }
471 } else if(!aptr) {
472 return -1;
473 } else {
474 return 1;
475 }
476}
477
478asn_random_fill_result_t
479NativeInteger_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
480 const asn_encoding_constraints_t *constraints,
481 size_t max_length) {
482 const asn_INTEGER_specifics_t *specs =
483 (const asn_INTEGER_specifics_t *)td->specifics;
484 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
485 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
486 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
487 long *st = *sptr;
488 const asn_INTEGER_enum_map_t *emap;
489 size_t emap_len;
490 intmax_t value;
491 int find_inside_map;
492
493 if(max_length == 0) return result_skipped;
494
495 if(st == NULL) {
496 st = (long *)CALLOC(1, sizeof(*st));
497 if(st == NULL) {
498 return result_failed;
499 }
500 }
501
502 if(specs) {
503 emap = specs->value2enum;
504 emap_len = specs->map_count;
505 if(specs->strict_enumeration) {
506 find_inside_map = emap_len > 0;
507 } else {
508 find_inside_map = emap_len ? asn_random_between(0, 1) : 0;
509 }
510 } else {
511 emap = 0;
512 emap_len = 0;
513 find_inside_map = 0;
514 }
515
516 if(find_inside_map) {
517 assert(emap_len > 0);
518 value = emap[asn_random_between(0, emap_len - 1)].nat_value;
519 } else {
520 const asn_per_constraints_t *ct;
521
522 static const long variants[] = {
523 -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384,
524 -16383, -257, -256, -255, -254, -129, -128, -127,
525 -126, -1, 0, 1, 126, 127, 128, 129,
526 254, 255, 256, 257, 16383, 16384, 16385, 32767,
527 32768, 32769, 65534, 65535, 65536, 65537};
528 if(specs && specs->field_unsigned) {
529 assert(variants[18] == 0);
530 value = variants[asn_random_between(
531 18, sizeof(variants) / sizeof(variants[0]) - 1)];
532 } else {
533 value = variants[asn_random_between(
534 0, sizeof(variants) / sizeof(variants[0]) - 1)];
535 }
536
537 if(!constraints) constraints = &td->encoding_constraints;
538 ct = constraints ? constraints->per_constraints : 0;
539 if(ct && (ct->value.flags & APC_CONSTRAINED)) {
540 if(value < ct->value.lower_bound || value > ct->value.upper_bound) {
541 value = asn_random_between(ct->value.lower_bound,
542 ct->value.upper_bound);
543 }
544 }
545 }
546
547 *sptr = st;
548 *st = value;
549 return result_ok;
550}