/* unzip.c -- IO for uncompress .zip files using zlib
Version 1.1, February 14h, 2010
part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
Modifications of Unzip for Zip64
Copyright (C) 2007-2008 Even Rouault
Modifications for Zip64 support on both zip and unzip
Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
Modifications for QIODevice support and other QuaZIP fixes
Copyright (C) 2005-2014 Sergey A. Tachenov
For more info read MiniZip_info.txt
------------------------------------------------------------------------------------
Decryption code comes from crypt.c by Info-ZIP but has been greatly reduced in terms of
compatibility with older software. The following is from the original crypt.c.
Code woven in by Terry Thorsen 1/2003.
Copyright (c) 1990-2000 Info-ZIP. All rights reserved.
See the accompanying file LICENSE, version 2000-Apr-09 or later
(the contents of which are also included in zip.h) for terms of use.
If, for some reason, all these files are missing, the Info-ZIP license
also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
crypt.c (full version) by Info-ZIP. Last revised: [see crypt.h]
The encryption/decryption parts of this source code (as opposed to the
non-echoing password parts) were originally written in Europe. The
whole source package can be freely distributed, including from the USA.
(Prior to January 2000, re-export from the US was a violation of US law.)
This encryption code is a direct transcription of the algorithm from
Roger Schlafly, described by Phil Katz in the file appnote.txt. This
file (appnote.txt) is distributed with the PKZIP program (even in the
version without encryption capabilities).
------------------------------------------------------------------------------------
Changes in unzip.c
2007-2008 - Even Rouault - Addition of cpl_unzGetCurrentFileZStreamPos
2007-2008 - Even Rouault - Decoration of symbol names unz* -> cpl_unz*
2007-2008 - Even Rouault - Remove old C style function prototypes
2007-2008 - Even Rouault - Add unzip support for ZIP64
Copyright (C) 2007-2008 Even Rouault
Oct-2009 - Mathias Svensson - Removed cpl_* from symbol names (Even Rouault added them but since this is now moved to a new project (minizip64) I renamed them again).
Oct-2009 - Mathias Svensson - Fixed problem if uncompressed size was > 4G and compressed size was <4G
should only read the compressed/uncompressed size from the Zip64 format if
the size from normal header was 0xFFFFFFFF
Oct-2009 - Mathias Svensson - Applied some bug fixes from paches recived from Gilles Vollant
Oct-2009 - Mathias Svensson - Applied support to unzip files with compression mathod BZIP2 (bzip2 lib is required)
Patch created by Daniel Borca
Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
Copyright (C) 1998 - 2010 Gilles Vollant, Even Rouault, Mathias Svensson
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zlib.h"
#if (ZLIB_VERNUM < 0x1270)
typedef uLongf z_crc_t;
#endif
#include "unzip.h"
#ifdef STDC
# include <stddef.h>
# include <string.h>
# include <stdlib.h>
#endif
#ifdef NO_ERRNO_H
extern int errno;
#else
# include <errno.h>
#endif
#ifndef local
# define local static
#endif
/* compile with -Dlocal if your debugger can't find static symbols */
#ifndef CASESENSITIVITYDEFAULT_NO
# if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES)
# define CASESENSITIVITYDEFAULT_NO
# endif
#endif
#ifndef UNZ_BUFSIZE
#define UNZ_BUFSIZE (16384)
#endif
#ifndef UNZ_MAXFILENAMEINZIP
#define UNZ_MAXFILENAMEINZIP (256)
#endif
#ifndef ALLOC
# define ALLOC(size) (malloc(size))
#endif
#ifndef TRYFREE
# define TRYFREE(p) {if (p) free(p);}
#endif
#define SIZECENTRALDIRITEM (0x2e)
#define SIZEZIPLOCALHEADER (0x1e)
const char unz_copyright[] =
" unzip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
/* unz_file_info_interntal contain internal info about a file in zipfile*/
typedef struct unz_file_info64_internal_s
{
ZPOS64_T offset_curfile;/* relative offset of local header 8 bytes */
} unz_file_info64_internal;
/* file_in_zip_read_info_s contain internal information about a file in zipfile,
when reading and decompress it */
typedef struct
{
char *read_buffer; /* internal buffer for compressed data */
z_stream stream; /* zLib stream structure for inflate */
#ifdef HAVE_BZIP2
bz_stream bstream; /* bzLib stream structure for bziped */
#endif
ZPOS64_T pos_in_zipfile; /* position in byte on the zipfile, for fseek*/
uLong stream_initialised; /* flag set if stream structure is initialised*/
ZPOS64_T offset_local_extrafield;/* offset of the local extra field */
uInt size_local_extrafield;/* size of the local extra field */
ZPOS64_T pos_local_extrafield; /* position in the local extra field in read*/
ZPOS64_T total_out_64;
uLong crc32; /* crc32 of all data uncompressed */
uLong crc32_wait; /* crc32 we must obtain after decompress all */
ZPOS64_T rest_read_compressed; /* number of byte to be decompressed */
ZPOS64_T rest_read_uncompressed;/*number of byte to be obtained after decomp*/
zlib_filefunc64_32_def z_filefunc;
voidpf filestream; /* io structore of the zipfile */
uLong compression_method; /* compression method (0==store) */
ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
int raw;
} file_in_zip64_read_info_s;
/* unz64_s contain internal information about the zipfile
*/
typedef struct
{
zlib_filefunc64_32_def z_filefunc;
int is64bitOpenFunction;
voidpf filestream; /* io structore of the zipfile */
unz_global_info64 gi; /* public global information */
ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
ZPOS64_T num_file; /* number of the current file in the zipfile*/
ZPOS64_T pos_in_central_dir; /* pos of the current file in the central dir*/
ZPOS64_T current_file_ok; /* flag about the usability of the current file*/
ZPOS64_T central_pos; /* position of the beginning of the central dir*/
ZPOS64_T size_central_dir; /* size of the central directory */
ZPOS64_T offset_central_dir; /* offset of start of central directory with
respect to the starting disk number */
unz_file_info64 cur_file_info; /* public info about the current file in zip*/
unz_file_info64_internal cur_file_info_internal; /* private info about it*/
file_in_zip64_read_info_s* pfile_in_zip_read; /* structure about the current
file if we are decompressing it */
int encrypted;
int isZip64;
unsigned flags;
# ifndef NOUNCRYPT
unsigned long keys[3]; /* keys defining the pseudo-random sequence */
const z_crc_t FAR * pcrc_32_tab;
# endif
} unz64_s;
#ifndef NOUNCRYPT
#include "crypt.h"
#endif
/* ===========================================================================
Read a byte from a gz_stream; update next_in and avail_in. Return EOF
for end of file.
IN assertion: the stream s has been sucessfully opened for reading.
*/
local int unz64local_getByte OF((
const zlib_filefunc64_32_def* pzlib_filefunc_def