diff options
author | sandmann <sandmann> | 2007-01-13 22:21:59 +0000 |
---|---|---|
committer | sandmann <sandmann> | 2007-01-13 22:21:59 +0000 |
commit | 7dddf7c049f8b0b5c89113d47f60e69ba9ed847a (patch) | |
tree | 6a0c0b898d10d7059c1a6d56df9ff97cfab21013 | |
parent | 6f97c690e85d3c9e7e6264059cb28bd4eaeefc69 (diff) |
Nul terminate lacbytequeue
-rw-r--r-- | src/lacbytequeue.c | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/src/lacbytequeue.c b/src/lacbytequeue.c index 6761430..e2cb53a 100644 --- a/src/lacbytequeue.c +++ b/src/lacbytequeue.c @@ -9,15 +9,21 @@ struct LacByteQueue guint8 * end; }; -LacByteQueue * -lac_byte_queue_new (void) +static void +initialize (LacByteQueue *queue) { - LacByteQueue *queue = g_new0 (LacByteQueue, 1); - queue->segment_size = 0; queue->segment = NULL; queue->start = NULL; queue->end = NULL; +} + +LacByteQueue * +lac_byte_queue_new (void) +{ + LacByteQueue *queue = g_new0 (LacByteQueue, 1); + + initialize (queue); return queue; } @@ -62,7 +68,7 @@ const guint8 * lac_byte_queue_get_data (LacByteQueue *queue, gsize *n_bytes) { - guint8 *result; + const guint8 *result; if (n_bytes) *n_bytes = queue->end - queue->start; @@ -71,6 +77,11 @@ lac_byte_queue_get_data (LacByteQueue *queue, queue->start = queue->segment; queue->end = queue->segment; + + if (!result) + result = (guint8 *)""; /* the result of this function is guaranteed + * to be NULL terminated + */ return result; } @@ -101,13 +112,20 @@ power_of_two_bigger_than (gsize n) } static void +null_terminate (LacByteQueue *queue) +{ + if (queue->end) + *(queue->end) = '\0'; +} + +static void ensure_room (LacByteQueue *queue, gsize extra) { gsize old_data_size = queue->end - queue->start; gsize new_data_size = old_data_size + extra; - if (queue->end + new_data_size > queue->segment + queue->segment_size) + if (queue->end + new_data_size + 1 > queue->segment + queue->segment_size) { gsize new_segment_size = power_of_two_bigger_than (2 * new_data_size); @@ -121,6 +139,8 @@ ensure_room (LacByteQueue *queue, queue->start = queue->segment; queue->end = queue->start + old_data_size; + + null_terminate (queue); } } @@ -156,6 +176,8 @@ lac_byte_queue_alloc_tail (LacByteQueue *queue, ensure_room (queue, size); queue->end += size; + + null_terminate (queue); return queue->end - size; } @@ -168,6 +190,8 @@ lac_byte_queue_delete_tail (LacByteQueue *queue, queue->end = queue->start; else queue->end -= size; + + null_terminate (queue); } void @@ -186,6 +210,8 @@ lac_byte_queue_append (LacByteQueue *queue, memcpy (tail, bytes, n_bytes); } + + null_terminate (queue); } /* Transfer data from @src to @dest, if possible without copying. @@ -204,11 +230,8 @@ lac_byte_queue_steal_data (LacByteQueue *dest, dest->segment = src->segment; dest->start = src->start; dest->end = src->end; - - src->segment_size = 0; - src->segment = NULL; - src->start = NULL; - src->end = NULL; + + initialize (src); } else { |