mirror of
https://github.com/QuasarApp/zip.git
synced 2025-05-04 16:19:33 +00:00
formatting
This commit is contained in:
parent
49656d5c10
commit
8cac9e042f
@ -1,5 +1,5 @@
|
||||
project(zip)
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
project(zip)
|
||||
|
||||
set(SRC src/miniz.h src/zip.h src/zip.c)
|
||||
|
||||
|
187
src/zip.c
187
src/zip.c
@ -7,16 +7,19 @@
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "miniz.h"
|
||||
#include "zip.h"
|
||||
#include "miniz.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
|
||||
/* Win32, OS/2, DOS */
|
||||
#define HAS_DEVICE(P) ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) && (P)[1] == ':')
|
||||
#define FILESYSTEM_PREFIX_LEN(P) (HAS_DEVICE (P) ? 2 : 0)
|
||||
#define HAS_DEVICE(P) \
|
||||
((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) && \
|
||||
(P)[1] == ':')
|
||||
#define FILESYSTEM_PREFIX_LEN(P) (HAS_DEVICE(P) ? 2 : 0)
|
||||
#define ISSLASH(C) ((C) == '/' || (C) == '\\')
|
||||
#endif
|
||||
|
||||
@ -28,12 +31,18 @@
|
||||
#define ISSLASH(C) ((C) == '/')
|
||||
#endif
|
||||
|
||||
#define cleanup(ptr) do { if (ptr) { free((void *)ptr); ptr = NULL; } } while (0)
|
||||
#define strclone(ptr) ((ptr) ? strdup(ptr) : NULL)
|
||||
#define cleanup(ptr) \
|
||||
do { \
|
||||
if (ptr) { \
|
||||
free((void *)ptr); \
|
||||
ptr = NULL; \
|
||||
} \
|
||||
} while (0)
|
||||
#define strclone(ptr) ((ptr) ? strdup(ptr) : NULL)
|
||||
|
||||
static char *basename(const char *name) {
|
||||
char const *p;
|
||||
char const *base = name += FILESYSTEM_PREFIX_LEN (name);
|
||||
char const *base = name += FILESYSTEM_PREFIX_LEN(name);
|
||||
int all_slashes = 1;
|
||||
|
||||
for (p = name; *p; p++) {
|
||||
@ -44,8 +53,7 @@ static char *basename(const char *name) {
|
||||
}
|
||||
|
||||
/* If NAME is all slashes, arrange to return `/'. */
|
||||
if (*base == '\0' && ISSLASH(*name) && all_slashes)
|
||||
--base;
|
||||
if (*base == '\0' && ISSLASH(*name) && all_slashes) --base;
|
||||
|
||||
return (char *)base;
|
||||
}
|
||||
@ -53,13 +61,13 @@ static char *basename(const char *name) {
|
||||
static int mkpath(const char *path) {
|
||||
const int mode = 0755;
|
||||
char const *p;
|
||||
char npath[MAX_PATH + 1] = { 0 };
|
||||
char npath[MAX_PATH + 1] = {0};
|
||||
int len = 0;
|
||||
|
||||
for (p = path; *p && len < MAX_PATH; p++) {
|
||||
if (ISSLASH(*p) && len > 0) {
|
||||
if (mkdir(npath, mode) == -1)
|
||||
if (errno != EEXIST) return -1;
|
||||
if (errno != EEXIST) return -1;
|
||||
}
|
||||
npath[len++] = *p;
|
||||
}
|
||||
@ -68,27 +76,27 @@ static int mkpath(const char *path) {
|
||||
}
|
||||
|
||||
struct zip_entry_t {
|
||||
const char *name;
|
||||
mz_uint64 uncomp_size;
|
||||
mz_uint64 comp_size;
|
||||
mz_uint32 uncomp_crc32;
|
||||
mz_uint64 offset;
|
||||
mz_uint8 header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
|
||||
mz_uint64 header_offset;
|
||||
mz_uint16 method;
|
||||
const char *name;
|
||||
mz_uint64 uncomp_size;
|
||||
mz_uint64 comp_size;
|
||||
mz_uint32 uncomp_crc32;
|
||||
mz_uint64 offset;
|
||||
mz_uint8 header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
|
||||
mz_uint64 header_offset;
|
||||
mz_uint16 method;
|
||||
mz_zip_writer_add_state state;
|
||||
tdefl_compressor comp;
|
||||
tdefl_compressor comp;
|
||||
};
|
||||
|
||||
struct zip_t {
|
||||
mz_zip_archive archive;
|
||||
mz_uint level;
|
||||
struct zip_entry_t entry;
|
||||
mz_zip_archive archive;
|
||||
mz_uint level;
|
||||
struct zip_entry_t entry;
|
||||
};
|
||||
|
||||
struct zip_t *zip_open(const char *zipname, int level, int append) {
|
||||
struct zip_t *zip = NULL;
|
||||
struct MZ_FILE_STAT_STRUCT fstat;
|
||||
struct zip_t *zip = NULL;
|
||||
struct MZ_FILE_STAT_STRUCT fstat;
|
||||
|
||||
if (!zipname || strlen(zipname) < 1) {
|
||||
// zip_t archive name is empty or NULL
|
||||
@ -107,7 +115,9 @@ struct zip_t *zip_open(const char *zipname, int level, int append) {
|
||||
|
||||
if (append && MZ_FILE_STAT(zipname, &fstat) == 0) {
|
||||
// Append to an existing archive.
|
||||
if (!mz_zip_reader_init_file(&(zip->archive), zipname, level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) {
|
||||
if (!mz_zip_reader_init_file(
|
||||
&(zip->archive), zipname,
|
||||
level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) {
|
||||
cleanup(zip);
|
||||
return NULL;
|
||||
}
|
||||
@ -132,7 +142,8 @@ struct zip_t *zip_open(const char *zipname, int level, int append) {
|
||||
|
||||
void zip_close(struct zip_t *zip) {
|
||||
if (zip) {
|
||||
// Always finalize, even if adding failed for some reason, so we have a valid central directory.
|
||||
// Always finalize, even if adding failed for some reason, so we have a
|
||||
// valid central directory.
|
||||
mz_zip_writer_finalize_archive(&(zip->archive));
|
||||
mz_zip_writer_end(&(zip->archive));
|
||||
|
||||
@ -142,10 +153,10 @@ void zip_close(struct zip_t *zip) {
|
||||
|
||||
int zip_entry_open(struct zip_t *zip, const char *entryname) {
|
||||
size_t entrylen = 0;
|
||||
mz_zip_archive *pzip = NULL;
|
||||
mz_uint num_alignment_padding_bytes, level;
|
||||
mz_zip_archive *pzip = NULL;
|
||||
mz_uint num_alignment_padding_bytes, level;
|
||||
|
||||
if (!zip || !entryname) {
|
||||
if (!zip || !entryname) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -165,11 +176,13 @@ int zip_entry_open(struct zip_t *zip, const char *entryname) {
|
||||
zip->entry.uncomp_crc32 = MZ_CRC32_INIT;
|
||||
zip->entry.offset = zip->archive.m_archive_size;
|
||||
zip->entry.header_offset = zip->archive.m_archive_size;
|
||||
memset(zip->entry.header, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE * sizeof(mz_uint8));
|
||||
memset(zip->entry.header, 0,
|
||||
MZ_ZIP_LOCAL_DIR_HEADER_SIZE * sizeof(mz_uint8));
|
||||
zip->entry.method = 0;
|
||||
|
||||
pzip = &(zip->archive);
|
||||
num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pzip);
|
||||
num_alignment_padding_bytes =
|
||||
mz_zip_writer_compute_padding_needed_for_file_alignment(pzip);
|
||||
|
||||
if (!pzip->m_pState || (pzip->m_zip_mode != MZ_ZIP_MODE_WRITING)) {
|
||||
// Wrong zip mode
|
||||
@ -180,20 +193,30 @@ int zip_entry_open(struct zip_t *zip, const char *entryname) {
|
||||
return -1;
|
||||
}
|
||||
// no zip64 support yet
|
||||
if ((pzip->m_total_files == 0xFFFF) || ((pzip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + entrylen) > 0xFFFFFFFF)) {
|
||||
if ((pzip->m_total_files == 0xFFFF) ||
|
||||
((pzip->m_archive_size + num_alignment_padding_bytes +
|
||||
MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
|
||||
entrylen) > 0xFFFFFFFF)) {
|
||||
// No zip64 support yet
|
||||
return -1;
|
||||
}
|
||||
if (!mz_zip_writer_write_zeros(pzip, zip->entry.offset, num_alignment_padding_bytes + sizeof(zip->entry.header))) {
|
||||
if (!mz_zip_writer_write_zeros(
|
||||
pzip, zip->entry.offset,
|
||||
num_alignment_padding_bytes + sizeof(zip->entry.header))) {
|
||||
// Cannot memset zip entry header
|
||||
return -1;
|
||||
}
|
||||
|
||||
zip->entry.header_offset += num_alignment_padding_bytes;
|
||||
if (pzip->m_file_offset_alignment) { MZ_ASSERT((zip->entry.header_offset & (pzip->m_file_offset_alignment - 1)) == 0); }
|
||||
zip->entry.offset += num_alignment_padding_bytes + sizeof(zip->entry.header);
|
||||
if (pzip->m_file_offset_alignment) {
|
||||
MZ_ASSERT((zip->entry.header_offset &
|
||||
(pzip->m_file_offset_alignment - 1)) == 0);
|
||||
}
|
||||
zip->entry.offset +=
|
||||
num_alignment_padding_bytes + sizeof(zip->entry.header);
|
||||
|
||||
if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, zip->entry.name, entrylen) != entrylen) {
|
||||
if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, zip->entry.name,
|
||||
entrylen) != entrylen) {
|
||||
// Cannot write data to zip entry
|
||||
return -1;
|
||||
}
|
||||
@ -205,7 +228,11 @@ int zip_entry_open(struct zip_t *zip, const char *entryname) {
|
||||
zip->entry.state.m_cur_archive_file_ofs = zip->entry.offset;
|
||||
zip->entry.state.m_comp_size = 0;
|
||||
|
||||
if (tdefl_init(&(zip->entry.comp), mz_zip_writer_add_put_buf_callback, &(zip->entry.state), tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY) {
|
||||
if (tdefl_init(&(zip->entry.comp), mz_zip_writer_add_put_buf_callback,
|
||||
&(zip->entry.state),
|
||||
tdefl_create_comp_flags_from_zip_params(
|
||||
level, -15, MZ_DEFAULT_STRATEGY)) !=
|
||||
TDEFL_STATUS_OKAY) {
|
||||
// Cannot initialize the zip compressor
|
||||
return -1;
|
||||
}
|
||||
@ -215,10 +242,10 @@ int zip_entry_open(struct zip_t *zip, const char *entryname) {
|
||||
}
|
||||
|
||||
int zip_entry_close(struct zip_t *zip) {
|
||||
mz_zip_archive *pzip = NULL;
|
||||
mz_uint level;
|
||||
tdefl_status done;
|
||||
mz_uint16 entrylen;
|
||||
mz_zip_archive *pzip = NULL;
|
||||
mz_uint level;
|
||||
tdefl_status done;
|
||||
mz_uint16 entrylen;
|
||||
time_t t;
|
||||
struct tm *tm;
|
||||
mz_uint16 dos_time, dos_date;
|
||||
@ -245,29 +272,41 @@ int zip_entry_close(struct zip_t *zip) {
|
||||
entrylen = (mz_uint16)strlen(zip->entry.name);
|
||||
t = time(NULL);
|
||||
tm = localtime(&t);
|
||||
dos_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1));
|
||||
dos_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday);
|
||||
dos_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) +
|
||||
((tm->tm_sec) >> 1));
|
||||
dos_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) +
|
||||
((tm->tm_mon + 1) << 5) + tm->tm_mday);
|
||||
|
||||
// no zip64 support yet
|
||||
if ((zip->entry.comp_size > 0xFFFFFFFF) || (zip->entry.offset > 0xFFFFFFFF)) {
|
||||
if ((zip->entry.comp_size > 0xFFFFFFFF) ||
|
||||
(zip->entry.offset > 0xFFFFFFFF)) {
|
||||
// No zip64 support, yet
|
||||
cleanup(zip->entry.name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!mz_zip_writer_create_local_dir_header(pzip, zip->entry.header, entrylen, 0, zip->entry.uncomp_size, zip->entry.comp_size, zip->entry.uncomp_crc32, zip->entry.method, 0, dos_time, dos_date)) {
|
||||
if (!mz_zip_writer_create_local_dir_header(
|
||||
pzip, zip->entry.header, entrylen, 0, zip->entry.uncomp_size,
|
||||
zip->entry.comp_size, zip->entry.uncomp_crc32, zip->entry.method, 0,
|
||||
dos_time, dos_date)) {
|
||||
// Cannot create zip entry header
|
||||
cleanup(zip->entry.name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.header_offset, zip->entry.header, sizeof(zip->entry.header)) != sizeof(zip->entry.header)) {
|
||||
if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.header_offset,
|
||||
zip->entry.header, sizeof(zip->entry.header)) !=
|
||||
sizeof(zip->entry.header)) {
|
||||
// Cannot write zip entry header
|
||||
cleanup(zip->entry.name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!mz_zip_writer_add_to_central_dir(pzip, zip->entry.name, entrylen, NULL, 0, "", 0, zip->entry.uncomp_size, zip->entry.comp_size, zip->entry.uncomp_crc32, zip->entry.method, 0, dos_time, dos_date, zip->entry.header_offset, 0)) {
|
||||
if (!mz_zip_writer_add_to_central_dir(
|
||||
pzip, zip->entry.name, entrylen, NULL, 0, "", 0,
|
||||
zip->entry.uncomp_size, zip->entry.comp_size,
|
||||
zip->entry.uncomp_crc32, zip->entry.method, 0, dos_time, dos_date,
|
||||
zip->entry.header_offset, 0)) {
|
||||
// Cannot write to zip central dir
|
||||
cleanup(zip->entry.name);
|
||||
return -1;
|
||||
@ -281,9 +320,9 @@ int zip_entry_close(struct zip_t *zip) {
|
||||
}
|
||||
|
||||
int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize) {
|
||||
mz_uint level;
|
||||
mz_zip_archive *pzip = NULL;
|
||||
tdefl_status status;
|
||||
mz_uint level;
|
||||
mz_zip_archive *pzip = NULL;
|
||||
tdefl_status status;
|
||||
|
||||
if (!zip) {
|
||||
// zip_t handler is not initialized
|
||||
@ -293,18 +332,21 @@ int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize) {
|
||||
pzip = &(zip->archive);
|
||||
if (buf && bufsize > 0) {
|
||||
zip->entry.uncomp_size += bufsize;
|
||||
zip->entry.uncomp_crc32 = (mz_uint32)mz_crc32(zip->entry.uncomp_crc32, (const mz_uint8 *)buf, bufsize);
|
||||
zip->entry.uncomp_crc32 = (mz_uint32)mz_crc32(
|
||||
zip->entry.uncomp_crc32, (const mz_uint8 *)buf, bufsize);
|
||||
|
||||
level = zip->level & 0xF;
|
||||
level = zip->level & 0xF;
|
||||
if (!level) {
|
||||
if ((pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, buf, bufsize) != bufsize)) {
|
||||
if ((pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, buf,
|
||||
bufsize) != bufsize)) {
|
||||
// Cannot write buffer
|
||||
return -1;
|
||||
}
|
||||
zip->entry.offset += bufsize;
|
||||
zip->entry.comp_size += bufsize;
|
||||
} else {
|
||||
status = tdefl_compress_buffer(&(zip->entry.comp), buf, bufsize, TDEFL_NO_FLUSH);
|
||||
status = tdefl_compress_buffer(&(zip->entry.comp), buf, bufsize,
|
||||
TDEFL_NO_FLUSH);
|
||||
if (status != TDEFL_STATUS_DONE && status != TDEFL_STATUS_OKAY) {
|
||||
// Cannot compress buffer
|
||||
return -1;
|
||||
@ -316,10 +358,10 @@ int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize) {
|
||||
}
|
||||
|
||||
int zip_entry_fwrite(struct zip_t *zip, const char *filename) {
|
||||
int status = 0;
|
||||
int status = 0;
|
||||
size_t n = 0;
|
||||
FILE *stream = NULL;
|
||||
mz_uint8 buf[MZ_ZIP_MAX_IO_BUF_SIZE] = { 0 };
|
||||
FILE *stream = NULL;
|
||||
mz_uint8 buf[MZ_ZIP_MAX_IO_BUF_SIZE] = {0};
|
||||
|
||||
if (!zip) {
|
||||
// zip_t handler is not initialized
|
||||
@ -332,7 +374,8 @@ int zip_entry_fwrite(struct zip_t *zip, const char *filename) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while((n = fread(buf, sizeof(mz_uint8), MZ_ZIP_MAX_IO_BUF_SIZE, stream)) > 0) {
|
||||
while ((n = fread(buf, sizeof(mz_uint8), MZ_ZIP_MAX_IO_BUF_SIZE, stream)) >
|
||||
0) {
|
||||
if (zip_entry_write(zip, buf, n) < 0) {
|
||||
status = -1;
|
||||
break;
|
||||
@ -344,9 +387,9 @@ int zip_entry_fwrite(struct zip_t *zip, const char *filename) {
|
||||
}
|
||||
|
||||
int zip_create(const char *zipname, const char *filenames[], size_t len) {
|
||||
int status = 0;
|
||||
size_t i;
|
||||
mz_zip_archive zip_archive;
|
||||
int status = 0;
|
||||
size_t i;
|
||||
mz_zip_archive zip_archive;
|
||||
|
||||
if (!zipname || strlen(zipname) < 1) {
|
||||
// zip_t archive name is empty or NULL
|
||||
@ -371,7 +414,8 @@ int zip_create(const char *zipname, const char *filenames[], size_t len) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!mz_zip_writer_add_file(&zip_archive, basename(name), name, "", 0, ZIP_DEFAULT_COMPRESSION_LEVEL)) {
|
||||
if (!mz_zip_writer_add_file(&zip_archive, basename(name), name, "", 0,
|
||||
ZIP_DEFAULT_COMPRESSION_LEVEL)) {
|
||||
// Cannot add file to zip_archive
|
||||
status = -1;
|
||||
break;
|
||||
@ -383,12 +427,13 @@ int zip_create(const char *zipname, const char *filenames[], size_t len) {
|
||||
return status;
|
||||
}
|
||||
|
||||
int zip_extract(const char *zipname, const char *dir, int (* on_extract)(const char *filename, void *arg), void *arg) {
|
||||
int zip_extract(const char *zipname, const char *dir,
|
||||
int (*on_extract)(const char *filename, void *arg), void *arg) {
|
||||
int status = 0;
|
||||
mz_uint i, n;
|
||||
char path[MAX_PATH + 1] = { 0 };
|
||||
mz_zip_archive zip_archive;
|
||||
mz_zip_archive_file_stat info;
|
||||
char path[MAX_PATH + 1] = {0};
|
||||
mz_zip_archive zip_archive;
|
||||
mz_zip_archive_file_stat info;
|
||||
size_t dirlen = 0;
|
||||
|
||||
if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
|
||||
@ -401,7 +446,7 @@ int zip_extract(const char *zipname, const char *dir, int (* on_extract)(const c
|
||||
return -1;
|
||||
}
|
||||
|
||||
dirlen = strlen(dir);
|
||||
dirlen = strlen(dir);
|
||||
if (dirlen + 1 > MAX_PATH) {
|
||||
return -1;
|
||||
}
|
||||
@ -414,7 +459,7 @@ int zip_extract(const char *zipname, const char *dir, int (* on_extract)(const c
|
||||
}
|
||||
|
||||
strcpy(path, dir);
|
||||
if (!ISSLASH(path[dirlen -1])) {
|
||||
if (!ISSLASH(path[dirlen - 1])) {
|
||||
#if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
|
||||
path[dirlen] = '\\';
|
||||
#else
|
||||
@ -459,6 +504,6 @@ int zip_extract(const char *zipname, const char *dir, int (* on_extract)(const c
|
||||
status = -1;
|
||||
}
|
||||
|
||||
finally:
|
||||
finally:
|
||||
return status;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user