blob: 8cd9fec1cb262c509e8f6d7d125fdd4cfc73785f [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,
Lev Walkin494fb702017-08-07 20:07:00 -070015 const asn_oer_constraints_t *constraints,
16 void **nint_ptr, const void *ptr, size_t size) {
Lev Walkin9ae018e2017-07-24 01:45:57 +040017 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;
Lev Walkinb8933ed2017-07-25 09:07:45 -070020 INTEGER_t tmpint;
21 INTEGER_t *tmpintptr = &tmpint;
22
23 memset(&tmpint, 0, sizeof(tmpint));
Lev Walkin9ae018e2017-07-24 01:45:57 +040024
Lev Walkin9ae018e2017-07-24 01:45:57 +040025 if(!native) {
26 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
27 if(!native) ASN__DECODE_FAILED;
28 }
29
Lev Walkin9ae018e2017-07-24 01:45:57 +040030 /*
31 * OPTIMIZATION: Encode directly rather than passing through INTEGER.
32 * Saves a memory allocation.
33 */
34 rval = INTEGER_decode_oer(opt_codec_ctx, &asn_DEF_INTEGER, constraints,
35 (void **)&tmpintptr, ptr, size);
36 if(rval.code != RC_OK) {
37 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
38 return rval;
39 }
40
41 if(specs && specs->field_unsigned) {
42 unsigned long ul;
43 if(asn_INTEGER2ulong(&tmpint, &ul) != 0) {
44 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
45 rval.code = RC_FAIL;
46 rval.consumed = 0;
47 return rval;
48 } else {
49 *native = ul;
50 }
51 } else {
52 long l;
53 if(asn_INTEGER2long(&tmpint, &l) != 0) {
54 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
55 rval.code = RC_FAIL;
56 rval.consumed = 0;
57 return rval;
58 } else {
59 *native = l;
60 }
61 }
62
63 return rval;
64}
65
66/*
67 * Encode as Canonical OER.
68 */
69asn_enc_rval_t
70NativeInteger_encode_oer(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -070071 const asn_oer_constraints_t *constraints, void *sptr,
72 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin9ae018e2017-07-24 01:45:57 +040073 asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics;
Lev Walkinb8933ed2017-07-25 09:07:45 -070074 INTEGER_t tmpint;
Lev Walkin9ae018e2017-07-24 01:45:57 +040075 long native;
76
77 if(!sptr) ASN__ENCODE_FAILED;
78
79 native = *(const long *)sptr;
Lev Walkinb8933ed2017-07-25 09:07:45 -070080 memset(&tmpint, 0, sizeof(tmpint));
Lev Walkin9ae018e2017-07-24 01:45:57 +040081
82 ASN_DEBUG("Encoding %s %ld as NativeInteger", td ? td->name : "", native);
83
Lev Walkin9ae018e2017-07-24 01:45:57 +040084 if((specs && specs->field_unsigned) ? asn_ulong2INTEGER(&tmpint, native)
85 : asn_long2INTEGER(&tmpint, native)) {
86 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
87 ASN__ENCODE_FAILED;
88 } else {
89 asn_enc_rval_t er =
90 INTEGER_encode_oer(td, constraints, &tmpint, cb, app_key);
91 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
92 return er;
93 }
94}
95
96#endif /* ASN_DISABLE_OER_SUPPORT */