mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:17:36 +00:00
LibGfx: Make validate_before_create() create a regular bool
This is for validating that a decoder with a weak or nonexistent sniff() method thinks it can decode an image. This should not be treated as an error. No behavior change.
This commit is contained in:
parent
69e4f924b7
commit
6607757b08
4 changed files with 12 additions and 10 deletions
|
@ -557,7 +557,7 @@ TEST_CASE(test_ppm)
|
||||||
TEST_CASE(test_targa_bottom_left)
|
TEST_CASE(test_targa_bottom_left)
|
||||||
{
|
{
|
||||||
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-uncompressed.tga"sv)));
|
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-uncompressed.tga"sv)));
|
||||||
EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
|
EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
|
||||||
auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
|
auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
|
||||||
|
|
||||||
TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
|
TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
|
||||||
|
@ -566,7 +566,7 @@ TEST_CASE(test_targa_bottom_left)
|
||||||
TEST_CASE(test_targa_top_left)
|
TEST_CASE(test_targa_top_left)
|
||||||
{
|
{
|
||||||
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-uncompressed.tga"sv)));
|
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-uncompressed.tga"sv)));
|
||||||
EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
|
EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
|
||||||
auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
|
auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
|
||||||
|
|
||||||
TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
|
TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
|
||||||
|
@ -575,7 +575,7 @@ TEST_CASE(test_targa_top_left)
|
||||||
TEST_CASE(test_targa_bottom_left_compressed)
|
TEST_CASE(test_targa_bottom_left_compressed)
|
||||||
{
|
{
|
||||||
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-compressed.tga"sv)));
|
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-bottom-left-compressed.tga"sv)));
|
||||||
EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
|
EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
|
||||||
auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
|
auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
|
||||||
|
|
||||||
TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
|
TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
|
||||||
|
@ -584,7 +584,7 @@ TEST_CASE(test_targa_bottom_left_compressed)
|
||||||
TEST_CASE(test_targa_top_left_compressed)
|
TEST_CASE(test_targa_top_left_compressed)
|
||||||
{
|
{
|
||||||
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-compressed.tga"sv)));
|
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-top-left-compressed.tga"sv)));
|
||||||
EXPECT(TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes())));
|
EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
|
||||||
auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
|
auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));
|
||||||
|
|
||||||
TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
|
TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
|
||||||
|
|
|
@ -64,7 +64,7 @@ static ErrorOr<OwnPtr<ImageDecoderPlugin>> probe_and_sniff_for_appropriate_plugi
|
||||||
static OwnPtr<ImageDecoderPlugin> probe_and_sniff_for_appropriate_plugin_with_known_mime_type(StringView mime_type, ReadonlyBytes bytes)
|
static OwnPtr<ImageDecoderPlugin> probe_and_sniff_for_appropriate_plugin_with_known_mime_type(StringView mime_type, ReadonlyBytes bytes)
|
||||||
{
|
{
|
||||||
struct ImagePluginWithMIMETypeInitializer {
|
struct ImagePluginWithMIMETypeInitializer {
|
||||||
ErrorOr<bool> (*validate_before_create)(ReadonlyBytes) = nullptr;
|
bool (*validate_before_create)(ReadonlyBytes) = nullptr;
|
||||||
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> (*create)(ReadonlyBytes) = nullptr;
|
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> (*create)(ReadonlyBytes) = nullptr;
|
||||||
StringView mime_type;
|
StringView mime_type;
|
||||||
};
|
};
|
||||||
|
@ -76,7 +76,7 @@ static OwnPtr<ImageDecoderPlugin> probe_and_sniff_for_appropriate_plugin_with_kn
|
||||||
for (auto& plugin : s_initializers_with_mime_type) {
|
for (auto& plugin : s_initializers_with_mime_type) {
|
||||||
if (plugin.mime_type != mime_type)
|
if (plugin.mime_type != mime_type)
|
||||||
continue;
|
continue;
|
||||||
auto validation_result = plugin.validate_before_create(bytes).release_value_but_fixme_should_propagate_errors();
|
auto validation_result = plugin.validate_before_create(bytes);
|
||||||
if (!validation_result)
|
if (!validation_result)
|
||||||
continue;
|
continue;
|
||||||
auto plugin_decoder = plugin.create(bytes);
|
auto plugin_decoder = plugin.create(bytes);
|
||||||
|
|
|
@ -91,11 +91,13 @@ ErrorOr<void> TGAImageDecoderPlugin::decode_tga_header()
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<bool> TGAImageDecoderPlugin::validate_before_create(ReadonlyBytes data)
|
bool TGAImageDecoderPlugin::validate_before_create(ReadonlyBytes data)
|
||||||
{
|
{
|
||||||
FixedMemoryStream stream { data };
|
FixedMemoryStream stream { data };
|
||||||
auto header = TRY(stream.read_value<Gfx::TGAHeader>());
|
auto header_or_err = stream.read_value<Gfx::TGAHeader>();
|
||||||
return !ensure_header_validity(header, data.size()).is_error();
|
if (header_or_err.is_error())
|
||||||
|
return false;
|
||||||
|
return !ensure_header_validity(header_or_err.release_value(), data.size()).is_error();
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> TGAImageDecoderPlugin::create(ReadonlyBytes data)
|
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> TGAImageDecoderPlugin::create(ReadonlyBytes data)
|
||||||
|
|
|
@ -14,7 +14,7 @@ struct TGALoadingContext;
|
||||||
|
|
||||||
class TGAImageDecoderPlugin final : public ImageDecoderPlugin {
|
class TGAImageDecoderPlugin final : public ImageDecoderPlugin {
|
||||||
public:
|
public:
|
||||||
static ErrorOr<bool> validate_before_create(ReadonlyBytes);
|
static bool validate_before_create(ReadonlyBytes);
|
||||||
static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
|
static ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> create(ReadonlyBytes);
|
||||||
|
|
||||||
virtual ~TGAImageDecoderPlugin() override;
|
virtual ~TGAImageDecoderPlugin() override;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue