2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #ifndef ASN_DISABLE_OER_SUPPORT
8 #include <asn_internal.h>
9 #include <NativeEnumerated.h>
13 asn__nativeenumerated_convert(const uint8_t *b, const uint8_t *end) {
16 /* Perform the sign initialization */
17 /* Actually value = -(*b >> 7); gains nothing, yet unreadable! */
19 value = (unsigned long)(-1);
24 /* Conversion engine */
26 value = (value << 8) | *b;
33 NativeEnumerated_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
34 const asn_TYPE_descriptor_t *td,
35 const asn_oer_constraints_t *constraints,
36 void **nint_ptr, const void *ptr, size_t size) {
37 asn_dec_rval_t rval = {RC_OK, 0};
38 long *native = (long *)*nint_ptr;
39 const uint8_t *b = ptr;
48 if((*b & 0x80) == 0) {
50 * X.696 (08/2015) #11.2 Short form for Enumerated.
53 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
54 if(!native) ASN__DECODE_FAILED;
61 * X.696 (08/2015) #11.4 Long form for Enumerated.
63 size_t length = *b & 0x7f;
67 if(length < 1 || length > sizeof(*native)) {
70 if((1 + length) > size) {
76 value = asn__nativeenumerated_convert(b, bend);
78 const asn_INTEGER_specifics_t *specs =
79 (const asn_INTEGER_specifics_t *)td->specifics;
80 if(specs && specs->field_unsigned) {
86 native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
87 if(!native) ASN__DECODE_FAILED;
92 rval.consumed = (1 + length);
99 * Encode as Canonical OER.
102 NativeEnumerated_encode_oer(const asn_TYPE_descriptor_t *td,
103 const asn_oer_constraints_t *constraints,
104 const void *sptr, asn_app_consume_bytes_f *cb,
106 asn_enc_rval_t er = {0,0,0};
111 if(!sptr) ASN__ENCODE_FAILED;
113 native = *(const long *)sptr;
115 if(native >= 0 && native <= 127) {
116 /* #11.2 Short form */
119 if(cb(&b, er.encoded, app_key) < 0) {
124 /* #11.2 Long form */
125 uint8_t buf[1 + sizeof(native)];
126 uint8_t *b = &buf[sizeof(native)]; /* Last addressable */
127 long final_pattern = -1 * (native < 0);
132 if(native == final_pattern) {
134 if((b[1] & 0x80)) break;
136 if(!(b[1] & 0x80)) break;
140 *b = 0x80 | (&buf[sizeof(native)] - b);
141 er.encoded = 1 + (&buf[sizeof(native)] - b);
142 if(cb(b, er.encoded, app_key) < 0) {
149 #endif /* ASN_DISABLE_OER_SUPPORT */