From 4301074a54a84128b89482c65faf26488b05b871 Mon Sep 17 00:00:00 2001 From: Enno Boland Date: Fri, 18 Aug 2023 12:46:55 +0200 Subject: [PATCH] reader: refactor reader_fill_buffer() This changes reader_fill_buffer() to calculate the remaining size to be copied to the buffer instead of using the size that is already copied to the buffer. Also this commit adds assertions to check if the result of reader_fill_buffer() is sound. --- lib/reader/reader.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/reader/reader.c b/lib/reader/reader.c index 7a09d548b..d1fca24a4 100644 --- a/lib/reader/reader.c +++ b/lib/reader/reader.c @@ -114,36 +114,38 @@ reader_fill_buffer(struct SqshReader *reader, size_t size) { const struct SqshReaderIteratorImpl *impl = reader->iterator_impl; struct SqshBuffer *buffer = &reader->buffer; sqsh_index_t offset = reader->offset; + sqsh_index_t iterator_offset = reader->iterator_offset; - size_t buffer_size = sqsh__buffer_size(buffer); + size_t remaining_size = size - sqsh__buffer_size(buffer); for (;;) { const uint8_t *data = impl->data(iterator); const size_t data_size = impl->size(iterator); - const size_t copy_size = - SQSH_MIN(data_size - offset, size - buffer_size); + const size_t copy_size = SQSH_MIN(data_size - offset, remaining_size); rv = sqsh__buffer_append(buffer, &data[offset], copy_size); if (rv < 0) { goto out; } offset = 0; - buffer_size += copy_size; - if (size <= buffer_size) { - assert(size == buffer_size); + remaining_size -= copy_size; + if (remaining_size == 0) { break; } - rv = reader_iterator_next(reader, size); + iterator_offset = size - remaining_size; + rv = reader_iterator_next(reader, remaining_size); if (rv < 0) { goto out; } - reader->iterator_offset = buffer_size; } + assert(size == sqsh__buffer_size(buffer)); + assert(iterator_offset != 0); + + reader->iterator_offset = iterator_offset; reader->offset = 0; reader->data = sqsh__buffer_data(buffer); reader->size = size; - assert(reader->iterator_offset != 0); out: return rv; }