|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 Jon Ringle <[email protected]>. All rights reserved. |
| 3 | + * Redistribution and modifications are permitted subject to BSD license. |
| 4 | + */ |
| 5 | +#include <asn_internal.h> |
| 6 | +#include <bner_encoder.h> |
| 7 | +#include <errno.h> |
| 8 | + |
| 9 | +/* |
| 10 | + * The BNER encoder of any type. |
| 11 | + */ |
| 12 | +asn_enc_rval_t |
| 13 | +bner_encode(const asn_TYPE_descriptor_t *td, const void *sptr, |
| 14 | + asn_app_consume_bytes_f *consume_bytes, void *app_key) { |
| 15 | + if(!td) ASN__ENCODE_FAILED; |
| 16 | + |
| 17 | + ASN_DEBUG("BNER encoder invoked for %s", td->name); |
| 18 | + |
| 19 | + if(is_bner_fixed_pdu(td->name)) { |
| 20 | + return bner_fixed_encoder(td, sptr, consume_bytes, app_key); |
| 21 | + } |
| 22 | + |
| 23 | + /* |
| 24 | + * Invoke type-specific encoder. |
| 25 | + */ |
| 26 | + return td->op->bner_encoder(td, sptr, 0, 0, consume_bytes, app_key); |
| 27 | +} |
| 28 | + |
| 29 | +/* |
| 30 | + * Argument type and callback necessary for bner_encode_to_buffer(). |
| 31 | + */ |
| 32 | +typedef struct enc_to_buf_arg { |
| 33 | + void *buffer; |
| 34 | + size_t left; |
| 35 | +} enc_to_buf_arg; |
| 36 | + |
| 37 | +static int |
| 38 | +encode_to_buffer_cb(const void *buffer, size_t size, void *key) { |
| 39 | + enc_to_buf_arg *arg = (enc_to_buf_arg *)key; |
| 40 | + |
| 41 | + if(arg->left < size) return -1; /* Data exceeds the available buffer size */ |
| 42 | + |
| 43 | + memcpy(arg->buffer, buffer, size); |
| 44 | + arg->buffer = ((char *)arg->buffer) + size; |
| 45 | + arg->left -= size; |
| 46 | + |
| 47 | + return 0; |
| 48 | +} |
| 49 | + |
| 50 | +/* |
| 51 | + * A variant of the bner_encode() which encodes the data into the provided |
| 52 | + * buffer |
| 53 | + */ |
| 54 | +asn_enc_rval_t |
| 55 | +bner_encode_to_buffer(const asn_TYPE_descriptor_t *td, |
| 56 | + const void *struct_ptr, void *buffer, |
| 57 | + size_t buffer_size) { |
| 58 | + enc_to_buf_arg arg; |
| 59 | + asn_enc_rval_t ec; |
| 60 | + |
| 61 | + arg.buffer = buffer; |
| 62 | + arg.left = buffer_size; |
| 63 | + |
| 64 | + if(is_bner_fixed_pdu(td->name)) { |
| 65 | + ec = bner_fixed_encoder(td, struct_ptr, |
| 66 | + encode_to_buffer_cb, &arg); |
| 67 | + } else { |
| 68 | + ec = td->op->bner_encoder( |
| 69 | + td, |
| 70 | + struct_ptr, /* Pointer to the destination structure */ |
| 71 | + 0, 0, encode_to_buffer_cb, &arg); |
| 72 | + } |
| 73 | + |
| 74 | + if(ec.encoded != -1) { |
| 75 | + assert(ec.encoded == (ssize_t)(buffer_size - arg.left)); |
| 76 | + /* Return the encoded contents size */ |
| 77 | + } |
| 78 | + return ec; |
| 79 | +} |
| 80 | + |
| 81 | +__attribute__((weak)) asn_enc_rval_t |
| 82 | +bner_fixed_encoder(const struct asn_TYPE_descriptor_s *td, |
| 83 | + const void *sptr, /* Structure to be encoded */ |
| 84 | + asn_app_consume_bytes_f *consume_bytes_cb, |
| 85 | + void *app_key /* Arbitrary callback argument */ |
| 86 | + ) { |
| 87 | + (void)td; |
| 88 | + (void)sptr; |
| 89 | + (void)consume_bytes_cb; |
| 90 | + (void)app_key; |
| 91 | + |
| 92 | + ASN_DEBUG("Failed to decode %s. No bner_fixed_encoder function provided", |
| 93 | + td->name); |
| 94 | + ASN__ENCODE_FAILED; |
| 95 | +} |
0 commit comments