1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
Paparoni Alvardo 2019-07-15 19:35:34 +03:00 committed by Alexander Medvednikov
parent a7529b7b05
commit f27777af56
4 changed files with 8390 additions and 0 deletions

6854
vlib/szip/miniz.h Normal file

File diff suppressed because it is too large Load Diff

287
vlib/szip/szip.v Normal file
View File

@ -0,0 +1,287 @@
module szip
// what the shit im do
// #flag -I @VROOT/thirdparty/szip
#flag -I .
#include "zip.c"
#include "zip.h"
type zip C.zip_t
type zip_ptr *zip
// Ref - miniz.h
const (
NO_COMPRESSION = 0
BEST_SPEED = 1
BEST_COMPRESSION = 9
UBER_COMPRESSION = 10
DEFAULT_LEVEL = 6
DEFAULT_COMPRESSION = -1
)
// Ref - zip.h
const (
DEFAULT_COMPRESSION_LEVEL = (DEFAULT_LEVEL)
)
const (
M_WRITE = 'w'
M_RONLY = 'r'
M_APPEND = 'a'
)
/**
* open opens zip archive with compression level using the given mode.
*
* @param zipname zip archive file name.
* @param level compression level (0-9 are the standard zlib-style levels).
* @param mode file access mode.
* - 'r': opens a file for reading/extracting (the file must exists).
* - 'w': creates an empty file for writing.
* - 'a': appends to an existing archive.
*
* @return the zip archive handler or NULL on error
*/
pub fn open(name string, level int, mode string) ?zip_ptr {
mut _nlevel := level
if (_nlevel & 0xF) > UBER_COMPRESSION {
_nlevel = DEFAULT_COMPRESSION_LEVEL
}
n := name.len
if n == 0 {
return error('szip: name of file empty')
}
if mode != M_WRITE && mode != M_RONLY && mode != M_APPEND {
return error('szip: invalid provided open mode')
}
/* struct zip_t* */_p_zip := zip_ptr(C.zip_open(name.cstr(),
_nlevel, mode.cstr()))
if _p_zip == zip_ptr(0) {
return error('szip: cannot open/create/append new zip archive.')
}
return _p_zip
}
/**
* close closes the zip archive, releases resources - always finalize.
*
* @param zip zip archive handler.
*/
pub fn (z mut zip_ptr) close() {
C.zip_close(z)
}
/**
* open_entry opens an entry by name in the zip archive.
*
* For zip archive opened in 'w' or 'a' mode the function will append
* a new entry. In readonly mode the function tries to locate the entry
* in global dictionary.
*
* @param zip zip archive handler.
* @param entryname an entry name in local dictionary.
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
pub fn (zentry mut zip_ptr) open_entry(name string) /*?*/bool {
res := C.zip_entry_open(zentry, name.cstr())
return res != -1
}
/**
* close_entry closes a zip entry, flushes buffer and releases resources.
*
* @param zip zip archive handler.
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
pub fn (zentry mut zip_ptr) close_entry() {
C.zip_entry_close(zentry)
}
/**
* name returns a local name of the current zip entry.
*
* The main difference between user's entry name and local entry name
* is optional relative path.
* Following .ZIP File Format Specification - the path stored MUST not contain
* a drive or device letter, or a leading slash.
* All slashes MUST be forward slashes '/' as opposed to backwards slashes '\'
* for compatibility with Amiga and UNIX file systems etc.
*
* @param zip: zip archive handler.
*
* @return the pointer to the current zip entry name, or NULL on error.
*/
pub fn (zentry mut zip_ptr) name() string {
_name := C.zip_entry_name(zentry)
if _name == 0 {
return ''
}
return tos_clone(_name)
}
/**
* index returns an index of the current zip entry.
*
* @param zip zip archive handler.
*
* @return the index on success, negative number (< 0) on error.
*/
pub fn (zentry mut zip_ptr) index() ?int {
_index := int(C.zip_entry_index(zentry))
if _index == -1 {
return error('szip: cannot get current index of zip entry.')
}
return _index // must be check for INVALID_VALUE
}
/**
* isdir determines if the current zip entry is a directory entry.
*
* @param zip zip archive handler.
*
* @return the return code - 1 (true), 0 (false), negative number (< 0) on
* error.
*/
pub fn (zentry mut zip_ptr) isdir() ?bool {
_isdir := C.zip_entry_isdir(zentry)
if _isdir == -1 {
return error('szip: cannot check entry type.')
}
dir := bool(_isdir) // wtf V , unary lvalue
return dir
}
/**
* size returns an uncompressed size of the current zip entry.
*
* @param zip zip archive handler.
*
* @return the uncompressed size in bytes.
*/
pub fn (zentry mut zip_ptr) size() i64 {
_size := i64(C.zip_entry_size(zentry))
return _size
}
/**
* crc32 returns CRC-32 checksum of the current zip entry.
*
* @param zip zip archive handler.
*
* @return the CRC-32 checksum.
*/
pub fn (zentry mut zip_ptr) crc32() u32 {
_checksum := u32(C.zip_entry_crc32(zentry))
return _checksum // 0
}
/**
* write_entry compresses an input buffer for the current zip entry.
*
* @param zip zip archive handler.
* @param buf input buffer.
* @param bufsize input buffer size (in bytes).
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
pub fn (zentry mut zip_ptr) write_entry(data []byte) bool {
if (data[0] & 0xff) == -1 {
return false
}
buf := data // alias of data
res := C.zip_entry_write(zentry, buf.data, buf.len)
return res == 0
}
/**
* create_entry compresses a file for the current zip entry.
*
* @param zip zip archive handler.
* @param filename input file.
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
pub fn (zentry mut zip_ptr) create_entry(name string) bool {
res := C.zip_entry_fwrite(zentry, name.cstr())
return res == 0
}
/**
* read_entry extracts the current zip entry into output buffer.
*
* The function allocates sufficient memory for a output buffer.
*
* @param zip zip archive handler.
* @param buf output buffer.
* @param bufsize output buffer size (in bytes).
*
* @note remember to release memory allocated for a output buffer.
* for large entries, please take a look at zip_entry_extract function.
*
* @return the return code - the number of bytes actually read on success.
* Otherwise a -1 on error.
*/
pub fn (zentry mut zip_ptr) read_entry() ?voidptr {
mut _buf := voidptr(0)
mut _bsize := i64(0)
res := C.zip_entry_read(zentry, &_buf, &_bsize)
if res == -1 {
return error('szip: cannot read properly data from entry.')
}
return _buf
}
/**
* extract_entry extracts the current zip entry into output file.
*
* @param zip zip archive handler.
* @param filename output file.
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
pub fn (zentry mut zip_ptr) extract_entry(path string) /*?*/bool {
if C.access(path.cstr(), 0) == -1 {
return false
//return error('Cannot open file for extracting, file not exists')
}
res := C.zip_entry_fread(zentry, path.cstr())
return res == 0
}
/**
* extract extracts the current zip entry using a callback function (on_extract).
*
* @param zip zip archive handler.
* @param on_extract callback function.
* @param arg opaque pointer (optional argument, which you can pass to the
* on_extract callback)
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
/*fn (zentry mut zip_ptr) extract(path string) bool {
if C.access(path.cstr(), 0) == -1 {
return false
//return error('Cannot open directory for extracting, directory not exists')
}
res := C.zip_extract(zentry, path.cstr(), 0, 0)
return res == 0
}*/
/**
* total returns the number of all entries (files and directories) in the zip archive.
*
* @param zip zip archive handler.
*
* @return the return code - the number of entries on success, negative number
* (< 0) on error.
*/
pub fn (zentry mut zip_ptr) total() ?int {
_tentry := int(C.zip_total_entries(zentry))
if _tentry == -1 {
return error('szip: cannot count total entries.')
}
return _tentry
}

