blob: a6f7a78fc8fe7302826b108731d8c90d199af0f6 [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
12oer_encode(asn_TYPE_descriptor_t *type_descriptor, void *struct_ptr,
13 asn_app_consume_bytes_f *consume_bytes, void *app_key) {
14
15 ASN_DEBUG("OER encoder invoked for %s",
16 type_descriptor->name);
17
18 /*
19 * Invoke type-specific encoder.
20 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080021 return type_descriptor->op->oer_encoder(type_descriptor, 0,
Lev Walkin4558ad02017-07-10 05:21:30 -070022 struct_ptr, /* Pointer to the destination structure */
23 consume_bytes, app_key);
24}
25
26/*
27 * Argument type and callback necessary for oer_encode_to_buffer().
28 */
29typedef struct enc_to_buf_arg {
30 void *buffer;
31 size_t left;
32} enc_to_buf_arg;
33static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
34 enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
35
36 if(arg->left < size)
37 return -1; /* Data exceeds the available buffer size */
38
39 memcpy(arg->buffer, buffer, size);
40 arg->buffer = ((char *)arg->buffer) + size;
41 arg->left -= size;
42
43 return 0;
44}
45
46/*
47 * A variant of the oer_encode() which encodes the data into the provided buffer
48 */
49asn_enc_rval_t
50oer_encode_to_buffer(struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkin494fb702017-08-07 20:07:00 -070051 const asn_oer_constraints_t *constraints,
Lev Walkin4558ad02017-07-10 05:21:30 -070052 void *struct_ptr, /* Structure to be encoded */
53 void *buffer, /* Pre-allocated buffer */
54 size_t buffer_size /* Initial buffer size (maximum) */
55 ) {
Lev Walkin486fd5c2017-07-10 22:08:14 -070056 enc_to_buf_arg arg;
57 asn_enc_rval_t ec;
Lev Walkin4558ad02017-07-10 05:21:30 -070058
Lev Walkin486fd5c2017-07-10 22:08:14 -070059 arg.buffer = buffer;
60 arg.left = buffer_size;
Lev Walkin4558ad02017-07-10 05:21:30 -070061
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080062 if(type_descriptor->op->oer_encoder == NULL) {
Lev Walkincc9b3ae2017-07-14 08:57:02 +040063 ec.encoded = -1;
64 ec.failed_type = type_descriptor;
65 ec.structure_ptr = struct_ptr;
66 ASN_DEBUG("OER encoder is not defined for %s",
67 type_descriptor->name);
68 } else {
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080069 ec = type_descriptor->op->oer_encoder(
Lev Walkincc9b3ae2017-07-14 08:57:02 +040070 type_descriptor, constraints,
71 struct_ptr, /* Pointer to the destination structure */
72 encode_to_buffer_cb, &arg);
73 if(ec.encoded != -1) {
74 assert(ec.encoded == (ssize_t)(buffer_size - arg.left));
75 /* Return the encoded contents size */
76 }
Lev Walkin486fd5c2017-07-10 22:08:14 -070077 }
78 return ec;
Lev Walkin4558ad02017-07-10 05:21:30 -070079}
Lev Walkinb5b524b2017-10-13 03:14:03 -070080
81asn_enc_rval_t
82oer_encode_primitive(asn_TYPE_descriptor_t *td,
83 const asn_oer_constraints_t *constraints, void *sptr,
84 asn_app_consume_bytes_f *cb, void *app_key) {
85 const ASN__PRIMITIVE_TYPE_t *st = (const ASN__PRIMITIVE_TYPE_t *)sptr;
86 asn_enc_rval_t er = {0, 0, 0};
87 ssize_t ret;
88
89 (void)constraints;
90
91 if(!st) ASN__ENCODE_FAILED;
92
93 ASN_DEBUG("Encoding %s (%zu bytes)", td ? td->name : "", st->size);
94
95 /*
96 * X.696 (08/2015) #27.2
97 */
98 ret = oer_serialize_length(st->size, cb, app_key);
99 if(ret < 0) {
100 ASN__ENCODE_FAILED;
101 }
102 er.encoded += ret;
103
104 er.encoded += st->size;
105 if(cb(st->buf, st->size, app_key) < 0) {
106 ASN__ENCODE_FAILED;
107 } else {
108 ASN__ENCODED_OK(er);
109 }
110}
111