blob: 911a5672f9105b1e3af085628f287b76d00383c0 [file] [log] [blame]
Lev Walkine7b73c42017-07-07 10:06:17 -07001/*
2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <asn_internal.h>
Lev Walkinb5b524b2017-10-13 03:14:03 -07006#include <asn_codecs_prim.h>
Lev Walkine7b73c42017-07-07 10:06:17 -07007
8/*
9 * The OER decoder of any type.
10 */
11asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -070012oer_decode(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -070013 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;
Lev Walkine7b73c42017-07-07 10:06:17 -070016
17 /*
18 * Stack checker requires that the codec context
19 * must be allocated on the stack.
20 */
21 if(opt_codec_ctx) {
22 if(opt_codec_ctx->max_stack_size) {
23 s_codec_ctx = *opt_codec_ctx;
24 opt_codec_ctx = &s_codec_ctx;
25 }
26 } else {
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;
31 }
32
33 /*
34 * Invoke type-specific decoder.
35 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080036 return type_descriptor->op->oer_decoder(opt_codec_ctx, type_descriptor, 0,
Lev Walkine7b73c42017-07-07 10:06:17 -070037 struct_ptr, /* Pointer to the destination structure */
38 ptr, size /* Buffer and its size */
39 );
40}
Lev Walkin20ea8522017-07-20 14:52:02 +030041
42/*
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.
45 */
46ssize_t
47oer_open_type_skip(const void *bufptr, size_t size) {
48 size_t len = 0;
49 return oer_fetch_length(bufptr, size, &len);
50}
51
52/*
53 * Read the Open Type (X.696 (08/2015), #30).
54 * RETURN VALUES:
55 * 0: More data expected than bufptr contains.
56 * -1: Fatal error deciphering length.
57 * >0: Number of bytes used from bufptr.
58 */
59ssize_t
Lev Walkinafbf2a92017-09-12 23:30:27 -070060oer_open_type_get(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -070061 const struct asn_TYPE_descriptor_s *td,
Lev Walkina5972be2017-09-29 23:15:58 -070062 const asn_oer_constraints_t *constraints, void **struct_ptr,
Lev Walkin20ea8522017-07-20 14:52:02 +030063 const void *bufptr, size_t size) {
Lev Walkin20ea8522017-07-20 14:52:02 +030064 asn_dec_rval_t dr;
65 size_t container_len = 0;
66 ssize_t len_len;
Lev Walkin312795a2017-10-17 15:48:34 -070067 enum asn_struct_free_method dispose_method =
68 (*struct_ptr) ? ASFM_FREE_UNDERLYING_AND_RESET : ASFM_FREE_EVERYTHING;
Lev Walkin20ea8522017-07-20 14:52:02 +030069
70 /* Get the size of a length determinant */
71 len_len = oer_fetch_length(bufptr, size, &container_len);
72 if(len_len <= 0) {
73 return len_len; /* Error or more data expected */
74 }
75
76 /*
77 * len_len can't be bigger than size, but size without len_len
78 * should be bigger or equal to container length
79 */
80 if(size - len_len < container_len) {
81 /* More data is expected */
82 return 0;
83 }
84
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080085 dr = td->op->oer_decoder(opt_codec_ctx, td, constraints, struct_ptr,
Lev Walkin20ea8522017-07-20 14:52:02 +030086 (const uint8_t *)bufptr + len_len, container_len);
87 if(dr.code == RC_OK) {
88 return len_len + container_len;
89 } else {
90 /* Even if RC_WMORE, we can't get more data into a closed container. */
Lev Walkin312795a2017-10-17 15:48:34 -070091 td->op->free_struct(td, *struct_ptr, dispose_method);
Lev Walkin20ea8522017-07-20 14:52:02 +030092 return -1;
93 }
94}
95
Lev Walkinb5b524b2017-10-13 03:14:03 -070096
97asn_dec_rval_t
98oer_decode_primitive(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -070099 const asn_TYPE_descriptor_t *td,
Lev Walkinb5b524b2017-10-13 03:14:03 -0700100 const asn_oer_constraints_t *constraints, void **sptr,
101 const void *ptr, size_t size) {
102 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr;
103 asn_dec_rval_t rval = {RC_OK, 0};
104 size_t expected_length = 0;
105 ssize_t len_len;
106
107 (void)opt_codec_ctx;
108 (void)constraints;
109
110 if(!st) {
111 st = (ASN__PRIMITIVE_TYPE_t *)(*sptr = CALLOC(
112 1, sizeof(ASN__PRIMITIVE_TYPE_t)));
113 if(!st) ASN__DECODE_FAILED;
114 }
115
116
117 /*
118 * X.696 (08/2015) #27.2
119 * Encode length determinant as _number of octets_, but only
120 * if upper bound is not equal to lower bound.
121 */
122 len_len = oer_fetch_length(ptr, size, &expected_length);
123 if(len_len > 0) {
124 rval.consumed = len_len;
125 ptr = (const char *)ptr + len_len;
126 size -= len_len;
127 } else if(len_len == 0) {
128 ASN__DECODE_STARVED;
129 } else if(len_len < 0) {
130 ASN__DECODE_FAILED;
131 }
132
133 if(size < expected_length) {
134 ASN__DECODE_STARVED;
135 } else {
136 uint8_t *buf = MALLOC(expected_length + 1);
137 if(buf == NULL) {
138 ASN__DECODE_FAILED;
139 } else {
140 memcpy(buf, ptr, expected_length);
141 buf[expected_length] = '\0';
142 }
143 FREEMEM(st->buf);
144 st->buf = buf;
145 st->size = expected_length;
146
147 rval.consumed += expected_length;
148 return rval;
149 }
150}