Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 12 | asn_dec_rval_t |
| 13 | NativeInteger_decode_oer(asn_codec_ctx_t *opt_codec_ctx, |
| 14 | asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 15 | const asn_oer_constraints_t *constraints, |
| 16 | void **nint_ptr, const void *ptr, size_t size) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 17 | const asn_INTEGER_specifics_t *specs = |
| 18 | (const asn_INTEGER_specifics_t *)td->specifics; |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 19 | asn_dec_rval_t rval = {RC_OK, 0}; |
| 20 | long *native = (long *)*nint_ptr; |
Lev Walkin | b8933ed | 2017-07-25 09:07:45 -0700 | [diff] [blame] | 21 | INTEGER_t tmpint; |
| 22 | INTEGER_t *tmpintptr = &tmpint; |
| 23 | |
| 24 | memset(&tmpint, 0, sizeof(tmpint)); |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 25 | |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 26 | if(!native) { |
| 27 | native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native))); |
| 28 | if(!native) ASN__DECODE_FAILED; |
| 29 | } |
| 30 | |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 31 | /* |
| 32 | * OPTIMIZATION: Encode directly rather than passing through INTEGER. |
| 33 | * Saves a memory allocation. |
| 34 | */ |
Lev Walkin | 94fc418 | 2017-08-28 00:14:59 -0700 | [diff] [blame] | 35 | rval = INTEGER_decode_oer(opt_codec_ctx, td, constraints, |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 36 | (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; |
| 44 | if(asn_INTEGER2ulong(&tmpint, &ul) != 0) { |
| 45 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint); |
| 46 | rval.code = RC_FAIL; |
| 47 | rval.consumed = 0; |
| 48 | return rval; |
| 49 | } else { |
| 50 | *native = ul; |
| 51 | } |
| 52 | } else { |
| 53 | long l; |
| 54 | if(asn_INTEGER2long(&tmpint, &l) != 0) { |
| 55 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint); |
| 56 | rval.code = RC_FAIL; |
| 57 | rval.consumed = 0; |
| 58 | return rval; |
| 59 | } else { |
| 60 | *native = l; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return rval; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Encode as Canonical OER. |
| 69 | */ |
| 70 | asn_enc_rval_t |
| 71 | NativeInteger_encode_oer(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 72 | const asn_oer_constraints_t *constraints, void *sptr, |
| 73 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 74 | const asn_INTEGER_specifics_t *specs = |
| 75 | (const asn_INTEGER_specifics_t *)td->specifics; |
Lev Walkin | b8933ed | 2017-07-25 09:07:45 -0700 | [diff] [blame] | 76 | INTEGER_t tmpint; |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 77 | long native; |
| 78 | |
| 79 | if(!sptr) ASN__ENCODE_FAILED; |
| 80 | |
| 81 | native = *(const long *)sptr; |
Lev Walkin | b8933ed | 2017-07-25 09:07:45 -0700 | [diff] [blame] | 82 | memset(&tmpint, 0, sizeof(tmpint)); |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 83 | |
| 84 | ASN_DEBUG("Encoding %s %ld as NativeInteger", td ? td->name : "", native); |
| 85 | |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 86 | 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 */ |