blob: 070173826d05c24918b25e0ebea8f19fb9245191 [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 Walkin9d328ee2017-10-20 03:50:41 -070092 *struct_ptr = NULL;
Lev Walkin20ea8522017-07-20 14:52:02 +030093 return -1;
94 }
95}
96
Lev Walkinb5b524b2017-10-13 03:14:03 -070097
98asn_dec_rval_t
99oer_decode_primitive(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -0700100 const asn_TYPE_descriptor_t *td,
Lev Walkinb5b524b2017-10-13 03:14:03 -0700101 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;
106 ssize_t len_len;
107
Lev Walkin1c0ce5e2017-10-18 16:42:58 -0700108 (void)td;
Lev Walkinb5b524b2017-10-13 03:14:03 -0700109 (void)opt_codec_ctx;
110 (void)constraints;
111
112 if(!st) {
113 st = (ASN__PRIMITIVE_TYPE_t *)(*sptr = CALLOC(
114 1, sizeof(ASN__PRIMITIVE_TYPE_t)));
115 if(!st) ASN__DECODE_FAILED;
116 }
117
118
119 /*
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.
123 */
124 len_len = oer_fetch_length(ptr, size, &expected_length);
125 if(len_len > 0) {
126 rval.consumed = len_len;
127 ptr = (const char *)ptr + len_len;
128 size -= len_len;
129 } else if(len_len == 0) {
130 ASN__DECODE_STARVED;
131 } else if(len_len < 0) {
132 ASN__DECODE_FAILED;
133 }
134
135 if(size < expected_length) {
136 ASN__DECODE_STARVED;
137 } else {
138 uint8_t *buf = MALLOC(expected_length + 1);
139 if(buf == NULL) {
140 ASN__DECODE_FAILED;
141 } else {
142 memcpy(buf, ptr, expected_length);
143 buf[expected_length] = '\0';
144 }
145 FREEMEM(st->buf);
146 st->buf = buf;
147 st->size = expected_length;
148
149 rval.consumed += expected_length;
150 return rval;
151 }
152}