blob: f9b505fe8c3f1d90aeeff72815fb382b0bcaa865 [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;
Lev Walkinb8933ed2017-07-25 09:07:45 -070021 INTEGER_t tmpint;
22 INTEGER_t *tmpintptr = &tmpint;
23
24 memset(&tmpint, 0, sizeof(tmpint));
Lev Walkin9ae018e2017-07-24 01:45:57 +040025
26 (void)opt_codec_ctx;
Lev Walkin9ae018e2017-07-24 01:45:57 +040027
28 if(!native) {
29 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
30 if(!native) ASN__DECODE_FAILED;
31 }
32
33 if(!constraints) constraints = td->oer_constraints;
34 ct = constraints ? &constraints->value : 0;
35
Lev Walkin9ae018e2017-07-24 01:45:57 +040036 /*
37 * OPTIMIZATION: Encode directly rather than passing through INTEGER.
38 * Saves a memory allocation.
39 */
40 rval = INTEGER_decode_oer(opt_codec_ctx, &asn_DEF_INTEGER, constraints,
41 (void **)&tmpintptr, ptr, size);
42 if(rval.code != RC_OK) {
43 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
44 return rval;
45 }
46
47 if(specs && specs->field_unsigned) {
48 unsigned long ul;
49 if(asn_INTEGER2ulong(&tmpint, &ul) != 0) {
50 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
51 rval.code = RC_FAIL;
52 rval.consumed = 0;
53 return rval;
54 } else {
55 *native = ul;
56 }
57 } else {
58 long l;
59 if(asn_INTEGER2long(&tmpint, &l) != 0) {
60 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
61 rval.code = RC_FAIL;
62 rval.consumed = 0;
63 return rval;
64 } else {
65 *native = l;
66 }
67 }
68
69 return rval;
70}
71
72/*
73 * Encode as Canonical OER.
74 */
75asn_enc_rval_t
76NativeInteger_encode_oer(asn_TYPE_descriptor_t *td,
77 asn_oer_constraints_t *constraints, void *sptr,
78 asn_app_consume_bytes_f *cb, void *app_key) {
79 asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
Lev Walkinb8933ed2017-07-25 09:07:45 -070080 INTEGER_t tmpint;
Lev Walkin9ae018e2017-07-24 01:45:57 +040081 long native;
82
83 if(!sptr) ASN__ENCODE_FAILED;
84
85 native = *(const long *)sptr;
Lev Walkinb8933ed2017-07-25 09:07:45 -070086 memset(&tmpint, 0, sizeof(tmpint));
Lev Walkin9ae018e2017-07-24 01:45:57 +040087
88 ASN_DEBUG("Encoding %s %ld as NativeInteger", td ? td->name : "", native);
89
Lev Walkin9ae018e2017-07-24 01:45:57 +040090 if((specs && specs->field_unsigned) ? asn_ulong2INTEGER(&tmpint, native)
91 : asn_long2INTEGER(&tmpint, native)) {
92 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
93 ASN__ENCODE_FAILED;
94 } else {
95 asn_enc_rval_t er =
96 INTEGER_encode_oer(td, constraints, &tmpint, cb, app_key);
97 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
98 return er;
99 }
100}
101
102#endif /* ASN_DISABLE_OER_SUPPORT */