blob: 5c98d3e757afbd71bfe56d0bf77dc24897ad10ee [file] [log] [blame]
Lev Walkined3a4ae2017-07-07 10:09:51 -07001/*
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 <INTEGER.h>
10#include <errno.h>
11
12asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070013INTEGER_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
14 const asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -070015 const asn_oer_constraints_t *constraints, void **sptr,
Lev Walkined3a4ae2017-07-07 10:09:51 -070016 const void *ptr, size_t size) {
Lev Walkind62a6622017-08-22 02:31:02 -070017 const asn_INTEGER_specifics_t *specs =
18 (const asn_INTEGER_specifics_t *)td->specifics;
Lev Walkined3a4ae2017-07-07 10:09:51 -070019 asn_dec_rval_t rval = {RC_OK, 0};
20 INTEGER_t *st = (INTEGER_t *)*sptr;
Lev Walkin4118ccf2017-08-02 10:37:31 -070021 struct asn_oer_constraint_number_s ct = {0, 0};
22 size_t req_bytes;
Lev Walkined3a4ae2017-07-07 10:09:51 -070023
24 (void)opt_codec_ctx;
Lev Walkine4d8c922017-07-10 20:29:33 -070025 (void)specs;
Lev Walkined3a4ae2017-07-07 10:09:51 -070026
27 if(!st) {
28 st = (INTEGER_t *)(*sptr = CALLOC(1, sizeof(*st)));
29 if(!st) ASN__DECODE_FAILED;
30 }
31
Lev Walkined3a4ae2017-07-07 10:09:51 -070032 FREEMEM(st->buf);
33 st->buf = 0;
34 st->size = 0;
Lev Walkine4d8c922017-07-10 20:29:33 -070035
Lev Walkina5972be2017-09-29 23:15:58 -070036 if(!constraints) constraints = td->encoding_constraints.oer_constraints;
Lev Walkin4118ccf2017-08-02 10:37:31 -070037 if(constraints) ct = constraints->value;
Lev Walkine4d8c922017-07-10 20:29:33 -070038
Lev Walkin4118ccf2017-08-02 10:37:31 -070039 if(ct.width) {
40 req_bytes = ct.width;
41 } else {
42 /* No lower bound and no upper bound, effectively */
43
44 ssize_t consumed = oer_fetch_length(ptr, size, &req_bytes);
45 if(consumed == 0) {
46 ASN__DECODE_STARVED;
47 } else if(consumed == -1) {
48 ASN__DECODE_FAILED;
49 }
50 rval.consumed += consumed;
51 ptr = (const char *)ptr + consumed;
52 size -= consumed;
53 }
54
55 if(req_bytes > size) {
56 ASN__DECODE_STARVED;
57 }
58
59 if(ct.positive) {
Lev Walkine4d8c922017-07-10 20:29:33 -070060 /* X.969 08/2015 10.2(a) */
61 unsigned msb; /* Most significant bit */
62 size_t useful_size;
63
Lev Walkine4d8c922017-07-10 20:29:33 -070064 /* 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);
68 if(!st->buf) {
69 ASN__DECODE_FAILED;
70 }
71
72 /*
73 * Record a large unsigned in a way not to confuse it
74 * with signed value.
75 */
76 st->buf[0] = '\0';
77 memcpy(st->buf + msb, ptr, req_bytes);
78 st->buf[useful_size] = '\0'; /* Just in case, 0-terminate */
79 st->size = useful_size;
80
81 rval.consumed += req_bytes;
82 return rval;
Lev Walkin4118ccf2017-08-02 10:37:31 -070083 } else {
84 /* X.969 08/2015 10.2(b) */
85 st->buf = (uint8_t *)MALLOC(req_bytes + 1);
86 if(!st->buf) {
Lev Walkine4d8c922017-07-10 20:29:33 -070087 ASN__DECODE_FAILED;
88 }
Lev Walkin4118ccf2017-08-02 10:37:31 -070089
90 memcpy(st->buf, ptr, req_bytes);
91 st->buf[req_bytes] = '\0'; /* Just in case, 0-terminate */
92 st->size = req_bytes;
93
94 rval.consumed += req_bytes;
95 return rval;
Lev Walkine4d8c922017-07-10 20:29:33 -070096 }
Lev Walkined3a4ae2017-07-07 10:09:51 -070097}
98
Lev Walkinfcfe19b2017-07-10 21:57:14 -070099/*
100 * Encode as Canonical OER.
101 */
102asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700103INTEGER_encode_oer(const asn_TYPE_descriptor_t *td,
104 const asn_oer_constraints_t *constraints, const void *sptr,
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700105 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkincc9b3ae2017-07-14 08:57:02 +0400106 const INTEGER_t *st = sptr;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700107 asn_enc_rval_t er;
Lev Walkin4118ccf2017-08-02 10:37:31 -0700108 struct asn_oer_constraint_number_s ct = {0, 0};
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700109 const uint8_t *buf;
110 const uint8_t *end;
111 size_t useful_bytes;
112 size_t req_bytes = 0;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700113 int sign = 0;
114
115 if(!st || st->size == 0) ASN__ENCODE_FAILED;
116
Lev Walkina5972be2017-09-29 23:15:58 -0700117 if(!constraints) constraints = td->encoding_constraints.oer_constraints;
Lev Walkin4118ccf2017-08-02 10:37:31 -0700118 if(constraints) ct = constraints->value;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700119
120 er.encoded = 0;
121
122 buf = st->buf;
123 end = buf + st->size;
124
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700125 sign = (buf && buf < end) ? buf[0] & 0x80 : 0;
126
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700127 /* Ignore 9 leading zeroes or ones */
Lev Walkin4118ccf2017-08-02 10:37:31 -0700128 if(ct.positive) {
Lev Walkincc9b3ae2017-07-14 08:57:02 +0400129 if(sign) {
130 /* The value given is a signed value. Can't proceed. */
131 ASN__ENCODE_FAILED;
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700132 }
Lev Walkincc9b3ae2017-07-14 08:57:02 +0400133 /* Remove leading zeros. */
134 for(; buf + 1 < end; buf++) {
135 if(buf[0] != 0x0) break;
136 }
137 } else {
138 for(; buf + 1 < end; buf++) {
139 if(buf[0] == 0x0 && (buf[1] & 0x80) == 0) {
140 continue;
141 } else if(buf[0] == 0xff && (buf[1] & 0x80) != 0) {
142 continue;
143 }
144 break;
145 }
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700146 }
147
148 useful_bytes = end - buf;
Lev Walkin4118ccf2017-08-02 10:37:31 -0700149 if(ct.width) {
150 req_bytes = ct.width;
151 } else {
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700152 ssize_t r = oer_serialize_length(useful_bytes, cb, app_key);
153 if(r < 0) {
154 ASN__ENCODE_FAILED;
155 }
156 er.encoded += r;
157 req_bytes = useful_bytes;
Lev Walkin4118ccf2017-08-02 10:37:31 -0700158 }
159
160 if(req_bytes < useful_bytes) {
Lev Walkinfcfe19b2017-07-10 21:57:14 -0700161 ASN__ENCODE_FAILED;
162 }
163
164 er.encoded += req_bytes;
165
166 for(; req_bytes > useful_bytes; req_bytes--) {
167 if(cb(sign?"\xff":"\0", 1, app_key) < 0) {
168 ASN__ENCODE_FAILED;
169 }
170 }
171
172 if(cb(buf, useful_bytes, app_key) < 0) {
173 ASN__ENCODE_FAILED;
174 }
175
176 ASN__ENCODED_OK(er);
177}
178
Lev Walkined3a4ae2017-07-07 10:09:51 -0700179#endif /* ASN_DISABLE_OER_SUPPORT */