szip: make compile (#6314)
parent
51d7d7cfae
commit
601d098b48
|
@ -2194,7 +2194,8 @@ tinfl_status tinfl_decompress(tinfl_decompressor *r,
|
||||||
} else
|
} else
|
||||||
tree_cur = pTable->m_tree[-tree_cur - 1];
|
tree_cur = pTable->m_tree[-tree_cur - 1];
|
||||||
}
|
}
|
||||||
tree_cur -= ((rev_code >>= 1) & 1);
|
rev_code >>= 1;
|
||||||
|
tree_cur -= (rev_code & 1);
|
||||||
pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index;
|
pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index;
|
||||||
}
|
}
|
||||||
if (r->m_type == 2) {
|
if (r->m_type == 2) {
|
||||||
|
@ -6142,7 +6143,7 @@ mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip,
|
||||||
if (!mz_zip_writer_validate_archive_name(pArchive_name))
|
if (!mz_zip_writer_validate_archive_name(pArchive_name))
|
||||||
return MZ_FALSE;
|
return MZ_FALSE;
|
||||||
|
|
||||||
#ifndef MINIZ_NO_TIME
|
#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_WRITING_APIS)
|
||||||
{
|
{
|
||||||
time_t cur_time;
|
time_t cur_time;
|
||||||
time(&cur_time);
|
time(&cur_time);
|
||||||
|
@ -6327,9 +6328,13 @@ mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name,
|
||||||
return MZ_FALSE;
|
return MZ_FALSE;
|
||||||
|
|
||||||
memset(&file_modified_time, 0, sizeof(file_modified_time));
|
memset(&file_modified_time, 0, sizeof(file_modified_time));
|
||||||
|
#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_WRITING_APIS)
|
||||||
|
#ifndef MINIZ_NO_STDIO
|
||||||
if (!mz_zip_get_file_modified_time(pSrc_filename, &file_modified_time))
|
if (!mz_zip_get_file_modified_time(pSrc_filename, &file_modified_time))
|
||||||
return MZ_FALSE;
|
return MZ_FALSE;
|
||||||
|
#endif
|
||||||
mz_zip_time_t_to_dos_time(file_modified_time, &dos_time, &dos_date);
|
mz_zip_time_t_to_dos_time(file_modified_time, &dos_time, &dos_date);
|
||||||
|
#endif
|
||||||
|
|
||||||
pSrc_file = MZ_FOPEN(pSrc_filename, "rb");
|
pSrc_file = MZ_FOPEN(pSrc_filename, "rb");
|
||||||
if (!pSrc_file)
|
if (!pSrc_file)
|
||||||
|
|
|
@ -155,9 +155,9 @@ struct zip_t {
|
||||||
|
|
||||||
struct zip_t *zip_open(const char *zipname, int level, char mode) {
|
struct zip_t *zip_open(const char *zipname, int level, char mode) {
|
||||||
struct zip_t *zip = NULL;
|
struct zip_t *zip = NULL;
|
||||||
|
|
||||||
if (!zipname || strlen(zipname) < 1) {
|
if (!zipname || strlen(zipname) < 1) {
|
||||||
// zip_t archive name is empty or NULL
|
// zip_t archive name is empty or NULL
|
||||||
printf("%s", "name len");
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,13 +165,11 @@ struct zip_t *zip_open(const char *zipname, int level, char mode) {
|
||||||
level = MZ_DEFAULT_LEVEL;
|
level = MZ_DEFAULT_LEVEL;
|
||||||
if ((level & 0xF) > MZ_UBER_COMPRESSION) {
|
if ((level & 0xF) > MZ_UBER_COMPRESSION) {
|
||||||
// Wrong compression level
|
// Wrong compression level
|
||||||
printf("%s", "Wrong compression");
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
|
zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
|
||||||
if (!zip)
|
if (!zip)
|
||||||
printf("%s", "Cannot allocate");
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
zip->level = (mz_uint)level;
|
zip->level = (mz_uint)level;
|
||||||
|
@ -179,7 +177,7 @@ struct zip_t *zip_open(const char *zipname, int level, char mode) {
|
||||||
case 'w':
|
case 'w':
|
||||||
// Create a new archive.
|
// Create a new archive.
|
||||||
if (!mz_zip_writer_init_file(&(zip->archive), zipname, 0)) {
|
if (!mz_zip_writer_init_file(&(zip->archive), zipname, 0)) {
|
||||||
printf("%s", "Cannot initialize zip_archive writer");
|
// Cannot initialize zip_archive writer
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -212,7 +210,6 @@ cleanup:
|
||||||
}
|
}
|
||||||
|
|
||||||
void zip_close(struct zip_t *zip) {
|
void zip_close(struct zip_t *zip) {
|
||||||
printf("%s", "try to close");
|
|
||||||
if (zip) {
|
if (zip) {
|
||||||
// Always finalize, even if adding failed for some reason, so we have a
|
// Always finalize, even if adding failed for some reason, so we have a
|
||||||
// valid central directory.
|
// valid central directory.
|
||||||
|
@ -225,6 +222,20 @@ void zip_close(struct zip_t *zip) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int zip_is64(struct zip_t *zip) {
|
||||||
|
if (!zip) {
|
||||||
|
// zip_t handler is not initialized
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!zip->archive.m_pState) {
|
||||||
|
// zip state is not initialized
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int)zip->archive.m_pState->m_zip64;
|
||||||
|
}
|
||||||
|
|
||||||
int zip_entry_open(struct zip_t *zip, const char *entryname) {
|
int zip_entry_open(struct zip_t *zip, const char *entryname) {
|
||||||
size_t entrylen = 0;
|
size_t entrylen = 0;
|
||||||
mz_zip_archive *pzip = NULL;
|
mz_zip_archive *pzip = NULL;
|
||||||
|
@ -276,7 +287,9 @@ int zip_entry_open(struct zip_t *zip, const char *entryname) {
|
||||||
zip->entry.header_offset = stats.m_local_header_ofs;
|
zip->entry.header_offset = stats.m_local_header_ofs;
|
||||||
zip->entry.method = stats.m_method;
|
zip->entry.method = stats.m_method;
|
||||||
zip->entry.external_attr = stats.m_external_attr;
|
zip->entry.external_attr = stats.m_external_attr;
|
||||||
|
#ifndef MINIZ_NO_TIME
|
||||||
zip->entry.m_time = stats.m_time;
|
zip->entry.m_time = stats.m_time;
|
||||||
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -426,7 +439,9 @@ int zip_entry_openbyindex(struct zip_t *zip, int index) {
|
||||||
zip->entry.header_offset = stats.m_local_header_ofs;
|
zip->entry.header_offset = stats.m_local_header_ofs;
|
||||||
zip->entry.method = stats.m_method;
|
zip->entry.method = stats.m_method;
|
||||||
zip->entry.external_attr = stats.m_external_attr;
|
zip->entry.external_attr = stats.m_external_attr;
|
||||||
|
#ifndef MINIZ_NO_TIME
|
||||||
zip->entry.m_time = stats.m_time;
|
zip->entry.m_time = stats.m_time;
|
||||||
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -469,7 +484,9 @@ int zip_entry_close(struct zip_t *zip) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_ARCHIVE_WRITING_APIS)
|
||||||
mz_zip_time_t_to_dos_time(zip->entry.m_time, &dos_time, &dos_date);
|
mz_zip_time_t_to_dos_time(zip->entry.m_time, &dos_time, &dos_date);
|
||||||
|
#endif
|
||||||
if (!mz_zip_writer_create_local_dir_header(
|
if (!mz_zip_writer_create_local_dir_header(
|
||||||
pzip, zip->entry.header, entrylen, 0, zip->entry.uncomp_size,
|
pzip, zip->entry.header, entrylen, 0, zip->entry.uncomp_size,
|
||||||
zip->entry.comp_size, zip->entry.uncomp_crc32, zip->entry.method, 0,
|
zip->entry.comp_size, zip->entry.uncomp_crc32, zip->entry.method, 0,
|
||||||
|
@ -797,7 +814,8 @@ int zip_create(const char *zipname, const char *filenames[], size_t len) {
|
||||||
|
|
||||||
if (MZ_FILE_STAT(name, &file_stat) != 0) {
|
if (MZ_FILE_STAT(name, &file_stat) != 0) {
|
||||||
// problem getting information - check errno
|
// problem getting information - check errno
|
||||||
return -1;
|
status = -1;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((file_stat.st_mode & 0200) == 0) {
|
if ((file_stat.st_mode & 0200) == 0) {
|
||||||
|
@ -820,40 +838,58 @@ int zip_create(const char *zipname, const char *filenames[], size_t len) {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
int zip_extract(const char *zipname, const char *dir,
|
static char *normalize(char *name, char *const nname, size_t len) {
|
||||||
int (*on_extract)(const char *filename, void *arg), void *arg) {
|
size_t offn = 0;
|
||||||
|
size_t offnn = 0, ncpy = 0;
|
||||||
|
|
||||||
|
if (name == NULL || nname == NULL || len <= 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
// skip trailing '/'
|
||||||
|
while (ISSLASH(*name))
|
||||||
|
name++;
|
||||||
|
|
||||||
|
for (; offn < len; offn++) {
|
||||||
|
if (ISSLASH(name[offn])) {
|
||||||
|
if (ncpy > 0 && strncmp(&nname[offnn], ".", 1) &&
|
||||||
|
strncmp(&nname[offnn], "..", 2)) {
|
||||||
|
offnn += ncpy;
|
||||||
|
nname[offnn++] = name[offn]; // append '/'
|
||||||
|
}
|
||||||
|
ncpy = 0;
|
||||||
|
} else {
|
||||||
|
nname[offnn + ncpy] = name[offn];
|
||||||
|
ncpy++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// at the end, extra check what we've already copied
|
||||||
|
if (ncpy == 0 || !strncmp(&nname[offnn], ".", 1) ||
|
||||||
|
!strncmp(&nname[offnn], "..", 2)) {
|
||||||
|
nname[offnn] = 0;
|
||||||
|
}
|
||||||
|
return nname;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int extract(mz_zip_archive *zip_archive, const char *dir,
|
||||||
|
int (*on_extract)(const char *filename, void *arg),
|
||||||
|
void *arg) {
|
||||||
int status = -1;
|
int status = -1;
|
||||||
mz_uint i, n;
|
mz_uint i, n;
|
||||||
char path[MAX_PATH + 1];
|
char path[MAX_PATH + 1];
|
||||||
char symlink_to[MAX_PATH + 1];
|
char symlink_to[MAX_PATH + 1];
|
||||||
mz_zip_archive zip_archive;
|
|
||||||
mz_zip_archive_file_stat info;
|
mz_zip_archive_file_stat info;
|
||||||
size_t dirlen = 0;
|
size_t dirlen = 0;
|
||||||
mz_uint32 xattr = 0;
|
mz_uint32 xattr = 0;
|
||||||
|
|
||||||
memset(path, 0, sizeof(path));
|
memset(path, 0, sizeof(path));
|
||||||
memset(symlink_to, 0, sizeof(symlink_to));
|
memset(symlink_to, 0, sizeof(symlink_to));
|
||||||
if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
|
|
||||||
// Cannot memset zip archive
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!zipname || !dir) {
|
|
||||||
// Cannot parse zip archive name
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
dirlen = strlen(dir);
|
dirlen = strlen(dir);
|
||||||
if (dirlen + 1 > MAX_PATH) {
|
if (dirlen + 1 > MAX_PATH) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now try to open the archive.
|
|
||||||
if (!mz_zip_reader_init_file(&zip_archive, zipname, 0)) {
|
|
||||||
// Cannot initialize zip_archive reader
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset((void *)&info, 0, sizeof(mz_zip_archive_file_stat));
|
memset((void *)&info, 0, sizeof(mz_zip_archive_file_stat));
|
||||||
|
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER)
|
||||||
|
@ -872,12 +908,16 @@ int zip_extract(const char *zipname, const char *dir,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get and print information about each file in the archive.
|
// Get and print information about each file in the archive.
|
||||||
n = mz_zip_reader_get_num_files(&zip_archive);
|
n = mz_zip_reader_get_num_files(zip_archive);
|
||||||
for (i = 0; i < n; ++i) {
|
for (i = 0; i < n; ++i) {
|
||||||
if (!mz_zip_reader_file_stat(&zip_archive, i, &info)) {
|
if (!mz_zip_reader_file_stat(zip_archive, i, &info)) {
|
||||||
// Cannot get information about zip archive;
|
// Cannot get information about zip archive;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
if (!normalize(info.m_filename, info.m_filename, strlen(info.m_filename))) {
|
||||||
|
// Cannot normalize file name;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
#if defined(_MSC_VER)
|
#if defined(_MSC_VER)
|
||||||
strncpy_s(&path[dirlen], MAX_PATH - dirlen, info.m_filename,
|
strncpy_s(&path[dirlen], MAX_PATH - dirlen, info.m_filename,
|
||||||
MAX_PATH - dirlen);
|
MAX_PATH - dirlen);
|
||||||
|
@ -900,7 +940,7 @@ int zip_extract(const char *zipname, const char *dir,
|
||||||
defined(__MINGW32__)
|
defined(__MINGW32__)
|
||||||
#else
|
#else
|
||||||
if (info.m_uncomp_size > MAX_PATH ||
|
if (info.m_uncomp_size > MAX_PATH ||
|
||||||
!mz_zip_reader_extract_to_mem_no_alloc(&zip_archive, i, symlink_to,
|
!mz_zip_reader_extract_to_mem_no_alloc(zip_archive, i, symlink_to,
|
||||||
MAX_PATH, 0, NULL, 0)) {
|
MAX_PATH, 0, NULL, 0)) {
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
@ -910,8 +950,8 @@ int zip_extract(const char *zipname, const char *dir,
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
if (!mz_zip_reader_is_file_a_directory(&zip_archive, i)) {
|
if (!mz_zip_reader_is_file_a_directory(zip_archive, i)) {
|
||||||
if (!mz_zip_reader_extract_to_file(&zip_archive, i, path, 0)) {
|
if (!mz_zip_reader_extract_to_file(zip_archive, i, path, 0)) {
|
||||||
// Cannot extract zip archive to file
|
// Cannot extract zip archive to file
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
@ -938,10 +978,53 @@ int zip_extract(const char *zipname, const char *dir,
|
||||||
|
|
||||||
out:
|
out:
|
||||||
// Close the archive, freeing any resources it was using
|
// Close the archive, freeing any resources it was using
|
||||||
if (!mz_zip_reader_end(&zip_archive)) {
|
if (!mz_zip_reader_end(zip_archive)) {
|
||||||
// Cannot end zip reader
|
// Cannot end zip reader
|
||||||
status = -1;
|
status = -1;
|
||||||
}
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
int zip_extract(const char *zipname, const char *dir,
|
||||||
|
int (*on_extract)(const char *filename, void *arg), void *arg) {
|
||||||
|
mz_zip_archive zip_archive;
|
||||||
|
if (!zipname || !dir) {
|
||||||
|
// Cannot parse zip archive name
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!memset(&zip_archive, 0, sizeof(mz_zip_archive))) {
|
||||||
|
// Cannot memset zip archive
|
||||||
|
return MZ_FALSE;
|
||||||
|
}
|
||||||
|
// Now try to open the archive.
|
||||||
|
if (!mz_zip_reader_init_file(&zip_archive, zipname, 0)) {
|
||||||
|
// Cannot initialize zip_archive reader
|
||||||
|
return MZ_FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int status = extract(&zip_archive, dir, on_extract, arg);
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
int zip_extract_stream(const char *stream, size_t size, const char *dir,
|
||||||
|
int (*on_extract)(const char *filename, void *arg),
|
||||||
|
void *arg) {
|
||||||
|
mz_zip_archive zip_archive;
|
||||||
|
if (!stream || !dir) {
|
||||||
|
// Cannot parse zip archive stream
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (!memset(&zip_archive, 0, sizeof(mz_zip_archive))) {
|
||||||
|
// Cannot memset zip archive
|
||||||
|
return MZ_FALSE;
|
||||||
|
}
|
||||||
|
if (!mz_zip_reader_init_mem(&zip_archive, stream, size, 0)) {
|
||||||
|
// Cannot initialize zip_archive reader
|
||||||
|
return MZ_FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int status = extract(&zip_archive, dir, on_extract, arg);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,8 +20,9 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(_SSIZE_T_DEFINED) && !defined(_SSIZE_T_DEFINED_) && \
|
#if !defined(_SSIZE_T_DEFINED) && !defined(_SSIZE_T_DEFINED_) && \
|
||||||
!defined(_SSIZE_T) && !defined(_SSIZE_T_) && !defined(__ssize_t_defined)
|
!defined(__DEFINED_ssize_t) && !defined(__ssize_t_defined) && \
|
||||||
#define _SSIZE_T
|
!defined(_SSIZE_T) && !defined(_SSIZE_T_) && !defined(_SSIZE_T_DECLARED)
|
||||||
|
|
||||||
// 64-bit Windows is the only mainstream platform
|
// 64-bit Windows is the only mainstream platform
|
||||||
// where sizeof(long) != sizeof(void*)
|
// where sizeof(long) != sizeof(void*)
|
||||||
#ifdef _WIN64
|
#ifdef _WIN64
|
||||||
|
@ -29,6 +30,15 @@ typedef long long ssize_t; /* byte count or error */
|
||||||
#else
|
#else
|
||||||
typedef long ssize_t; /* byte count or error */
|
typedef long ssize_t; /* byte count or error */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define _SSIZE_T_DEFINED
|
||||||
|
#define _SSIZE_T_DEFINED_
|
||||||
|
#define __DEFINED_ssize_t
|
||||||
|
#define __ssize_t_defined
|
||||||
|
#define _SSIZE_T
|
||||||
|
#define _SSIZE_T_
|
||||||
|
#define _SSIZE_T_DECLARED
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MAX_PATH
|
#ifndef MAX_PATH
|
||||||
|
@ -81,6 +91,16 @@ extern struct zip_t *zip_open(const char *zipname, int level, char mode);
|
||||||
*/
|
*/
|
||||||
extern void zip_close(struct zip_t *zip);
|
extern void zip_close(struct zip_t *zip);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines if the archive has a zip64 end of central directory headers.
|
||||||
|
*
|
||||||
|
* @param zip zip archive handler.
|
||||||
|
*
|
||||||
|
* @return the return code - 1 (true), 0 (false), negative number (< 0) on
|
||||||
|
* error.
|
||||||
|
*/
|
||||||
|
extern int zip_is64(struct zip_t *zip);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens an entry by name in the zip archive.
|
* Opens an entry by name in the zip archive.
|
||||||
*
|
*
|
||||||
|
@ -293,6 +313,27 @@ extern int zip_extract(const char *zipname, const char *dir,
|
||||||
int (*on_extract_entry)(const char *filename, void *arg),
|
int (*on_extract_entry)(const char *filename, void *arg),
|
||||||
void *arg);
|
void *arg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extracts a zip archive stream into directory.
|
||||||
|
*
|
||||||
|
* If on_extract is not NULL, the callback will be called after
|
||||||
|
* successfully extracted each zip entry.
|
||||||
|
* Returning a negative value from the callback will cause abort and return an
|
||||||
|
* error. The last argument (void *arg) is optional, which you can use to pass
|
||||||
|
* data to the on_extract callback.
|
||||||
|
*
|
||||||
|
* @param stream zip archive stream.
|
||||||
|
* @param size stream size.
|
||||||
|
* @param dir output directory.
|
||||||
|
* @param on_extract on extract callback.
|
||||||
|
* @param arg opaque pointer.
|
||||||
|
*
|
||||||
|
* @return the return code - 0 on success, negative number (< 0) on error.
|
||||||
|
*/
|
||||||
|
extern int zip_extract_stream(const char *stream, size_t size, const char *dir,
|
||||||
|
int (*on_extract)(const char *filename,
|
||||||
|
void *arg),
|
||||||
|
void *arg);
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
249
vlib/szip/szip.v
249
vlib/szip/szip.v
|
@ -1,32 +1,42 @@
|
||||||
module szip
|
module szip
|
||||||
|
|
||||||
#flag -I @VROOT/thirdparty/zip
|
#flag -I @VROOT/thirdparty/zip
|
||||||
|
|
||||||
#include "zip.c"
|
#include "zip.c"
|
||||||
#include "zip.h"
|
#include "zip.h"
|
||||||
|
|
||||||
type zip C.zip_t
|
struct C.zip_t {}
|
||||||
type zip_ptr &zip
|
|
||||||
|
type Zip C.zip_t
|
||||||
|
|
||||||
|
fn C.zip_open(byteptr, int, byte) &Zip
|
||||||
|
fn C.zip_close(&Zip)
|
||||||
|
fn C.zip_entry_open(&Zip, byteptr) int
|
||||||
|
fn C.zip_entry_close(&Zip) int
|
||||||
|
fn C.zip_entry_name(&Zip) byteptr
|
||||||
|
fn C.zip_entry_index(&Zip) int
|
||||||
|
fn C.zip_entry_isdir(&Zip) int
|
||||||
|
fn C.zip_entry_size(&Zip) u64
|
||||||
|
fn C.zip_entry_crc32(&Zip) u32
|
||||||
|
fn C.zip_entry_write(&Zip, voidptr, int) int
|
||||||
|
fn C.zip_entry_fwrite(&Zip, byteptr) int
|
||||||
|
fn C.zip_entry_read(&Zip, byteptr, int) int
|
||||||
|
fn C.zip_entry_fread(&Zip, byteptr) int
|
||||||
|
fn C.zip_total_entries(&Zip) int
|
||||||
|
|
||||||
// Ref - miniz.h
|
// Ref - miniz.h
|
||||||
const (
|
const (
|
||||||
no_compression = 0
|
no_compression = 0
|
||||||
best_speed = 1
|
best_speed = 1
|
||||||
best_compression = 9
|
best_compression = 9
|
||||||
uber_compression = 10
|
uber_compression = 10
|
||||||
default_level = 6
|
default_level = 6
|
||||||
default_compression = -1
|
default_compression = -1
|
||||||
)
|
|
||||||
|
|
||||||
// Ref - zip.h
|
|
||||||
const (
|
|
||||||
default_compression_level = (default_level)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
m_write = 'w'
|
m_write = `w`
|
||||||
m_ronly = 'r'
|
m_ronly = `r`
|
||||||
m_append = 'a'
|
m_append = `a`
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -40,34 +50,32 @@ open opens zip archive with compression level using the given mode.
|
||||||
- 'a': appends to an existing archive.
|
- 'a': appends to an existing archive.
|
||||||
|
|
||||||
@return the zip archive handler or NULL on error
|
@return the zip archive handler or NULL on error
|
||||||
*/
|
*/
|
||||||
pub fn open(name string, level int, mode string) ?zip_ptr {
|
pub fn open(name string, level int, mode byte) ?&Zip {
|
||||||
mut _nlevel := level
|
mut nlevel := level
|
||||||
if (_nlevel & 0xF) > uber_compression {
|
if (nlevel & 0xF) > uber_compression {
|
||||||
_nlevel = default_compression_level
|
nlevel = default_level
|
||||||
}
|
}
|
||||||
n := name.len
|
if name.len == 0 {
|
||||||
if n == 0 {
|
return error('szip: name of file empty')
|
||||||
return error('szip: name of file empty')
|
}
|
||||||
}
|
if mode !in [m_write, m_ronly, m_append] {
|
||||||
if mode != m_write && mode != m_ronly && mode != m_append {
|
return error('szip: invalid provided open mode')
|
||||||
return error('szip: invalid provided open mode')
|
}
|
||||||
}
|
p_zip := &Zip(C.zip_open(name.str, nlevel, mode))
|
||||||
/* struct zip_t* */_p_zip := zip_ptr(C.zip_open(name.str,
|
if isnil(p_zip) {
|
||||||
_nlevel, mode.str))
|
return error('szip: cannot open/create/append new zip archive')
|
||||||
if _p_zip == zip_ptr(0) {
|
}
|
||||||
return error('szip: cannot open/create/append new zip archive')
|
return p_zip
|
||||||
}
|
|
||||||
return _p_zip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
close closes the zip archive, releases resources - always finalize.
|
close closes the zip archive, releases resources - always finalize.
|
||||||
|
|
||||||
@param zip zip archive handler.
|
@param zip zip archive handler.
|
||||||
*/
|
*/
|
||||||
pub fn (mut z zip_ptr) close() {
|
pub fn (mut z Zip) close() {
|
||||||
C.zip_close(z)
|
C.zip_close(z)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -81,21 +89,23 @@ in global dictionary.
|
||||||
@param entryname an entry name in local dictionary.
|
@param entryname an entry name in local dictionary.
|
||||||
|
|
||||||
@return the return code - 0 on success, negative number (< 0) on error.
|
@return the return code - 0 on success, negative number (< 0) on error.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) open_entry(name string) /*?*/bool {
|
pub fn (mut zentry Zip) open_entry(name string) ? {
|
||||||
res := C.zip_entry_open(zentry, name.str)
|
res := C.zip_entry_open(zentry, name.str)
|
||||||
return res != -1
|
if res == -1 {
|
||||||
|
return error('szip: cannot open archive entry')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* close_entry closes a zip entry, flushes buffer and releases resources.
|
* close_entry closes a zip entry, flushes buffer and releases resources.
|
||||||
*
|
*
|
||||||
* @param zip zip archive handler.
|
* @param zip zip archive handler.
|
||||||
*
|
*
|
||||||
* @return the return code - 0 on success, negative number (< 0) on error.
|
* @return the return code - 0 on success, negative number (< 0) on error.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) close_entry() {
|
pub fn (mut zentry Zip) close_entry() {
|
||||||
C.zip_entry_close(zentry)
|
C.zip_entry_close(zentry)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -111,13 +121,13 @@ for compatibility with Amiga and UNIX file systems etc.
|
||||||
@param zip: zip archive handler.
|
@param zip: zip archive handler.
|
||||||
|
|
||||||
@return the pointer to the current zip entry name, or NULL on error.
|
@return the pointer to the current zip entry name, or NULL on error.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) name() string {
|
pub fn (mut zentry Zip) name() string {
|
||||||
_name := C.zip_entry_name(zentry)
|
name := C.zip_entry_name(zentry)
|
||||||
if _name == 0 {
|
if name == 0 {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
return tos_clone(_name)
|
return tos_clone(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -126,13 +136,13 @@ index returns an index of the current zip entry.
|
||||||
@param zip zip archive handler.
|
@param zip zip archive handler.
|
||||||
|
|
||||||
@return the index on success, negative number (< 0) on error.
|
@return the index on success, negative number (< 0) on error.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) index() ?int {
|
pub fn (mut zentry Zip) index() ?int {
|
||||||
_index := int(C.zip_entry_index(zentry))
|
index := int(C.zip_entry_index(zentry))
|
||||||
if _index == -1 {
|
if index == -1 {
|
||||||
return error('szip: cannot get current index of zip entry')
|
return error('szip: cannot get current index of zip entry')
|
||||||
}
|
}
|
||||||
return _index // must be check for INVALID_VALUE
|
return index // must be check for INVALID_VALUE
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -142,14 +152,13 @@ isdir determines if the current zip entry is a directory entry.
|
||||||
|
|
||||||
@return the return code - 1 (true), 0 (false), negative number (< 0) on
|
@return the return code - 1 (true), 0 (false), negative number (< 0) on
|
||||||
error.
|
error.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) isdir() ?bool {
|
pub fn (mut zentry Zip) isdir() ?bool {
|
||||||
_isdir := C.zip_entry_isdir(zentry)
|
isdir := C.zip_entry_isdir(zentry)
|
||||||
if _isdir == -1 {
|
if isdir < 0 {
|
||||||
return error('szip: cannot check entry type')
|
return error('szip: cannot check entry type')
|
||||||
}
|
}
|
||||||
dir := bool(_isdir) // wtf V , unary lvalue
|
return isdir == 1
|
||||||
return dir
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -158,10 +167,9 @@ size returns an uncompressed size of the current zip entry.
|
||||||
@param zip zip archive handler.
|
@param zip zip archive handler.
|
||||||
|
|
||||||
@return the uncompressed size in bytes.
|
@return the uncompressed size in bytes.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) size() i64 {
|
pub fn (mut zentry Zip) size() u64 {
|
||||||
_size := i64(C.zip_entry_size(zentry))
|
return C.zip_entry_size(zentry)
|
||||||
return _size
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -170,28 +178,29 @@ crc32 returns CRC-32 checksum of the current zip entry.
|
||||||
@param zip zip archive handler.
|
@param zip zip archive handler.
|
||||||
|
|
||||||
@return the CRC-32 checksum.
|
@return the CRC-32 checksum.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) crc32() u32 {
|
pub fn (mut zentry Zip) crc32() u32 {
|
||||||
_checksum := u32(C.zip_entry_crc32(zentry))
|
return C.zip_entry_crc32(zentry)
|
||||||
return _checksum // 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
write_entry compresses an input buffer for the current zip entry.
|
write_entry compresses an input buffer for the current zip entry.
|
||||||
|
|
||||||
@param zip zip archive handler.
|
@param zip zip archive handler.
|
||||||
@param buf input buffer.
|
@param buf input buffer.
|
||||||
@param bufsize input buffer size (in bytes).
|
@param bufsize input buffer size (in bytes).
|
||||||
|
|
||||||
@return the return code - 0 on success, negative number (< 0) on error.
|
@return the return code - 0 on success, negative number (< 0) on error.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) write_entry(data []byte) bool {
|
pub fn (mut zentry Zip) write_entry(data []byte) ? {
|
||||||
if (data[0] & 0xff) == -1 {
|
if (data[0] & 0xff) == -1 {
|
||||||
return false
|
return error('szip: cannot write entry')
|
||||||
}
|
}
|
||||||
buf := data // alias of data
|
buf := data // alias of data
|
||||||
res := C.zip_entry_write(zentry, buf.data, buf.len)
|
res := C.zip_entry_write(zentry, buf.data, buf.len)
|
||||||
return res == 0
|
if res != 0 {
|
||||||
|
return error('szip: failed to write entry')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -201,10 +210,12 @@ create_entry compresses a file for the current zip entry.
|
||||||
@param filename input file.
|
@param filename input file.
|
||||||
|
|
||||||
@return the return code - 0 on success, negative number (< 0) on error.
|
@return the return code - 0 on success, negative number (< 0) on error.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) create_entry(name string) bool {
|
pub fn (mut zentry Zip) create_entry(name string) ? {
|
||||||
res := C.zip_entry_fwrite(zentry, name.str)
|
res := C.zip_entry_fwrite(zentry, name.str)
|
||||||
return res == 0
|
if res != 0 {
|
||||||
|
return error('szip: failed to create entry')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -221,15 +232,15 @@ for large entries, please take a look at zip_entry_extract function.
|
||||||
|
|
||||||
@return the return code - the number of bytes actually read on success.
|
@return the return code - the number of bytes actually read on success.
|
||||||
Otherwise a -1 on error.
|
Otherwise a -1 on error.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) read_entry() ?voidptr {
|
pub fn (mut zentry Zip) read_entry() ?voidptr {
|
||||||
mut _buf := voidptr(0)
|
mut buf := voidptr(0)
|
||||||
mut _bsize := i64(0)
|
mut bsize := i64(0)
|
||||||
res := C.zip_entry_read(zentry, &_buf, &_bsize)
|
res := C.zip_entry_read(zentry, &buf, &bsize)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return error('szip: cannot read properly data from entry')
|
return error('szip: cannot read properly data from entry')
|
||||||
}
|
}
|
||||||
return _buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -239,14 +250,15 @@ extract_entry extracts the current zip entry into output file.
|
||||||
@param filename output file.
|
@param filename output file.
|
||||||
|
|
||||||
@return the return code - 0 on success, negative number (< 0) on error.
|
@return the return code - 0 on success, negative number (< 0) on error.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) extract_entry(path string) /*?*/bool {
|
pub fn (mut zentry Zip) extract_entry(path string) ? {
|
||||||
if C.access(charptr(path.str), 0) == -1 {
|
if C.access(charptr(path.str), 0) == -1 {
|
||||||
return false
|
return error('Cannot open file for extracting, file not exists')
|
||||||
//return error('Cannot open file for extracting, file not exists')
|
}
|
||||||
}
|
res := C.zip_entry_fread(zentry, path.str)
|
||||||
res := C.zip_entry_fread(zentry, path.str)
|
if res != 0 {
|
||||||
return res == 0
|
return error('szip: failed to extract entry')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -258,16 +270,17 @@ extract extracts the current zip entry using a callback function (on_extract).
|
||||||
on_extract callback)
|
on_extract callback)
|
||||||
|
|
||||||
@return the return code - 0 on success, negative number (< 0) on error.
|
@return the return code - 0 on success, negative number (< 0) on error.
|
||||||
*/
|
*/
|
||||||
/*fn (mut zentry zip_ptr) extract(path string) bool {
|
/*
|
||||||
|
fn (mut zentry Zip) extract(path string) bool {
|
||||||
if C.access(path.str, 0) == -1 {
|
if C.access(path.str, 0) == -1 {
|
||||||
return false
|
return false
|
||||||
//return error('Cannot open directory for extracting, directory not exists')
|
//return error('Cannot open directory for extracting, directory not exists')
|
||||||
}
|
}
|
||||||
res := C.zip_extract(zentry, path.str, 0, 0)
|
res := C.zip_extract(zentry, path.str, 0, 0)
|
||||||
return res == 0
|
return res == 0
|
||||||
}*/
|
}
|
||||||
|
*/
|
||||||
/*
|
/*
|
||||||
total returns the number of all entries (files and directories) in the zip archive.
|
total returns the number of all entries (files and directories) in the zip archive.
|
||||||
|
|
||||||
|
@ -275,11 +288,11 @@ total returns the number of all entries (files and directories) in the zip archi
|
||||||
|
|
||||||
@return the return code - the number of entries on success, negative number
|
@return the return code - the number of entries on success, negative number
|
||||||
(< 0) on error.
|
(< 0) on error.
|
||||||
*/
|
*/
|
||||||
pub fn (mut zentry zip_ptr) total() ?int {
|
pub fn (mut zentry Zip) total() ?int {
|
||||||
_tentry := int(C.zip_total_entries(zentry))
|
tentry := int(C.zip_total_entries(zentry))
|
||||||
if _tentry == -1 {
|
if tentry == -1 {
|
||||||
return error('szip: cannot count total entries')
|
return error('szip: cannot count total entries')
|
||||||
}
|
}
|
||||||
return _tentry
|
return tentry
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
import szip
|
||||||
|
import os
|
||||||
|
|
||||||
|
fn test_compile() {
|
||||||
|
szip.open('test_compile.zip', szip.best_speed, szip.m_write) or {
|
||||||
|
assert false
|
||||||
|
}
|
||||||
|
os.rm('test_compile.zip') or { }
|
||||||
|
}
|
Loading…
Reference in New Issue