From 4e3a64fcc746f9ce2badea1926802cf3ff601f35 Mon Sep 17 00:00:00 2001 From: JustOff Date: Sat, 9 Sep 2017 15:27:00 +0300 Subject: [PATCH] Detect WEBP from content even if it's received with wrong mime type --- image/src/imgLoader.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/image/src/imgLoader.cpp b/image/src/imgLoader.cpp index 6ecaf6d205..ee7236fe79 100644 --- a/image/src/imgLoader.cpp +++ b/image/src/imgLoader.cpp @@ -2400,6 +2400,26 @@ nsresult imgLoader::GetMimeTypeFromContent(const char* aContents, uint32_t aLeng aContentType.AssignLiteral(IMAGE_JPEG); } + /* maybe a WEBP? */ + /* WebP files are technically RIFF files. RIFF is a container format like TIFF. + * WebP files are RIFF files that contain a single WEBP chunk. + * + * To detect it, first check that the first 4 bytes are "RIFF", + * and then that bytes 8-11 are "WEBP". + */ + else if (aLength >= 12 && + ((unsigned char)aContents[0])==0x52 && + ((unsigned char)aContents[1])==0x49 && + ((unsigned char)aContents[2])==0x46 && + ((unsigned char)aContents[3])==0x46 && + ((unsigned char)aContents[8])==0x57 && + ((unsigned char)aContents[9])==0x45 && + ((unsigned char)aContents[10])==0x42 && + ((unsigned char)aContents[11])==0x50) + { + aContentType.AssignLiteral(IMAGE_WEBP); + } + /* or how about ART? */ /* ART begins with JG (4A 47). Major version offset 2. * Minor version offset 3. Offset 4 must be nullptr.