From ffcd1d4b197a33c7d2d9f500451a2a40b52c23e9 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 30 Jul 2015 14:48:37 +0200 Subject: Use new fz_read_int functions. --- source/fitz/unzip.c | 143 +++++++++++++++++++++++----------------------------- 1 file changed, 63 insertions(+), 80 deletions(-) (limited to 'source/fitz/unzip.c') diff --git a/source/fitz/unzip.c b/source/fitz/unzip.c index ebbca66a..8c7c1f61 100644 --- a/source/fitz/unzip.c +++ b/source/fitz/unzip.c @@ -27,29 +27,6 @@ struct fz_archive_s struct zip_entry *table; }; -static inline int getshort(fz_context *ctx, fz_stream *file) -{ - int a = fz_read_byte(ctx, file); - int b = fz_read_byte(ctx, file); - return a | b << 8; -} - -static inline int getlong(fz_context *ctx, fz_stream *file) -{ - int a = fz_read_byte(ctx, file); - int b = fz_read_byte(ctx, file); - int c = fz_read_byte(ctx, file); - int d = fz_read_byte(ctx, file); - return a | b << 8 | c << 16 | d << 24; -} - -static inline int getlong64(fz_context *ctx, fz_stream *file) -{ - int a = getlong(ctx, file); - int b = getlong(ctx, file); - return b != 0 ? -1 : a; -} - static inline int zip_isdigit(int c) { return c >= '0' && c <= '9'; @@ -108,55 +85,61 @@ static void read_zip_dir_imp(fz_context *ctx, fz_archive *zip, int start_offset) fz_seek(ctx, file, start_offset, 0); - sig = getlong(ctx, file); + sig = fz_read_int32_le(ctx, file); if (sig != ZIP_END_OF_CENTRAL_DIRECTORY_SIG) fz_throw(ctx, FZ_ERROR_GENERIC, "wrong zip end of central directory signature (0x%x)", sig); - (void) getshort(ctx, file); /* this disk */ - (void) getshort(ctx, file); /* start disk */ - (void) getshort(ctx, file); /* entries in this disk */ - count = getshort(ctx, file); /* entries in central directory disk */ - (void) getlong(ctx, file); /* size of central directory */ - offset = getlong(ctx, file); /* offset to central directory */ + (void) fz_read_int16_le(ctx, file); /* this disk */ + (void) fz_read_int16_le(ctx, file); /* start disk */ + (void) fz_read_int16_le(ctx, file); /* entries in this disk */ + count = fz_read_int16_le(ctx, file); /* entries in central directory disk */ + (void) fz_read_int32_le(ctx, file); /* size of central directory */ + offset = fz_read_int32_le(ctx, file); /* offset to central directory */ /* ZIP64 */ if (count == 0xFFFF || offset == 0xFFFFFFFF) { - int offset64, count64; + int64_t offset64, count64; fz_seek(ctx, file, start_offset - 20, 0); - sig = getlong(ctx, file); + sig = fz_read_int32_le(ctx, file); if (sig != ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR_SIG) fz_throw(ctx, FZ_ERROR_GENERIC, "wrong zip64 end of central directory locator signature (0x%x)", sig); - (void) getlong(ctx, file); /* start disk */ - offset64 = getlong64(ctx, file); /* offset to end of central directory record */ - if (offset64 < 0) + (void) fz_read_int32_le(ctx, file); /* start disk */ + offset64 = fz_read_int64_le(ctx, file); /* offset to end of central directory record */ + if (offset64 > INT32_MAX) fz_throw(ctx, FZ_ERROR_GENERIC, "zip64 files larger than 2 GB aren't supported"); fz_seek(ctx, file, offset64, 0); - sig = getlong(ctx, file); + sig = fz_read_int32_le(ctx, file); if (sig != ZIP64_END_OF_CENTRAL_DIRECTORY_SIG) fz_throw(ctx, FZ_ERROR_GENERIC, "wrong zip64 end of central directory signature (0x%x)", sig); - (void) getlong64(ctx, file); /* size of record */ - (void) getshort(ctx, file); /* version made by */ - (void) getshort(ctx, file); /* version to extract */ - (void) getlong(ctx, file); /* disk number */ - (void) getlong(ctx, file); /* disk number start */ - count64 = getlong64(ctx, file); /* entries in central directory disk */ - (void) getlong64(ctx, file); /* entries in central directory */ - (void) getlong64(ctx, file); /* size of central directory */ - offset64 = getlong64(ctx, file); /* offset to central directory */ + (void) fz_read_int64_le(ctx, file); /* size of record */ + (void) fz_read_int16_le(ctx, file); /* version made by */ + (void) fz_read_int16_le(ctx, file); /* version to extract */ + (void) fz_read_int32_le(ctx, file); /* disk number */ + (void) fz_read_int32_le(ctx, file); /* disk number start */ + count64 = fz_read_int64_le(ctx, file); /* entries in central directory disk */ + (void) fz_read_int64_le(ctx, file); /* entries in central directory */ + (void) fz_read_int64_le(ctx, file); /* size of central directory */ + offset64 = fz_read_int64_le(ctx, file); /* offset to central directory */ if (count == 0xFFFF) + { + if (count64 > INT32_MAX) + fz_throw(ctx, FZ_ERROR_GENERIC, "zip64 files larger than 2 GB aren't supported"); count = count64; + } if (offset == 0xFFFFFFFF) + { + if (offset64 > INT32_MAX) + fz_throw(ctx, FZ_ERROR_GENERIC, "zip64 files larger than 2 GB aren't supported"); offset = offset64; - if (count < 0 || offset < 0) - fz_throw(ctx, FZ_ERROR_GENERIC, "zip64 files larger than 2 GB aren't supported"); + } } zip->count = count; @@ -167,26 +150,26 @@ static void read_zip_dir_imp(fz_context *ctx, fz_archive *zip, int start_offset) for (i = 0; i < count; i++) { - sig = getlong(ctx, file); + sig = fz_read_int32_le(ctx, file); if (sig != ZIP_CENTRAL_DIRECTORY_SIG) fz_throw(ctx, FZ_ERROR_GENERIC, "wrong zip central directory signature (0x%x)", sig); - (void) getshort(ctx, file); /* version made by */ - (void) getshort(ctx, file); /* version to extract */ - (void) getshort(ctx, file); /* general */ - (void) getshort(ctx, file); /* method */ - (void) getshort(ctx, file); /* last mod file time */ - (void) getshort(ctx, file); /* last mod file date */ - (void) getlong(ctx, file); /* crc-32 */ - zip->table[i].csize = getlong(ctx, file); - zip->table[i].usize = getlong(ctx, file); - namesize = getshort(ctx, file); - metasize = getshort(ctx, file); - commentsize = getshort(ctx, file); - (void) getshort(ctx, file); /* disk number start */ - (void) getshort(ctx, file); /* int file atts */ - (void) getlong(ctx, file); /* ext file atts */ - zip->table[i].offset = getlong(ctx, file); + (void) fz_read_int16_le(ctx, file); /* version made by */ + (void) fz_read_int16_le(ctx, file); /* version to extract */ + (void) fz_read_int16_le(ctx, file); /* general */ + (void) fz_read_int16_le(ctx, file); /* method */ + (void) fz_read_int16_le(ctx, file); /* last mod file time */ + (void) fz_read_int16_le(ctx, file); /* last mod file date */ + (void) fz_read_int32_le(ctx, file); /* crc-32 */ + zip->table[i].csize = fz_read_int32_le(ctx, file); + zip->table[i].usize = fz_read_int32_le(ctx, file); + namesize = fz_read_int16_le(ctx, file); + metasize = fz_read_int16_le(ctx, file); + commentsize = fz_read_int16_le(ctx, file); + (void) fz_read_int16_le(ctx, file); /* disk number start */ + (void) fz_read_int16_le(ctx, file); /* int file atts */ + (void) fz_read_int32_le(ctx, file); /* ext file atts */ + zip->table[i].offset = fz_read_int32_le(ctx, file); zip->table[i].name = fz_malloc(ctx, namesize + 1); fz_read(ctx, file, (unsigned char*)zip->table[i].name, namesize); @@ -194,24 +177,24 @@ static void read_zip_dir_imp(fz_context *ctx, fz_archive *zip, int start_offset) while (metasize > 0) { - int type = getshort(ctx, file); - int size = getshort(ctx, file); + int type = fz_read_int16_le(ctx, file); + int size = fz_read_int16_le(ctx, file); if (type == ZIP64_EXTRA_FIELD_SIG) { int sizeleft = size; if (zip->table[i].usize == 0xFFFFFFFF && sizeleft >= 8) { - zip->table[i].usize = getlong64(ctx, file); + zip->table[i].usize = fz_read_int64_le(ctx, file); sizeleft -= 8; } if (zip->table[i].csize == 0xFFFFFFFF && sizeleft >= 8) { - zip->table[i].csize = getlong64(ctx, file); + zip->table[i].csize = fz_read_int64_le(ctx, file); sizeleft -= 8; } if (zip->table[i].offset == 0xFFFFFFFF && sizeleft >= 8) { - zip->table[i].offset = getlong64(ctx, file); + zip->table[i].offset = fz_read_int64_le(ctx, file); sizeleft -= 8; } fz_seek(ctx, file, sizeleft - size, 1); @@ -266,23 +249,23 @@ static int read_zip_entry_header(fz_context *ctx, fz_archive *zip, struct zip_en fz_seek(ctx, file, ent->offset, 0); - sig = getlong(ctx, file); + sig = fz_read_int32_le(ctx, file); if (sig != ZIP_LOCAL_FILE_SIG) fz_throw(ctx, FZ_ERROR_GENERIC, "wrong zip local file signature (0x%x)", sig); - (void) getshort(ctx, file); /* version */ - general = getshort(ctx, file); /* general */ + (void) fz_read_int16_le(ctx, file); /* version */ + general = fz_read_int16_le(ctx, file); /* general */ if (general & ZIP_ENCRYPTED_FLAG) fz_throw(ctx, FZ_ERROR_GENERIC, "zip content is encrypted"); - method = getshort(ctx, file); - (void) getshort(ctx, file); /* file time */ - (void) getshort(ctx, file); /* file date */ - (void) getlong(ctx, file); /* crc-32 */ - (void) getlong(ctx, file); /* csize */ - (void) getlong(ctx, file); /* usize */ - namelength = getshort(ctx, file); - extralength = getshort(ctx, file); + method = fz_read_int16_le(ctx, file); + (void) fz_read_int16_le(ctx, file); /* file time */ + (void) fz_read_int16_le(ctx, file); /* file date */ + (void) fz_read_int32_le(ctx, file); /* crc-32 */ + (void) fz_read_int32_le(ctx, file); /* csize */ + (void) fz_read_int32_le(ctx, file); /* usize */ + namelength = fz_read_int16_le(ctx, file); + extralength = fz_read_int16_le(ctx, file); fz_seek(ctx, file, namelength + extralength, 1); -- cgit v1.2.3