diff options
Diffstat (limited to 'src/DecoderBuffer.cxx')
-rw-r--r-- | src/DecoderBuffer.cxx | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/src/DecoderBuffer.cxx b/src/DecoderBuffer.cxx index 6aad53cb2..18fd3aa5c 100644 --- a/src/DecoderBuffer.cxx +++ b/src/DecoderBuffer.cxx @@ -20,11 +20,12 @@ #include "config.h" #include "DecoderBuffer.hxx" #include "DecoderAPI.hxx" - -#include <glib.h> +#include "util/ConstBuffer.hxx" +#include "util/VarSize.hxx" #include <assert.h> #include <string.h> +#include <stdlib.h> struct DecoderBuffer { Decoder *decoder; @@ -42,24 +43,22 @@ struct DecoderBuffer { /** the actual buffer (dynamic size) */ unsigned char data[sizeof(size_t)]; + + DecoderBuffer(Decoder *_decoder, InputStream &_is, + size_t _size) + :decoder(_decoder), is(&_is), + size(_size), length(0), consumed(0) {} }; DecoderBuffer * decoder_buffer_new(Decoder *decoder, InputStream &is, size_t size) { - DecoderBuffer *buffer = (DecoderBuffer *) - g_malloc(sizeof(*buffer) - sizeof(buffer->data) + size); - assert(size > 0); - buffer->decoder = decoder; - buffer->is = &is; - buffer->size = size; - buffer->length = 0; - buffer->consumed = 0; - - return buffer; + return NewVarSize<DecoderBuffer>(sizeof(DecoderBuffer::data), + size, + decoder, is, size); } void @@ -67,7 +66,7 @@ decoder_buffer_free(DecoderBuffer *buffer) { assert(buffer != nullptr); - g_free(buffer); + DeleteVarSize(buffer); } bool @@ -82,6 +81,12 @@ decoder_buffer_is_full(const DecoderBuffer *buffer) return buffer->consumed == 0 && buffer->length == buffer->size; } +void +decoder_buffer_clear(DecoderBuffer *buffer) +{ + buffer->length = buffer->consumed = 0; +} + static void decoder_buffer_shift(DecoderBuffer *buffer) { @@ -118,15 +123,13 @@ decoder_buffer_fill(DecoderBuffer *buffer) return true; } -const void * -decoder_buffer_read(const DecoderBuffer *buffer, size_t *length_r) +ConstBuffer<void> +decoder_buffer_read(const DecoderBuffer *buffer) { - if (buffer->consumed >= buffer->length) - /* buffer is empty */ - return nullptr; - - *length_r = buffer->length - buffer->consumed; - return buffer->data + buffer->consumed; + return { + buffer->data + buffer->consumed, + buffer->length - buffer->consumed + }; } void @@ -143,19 +146,17 @@ decoder_buffer_consume(DecoderBuffer *buffer, size_t nbytes) bool decoder_buffer_skip(DecoderBuffer *buffer, size_t nbytes) { - size_t length; - const void *data; bool success; /* this could probably be optimized by seeking */ while (true) { - data = decoder_buffer_read(buffer, &length); - if (data != nullptr) { - if (length > nbytes) - length = nbytes; - decoder_buffer_consume(buffer, length); - nbytes -= length; + auto data = decoder_buffer_read(buffer); + if (!data.IsEmpty()) { + if (data.size > nbytes) + data.size = nbytes; + decoder_buffer_consume(buffer, data.size); + nbytes -= data.size; if (nbytes == 0) return true; } |