blob: 25794e2571857705f5b0eedee58e4516003768a9 [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 Walkind62a6622017-08-22 02:31:02 -070017 const asn_INTEGER_specifics_t *specs =
18 (const asn_INTEGER_specifics_t *)td->specifics;
Lev Walkin9ae018e2017-07-24 01:45:57 +040019 asn_dec_rval_t rval = {RC_OK, 0};
20 long *native = (long *)*nint_ptr;
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
Lev Walkin9ae018e2017-07-24 01:45:57 +040026 if(!native) {
27 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
28 if(!native) ASN__DECODE_FAILED;
29 }
30
Lev Walkin9ae018e2017-07-24 01:45:57 +040031 /*
32 * OPTIMIZATION: Encode directly rather than passing through INTEGER.
33 * Saves a memory allocation.
34 */
Lev Walkin94fc4182017-08-28 00:14:59 -070035 rval = INTEGER_decode_oer(opt_codec_ctx, td, constraints,
Lev Walkin9ae018e2017-07-24 01:45:57 +040036 (void **)&tmpintptr, ptr, size);
37 if(rval.code != RC_OK) {
38 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
39 return rval;
40 }
41
42 if(specs && specs->field_unsigned) {
43 unsigned long ul;
Lev Walkin96f99212017-08-29 23:38:31 -070044 int ok = asn_INTEGER2ulong(&tmpint, &ul) == 0;
45 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
46 if(ok) {
Lev Walkin9ae018e2017-07-24 01:45:57 +040047 *native = ul;
Lev Walkin96f99212017-08-29 23:38:31 -070048 } else {
49 rval.code = RC_FAIL;
50 return rval;
Lev Walkin9ae018e2017-07-24 01:45:57 +040051 }
52 } else {
53 long l;
Lev Walkin96f99212017-08-29 23:38:31 -070054 int ok = asn_INTEGER2long(&tmpint, &l) == 0;
55 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
56 if(ok) {
Lev Walkin9ae018e2017-07-24 01:45:57 +040057 *native = l;
Lev Walkin96f99212017-08-29 23:38:31 -070058 } else {
59 rval.code = RC_FAIL;
60 return rval;
Lev Walkin9ae018e2017-07-24 01:45:57 +040061 }
62 }
63
64 return rval;
65}
66
67/*
68 * Encode as Canonical OER.
69 */
70asn_enc_rval_t
71NativeInteger_encode_oer(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -070072 const asn_oer_constraints_t *constraints, void *sptr,
73 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind62a6622017-08-22 02:31:02 -070074 const asn_INTEGER_specifics_t *specs =
75 (const asn_INTEGER_specifics_t *)td->specifics;
Lev Walkinb8933ed2017-07-25 09:07:45 -070076 INTEGER_t tmpint;
Lev Walkin9ae018e2017-07-24 01:45:57 +040077 long native;
78
79 if(!sptr) ASN__ENCODE_FAILED;
80
81 native = *(const long *)sptr;
Lev Walkinb8933ed2017-07-25 09:07:45 -070082 memset(&tmpint, 0, sizeof(tmpint));
Lev Walkin9ae018e2017-07-24 01:45:57 +040083
84 ASN_DEBUG("Encoding %s %ld as NativeInteger", td ? td->name : "", native);
85
Lev Walkin9ae018e2017-07-24 01:45:57 +040086 if((specs && specs->field_unsigned) ? asn_ulong2INTEGER(&tmpint, native)
87 : asn_long2INTEGER(&tmpint, native)) {
88 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
89 ASN__ENCODE_FAILED;
90 } else {
91 asn_enc_rval_t er =
92 INTEGER_encode_oer(td, constraints, &tmpint, cb, app_key);
93 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
94 return er;
95 }
96}
97
98#endif /* ASN_DISABLE_OER_SUPPORT */