blob: 895aed4ecb42591943b964e4779ed02c6d41e049 [file] [log] [blame]
Lev Walkin4558ad02017-07-10 05:21:30 -07001/*
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 Walkinb5b524b2017-10-13 03:14:03 -07006#include <asn_codecs_prim.h>
Lev Walkin4558ad02017-07-10 05:21:30 -07007
8/*
9 * The OER encoder of any type.
10 */
11asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070012oer_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 Walkin4558ad02017-07-10 05:21:30 -070015
Lev Walkin20696a42017-10-17 21:27:33 -070016 /*
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 Walkin4558ad02017-07-10 05:21:30 -070023}
24
25/*
26 * Argument type and callback necessary for oer_encode_to_buffer().
27 */
28typedef struct enc_to_buf_arg {
29 void *buffer;
30 size_t left;
31} enc_to_buf_arg;
Lev Walkin20696a42017-10-17 21:27:33 -070032static int
33encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
34 enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
Lev Walkin4558ad02017-07-10 05:21:30 -070035
Lev Walkin20696a42017-10-17 21:27:33 -070036 if(arg->left < size) return -1; /* Data exceeds the available buffer size */
Lev Walkin4558ad02017-07-10 05:21:30 -070037
Lev Walkin20696a42017-10-17 21:27:33 -070038 memcpy(arg->buffer, buffer, size);
39 arg->buffer = ((char *)arg->buffer) + size;
40 arg->left -= size;
Lev Walkin4558ad02017-07-10 05:21:30 -070041
Lev Walkin20696a42017-10-17 21:27:33 -070042 return 0;
Lev Walkin4558ad02017-07-10 05:21:30 -070043}
44
45/*
46 * A variant of the oer_encode() which encodes the data into the provided buffer
47 */
48asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070049oer_encode_to_buffer(const asn_TYPE_descriptor_t *type_descriptor,
Lev Walkin494fb702017-08-07 20:07:00 -070050 const asn_oer_constraints_t *constraints,
Lev Walkin20696a42017-10-17 21:27:33 -070051 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 Walkin486fd5c2017-07-10 22:08:14 -070055 enc_to_buf_arg arg;
56 asn_enc_rval_t ec;
Lev Walkin4558ad02017-07-10 05:21:30 -070057
Lev Walkin486fd5c2017-07-10 22:08:14 -070058 arg.buffer = buffer;
59 arg.left = buffer_size;
Lev Walkin4558ad02017-07-10 05:21:30 -070060
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080061 if(type_descriptor->op->oer_encoder == NULL) {
Lev Walkincc9b3ae2017-07-14 08:57:02 +040062 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, Chiu1f87ac02017-08-20 01:25:45 +080068 ec = type_descriptor->op->oer_encoder(
Lev Walkincc9b3ae2017-07-14 08:57:02 +040069 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 Walkin486fd5c2017-07-10 22:08:14 -070076 }
77 return ec;
Lev Walkin4558ad02017-07-10 05:21:30 -070078}
Lev Walkinb5b524b2017-10-13 03:14:03 -070079
80asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -070081oer_encode_primitive(const asn_TYPE_descriptor_t *td,
82 const asn_oer_constraints_t *constraints, const void *sptr,
Lev Walkinb5b524b2017-10-13 03:14:03 -070083 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
Lev Walkin9f470d62017-10-21 16:27:08 -070092 ASN_DEBUG("Encoding %s (%" ASN_PRI_SIZE " bytes)", td ? td->name : "", st->size);
Lev Walkinb5b524b2017-10-13 03:14:03 -070093
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 Walkin312795a2017-10-17 15:48:34 -0700111static int
112oer__count_bytes(const void *buffer, size_t size, void *bytes_ptr) {
113 size_t *bytes = bytes_ptr;
Lev Walkin20696a42017-10-17 21:27:33 -0700114 (void)buffer;
Lev Walkin312795a2017-10-17 15:48:34 -0700115 *bytes += size;
116 return 0;
117}
118
119ssize_t
Lev Walkin20696a42017-10-17 21:27:33 -0700120oer_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 Walkin312795a2017-10-17 15:48:34 -0700123 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 Walkin20696a42017-10-17 21:27:33 -0700129 if(er.encoded < 0) return -1;
130 assert(serialized_byte_count == (size_t)er.encoded);
Lev Walkin312795a2017-10-17 15:48:34 -0700131
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 Walkin20696a42017-10-17 21:27:33 -0700136 if(er.encoded < 0) return -1;
137 assert(serialized_byte_count == (size_t)er.encoded);
Lev Walkin312795a2017-10-17 15:48:34 -0700138
Lev Walkin42f6c882017-10-20 02:39:08 -0700139 return len_len + er.encoded;
Lev Walkin312795a2017-10-17 15:48:34 -0700140}
141