| 1 | /* |
| 2 | * Copyright (c) 2017 Lev Walkin <[email protected]>. |
| 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>cxx . . $ `<file>/lib/s1ap/asn1c/asn_internal.h`/ |
| 9 | #include <NativeEnumerated.h>cxx . . $ `<file>/lib/s1ap/asn1c/NativeEnumerated.h`/ |
| 10 | #include <errno.h> |
| 11 | |
| 12 | static long |
| 13 | asn__nativeenumerated_convertcxx . . $ asn__nativeenumerated_convert(f20f382ab637f5c2).(const uint8_tcxx . . $ uint8_t# *b, const uint8_tcxx . . $ uint8_t# *end) { |
| 14 | unsigned long value; |
| 15 | |
| 16 | /* Perform the sign initialization */ |
| 17 | /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */ |
| 18 | if((*b >> 7)) { |
| 19 | value = (unsigned long)(-1); |
| 20 | } else { |
| 21 | value = 0; |
| 22 | } |
| 23 | |
| 24 | /* Conversion engine */ |
| 25 | for(; b < end; b++) { |
| 26 | value = (value << 8) | *b; |
| 27 | } |
| 28 | |
| 29 | return value; |
| 30 | } |
| 31 | |
| 32 | asn_dec_rval_tcxx . . $ asn_dec_rval_t# |
| 33 | NativeEnumerated_decode_oercxx . . $ NativeEnumerated_decode_oer(7e0840003868e3f).(const asn_codec_ctx_tcxx . . $ asn_codec_ctx_t# *opt_codec_ctx, |
| 34 | const asn_TYPE_descriptor_tcxx . . $ asn_TYPE_descriptor_t# *td, |
| 35 | const asn_oer_constraints_tcxx . . $ asn_oer_constraints_t# *constraints, |
| 36 | void **nint_ptr, const void *ptr, size_t size) { |
| 37 | asn_dec_rval_tcxx . . $ asn_dec_rval_t# rval = {RC_OKcxx . . $ RC_OK., 0}; |
| 38 | long *native = (long *)*nint_ptr; |
| 39 | const uint8_tcxx . . $ uint8_t# *b = ptr; |
| 40 | |
| 41 | (void)opt_codec_ctx; |
| 42 | (void)constraints; |
| 43 | |
| 44 | if(size < 1) { |
| 45 | ASN__DECODE_STARVEDcxx . . $ `lib/s1ap/asn1c/asn_codecs.h:97:9`!; |
| 46 | } |
| 47 | |
| 48 | if((*b & 0x80) == 0) { |
| 49 | /* |
| 50 | * X.696 (08/2015) #11.2 Short form for Enumerated. |
| 51 | */ |
| 52 | if(!native) { |
| 53 | native = (long *)(*nint_ptr = CALLOCcxx . . $ `lib/s1ap/asn1c/asn_internal.h:33:9`!(1, sizeof(*native))); |
| 54 | if(!native) ASN__DECODE_FAILEDcxx . . $ `lib/s1ap/asn1c/asn_codecs.h:90:9`!; |
| 55 | } |
| 56 | |
| 57 | *native = *b; |
| 58 | rval.consumedcxx . . $ asn_dec_rval_s#consumed. = 1; |
| 59 | } else { |
| 60 | /* |
| 61 | * X.696 (08/2015) #11.4 Long form for Enumerated. |
| 62 | */ |
| 63 | size_t length = *b & 0x7f; |
| 64 | const uint8_tcxx . . $ uint8_t# *bend; |
| 65 | long value; |
| 66 | |
| 67 | if(length < 1 || length > sizeof(*native)) { |
| 68 | ASN__DECODE_FAILEDcxx . . $ `lib/s1ap/asn1c/asn_codecs.h:90:9`!; |
| 69 | } |
| 70 | if((1 + length) > size) { |
| 71 | ASN__DECODE_STARVEDcxx . . $ `lib/s1ap/asn1c/asn_codecs.h:97:9`!; |
| 72 | } |
| 73 | b++; |
| 74 | bend = b + length; |
| 75 | |
| 76 | value = asn__nativeenumerated_convertcxx . . $ asn__nativeenumerated_convert(f20f382ab637f5c2).(b, bend); |
| 77 | if(value < 0) { |
| 78 | const asn_INTEGER_specifics_tcxx . . $ asn_INTEGER_specifics_t# *specs = |
| 79 | (const asn_INTEGER_specifics_tcxx . . $ asn_INTEGER_specifics_t# *)td->specificscxx . . $ asn_TYPE_descriptor_s#specifics.; |
| 80 | if(specs && specs->field_unsignedcxx . . $ asn_INTEGER_specifics_s#field_unsigned.) { |
| 81 | ASN__DECODE_FAILEDcxx . . $ `lib/s1ap/asn1c/asn_codecs.h:90:9`!; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if(!native) { |
| 86 | native = (long *)(*nint_ptr = CALLOCcxx . . $ `lib/s1ap/asn1c/asn_internal.h:33:9`!(1, sizeof(*native))); |
| 87 | if(!native) ASN__DECODE_FAILEDcxx . . $ `lib/s1ap/asn1c/asn_codecs.h:90:9`!; |
| 88 | } |
| 89 | |
| 90 | *native = value; |
| 91 | |
| 92 | rval.consumedcxx . . $ asn_dec_rval_s#consumed. = (1 + length); |
| 93 | } |
| 94 | |
| 95 | return rval; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Encode as Canonical OER. |
| 100 | */ |
| 101 | asn_enc_rval_tcxx . . $ asn_enc_rval_t# |
| 102 | NativeEnumerated_encode_oercxx . . $ NativeEnumerated_encode_oer(f4853efe7f383924).(const asn_TYPE_descriptor_tcxx . . $ asn_TYPE_descriptor_t# *td, |
| 103 | const asn_oer_constraints_tcxx . . $ asn_oer_constraints_t# *constraints, |
| 104 | const void *sptr, asn_app_consume_bytes_fcxx . . $ asn_app_consume_bytes_f# *cb, |
| 105 | void *app_key) { |
| 106 | asn_enc_rval_tcxx . . $ asn_enc_rval_t# er; |
| 107 | long native; |
| 108 | |
| 109 | (void)constraints; |
| 110 | |
| 111 | if(!sptr) ASN__ENCODE_FAILEDcxx . . $ `lib/s1ap/asn1c/asn_codecs.h:59:9`!; |
| 112 | |
| 113 | native = *(const long *)sptr; |
| 114 | |
| 115 | if(native >= 0 && native <= 127) { |
| 116 | /* #11.2 Short form */ |
| 117 | uint8_tcxx . . $ uint8_t# b = native; |
| 118 | er.encodedcxx . . $ asn_enc_rval_s#encoded. = 1; |
| 119 | if(cb(&b, er.encodedcxx . . $ asn_enc_rval_s#encoded., app_key) < 0) { |
| 120 | ASN__ENCODE_FAILEDcxx . . $ `lib/s1ap/asn1c/asn_codecs.h:59:9`!; |
| 121 | } |
| 122 | ASN__ENCODED_OKcxx . . $ `lib/s1ap/asn1c/asn_codecs.h:67:9`!(er); |
| 123 | } else { |
| 124 | /* #11.2 Long form */ |
| 125 | uint8_tcxx . . $ uint8_t# buf[1 + sizeof(native)]; |
| 126 | uint8_tcxx . . $ uint8_t# *b = &buf[sizeof(native)]; /* Last addressable */ |
| 127 | long final_pattern = -1 * (native < 0); |
| 128 | |
| 129 | for(;;) { |
| 130 | *b-- = native; |
| 131 | native >>= 8; |
| 132 | if(native == final_pattern) { |
| 133 | if(final_pattern) { |
| 134 | if((b[1] & 0x80)) break; |
| 135 | } else { |
| 136 | if(!(b[1] & 0x80)) break; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | *b = 0x80 | (&buf[sizeof(native)] - b); |
| 141 | er.encodedcxx . . $ asn_enc_rval_s#encoded. = 1 + (&buf[sizeof(native)] - b); |
| 142 | if(cb(b, er.encodedcxx . . $ asn_enc_rval_s#encoded., app_key) < 0) { |
| 143 | ASN__ENCODE_FAILEDcxx . . $ `lib/s1ap/asn1c/asn_codecs.h:59:9`!; |
| 144 | } |
| 145 | ASN__ENCODED_OKcxx . . $ `lib/s1ap/asn1c/asn_codecs.h:67:9`!(er); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | #endif /* ASN_DISABLE_OER_SUPPORT */ |