Detect webp files by file content (fix #2807)

This commit is contained in:
David Capello 2021-07-01 11:37:11 -03:00
parent f99b5218b9
commit ffe65bface
3 changed files with 16 additions and 6 deletions

View File

@ -1,4 +1,4 @@
Copyright (c) 2018 Igara Studio S.A.
Copyright (c) 2018-2021 Igara Studio S.A.
Copyright (c) 2016-2018 David Capello
Permission is hereby granted, free of charge, to any person obtaining

View File

@ -1,4 +1,5 @@
// Aseprite Document IO Library
// Copyright (c) 2021 Igara Studio S.A.
// Copyright (c) 2017 David Capello
//
// This file is released under the terms of the MIT license.
@ -22,8 +23,8 @@ bool decode_file(DecodeDelegate* delegate,
assert(delegate);
assert(f);
std::vector<uint8_t> buf(8, 0);
size_t n = f->readBytes(&buf[0], 8);
uint8_t buf[12];
size_t n = f->readBytes(&buf[0], 12);
FileFormat format = detect_format_by_file_content_bytes(&buf[0], n);
f->seek(0); // Rewind

View File

@ -1,4 +1,5 @@
// Aseprite Document IO Library
// Copyright (c) 2021 Igara Studio S.A.
// Copyright (c) 2016-2018 David Capello
//
// This file is released under the terms of the MIT license.
@ -20,6 +21,8 @@
#define GIF_89_STAMP "GIF89a"
#define PNG_MAGIC_DWORD1 0x474E5089
#define PNG_MAGIC_DWORD2 0x0A1A0A0D
#define WEBP_STAMP_1 "RIFF" // "RIFFnnnnWEBP"
#define WEBP_STAMP_2 "WEBP"
namespace dio {
@ -47,6 +50,12 @@ FileFormat detect_format_by_file_content_bytes(const uint8_t* buf,
if (n >= 2) {
if (n >= 6) {
if (n >= 8) {
if (n >= 12) {
if (std::strncmp((const char*)buf, WEBP_STAMP_1, 4) == 0 ||
std::strncmp((const char*)buf+8, WEBP_STAMP_2, 4) == 0)
return FileFormat::WEBP_ANIMATION;
}
if (IS_MAGIC_DWORD(0, PNG_MAGIC_DWORD1) &&
IS_MAGIC_DWORD(4, PNG_MAGIC_DWORD2))
return FileFormat::PNG_IMAGE;
@ -81,8 +90,8 @@ FileFormat detect_format_by_file_content(const std::string& filename)
return FileFormat::ERROR;
FILE* f = handle.get();
uint8_t buf[8];
int n = (int)fread(buf, 1, 8, f);
uint8_t buf[12];
int n = (int)fread(buf, 1, 12, f);
return detect_format_by_file_content_bytes(buf, n);
}
@ -134,7 +143,7 @@ FileFormat detect_format_by_file_extension(const std::string& filename)
if (ext == "png")
return FileFormat::PNG_IMAGE;
if (ext == "svg")
return FileFormat::SVG_IMAGE;