From 77e2045dfa9e090e9f3675702385e5b675328a04 Mon Sep 17 00:00:00 2001 From: Ahmet Artu Yildirim Date: Wed, 21 Dec 2022 16:18:16 -0800 Subject: [PATCH] Add buffer own and copy functions --- src/check_progbuf.c | 34 +++++---------------- src/progbuf.c | 74 +++++++++++++++++++++++++++++++++++++-------- src/progbuf.h | 7 +++-- 3 files changed, 75 insertions(+), 40 deletions(-) diff --git a/src/check_progbuf.c b/src/check_progbuf.c index 756b3a4..961e797 100644 --- a/src/check_progbuf.c +++ b/src/check_progbuf.c @@ -9,23 +9,23 @@ START_TEST (test_progbuf_alloc_version) int ret; long message_tag; - ret = progbuf_get_message_tag (NULL, &message_tag); + ret = progbuf_message_tag (NULL, &message_tag); ck_assert (ret == PROGBUF_ERROR_NULL_PARAM); progbuf_h buf = progbuf_alloc (1); ck_assert (buf); - ret = progbuf_get_message_tag (buf, NULL); + ret = progbuf_message_tag (buf, NULL); ck_assert (ret == PROGBUF_ERROR_NULL_PARAM); - ret = progbuf_get_message_tag (buf, &message_tag); + ret = progbuf_message_tag (buf, &message_tag); ck_assert (ret == PROGBUF_SUCCESS); ck_assert (message_tag == 1); size_t size; - ret = progbuf_get_message_buffer (buf, NULL, &size); + ret = progbuf_buffer_size (buf, &size); ck_assert (ret == PROGBUF_SUCCESS); @@ -51,7 +51,7 @@ parametric_test_progbuf_write_read_long (const long val, ck_assert (ret == PROGBUF_SUCCESS); size_t size; - ret = progbuf_get_message_buffer (buf, NULL, &size); + ret = progbuf_buffer_size (buf, &size); ck_assert (ret == PROGBUF_SUCCESS); @@ -131,16 +131,7 @@ START_TEST (test_progbuf_write_read_ulong) size_t size; - ret = progbuf_get_message_buffer (NULL, NULL, NULL); - ck_assert (ret == PROGBUF_ERROR_NULL_PARAM); - - ret = progbuf_get_message_buffer (NULL, NULL, &size); - ck_assert (ret == PROGBUF_ERROR_NULL_PARAM); - - ret = progbuf_get_message_buffer (buf, NULL, NULL); - ck_assert (ret == PROGBUF_ERROR_NULL_PARAM); - - ret = progbuf_get_message_buffer (buf, NULL, &size); + ret = progbuf_buffer_size (buf, &size); ck_assert (ret == PROGBUF_SUCCESS); ck_assert (size == 1 + 6); @@ -181,7 +172,7 @@ parametric_test_progbuf_write_read_longlong (const long long val, ck_assert (ret == PROGBUF_SUCCESS); size_t size; - ret = progbuf_get_message_buffer (buf, NULL, &size); + ret = progbuf_buffer_size (buf, &size); ck_assert (ret == PROGBUF_SUCCESS); @@ -289,16 +280,7 @@ START_TEST (test_progbuf_write_read_ulonglong) size_t size; - ret = progbuf_get_message_buffer (NULL, NULL, NULL); - ck_assert (ret == PROGBUF_ERROR_NULL_PARAM); - - ret = progbuf_get_message_buffer (NULL, NULL, &size); - ck_assert (ret == PROGBUF_ERROR_NULL_PARAM); - - ret = progbuf_get_message_buffer (buf, NULL, NULL); - ck_assert (ret == PROGBUF_ERROR_NULL_PARAM); - - ret = progbuf_get_message_buffer (buf, NULL, &size); + ret = progbuf_buffer_size (buf, &size); ck_assert (ret == PROGBUF_SUCCESS); ck_assert (size == 1 + 1 + 10); diff --git a/src/progbuf.c b/src/progbuf.c index 47d450e..1abbfb0 100644 --- a/src/progbuf.c +++ b/src/progbuf.c @@ -142,6 +142,9 @@ WRITE_VAR_SIZE (ulonglong) if (!buf) \ return PROGBUF_ERROR_NULL_PARAM; \ \ + if (!buf->buffer) \ + return PROGBUF_ERROR_NOT_OWNING; \ + \ val_size = determine_var_##utype##_size (ABS (value)); \ ret = check_buffer_and_expand (buf, val_size + 1); \ if (ret != 0) \ @@ -161,6 +164,9 @@ WRITE_VAR_SIZE (ulonglong) if (!iter || !value) \ 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; \ \ @@ -234,6 +240,9 @@ progbuf_iter_alloc (progbuf_h buf) if (!buf) return 0; + if (!buf->buffer) + return 0; + struct progbuf_it_s *iter = malloc (sizeof (struct progbuf_it_s)); if (!iter) @@ -251,7 +260,9 @@ progbuf_free (progbuf_h buf) if (!buf) return PROGBUF_ERROR_NULL_PARAM; - free (buf->buffer); + if (buf->buffer) + free (buf->buffer); + free (buf); return PROGBUF_SUCCESS; @@ -269,31 +280,67 @@ progbuf_iter_free (progbuf_it_h iter) } int -progbuf_get_message_tag (progbuf_h buf, long *message_tag) +progbuf_message_tag (progbuf_h buf, long *message_tag) { if (!buf || !message_tag) return PROGBUF_ERROR_NULL_PARAM; + if (!buf->buffer) + return PROGBUF_ERROR_NOT_OWNING; + *message_tag = buf->message_tag; return PROGBUF_SUCCESS; } int -progbuf_get_message_buffer (progbuf_h buf, char **buffer, size_t *size) +progbuf_own_buffer (progbuf_h buf, char **buffer, size_t *size) { - if (!buf || (!buffer && !size)) + if (!buf || !buffer || !size) return PROGBUF_ERROR_NULL_PARAM; - if (buffer) - { - *buffer = buf->buffer; - } + if (!buf->buffer) + return PROGBUF_ERROR_NOT_OWNING; - if (size) - { - *size = buf->size; - } + *buffer = buf->buffer; + buf->buffer = 0; + + *size = buf->size; + + return 0; +} + +int +progbuf_copy_buffer (progbuf_h buf, char **buffer, size_t *size) +{ + if (!buf || !buffer || !size) + return PROGBUF_ERROR_NULL_PARAM; + + if (!buf->buffer) + return PROGBUF_ERROR_NOT_OWNING; + + *buffer = malloc (buf->size); + + if (!*buffer) + return PROGBUF_ERROR_MEM_ALLOC; + + memcpy (*buffer, buf->buffer, buf->size); + + *size = buf->size; + + return 0; +} + +int +progbuf_buffer_size (progbuf_h buf, size_t *size) +{ + if (!buf || !size) + return PROGBUF_ERROR_NULL_PARAM; + + if (!buf->buffer) + return PROGBUF_ERROR_NOT_OWNING; + + *size = buf->size; return 0; } @@ -304,6 +351,9 @@ progbuf_iter_reset (progbuf_it_h iter) if (!iter) return PROGBUF_ERROR_NULL_PARAM; + if (!iter->buf->buffer) + return PROGBUF_ERROR_NOT_OWNING; + iter->read_pos = iter->buf->header_size; return PROGBUF_SUCCESS; diff --git a/src/progbuf.h b/src/progbuf.h index 8995862..ce6ebd6 100644 --- a/src/progbuf.h +++ b/src/progbuf.h @@ -10,14 +10,17 @@ #define PROGBUF_ERROR_UNEXPECTED_TYPE -4 #define PROGBUF_ERROR_READ -5 #define PROGBUF_ERROR_INCORRECT_SIGN -6 +#define PROGBUF_ERROR_NOT_OWNING -7 typedef struct progbuf_s *progbuf_h; typedef struct progbuf_it_s *progbuf_it_h; progbuf_h progbuf_alloc (long message_tag); -int progbuf_get_message_tag (progbuf_h buf, long *message_tag); -int progbuf_get_message_buffer (progbuf_h buf, char **buffer, size_t *size); +int progbuf_message_tag (progbuf_h buf, long *message_tag); +int progbuf_own_buffer (progbuf_h buf, char **buffer, size_t *size); +int progbuf_copy_buffer (progbuf_h buf, char **buffer, size_t *size); +int progbuf_buffer_size (progbuf_h buf, size_t *size); int progbuf_free (progbuf_h buf); int progbuf_set_int (progbuf_h buf, int value); -- 2.20.1