summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2016-09-30 16:08:34 +0200
committerChristophe Grenier <grenier@cgsecurity.org>2016-09-30 16:08:34 +0200
commit1b8ec405730376ace620cc39fe534006cf648b55 (patch)
tree9d5713e2280734aa74522a703dd79a1a8381ac19
parent05f8db763826fdd8771a82a34600704ba7390801 (diff)
header_ignored_adv(): generalized the speed optimization created for zip file
in commit b947e58fa0a049692202e04d14992d5d829d5356
-rw-r--r--src/file_zip.c20
-rw-r--r--src/filegen.c42
-rw-r--r--src/filegen.h1
3 files changed, 46 insertions, 17 deletions
diff --git a/src/file_zip.c b/src/file_zip.c
index 539ec2b..d449d11 100644
--- a/src/file_zip.c
+++ b/src/file_zip.c
@@ -784,8 +784,8 @@ static int header_check_zip(const unsigned char *buffer, const unsigned int buff
if(file_recovery->file_stat!=NULL &&
file_recovery->file_stat->file_hint==&file_hint_doc)
{
- header_ignored(file_recovery_new);
- return 0;
+ if(header_ignored_adv(file_recovery, file_recovery_new)==0)
+ return 0;
}
/* A zip file begins by ZIP_FILE_ENTRY, this signature can also be
* found for each compressed file */
@@ -793,22 +793,8 @@ static int header_check_zip(const unsigned char *buffer, const unsigned int buff
file_recovery->file_stat->file_hint==&file_hint_zip &&
safe_header_only==0)
{
- file_recovery_t fr_test;
- off_t offset;
- memcpy(&fr_test, file_recovery, sizeof(fr_test));
-#ifdef HAVE_FTELLO
- if((offset=ftello(file_recovery->handle)) == -1)
- offset=ftell(file_recovery->handle);
-#else
- offset=ftell(file_recovery->handle);
-#endif
- file_check_zip(&fr_test);
- fseek(file_recovery->handle, offset, SEEK_SET);
- if(fr_test.file_size==0)
- {
- header_ignored(file_recovery_new);
+ if(header_ignored_adv(file_recovery, file_recovery_new)==0)
return 0;
- }
}
reset_file_recovery(file_recovery_new);
file_recovery_new->min_filesize=21;
diff --git a/src/filegen.c b/src/filegen.c
index ec0b5c4..9895114 100644
--- a/src/filegen.c
+++ b/src/filegen.c
@@ -31,6 +31,7 @@
#endif
#include <stdio.h>
#include <ctype.h>
+#include <assert.h>
#include "types.h"
#include "common.h"
#include "filegen.h"
@@ -592,6 +593,47 @@ int file_rename_unicode(file_recovery_t *file_recovery, const void *buffer, cons
static uint64_t offset_skipped_header=0;
+/* 0: file_recovery is bad *
+ * 1: file_recovery is ok */
+int header_ignored_adv(const file_recovery_t *file_recovery, const file_recovery_t *file_recovery_new)
+{
+ file_recovery_t fr_test;
+ off_t offset;
+ assert(file_recovery!=NULL);
+ assert(file_recovery_new!=NULL);
+ if(file_recovery->file_check==NULL)
+ {
+ log_warning("header_ignored_adv: file_check==NULL\n");
+ return 1;
+ }
+ if(file_recovery->handle==NULL)
+ {
+ if(file_recovery_new->location.start==0 || offset_skipped_header==0)
+ offset_skipped_header=file_recovery_new->location.start;
+ return 0;
+ }
+
+ memcpy(&fr_test, file_recovery, sizeof(fr_test));
+#ifdef HAVE_FTELLO
+ if((offset=ftello(file_recovery->handle)) < 0)
+ offset=ftell(file_recovery->handle);
+#else
+ offset=ftell(file_recovery->handle);
+#endif
+ assert(offset >= 0);
+ file_recovery->file_check(&fr_test);
+ if(fr_test.file_size>0)
+ return 1;
+ if(my_fseek(file_recovery->handle, offset, SEEK_SET) < 0)
+ {
+ log_error("BUG in header_ignored_adv: my_fseek() failed\n");
+ return 1;
+ }
+ if(file_recovery_new->location.start==0 || offset_skipped_header==0)
+ offset_skipped_header=file_recovery_new->location.start;
+ return 0;
+}
+
void header_ignored(const file_recovery_t *file_recovery_new)
{
if(file_recovery_new==NULL)
diff --git a/src/filegen.h b/src/filegen.h
index ed12995..7d220e1 100644
--- a/src/filegen.h
+++ b/src/filegen.h
@@ -136,6 +136,7 @@ file_stat_t * init_file_stats(file_enable_t *files_enable);
int file_rename(file_recovery_t *file_recovery, const void *buffer, const int buffer_size, const int offset, const char *new_ext, const int force_ext);
int file_rename_unicode(file_recovery_t *file_recovery, const void *buffer, const int buffer_size, const int offset, const char *new_ext, const int force_ext);
void header_ignored(const file_recovery_t *file_recovery_new);
+int header_ignored_adv(const file_recovery_t *file_recovery, const file_recovery_t *file_recovery_new);
int my_fseek(FILE *stream, off_t offset, int whence);
#ifdef __cplusplus