2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <constr_SET_OF.h>
8 #include <asn_SET_OF.h>
11 * Number of bytes left for this structure.
12 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
13 * (size) contains the number of bytes in the buffer passed.
15 #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
18 * If the subprocessor function returns with an indication that it wants
19 * more data, it may well be a fatal decoding problem, because the
20 * size is constrained by the <TLV>'s L, even if the buffer size allows
22 * For example, consider the buffer containing the following TLVs:
23 * <T:5><L:1><V> <T:6>...
24 * The TLV length clearly indicates that one byte is expected in V, but
25 * if the V processor returns with "want more data" even if the buffer
26 * contains way more data than the V processor have seen.
28 #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
31 * This macro "eats" the part of the buffer which is definitely "consumed",
32 * i.e. was correctly converted into local representation or rightfully skipped.
35 #define ADVANCE(num_bytes) \
37 size_t num = num_bytes; \
38 ptr = ((const char *)ptr) + num; \
42 consumed_myself += num; \
46 * Switch to the next phase of parsing.
49 #define NEXT_PHASE(ctx) \
55 #define PHASE_OUT(ctx) \
61 * Return a standardized complex structure.
64 #define RETURN(_code) \
67 rval.consumed = consumed_myself; \
72 * The decoder of the SET OF type.
75 SET_OF_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
76 const asn_TYPE_descriptor_t *td, void **struct_ptr,
77 const void *ptr, size_t size, int tag_mode) {
79 * Bring closer parts of structure description.
81 const asn_SET_OF_specifics_t *specs = (const asn_SET_OF_specifics_t *)td->specifics;
82 const asn_TYPE_member_t *elm = td->elements; /* Single one */
85 * Parts of the structure being constructed.
87 void *st = *struct_ptr; /* Target structure. */
88 asn_struct_ctx_t *ctx; /* Decoder context */
90 ber_tlv_tag_t tlv_tag; /* T from TLV */
91 asn_dec_rval_t rval; /* Return code from subparsers */
93 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
95 ASN_DEBUG("Decoding %s as SET OF", td->name);
98 * Create the target structure if it is not present already.
101 st = *struct_ptr = CALLOC(1, specs->struct_size);
108 * Restore parsing context.
110 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
113 * Start to parse where left previously
119 * Check that the set of tags associated with given structure
120 * perfectly fits our expectations.
123 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
124 tag_mode, 1, &ctx->left, 0);
125 if(rval.code != RC_OK) {
126 ASN_DEBUG("%s tagging check failed: %d",
127 td->name, rval.code);
132 ctx->left += rval.consumed; /* ?Subtracted below! */
133 ADVANCE(rval.consumed);
135 ASN_DEBUG("Structure consumes %ld bytes, "
136 "buffer %ld", (long)ctx->left, (long)size);
143 * From the place where we've left it previously,
144 * try to decode the next item.
146 for(;; ctx->step = 0) {
147 ssize_t tag_len; /* Length of TLV's T */
153 * MICROPHASE 1: Synchronize decoding.
157 ASN_DEBUG("End of SET OF %s", td->name);
159 * No more things to decode.
167 * Fetch the T from TLV.
169 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
171 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
173 case -1: RETURN(RC_FAIL);
176 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
182 } else if(((const uint8_t *)ptr)[1] == 0) {
184 * Found the terminator of the
185 * indefinite length structure.
191 /* Outmost tag may be unknown and cannot be fetched/compared */
192 if(elm->tag != (ber_tlv_tag_t)-1) {
193 if(BER_TAGS_EQUAL(tlv_tag, elm->tag)) {
195 * The new list member of expected type has arrived.
198 ASN_DEBUG("Unexpected tag %s fixed SET OF %s",
199 ber_tlv_tag_string(tlv_tag), td->name);
200 ASN_DEBUG("%s SET OF has tag %s",
201 td->name, ber_tlv_tag_string(elm->tag));
207 * MICROPHASE 2: Invoke the member-specific decoder.
209 ctx->step |= 1; /* Confirm entering next microphase */
213 * Invoke the member fetch routine according to member's type
215 rval = elm->type->op->ber_decoder(opt_codec_ctx,
216 elm->type, &ctx->ptr,
218 ASN_DEBUG("In %s SET OF %s code %d consumed %d",
219 td->name, elm->type->name,
220 rval.code, (int)rval.consumed);
224 asn_anonymous_set_ *list = _A_SET_FROM_VOID(st);
225 if(ASN_SET_ADD(list, ctx->ptr) != 0)
231 case RC_WMORE: /* More data expected */
232 if(!SIZE_VIOLATION) {
233 ADVANCE(rval.consumed);
237 case RC_FAIL: /* Fatal error */
238 ASN_STRUCT_FREE(*elm->type, ctx->ptr);
243 ADVANCE(rval.consumed);
244 } /* for(all list members) */
249 * Read in all "end of content" TLVs.
251 while(ctx->left < 0) {
253 if(LEFT > 0 && ((const char *)ptr)[0] != 0) {
260 if(((const char *)ptr)[0] == 0
261 && ((const char *)ptr)[1] == 0) {
276 * The DER encoder of the SET OF type.
279 SET_OF_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
280 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
282 const asn_TYPE_member_t *elm = td->elements;
283 const asn_anonymous_set_ *list = _A_CSET_FROM_VOID(sptr);
284 size_t computed_size = 0;
285 ssize_t encoding_size = 0;
286 struct _el_buffer *encoded_els;
289 ASN_DEBUG("Estimating size for SET OF %s", td->name);
292 * Gather the length of the underlying members sequence.
294 for(edx = 0; edx < list->count; edx++) {
295 void *memb_ptr = list->array[edx];
296 asn_enc_rval_t erval = {0,0,0};
298 if(!memb_ptr) ASN__ENCODE_FAILED;
301 elm->type->op->der_encoder(elm->type, memb_ptr, 0, elm->tag, 0, 0);
302 if(erval.encoded == -1) return erval;
303 computed_size += erval.encoded;
307 * Encode the TLV for the sequence itself.
310 der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
311 if(encoding_size < 0) {
314 computed_size += encoding_size;
316 if(!cb || list->count == 0) {
317 asn_enc_rval_t erval = {0,0,0};
318 erval.encoded = computed_size;
319 ASN__ENCODED_OK(erval);
322 ASN_DEBUG("Encoding members of %s SET OF", td->name);
325 * DER mandates dynamic sorting of the SET OF elements
326 * according to their encodings. Build an array of the
329 encoded_els = SET_OF__encode_sorted(elm, list, SOES_DER);
332 * Report encoded elements to the application.
333 * Dispose of temporary sorted members table.
335 for(edx = 0; edx < list->count; edx++) {
336 struct _el_buffer *encoded_el = &encoded_els[edx];
337 /* Report encoded chunks to the application */
338 if(cb(encoded_el->buf, encoded_el->length, app_key) < 0) {
341 encoding_size += encoded_el->length;
345 SET_OF__encode_sorted_free(encoded_els, list->count);
347 if(edx == list->count) {
348 asn_enc_rval_t erval = {0,0,0};
349 assert(computed_size == (size_t)encoding_size);
350 erval.encoded = computed_size;
351 ASN__ENCODED_OK(erval);