From 7112c709ad2a514d8c814aa061eb6b157abf0816 Mon Sep 17 00:00:00 2001 From: jorgenkraghjakobsen Date: Fri, 24 Apr 2020 12:19:43 +0200 Subject: [PATCH] buffer striped --- components/libbuffer/buffer.c | 165 +++++++++++++++++++++++++ components/libbuffer/component.mk | 2 + components/libbuffer/include/buffer.h | 168 ++++++++++++++++++++++++++ 3 files changed, 335 insertions(+) create mode 100644 components/libbuffer/buffer.c create mode 100644 components/libbuffer/component.mk create mode 100644 components/libbuffer/include/buffer.h diff --git a/components/libbuffer/buffer.c b/components/libbuffer/buffer.c new file mode 100644 index 0000000..c049eed --- /dev/null +++ b/components/libbuffer/buffer.c @@ -0,0 +1,165 @@ +#include "buffer.h" + +void buffer_read_init(read_buffer_t *buffer, const char *data, size_t size) { + buffer->buffer = data; + buffer->size = size; + buffer->index = 0; +} + +void buffer_write_init(write_buffer_t *buffer, char *data, size_t size) { + buffer->buffer = data; + buffer->size = size; + buffer->index = 0; +} + +int buffer_read_buffer(read_buffer_t *buffer, char *data, size_t size) { + int i; + + if (buffer->size - buffer->index < size) { + return 1; + } + + for (i = 0; i < size; i++) { + data[i] = buffer->buffer[buffer->index++]; + } + + return 0; +} + +int buffer_write_buffer(write_buffer_t *buffer, const char *data, size_t size) { + int i; + + if (buffer->size - buffer->index < size) { + return 1; + } + + for (i = 0; i < size; i++) { + buffer->buffer[buffer->index++] = data[i]; + } + + return 0; +} + +int buffer_read_uint32(read_buffer_t *buffer, uint32_t *data) { + if (buffer->size - buffer->index < sizeof(uint32_t)) { + return 1; + } + + *data = buffer->buffer[buffer->index++] & 0xff; + *data |= (buffer->buffer[buffer->index++] & 0xff) << 8; + *data |= (buffer->buffer[buffer->index++] & 0xff) << 16; + *data |= (buffer->buffer[buffer->index++] & 0xff) << 24; + return 0; +} + +int buffer_write_uint32(write_buffer_t *buffer, uint32_t data) { + if (buffer->size - buffer->index < sizeof(uint32_t)) { + return 1; + } + + buffer->buffer[buffer->index++] = data & 0xff; + buffer->buffer[buffer->index++] = (data >> 8) & 0xff; + buffer->buffer[buffer->index++] = (data >> 16) & 0xff; + buffer->buffer[buffer->index++] = (data >> 24) & 0xff; + return 0; +} + +int buffer_read_uint16(read_buffer_t *buffer, uint16_t *data) { + if (buffer->size - buffer->index < sizeof(uint16_t)) { + return 1; + } + + *data = buffer->buffer[buffer->index++] & 0xff; + *data |= (buffer->buffer[buffer->index++] & 0xff) << 8; + return 0; +} + +int buffer_write_uint16(write_buffer_t *buffer, uint16_t data) { + if (buffer->size - buffer->index < sizeof(uint16_t)) { + return 1; + } + + buffer->buffer[buffer->index++] = data & 0xff; + buffer->buffer[buffer->index++] = (data >> 8) & 0xff; + return 0; +} + +int buffer_read_uint8(read_buffer_t *buffer, uint8_t *data) { + if (buffer->size - buffer->index < sizeof(uint8_t)) { + return 1; + } + + *data = buffer->buffer[buffer->index++] & 0xff; + return 0; +} + +int buffer_write_uint8(write_buffer_t *buffer, uint8_t data) { + if (buffer->size - buffer->index < sizeof(uint8_t)) { + return 1; + } + + buffer->buffer[buffer->index++] = data & 0xff; + return 0; +} + +int buffer_read_int32(read_buffer_t *buffer, int32_t *data) { + if (buffer->size - buffer->index < sizeof(int32_t)) { + return 1; + } + + *data = buffer->buffer[buffer->index++] & 0xff; + *data |= (buffer->buffer[buffer->index++] & 0xff) << 8; + *data |= (buffer->buffer[buffer->index++] & 0xff) << 16; + *data |= (buffer->buffer[buffer->index++] & 0xff) << 24; + return 0; +} + +int buffer_write_int32(write_buffer_t *buffer, int32_t data) { + if (buffer->size - buffer->index < sizeof(int32_t)) { + return 1; + } + + buffer->buffer[buffer->index++] = data & 0xff; + buffer->buffer[buffer->index++] = (data >> 8) & 0xff; + buffer->buffer[buffer->index++] = (data >> 16) & 0xff; + buffer->buffer[buffer->index++] = (data >> 24) & 0xff; + return 0; +} + +int buffer_read_int16(read_buffer_t *buffer, int16_t *data) { + if (buffer->size - buffer->index < sizeof(int16_t)) { + return 1; + } + + *data = buffer->buffer[buffer->index++] & 0xff; + *data |= (buffer->buffer[buffer->index++] & 0xff) << 8; + return 0; +} + +int buffer_write_int16(write_buffer_t *buffer, int16_t data) { + if (buffer->size - buffer->index < sizeof(int16_t)) { + return 1; + } + + buffer->buffer[buffer->index++] = data & 0xff; + buffer->buffer[buffer->index++] = (data >> 8) & 0xff; + return 0; +} + +int buffer_read_int8(read_buffer_t *buffer, int8_t *data) { + if (buffer->size - buffer->index < sizeof(int8_t)) { + return 1; + } + + *data = buffer->buffer[buffer->index++] & 0xff; + return 0; +} + +int buffer_write_int8(write_buffer_t *buffer, int8_t data) { + if (buffer->size - buffer->index < sizeof(int8_t)) { + return 1; + } + + buffer->buffer[buffer->index++] = data & 0xff; + return 0; +} diff --git a/components/libbuffer/component.mk b/components/libbuffer/component.mk new file mode 100644 index 0000000..3272a3c --- /dev/null +++ b/components/libbuffer/component.mk @@ -0,0 +1,2 @@ +COMPONENT_SRCDIRS := . +# CFLAGS += diff --git a/components/libbuffer/include/buffer.h b/components/libbuffer/include/buffer.h new file mode 100644 index 0000000..5a5d8d1 --- /dev/null +++ b/components/libbuffer/include/buffer.h @@ -0,0 +1,168 @@ +#ifndef __BUFFER_H__ +#define __BUFFER_H__ + +#include +#include + +typedef struct read_buffer_t { + const char *buffer; + size_t size, index; +} read_buffer_t; + +typedef struct write_buffer_t { + char *buffer; + size_t size, index; +} write_buffer_t; + +/** + * Init the read buffer. + * + * The caller owns the memory pointed to by "data". The lifetime of the memory + * should persist across the lifetime of the buffer. + * + * @param[in] buffer The buffer to initialize. + * @param[in] data The array of bytes to read from. + * @param[in] size The size of the array of bytes. + */ + +void buffer_read_init(read_buffer_t *buffer, const char *data, size_t size); + +/** + * Init the write buffer. + * + * @param[in] buffer The buffer to initialize. + * @param[out] data The array of bytes to write to. + * @param[in] size The size of the array of bytes. + */ +void buffer_write_init(write_buffer_t *buffer, char *data, size_t size); + +/** + * Read an array of bytes from the buffer. + * + * @param[in] buffer The buffer to read from. + * @param[out] data The read data. + * @param[in] size The size of the array of bytes. + * @return 1 if there are not enough bytes to read from the buffer, 0 otherwise. +*/ +int buffer_read_buffer(read_buffer_t *buffer, char *data, size_t size); + +/** + * Write an array of bytes to the buffer. + * + * @param[in] buffer The buffer to write to. + * @param[in] data The data to write. + * @param[in] size The size of the array of bytes. + * @return 1 if there is not enough room in the buffer to write the data, 0 otherwise. +*/ +int buffer_write_buffer(write_buffer_t *buffer, const char *data, size_t size); + +/** + * Read an uint32_t from the buffer. + * + * @param[in] buffer The buffer to read from. + * @param[out] data The read data. + * @return 1 if there are not enough bytes to read from the buffer, 0 otherwise. +*/ +int buffer_read_uint32(read_buffer_t *buffer, uint32_t *data); + +/** + * Write an uint32_t to the buffer. + * + * @param[in] buffer The buffer to write to. + * @param[in] data The data to write. + * @return 1 if there is not enough room in the buffer to write the data, 0 otherwise. +*/ +int buffer_write_uint32(write_buffer_t *buffer, uint32_t data); + +/** + * Read an uint16_t from the buffer. + * + * @param[in] buffer The buffer to read from. + * @param[out] data The read data. + * @return 1 if there are not enough bytes to read from the buffer, 0 otherwise. +*/ +int buffer_read_uint16(read_buffer_t *buffer, uint16_t *data); + +/** + * Write an uint16_t to the buffer. + * + * @param[in] buffer The buffer to write to. + * @param[in] data The data to write. + * @return 1 if there is not enough room in the buffer to write the data, 0 otherwise. +*/ +int buffer_write_uint16(write_buffer_t *buffer, uint16_t data); + +/** + * Read an uint8_t from the buffer. + * + * @param[in] buffer The buffer to read from. + * @param[out] data The read data. + * @return 1 if there are not enough bytes to read from the buffer, 0 otherwise. +*/ +int buffer_read_uint8(read_buffer_t *buffer, uint8_t *data); + +/** + * Write an uint8_t to the buffer. + * + * @param[in] buffer The buffer to write to. + * @param[in] data The data to write. + * @return 1 if there is not enough room in the buffer to write the data, 0 otherwise. +*/ +int buffer_write_uint8(write_buffer_t *buffer, uint8_t data); + +/** + * Read an int32_t from the buffer. + * + * @param[in] buffer The buffer to read from. + * @param[out] data The read data. + * @return 1 if there are not enough bytes to read from the buffer, 0 otherwise. +*/ +int buffer_read_int32(read_buffer_t *buffer, int32_t *data); + +/** + * Write an int32_t to the buffer. + * + * @param[in] buffer The buffer to write to. + * @param[in] data The data to write. + * @return 1 if there is not enough room in the buffer to write the data, 0 otherwise. +*/ +int buffer_write_int32(write_buffer_t *buffer, int32_t data); + +/** + * Read an int16_t from the buffer. + * + * @param[in] buffer The buffer to read from. + * @param[out] data The read data. + * @return 1 if there are not enough bytes to read from the buffer, 0 otherwise. +*/ +int buffer_read_int16(read_buffer_t *buffer, int16_t *data); + +/** + * Write an int16_t to the buffer. + * + * @param[in] buffer The buffer to write to. + * @param[in] data The data to write. + * @return 1 if there is not enough room in the buffer to write the data, 0 otherwise. +*/ +int buffer_write_int16(write_buffer_t *buffer, int16_t data); + +/** + * Read an int8_t from the buffer. + * + * @param[in] buffer The buffer to read from. + * @param[out] data The read data. + * @return 1 if there are not enough bytes to read from the buffer, 0 otherwise. +*/ +int buffer_read_int8(read_buffer_t *buffer, int8_t *data); + +/** + * Write an int8_t to the buffer. + * + * @param[in] buffer The buffer to write to. + * @param[in] data The data to write. + * @return 1 if there is not enough room in the buffer to write the data, 0 otherwise. +*/ +int buffer_write_int8(write_buffer_t *buffer, int8_t data); + + +#endif // __BUFFER_H__