summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/file_pnm.c67
1 files changed, 30 insertions, 37 deletions
diff --git a/src/file_pnm.c b/src/file_pnm.c
index 797c245..066c37c 100644
--- a/src/file_pnm.c
+++ b/src/file_pnm.c
@@ -31,7 +31,6 @@
#include "filegen.h"
static void register_header_check_pnm(file_stat_t *file_stat);
-static int header_check_pnm(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new);
const file_hint_t file_hint_pnm= {
.extension="pnm",
@@ -43,46 +42,40 @@ const file_hint_t file_hint_pnm= {
.register_header_check=&register_header_check_pnm
};
-static const unsigned char p1_header[5]= { 'P', '1', '\n', '#', ' ' };
-static const unsigned char p2_header[5]= { 'P', '2', '\n', '#', ' ' };
-static const unsigned char p3_header[5]= { 'P', '3', '\n', '#', ' ' };
-static const unsigned char p4_header[5]= { 'P', '4', '\n', '#', ' ' };
-static const unsigned char p5_header[5]= { 'P', '5', '\n', '#', ' ' };
-static const unsigned char p6_header[5]= { 'P', '6', '\n', '#', ' ' };
+static int header_check_pbm(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
+{
+ if(!isprint(buffer[5]) || !isprint(buffer[6]) || !isprint(buffer[7]))
+ return 0;
+ reset_file_recovery(file_recovery_new);
+ file_recovery_new->extension="pbm";
+ return 1;
+}
-static void register_header_check_pnm(file_stat_t *file_stat)
+static int header_check_pgm(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
{
- register_header_check(0, p1_header, sizeof(p1_header), &header_check_pnm, file_stat);
- register_header_check(0, p2_header, sizeof(p2_header), &header_check_pnm, file_stat);
- register_header_check(0, p3_header, sizeof(p3_header), &header_check_pnm, file_stat);
- register_header_check(0, p4_header, sizeof(p4_header), &header_check_pnm, file_stat);
- register_header_check(0, p5_header, sizeof(p5_header), &header_check_pnm, file_stat);
- register_header_check(0, p6_header, sizeof(p6_header), &header_check_pnm, file_stat);
+ if(!isprint(buffer[5]) || !isprint(buffer[6]) || !isprint(buffer[7]))
+ return 0;
+ reset_file_recovery(file_recovery_new);
+ file_recovery_new->extension="pgm";
+ return 1;
}
-static int header_check_pnm(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
+static int header_check_ppm(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
+{
+ if(!isprint(buffer[5]) || !isprint(buffer[6]) || !isprint(buffer[7]))
+ return 0;
+ reset_file_recovery(file_recovery_new);
+ file_recovery_new->extension="ppm";
+ return 1;
+}
+
+static void register_header_check_pnm(file_stat_t *file_stat)
{
/* See http://en.wikipedia.org/wiki/Netpbm_format */
- if(memcmp(buffer, p1_header, sizeof(p1_header))==0 ||
- memcmp(buffer, p4_header, sizeof(p4_header))==0)
- {
- reset_file_recovery(file_recovery_new);
- file_recovery_new->extension="pbm";
- return 1;
- }
- if(memcmp(buffer, p2_header, sizeof(p2_header))==0 ||
- memcmp(buffer, p5_header, sizeof(p5_header))==0)
- {
- reset_file_recovery(file_recovery_new);
- file_recovery_new->extension="pgm";
- return 1;
- }
- if(memcmp(buffer, p3_header, sizeof(p3_header))==0 ||
- memcmp(buffer, p6_header, sizeof(p6_header))==0)
- {
- reset_file_recovery(file_recovery_new);
- file_recovery_new->extension="ppm";
- return 1;
- }
- return 0;
+ register_header_check(0, "P1\n# ", 5, &header_check_pbm, file_stat);
+ register_header_check(0, "P2\n# ", 5, &header_check_pgm, file_stat);
+ register_header_check(0, "P3\n# ", 5, &header_check_ppm, file_stat);
+ register_header_check(0, "P4\n# ", 5, &header_check_pbm, file_stat);
+ register_header_check(0, "P5\n# ", 5, &header_check_pgm, file_stat);
+ register_header_check(0, "P6\n# ", 5, &header_check_ppm, file_stat);
}