From 70279ab5a26b62e78f5e0a6815f5b64771e87758 Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Sun, 3 Sep 2017 12:51:21 +1000 Subject: [PATCH] that doesn't require an instance (nw) --- src/emu/rendutil.cpp | 3 +-- src/lib/util/png.cpp | 36 ++++++++++++++++++------------------ src/lib/util/png.h | 3 ++- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/emu/rendutil.cpp b/src/emu/rendutil.cpp index d1ffecd7c7b..3511d311bdc 100644 --- a/src/emu/rendutil.cpp +++ b/src/emu/rendutil.cpp @@ -815,8 +815,7 @@ ru_imgformat render_detect_image(emu_file &file, const char *dirname, const char return RENDUTIL_IMGFORMAT_ERROR; // PNG: check for valid header - png_info png; - png_error const result = png.verify_header(file); + png_error const result = png_info::verify_header(file); if (result == PNGERR_NONE) { file.close(); diff --git a/src/lib/util/png.cpp b/src/lib/util/png.cpp index a62d08c8161..0a3b616b01f 100644 --- a/src/lib/util/png.cpp +++ b/src/lib/util/png.cpp @@ -66,7 +66,7 @@ void png_info::free_data() namespace { -#define PNG_Signature "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A" +constexpr std::uint8_t PNG_SIGNATURE[] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; #define MNG_Signature "\x8A\x4D\x4E\x47\x0D\x0A\x1A\x0A" // Chunk names @@ -690,21 +690,6 @@ public: return PNGERR_NONE; } - png_error verify_header(util::core_file &fp) - { - uint8_t signature[8]; - - /* read 8 bytes */ - if (fp.read(signature, 8) != 8) - return PNGERR_FILE_TRUNCATED; - - /* return an error if we don't match */ - if (memcmp(signature, PNG_Signature, 8) != 0) - return PNGERR_BAD_SIGNATURE; - - return PNGERR_NONE; - } - png_error read_file(util::core_file &fp) { // initialize the data structures @@ -741,6 +726,21 @@ public: return error; } + + static png_error verify_header(util::core_file &fp) + { + EQUIVALENT_ARRAY(PNG_SIGNATURE, std::uint8_t) signature; + + // read 8 bytes + if (fp.read(signature, sizeof(signature)) != sizeof(signature)) + return PNGERR_FILE_TRUNCATED; + + // return an error if we don't match + if (std::memcmp(signature, PNG_SIGNATURE, sizeof(PNG_SIGNATURE))) + return PNGERR_BAD_SIGNATURE; + + return PNGERR_NONE; + } }; constexpr unsigned png_private::ADAM7_X_BIAS[7]; @@ -762,7 +762,7 @@ constexpr unsigned png_private::ADAM7_Y_OFFS[7]; png_error png_info::verify_header(util::core_file &fp) { - return png_private(*this).verify_header(fp); + return png_private::verify_header(fp); } @@ -1230,7 +1230,7 @@ png_error png_write_bitmap(util::core_file &fp, png_info *info, bitmap_t const & info = &pnginfo; // write the PNG signature - if (fp.write(PNG_Signature, 8) != 8) + if (fp.write(PNG_SIGNATURE, sizeof(PNG_SIGNATURE)) != sizeof(PNG_SIGNATURE)) return PNGERR_FILE_ERROR; /* write the rest of the PNG data */ diff --git a/src/lib/util/png.h b/src/lib/util/png.h index 3925f6e4032..ff14ff5f674 100644 --- a/src/lib/util/png.h +++ b/src/lib/util/png.h @@ -57,7 +57,6 @@ public: ~png_info() { free_data(); } - png_error verify_header(util::core_file &fp); png_error read_file(util::core_file &fp); png_error copy_to_bitmap(bitmap_argb32 &bitmap, bool &hasalpha); png_error expand_buffer_8bit(); @@ -67,6 +66,8 @@ public: void free_data(); void reset() { free_data(); operator=(png_info()); } + static png_error verify_header(util::core_file &fp); + std::unique_ptr image; std::uint32_t width, height; std::uint32_t xres = 0, yres = 0;