blob: 3760828037588561a6988af59c2527643357b92d [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;
Lev Walkin9ae018e2017-07-24 01:45:57 +040023
24 if(!native) {
25 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
26 if(!native) ASN__DECODE_FAILED;
27 }
28
29 if(!constraints) constraints = td->oer_constraints;
30 ct = constraints ? &constraints->value : 0;
31
32 INTEGER_t tmpint;
33 INTEGER_t *tmpintptr = &tmpint;
34 memset(&tmpint, 0, sizeof(tmpint));
35
36 /*
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;
80 long native;
81
82 if(!sptr) ASN__ENCODE_FAILED;
83
84 native = *(const long *)sptr;
85
86 ASN_DEBUG("Encoding %s %ld as NativeInteger", td ? td->name : "", native);
87
88 INTEGER_t tmpint;
89 memset(&tmpint, 0, sizeof(tmpint));
90
91 if((specs && specs->field_unsigned) ? asn_ulong2INTEGER(&tmpint, native)
92 : asn_long2INTEGER(&tmpint, native)) {
93 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
94 ASN__ENCODE_FAILED;
95 } else {
96 asn_enc_rval_t er =
97 INTEGER_encode_oer(td, constraints, &tmpint, cb, app_key);
98 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
99 return er;
100 }
101}
102
103#endif /* ASN_DISABLE_OER_SUPPORT */