3 * Copyright (c) 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
5 * Redistribution and modifications are permitted subject to BSD license.
8 * Read the NativeInteger.h for the explanation wrt. differences between
9 * INTEGER and NativeInteger.
10 * Basically, both are decoders and encoders of ASN.1 INTEGER type, but this
11 * implementation deals with the standard (machine-specific) representation
12 * of them instead of using the platform-independent buffer.
14 #include <asn_internal.h>
15 #include <NativeInteger.h>
18 * NativeInteger basic type description.
20 static const ber_tlv_tag_t asn_DEF_NativeInteger_tags[] = {
21 (ASN_TAG_CLASS_UNIVERSAL | (2 << 2))
23 asn_TYPE_operation_t asn_OP_NativeInteger = {
26 NativeInteger_compare,
27 NativeInteger_decode_ber,
28 NativeInteger_encode_der,
29 NativeInteger_decode_xer,
30 NativeInteger_encode_xer,
31 #ifdef ASN_DISABLE_OER_SUPPORT
35 NativeInteger_decode_oer, /* OER decoder */
36 NativeInteger_encode_oer, /* Canonical OER encoder */
37 #endif /* ASN_DISABLE_OER_SUPPORT */
38 #ifdef ASN_DISABLE_PER_SUPPORT
44 NativeInteger_decode_uper, /* Unaligned PER decoder */
45 NativeInteger_encode_uper, /* Unaligned PER encoder */
46 NativeInteger_decode_aper, /* Aligned PER decoder */
47 NativeInteger_encode_aper, /* Aligned PER encoder */
48 #endif /* ASN_DISABLE_PER_SUPPORT */
49 NativeInteger_random_fill,
50 0 /* Use generic outmost tag fetcher */
52 asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
53 "INTEGER", /* The ASN.1 type is still INTEGER */
55 &asn_OP_NativeInteger,
56 asn_DEF_NativeInteger_tags,
57 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
58 asn_DEF_NativeInteger_tags, /* Same as above */
59 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
60 { 0, 0, asn_generic_no_constraint },
61 0, 0, /* No members */
66 * Decode INTEGER type.
69 NativeInteger_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
70 const asn_TYPE_descriptor_t *td, void **nint_ptr,
71 const void *buf_ptr, size_t size, int tag_mode) {
72 const asn_INTEGER_specifics_t *specs =
73 (const asn_INTEGER_specifics_t *)td->specifics;
74 long *native = (long *)*nint_ptr;
79 * If the structure is not there, allocate it.
82 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
90 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
96 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
97 tag_mode, 0, &length, 0);
98 if(rval.code != RC_OK)
101 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
104 * Make sure we have this length.
106 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
107 size -= rval.consumed;
108 if(length > (ber_tlv_len_t)size) {
109 rval.code = RC_WMORE;
115 * ASN.1 encoded INTEGER: buf_ptr, length
116 * Fill the native, at the same time checking for overflow.
117 * If overflow occured, return with RC_FAIL.
122 const void *constbuf;
127 unconst_buf.constbuf = buf_ptr;
128 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
131 if((specs&&specs->field_unsigned)
132 ? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */
133 : asn_INTEGER2long(&tmp, &l)) {
143 rval.consumed += length;
145 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
146 (long)rval.consumed, (long)length, td->name, (long)*native);
152 * Encode the NativeInteger using the standard INTEGER type DER encoder.
155 NativeInteger_encode_der(const asn_TYPE_descriptor_t *sd, const void *ptr,
156 int tag_mode, ber_tlv_tag_t tag,
157 asn_app_consume_bytes_f *cb, void *app_key) {
158 unsigned long native = *(const unsigned long *)ptr; /* Disable sign ext. */
159 asn_enc_rval_t erval = {0,0,0};
162 #ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
164 tmp.buf = (uint8_t *)&native;
165 tmp.size = sizeof(native);
167 #else /* Works even if WORDS_BIGENDIAN is not set where should've been */
168 uint8_t buf[sizeof(native)];
171 /* Prepare a fake INTEGER */
172 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
173 *p = (uint8_t)native;
176 tmp.size = sizeof(buf);
177 #endif /* WORDS_BIGENDIAN */
179 /* Encode fake INTEGER */
180 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
181 if(erval.structure_ptr == &tmp) {
182 erval.structure_ptr = ptr;
188 * Decode the chunk of XML text encoding INTEGER.
191 NativeInteger_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
192 const asn_TYPE_descriptor_t *td, void **sptr,
193 const char *opt_mname, const void *buf_ptr,
195 const asn_INTEGER_specifics_t *specs =
196 (const asn_INTEGER_specifics_t *)td->specifics;
199 void *st_ptr = (void *)&st;
200 long *native = (long *)*sptr;
203 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
204 if(!native) ASN__DECODE_FAILED;
207 memset(&st, 0, sizeof(st));
208 rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr,
209 opt_mname, buf_ptr, size);
210 if(rval.code == RC_OK) {
212 if((specs&&specs->field_unsigned)
213 ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
214 : asn_INTEGER2long(&st, &l)) {
222 * Cannot restart from the middle;
223 * there is no place to save state in the native type.
224 * Request a continuation from the very beginning.
228 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
234 NativeInteger_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
235 int ilevel, enum xer_encoder_flags_e flags,
236 asn_app_consume_bytes_f *cb, void *app_key) {
237 const asn_INTEGER_specifics_t *specs =
238 (const asn_INTEGER_specifics_t *)td->specifics;
239 char scratch[32]; /* Enough for 64-bit int */
240 asn_enc_rval_t er = {0,0,0};
241 const long *native = (const long *)sptr;
246 if(!native) ASN__ENCODE_FAILED;
248 er.encoded = snprintf(scratch, sizeof(scratch),
249 (specs && specs->field_unsigned)
250 ? "%lu" : "%ld", *native);
251 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
252 || cb(scratch, er.encoded, app_key) < 0)
258 #ifndef ASN_DISABLE_PER_SUPPORT
261 NativeInteger_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
262 const asn_TYPE_descriptor_t *td,
263 const asn_per_constraints_t *constraints, void **sptr,
264 asn_per_data_t *pd) {
265 const asn_INTEGER_specifics_t *specs =
266 (const asn_INTEGER_specifics_t *)td->specifics;
268 long *native = (long *)*sptr;
270 void *tmpintptr = &tmpint;
273 ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name);
276 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
277 if(!native) ASN__DECODE_FAILED;
280 memset(&tmpint, 0, sizeof tmpint);
281 rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints,
283 if(rval.code == RC_OK) {
284 if((specs&&specs->field_unsigned)
285 ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
286 : asn_INTEGER2long(&tmpint, native))
289 ASN_DEBUG("NativeInteger %s got value %ld",
292 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
298 NativeInteger_encode_uper(const asn_TYPE_descriptor_t *td,
299 const asn_per_constraints_t *constraints,
300 const void *sptr, asn_per_outp_t *po) {
301 const asn_INTEGER_specifics_t *specs =
302 (const asn_INTEGER_specifics_t *)td->specifics;
303 asn_enc_rval_t er = {0,0,0};
307 if(!sptr) ASN__ENCODE_FAILED;
309 native = *(const long *)sptr;
311 ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native);
313 memset(&tmpint, 0, sizeof(tmpint));
314 if((specs&&specs->field_unsigned)
315 ? asn_ulong2INTEGER(&tmpint, native)
316 : asn_long2INTEGER(&tmpint, native))
318 er = INTEGER_encode_uper(td, constraints, &tmpint, po);
319 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
324 NativeInteger_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
325 const asn_TYPE_descriptor_t *td,
326 const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
328 const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics;
330 long *native = (long *)*sptr;
332 void *tmpintptr = &tmpint;
335 ASN_DEBUG("Decoding NativeInteger %s (APER)", td->name);
338 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
339 if(!native) ASN__DECODE_FAILED;
342 memset(&tmpint, 0, sizeof tmpint);
343 rval = INTEGER_decode_aper(opt_codec_ctx, td, constraints,
345 if(rval.code == RC_OK) {
346 if((specs&&specs->field_unsigned)
347 ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
348 : asn_INTEGER2long(&tmpint, native))
351 ASN_DEBUG("NativeInteger %s got value %ld",
354 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
360 NativeInteger_encode_aper(const asn_TYPE_descriptor_t *td,
361 const asn_per_constraints_t *constraints,
362 const void *sptr, asn_per_outp_t *po) {
364 const asn_INTEGER_specifics_t *specs = (const asn_INTEGER_specifics_t *)td->specifics;
365 asn_enc_rval_t er = {0,0,0};
369 if(!sptr) ASN__ENCODE_FAILED;
371 native = *(const long *)sptr;
373 ASN_DEBUG("Encoding NativeInteger %s %ld (APER)", td->name, native);
375 memset(&tmpint, 0, sizeof(tmpint));
376 if((specs&&specs->field_unsigned)
377 ? asn_ulong2INTEGER(&tmpint, (unsigned long)native)
378 : asn_long2INTEGER(&tmpint, native))
380 er = INTEGER_encode_aper(td, constraints, &tmpint, po);
381 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
385 #endif /* ASN_DISABLE_PER_SUPPORT */
388 * INTEGER specific human-readable output.
391 NativeInteger_print(const asn_TYPE_descriptor_t *td, const void *sptr,
392 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
393 const asn_INTEGER_specifics_t *specs =
394 (const asn_INTEGER_specifics_t *)td->specifics;
395 const long *native = (const long *)sptr;
396 char scratch[32]; /* Enough for 64-bit int */
399 (void)td; /* Unused argument */
400 (void)ilevel; /* Unused argument */
403 long value = *native;
404 ret = snprintf(scratch, sizeof(scratch),
405 (specs && specs->field_unsigned) ? "%lu" : "%ld", value);
406 assert(ret > 0 && (size_t)ret < sizeof(scratch));
407 if(cb(scratch, ret, app_key) < 0) return -1;
408 if(specs && (value >= 0 || !specs->field_unsigned)) {
409 const asn_INTEGER_enum_map_t *el =
410 INTEGER_map_value2enum(specs, value);
412 if(cb(" (", 2, app_key) < 0) return -1;
413 if(cb(el->enum_name, el->enum_len, app_key) < 0) return -1;
414 if(cb(")", 1, app_key) < 0) return -1;
419 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
424 NativeInteger_free(const asn_TYPE_descriptor_t *td, void *ptr,
425 enum asn_struct_free_method method) {
429 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
430 td->name, method, ptr);
433 case ASFM_FREE_EVERYTHING:
436 case ASFM_FREE_UNDERLYING:
438 case ASFM_FREE_UNDERLYING_AND_RESET:
439 memset(ptr, 0, sizeof(long));
445 NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
449 const asn_INTEGER_specifics_t *specs =
450 (const asn_INTEGER_specifics_t *)td->specifics;
451 if(specs && specs->field_unsigned) {
452 const unsigned long *a = aptr;
453 const unsigned long *b = bptr;
462 const long *a = aptr;
463 const long *b = bptr;
479 asn_random_fill_result_t
480 NativeInteger_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
481 const asn_encoding_constraints_t *constraints,
483 const asn_INTEGER_specifics_t *specs =
484 (const asn_INTEGER_specifics_t *)td->specifics;
485 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
486 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
487 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
489 const asn_INTEGER_enum_map_t *emap;
494 if(max_length == 0) return result_skipped;
497 st = (long *)CALLOC(1, sizeof(*st));
499 return result_failed;
504 emap = specs->value2enum;
505 emap_len = specs->map_count;
506 if(specs->strict_enumeration) {
507 find_inside_map = emap_len > 0;
509 find_inside_map = emap_len ? asn_random_between(0, 1) : 0;
517 if(find_inside_map) {
518 assert(emap_len > 0);
519 value = emap[asn_random_between(0, emap_len - 1)].nat_value;
521 const asn_per_constraints_t *ct;
523 static const long variants[] = {
524 -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384,
525 -16383, -257, -256, -255, -254, -129, -128, -127,
526 -126, -1, 0, 1, 126, 127, 128, 129,
527 254, 255, 256, 257, 16383, 16384, 16385, 32767,
528 32768, 32769, 65534, 65535, 65536, 65537};
529 if(specs && specs->field_unsigned) {
530 assert(variants[18] == 0);
531 value = variants[asn_random_between(
532 18, sizeof(variants) / sizeof(variants[0]) - 1)];
534 value = variants[asn_random_between(
535 0, sizeof(variants) / sizeof(variants[0]) - 1)];
538 if(!constraints) constraints = &td->encoding_constraints;
539 ct = constraints ? constraints->per_constraints : 0;
540 if(ct && (ct->value.flags & APC_CONSTRAINED)) {
541 if(value < ct->value.lower_bound || value > ct->value.upper_bound) {
542 value = asn_random_between(ct->value.lower_bound,
543 ct->value.upper_bound);