2 * Copyright (c) 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
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.
13 #include <asn_internal.h>
14 #include <NativeInteger.h>
17 * NativeInteger basic type description.
19 static const ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
20 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
22 asn_TYPE_operation_t asn_OP_NativeInteger = {
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
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
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 */
51 asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
52 "INTEGER", /* The ASN.1 type is still 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 */
65 * Decode INTEGER type.
68 NativeInteger_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;
78 * If the structure is not there, allocate it.
81 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
89 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
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)
100 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
103 * Make sure we have this length.
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;
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.
121 const void *constbuf;
126 unconst_buf.constbuf = buf_ptr;
127 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
130 if((specs&&specs->field_unsigned)
131 ? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */
132 : asn_INTEGER2long(&tmp, &l)) {
142 rval.consumed += length;
144 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
145 (long)rval.consumed, (long)length, td->name, (long)*native);
151 * Encode the NativeInteger using the standard INTEGER type DER encoder.
154 NativeInteger_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};
161 #ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
163 tmp.buf = (uint8_t *)&native;
164 tmp.size = sizeof(native);
166 #else /* Works even if WORDS_BIGENDIAN is not set where should've been */
167 uint8_t buf[sizeof(native)];
170 /* Prepare a fake INTEGER */
171 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
172 *p = (uint8_t)native;
175 tmp.size = sizeof(buf);
176 #endif /* WORDS_BIGENDIAN */
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;
187 * Decode the chunk of XML text encoding INTEGER.
190 NativeInteger_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,
194 const asn_INTEGER_specifics_t *specs =
195 (const asn_INTEGER_specifics_t *)td->specifics;
198 void *st_ptr = (void *)&st;
199 long *native = (long *)*sptr;
202 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
203 if(!native) ASN__DECODE_FAILED;
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) {
211 if((specs&&specs->field_unsigned)
212 ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
213 : asn_INTEGER2long(&st, &l)) {
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.
227 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
233 NativeInteger_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;
245 if(!native) ASN__ENCODE_FAILED;
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)
257 #ifndef ASN_DISABLE_PER_SUPPORT
260 NativeInteger_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;
267 long *native = (long *)*sptr;
269 void *tmpintptr = &tmpint;
272 ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name);
275 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
276 if(!native) ASN__DECODE_FAILED;
279 memset(&tmpint, 0, sizeof tmpint);
280 rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints,
282 if(rval.code == RC_OK) {
283 if((specs&&specs->field_unsigned)
284 ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
285 : asn_INTEGER2long(&tmpint, native))
288 ASN_DEBUG("NativeInteger %s got value %ld",
291 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
297 NativeInteger_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};
306 if(!sptr) ASN__ENCODE_FAILED;
308 native = *(const long *)sptr;
310 ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native);
312 memset(&tmpint, 0, sizeof(tmpint));
313 if((specs&&specs->field_unsigned)
314 ? asn_ulong2INTEGER(&tmpint, native)
315 : asn_long2INTEGER(&tmpint, native))
317 er = INTEGER_encode_uper(td, constraints, &tmpint, po);
318 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
323 NativeInteger_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) {
327 const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics;
329 long *native = (long *)*sptr;
331 void *tmpintptr = &tmpint;
334 ASN_DEBUG("Decoding NativeInteger %s (APER)", td->name);
337 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
338 if(!native) ASN__DECODE_FAILED;
341 memset(&tmpint, 0, sizeof tmpint);
342 rval = INTEGER_decode_aper(opt_codec_ctx, td, constraints,
344 if(rval.code == RC_OK) {
345 if((specs&&specs->field_unsigned)
346 ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
347 : asn_INTEGER2long(&tmpint, native))
350 ASN_DEBUG("NativeInteger %s got value %ld",
353 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
359 NativeInteger_encode_aper(const asn_TYPE_descriptor_t *td,
360 const asn_per_constraints_t *constraints,
361 const void *sptr, asn_per_outp_t *po) {
363 const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics;
364 asn_enc_rval_t er = {0,0,0};
368 if(!sptr) ASN__ENCODE_FAILED;
370 native = *(const long *)sptr;
372 ASN_DEBUG("Encoding NativeInteger %s %ld (APER)", td->name, native);
374 memset(&tmpint, 0, sizeof(tmpint));
375 if((specs&&specs->field_unsigned)
376 ? asn_ulong2INTEGER(&tmpint, (unsigned long)native)
377 : asn_long2INTEGER(&tmpint, native))
379 er = INTEGER_encode_aper(td, constraints, &tmpint, po);
380 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
384 #endif /* ASN_DISABLE_PER_SUPPORT */
387 * INTEGER specific human-readable output.
390 NativeInteger_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 */
398 (void)td; /* Unused argument */
399 (void)ilevel; /* Unused argument */
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);
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;
418 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
423 NativeInteger_free(const asn_TYPE_descriptor_t *td, void *ptr,
424 enum asn_struct_free_method method) {
428 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
429 td->name, method, ptr);
432 case ASFM_FREE_EVERYTHING:
435 case ASFM_FREE_UNDERLYING:
437 case ASFM_FREE_UNDERLYING_AND_RESET:
438 memset(ptr, 0, sizeof(long));
444 NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *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;
461 const long *a = aptr;
462 const long *b = bptr;
478 asn_random_fill_result_t
479 NativeInteger_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
480 const asn_encoding_constraints_t *constraints,
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};
488 const asn_INTEGER_enum_map_t *emap;
493 if(max_length == 0) return result_skipped;
496 st = (long *)CALLOC(1, sizeof(*st));
498 return result_failed;
503 emap = specs->value2enum;
504 emap_len = specs->map_count;
505 if(specs->strict_enumeration) {
506 find_inside_map = emap_len > 0;
508 find_inside_map = emap_len ? asn_random_between(0, 1) : 0;
516 if(find_inside_map) {
517 assert(emap_len > 0);
518 value = emap[asn_random_between(0, emap_len - 1)].nat_value;
520 const asn_per_constraints_t *ct;
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)];
533 value = variants[asn_random_between(
534 0, sizeof(variants) / sizeof(variants[0]) - 1)];
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);