formats: check magic bytes for jpeg and webp

This commit is contained in:
Maximilian Seidler 2025-07-09 10:55:54 +02:00
parent 45dcdd67d2
commit fc22c66103
3 changed files with 7 additions and 1 deletions

View file

@ -74,7 +74,7 @@ Hyprgraphics::CImage::CImage(const std::string& path) : filepath(path) {
if (first_word == "PNG") {
CAIROSURFACE = PNG::createSurfaceFromPNG(path);
mime = "image/png";
} else if (first_word == "JPEG" && !type_str.contains("XL") && !type_str.contains("2000") {
} else if (first_word == "JPEG" && !type_str.contains("XL") && !type_str.contains("2000")) {
CAIROSURFACE = JPEG::createSurfaceFromJPEG(path);
imageHasAlpha = false;
mime = "image/jpeg";

View file

@ -19,6 +19,9 @@ std::expected<cairo_surface_t*, std::string> JPEG::createSurfaceFromJPEG(const s
file.seekg(0);
file.read(reinterpret_cast<char*>(bytes.data()), bytes.size());
if (bytes[0] != 0xFF || bytes[1] != 0xD8)
return std::unexpected("loading jpeg: invalid magic bytes");
// now the JPEG is in the memory
jpeg_decompress_struct decompressStruct = {};

View file

@ -17,6 +17,9 @@ std::expected<cairo_surface_t*, std::string> WEBP::createSurfaceFromWEBP(const s
file.seekg(0);
file.read(reinterpret_cast<char*>(bytes.data()), bytes.size());
if (bytes[0] != 'R' || bytes[1] != 'I' || bytes[2] != 'F' || bytes[3] != 'F')
return std::unexpected("loading webp: invalid magic bytes");
// now the WebP is in the memory
WebPDecoderConfig config;