947
vlib/szip/zip.c Normal file
View File

@ -0,0 +1,947 @@
/*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#define __STDC_WANT_LIB_EXT1__ 1
#include <errno.h>
#include <sys/stat.h>
#include <time.h>
#if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \
defined(__MINGW32__)
/* Win32, DOS, MSVC, MSVS */
#include <direct.h>
#define MKDIR(DIRNAME) _mkdir(DIRNAME)
#define STRCLONE(STR) ((STR) ? _strdup(STR) : NULL)
#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)
#else
#include <unistd.h> // needed for symlink() on BSD
int symlink(const char *target, const char *linkpath); // needed on Linux
#define MKDIR(DIRNAME) mkdir(DIRNAME, 0755)
#define STRCLONE(STR) ((STR) ? strdup(STR) : NULL)
#endif
#include "miniz.h"
#include "zip.h"
#ifndef HAS_DEVICE
#define HAS_DEVICE(P) 0
#endif
#ifndef FILESYSTEM_PREFIX_LEN
#define FILESYSTEM_PREFIX_LEN(P) 0
#endif
#ifndef ISSLASH
#define ISSLASH(C) ((C) == '/' || (C) == '\\')
#endif
#define CLEANUP(ptr) \
do { \
if (ptr) { \
free((void *)ptr); \
ptr = NULL; \
} \
} while (0)
static const char *base_name(const char *name) {
char const *p;
char const *base = name += FILESYSTEM_PREFIX_LEN(name);
int all_slashes = 1;
for (p = name; *p; p++) {
if (ISSLASH(*p))
base = p + 1;
else
all_slashes = 0;
}
/* If NAME is all slashes, arrange to return `/'. */
if (*base == '\0' && ISSLASH(*name) && all_slashes)
--base;
return base;
}
static int mkpath(char *path) {
char *p;
char npath[MAX_PATH + 1];
int len = 0;
int has_device = HAS_DEVICE(path);
memset(npath, 0, MAX_PATH + 1);
if (has_device) {
// only on windows
npath[0] = path[0];
npath[1] = path[1];
len = 2;
}
for (p = path + len; *p && len < MAX_PATH; p++) {
if (ISSLASH(*p) && ((!has_device && len > 0) || (has_device && len > 2))) {
#if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \
defined(__MINGW32__)
#else
if ('\\' == *p) {
*p = '/';
}
#endif
if (MKDIR(npath) == -1) {
if (errno != EEXIST) {
return -1;
}
}
}
npath[len++] = *p;
}
return 0;
}
static char *strrpl(const char *str, size_t n, char oldchar, char newchar) {
char c;
size_t i;
char *rpl = (char *)calloc((1 + n), sizeof(char));
char *begin = rpl;
if (!rpl) {
return NULL;
}
for (i = 0; (i < n) && (c = *str++); ++i) {
if (c == oldchar) {
c = newchar;
}
*rpl++ = c;
}
return begin;
}
struct zip_entry_t {
int index;
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;
mz_uint32 external_attr;
time_t m_time;
};
struct zip_t {
mz_zip_archive archive;
mz_uint level;
struct zip_entry_t entry;
};
struct zip_t *zip_open(const char *zipname, int level, char mode) {
struct zip_t *zip = NULL;
if (!zipname || strlen(zipname) < 1) {
// zip_t archive name is empty or NULL
printf("%s", "name len");
goto cleanup;
}
if (level < 0)
level = MZ_DEFAULT_LEVEL;
if ((level & 0xF) > MZ_UBER_COMPRESSION) {
// Wrong compression level
printf("%s", "Wrong compression");
goto cleanup;
}
zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
if (!zip)
printf("%s", "Cannot allocate");
goto cleanup;
zip->level = (mz_uint)level;
switch (mode) {
case 'w':
// Create a new archive.
if (!mz_zip_writer_init_file(&(zip->archive), zipname, 0)) {
printf("%s", "Cannot initialize zip_archive writer");
goto cleanup;
}
break;
case 'r':
case 'a':
if (!mz_zip_reader_init_file(
&(zip->archive), zipname,
zip->level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) {
// An archive file does not exist or cannot initialize
// zip_archive reader
goto cleanup;
}
if (mode == 'a' &&
!mz_zip_writer_init_from_reader(&(zip->archive), zipname)) {
mz_zip_reader_end(&(zip->archive));
goto cleanup;
}
break;
default:
goto cleanup;
}
return zip;
cleanup:
CLEANUP(zip);
return NULL;
}
void zip_close(struct zip_t *zip) {
printf("%s", "try to close");
if (zip) {
// 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));
mz_zip_reader_end(&(zip->archive));
CLEANUP(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_file_stat stats;
if (!zip || !entryname) {
return -1;
}
entrylen = strlen(entryname);
if (entrylen < 1) {
return -1;
}
/*
.ZIP File Format Specification Version: 6.3.3
4.4.17.1 The name of the file, with optional relative path.
The path stored MUST not contain a drive or
device letter, or a leading slash. All slashes
MUST be forward slashes '/' as opposed to
backwards slashes '\' for compatibility with Amiga
and UNIX file systems etc. If input came from standard
input, there is no file name field.
*/
zip->entry.name = strrpl(entryname, entrylen, '\\', '/');
if (!zip->entry.name) {
// Cannot parse zip entry name
return -1;
}
pzip = &(zip->archive);
if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) {
zip->entry.index =
mz_zip_reader_locate_file(pzip, zip->entry.name, NULL, 0);
if (zip->entry.index < 0) {
goto cleanup;
}
if (!mz_zip_reader_file_stat(pzip, (mz_uint)zip->entry.index, &stats)) {
goto cleanup;
}
zip->entry.comp_size = stats.m_comp_size;
zip->entry.uncomp_size = stats.m_uncomp_size;
zip->entry.uncomp_crc32 = stats.m_crc32;
zip->entry.offset = stats.m_central_dir_ofs;
zip->entry.header_offset = stats.m_local_header_ofs;
zip->entry.method = stats.m_method;
zip->entry.external_attr = stats.m_external_attr;
zip->entry.m_time = stats.m_time;
return 0;
}
zip->entry.index = (int)zip->archive.m_total_files;
zip->entry.comp_size = 0;
zip->entry.uncomp_size = 0;
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));
zip->entry.method = 0;
// UNIX or APPLE
#if MZ_PLATFORM == 3 || MZ_PLATFORM == 19
// regular file with rw-r--r-- persmissions
zip->entry.external_attr = (mz_uint32)(0100644) << 16;
#else
zip->entry.external_attr = 0;
#endif
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
goto cleanup;
}
if (zip->level & MZ_ZIP_FLAG_COMPRESSED_DATA) {
// Wrong zip compression level
goto cleanup;
}
// 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)) {
// No zip64 support yet
goto cleanup;
}
if (!mz_zip_writer_write_zeros(pzip, zip->entry.offset,
num_alignment_padding_bytes +
sizeof(zip->entry.header))) {
// Cannot memset zip entry header
goto cleanup;
}
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_pWrite(pzip->m_pIO_opaque, zip->entry.offset, zip->entry.name,
entrylen) != entrylen) {
// Cannot write data to zip entry
goto cleanup;
}
zip->entry.offset += entrylen;
level = zip->level & 0xF;
if (level) {
zip->entry.state.m_pZip = pzip;
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),
(int)tdefl_create_comp_flags_from_zip_params(
(int)level, -15, MZ_DEFAULT_STRATEGY)) !=
TDEFL_STATUS_OKAY) {
// Cannot initialize the zip compressor
goto cleanup;
}
}
zip->entry.m_time = time(NULL);
return 0;
cleanup:
CLEANUP(zip->entry.name);
return -1;
}
int zip_entry_openbyindex(struct zip_t *zip, int index) {
mz_zip_archive *pZip = NULL;
mz_zip_archive_file_stat stats;
mz_uint namelen;
const mz_uint8 *pHeader;
const char *pFilename;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
pZip = &(zip->archive);
if (pZip->m_zip_mode != MZ_ZIP_MODE_READING) {
// open by index requires readonly mode
return -1;
}
if (index < 0 || (mz_uint)index >= pZip->m_total_files) {
// index out of range
return -1;
}
if (!(pHeader = &MZ_ZIP_ARRAY_ELEMENT(
&pZip->m_pState->m_central_dir, mz_uint8,
MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets,
mz_uint32, index)))) {
// cannot find header in central directory
return -1;
}
namelen = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS);
pFilename = (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
/*
.ZIP File Format Specification Version: 6.3.3
4.4.17.1 The name of the file, with optional relative path.
The path stored MUST not contain a drive or
device letter, or a leading slash. All slashes
MUST be forward slashes '/' as opposed to
backwards slashes '\' for compatibility with Amiga
and UNIX file systems etc. If input came from standard
input, there is no file name field.
*/
zip->entry.name = strrpl(pFilename, namelen, '\\', '/');
if (!zip->entry.name) {
// local entry name is NULL
return -1;
}
if (!mz_zip_reader_file_stat(pZip, (mz_uint)index, &stats)) {
return -1;
}
zip->entry.index = index;
zip->entry.comp_size = stats.m_comp_size;
zip->entry.uncomp_size = stats.m_uncomp_size;
zip->entry.uncomp_crc32 = stats.m_crc32;
zip->entry.offset = stats.m_central_dir_ofs;
zip->entry.header_offset = stats.m_local_header_ofs;
zip->entry.method = stats.m_method;
zip->entry.external_attr = stats.m_external_attr;
zip->entry.m_time = stats.m_time;
return 0;
}
int zip_entry_close(struct zip_t *zip) {
mz_zip_archive *pzip = NULL;
mz_uint level;
tdefl_status done;
mz_uint16 entrylen;
mz_uint16 dos_time, dos_date;
int status = -1;
if (!zip) {
// zip_t handler is not initialized
goto cleanup;
}
pzip = &(zip->archive);
if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) {
status = 0;
goto cleanup;
}
level = zip->level & 0xF;
if (level) {
done = tdefl_compress_buffer(&(zip->entry.comp), "", 0, TDEFL_FINISH);
if (done != TDEFL_STATUS_DONE && done != TDEFL_STATUS_OKAY) {
// Cannot flush compressed buffer
goto cleanup;
}
zip->entry.comp_size = zip->entry.state.m_comp_size;
zip->entry.offset = zip->entry.state.m_cur_archive_file_ofs;
zip->entry.method = MZ_DEFLATED;
}
entrylen = (mz_uint16)strlen(zip->entry.name);
// no zip64 support yet
if ((zip->entry.comp_size > 0xFFFFFFFF) || (zip->entry.offset > 0xFFFFFFFF)) {
// No zip64 support, yet
goto cleanup;
}
mz_zip_time_t_to_dos_time(zip->entry.m_time, &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
goto cleanup;
}
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
goto cleanup;
}
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,
zip->entry.external_attr)) {
// Cannot write to zip central dir
goto cleanup;
}
pzip->m_total_files++;
pzip->m_archive_size = zip->entry.offset;
status = 0;
cleanup:
if (zip) {
zip->entry.m_time = 0;
CLEANUP(zip->entry.name);
}
return status;
}
const char *zip_entry_name(struct zip_t *zip) {
if (!zip) {
// zip_t handler is not initialized
return NULL;
}
return zip->entry.name;
}
int zip_entry_index(struct zip_t *zip) {
if (!zip) {
// zip_t handler is not initialized
return -1;
}
return zip->entry.index;
}
int zip_entry_isdir(struct zip_t *zip) {
if (!zip) {
// zip_t handler is not initialized
return -1;
}
if (zip->entry.index < 0) {
// zip entry is not opened
return -1;
}
return (int)mz_zip_reader_is_file_a_directory(&zip->archive,
(mz_uint)zip->entry.index);
}
unsigned long long zip_entry_size(struct zip_t *zip) {
return zip ? zip->entry.uncomp_size : 0;
}
unsigned int zip_entry_crc32(struct zip_t *zip) {
return zip ? zip->entry.uncomp_crc32 : 0;
}
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;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
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);
level = zip->level & 0xF;
if (!level) {
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);
if (status != TDEFL_STATUS_DONE && status != TDEFL_STATUS_OKAY) {
// Cannot compress buffer
return -1;
}
}
}
return 0;
}
int zip_entry_fwrite(struct zip_t *zip, const char *filename) {
int status = 0;
size_t n = 0;
FILE *stream = NULL;
mz_uint8 buf[MZ_ZIP_MAX_IO_BUF_SIZE];
struct MZ_FILE_STAT_STRUCT file_stat;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
memset(buf, 0, MZ_ZIP_MAX_IO_BUF_SIZE);
memset((void *)&file_stat, 0, sizeof(struct MZ_FILE_STAT_STRUCT));
if (MZ_FILE_STAT(filename, &file_stat) != 0) {
// problem getting information - check errno
return -1;
}
if ((file_stat.st_mode & 0200) == 0) {
// MS-DOS read-only attribute
zip->entry.external_attr |= 0x01;
}
zip->entry.external_attr |= (mz_uint32)((file_stat.st_mode & 0xFFFF) << 16);
zip->entry.m_time = file_stat.st_mtime;
#if defined(_MSC_VER)
if (fopen_s(&stream, filename, "rb"))
#else
if (!(stream = fopen(filename, "rb")))
#endif
{
// Cannot open filename
return -1;
}
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;
}
}
fclose(stream);
return status;
}
ssize_t zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize) {
mz_zip_archive *pzip = NULL;
mz_uint idx;
size_t size = 0;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
pzip = &(zip->archive);
if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) {
// the entry is not found or we do not have read access
return -1;
}
idx = (mz_uint)zip->entry.index;
if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
// the entry is a directory
return -1;
}
*buf = mz_zip_reader_extract_to_heap(pzip, idx, &size, 0);
if (*buf && bufsize) {
*bufsize = size;
}
return size;
}
ssize_t zip_entry_noallocread(struct zip_t *zip, void *buf, size_t bufsize) {
mz_zip_archive *pzip = NULL;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
pzip = &(zip->archive);
if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) {
// the entry is not found or we do not have read access
return -1;
}
if (!mz_zip_reader_extract_to_mem_no_alloc(pzip, (mz_uint)zip->entry.index,
buf, bufsize, 0, NULL, 0)) {
return -1;
}
return (ssize_t)zip->entry.uncomp_size;
}
int zip_entry_fread(struct zip_t *zip, const char *filename) {
mz_zip_archive *pzip = NULL;
mz_uint idx;
mz_uint32 xattr = 0;
mz_zip_archive_file_stat info;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
memset((void *)&info, 0, sizeof(mz_zip_archive_file_stat));
pzip = &(zip->archive);
if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) {
// the entry is not found or we do not have read access
return -1;
}
idx = (mz_uint)zip->entry.index;
if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
// the entry is a directory
return -1;
}
if (!mz_zip_reader_extract_to_file(pzip, idx, filename, 0)) {
return -1;
}
#if defined(_MSC_VER)
#else
if (!mz_zip_reader_file_stat(pzip, idx, &info)) {
// Cannot get information about zip archive;
return -1;
}
xattr = (info.m_external_attr >> 16) & 0xFFFF;
if (xattr > 0) {
if (chmod(filename, (mode_t)xattr) < 0) {
return -1;
}
}
#endif
return 0;
}
int zip_entry_extract(struct zip_t *zip,
size_t (*on_extract)(void *arg, unsigned long long offset,
const void *buf, size_t bufsize),
void *arg) {
mz_zip_archive *pzip = NULL;
mz_uint idx;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
pzip = &(zip->archive);
if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) {
// the entry is not found or we do not have read access
return -1;
}
idx = (mz_uint)zip->entry.index;
return (mz_zip_reader_extract_to_callback(pzip, idx, on_extract, arg, 0))
? 0
: -1;
}
int zip_total_entries(struct zip_t *zip) {
if (!zip) {
// zip_t handler is not initialized
return -1;
}
return (int)zip->archive.m_total_files;
}
int zip_create(const char *zipname, const char *filenames[], size_t len) {
int status = 0;
size_t i;
mz_zip_archive zip_archive;
struct MZ_FILE_STAT_STRUCT file_stat;
mz_uint32 ext_attributes = 0;
if (!zipname || strlen(zipname) < 1) {
// zip_t archive name is empty or NULL
return -1;
}
// Create a new archive.
if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
// Cannot memset zip archive
return -1;
}
if (!mz_zip_writer_init_file(&zip_archive, zipname, 0)) {
// Cannot initialize zip_archive writer
return -1;
}
memset((void *)&file_stat, 0, sizeof(struct MZ_FILE_STAT_STRUCT));
for (i = 0; i < len; ++i) {
const char *name = filenames[i];
if (!name) {
status = -1;
break;
}
if (MZ_FILE_STAT(name, &file_stat) != 0) {
// problem getting information - check errno
return -1;
}
if ((file_stat.st_mode & 0200) == 0) {
// MS-DOS read-only attribute
ext_attributes |= 0x01;
}
ext_attributes |= (mz_uint32)((file_stat.st_mode & 0xFFFF) << 16);
if (!mz_zip_writer_add_file(&zip_archive, base_name(name), name, "", 0,
ZIP_DEFAULT_COMPRESSION_LEVEL,
ext_attributes)) {
// Cannot add file to zip_archive
status = -1;
break;
}
}
mz_zip_writer_finalize_archive(&zip_archive);
mz_zip_writer_end(&zip_archive);
return status;
}
int zip_extract(const char *zipname, const char *dir,
int (*on_extract)(const char *filename, void *arg), void *arg) {
int status = -1;
mz_uint i, n;
char path[MAX_PATH + 1];
char symlink_to[MAX_PATH + 1];
mz_zip_archive zip_archive;
mz_zip_archive_file_stat info;
size_t dirlen = 0;
mz_uint32 xattr = 0;
memset(path, 0, sizeof(path));
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);
if (dirlen + 1 > MAX_PATH) {
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));
#if defined(_MSC_VER)
strcpy_s(path, MAX_PATH, dir);
#else
strcpy(path, dir);
#endif
if (!ISSLASH(path[dirlen - 1])) {
#if defined(_WIN32) || defined(__WIN32__)
path[dirlen] = '\\';
#else
path[dirlen] = '/';
#endif
++dirlen;
}
// Get and print information about each file in the archive.
n = mz_zip_reader_get_num_files(&zip_archive);
for (i = 0; i < n; ++i) {
if (!mz_zip_reader_file_stat(&zip_archive, i, &info)) {
// Cannot get information about zip archive;
goto out;
}
#if defined(_MSC_VER)
strncpy_s(&path[dirlen], MAX_PATH - dirlen, info.m_filename,
MAX_PATH - dirlen);
#else
strncpy(&path[dirlen], info.m_filename, MAX_PATH - dirlen);
#endif
if (mkpath(path) < 0) {
// Cannot make a path
goto out;
}
if ((((info.m_version_made_by >> 8) == 3) ||
((info.m_version_made_by >> 8) ==
19)) // if zip is produced on Unix or macOS (3 and 19 from
// section 4.4.2.2 of zip standard)
&& info.m_external_attr &
(0x20 << 24)) { // and has sym link attribute (0x80 is file, 0x40
// is directory)
#if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \
defined(__MINGW32__)
#else
if (info.m_uncomp_size > MAX_PATH ||
!mz_zip_reader_extract_to_mem_no_alloc(&zip_archive, i, symlink_to,
MAX_PATH, 0, NULL, 0)) {
goto out;
}
symlink_to[info.m_uncomp_size] = '\0';
if (symlink(symlink_to, path) != 0) {
goto out;
}
#endif
} else {
if (!mz_zip_reader_is_file_a_directory(&zip_archive, i)) {
if (!mz_zip_reader_extract_to_file(&zip_archive, i, path, 0)) {
// Cannot extract zip archive to file
goto out;
}
}
#if defined(_MSC_VER)
#else
xattr = (info.m_external_attr >> 16) & 0xFFFF;
if (xattr > 0) {
if (chmod(path, (mode_t)xattr) < 0) {
goto out;
}
}
#endif
}
if (on_extract) {
if (on_extract(path, arg) < 0) {
goto out;
}
}
}
status = 0;
out:
// Close the archive, freeing any resources it was using
if (!mz_zip_reader_end(&zip_archive)) {
// Cannot end zip reader
status = -1;
}
return status;
}

302
vlib/szip/zip.h Normal file
View File

@ -0,0 +1,302 @@
/*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#ifndef ZIP_H
#define ZIP_H
#include <string.h>
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
#if !defined(_SSIZE_T_DEFINED) && !defined(_SSIZE_T_DEFINED_) && \
!defined(_SSIZE_T) && !defined(_SSIZE_T_) && !defined(__ssize_t_defined)
#define _SSIZE_T
// 64-bit Windows is the only mainstream platform
// where sizeof(long) != sizeof(void*)
#ifdef _WIN64
typedef long long ssize_t; /* byte count or error */
#else
typedef long ssize_t; /* byte count or error */
#endif
#endif
#ifndef MAX_PATH
#define MAX_PATH 32767 /* # chars in a path name including NULL */
#endif
/**
* @mainpage
*
* Documenation for @ref zip.
*/
/**
* @addtogroup zip
* @{
*/
/**
* Default zip compression level.
*/
#define ZIP_DEFAULT_COMPRESSION_LEVEL 6
/**
* @struct zip_t
*
* This data structure is used throughout the library to represent zip archive -
* forward declaration.
*/
struct zip_t;
/**
* Opens zip archive with compression level using the given mode.
*
* @param zipname zip archive file name.
* @param level compression level (0-9 are the standard zlib-style levels).
* @param mode file access mode.
* - 'r': opens a file for reading/extracting (the file must exists).
* - 'w': creates an empty file for writing.
* - 'a': appends to an existing archive.
*
* @return the zip archive handler or NULL on error
*/
extern struct zip_t *zip_open(const char *zipname, int level, char mode);
/**
* Closes the zip archive, releases resources - always finalize.
*
* @param zip zip archive handler.
*/
extern void zip_close(struct zip_t *zip);
/**
* Opens an entry by name in the zip archive.
*
* For zip archive opened in 'w' or 'a' mode the function will append
* a new entry. In readonly mode the function tries to locate the entry
* in global dictionary.
*
* @param zip zip archive handler.
* @param entryname an entry name in local dictionary.
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_open(struct zip_t *zip, const char *entryname);
/**
* Opens a new entry by index in the zip archive.
*
* This function is only valid if zip archive was opened in 'r' (readonly) mode.
*
* @param zip zip archive handler.
* @param index index in local dictionary.
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_openbyindex(struct zip_t *zip, int index);
/**
* Closes a zip entry, flushes buffer and releases resources.
*
* @param zip zip archive handler.
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_close(struct zip_t *zip);
/**
* Returns a local name of the current zip entry.
*
* The main difference between user's entry name and local entry name
* is optional relative path.
* Following .ZIP File Format Specification - the path stored MUST not contain
* a drive or device letter, or a leading slash.
* All slashes MUST be forward slashes '/' as opposed to backwards slashes '\'
* for compatibility with Amiga and UNIX file systems etc.
*
* @param zip: zip archive handler.
*
* @return the pointer to the current zip entry name, or NULL on error.
*/
extern const char *zip_entry_name(struct zip_t *zip);
/**
* Returns an index of the current zip entry.
*
* @param zip zip archive handler.
*
* @return the index on success, negative number (< 0) on error.
*/
extern int zip_entry_index(struct zip_t *zip);
/**
* Determines if the current zip entry is a directory entry.
*
* @param zip zip archive handler.
*
* @return the return code - 1 (true), 0 (false), negative number (< 0) on
* error.
*/
extern int zip_entry_isdir(struct zip_t *zip);
/**
* Returns an uncompressed size of the current zip entry.
*
* @param zip zip archive handler.
*
* @return the uncompressed size in bytes.
*/
extern unsigned long long zip_entry_size(struct zip_t *zip);
/**
* Returns CRC-32 checksum of the current zip entry.
*
* @param zip zip archive handler.
*
* @return the CRC-32 checksum.
*/
extern unsigned int zip_entry_crc32(struct zip_t *zip);
/**
* Compresses an input buffer for the current zip entry.
*
* @param zip zip archive handler.
* @param buf input buffer.
* @param bufsize input buffer size (in bytes).
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize);
/**
* Compresses a file for the current zip entry.
*
* @param zip zip archive handler.
* @param filename input file.
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_fwrite(struct zip_t *zip, const char *filename);
/**
* Extracts the current zip entry into output buffer.
*
* The function allocates sufficient memory for a output buffer.
*
* @param zip zip archive handler.
* @param buf output buffer.
* @param bufsize output buffer size (in bytes).
*
* @note remember to release memory allocated for a output buffer.
* for large entries, please take a look at zip_entry_extract function.
*
* @return the return code - the number of bytes actually read on success.
* Otherwise a -1 on error.
*/
extern ssize_t zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize);
/**
* Extracts the current zip entry into a memory buffer using no memory
* allocation.
*
* @param zip zip archive handler.
* @param buf preallocated output buffer.
* @param bufsize output buffer size (in bytes).
*
* @note ensure supplied output buffer is large enough.
* zip_entry_size function (returns uncompressed size for the current
* entry) can be handy to estimate how big buffer is needed. for large
* entries, please take a look at zip_entry_extract function.
*
* @return the return code - the number of bytes actually read on success.
* Otherwise a -1 on error (e.g. bufsize is not large enough).
*/
extern ssize_t zip_entry_noallocread(struct zip_t *zip, void *buf,
size_t bufsize);
/**
* Extracts the current zip entry into output file.
*
* @param zip zip archive handler.
* @param filename output file.
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_entry_fread(struct zip_t *zip, const char *filename);
/**
* Extracts the current zip entry using a callback function (on_extract).
*
* @param zip zip archive handler.
* @param on_extract callback function.
* @param arg opaque pointer (optional argument, which you can pass to the
* on_extract callback)
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
extern int
zip_entry_extract(struct zip_t *zip,
size_t (*on_extract)(void *arg, unsigned long long offset,
const void *data, size_t size),
void *arg);
/**
* Returns the number of all entries (files and directories) in the zip archive.
*
* @param zip zip archive handler.
*
* @return the return code - the number of entries on success, negative number
* (< 0) on error.
*/
extern int zip_total_entries(struct zip_t *zip);
/**
* Creates a new archive and puts files into a single zip archive.
*
* @param zipname zip archive file.
* @param filenames input files.
* @param len: number of input files.
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_create(const char *zipname, const char *filenames[], size_t len);
/**
* Extracts a zip archive file into directory.
*
* If on_extract_entry 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_entry callback.
*
* @param zipname zip archive file.
* @param dir output directory.
* @param on_extract_entry on extract callback.
* @param arg opaque pointer.
*
* @return the return code - 0 on success, negative number (< 0) on error.
*/
extern int zip_extract(const char *zipname, const char *dir,
int (*on_extract_entry)(const char *filename, void *arg),
void *arg);
/** @} */
#ifdef __cplusplus
}
#endif
#endif