blob: 15560bd61d0b29fa80af08ce48c3751b61b3cef7 [file] [log] [blame]
Lev Walkin9ae018e2017-07-24 01:45:57 +04001/*
2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
5 */
6#ifndef ASN_DISABLE_OER_SUPPORT
7
8#include <asn_internal.h>
9#include <NativeInteger.h>
10#include <errno.h>
11
12asn_dec_rval_t
13NativeInteger_decode_oer(asn_codec_ctx_t *opt_codec_ctx,
14 asn_TYPE_descriptor_t *td,
15 asn_oer_constraints_t *constraints, void **nint_ptr,
16 const void *ptr, size_t size) {
17 asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
18 asn_dec_rval_t rval = {RC_OK, 0};
19 long *native = (long *)*nint_ptr;
20 asn_oer_constraint_t *ct;
21
22 (void)opt_codec_ctx;
23 (void)specs;
24
25 if(!native) {
26 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
27 if(!native) ASN__DECODE_FAILED;
28 }
29
30 if(!constraints) constraints = td->oer_constraints;
31 ct = constraints ? &constraints->value : 0;
32
33 INTEGER_t tmpint;
34 INTEGER_t *tmpintptr = &tmpint;
35 memset(&tmpint, 0, sizeof(tmpint));
36
37 /*
38 * OPTIMIZATION: Encode directly rather than passing through INTEGER.
39 * Saves a memory allocation.
40 */
41 rval = INTEGER_decode_oer(opt_codec_ctx, &asn_DEF_INTEGER, constraints,
42 (void **)&tmpintptr, ptr, size);
43 if(rval.code != RC_OK) {
44 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
45 return rval;
46 }
47
48 if(specs && specs->field_unsigned) {
49 unsigned long ul;
50 if(asn_INTEGER2ulong(&tmpint, &ul) != 0) {
51 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
52 rval.code = RC_FAIL;
53 rval.consumed = 0;
54 return rval;
55 } else {
56 *native = ul;
57 }
58 } else {
59 long l;
60 if(asn_INTEGER2long(&tmpint, &l) != 0) {
61 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
62 rval.code = RC_FAIL;
63 rval.consumed = 0;
64 return rval;
65 } else {
66 *native = l;
67 }
68 }
69
70 return rval;
71}
72
73/*
74 * Encode as Canonical OER.
75 */
76asn_enc_rval_t
77NativeInteger_encode_oer(asn_TYPE_descriptor_t *td,
78 asn_oer_constraints_t *constraints, void *sptr,
79 asn_app_consume_bytes_f *cb, void *app_key) {
80 asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
81 long native;
82
83 if(!sptr) ASN__ENCODE_FAILED;
84
85 native = *(const long *)sptr;
86
87 ASN_DEBUG("Encoding %s %ld as NativeInteger", td ? td->name : "", native);
88
89 INTEGER_t tmpint;
90 memset(&tmpint, 0, sizeof(tmpint));
91
92 if((specs && specs->field_unsigned) ? asn_ulong2INTEGER(&tmpint, native)
93 : asn_long2INTEGER(&tmpint, native)) {
94 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
95 ASN__ENCODE_FAILED;
96 } else {
97 asn_enc_rval_t er =
98 INTEGER_encode_oer(td, constraints, &tmpint, cb, app_key);
99 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
100 return er;
101 }
102}
103
104#endif /* ASN_DISABLE_OER_SUPPORT */