From d6215cf4a5f416cf0b64a4fbba95c519f03fe467 Mon Sep 17 00:00:00 2001 From: yujieqin Date: Thu, 10 Mar 2016 05:15:49 -0800 Subject: [PATCH] Add a quick check to the TIFF header of DNG image BUG=b/27475341 GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1782063002 Review URL: https://codereview.chromium.org/1782063002 --- src/codec/SkRawCodec.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/codec/SkRawCodec.cpp b/src/codec/SkRawCodec.cpp index 609b0ae..208bd89 100644 --- a/src/codec/SkRawCodec.cpp +++ b/src/codec/SkRawCodec.cpp @@ -215,6 +215,8 @@ public: } private: + // Most of valid RAW images will not be larger than 100MB. This limit is helpful to avoid + // streaming too large data chunk. We can always adjust the limit here if we need. const size_t kMaxStreamSize = 100 * 1024 * 1024; // 100MB typedef SkDynamicMemoryWStream INHERITED; @@ -445,6 +447,10 @@ public: */ static SkDngImage* NewFromStream(SkRawStream* stream) { SkAutoTDelete dngImage(new SkDngImage(stream)); + if (!dngImage->isTiffHeaderValid()) { + return nullptr; + } + if (!dngImage->initFromPiex()) { if (!dngImage->readDng()) { return nullptr; @@ -522,6 +528,20 @@ public: } private: + // Quick check if the image contains a valid TIFF header as requested by DNG format. + bool isTiffHeaderValid() const { + const size_t kHeaderSize = 4; + SkAutoSTMalloc header(kHeaderSize); + if (!fStream->read(header.get(), 0 /* offset */, kHeaderSize)) { + return false; + } + + // Check if the header is valid (endian info and magic number "42"). + return + (header[0] == 0x49 && header[1] == 0x49 && header[2] == 0x2A && header[3] == 0x00) || + (header[0] == 0x4D && header[1] == 0x4D && header[2] == 0x00 && header[3] == 0x2A); + } + void init(const int width, const int height, const dng_point& cfaPatternSize) { fImageInfo = SkImageInfo::Make(width, height, kN32_SkColorType, kOpaque_SkAlphaType); -- 2.7.4