Index: src/codec/SkCodec.cpp |
diff --git a/src/codec/SkCodec.cpp b/src/codec/SkCodec.cpp |
index 11eb1f98479461b7307695ac49d44cde6f85cecb..27fe88befb07e2ed377a115001b7edb93f897349 100644 |
--- a/src/codec/SkCodec.cpp |
+++ b/src/codec/SkCodec.cpp |
@@ -20,7 +20,7 @@ |
#include "SkWebpCodec.h" |
struct DecoderProc { |
- bool (*IsFormat)(SkStream*); |
+ bool (*IsFormat)(const char*, size_t); |
SkCodec* (*NewFromStream)(SkStream*); |
}; |
@@ -42,23 +42,29 @@ SkCodec* SkCodec::NewFromStream(SkStream* stream, |
} |
SkAutoTDelete<SkStream> streamDeleter(stream); |
- |
+ |
+ // 14 is enough to read all of the supported types. |
scroggo
2015/11/25 22:08:35
I've convinced myself that 14 bytes is enough to s
msarett
2015/11/30 14:08:11
This matches my understanding as well. Looks like
scroggo
2015/11/30 20:02:30
Added a test.
|
+ size_t bytesRead = 14; |
+ char buffer[bytesRead]; |
+ if (!stream->peek(buffer, bytesRead)) { |
+ SkCodecPrintf("Could not peek!\n"); |
+ // It is possible the stream does not support peeking, but does support |
+ // rewinding, so try reading. |
+ // (Is there a valid image that is less than 14 bytes, causing peek to |
+ // fail, even though we read enough?) |
msarett
2015/11/30 14:08:11
Good question. I don't know, but I would guess th
scroggo
2015/11/30 20:02:30
Yeah, that should do it:
1 + 1 (headers) + 1 (wid
|
+ bytesRead = stream->read(buffer, bytesRead); |
+ if (!stream->rewind()) { |
+ return nullptr; |
+ } |
+ } |
SkAutoTDelete<SkCodec> codec(nullptr); |
// PNG is special, since we want to be able to supply an SkPngChunkReader. |
// But this code follows the same pattern as the loop. |
- const bool isPng = SkPngCodec::IsPng(stream); |
- if (!stream->rewind()) { |
- return NULL; |
- } |
- if (isPng) { |
+ if (SkPngCodec::IsPng((const char*)buffer, bytesRead)) { |
codec.reset(SkPngCodec::NewFromStream(streamDeleter.detach(), chunkReader)); |
} else { |
for (DecoderProc proc : gDecoderProcs) { |
- const bool correctFormat = proc.IsFormat(stream); |
- if (!stream->rewind()) { |
- return nullptr; |
- } |
- if (correctFormat) { |
+ if (proc.IsFormat(buffer, bytesRead)) { |
codec.reset(proc.NewFromStream(streamDeleter.detach())); |
break; |
} |