blob: 386e846729daa01bfc838ffb81946037e6dc5d9b [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 Walkine7b73c42017-07-07 10:06:17 -070013 asn_TYPE_descriptor_t *type_descriptor,
14 void **struct_ptr, const void *ptr, size_t size) {
15 asn_codec_ctx_t s_codec_ctx;
16
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 Walkin20ea8522017-07-20 14:52:02 +030061 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;
67
68 /* Get the size of a length determinant */
69 len_len = oer_fetch_length(bufptr, size, &container_len);
70 if(len_len <= 0) {
71 return len_len; /* Error or more data expected */
72 }
73
74 /*
75 * len_len can't be bigger than size, but size without len_len
76 * should be bigger or equal to container length
77 */
78 if(size - len_len < container_len) {
79 /* More data is expected */
80 return 0;
81 }
82
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080083 dr = td->op->oer_decoder(opt_codec_ctx, td, constraints, struct_ptr,
Lev Walkin20ea8522017-07-20 14:52:02 +030084 (const uint8_t *)bufptr + len_len, container_len);
85 if(dr.code == RC_OK) {
86 return len_len + container_len;
87 } else {
88 /* Even if RC_WMORE, we can't get more data into a closed container. */
89 ASN_STRUCT_FREE(*td, *struct_ptr);
90 *struct_ptr = 0;
91 return -1;
92 }
93}
94
Lev Walkinb5b524b2017-10-13 03:14:03 -070095
96asn_dec_rval_t
97oer_decode_primitive(const asn_codec_ctx_t *opt_codec_ctx,
98 asn_TYPE_descriptor_t *td,
99 const asn_oer_constraints_t *constraints, void **sptr,
100 const void *ptr, size_t size) {
101 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr;
102 asn_dec_rval_t rval = {RC_OK, 0};
103 size_t expected_length = 0;
104 ssize_t len_len;
105
106 (void)opt_codec_ctx;
107 (void)constraints;
108
109 if(!st) {
110 st = (ASN__PRIMITIVE_TYPE_t *)(*sptr = CALLOC(
111 1, sizeof(ASN__PRIMITIVE_TYPE_t)));
112 if(!st) ASN__DECODE_FAILED;
113 }
114
115
116 /*
117 * X.696 (08/2015) #27.2
118 * Encode length determinant as _number of octets_, but only
119 * if upper bound is not equal to lower bound.
120 */
121 len_len = oer_fetch_length(ptr, size, &expected_length);
122 if(len_len > 0) {
123 rval.consumed = len_len;
124 ptr = (const char *)ptr + len_len;
125 size -= len_len;
126 } else if(len_len == 0) {
127 ASN__DECODE_STARVED;
128 } else if(len_len < 0) {
129 ASN__DECODE_FAILED;
130 }
131
132 if(size < expected_length) {
133 ASN__DECODE_STARVED;
134 } else {
135 uint8_t *buf = MALLOC(expected_length + 1);
136 if(buf == NULL) {
137 ASN__DECODE_FAILED;
138 } else {
139 memcpy(buf, ptr, expected_length);
140 buf[expected_length] = '\0';
141 }
142 FREEMEM(st->buf);
143 st->buf = buf;
144 st->size = expected_length;
145
146 rval.consumed += expected_length;
147 return rval;
148 }
149}