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 | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 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; |
Lev Walkin | b8933ed | 2017-07-25 09:07:45 -0700 | [diff] [blame] | 20 | INTEGER_t tmpint; |
| 21 | INTEGER_t *tmpintptr = &tmpint; |
| 22 | |
| 23 | memset(&tmpint, 0, sizeof(tmpint)); |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 24 | |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 25 | if(!native) { |
| 26 | native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native))); |
| 27 | if(!native) ASN__DECODE_FAILED; |
| 28 | } |
| 29 | |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 30 | /* |
| 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 | */ |
| 69 | asn_enc_rval_t |
| 70 | NativeInteger_encode_oer(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 71 | const asn_oer_constraints_t *constraints, void *sptr, |
| 72 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 73 | asn_INTEGER_specifics_t *specs = (asn_INTEGER_specifics_t *)td->specifics; |
Lev Walkin | b8933ed | 2017-07-25 09:07:45 -0700 | [diff] [blame] | 74 | INTEGER_t tmpint; |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 75 | long native; |
| 76 | |
| 77 | if(!sptr) ASN__ENCODE_FAILED; |
| 78 | |
| 79 | native = *(const long *)sptr; |
Lev Walkin | b8933ed | 2017-07-25 09:07:45 -0700 | [diff] [blame] | 80 | memset(&tmpint, 0, sizeof(tmpint)); |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 81 | |
| 82 | ASN_DEBUG("Encoding %s %ld as NativeInteger", td ? td->name : "", native); |
| 83 | |
Lev Walkin | 9ae018e | 2017-07-24 01:45:57 +0400 | [diff] [blame] | 84 | 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 */ |