libextractor
1.11
About: GNU libextractor is a library used to extract meta-data from files of arbitrary type.
![]() ![]() |
API to access ZIP archives. More...
Go to the source code of this file.
Data Structures | |
struct | FileFuncDefs |
struct | GlobalInfo |
struct | UnzipFileInfoInternal |
struct | FileInZipReadInfo |
struct | EXTRACTOR_UnzipFile |
Macros | |
#define | CASESENSITIVITY (0) |
#define | MAXFILENAME (256) |
#define | UNZ_BUFSIZE (16384) |
#define | UNZ_MAXFILENAMEINZIP (256) |
#define | SIZECENTRALDIRITEM (0x2e) |
#define | SIZEZIPLOCALHEADER (0x1e) |
#define | ZREAD(filefunc, buf, size) |
#define | ZTELL(filefunc) ((*((filefunc).ztell_file))((filefunc).opaque)) |
#define | ZSEEK(filefunc, pos, mode) |
#define | CASESENSITIVITYDEFAULT_NO |
#define | CASESENSITIVITYDEFAULTVALUE 2 |
#define | BUFREADCOMMENT (0x400) |
Functions | |
static int | read_byte_from_ffd (const struct FileFuncDefs *ffd, int *pi) |
static int | read_short_from_ffd (const struct FileFuncDefs *ffd, uLong *pX) |
static int | read_long_from_ffd (const struct FileFuncDefs *ffd, uLong *pX) |
static int | EXTRACTOR_common_unzip_string_file_name_compare (const char *fileName1, const char *fileName2, int iCaseSensitivity) |
static uLong | locate_central_directory (const struct FileFuncDefs *ffd) |
static void | dos_date_to_tmu_date (uLong ulDosDate, struct EXTRACTOR_UnzipDateTimeInfo *ptm) |
static int | get_current_file_info (struct EXTRACTOR_UnzipFile *file, struct EXTRACTOR_UnzipFileInfo *pfile_info, struct UnzipFileInfoInternal *pfile_info_internal, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize) |
int | EXTRACTOR_common_unzip_go_to_first_file (struct EXTRACTOR_UnzipFile *file) |
static struct EXTRACTOR_UnzipFile * | unzip_open_using_ffd (struct FileFuncDefs *ffd) |
int | EXTRACTOR_common_unzip_close_current_file (struct EXTRACTOR_UnzipFile *file) |
int | EXTRACTOR_common_unzip_close (struct EXTRACTOR_UnzipFile *file) |
int | EXTRACTOR_common_unzip_get_global_comment (struct EXTRACTOR_UnzipFile *file, char *comment, size_t comment_len) |
int | EXTRACTOR_common_unzip_get_current_file_info (struct EXTRACTOR_UnzipFile *file, struct EXTRACTOR_UnzipFileInfo *pfile_info, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize) |
int | EXTRACTOR_common_unzip_go_to_next_file (struct EXTRACTOR_UnzipFile *file) |
int | EXTRACTOR_common_unzip_go_find_local_file (struct EXTRACTOR_UnzipFile *file, const char *szFileName, int iCaseSensitivity) |
ssize_t | EXTRACTOR_common_unzip_read_current_file (struct EXTRACTOR_UnzipFile *file, void *buf, size_t len) |
static int | parse_current_file_coherency_header (struct EXTRACTOR_UnzipFile *file, uInt *piSizeVar, uLong *poffset_local_extrafield, uInt *psize_local_extrafield) |
int | EXTRACTOR_common_unzip_open_current_file (struct EXTRACTOR_UnzipFile *file) |
static uLong | ec_read_file_func (voidpf opaque, void *buf, uLong size) |
static long | ec_tell_file_func (voidpf opaque) |
static long | ec_seek_file_func (voidpf opaque, uLong offset, int origin) |
struct EXTRACTOR_UnzipFile * | EXTRACTOR_common_unzip_open (struct EXTRACTOR_ExtractContext *ec) |
API to access ZIP archives.
This code is based in part on unzip 1.00 Copyright 1998-2003 Gilles Vollant http://www.winimage.com/zLibDll"
The filenames for each file in a zipfile are stored in two locations. There is one at the start of each entry, just before the compressed data, and another at the end in a 'central directory structure'.
In order to catch self-extracting executables, we scan backwards from the end of the file looking for the central directory structure. The previous version of this went forewards through the local headers, but that only works for plain vanilla zip's and I don't feel like writing a special case for each of the dozen self-extracting executable stubs.
This assumes that the zip file is considered to be non-corrupt/non-truncated. If it is truncated then it's not considered to be a zip and skipped.
ZIP format description from appnote.iz and appnote.txt (more or less):
(this is why you always need to put in the last floppy if you span disks)
0- 3 end of central dir signature 4 bytes (0x06054b50) P K ^E ^F 4- 5 number of this disk 2 bytes 6- 7 number of the disk with the start of the central directory 2 bytes 8- 9 total number of entries in the central dir on this disk 2 bytes 10-11 total number of entries in the central dir 2 bytes 12-15 size of the central directory 4 bytes 16-19 offset of start of central directory with respect to the starting disk number 4 bytes 20-21 zipfile comment length 2 bytes 22-?? zipfile comment (variable size) max length 65536 bytes
Definition in file unzip.c.
#define ZREAD | ( | filefunc, | |
buf, | |||
size | |||
) |
#define ZSEEK | ( | filefunc, | |
pos, | |||
mode | |||
) |
#define ZTELL | ( | filefunc | ) | ((*((filefunc).ztell_file))((filefunc).opaque)) |
|
static |
Translate date/time from Dos format to struct EXTRACTOR_UnzipDateTimeInfo
(readable more easilty)
ulDosDate | time in DOS format (input) |
ptm | where to write time in readable format |
Definition at line 516 of file unzip.c.
References EXTRACTOR_UnzipDateTimeInfo::tm_hour, EXTRACTOR_UnzipDateTimeInfo::tm_mday, EXTRACTOR_UnzipDateTimeInfo::tm_min, EXTRACTOR_UnzipDateTimeInfo::tm_mon, EXTRACTOR_UnzipDateTimeInfo::tm_sec, and EXTRACTOR_UnzipDateTimeInfo::tm_year.
Referenced by get_current_file_info().
|
static |
Callback to perform read operation using LE API. Note that partial reads are not allowed.
opaque | the 'struct EXTRACTOR_ExtractContext' |
buf | where to write bytes read |
size | number of bytes desired |
Definition at line 1353 of file unzip.c.
References EXTRACTOR_ExtractContext::cls, and EXTRACTOR_ExtractContext::read.
Referenced by EXTRACTOR_common_unzip_open().
|
static |
Callback to perform seek operation using LE API.
opaque | the 'struct EXTRACTOR_ExtractContext' |
offset | where to seek |
origin | relative to where should we seek |
Definition at line 1401 of file unzip.c.
References EXTRACTOR_ExtractContext::cls, EXTRACTOR_UNZIP_INTERNALERROR, EXTRACTOR_UNZIP_OK, and EXTRACTOR_ExtractContext::seek.
Referenced by EXTRACTOR_common_unzip_open().
|
static |
Callback to obtain current offset in file using LE API.
opaque | the 'struct EXTRACTOR_ExtractContext' |
Definition at line 1384 of file unzip.c.
References EXTRACTOR_ExtractContext::cls, and EXTRACTOR_ExtractContext::seek.
Referenced by EXTRACTOR_common_unzip_open().
int EXTRACTOR_common_unzip_close | ( | struct EXTRACTOR_UnzipFile * | file | ) |
Close a ZipFile.
file | zip file to close |
Definition at line 854 of file unzip.c.
References EXTRACTOR_common_unzip_close_current_file(), EXTRACTOR_UNZIP_OK, EXTRACTOR_UNZIP_PARAMERROR, NULL, and EXTRACTOR_UnzipFile::pfile_in_zip_read.
Referenced by EXTRACTOR_odf_extract_method(), and EXTRACTOR_zip_extract_method().
int EXTRACTOR_common_unzip_close_current_file | ( | struct EXTRACTOR_UnzipFile * | file | ) |
Close the file in zip opened with EXTRACTOR_common_unzip_open_current_file().
Definition at line 823 of file unzip.c.
References FileInZipReadInfo::crc32, FileInZipReadInfo::crc32_wait, EXTRACTOR_UNZIP_CRCERROR, EXTRACTOR_UNZIP_OK, EXTRACTOR_UNZIP_PARAMERROR, NULL, EXTRACTOR_UnzipFile::pfile_in_zip_read, FileInZipReadInfo::read_buffer, FileInZipReadInfo::rest_read_uncompressed, FileInZipReadInfo::stream, and FileInZipReadInfo::stream_initialised.
Referenced by EXTRACTOR_common_unzip_close(), EXTRACTOR_common_unzip_open_current_file(), EXTRACTOR_odf_extract_method(), and libextractor_oo_getmimetype().
int EXTRACTOR_common_unzip_get_current_file_info | ( | struct EXTRACTOR_UnzipFile * | file, |
struct EXTRACTOR_UnzipFileInfo * | pfile_info, | ||
char * | szFileName, | ||
uLong | fileNameBufferSize, | ||
void * | extraField, | ||
uLong | extraFieldBufferSize, | ||
char * | szComment, | ||
uLong | commentBufferSize | ||
) |
Write info about the ZipFile in the *pglobal_info structure. No preparation of the structure is needed.
file | zipfile to manipulate |
pfile_info | file information to initialize |
szFileName | where to write the name of the current file |
fileNameBufferSize | number of bytes available in szFileName |
extraField | where to write extra data |
extraFieldBufferSize | number of bytes available in extraField |
szComment | where to write the comment on the current file |
commentBufferSize | number of bytes available in szComment |
Definition at line 908 of file unzip.c.
References get_current_file_info(), and NULL.
Referenced by EXTRACTOR_common_unzip_go_find_local_file(), EXTRACTOR_odf_extract_method(), EXTRACTOR_zip_extract_method(), and libextractor_oo_getmimetype().
int EXTRACTOR_common_unzip_get_global_comment | ( | struct EXTRACTOR_UnzipFile * | file, |
char * | comment, | ||
size_t | comment_len | ||
) |
Obtain the global comment from a ZIP file.
file | unzip file to inspect |
comment | where to copy the comment |
comment_len | maximum number of bytes available in comment |
Definition at line 874 of file unzip.c.
References EXTRACTOR_UNZIP_ERRNO, EXTRACTOR_UNZIP_OK, EXTRACTOR_UNZIP_PARAMERROR, EXTRACTOR_UnzipFile::gi, NULL, GlobalInfo::offset_comment, GlobalInfo::size_comment, EXTRACTOR_UnzipFile::z_filefunc, ZREAD, and ZSEEK.
Referenced by EXTRACTOR_zip_extract_method().
int EXTRACTOR_common_unzip_go_find_local_file | ( | struct EXTRACTOR_UnzipFile * | file, |
const char * | szFileName, | ||
int | iCaseSensitivity | ||
) |
Try locate the file szFileName in the zipfile.
file | zipfile to manipulate |
szFileName | name to find |
iCaseSensitivity,use | 1 for case sensitivity (like strcmp); 2 for no case sensitivity (like strcmpi or strcasecmp); or 0 for default of your operating system (like 1 on Unix, 2 on Windows) |
Definition at line 969 of file unzip.c.
References EXTRACTOR_UnzipFile::cur_file_info, EXTRACTOR_UnzipFile::cur_file_info_internal, EXTRACTOR_UnzipFile::current_file_ok, EXTRACTOR_common_unzip_get_current_file_info(), EXTRACTOR_common_unzip_go_to_first_file(), EXTRACTOR_common_unzip_go_to_next_file(), EXTRACTOR_common_unzip_string_file_name_compare(), EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE, EXTRACTOR_UNZIP_OK, EXTRACTOR_UNZIP_PARAMERROR, NULL, EXTRACTOR_UnzipFile::num_file, EXTRACTOR_UnzipFile::pos_in_central_dir, and UNZ_MAXFILENAMEINZIP.
Referenced by EXTRACTOR_odf_extract_method(), EXTRACTOR_zip_extract_method(), and libextractor_oo_getmimetype().
int EXTRACTOR_common_unzip_go_to_first_file | ( | struct EXTRACTOR_UnzipFile * | file | ) |
Set the current file of the zipfile to the first file.
file | zipfile to manipulate |
Definition at line 707 of file unzip.c.
References EXTRACTOR_UnzipFile::cur_file_info, EXTRACTOR_UnzipFile::cur_file_info_internal, EXTRACTOR_UnzipFile::current_file_ok, EXTRACTOR_UNZIP_OK, EXTRACTOR_UNZIP_PARAMERROR, get_current_file_info(), NULL, EXTRACTOR_UnzipFile::num_file, EXTRACTOR_UnzipFile::offset_central_dir, and EXTRACTOR_UnzipFile::pos_in_central_dir.
Referenced by EXTRACTOR_common_unzip_go_find_local_file(), EXTRACTOR_zip_extract_method(), and unzip_open_using_ffd().
int EXTRACTOR_common_unzip_go_to_next_file | ( | struct EXTRACTOR_UnzipFile * | file | ) |
Set the current file of the zipfile to the next file.
file | zipfile to manipulate |
Definition at line 933 of file unzip.c.
References EXTRACTOR_UnzipFile::cur_file_info, EXTRACTOR_UnzipFile::cur_file_info_internal, EXTRACTOR_UnzipFile::current_file_ok, EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE, EXTRACTOR_UNZIP_OK, EXTRACTOR_UNZIP_PARAMERROR, get_current_file_info(), EXTRACTOR_UnzipFile::gi, NULL, EXTRACTOR_UnzipFile::num_file, GlobalInfo::number_entry, EXTRACTOR_UnzipFile::pos_in_central_dir, EXTRACTOR_UnzipFileInfo::size_file_comment, EXTRACTOR_UnzipFileInfo::size_file_extra, EXTRACTOR_UnzipFileInfo::size_filename, and SIZECENTRALDIRITEM.
Referenced by EXTRACTOR_common_unzip_go_find_local_file(), and EXTRACTOR_zip_extract_method().
struct EXTRACTOR_UnzipFile * EXTRACTOR_common_unzip_open | ( | struct EXTRACTOR_ExtractContext * | ec | ) |
Open a zip file for processing using the data access functions from the extract context.
ec | extract context to use |
Definition at line 1421 of file unzip.c.
References ec_read_file_func(), ec_seek_file_func(), ec_tell_file_func(), FileFuncDefs::opaque, unzip_open_using_ffd(), FileFuncDefs::zread_file, FileFuncDefs::zseek_file, and FileFuncDefs::ztell_file.
Referenced by EXTRACTOR_odf_extract_method(), and EXTRACTOR_zip_extract_method().
int EXTRACTOR_common_unzip_open_current_file | ( | struct EXTRACTOR_UnzipFile * | file | ) |
Open for reading data the current file in the zipfile.
file | zipfile to manipulate |
Definition at line 1259 of file unzip.c.
References FileInZipReadInfo::byte_before_the_zipfile, EXTRACTOR_UnzipFile::byte_before_the_zipfile, EXTRACTOR_UnzipFileInfo::compressed_size, FileInZipReadInfo::compression_method, EXTRACTOR_UnzipFileInfo::compression_method, EXTRACTOR_UnzipFileInfo::crc, FileInZipReadInfo::crc32, FileInZipReadInfo::crc32_wait, EXTRACTOR_UnzipFile::cur_file_info, EXTRACTOR_UnzipFile::cur_file_info_internal, EXTRACTOR_UnzipFile::current_file_ok, EXTRACTOR_common_unzip_close_current_file(), EXTRACTOR_UNZIP_BADZIPFILE, EXTRACTOR_UNZIP_INTERNALERROR, EXTRACTOR_UNZIP_OK, EXTRACTOR_UNZIP_PARAMERROR, NULL, UnzipFileInfoInternal::offset_curfile, FileInZipReadInfo::offset_local_extrafield, parse_current_file_coherency_header(), EXTRACTOR_UnzipFile::pfile_in_zip_read, FileInZipReadInfo::pos_in_zipfile, FileInZipReadInfo::pos_local_extrafield, FileInZipReadInfo::read_buffer, FileInZipReadInfo::rest_read_compressed, FileInZipReadInfo::rest_read_uncompressed, FileInZipReadInfo::size_local_extrafield, SIZEZIPLOCALHEADER, FileInZipReadInfo::stream, FileInZipReadInfo::stream_initialised, EXTRACTOR_UnzipFileInfo::uncompressed_size, UNZ_BUFSIZE, FileInZipReadInfo::z_filefunc, and EXTRACTOR_UnzipFile::z_filefunc.
Referenced by EXTRACTOR_odf_extract_method(), and libextractor_oo_getmimetype().
ssize_t EXTRACTOR_common_unzip_read_current_file | ( | struct EXTRACTOR_UnzipFile * | file, |
void * | buf, | ||
size_t | len | ||
) |
Read bytes from the current file (must have been opened).
buf | contain buffer where data must be copied |
len | the size of buf. |
Definition at line 1038 of file unzip.c.
References FileInZipReadInfo::byte_before_the_zipfile, FileInZipReadInfo::compression_method, FileInZipReadInfo::crc32, EXTRACTOR_UNZIP_END_OF_LIST_OF_FILE, EXTRACTOR_UNZIP_EOF, EXTRACTOR_UNZIP_ERRNO, EXTRACTOR_UNZIP_OK, EXTRACTOR_UNZIP_PARAMERROR, NULL, EXTRACTOR_UnzipFile::pfile_in_zip_read, FileInZipReadInfo::pos_in_zipfile, FileInZipReadInfo::read_buffer, FileInZipReadInfo::rest_read_compressed, FileInZipReadInfo::rest_read_uncompressed, FileInZipReadInfo::stream, UNZ_BUFSIZE, FileInZipReadInfo::z_filefunc, ZREAD, and ZSEEK.
Referenced by EXTRACTOR_odf_extract_method(), and libextractor_oo_getmimetype().
|
static |
Compare two filenames (fileName1, fileName2).
filename1 | name of first file |
filename2 | name of second file |
iCaseSensitivity,use | 1 for case sensitivity (like strcmp); 2 for no case sensitivity (like strcmpi or strcasecmp); or 0 for default of your operating system (like 1 on Unix, 2 on Windows) |
Definition at line 441 of file unzip.c.
References CASESENSITIVITYDEFAULTVALUE.
Referenced by EXTRACTOR_common_unzip_go_find_local_file().
|
static |
Write info about the ZipFile in the *pglobal_info structure. No preparation of the structure is needed.
file | zipfile to manipulate |
pfile_info | file information to initialize |
pfile_info_internal | internal file information to initialize |
szFileName | where to write the name of the current file |
fileNameBufferSize | number of bytes available in szFileName |
extraField | where to write extra data |
extraFieldBufferSize | number of bytes available in extraField |
szComment | where to write the comment on the current file |
commentBufferSize | number of bytes available in szComment |
Definition at line 547 of file unzip.c.
References EXTRACTOR_UnzipFile::byte_before_the_zipfile, EXTRACTOR_UnzipFileInfo::compressed_size, EXTRACTOR_UnzipFileInfo::compression_method, EXTRACTOR_UnzipFileInfo::crc, EXTRACTOR_UnzipFileInfo::disk_num_start, dos_date_to_tmu_date(), EXTRACTOR_UnzipFileInfo::dosDate, EXTRACTOR_UnzipFileInfo::external_fa, EXTRACTOR_UNZIP_BADZIPFILE, EXTRACTOR_UNZIP_ERRNO, EXTRACTOR_UNZIP_OK, EXTRACTOR_UNZIP_PARAMERROR, EXTRACTOR_UnzipFileInfo::flag, EXTRACTOR_UnzipFileInfo::internal_fa, NULL, UnzipFileInfoInternal::offset_curfile, EXTRACTOR_UnzipFile::pos_in_central_dir, read_long_from_ffd(), read_short_from_ffd(), EXTRACTOR_UnzipFileInfo::size_file_comment, EXTRACTOR_UnzipFileInfo::size_file_extra, EXTRACTOR_UnzipFileInfo::size_filename, EXTRACTOR_UnzipFileInfo::tmu_date, EXTRACTOR_UnzipFileInfo::uncompressed_size, EXTRACTOR_UnzipFileInfo::version, EXTRACTOR_UnzipFileInfo::version_needed, EXTRACTOR_UnzipFile::z_filefunc, ZREAD, and ZSEEK.
Referenced by EXTRACTOR_common_unzip_get_current_file_info(), EXTRACTOR_common_unzip_go_to_first_file(), and EXTRACTOR_common_unzip_go_to_next_file().
|
static |
Locate the central directory in the ZIP file.
ffd | IO functions |
Definition at line 465 of file unzip.c.
References BUFREADCOMMENT, ZREAD, ZSEEK, and ZTELL.
Referenced by unzip_open_using_ffd().
|
static |
Read the local header of the current zipfile. Check the coherency of the local header and info in the end of central directory about this file. Store in *piSizeVar the size of extra info in local header (filename and size of extra field data)
file | zipfile to process |
piSizeVar | where to store the size of the extra info |
poffset_local_extrafield | where to store the offset of the local extrafield |
psoze_local_extrafield | where to store the size of the local extrafield |
Definition at line 1173 of file unzip.c.
References EXTRACTOR_UnzipFile::byte_before_the_zipfile, EXTRACTOR_UnzipFileInfo::compressed_size, EXTRACTOR_UnzipFileInfo::compression_method, EXTRACTOR_UnzipFileInfo::crc, EXTRACTOR_UnzipFile::cur_file_info, EXTRACTOR_UnzipFile::cur_file_info_internal, EXTRACTOR_UNZIP_BADZIPFILE, EXTRACTOR_UNZIP_ERRNO, EXTRACTOR_UNZIP_OK, UnzipFileInfoInternal::offset_curfile, read_long_from_ffd(), read_short_from_ffd(), EXTRACTOR_UnzipFileInfo::size_filename, SIZEZIPLOCALHEADER, EXTRACTOR_UnzipFileInfo::uncompressed_size, EXTRACTOR_UnzipFile::z_filefunc, and ZSEEK.
Referenced by EXTRACTOR_common_unzip_open_current_file().
|
static |
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 successfully opened for reading.
ffd | functions for performing IO operations |
pi | where to store the byte that was read |
Definition at line 341 of file unzip.c.
References EXTRACTOR_UNZIP_EOF, EXTRACTOR_UNZIP_OK, and ZREAD.
Referenced by read_long_from_ffd(), and read_short_from_ffd().
|
static |
Read a 'long' (4 bytes) from a gz_stream; update next_in and avail_in. Return EOF for end of file. IN assertion: the stream s has been successfully opened for reading.
ffd | functions for performing IO operations |
pi | where to store the long that was read |
Definition at line 392 of file unzip.c.
References EXTRACTOR_UNZIP_OK, and read_byte_from_ffd().
Referenced by get_current_file_info(), parse_current_file_coherency_header(), and unzip_open_using_ffd().
|
static |
Read a short (2 bytes) from a gz_stream; update next_in and avail_in. Return EOF for end of file. IN assertion: the stream s has been successfully opened for reading.
ffd | functions for performing IO operations |
pi | where to store the short that was read |
Definition at line 363 of file unzip.c.
References EXTRACTOR_UNZIP_OK, and read_byte_from_ffd().
Referenced by get_current_file_info(), parse_current_file_coherency_header(), and unzip_open_using_ffd().
|
static |
Open a Zip file.
ffd | IO functions |
Definition at line 731 of file unzip.c.
References EXTRACTOR_UnzipFile::byte_before_the_zipfile, EXTRACTOR_UnzipFile::central_pos, EXTRACTOR_UnzipFile::encrypted, EXTRACTOR_common_unzip_go_to_first_file(), EXTRACTOR_UNZIP_OK, EXTRACTOR_UnzipFile::gi, locate_central_directory(), NULL, GlobalInfo::number_entry, EXTRACTOR_UnzipFile::offset_central_dir, GlobalInfo::offset_comment, EXTRACTOR_UnzipFile::pfile_in_zip_read, read_long_from_ffd(), read_short_from_ffd(), EXTRACTOR_UnzipFile::size_central_dir, GlobalInfo::size_comment, EXTRACTOR_UnzipFile::z_filefunc, ZSEEK, and ZTELL.
Referenced by EXTRACTOR_common_unzip_open().