2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_internal.h>
6 #include <asn_application.h>
9 static asn_enc_rval_t asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
10 enum asn_transfer_syntax syntax,
11 const asn_TYPE_descriptor_t *td,
13 asn_app_consume_bytes_f *callback,
17 struct callback_count_bytes_key {
18 asn_app_consume_bytes_f *callback;
24 * Encoder which just counts bytes that come through it.
27 callback_count_bytes_cb(const void *data, size_t size, void *keyp) {
28 struct callback_count_bytes_key *key = keyp;
31 ret = key->callback(data, size, key->callback_key);
33 key->computed_size += size;
39 struct overrun_encoder_key {
45 struct dynamic_encoder_key {
51 struct callback_failure_catch_key {
52 asn_app_consume_bytes_f *callback;
58 * Encoder which doesn't stop counting bytes
59 * even if it reaches the end of the buffer.
62 overrun_encoder_cb(const void *data, size_t size, void *keyp) {
63 struct overrun_encoder_key *key = keyp;
65 if(key->computed_size + size > key->buffer_size) {
67 * Avoid accident on the next call:
68 * stop adding bytes to the buffer.
72 memcpy((char *)key->buffer + key->computed_size, data, size);
74 key->computed_size += size;
80 * Encoder which dynamically allocates output, and continues
81 * to count even if allocation failed.
84 dynamic_encoder_cb(const void *data, size_t size, void *keyp) {
85 struct dynamic_encoder_key *key = keyp;
88 if(key->computed_size + size >= key->buffer_size) {
90 size_t new_size = key->buffer_size;
94 } while(new_size <= key->computed_size + size);
96 p = REALLOC(key->buffer, new_size);
99 key->buffer_size = new_size;
101 FREEMEM(key->buffer);
103 key->buffer_size = 0;
104 key->computed_size += size;
108 memcpy((char *)key->buffer + key->computed_size, data, size);
111 key->computed_size += size;
117 * Encoder which help convert the application level encoder failure into EIO.
120 callback_failure_catch_cb(const void *data, size_t size, void *keyp) {
121 struct callback_failure_catch_key *key = keyp;
124 ret = key->callback(data, size, key->callback_key);
126 key->callback_failed = 1;
133 asn_encode(const asn_codec_ctx_t *opt_codec_ctx,
134 enum asn_transfer_syntax syntax, const asn_TYPE_descriptor_t *td,
135 const void *sptr, asn_app_consume_bytes_f *callback, void *callback_key) {
136 struct callback_failure_catch_key cb_key;
144 cb_key.callback = callback;
145 cb_key.callback_key = callback_key;
146 cb_key.callback_failed = 0;
148 er = asn_encode_internal(opt_codec_ctx, syntax, td, sptr,
149 callback_failure_catch_cb, &cb_key);
150 if(cb_key.callback_failed) {
151 assert(er.encoded == -1);
152 assert(errno == EBADF);
160 asn_encode_to_buffer(const asn_codec_ctx_t *opt_codec_ctx,
161 enum asn_transfer_syntax syntax,
162 const asn_TYPE_descriptor_t *td, const void *sptr,
163 void *buffer, size_t buffer_size) {
164 struct overrun_encoder_key buf_key;
167 if(buffer_size > 0 && !buffer) {
172 buf_key.buffer = buffer;
173 buf_key.buffer_size = buffer_size;
174 buf_key.computed_size = 0;
176 er = asn_encode_internal(opt_codec_ctx, syntax, td, sptr,
177 overrun_encoder_cb, &buf_key);
179 if(er.encoded >= 0 && (size_t)er.encoded != buf_key.computed_size) {
180 ASN_DEBUG("asn_encode() returned %" ASN_PRI_SSIZE
181 " yet produced %" ASN_PRI_SIZE " bytes",
182 er.encoded, buf_key.computed_size);
183 assert(er.encoded < 0 || (size_t)er.encoded == buf_key.computed_size);
189 asn_encode_to_new_buffer_result_t
190 asn_encode_to_new_buffer(const asn_codec_ctx_t *opt_codec_ctx,
191 enum asn_transfer_syntax syntax,
192 const asn_TYPE_descriptor_t *td, const void *sptr) {
193 struct dynamic_encoder_key buf_key;
194 asn_encode_to_new_buffer_result_t res;
196 buf_key.buffer_size = 16;
197 buf_key.buffer = MALLOC(buf_key.buffer_size);
198 buf_key.computed_size = 0;
200 res.result = asn_encode_internal(opt_codec_ctx, syntax, td, sptr,
201 dynamic_encoder_cb, &buf_key);
203 if(res.result.encoded >= 0
204 && (size_t)res.result.encoded != buf_key.computed_size) {
205 ASN_DEBUG("asn_encode() returned %" ASN_PRI_SSIZE
206 " yet produced %" ASN_PRI_SIZE " bytes",
207 res.result.encoded, buf_key.computed_size);
208 assert(res.result.encoded < 0
209 || (size_t)res.result.encoded == buf_key.computed_size);
212 res.buffer = buf_key.buffer;
214 /* 0-terminate just in case. */
216 assert(buf_key.computed_size < buf_key.buffer_size);
217 ((char *)res.buffer)[buf_key.computed_size] = '\0';
223 static asn_enc_rval_t
224 asn_encode_internal(const asn_codec_ctx_t *opt_codec_ctx,
225 enum asn_transfer_syntax syntax,
226 const asn_TYPE_descriptor_t *td, const void *sptr,
227 asn_app_consume_bytes_f *callback, void *callback_key) {
229 enum xer_encoder_flags_e xer_flags = XER_F_CANONICAL;
231 (void)opt_codec_ctx; /* Parameters are not checked on encode yet. */
239 case ATS_NONSTANDARD_PLAINTEXT:
240 if(td->op->print_struct) {
241 struct callback_count_bytes_key cb_key;
242 cb_key.callback = callback;
243 cb_key.callback_key = callback_key;
244 cb_key.computed_size = 0;
245 if(td->op->print_struct(td, sptr, 1, callback_count_bytes_cb,
248 || callback_count_bytes_cb("\n", 1, &cb_key) < 0) {
249 errno = EBADF; /* Structure has incorrect form. */
252 er.structure_ptr = sptr;
254 er.encoded = cb_key.computed_size;
256 er.structure_ptr = 0;
259 errno = ENOENT; /* Transfer syntax is not defined for this type. */
265 errno = ENOENT; /* Randomization doesn't make sense on output. */
269 /* BER is a superset of DER. */
272 if(td->op->der_encoder) {
273 er = der_encode(td, sptr, callback, callback_key);
274 if(er.encoded == -1) {
275 if(er.failed_type && er.failed_type->op->der_encoder) {
276 errno = EBADF; /* Structure has incorrect form. */
278 errno = ENOENT; /* DER is not defined for this type. */
282 errno = ENOENT; /* Transfer syntax is not defined for this type. */
287 errno = ENOENT; /* Transfer syntax is not defined for any type. */
290 #ifdef ASN_DISABLE_OER_SUPPORT
292 case ATS_CANONICAL_OER:
293 errno = ENOENT; /* PER is not defined. */
296 #else /* ASN_DISABLE_OER_SUPPORT */
298 /* CANONICAL-OER is a superset of BASIC-OER. */
300 case ATS_CANONICAL_OER:
301 if(td->op->oer_encoder) {
302 er = oer_encode(td, sptr, callback, callback_key);
303 if(er.encoded == -1) {
304 if(er.failed_type && er.failed_type->op->oer_encoder) {
305 errno = EBADF; /* Structure has incorrect form. */
307 errno = ENOENT; /* OER is not defined for this type. */
311 errno = ENOENT; /* Transfer syntax is not defined for this type. */
315 #endif /* ASN_DISABLE_OER_SUPPORT */
317 #ifdef ASN_DISABLE_PER_SUPPORT
318 case ATS_UNALIGNED_BASIC_PER:
319 case ATS_UNALIGNED_CANONICAL_PER:
320 errno = ENOENT; /* PER is not defined. */
323 #else /* ASN_DISABLE_PER_SUPPORT */
324 case ATS_UNALIGNED_BASIC_PER:
325 /* CANONICAL-UPER is a superset of BASIC-UPER. */
327 case ATS_UNALIGNED_CANONICAL_PER:
328 if(td->op->uper_encoder) {
329 er = uper_encode(td, 0, sptr, callback, callback_key);
330 if(er.encoded == -1) {
331 if(er.failed_type && er.failed_type->op->uper_encoder) {
332 errno = EBADF; /* Structure has incorrect form. */
334 errno = ENOENT; /* UPER is not defined for this type. */
337 ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
338 if(er.encoded == 0) {
339 /* Enforce "Complete Encoding" of X.691 #11.1 */
340 if(callback("\0", 1, callback_key) < 0) {
344 er.encoded = 8; /* Exactly 8 zero bits is added. */
346 /* Convert bits into bytes */
347 er.encoded = (er.encoded + 7) >> 3;
350 errno = ENOENT; /* Transfer syntax is not defined for this type. */
354 #endif /* ASN_DISABLE_PER_SUPPORT */
357 /* CANONICAL-XER is a superset of BASIC-XER. */
358 xer_flags &= ~XER_F_CANONICAL;
359 xer_flags |= XER_F_BASIC;
361 case ATS_CANONICAL_XER:
362 if(td->op->xer_encoder) {
363 er = xer_encode(td, sptr, xer_flags, callback, callback_key);
364 if(er.encoded == -1) {
365 if(er.failed_type && er.failed_type->op->xer_encoder) {
366 errno = EBADF; /* Structure has incorrect form. */
368 errno = ENOENT; /* XER is not defined for this type. */
372 errno = ENOENT; /* Transfer syntax is not defined for this type. */
386 asn_decode(const asn_codec_ctx_t *opt_codec_ctx,
387 enum asn_transfer_syntax syntax, const asn_TYPE_descriptor_t *td,
388 void **sptr, const void *buffer, size_t size) {
389 if(!td || !td->op || !sptr || (size && !buffer)) {
395 case ATS_NONSTANDARD_PLAINTEXT:
401 if(!td->op->random_fill) {
404 if(asn_random_fill(td, sptr, 16000) == 0) {
405 asn_dec_rval_t ret = {RC_OK, 0};
415 return ber_decode(opt_codec_ctx, td, sptr, buffer, size);
418 case ATS_CANONICAL_OER:
419 #ifdef ASN_DISABLE_OER_SUPPORT
423 return oer_decode(opt_codec_ctx, td, sptr, buffer, size);
426 case ATS_UNALIGNED_BASIC_PER:
427 case ATS_UNALIGNED_CANONICAL_PER:
428 #ifdef ASN_DISABLE_PER_SUPPORT
432 return uper_decode_complete(opt_codec_ctx, td, sptr, buffer, size);
436 case ATS_CANONICAL_XER:
437 return xer_decode(opt_codec_ctx, td, sptr, buffer, size);