blob: 0008fa984c04d07f3b331c429512ed18382aef5d [file] [log] [blame]
Lev Walkinc6bd3592017-08-30 17:36:23 -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 <constr_SET_OF.h>
10#include <asn_SET_OF.h>
11#include <errno.h>
12
13/*
14 * This macro "eats" the part of the buffer which is definitely "consumed",
15 * i.e. was correctly converted into local representation or rightfully skipped.
16 */
17#undef ADVANCE
18#define ADVANCE(num_bytes) \
19 do { \
20 size_t num = num_bytes; \
21 ptr = ((const char *)ptr) + num; \
22 size -= num; \
23 consumed_myself += num; \
24 } while(0)
25
26/*
27 * Switch to the next phase of parsing.
28 */
29#undef NEXT_PHASE
30#define NEXT_PHASE(ctx) \
31 do { \
32 ctx->phase++; \
33 ctx->step = 0; \
34 } while(0)
35#undef SET_PHASE
36#define SET_PHASE(ctx, value) \
37 do { \
38 ctx->phase = value; \
39 ctx->step = 0; \
40 } while(0)
41
42/*
43 * Return a standardized complex structure.
44 */
45#undef RETURN
46#define RETURN(_code) \
47 do { \
48 asn_dec_rval_t rval; \
49 rval.code = _code; \
50 rval.consumed = consumed_myself; \
51 return rval; \
52 } while(0)
53
54/*
55 * The SEQUENCE OF and SET OF values utilize a "quantity field".
56 * It is is a pointless combination of #8.6 (length determinant, capable
57 * of encoding tiny and huge numbers in the shortest possible number of octets)
58 * and the variable sized integer. What could have been encoded by #8.6 alone
59 * is required to be encoded by #8.6 followed by that number of unsigned octets.
60 * This doesn't make too much sense. It seems that the original version of OER
61 * standard have been using the unconstrained unsigned integer as a quantity
62 * field, and this legacy have gone through ISO/ITU-T standardization process.
63 */
64static ssize_t
65oer_fetch_quantity(const void *ptr, size_t size, size_t *qty_r) {
Lev Walkineff98a52017-09-13 22:24:35 +000066 const uint8_t *b;
67 const uint8_t *bend;
Lev Walkinc6bd3592017-08-30 17:36:23 -070068 size_t len = 0;
69 size_t qty;
70
71 ssize_t len_len = oer_fetch_length(ptr, size, &len);
72 if(len_len <= 0) {
73 *qty_r = 0;
74 return len_len;
75 }
76
77 if((len_len + len) > size) {
78 *qty_r = 0;
79 return 0;
80 }
81
Lev Walkineff98a52017-09-13 22:24:35 +000082 b = (const uint8_t *)ptr + len_len;
83 bend = b + len;
Lev Walkinc6bd3592017-08-30 17:36:23 -070084
85 /* Skip the leading 0-bytes */
86 for(; b < bend && *b == 0; b++) {
87 }
88
Lev Walkinc6bd3592017-08-30 17:36:23 -070089 if((bend - b) > (ssize_t)sizeof(size_t)) {
90 /* Length is not representable by the native size_t type */
91 *qty_r = 0;
92 return -1;
93 }
94
95 for(qty = 0; b < bend; b++) {
96 qty = (qty << 8) + *b;
97 }
98
99 if(qty > RSIZE_MAX) { /* A bit of C11 validation */
100 *qty_r = 0;
101 return -1;
102 }
103
104 *qty_r = qty;
105 assert((size_t)len_len + len == (size_t)(bend - (const uint8_t *)ptr));
106 return len_len + len;
107}
108
109asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700110SET_OF_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkinc6bd3592017-08-30 17:36:23 -0700111 const asn_oer_constraints_t *constraints, void **struct_ptr,
112 const void *ptr, size_t size) {
Lev Walkind84f6032017-10-03 16:33:59 -0700113 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
Lev Walkinc6bd3592017-08-30 17:36:23 -0700114 asn_dec_rval_t rval = {RC_OK, 0};
115 void *st = *struct_ptr; /* Target structure */
116 asn_struct_ctx_t *ctx; /* Decoder context */
117 size_t consumed_myself = 0; /* Consumed bytes from ptr. */
118
119 (void)opt_codec_ctx;
120 (void)constraints;
121
122 /*
123 * Create the target structure if it is not present already.
124 */
125 if(st == 0) {
126 st = *struct_ptr = CALLOC(1, specs->struct_size);
127 if(st == 0) {
128 RETURN(RC_FAIL);
129 }
130 }
131
132 /*
133 * Restore parsing context.
134 */
135 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
136
137 /*
138 * Start to parse where left previously.
139 */
140 switch(ctx->phase) {
141 case 0: {
142 /*
143 * Fetch number of elements to decode.
144 */
145 size_t length = 0;
146 size_t len_size = oer_fetch_quantity(ptr, size, &length);
147 switch(len_size) {
148 case 0:
149 RETURN(RC_WMORE);
150 case -1:
151 RETURN(RC_FAIL);
152 default:
153 ASN_DEBUG("ptr[] = %02x, advancing %zu, length=%zu", *(const uint8_t *)ptr, len_size, length);
154 ADVANCE(len_size);
155 ctx->left = length;
156 }
157 }
158 NEXT_PHASE(ctx);
159 /* FALL THROUGH */
160 case 1: {
161 /* Decode components of the extension root */
162 asn_TYPE_member_t *elm = td->elements;
163 asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
164
165 assert(td->elements_count == 1);
166
167 ASN_DEBUG("OER SET OF %s Decoding PHASE 1", td->name);
168
169 for(; ctx->left > 0; ctx->left--) {
170 asn_dec_rval_t rv = elm->type->op->oer_decoder(
Lev Walkina5972be2017-09-29 23:15:58 -0700171 opt_codec_ctx, elm->type,
172 elm->encoding_constraints.oer_constraints, &ctx->ptr, ptr,
Lev Walkinc6bd3592017-08-30 17:36:23 -0700173 size);
174 ADVANCE(rv.consumed);
175 switch(rv.code) {
176 case RC_OK:
177 if(ASN_SET_ADD(list, ctx->ptr) != 0) {
178 RETURN(RC_FAIL);
179 } else {
180 ctx->ptr = 0;
181 break;
182 }
183 case RC_WMORE:
184 RETURN(RC_WMORE);
185 case RC_FAIL:
186 ASN_STRUCT_FREE(*elm->type, ctx->ptr);
187 ctx->ptr = 0;
188 SET_PHASE(ctx, 3);
189 RETURN(RC_FAIL);
190 }
191 }
192 /* Decoded decently. */
193 NEXT_PHASE(ctx);
194 }
195 /* Fall through */
196 case 2:
197 /* Ignore fully decoded */
198 assert(ctx->left == 0);
199 RETURN(RC_OK);
200 case 3:
201 /* Failed to decode. */
202 RETURN(RC_FAIL);
203 }
204
205 return rval;
206}
207
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700208static ssize_t
209oer_put_quantity(size_t qty, asn_app_consume_bytes_f *cb, void *app_key) {
210 uint8_t buf[1 + sizeof(size_t)];
211 uint8_t *b = &buf[sizeof(size_t)]; /* Last addressable */
212 size_t encoded;
213
214 do {
215 *b-- = qty;
216 qty >>= 8;
217 } while(qty);
218
219 *b = sizeof(buf) - (b-buf) - 1;
220 encoded = sizeof(buf) - (b-buf);
221 if(cb(b, encoded, app_key) < 0)
222 return -1;
223 return encoded;
224}
225
Lev Walkinc6bd3592017-08-30 17:36:23 -0700226/*
227 * Encode as Canonical OER.
228 */
229asn_enc_rval_t
230SET_OF_encode_oer(asn_TYPE_descriptor_t *td,
231 const asn_oer_constraints_t *constraints, void *sptr,
232 asn_app_consume_bytes_f *cb, void *app_key) {
233 size_t computed_size = 0;
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700234 ssize_t qty_len;
235 asn_TYPE_member_t *elm;
236 asn_anonymous_set_ *list;
Lev Walkin71191ba2017-08-31 01:00:00 -0700237 int n;
Lev Walkinc6bd3592017-08-30 17:36:23 -0700238
239 (void)constraints;
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700240
241 if(!sptr) ASN__ENCODE_FAILED;
242
243 elm = td->elements;
244 list = _A_SET_FROM_VOID(sptr);
245
246 qty_len = oer_put_quantity(list->count, cb, app_key);
247 if(qty_len < 0) {
248 ASN__ENCODE_FAILED;
249 }
250 computed_size += qty_len;
251
252 for(n = 0; n < list->count; n++) {
253 void *memb_ptr = list->array[n];
254 asn_enc_rval_t er;
Lev Walkina5972be2017-09-29 23:15:58 -0700255 er = elm->type->op->oer_encoder(
256 elm->type, elm->encoding_constraints.oer_constraints, memb_ptr, cb,
257 app_key);
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700258 if(er.encoded < 0) {
259 return er;
260 } else {
261 computed_size += er.encoded;
262 }
263 }
Lev Walkinc6bd3592017-08-30 17:36:23 -0700264
265 {
Lev Walkineff98a52017-09-13 22:24:35 +0000266 asn_enc_rval_t erval;
267 erval.encoded = computed_size;
268 ASN__ENCODED_OK(erval);
Lev Walkinc6bd3592017-08-30 17:36:23 -0700269 }
270}
271
272#endif /* ASN_DISABLE_OER_SUPPORT */