diff options
Diffstat (limited to 'components/service/common/serializer')
3 files changed, 134 insertions, 0 deletions
diff --git a/components/service/common/serializer/protobuf/component.cmake b/components/service/common/serializer/protobuf/component.cmake new file mode 100644 index 000000000..b9f8d975e --- /dev/null +++ b/components/service/common/serializer/protobuf/component.cmake @@ -0,0 +1,13 @@ +#------------------------------------------------------------------------------- +# Copyright (c) 2020, Arm Limited and Contributors. All rights reserved. +# +# SPDX-License-Identifier: BSD-3-Clause +# +#------------------------------------------------------------------------------- +if (NOT DEFINED TGT) + message(FATAL_ERROR "mandatory parameter TGT is not defined.") +endif() + +target_sources(${TGT} PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/pb_helper.c" + ) diff --git a/components/service/common/serializer/protobuf/pb_helper.c b/components/service/common/serializer/protobuf/pb_helper.c new file mode 100644 index 000000000..d32b30c9f --- /dev/null +++ b/components/service/common/serializer/protobuf/pb_helper.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include <stdlib.h> +#include <string.h> +#include "pb_helper.h" +#include <pb_encode.h> +#include <pb_decode.h> + +static bool pb_encode_byte_array(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { + + const pb_bytes_array_t *byte_array = (const pb_bytes_array_t *)*arg; + if (!pb_encode_tag_for_field(stream, field)) return false; + + return pb_encode_string(stream, byte_array->bytes, byte_array->size); +} + +static bool pb_decode_byte_array(pb_istream_t *stream, const pb_field_t *field, void **arg) { + + (void)field; + pb_bytes_array_t *byte_array = (pb_bytes_array_t *)*arg; + if (stream->bytes_left > byte_array->size) return false; + + byte_array->size = stream->bytes_left; + + return pb_read(stream, byte_array->bytes, stream->bytes_left); +} + +pb_callback_t pb_out_byte_array(const pb_bytes_array_t *byte_array) { + + pb_callback_t callback; + callback.funcs.encode = pb_encode_byte_array; + callback.arg = (void*)byte_array; + + return callback; +} + +pb_callback_t pb_in_byte_array(pb_bytes_array_t *byte_array) { + + pb_callback_t callback; + callback.funcs.decode = pb_decode_byte_array; + callback.arg = (void*)byte_array; + return callback; +} + +pb_bytes_array_t *pb_malloc_byte_array(size_t num_bytes) { + + pb_bytes_array_t *byte_array = (pb_bytes_array_t*)malloc(offsetof(pb_bytes_array_t, bytes) + num_bytes); + + if (byte_array) { + + byte_array->size = num_bytes; + } + + return byte_array; +} + +pb_bytes_array_t *pb_malloc_byte_array_containing_string(const char *str) { + + pb_bytes_array_t *byte_array; + size_t required_space = strlen(str) + 1; + + byte_array = pb_malloc_byte_array(required_space); + + if (byte_array) { + + memcpy(byte_array->bytes, str, required_space); + } + + return byte_array; +} + +pb_bytes_array_t *pb_malloc_byte_array_containing_bytes(const uint8_t *buf, size_t num_bytes) { + + pb_bytes_array_t *byte_array = pb_malloc_byte_array(num_bytes); + + if (byte_array) { + + memcpy(byte_array->bytes, buf, num_bytes); + } + + return byte_array; +} diff --git a/components/service/common/serializer/protobuf/pb_helper.h b/components/service/common/serializer/protobuf/pb_helper.h new file mode 100644 index 000000000..1f109b0d1 --- /dev/null +++ b/components/service/common/serializer/protobuf/pb_helper.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2020, Arm Limited and Contributors. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef PB_HELPER_H +#define PB_HELPER_H + +#include <pb.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* Returns an initialised pb_callback_t structure for encoding a variable length byte array */ +extern pb_callback_t pb_out_byte_array(const pb_bytes_array_t *byte_array); + +/* Returns an initialised pb_callback_t structure for decoding a variable length byte array */ +extern pb_callback_t pb_in_byte_array(pb_bytes_array_t *byte_array); + +/* Malloc space for a pb_bytes_array_t object with space for the requested number of bytes */ +extern pb_bytes_array_t *pb_malloc_byte_array(size_t num_bytes); + +/* Malloc space for a pb_bytes_array_t object containing the given string */ +extern pb_bytes_array_t *pb_malloc_byte_array_containing_string(const char *str); + +/* Malloc space for a pb_bytes_array_t object containing the given bytes */ +extern pb_bytes_array_t *pb_malloc_byte_array_containing_bytes(const uint8_t *buf, size_t num_bytes); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* PB_HELPER_H */ |