diff options
author | Max Kellermann <max@duempel.org> | 2013-12-14 12:43:06 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-07-09 19:03:58 +0200 |
commit | 0759421d119d8d3d829978ede81683ca54d65d69 (patch) | |
tree | ecb6968a7842e6b12de5f2c8b1e194358d697872 | |
parent | bf7417981f2527348fb261f40f4de15f9b350db5 (diff) |
DecoderAPI: add function decoder_read_full()
Move code from the "mad" plugin.
-rw-r--r-- | src/DecoderAPI.cxx | 18 | ||||
-rw-r--r-- | src/DecoderAPI.hxx | 11 | ||||
-rw-r--r-- | src/decoder/MadDecoderPlugin.cxx | 14 | ||||
-rw-r--r-- | test/FakeDecoderAPI.cxx | 18 | ||||
-rw-r--r-- | test/run_decoder.cxx | 18 |
5 files changed, 67 insertions, 12 deletions
diff --git a/src/DecoderAPI.cxx b/src/DecoderAPI.cxx index 52b30cfb5..e4122d60e 100644 --- a/src/DecoderAPI.cxx +++ b/src/DecoderAPI.cxx @@ -293,6 +293,24 @@ decoder_read(Decoder *decoder, } bool +decoder_read_full(Decoder *decoder, InputStream &is, + void *_buffer, size_t size) +{ + uint8_t *buffer = (uint8_t *)_buffer; + + while (size > 0) { + size_t nbytes = decoder_read(decoder, is, buffer, size); + if (nbytes == 0) + return false; + + buffer += nbytes; + size -= nbytes; + } + + return true; +} + +bool decoder_skip(Decoder *decoder, InputStream &is, size_t size) { while (size > 0) { diff --git a/src/DecoderAPI.hxx b/src/DecoderAPI.hxx index 50cd3e74a..a9da76305 100644 --- a/src/DecoderAPI.hxx +++ b/src/DecoderAPI.hxx @@ -113,6 +113,17 @@ decoder_read(Decoder &decoder, InputStream &is, } /** + * Blocking read from the input stream. Attempts to fill the buffer + * completely; there is no partial result. + * + * @return true on success, false on error or command or not enough + * data + */ +bool +decoder_read_full(Decoder *decoder, InputStream &is, + void *buffer, size_t size); + +/** * Skip data on the #InputStream. * * @return true on success, false on error or command diff --git a/src/decoder/MadDecoderPlugin.cxx b/src/decoder/MadDecoderPlugin.cxx index 7afc8ef8f..6f619b34b 100644 --- a/src/decoder/MadDecoderPlugin.cxx +++ b/src/decoder/MadDecoderPlugin.cxx @@ -353,18 +353,8 @@ MadDecoder::ParseId3(size_t tagsize, Tag **mpd_tag) memcpy(allocated, stream.this_frame, count); mad_stream_skip(&(stream), count); - while (count < tagsize) { - size_t len; - - len = decoder_read(decoder, input_stream, - allocated + count, tagsize - count); - if (len == 0) - break; - else - count += len; - } - - if (count != tagsize) { + if (!decoder_read_full(decoder, input_stream, + allocated + count, tagsize - count)) { LogDebug(mad_domain, "error parsing ID3 tag"); g_free(allocated); return; diff --git a/test/FakeDecoderAPI.cxx b/test/FakeDecoderAPI.cxx index 469e37feb..ca09ca982 100644 --- a/test/FakeDecoderAPI.cxx +++ b/test/FakeDecoderAPI.cxx @@ -66,6 +66,24 @@ decoder_read(gcc_unused Decoder *decoder, } bool +decoder_read_full(Decoder *decoder, InputStream &is, + void *_buffer, size_t size) +{ + uint8_t *buffer = (uint8_t *)_buffer; + + while (size > 0) { + size_t nbytes = decoder_read(decoder, is, buffer, size); + if (nbytes == 0) + return false; + + buffer += nbytes; + size -= nbytes; + } + + return true; +} + +bool decoder_skip(Decoder *decoder, InputStream &is, size_t size) { while (size > 0) { diff --git a/test/run_decoder.cxx b/test/run_decoder.cxx index cab68c64e..7db8dde22 100644 --- a/test/run_decoder.cxx +++ b/test/run_decoder.cxx @@ -102,6 +102,24 @@ decoder_read(gcc_unused Decoder *decoder, } bool +decoder_read_full(Decoder *decoder, InputStream &is, + void *_buffer, size_t size) +{ + uint8_t *buffer = (uint8_t *)_buffer; + + while (size > 0) { + size_t nbytes = decoder_read(decoder, is, buffer, size); + if (nbytes == 0) + return false; + + buffer += nbytes; + size -= nbytes; + } + + return true; +} + +bool decoder_skip(Decoder *decoder, InputStream &is, size_t size) { while (size > 0) { |