blob: 8cbf81fe0f2f2ec5f42d0ffd7cc09aafb8a6a2ec [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) {
66 size_t len = 0;
67 size_t qty;
68
69 ssize_t len_len = oer_fetch_length(ptr, size, &len);
70 if(len_len <= 0) {
71 *qty_r = 0;
72 return len_len;
73 }
74
75 if((len_len + len) > size) {
76 *qty_r = 0;
77 return 0;
78 }
79
80 const uint8_t *b = (const uint8_t *)ptr + len_len;
Lev Walkinfc89b9d2017-08-31 00:54:38 -070081 const uint8_t *bend = b + len;
Lev Walkinc6bd3592017-08-30 17:36:23 -070082
83
84 /* Skip the leading 0-bytes */
85 for(; b < bend && *b == 0; b++) {
86 }
87
88
89 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) {
113 asn_SET_OF_specifics_t *specs = (asn_SET_OF_specifics_t *)td->specifics;
114 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(
171 opt_codec_ctx, elm->type, elm->oer_constraints, &ctx->ptr, ptr,
172 size);
173 ADVANCE(rv.consumed);
174 switch(rv.code) {
175 case RC_OK:
176 if(ASN_SET_ADD(list, ctx->ptr) != 0) {
177 RETURN(RC_FAIL);
178 } else {
179 ctx->ptr = 0;
180 break;
181 }
182 case RC_WMORE:
183 RETURN(RC_WMORE);
184 case RC_FAIL:
185 ASN_STRUCT_FREE(*elm->type, ctx->ptr);
186 ctx->ptr = 0;
187 SET_PHASE(ctx, 3);
188 RETURN(RC_FAIL);
189 }
190 }
191 /* Decoded decently. */
192 NEXT_PHASE(ctx);
193 }
194 /* Fall through */
195 case 2:
196 /* Ignore fully decoded */
197 assert(ctx->left == 0);
198 RETURN(RC_OK);
199 case 3:
200 /* Failed to decode. */
201 RETURN(RC_FAIL);
202 }
203
204 return rval;
205}
206
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700207static ssize_t
208oer_put_quantity(size_t qty, asn_app_consume_bytes_f *cb, void *app_key) {
209 uint8_t buf[1 + sizeof(size_t)];
210 uint8_t *b = &buf[sizeof(size_t)]; /* Last addressable */
211 size_t encoded;
212
213 do {
214 *b-- = qty;
215 qty >>= 8;
216 } while(qty);
217
218 *b = sizeof(buf) - (b-buf) - 1;
219 encoded = sizeof(buf) - (b-buf);
220 if(cb(b, encoded, app_key) < 0)
221 return -1;
222 return encoded;
223}
224
Lev Walkinc6bd3592017-08-30 17:36:23 -0700225/*
226 * Encode as Canonical OER.
227 */
228asn_enc_rval_t
229SET_OF_encode_oer(asn_TYPE_descriptor_t *td,
230 const asn_oer_constraints_t *constraints, void *sptr,
231 asn_app_consume_bytes_f *cb, void *app_key) {
232 size_t computed_size = 0;
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700233 ssize_t qty_len;
234 asn_TYPE_member_t *elm;
235 asn_anonymous_set_ *list;
Lev Walkin71191ba2017-08-31 01:00:00 -0700236 int n;
Lev Walkinc6bd3592017-08-30 17:36:23 -0700237
238 (void)constraints;
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700239
240 if(!sptr) ASN__ENCODE_FAILED;
241
242 elm = td->elements;
243 list = _A_SET_FROM_VOID(sptr);
244
245 qty_len = oer_put_quantity(list->count, cb, app_key);
246 if(qty_len < 0) {
247 ASN__ENCODE_FAILED;
248 }
249 computed_size += qty_len;
250
251 for(n = 0; n < list->count; n++) {
252 void *memb_ptr = list->array[n];
253 asn_enc_rval_t er;
254 er = elm->type->op->oer_encoder(elm->type, elm->oer_constraints,
255 memb_ptr, cb, app_key);
256 if(er.encoded < 0) {
257 return er;
258 } else {
259 computed_size += er.encoded;
260 }
261 }
Lev Walkinc6bd3592017-08-30 17:36:23 -0700262
263 {
Lev Walkinfc89b9d2017-08-31 00:54:38 -0700264 asn_enc_rval_t erval = {computed_size, 0, 0};
265 return erval;
Lev Walkinc6bd3592017-08-30 17:36:23 -0700266 }
267}
268
269#endif /* ASN_DISABLE_OER_SUPPORT */