summaryrefslogtreecommitdiffstats
path: root/src/file_zip.c
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2015-04-11 14:23:16 +0200
committerChristophe Grenier <grenier@cgsecurity.org>2015-04-11 14:23:16 +0200
commit67054372873f2ed5414da7fff1cb5744148289d6 (patch)
treea8cd9501bcfc1e1efec8ca6603e7f11b0ad2ec0c /src/file_zip.c
parentefc94ee87ac3b4e3ecf4fb3ab4adac2ef0a6278f (diff)
add gcc_struct attribute to all __packed__ structure
do not use fseeko() with mingw32 gcc compiler
Diffstat (limited to 'src/file_zip.c')
-rw-r--r--src/file_zip.c99
1 files changed, 29 insertions, 70 deletions
diff --git a/src/file_zip.c b/src/file_zip.c
index 8fef36f..4ca2c77 100644
--- a/src/file_zip.c
+++ b/src/file_zip.c
@@ -41,6 +41,13 @@
#include "log.h"
/* #define DEBUG_ZIP */
+
+#if defined(HAVE_FSEEKO) && !defined(__MINGW32__)
+#define my_fseek fseeko
+#else
+#define my_fseek fseek
+#endif
+
extern const file_hint_t file_hint_doc;
static void register_header_check_zip(file_stat_t *file_stat);
static int header_check_zip(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);
@@ -92,8 +99,7 @@ struct zip_file_entry {
uint32_t uncompressed_size; /** Uncompressed size */
uint16_t filename_length; /** Filename length */
uint16_t extra_length; /** Extra fields length */
-} __attribute__ ((__packed__));
-typedef struct zip_file_entry zip_file_entry_t;
+} __attribute__ ((gcc_struct, __packed__));
struct zip64_extra_entry
{
@@ -103,7 +109,9 @@ struct zip64_extra_entry
uint64_t compressed_size;
uint64_t offset; /* Offset of local header record */
uint32_t disk_start_number; /* Number of the disk on which this file starts */
-} __attribute__ ((__packed__));
+} __attribute__ ((gcc_struct, __packed__));
+
+typedef struct zip_file_entry zip_file_entry_t;
typedef struct zip64_extra_entry zip64_extra_entry_t;
static uint32_t expected_compressed_size=0;
@@ -128,11 +136,7 @@ static int64_t file_get_pos(FILE *f, const void* needle, const unsigned int size
if (buffer[count]==*(const char *)needle && memcmp(buffer+count, needle, size)==0)
{
free(buffer);
-#ifdef HAVE_FSEEKO
- if(fseeko(f, (long)count-read_size, SEEK_CUR)<0)
-#else
- if(fseek(f, (long)count-read_size, SEEK_CUR)<0)
-#endif
+ if(my_fseek(f, (long)count-read_size, SEEK_CUR)<0)
return -1;
return total;
}
@@ -140,11 +144,7 @@ static int64_t file_get_pos(FILE *f, const void* needle, const unsigned int size
total++;
left--;
}
-#ifdef HAVE_FSEEKO
- if(feof(f) || fseeko(f, (long)1-size, SEEK_CUR)<0)
-#else
- if(feof(f) || fseek(f, (long)1-size, SEEK_CUR)<0)
-#endif
+ if(feof(f) || my_fseek(f, (long)1-size, SEEK_CUR)<0)
{
free(buffer);
return -1;
@@ -226,11 +226,7 @@ static int zip_parse_file_entry(file_recovery_t *fr, const char **ext, const uns
free(filename);
return -1;
}
-#ifdef HAVE_FSEEKO
- if (fseeko(fr->handle, -to_read, SEEK_CUR) < 0)
-#else
- if (fseek(fr->handle, -to_read, SEEK_CUR) < 0)
-#endif
+ if (my_fseek(fr->handle, -to_read, SEEK_CUR) < 0)
{
log_info("fseek failed\n");
free(filename);
@@ -319,13 +315,8 @@ static int zip_parse_file_entry(file_recovery_t *fr, const char **ext, const uns
log_trace("zip: Unexpected EOF in file_entry header: %lu bytes expected\n", len);
#endif
}
-#ifdef HAVE_FSEEKO
- if (fseeko(fr->handle, fr->file_size, SEEK_SET) == -1 ||
- fseeko(fr->handle, len, SEEK_CUR) == -1)
-#else
- if (fseek(fr->handle, fr->file_size, SEEK_SET) == -1 ||
- fseek(fr->handle, len, SEEK_CUR) == -1)
-#endif
+ if (my_fseek(fr->handle, fr->file_size, SEEK_SET) == -1 ||
+ my_fseek(fr->handle, len, SEEK_CUR) == -1)
{
#ifdef DEBUG_ZIP
log_trace("zip: Unexpected EOF in file_entry header: %lu bytes expected\n", len);
@@ -343,11 +334,7 @@ static int zip_parse_file_entry(file_recovery_t *fr, const char **ext, const uns
len=krita;
if (len>0)
{
-#ifdef HAVE_FSEEKO
- if (fseeko(fr->handle, len, SEEK_CUR) == -1)
-#else
- if (fseek(fr->handle, len, SEEK_CUR) == -1)
-#endif
+ if (my_fseek(fr->handle, len, SEEK_CUR) == -1)
{
#ifdef DEBUG_ZIP
log_trace("zip: Unexpected EOF in file_entry data: %lu bytes expected\n", len);
@@ -396,12 +383,8 @@ static int zip_parse_central_dir(file_recovery_t *fr)
uint16_t internal_attr; /** Internal file attributes */
uint32_t external_attr; /** External file attributes */
uint32_t offset_header; /** Relative offset of local header */
- } __attribute__ ((__packed__)) dir;
-#ifdef HAVE_FSEEKO
- if (fseeko(fr->handle, 2, SEEK_CUR) == -1)
-#else
- if (fseek(fr->handle, 2, SEEK_CUR) == -1)
-#endif
+ } __attribute__ ((gcc_struct, __packed__)) dir;
+ if (my_fseek(fr->handle, 2, SEEK_CUR) == -1)
{
#ifdef DEBUG_ZIP
log_trace("Unexpected EOF skipping version from central_dir\n");
@@ -433,11 +416,7 @@ static int zip_parse_central_dir(file_recovery_t *fr)
/* Rest of the block - could attempt CRC check */
len = le16(file.extra_length) + le16(dir.comment_length) + le16(file.filename_length);
-#ifdef HAVE_FSEEKO
- if (fseeko(fr->handle, len, SEEK_CUR) == -1)
-#else
- if (fseek(fr->handle, len, SEEK_CUR) == -1)
-#endif
+ if (my_fseek(fr->handle, len, SEEK_CUR) == -1)
{
#ifdef DEBUG_ZIP
log_trace("zip: Unexpected EOF in central_dir: %u bytes expected\n", len);
@@ -463,7 +442,7 @@ static int zip64_parse_end_central_dir(file_recovery_t *fr)
uint64_t number_entries2; /** Total number of entries in the central directory */
uint64_t size; /** Size of the central directory */
uint64_t offset; /** Offset of start of central directory */
- } __attribute__ ((__packed__)) dir;
+ } __attribute__ ((gcc_struct, __packed__)) dir;
if (fread(&dir, sizeof(dir), 1, fr->handle) != 1)
{
@@ -477,11 +456,7 @@ static int zip64_parse_end_central_dir(file_recovery_t *fr)
if (dir.end_size > 0)
{
const uint64_t len = le64(dir.end_size);
-#ifdef HAVE_FSEEKO
- if (fseeko(fr->handle, len, SEEK_CUR) == -1)
-#else
- if (fseek(fr->handle, len, SEEK_CUR) == -1)
-#endif
+ if (my_fseek(fr->handle, len, SEEK_CUR) == -1)
{
#ifdef DEBUG_ZIP
log_trace("zip: Unexpected EOF in end_central_dir_64: expected %llu bytes\n", (long long unsigned)len);
@@ -507,7 +482,7 @@ static int zip_parse_end_central_dir(file_recovery_t *fr)
uint32_t size; /** Size of the central directory */
uint32_t offset; /** Offset of start of central directory */
uint16_t comment_length; /** Comment length */
- } __attribute__ ((__packed__)) dir;
+ } __attribute__ ((gcc_struct, __packed__)) dir;
if (fread(&dir, sizeof(dir), 1, fr->handle) != 1)
{
@@ -521,11 +496,7 @@ static int zip_parse_end_central_dir(file_recovery_t *fr)
if (dir.comment_length)
{
const uint16_t len = le16(dir.comment_length);
-#ifdef HAVE_FSEEKO
- if (fseeko(fr->handle, len, SEEK_CUR) == -1)
-#else
- if (fseek(fr->handle, len, SEEK_CUR) == -1)
-#endif
+ if (my_fseek(fr->handle, len, SEEK_CUR) == -1)
{
#ifdef DEBUG_ZIP
log_trace("zip: Unexpected EOF in end_central_dir: expected %u bytes\n", len);
@@ -546,7 +517,7 @@ static int zip_parse_data_desc(file_recovery_t *fr)
uint32_t crc32; /** Checksum (CRC32) */
uint32_t compressed_size; /** Compressed size (bytes) */
uint32_t uncompressed_size; /** Uncompressed size (bytes) */
- } __attribute__ ((__packed__)) desc;
+ } __attribute__ ((gcc_struct, __packed__)) desc;
if (fread(&desc, sizeof(desc), 1, fr->handle) != 1)
{
@@ -583,11 +554,7 @@ static int zip_parse_signature(file_recovery_t *fr)
if (len)
{
len = le16(len);
-#ifdef HAVE_FSEEKO
- if (fseeko(fr->handle, len, SEEK_CUR) == -1)
-#else
- if (fseek(fr->handle, len, SEEK_CUR) == -1)
-#endif
+ if (my_fseek(fr->handle, len, SEEK_CUR) == -1)
{
#ifdef DEBUG_ZIP
log_trace("zip: Unexpected EOF in zip_parse_signature: expected %u bytes\n", len);
@@ -606,7 +573,7 @@ static int zip64_parse_end_central_dir_locator(file_recovery_t *fr)
uint32_t disk_number; /** Number of the disk with the start of the zip64 end of central directory */
uint64_t relative_offset; /** Relative offset of the zip64 end of central directory record */
uint32_t disk_total_number; /** Total number of disks */
- } __attribute__ ((__packed__)) loc;
+ } __attribute__ ((gcc_struct, __packed__)) loc;
if (fread(&loc, sizeof(loc), 1, fr->handle) != 1)
{
@@ -627,11 +594,7 @@ static void file_check_zip(file_recovery_t *fr)
fr->offset_error=0;
fr->offset_ok=0;
first_filename[0]='\0';
-#ifdef HAVE_FSEEKO
- if(fseeko(fr->handle, 0, SEEK_SET) < 0)
-#else
- if(fseek(fr->handle, 0, SEEK_SET) < 0)
-#endif
+ if(my_fseek(fr->handle, 0, SEEK_SET) < 0)
return ;
while (1)
{
@@ -717,11 +680,7 @@ static void file_rename_zip(const char *old_filename)
fr.file_size = 0;
fr.offset_error=0;
first_filename[0]='\0';
-#ifdef HAVE_FSEEKO
- if(fseeko(fr.handle, 0, SEEK_SET) < 0)
-#else
- if(fseek(fr.handle, 0, SEEK_SET) < 0)
-#endif
+ if(my_fseek(fr.handle, 0, SEEK_SET) < 0)
{
fclose(fr.handle);
return ;