Lev Walkin | 4558ad0 | 2017-07-10 05:21:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Walkin | b5b524b | 2017-10-13 03:14:03 -0700 | [diff] [blame] | 6 | #include <asn_codecs_prim.h> |
Lev Walkin | 4558ad0 | 2017-07-10 05:21:30 -0700 | [diff] [blame] | 7 | |
| 8 | /* |
| 9 | * The OER encoder of any type. |
| 10 | */ |
| 11 | asn_enc_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 12 | oer_encode(const asn_TYPE_descriptor_t *type_descriptor, const void *struct_ptr, |
| 13 | asn_app_consume_bytes_f *consume_bytes, void *app_key) { |
| 14 | ASN_DEBUG("OER encoder invoked for %s", type_descriptor->name); |
Lev Walkin | 4558ad0 | 2017-07-10 05:21:30 -0700 | [diff] [blame] | 15 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 16 | /* |
| 17 | * Invoke type-specific encoder. |
| 18 | */ |
| 19 | return type_descriptor->op->oer_encoder( |
| 20 | type_descriptor, 0, |
| 21 | struct_ptr, /* Pointer to the destination structure */ |
| 22 | consume_bytes, app_key); |
Lev Walkin | 4558ad0 | 2017-07-10 05:21:30 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | /* |
| 26 | * Argument type and callback necessary for oer_encode_to_buffer(). |
| 27 | */ |
| 28 | typedef struct enc_to_buf_arg { |
| 29 | void *buffer; |
| 30 | size_t left; |
| 31 | } enc_to_buf_arg; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 32 | static int |
| 33 | encode_to_buffer_cb(const void *buffer, size_t size, void *key) { |
| 34 | enc_to_buf_arg *arg = (enc_to_buf_arg *)key; |
Lev Walkin | 4558ad0 | 2017-07-10 05:21:30 -0700 | [diff] [blame] | 35 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 36 | if(arg->left < size) return -1; /* Data exceeds the available buffer size */ |
Lev Walkin | 4558ad0 | 2017-07-10 05:21:30 -0700 | [diff] [blame] | 37 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 38 | memcpy(arg->buffer, buffer, size); |
| 39 | arg->buffer = ((char *)arg->buffer) + size; |
| 40 | arg->left -= size; |
Lev Walkin | 4558ad0 | 2017-07-10 05:21:30 -0700 | [diff] [blame] | 41 | |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 42 | return 0; |
Lev Walkin | 4558ad0 | 2017-07-10 05:21:30 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* |
| 46 | * A variant of the oer_encode() which encodes the data into the provided buffer |
| 47 | */ |
| 48 | asn_enc_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 49 | oer_encode_to_buffer(const asn_TYPE_descriptor_t *type_descriptor, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 50 | const asn_oer_constraints_t *constraints, |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 51 | const void *struct_ptr, /* Structure to be encoded */ |
| 52 | void *buffer, /* Pre-allocated buffer */ |
| 53 | size_t buffer_size /* Initial buffer size (maximum) */ |
| 54 | ) { |
Lev Walkin | 486fd5c | 2017-07-10 22:08:14 -0700 | [diff] [blame] | 55 | enc_to_buf_arg arg; |
| 56 | asn_enc_rval_t ec; |
Lev Walkin | 4558ad0 | 2017-07-10 05:21:30 -0700 | [diff] [blame] | 57 | |
Lev Walkin | 486fd5c | 2017-07-10 22:08:14 -0700 | [diff] [blame] | 58 | arg.buffer = buffer; |
| 59 | arg.left = buffer_size; |
Lev Walkin | 4558ad0 | 2017-07-10 05:21:30 -0700 | [diff] [blame] | 60 | |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 61 | if(type_descriptor->op->oer_encoder == NULL) { |
Lev Walkin | cc9b3ae | 2017-07-14 08:57:02 +0400 | [diff] [blame] | 62 | ec.encoded = -1; |
| 63 | ec.failed_type = type_descriptor; |
| 64 | ec.structure_ptr = struct_ptr; |
| 65 | ASN_DEBUG("OER encoder is not defined for %s", |
| 66 | type_descriptor->name); |
| 67 | } else { |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 68 | ec = type_descriptor->op->oer_encoder( |
Lev Walkin | cc9b3ae | 2017-07-14 08:57:02 +0400 | [diff] [blame] | 69 | type_descriptor, constraints, |
| 70 | struct_ptr, /* Pointer to the destination structure */ |
| 71 | encode_to_buffer_cb, &arg); |
| 72 | if(ec.encoded != -1) { |
| 73 | assert(ec.encoded == (ssize_t)(buffer_size - arg.left)); |
| 74 | /* Return the encoded contents size */ |
| 75 | } |
Lev Walkin | 486fd5c | 2017-07-10 22:08:14 -0700 | [diff] [blame] | 76 | } |
| 77 | return ec; |
Lev Walkin | 4558ad0 | 2017-07-10 05:21:30 -0700 | [diff] [blame] | 78 | } |
Lev Walkin | b5b524b | 2017-10-13 03:14:03 -0700 | [diff] [blame] | 79 | |
| 80 | asn_enc_rval_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 81 | oer_encode_primitive(const asn_TYPE_descriptor_t *td, |
| 82 | const asn_oer_constraints_t *constraints, const void *sptr, |
Lev Walkin | b5b524b | 2017-10-13 03:14:03 -0700 | [diff] [blame] | 83 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 84 | const ASN__PRIMITIVE_TYPE_t *st = (const ASN__PRIMITIVE_TYPE_t *)sptr; |
| 85 | asn_enc_rval_t er = {0, 0, 0}; |
| 86 | ssize_t ret; |
| 87 | |
| 88 | (void)constraints; |
| 89 | |
| 90 | if(!st) ASN__ENCODE_FAILED; |
| 91 | |
| 92 | ASN_DEBUG("Encoding %s (%zu bytes)", td ? td->name : "", st->size); |
| 93 | |
| 94 | /* |
| 95 | * X.696 (08/2015) #27.2 |
| 96 | */ |
| 97 | ret = oer_serialize_length(st->size, cb, app_key); |
| 98 | if(ret < 0) { |
| 99 | ASN__ENCODE_FAILED; |
| 100 | } |
| 101 | er.encoded += ret; |
| 102 | |
| 103 | er.encoded += st->size; |
| 104 | if(cb(st->buf, st->size, app_key) < 0) { |
| 105 | ASN__ENCODE_FAILED; |
| 106 | } else { |
| 107 | ASN__ENCODED_OK(er); |
| 108 | } |
| 109 | } |
| 110 | |
Lev Walkin | 312795a | 2017-10-17 15:48:34 -0700 | [diff] [blame] | 111 | static int |
| 112 | oer__count_bytes(const void *buffer, size_t size, void *bytes_ptr) { |
| 113 | size_t *bytes = bytes_ptr; |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 114 | (void)buffer; |
Lev Walkin | 312795a | 2017-10-17 15:48:34 -0700 | [diff] [blame] | 115 | *bytes += size; |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | ssize_t |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 120 | oer_open_type_put(const asn_TYPE_descriptor_t *td, |
| 121 | const asn_oer_constraints_t *constraints, const void *sptr, |
| 122 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 312795a | 2017-10-17 15:48:34 -0700 | [diff] [blame] | 123 | size_t serialized_byte_count = 0; |
| 124 | asn_enc_rval_t er; |
| 125 | ssize_t len_len; |
| 126 | |
| 127 | er = td->op->oer_encoder(td, constraints, sptr, oer__count_bytes, |
| 128 | &serialized_byte_count); |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 129 | if(er.encoded < 0) return -1; |
| 130 | assert(serialized_byte_count == (size_t)er.encoded); |
Lev Walkin | 312795a | 2017-10-17 15:48:34 -0700 | [diff] [blame] | 131 | |
| 132 | len_len = oer_serialize_length(serialized_byte_count, cb, app_key); |
| 133 | if(len_len == -1) return -1; |
| 134 | |
| 135 | er = td->op->oer_encoder(td, constraints, sptr, cb, app_key); |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 136 | if(er.encoded < 0) return -1; |
| 137 | assert(serialized_byte_count == (size_t)er.encoded); |
Lev Walkin | 312795a | 2017-10-17 15:48:34 -0700 | [diff] [blame] | 138 | |
Lev Walkin | 42f6c88 | 2017-10-20 02:39:08 -0700 | [diff] [blame] | 139 | return len_len + er.encoded; |
Lev Walkin | 312795a | 2017-10-17 15:48:34 -0700 | [diff] [blame] | 140 | } |
| 141 | |