2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #ifndef ASN_DISABLE_OER_SUPPORT
8 #include <asn_internal.h>
9 #include <NativeInteger.h>
13 NativeInteger_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
14 const asn_TYPE_descriptor_t *td,
15 const asn_oer_constraints_t *constraints,
16 void **nint_ptr, const void *ptr, size_t size) {
17 const asn_INTEGER_specifics_t *specs =
18 (const asn_INTEGER_specifics_t *)td->specifics;
19 asn_dec_rval_t rval = {RC_OK, 0};
20 long *native = (long *)*nint_ptr;
22 INTEGER_t *tmpintptr = &tmpint;
24 memset(&tmpint, 0, sizeof(tmpint));
27 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
28 if(!native) ASN__DECODE_FAILED;
32 * OPTIMIZATION: Encode directly rather than passing through INTEGER.
33 * Saves a memory allocation.
35 rval = INTEGER_decode_oer(opt_codec_ctx, td, constraints,
36 (void **)&tmpintptr, ptr, size);
37 if(rval.code != RC_OK) {
38 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
42 if(specs && specs->field_unsigned) {
44 int ok = asn_INTEGER2ulong(&tmpint, &ul) == 0;
45 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
54 int ok = asn_INTEGER2long(&tmpint, &l) == 0;
55 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
68 * Encode as Canonical OER.
71 NativeInteger_encode_oer(const asn_TYPE_descriptor_t *td,
72 const asn_oer_constraints_t *constraints,
73 const void *sptr, asn_app_consume_bytes_f *cb,
75 const asn_INTEGER_specifics_t *specs =
76 (const asn_INTEGER_specifics_t *)td->specifics;
80 if(!sptr) ASN__ENCODE_FAILED;
82 native = *(const long *)sptr;
83 memset(&tmpint, 0, sizeof(tmpint));
85 ASN_DEBUG("Encoding %s %ld as NativeInteger", td ? td->name : "", native);
87 if((specs && specs->field_unsigned) ? asn_ulong2INTEGER(&tmpint, native)
88 : asn_long2INTEGER(&tmpint, native)) {
89 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
93 INTEGER_encode_oer(td, constraints, &tmpint, cb, app_key);
94 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
99 #endif /* ASN_DISABLE_OER_SUPPORT */