From 28817c92d9504083bc23dd4dcc19a54581b227f4 Mon Sep 17 00:00:00 2001 From: Ahmet Artu Yildirim Date: Fri, 23 Dec 2022 14:30:30 -0800 Subject: [PATCH] Add getter and setter for raw pointer data --- src/common.h | 1 + src/progbuf.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/progbuf.h | 3 +++ 3 files changed, 75 insertions(+) diff --git a/src/common.h b/src/common.h index d9df28b..f56318e 100644 --- a/src/common.h +++ b/src/common.h @@ -11,6 +11,7 @@ #define PROGBUF_TYPE_FLOAT 0x02 #define PROGBUF_TYPE_DOUBLE 0x03 #define PROGBUF_TYPE_CHAR 0x04 +#define PROGBUF_TYPE_RAW 0x05 #define PROGBUF_TYPE_ARRAY 0x10 #define PROGBUF_TYPE_VAR_INT_ARRAY 0x11 #define PROGBUF_TYPE_VAR_FLOAT_ARRAY 0x12 diff --git a/src/progbuf.c b/src/progbuf.c index 671248f..8d2747b 100644 --- a/src/progbuf.c +++ b/src/progbuf.c @@ -628,6 +628,77 @@ PROGBUF_FLOAT_ARRAY_SET (float, PROGBUF_TYPE_VAR_FLOAT_ARRAY) PROGBUF_FLOAT_ARRAY_GET (double, PROGBUF_TYPE_VAR_DOUBLE_ARRAY) PROGBUF_FLOAT_ARRAY_SET (double, PROGBUF_TYPE_VAR_DOUBLE_ARRAY) +int +progbuf_set_raw_data (progbuf_h buf, const void *ptr, size_t len) +{ + int val_size, ret; + + if (!buf || !ptr) + return PROGBUF_ERROR_NULL_PARAM; + + if (!buf->buffer) + return PROGBUF_ERROR_NOT_OWNING; + + val_size = determine_var_size_t_size (len); + ret = check_buffer_and_expand (buf, 1 + val_size + len); + if (ret != 0) + return ret; + + buf->buffer[buf->size] = PROGBUF_TYPE_RAW; + buf->size++; + + write_var_size_t (buf, len, val_size, 0); + memcpy (buf->buffer + buf->size, ptr, len); + buf->size += len; + + return PROGBUF_SUCCESS; +} + +int +progbuf_get_raw_data (progbuf_it_h iter, void **ptr, size_t *len) +{ + if (!iter || !ptr || !len) + return PROGBUF_ERROR_NULL_PARAM; + + if (!iter->buf->buffer) + return PROGBUF_ERROR_NOT_OWNING; + + if (iter->read_pos >= iter->buf->size) + return PROGBUF_ERROR_END_OF_ITER; + + char val_type = iter->buf->buffer[iter->read_pos]; + + if ((val_type & PROGBUF_TYPE_RAW) != PROGBUF_TYPE_RAW) + return PROGBUF_ERROR_UNEXPECTED_TYPE; + + iter->read_pos++; + + size_t u_len; + size_t prev_read_pos = iter->read_pos; + int negative; + if (read_var_size_t (iter, &u_len, &negative) != 0) + { + iter->read_pos--; + return PROGBUF_ERROR_READ; + } + + void *l_ptr = malloc (u_len); + + if (!l_ptr) + { + iter->read_pos -= (1 + iter->read_pos - prev_read_pos); + return PROGBUF_ERROR_MEM_ALLOC; + } + + memcpy (l_ptr, iter->buf->buffer + iter->read_pos, u_len); + iter->read_pos += u_len; + + *ptr = l_ptr; + *len = u_len; + + return PROGBUF_SUCCESS; +} + int progbuf_set_string (progbuf_h buf, const char *str) { diff --git a/src/progbuf.h b/src/progbuf.h index 361d7ef..806631a 100644 --- a/src/progbuf.h +++ b/src/progbuf.h @@ -51,6 +51,9 @@ int progbuf_get_double (progbuf_it_h iter, double *value); int progbuf_set_string (progbuf_h buf, const char *str); int progbuf_get_string (progbuf_it_h iter, char **str); +int progbuf_set_raw_data (progbuf_h buf, const void *ptr, size_t len); +int progbuf_get_raw_data (progbuf_it_h iter, void **ptr, size_t *len); + int progbuf_set_int_array (progbuf_h buf, const int *arr, size_t len); int progbuf_get_int_array (progbuf_it_h iter, int **arr, size_t *len); int progbuf_set_uint_array (progbuf_h buf, const unsigned int *arr, size_t len); -- 2.20.1