From b73f36695e968e1b405df4087894911ab3477b81 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Tue, 5 May 2026 08:43:08 +0200 Subject: [PATCH] refactor image type check --- src/kds_s2000w_image_type_check.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/kds_s2000w_image_type_check.c b/src/kds_s2000w_image_type_check.c index a6a396c..f05820c 100644 --- a/src/kds_s2000w_image_type_check.c +++ b/src/kds_s2000w_image_type_check.c @@ -1,34 +1,34 @@ #include +#include #include "kds_s2000w_image_type_check.h" +#define TIFF_SIG_LEN 4 +#define PNM_SIG_LEN 2 +#define JPEG_SIG_LEN 2 + bool kds_s2000w_image_type_check_is_tiff(const blobdata_t* image) { - if (image->size < 4) + if (image->size < TIFF_SIG_LEN) return false; - const char* image_data = (const char*) image->data; - bool result = image_data[0] == (char)0x49; - result = result && image_data[1] == (char)0x49; - result = result && image_data[2] == (char)0x2A; - return result && image_data[3] == (char)0x00; + const uint8_t tiff_sig[] = {0x49, 0x49, 0x2a, 0x00}; + return memcmp(image->data, tiff_sig, TIFF_SIG_LEN) == 0; } bool kds_s2000w_image_type_check_is_pnm(const blobdata_t* image) { - if (image->size < 2) + if (image->size < PNM_SIG_LEN) return false; - const char* image_data = (const char*) image->data; - return strncmp(image_data, "P4", 2) == 0 - || (strncmp(image_data, "P6", 2) == 0); + return memcmp(image->data, "P4", PNM_SIG_LEN) == 0 + || memcmp(image->data, "P6", PNM_SIG_LEN) == 0; } bool kds_s2000w_image_type_check_is_jpeg(const blobdata_t* image) { - if (image->size < 2) + if (image->size < JPEG_SIG_LEN) return false; - const char* image_data = (const char*) image->data; - bool result = image_data[0] == (char)0xff; - return result && image_data[1] == (char)0xd8; + const uint8_t jpeg_sig[] = {0xff, 0xd8}; + return memcmp(image->data, jpeg_sig, JPEG_SIG_LEN) == 0; } \ No newline at end of file -- 2.47.3