]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
refactor image type check
authorBastian Dehn <hhaalo@arcor.de>
Tue, 5 May 2026 06:43:08 +0000 (08:43 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Tue, 5 May 2026 06:43:15 +0000 (08:43 +0200)
src/kds_s2000w_image_type_check.c

index a6a396ca7f508f44c4cdb0c6657e4f788d30d805..f05820c0c494c83338bf8ce3f2172ce3e62df1ae 100644 (file)
@@ -1,34 +1,34 @@
 #include <string.h>
+#include <stdint.h>
 #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