diff options
author | 2013-08-21 14:56:09 +0000 | |
---|---|---|
committer | 2013-08-21 14:56:09 +0000 | |
commit | 3c8730a84e469cfc71866a2f8d6bfd1d2ea689e3 (patch) | |
tree | e682785532351bac776a35ad158fc585c7abb7d2 | |
parent | af9296e1e96a6ccec9630da769ef5e19456e369a (diff) |
Remove dependency on getLength from webp decoder.
In webp_parse_header, continue reading until end of stream is reached,
or we have read WEBP_VP8_HEADER_SIZE bytes. Do not check to see if the
stream was too short, since it may not have a way to report its length,
and WEBP_VP8_HEADER_SIZE is padded slightly. Instead, depend on
WebPGetFeatures to report that the stream did not have enough data.
In webp_idecode, only check length if it is available.
BUG=https://b.corp.google.com/issue?id=8432093
R=djsollen@google.com, vikasa@google.com
Review URL: https://codereview.chromium.org/22841005
git-svn-id: http://skia.googlecode.com/svn/trunk@10848 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | src/images/SkImageDecoder_libwebp.cpp | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/src/images/SkImageDecoder_libwebp.cpp b/src/images/SkImageDecoder_libwebp.cpp index 4691d0d13e..9f1116e5d5 100644 --- a/src/images/SkImageDecoder_libwebp.cpp +++ b/src/images/SkImageDecoder_libwebp.cpp @@ -60,16 +60,24 @@ static const size_t WEBP_IDECODE_BUFFER_SZ = (1 << 16); // Parse headers of RIFF container, and check for valid Webp (VP8) content. static bool webp_parse_header(SkStream* stream, int* width, int* height, int* alpha) { unsigned char buffer[WEBP_VP8_HEADER_SIZE]; - const uint32_t contentSize = stream->getLength(); - const size_t len = stream->read(buffer, WEBP_VP8_HEADER_SIZE); - const uint32_t read_bytes = - (contentSize < WEBP_VP8_HEADER_SIZE) ? contentSize : WEBP_VP8_HEADER_SIZE; - if (len != read_bytes) { - return false; // can't read enough - } + size_t bytesToRead = WEBP_VP8_HEADER_SIZE; + size_t totalBytesRead = 0; + do { + unsigned char* dst = buffer + totalBytesRead; + const size_t bytesRead = stream->read(dst, bytesToRead); + if (0 == bytesRead) { + // Could not read any bytes. Check to see if we are at the end (exit + // condition), and continue reading if not. Important for streams + // that do not have all the data ready. + continue; + } + bytesToRead -= bytesRead; + totalBytesRead += bytesRead; + SkASSERT(bytesToRead + totalBytesRead == WEBP_VP8_HEADER_SIZE); + } while (!stream->isAtEnd() && bytesToRead > 0); WebPBitstreamFeatures features; - VP8StatusCode status = WebPGetFeatures(buffer, read_bytes, &features); + VP8StatusCode status = WebPGetFeatures(buffer, totalBytesRead, &features); if (VP8_STATUS_OK != status) { return false; // Invalid WebP file. } @@ -192,9 +200,8 @@ static bool webp_idecode(SkStream* stream, WebPDecoderConfig* config) { } stream->rewind(); - const uint32_t contentSize = stream->getLength(); - const uint32_t readBufferSize = (contentSize < WEBP_IDECODE_BUFFER_SZ) ? - contentSize : WEBP_IDECODE_BUFFER_SZ; + const size_t readBufferSize = stream->hasLength() ? + SkTMin(stream->getLength(), WEBP_IDECODE_BUFFER_SZ) : WEBP_IDECODE_BUFFER_SZ; SkAutoMalloc srcStorage(readBufferSize); unsigned char* input = (uint8_t*)srcStorage.get(); if (NULL == input) { |