From 16d5d29049c08e21f57f7928ceedf40586a2d294 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Sat, 3 Dec 2022 11:10:06 +1100 Subject: Use std::span (backported) and std::byte to make our buffers safer --- src/audio/include/audio_decoder.hpp | 9 ++++++--- src/audio/include/audio_element.hpp | 7 +++---- src/audio/include/chunk.hpp | 13 +++++++------ src/audio/include/fatfs_audio_input.hpp | 23 ++++++++++++----------- src/audio/include/stream_message.hpp | 25 ++++++++++++++----------- 5 files changed, 42 insertions(+), 35 deletions(-) (limited to 'src/audio/include') diff --git a/src/audio/include/audio_decoder.hpp b/src/audio/include/audio_decoder.hpp index 4d2fd5f3..a32442da 100644 --- a/src/audio/include/audio_decoder.hpp +++ b/src/audio/include/audio_decoder.hpp @@ -1,8 +1,10 @@ #pragma once #include +#include #include "ff.h" +#include "span.hpp" #include "audio_element.hpp" #include "codec.hpp" @@ -23,8 +25,8 @@ class AudioDecoder : public IAudioElement { auto ProcessStreamInfo(StreamInfo& info) -> cpp::result; - auto ProcessChunk(uint8_t* data, std::size_t length) - -> cpp::result; + auto ProcessChunk(cpp::span& chunk) + -> cpp::result; auto ProcessIdle() -> cpp::result; AudioDecoder(const AudioDecoder&) = delete; @@ -34,7 +36,8 @@ class AudioDecoder : public IAudioElement { std::unique_ptr current_codec_; std::optional stream_info_; - uint8_t* chunk_buffer_; + std::byte* raw_chunk_buffer_; + cpp::span chunk_buffer_; }; } // namespace audio diff --git a/src/audio/include/audio_element.hpp b/src/audio/include/audio_element.hpp index 5b697784..06e47b35 100644 --- a/src/audio/include/audio_element.hpp +++ b/src/audio/include/audio_element.hpp @@ -1,7 +1,5 @@ #pragma once -#include - #include #include "freertos/FreeRTOS.h" @@ -9,6 +7,7 @@ #include "freertos/message_buffer.h" #include "freertos/portmacro.h" #include "result.hpp" +#include "span.hpp" #include "stream_info.hpp" #include "types.hpp" @@ -77,8 +76,8 @@ class IAudioElement { * bytes in this chunk that were actually used; leftover bytes will be * prepended to the next call. */ - virtual auto ProcessChunk(uint8_t* data, std::size_t length) - -> cpp::result = 0; + virtual auto ProcessChunk(cpp::span& chunk) + -> cpp::result = 0; /* * Called when there has been no data received over the input buffer for some diff --git a/src/audio/include/chunk.hpp b/src/audio/include/chunk.hpp index 365c83d0..0cbe8d5c 100644 --- a/src/audio/include/chunk.hpp +++ b/src/audio/include/chunk.hpp @@ -12,6 +12,7 @@ #include "freertos/portmacro.h" #include "freertos/queue.h" #include "result.hpp" +#include "span.hpp" namespace audio { @@ -37,9 +38,8 @@ enum ChunkWriteResult { * more input to read. */ auto WriteChunksToStream(MessageBufferHandle_t* stream, - uint8_t* working_buffer, - size_t working_buffer_length, - std::function callback, + cpp::span working_buffer, + std::function)> callback, TickType_t max_wait) -> ChunkWriteResult; enum ChunkReadResult { @@ -64,7 +64,7 @@ class ChunkReader { auto Reset() -> void; - auto GetLastMessage() -> std::pair; + auto GetLastMessage() -> cpp::span; /* * Reads chunks of data from the given input stream, and invokes the given @@ -79,12 +79,13 @@ class ChunkReader { * will place the message at the start of the working_buffer and then return. */ auto ReadChunkFromStream( - std::function(uint8_t*, size_t)> callback, + std::function(cpp::span)> callback, TickType_t max_wait) -> ChunkReadResult; private: MessageBufferHandle_t* stream_; - uint8_t* working_buffer_; + std::byte* raw_working_buffer_; + cpp::span working_buffer_; std::size_t leftover_bytes_ = 0; std::size_t last_message_size_ = 0; diff --git a/src/audio/include/fatfs_audio_input.hpp b/src/audio/include/fatfs_audio_input.hpp index c54b32bd..3ca79457 100644 --- a/src/audio/include/fatfs_audio_input.hpp +++ b/src/audio/include/fatfs_audio_input.hpp @@ -8,6 +8,7 @@ #include "freertos/message_buffer.h" #include "freertos/queue.h" +#include "span.hpp" #include "audio_element.hpp" #include "storage.hpp" @@ -21,28 +22,28 @@ class FatfsAudioInput : public IAudioElement { auto ProcessStreamInfo(StreamInfo& info) -> cpp::result; - auto ProcessChunk(uint8_t* data, std::size_t length) - -> cpp::result; + auto ProcessChunk(cpp::span& chunk) + -> cpp::result = 0; auto ProcessIdle() -> cpp::result; - auto SendChunk(uint8_t* buffer, size_t size) -> size_t; + auto SendChunk(cpp::span dest) -> size_t; private: auto GetRingBufferDistance() -> size_t; std::shared_ptr storage_; - uint8_t* file_buffer_; - uint8_t* file_buffer_read_pos_; - uint8_t* pending_read_pos_; - uint8_t* file_buffer_write_pos_; + std::byte* raw_file_buffer_; + cpp::span file_buffer_; + cpp::span::iterator file_buffer_read_pos_; + cpp::span::iterator pending_read_pos_; + cpp::span::iterator file_buffer_write_pos_; - uint8_t* chunk_buffer_; + std::byte* raw_chunk_buffer_; + cpp::span chunk_buffer_; FIL current_file_; - bool is_file_open_ = false; - - MessageBufferHandle_t input_buffer_; + bool is_file_open_; uint8_t* output_buffer_memory_; StaticMessageBuffer_t output_buffer_metadata_; diff --git a/src/audio/include/stream_message.hpp b/src/audio/include/stream_message.hpp index cbd7c733..043f9dc3 100644 --- a/src/audio/include/stream_message.hpp +++ b/src/audio/include/stream_message.hpp @@ -1,12 +1,12 @@ #pragma once -#include - +#include #include #include #include "cbor.h" #include "result.hpp" +#include "span.hpp" namespace audio { @@ -20,14 +20,13 @@ enum MessageType { }; template -auto WriteMessage(MessageType type, - Writer&& writer, - uint8_t* buffer, - size_t length) -> cpp::result { +auto WriteMessage(MessageType type, Writer&& writer, cpp::span data) + -> cpp::result { CborEncoder root; CborEncoder container; + uint8_t* cast_data = reinterpret_cast(data.data()); - cbor_encoder_init(&root, buffer, length, kEncoderFlags); + cbor_encoder_init(&root, cast_data, data.size(), kEncoderFlags); cbor_encoder_create_array(&root, &container, 2); cbor_encode_uint(&container, type); @@ -37,17 +36,18 @@ auto WriteMessage(MessageType type, } cbor_encoder_close_container(&root, &container); - return cbor_encoder_get_buffer_size(&root, buffer); + return cbor_encoder_get_buffer_size(&root, cast_data); } template -auto ReadMessage(Reader&& reader, uint8_t* buffer, size_t length) +auto ReadMessage(Reader&& reader, cpp::span data) -> cpp::result { CborParser parser; CborValue root; CborValue container; - cbor_parser_init(buffer, length, kDecoderFlags, &parser, &root); + cbor_parser_init(reinterpret_cast(data.data()), data.size(), + kDecoderFlags, &parser, &root); cbor_value_enter_container(&root, &container); // Skip the type header cbor_value_advance_fixed(&container); @@ -55,6 +55,9 @@ auto ReadMessage(Reader&& reader, uint8_t* buffer, size_t length) return std::invoke(reader, container); } -auto ReadMessageType(uint8_t* buffer, size_t length) -> MessageType; +auto WriteTypeOnlyMessage(MessageType type, cpp::span data) + -> cpp::result; +auto ReadMessageType(cpp::span msg) -> MessageType; +auto GetAdditionalData(cpp::span msg) -> cpp::span; } // namespace audio -- cgit v1.2.3