blob: e0b545a53b11c5d03abea60d386988d4efc8b933 [file] [log] [blame]
vlmc4436cf2004-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 {
139 ASN__PRIMITIVE_TYPE_t *sptr;
140 ssize_t (*prim_body_decode)(ASN__PRIMITIVE_TYPE_t *sptr,
141 void *chunk_buf, size_t chunk_size);
142 int decoded_something;
143 int want_more;
144};
145
146static int
147xer_decode__unexpected_tag(void *key, void *chunk_buf, size_t chunk_size) {
148 struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
149 ssize_t decoded;
150
151 if(arg->decoded_something) {
152 /*
153 * Decoding was done once already. Prohibit doing it again.
154 */
155 return -1;
156 }
157
158 decoded = arg->prim_body_decode(arg->sptr, chunk_buf, chunk_size);
159 if(decoded < 0) {
160 return -1;
161 } else {
162 /* Tag decoded successfully */
163 arg->decoded_something = 1;
164 return 0;
165 }
166}
167
168static ssize_t
169xer_decode__body(void *key, void *chunk_buf, size_t chunk_size, int have_more) {
170 struct xdp_arg_s *arg = (struct xdp_arg_s *)key;
171 ssize_t decoded;
172
173 if(arg->decoded_something) {
174 /*
175 * Decoding was done once already. Prohibit doing it again.
176 */
177 return -1;
178 }
179
180 if(!have_more) {
181 /*
182 * If we've received something like "1", we can't really
183 * tell whether it is really `1` or `123`, until we know
184 * that there is no more data coming.
185 * The have_more argument will be set to 1 once something
186 * like this is available to the caller of this callback:
187 * "1<tag_start..."
188 */
189 arg->want_more = 1;
190 return -1;
191 }
192
193 decoded = arg->prim_body_decode(arg->sptr, chunk_buf, chunk_size);
194 if(decoded < 0) {
195 return -1;
196 } else {
197 arg->decoded_something = 1;
198 return decoded;
199 }
200}
201
202
203asn_dec_rval_t
204xer_decode_primitive(asn_codec_ctx_t *opt_codec_ctx,
205 asn_TYPE_descriptor_t *td,
206 ASN__PRIMITIVE_TYPE_t **sptr,
207 const char *opt_mname,
208 void *buf_ptr, size_t size,
209 ssize_t (*prim_body_decode)(ASN__PRIMITIVE_TYPE_t *sptr,
210 void *chunk_buf, size_t chunk_size)
211) {
212 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
213 asn_struct_ctx_t s_ctx;
214 struct xdp_arg_s s_arg;
215 asn_dec_rval_t rc;
216
217 /*
218 * Create the structure if does not exist.
219 */
220 if(!*sptr) {
221 *sptr = CALLOC(1, sizeof(ASN__PRIMITIVE_TYPE_t));
222 if(!*sptr) {
223 asn_dec_rval_t rval;
224 rval.code = RC_FAIL;
225 rval.consumed = 0;
226 return rval;
227 }
228 }
229
230 memset(&s_ctx, 0, sizeof(s_ctx));
231 s_arg.sptr = *sptr;
232 s_arg.prim_body_decode = prim_body_decode;
233 s_arg.decoded_something = 0;
234 s_arg.want_more = 0;
235
236 rc = xer_decode_general(opt_codec_ctx, &s_ctx, &s_arg,
237 xml_tag, buf_ptr, size,
238 xer_decode__unexpected_tag, xer_decode__body);
239 switch(rc.code) {
240 case RC_OK:
241 if(!s_arg.decoded_something) {
242 /* Opportunity has come and gone. Where's the result? */
243 rc.code = RC_FAIL;
244 rc.consumed = 0;
245 }
246 break;
247 case RC_WMORE:
248 /*
249 * Redo the whole thing later.
250 * We don't have a context to save intermediate parsing state.
251 */
252 rc.consumed = 0;
253 break;
254 case RC_FAIL:
255 rc.consumed = 0;
256 if(s_arg.want_more)
257 rc.code = RC_WMORE;
258 break;
259 }
260 return rc;
261}
262