blob: 1357c2b6e644839ba5576cddd54adc1ef441f5bf [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 Walkin20696a42017-10-17 21:27:33 -0700110SET_OF_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
111 const asn_TYPE_descriptor_t *td,
112 const asn_oer_constraints_t *constraints, void **struct_ptr,
113 const void *ptr, size_t size) {
Lev Walkind84f6032017-10-03 16:33:59 -0700114 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
Lev Walkinc6bd3592017-08-30 17:36:23 -0700115 asn_dec_rval_t rval = {RC_OK, 0};
116 void *st = *struct_ptr; /* Target structure */
117 asn_struct_ctx_t *ctx; /* Decoder context */
118 size_t consumed_myself = 0; /* Consumed bytes from ptr. */
119
120 (void)opt_codec_ctx;
121 (void)constraints;
122
123 /*
124 * Create the target structure if it is not present already.
125 */
126 if(st == 0) {
127 st = *struct_ptr = CALLOC(1, specs->struct_size);
128 if(st == 0) {
129 RETURN(RC_FAIL);
130 }
131 }
132
133 /*
134 * Restore parsing context.
135 */
136 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
137
138 /*
139 * Start to parse where left previously.
140 */
141 switch(ctx->phase) {
142 case 0: {
143 /*
144 * Fetch number of elements to decode.
145 */
146 size_t length = 0;
147 size_t len_size = oer_fetch_quantity(ptr, size, &length);
148 switch(len_size) {
149 case 0:
150 RETURN(RC_WMORE);
151 case -1:
152 RETURN(RC_FAIL);
153 default:
154 ASN_DEBUG("ptr[] = %02x, advancing %zu, length=%zu", *(const uint8_t *)ptr, len_size, length);
155 ADVANCE(len_size);
156 ctx->left = length;
157 }
158 }
159 NEXT_PHASE(ctx);
160 /* FALL THROUGH */
161 case 1: {
162 /* Decode components of the extension root */
163 asn_TYPE_member_t *elm = td->elements;
164 asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
165
166 assert(td->elements_count == 1);
167
168 ASN_DEBUG("OER SET OF %s Decoding PHASE 1", td->name);
169
170 for(; ctx->left > 0; ctx->left--) {
171 asn_dec_rval_t rv = elm->type->op->oer_decoder(
Lev Walkina5972be2017-09-29 23:15:58 -0700172 opt_codec_ctx, elm->type,
173 elm->encoding_constraints.oer_constraints, &ctx->ptr, ptr,
Lev Walkinc6bd3592017-08-30 17:36:23 -0700174 size);
175 ADVANCE(rv.consumed);
176 switch(rv.code) {
177 case RC_OK:
178 if(ASN_SET_ADD(list, ctx->ptr) != 0) {
179 RETURN(RC_FAIL);
180 } else {
181 ctx->ptr = 0;
182 break;
183 }
184 case RC_WMORE:
185 RETURN(RC_WMORE);
186 case RC_FAIL:
187 ASN_STRUCT_FREE(*elm->type, ctx->ptr);
188 ctx->ptr = 0;
189 SET_PHASE(ctx, 3);
190 RETURN(RC_FAIL);
191 }
192 }
193 /* Decoded decently. */
194 NEXT_PHASE(ctx);
195 }
196 /* Fall through */
197 case 2:
198 /* Ignore fully decoded */
199 assert(ctx->left == 0);
200 RETURN(RC_OK);
201 case 3:
202 /* Failed to decode. */
203 RETURN(RC_FAIL);
204 }
205
206 return rval;
207}
208
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700209static ssize_t
210oer_put_quantity(size_t qty, asn_app_consume_bytes_f *cb, void *app_key) {
211 uint8_t buf[1 + sizeof(size_t)];
212 uint8_t *b = &buf[sizeof(size_t)]; /* Last addressable */
213 size_t encoded;
214
215 do {
216 *b-- = qty;
217 qty >>= 8;
218 } while(qty);
219
220 *b = sizeof(buf) - (b-buf) - 1;
221 encoded = sizeof(buf) - (b-buf);
222 if(cb(b, encoded, app_key) < 0)
223 return -1;
224 return encoded;
225}
226
Lev Walkinc6bd3592017-08-30 17:36:23 -0700227/*
228 * Encode as Canonical OER.
229 */
230asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700231SET_OF_encode_oer(const asn_TYPE_descriptor_t *td,
232 const asn_oer_constraints_t *constraints, const void *sptr,
233 asn_app_consume_bytes_f *cb, void *app_key) {
234 const asn_TYPE_member_t *elm;
235 const asn_anonymous_set_ *list;
Lev Walkinc6bd3592017-08-30 17:36:23 -0700236 size_t computed_size = 0;
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700237 ssize_t qty_len;
Lev Walkin71191ba2017-08-31 01:00:00 -0700238 int n;
Lev Walkinc6bd3592017-08-30 17:36:23 -0700239
240 (void)constraints;
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700241
242 if(!sptr) ASN__ENCODE_FAILED;
243
244 elm = td->elements;
Lev Walkin20696a42017-10-17 21:27:33 -0700245 list = _A_CSET_FROM_VOID(sptr);
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700246
247 qty_len = oer_put_quantity(list->count, cb, app_key);
248 if(qty_len < 0) {
249 ASN__ENCODE_FAILED;
250 }
251 computed_size += qty_len;
252
253 for(n = 0; n < list->count; n++) {
254 void *memb_ptr = list->array[n];
255 asn_enc_rval_t er;
Lev Walkina5972be2017-09-29 23:15:58 -0700256 er = elm->type->op->oer_encoder(
257 elm->type, elm->encoding_constraints.oer_constraints, memb_ptr, cb,
258 app_key);
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700259 if(er.encoded < 0) {
260 return er;
261 } else {
262 computed_size += er.encoded;
263 }
264 }
Lev Walkinc6bd3592017-08-30 17:36:23 -0700265
266 {
Lev Walkineff98a52017-09-13 22:24:35 +0000267 asn_enc_rval_t erval;
268 erval.encoded = computed_size;
269 ASN__ENCODED_OK(erval);
Lev Walkinc6bd3592017-08-30 17:36:23 -0700270 }
271}
272
273#endif /* ASN_DISABLE_OER_SUPPORT */