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>
13 INTEGER_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
14 const asn_TYPE_descriptor_t *td,
15 const asn_oer_constraints_t *constraints, void **sptr,
16 const void *ptr, size_t size) {
17 const asn_INTEGER_specifics_t *specs =
18 (const asn_INTEGER_specifics_t *)td->specifics;
19 asn_dec_rval_t rval = {RC_OK, 0};
20 INTEGER_t *st = (INTEGER_t *)*sptr;
21 struct asn_oer_constraint_number_s ct = {0, 0};
28 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
29 if(!st) ASN__DECODE_FAILED;
36 if(!constraints) constraints = td->encoding_constraints.oer_constraints;
37 if(constraints) ct = constraints->value;
42 /* No lower bound and no upper bound, effectively */
44 ssize_t consumed = oer_fetch_length(ptr, size, &req_bytes);
47 } else if(consumed == -1) {
50 rval.consumed += consumed;
51 ptr = (const char *)ptr + consumed;
55 if(req_bytes > size) {
60 /* X.969 08/2015 10.2(a) */
61 unsigned msb; /* Most significant bit */
64 /* Check most significant bit */
65 msb = *(const uint8_t *)ptr >> 7; /* yields 0 or 1 */
66 useful_size = msb + req_bytes;
67 st->buf = (uint8_t *)MALLOC(useful_size + 1);
73 * Record a large unsigned in a way not to confuse it
77 memcpy(st->buf + msb, ptr, req_bytes);
78 st->buf[useful_size] = '\0'; /* Just in case, 0-terminate */
79 st->size = useful_size;
81 rval.consumed += req_bytes;
84 /* X.969 08/2015 10.2(b) */
85 st->buf = (uint8_t *)MALLOC(req_bytes + 1);
90 memcpy(st->buf, ptr, req_bytes);
91 st->buf[req_bytes] = '\0'; /* Just in case, 0-terminate */
94 rval.consumed += req_bytes;
100 * Encode as Canonical OER.
103 INTEGER_encode_oer(const asn_TYPE_descriptor_t *td,
104 const asn_oer_constraints_t *constraints, const void *sptr,
105 asn_app_consume_bytes_f *cb, void *app_key) {
106 const INTEGER_t *st = sptr;
107 asn_enc_rval_t er = {0,0,0};
108 struct asn_oer_constraint_number_s ct = {0, 0};
112 size_t req_bytes = 0;
115 if(!st || st->size == 0) ASN__ENCODE_FAILED;
117 if(!constraints) constraints = td->encoding_constraints.oer_constraints;
118 if(constraints) ct = constraints->value;
123 end = buf + st->size;
125 sign = (buf && buf < end) ? buf[0] & 0x80 : 0;
127 /* Ignore 9 leading zeroes or ones */
130 /* The value given is a signed value. Can't proceed. */
133 /* Remove leading zeros. */
134 for(; buf + 1 < end; buf++) {
135 if(buf[0] != 0x0) break;
138 for(; buf + 1 < end; buf++) {
139 if(buf[0] == 0x0 && (buf[1] & 0x80) == 0) {
141 } else if(buf[0] == 0xff && (buf[1] & 0x80) != 0) {
148 useful_bytes = end - buf;
150 req_bytes = ct.width;
152 ssize_t r = oer_serialize_length(useful_bytes, cb, app_key);
157 req_bytes = useful_bytes;
160 if(req_bytes < useful_bytes) {
164 er.encoded += req_bytes;
166 for(; req_bytes > useful_bytes; req_bytes--) {
167 if(cb(sign?"\xff":"\0", 1, app_key) < 0) {
172 if(cb(buf, useful_bytes, app_key) < 0) {
179 #endif /* ASN_DISABLE_OER_SUPPORT */