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/src@10848 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/images/SkImageDecoder_libwebp.cpp b/images/SkImageDecoder_libwebp.cpp
index 4691d0d..9f1116e 100644
--- a/images/SkImageDecoder_libwebp.cpp
+++ b/images/SkImageDecoder_libwebp.cpp
@@ -60,16 +60,24 @@
 // 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 @@
     }
 
     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) {