blob: 089e54ee8b1d6478096e7eaef8218420c636cf06 [file] [log] [blame]
Lev Walkin4fd73182004-10-21 11:20:50 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <asn_internal.h>
6#include <asn_codecs_prim.h>
7#include <assert.h>
8#include <errno.h>
9
10/*
11 * Decode an always-primitive type.
12 */
13asn_dec_rval_t
14ber_decode_primitive(asn_codec_ctx_t *opt_codec_ctx,
15 asn_TYPE_descriptor_t *td,
16 void **sptr, void *buf_ptr, size_t size, int tag_mode) {
17 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)*sptr;
18 asn_dec_rval_t rval;
19 ber_tlv_len_t length;
20
21 /*
22 * If the structure is not there, allocate it.
23 */
24 if(st == NULL) {
25 (void *)st = *sptr = CALLOC(1, sizeof(*st));
26 if(st == NULL) {
27 rval.code = RC_FAIL;
28 rval.consumed = 0;
29 return rval;
30 }
31 }
32
33 ASN_DEBUG("Decoding %s as plain primitive (tm=%d)",
34 td->name, tag_mode);
35
36 /*
37 * Check tags and extract value length.
38 */
39 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
40 tag_mode, 0, &length, 0);
41 if(rval.code != RC_OK)
42 return rval;
43
44 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
45
46 /*
47 * Make sure we have this length.
48 */
49 buf_ptr = ((char *)buf_ptr) + rval.consumed;
50 size -= rval.consumed;
51 if(length > (ber_tlv_len_t)size) {
52 rval.code = RC_WMORE;
53 rval.consumed = 0;
54 return rval;
55 }
56
57 st->buf = (uint8_t *)MALLOC(length + 1);
58 if(st->buf) {
59 st->size = length;
60 } else {
61 rval.code = RC_FAIL;
62 rval.consumed = 0;
63 return rval;
64 }
65
66 memcpy(st->buf, buf_ptr, length);
67 st->buf[length] = '\0'; /* Just in case */
68
69 rval.code = RC_OK;
70 rval.consumed += length;
71
72 ASN_DEBUG("Took %ld/%ld bytes to encode %s",
73 (long)rval.consumed,
74 (long)length, td->name);
75
76 return rval;
77}
78
79/*
80 * Encode an always-primitive type using DER.
81 */
82asn_enc_rval_t
83der_encode_primitive(asn_TYPE_descriptor_t *td, void *sptr,
84 int tag_mode, ber_tlv_tag_t tag,
85 asn_app_consume_bytes_f *cb, void *app_key) {
86 asn_enc_rval_t erval;
87 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr;
88
89 ASN_DEBUG("%s %s as a primitive type (tm=%d)",
90 cb?"Encoding":"Estimating", td->name, tag_mode);
91
92 erval.encoded = der_write_tags(td, st->size, tag_mode, 0, tag,
93 cb, app_key);
94 ASN_DEBUG("%s wrote tags %d", td->name, (int)erval.encoded);
95 if(erval.encoded == -1) {
96 erval.failed_type = td;
97 erval.structure_ptr = sptr;
98 return erval;
99 }
100
101 if(cb && st->buf) {
102 if(cb(st->buf, st->size, app_key) < 0) {
103 erval.encoded = -1;
104 erval.failed_type = td;
105 erval.structure_ptr = sptr;
106 return erval;
107 }
108 } else {
109 assert(st->buf || st->size == 0);
110 }
111
112 erval.encoded += st->size;
113
114 return erval;
115}
116
117void
118ASN__PRIMITIVE_TYPE_free(asn_TYPE_descriptor_t *td, void *sptr,
119 int contents_only) {
120 ASN__PRIMITIVE_TYPE_t *st = (ASN__PRIMITIVE_TYPE_t *)sptr;
121
122 if(!td || !sptr)
123 return;
124
125 ASN_DEBUG("Freeing %s as a primitive type", td->name);
126
127 if(st->buf)
128 FREEMEM(st->buf);
129
130 if(!contents_only)
131 FREEMEM(st);
132}
133
134
135/*
136 * Local internal type passed around as an argument.
137 */
138struct xdp_arg_s {
Lev Walkin8471cec2004-10-21 14:02:19 +0000139 void *struct_key;
140 ssize_t (*prim_body_decode)(void *struct_key,
Lev Walkin4fd73182004-10-21 11:20:50 +0000141 void *chunk_buf, size_t chunk_size);
142 int decoded_something;
143 int want_more;
144};
145
Lev Walkin0be3a992004-10-21 12:11:57 +0000146
Lev Walkin4fd73182004-10-21 11:20:50 +0000147static int
148xer_decode__unexpected_tag(void *key, void *chunk_buf, size_t chunk_size) {
149 struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
150 ssize_t decoded;
151
152 if(arg->decoded_something) {
Lev Walkindde25b32004-10-22 08:17:16 +0000153 if(xer_is_whitespace(chunk_buf, chunk_size))
Lev Walkin0be3a992004-10-21 12:11:57 +0000154 return chunk_size;
Lev Walkin4fd73182004-10-21 11:20:50 +0000155 /*
156 * Decoding was done once already. Prohibit doing it again.
157 */
158 return -1;
159 }
160
Lev Walkin8471cec2004-10-21 14:02:19 +0000161 decoded = arg->prim_body_decode(arg->struct_key, chunk_buf, chunk_size);
Lev Walkin4fd73182004-10-21 11:20:50 +0000162 if(decoded < 0) {
163 return -1;
164 } else {
165 /* Tag decoded successfully */
166 arg->decoded_something = 1;
167 return 0;
168 }
169}
170
171static ssize_t
172xer_decode__body(void *key, void *chunk_buf, size_t chunk_size, int have_more) {
173 struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
174 ssize_t decoded;
175
176 if(arg->decoded_something) {
Lev Walkindde25b32004-10-22 08:17:16 +0000177 if(xer_is_whitespace(chunk_buf, chunk_size))
Lev Walkin0be3a992004-10-21 12:11:57 +0000178 return chunk_size;
Lev Walkin4fd73182004-10-21 11:20:50 +0000179 /*
180 * Decoding was done once already. Prohibit doing it again.
181 */
182 return -1;
183 }
184
185 if(!have_more) {
186 /*
187 * If we've received something like "1", we can't really
188 * tell whether it is really `1` or `123`, until we know
189 * that there is no more data coming.
190 * The have_more argument will be set to 1 once something
191 * like this is available to the caller of this callback:
192 * "1<tag_start..."
193 */
194 arg->want_more = 1;
195 return -1;
196 }
197
Lev Walkin8471cec2004-10-21 14:02:19 +0000198 decoded = arg->prim_body_decode(arg->struct_key, chunk_buf, chunk_size);
Lev Walkin4fd73182004-10-21 11:20:50 +0000199 if(decoded < 0) {
200 return -1;
201 } else {
202 arg->decoded_something = 1;
203 return decoded;
204 }
205}
206
207
208asn_dec_rval_t
209xer_decode_primitive(asn_codec_ctx_t *opt_codec_ctx,
210 asn_TYPE_descriptor_t *td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000211 void **sptr,
212 size_t struct_size,
Lev Walkin4fd73182004-10-21 11:20:50 +0000213 const char *opt_mname,
214 void *buf_ptr, size_t size,
Lev Walkin8471cec2004-10-21 14:02:19 +0000215 ssize_t (*prim_body_decode)(void *struct_key,
Lev Walkin4fd73182004-10-21 11:20:50 +0000216 void *chunk_buf, size_t chunk_size)
217) {
218 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
219 asn_struct_ctx_t s_ctx;
220 struct xdp_arg_s s_arg;
221 asn_dec_rval_t rc;
222
223 /*
224 * Create the structure if does not exist.
225 */
226 if(!*sptr) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000227 *sptr = CALLOC(1, struct_size);
Lev Walkin4fd73182004-10-21 11:20:50 +0000228 if(!*sptr) {
229 asn_dec_rval_t rval;
230 rval.code = RC_FAIL;
231 rval.consumed = 0;
232 return rval;
233 }
234 }
235
236 memset(&s_ctx, 0, sizeof(s_ctx));
Lev Walkin8471cec2004-10-21 14:02:19 +0000237 s_arg.struct_key = *sptr;
Lev Walkin4fd73182004-10-21 11:20:50 +0000238 s_arg.prim_body_decode = prim_body_decode;
239 s_arg.decoded_something = 0;
240 s_arg.want_more = 0;
241
242 rc = xer_decode_general(opt_codec_ctx, &s_ctx, &s_arg,
243 xml_tag, buf_ptr, size,
244 xer_decode__unexpected_tag, xer_decode__body);
245 switch(rc.code) {
246 case RC_OK:
247 if(!s_arg.decoded_something) {
248 /* Opportunity has come and gone. Where's the result? */
249 rc.code = RC_FAIL;
250 rc.consumed = 0;
251 }
252 break;
253 case RC_WMORE:
254 /*
255 * Redo the whole thing later.
256 * We don't have a context to save intermediate parsing state.
257 */
258 rc.consumed = 0;
259 break;
260 case RC_FAIL:
261 rc.consumed = 0;
262 if(s_arg.want_more)
263 rc.code = RC_WMORE;
264 break;
265 }
266 return rc;
267}
268