2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_internal.h>
6 #include <asn_codecs_prim.h>
9 * The OER decoder of any type.
12 oer_decode(const asn_codec_ctx_t *opt_codec_ctx,
13 const asn_TYPE_descriptor_t *type_descriptor, void **struct_ptr,
14 const void *ptr, size_t size) {
15 asn_codec_ctx_t s_codec_ctx;
18 * Stack checker requires that the codec context
19 * must be allocated on the stack.
22 if(opt_codec_ctx->max_stack_size) {
23 s_codec_ctx = *opt_codec_ctx;
24 opt_codec_ctx = &s_codec_ctx;
27 /* If context is not given, be security-conscious anyway */
28 memset(&s_codec_ctx, 0, sizeof(s_codec_ctx));
29 s_codec_ctx.max_stack_size = ASN__DEFAULT_STACK_MAX;
30 opt_codec_ctx = &s_codec_ctx;
34 * Invoke type-specific decoder.
36 return type_descriptor->op->oer_decoder(opt_codec_ctx, type_descriptor, 0,
37 struct_ptr, /* Pointer to the destination structure */
38 ptr, size /* Buffer and its size */
43 * Open Type is encoded as a length (#8.6) followed by that number of bytes.
44 * Since we're just skipping, reading the length would be enough.
47 oer_open_type_skip(const void *bufptr, size_t size) {
49 return oer_fetch_length(bufptr, size, &len);
53 * Read the Open Type (X.696 (08/2015), #30).
55 * 0: More data expected than bufptr contains.
56 * -1: Fatal error deciphering length.
57 * >0: Number of bytes used from bufptr.
60 oer_open_type_get(const asn_codec_ctx_t *opt_codec_ctx,
61 const struct asn_TYPE_descriptor_s *td,
62 const asn_oer_constraints_t *constraints, void **struct_ptr,
63 const void *bufptr, size_t size) {
65 size_t container_len = 0;
67 enum asn_struct_free_method dispose_method =
68 (*struct_ptr) ? ASFM_FREE_UNDERLYING_AND_RESET : ASFM_FREE_EVERYTHING;
70 /* Get the size of a length determinant */
71 len_len = oer_fetch_length(bufptr, size, &container_len);
73 return len_len; /* Error or more data expected */
77 * len_len can't be bigger than size, but size without len_len
78 * should be bigger or equal to container length
80 if(size - len_len < container_len) {
81 /* More data is expected */
85 dr = td->op->oer_decoder(opt_codec_ctx, td, constraints, struct_ptr,
86 (const uint8_t *)bufptr + len_len, container_len);
87 if(dr.code == RC_OK) {
88 return len_len + container_len;
90 /* Even if RC_WMORE, we can't get more data into a closed container. */
91 td->op->free_struct(td, *struct_ptr, dispose_method);
99 oer_decode_primitive(const asn_codec_ctx_t *opt_codec_ctx,
100 const asn_TYPE_descriptor_t *td,
101 const asn_oer_constraints_t *constraints, void **sptr,
102 const void *ptr, size_t size) {
103 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr;
104 asn_dec_rval_t rval = {RC_OK, 0};
105 size_t expected_length = 0;
113 st = (ASN__PRIMITIVE_TYPE_t *)(*sptr = CALLOC(
114 1, sizeof(ASN__PRIMITIVE_TYPE_t)));
115 if(!st) ASN__DECODE_FAILED;
120 * X.696 (08/2015) #27.2
121 * Encode length determinant as _number of octets_, but only
122 * if upper bound is not equal to lower bound.
124 len_len = oer_fetch_length(ptr, size, &expected_length);
126 rval.consumed = len_len;
127 ptr = (const char *)ptr + len_len;
129 } else if(len_len == 0) {
131 } else if(len_len < 0) {
135 if(size < expected_length) {
138 uint8_t *buf = MALLOC(expected_length + 1);
142 memcpy(buf, ptr, expected_length);
143 buf[expected_length] = '\0';
147 st->size = expected_length;
149 rval.consumed += expected_length;