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
41 NativeInteger_decode_uper, /* Unaligned PER decoder */
42 NativeInteger_encode_uper, /* Unaligned PER encoder */
43 #endif /* ASN_DISABLE_PER_SUPPORT */
44 NativeInteger_random_fill,
45 0 /* Use generic outmost tag fetcher */
47 asn_TYPE_descriptor_t asn_DEF_NativeInteger = {
48 "INTEGER", /* The ASN.1 type is still INTEGER */
50 &asn_OP_NativeInteger,
51 asn_DEF_NativeInteger_tags,
52 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
53 asn_DEF_NativeInteger_tags, /* Same as above */
54 sizeof(asn_DEF_NativeInteger_tags) / sizeof(asn_DEF_NativeInteger_tags[0]),
55 { 0, 0, asn_generic_no_constraint },
56 0, 0, /* No members */
61 * Decode INTEGER type.
64 NativeInteger_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
65 const asn_TYPE_descriptor_t *td, void **nint_ptr,
66 const void *buf_ptr, size_t size, int tag_mode) {
67 const asn_INTEGER_specifics_t *specs =
68 (const asn_INTEGER_specifics_t *)td->specifics;
69 long *native = (long *)*nint_ptr;
74 * If the structure is not there, allocate it.
77 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
85 ASN_DEBUG("Decoding %s as INTEGER (tm=%d)",
91 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
92 tag_mode, 0, &length, 0);
93 if(rval.code != RC_OK)
96 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
99 * Make sure we have this length.
101 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
102 size -= rval.consumed;
103 if(length > (ber_tlv_len_t)size) {
104 rval.code = RC_WMORE;
110 * ASN.1 encoded INTEGER: buf_ptr, length
111 * Fill the native, at the same time checking for overflow.
112 * If overflow occurred, return with RC_FAIL.
117 const void *constbuf;
122 unconst_buf.constbuf = buf_ptr;
123 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
126 if((specs&&specs->field_unsigned)
127 ? asn_INTEGER2ulong(&tmp, (unsigned long *)&l) /* sic */
128 : asn_INTEGER2long(&tmp, &l)) {
138 rval.consumed += length;
140 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%ld)",
141 (long)rval.consumed, (long)length, td->name, (long)*native);
147 * Encode the NativeInteger using the standard INTEGER type DER encoder.
150 NativeInteger_encode_der(const asn_TYPE_descriptor_t *sd, const void *ptr,
151 int tag_mode, ber_tlv_tag_t tag,
152 asn_app_consume_bytes_f *cb, void *app_key) {
153 unsigned long native = *(const unsigned long *)ptr; /* Disable sign ext. */
154 asn_enc_rval_t erval;
157 #ifdef WORDS_BIGENDIAN /* Opportunistic optimization */
159 tmp.buf = (uint8_t *)&native;
160 tmp.size = sizeof(native);
162 #else /* Works even if WORDS_BIGENDIAN is not set where should've been */
163 uint8_t buf[sizeof(native)];
166 /* Prepare a fake INTEGER */
167 for(p = buf + sizeof(buf) - 1; p >= buf; p--, native >>= 8)
168 *p = (uint8_t)native;
171 tmp.size = sizeof(buf);
172 #endif /* WORDS_BIGENDIAN */
174 /* Encode fake INTEGER */
175 erval = INTEGER_encode_der(sd, &tmp, tag_mode, tag, cb, app_key);
176 if(erval.structure_ptr == &tmp) {
177 erval.structure_ptr = ptr;
183 * Decode the chunk of XML text encoding INTEGER.
186 NativeInteger_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
187 const asn_TYPE_descriptor_t *td, void **sptr,
188 const char *opt_mname, const void *buf_ptr,
190 const asn_INTEGER_specifics_t *specs =
191 (const asn_INTEGER_specifics_t *)td->specifics;
194 void *st_ptr = (void *)&st;
195 long *native = (long *)*sptr;
198 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
199 if(!native) ASN__DECODE_FAILED;
202 memset(&st, 0, sizeof(st));
203 rval = INTEGER_decode_xer(opt_codec_ctx, td, &st_ptr,
204 opt_mname, buf_ptr, size);
205 if(rval.code == RC_OK) {
207 if((specs&&specs->field_unsigned)
208 ? asn_INTEGER2ulong(&st, (unsigned long *)&l) /* sic */
209 : asn_INTEGER2long(&st, &l)) {
217 * Cannot restart from the middle;
218 * there is no place to save state in the native type.
219 * Request a continuation from the very beginning.
223 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &st);
229 NativeInteger_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
230 int ilevel, enum xer_encoder_flags_e flags,
231 asn_app_consume_bytes_f *cb, void *app_key) {
232 const asn_INTEGER_specifics_t *specs =
233 (const asn_INTEGER_specifics_t *)td->specifics;
234 char scratch[32]; /* Enough for 64-bit int */
236 const long *native = (const long *)sptr;
241 if(!native) ASN__ENCODE_FAILED;
243 er.encoded = snprintf(scratch, sizeof(scratch),
244 (specs && specs->field_unsigned)
245 ? "%lu" : "%ld", *native);
246 if(er.encoded <= 0 || (size_t)er.encoded >= sizeof(scratch)
247 || cb(scratch, er.encoded, app_key) < 0)
253 #ifndef ASN_DISABLE_PER_SUPPORT
256 NativeInteger_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
257 const asn_TYPE_descriptor_t *td,
258 const asn_per_constraints_t *constraints, void **sptr,
259 asn_per_data_t *pd) {
260 const asn_INTEGER_specifics_t *specs =
261 (const asn_INTEGER_specifics_t *)td->specifics;
263 long *native = (long *)*sptr;
265 void *tmpintptr = &tmpint;
268 ASN_DEBUG("Decoding NativeInteger %s (UPER)", td->name);
271 native = (long *)(*sptr = CALLOC(1, sizeof(*native)));
272 if(!native) ASN__DECODE_FAILED;
275 memset(&tmpint, 0, sizeof tmpint);
276 rval = INTEGER_decode_uper(opt_codec_ctx, td, constraints,
278 if(rval.code == RC_OK) {
279 if((specs&&specs->field_unsigned)
280 ? asn_INTEGER2ulong(&tmpint, (unsigned long *)native)
281 : asn_INTEGER2long(&tmpint, native))
284 ASN_DEBUG("NativeInteger %s got value %ld",
287 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
293 NativeInteger_encode_uper(const asn_TYPE_descriptor_t *td,
294 const asn_per_constraints_t *constraints,
295 const void *sptr, asn_per_outp_t *po) {
296 const asn_INTEGER_specifics_t *specs =
297 (const asn_INTEGER_specifics_t *)td->specifics;
302 if(!sptr) ASN__ENCODE_FAILED;
304 native = *(const long *)sptr;
306 ASN_DEBUG("Encoding NativeInteger %s %ld (UPER)", td->name, native);
308 memset(&tmpint, 0, sizeof(tmpint));
309 if((specs&&specs->field_unsigned)
310 ? asn_ulong2INTEGER(&tmpint, native)
311 : asn_long2INTEGER(&tmpint, native))
313 er = INTEGER_encode_uper(td, constraints, &tmpint, po);
314 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
318 #endif /* ASN_DISABLE_PER_SUPPORT */
321 * INTEGER specific human-readable output.
324 NativeInteger_print(const asn_TYPE_descriptor_t *td, const void *sptr,
325 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
326 const asn_INTEGER_specifics_t *specs =
327 (const asn_INTEGER_specifics_t *)td->specifics;
328 const long *native = (const long *)sptr;
329 char scratch[32]; /* Enough for 64-bit int */
332 (void)td; /* Unused argument */
333 (void)ilevel; /* Unused argument */
336 long value = *native;
337 ret = snprintf(scratch, sizeof(scratch),
338 (specs && specs->field_unsigned) ? "%lu" : "%ld", value);
339 assert(ret > 0 && (size_t)ret < sizeof(scratch));
340 if(cb(scratch, ret, app_key) < 0) return -1;
341 if(specs && (value >= 0 || !specs->field_unsigned)) {
342 const asn_INTEGER_enum_map_t *el =
343 INTEGER_map_value2enum(specs, value);
345 if(cb(" (", 2, app_key) < 0) return -1;
346 if(cb(el->enum_name, el->enum_len, app_key) < 0) return -1;
347 if(cb(")", 1, app_key) < 0) return -1;
352 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
357 NativeInteger_free(const asn_TYPE_descriptor_t *td, void *ptr,
358 enum asn_struct_free_method method) {
362 ASN_DEBUG("Freeing %s as INTEGER (%d, %p, Native)",
363 td->name, method, ptr);
366 case ASFM_FREE_EVERYTHING:
369 case ASFM_FREE_UNDERLYING:
371 case ASFM_FREE_UNDERLYING_AND_RESET:
372 memset(ptr, 0, sizeof(long));
378 NativeInteger_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
382 const asn_INTEGER_specifics_t *specs =
383 (const asn_INTEGER_specifics_t *)td->specifics;
384 if(specs && specs->field_unsigned) {
385 const unsigned long *a = aptr;
386 const unsigned long *b = bptr;
395 const long *a = aptr;
396 const long *b = bptr;
412 asn_random_fill_result_t
413 NativeInteger_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
414 const asn_encoding_constraints_t *constraints,
416 const asn_INTEGER_specifics_t *specs =
417 (const asn_INTEGER_specifics_t *)td->specifics;
418 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
419 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
420 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
422 const asn_INTEGER_enum_map_t *emap;
427 if(max_length == 0) return result_skipped;
430 st = (long *)CALLOC(1, sizeof(*st));
432 return result_failed;
437 emap = specs->value2enum;
438 emap_len = specs->map_count;
439 if(specs->strict_enumeration) {
440 find_inside_map = emap_len > 0;
442 find_inside_map = emap_len ? asn_random_between(0, 1) : 0;
450 if(find_inside_map) {
451 assert(emap_len > 0);
452 value = emap[asn_random_between(0, emap_len - 1)].nat_value;
454 const asn_per_constraints_t *ct;
456 static const long variants[] = {
457 -65536, -65535, -65534, -32769, -32768, -32767, -16385, -16384,
458 -16383, -257, -256, -255, -254, -129, -128, -127,
459 -126, -1, 0, 1, 126, 127, 128, 129,
460 254, 255, 256, 257, 16383, 16384, 16385, 32767,
461 32768, 32769, 65534, 65535, 65536, 65537};
462 if(specs && specs->field_unsigned) {
463 assert(variants[18] == 0);
464 value = variants[asn_random_between(
465 18, sizeof(variants) / sizeof(variants[0]) - 1)];
467 value = variants[asn_random_between(
468 0, sizeof(variants) / sizeof(variants[0]) - 1)];
471 if(!constraints) constraints = &td->encoding_constraints;
472 ct = constraints ? constraints->per_constraints : 0;
473 if(ct && (ct->value.flags & APC_CONSTRAINED)) {
474 if(value < ct->value.lower_bound || value > ct->value.upper_bound) {
475 value = asn_random_between(ct->value.lower_bound,
476 ct->value.upper_bound);