Skip to content

Commit

Permalink
Add error lookup function
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-- committed Mar 13, 2021
1 parent f0e5c7b commit 252d6c1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
67 changes: 56 additions & 11 deletions src/zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,61 @@ struct zip_t {
struct zip_entry_t entry;
};

enum modify_t {
enum zip_modify_t {
MZ_KEEP = 0,
MZ_DELETE = 1,
MZ_MOVE = 2,
};

struct entry_mark_t {
struct zip_entry_mark_t {
int file_index;
enum modify_t type;
enum zip_modify_t type;
mz_uint64 m_local_header_ofs;
mz_uint64 lf_length;
};

static const char *const zip_errlist[30] = {
NULL,
"not initialized\0",
"invalid entry name\0",
"entry not found\0",
"invalid zip mode\0",
"invalid compression level\0",
"no zip 64 support\0",
"memset error\0",
"cannot write data to entry\0",
"cannot initialize tdefl compressor\0",
"invalid index\0",
"header not found\0",
"cannot flush tdefl buffer\0",
"cannot write entry header\0",
"cannot create entry header\0",
"cannot write to central dir\0",
"cannot open file\0",
"invalid entry type\0",
"extracting data using no memory allocation\0",
"file not found\0",
"no permission\0",
"out of memory\0",
"invalid zip archive name\0",
"make dir error\0"
"symlink error\0"
"close archive error\0"
"capacity size too small\0",
"fseek error\0",
"fread error\0",
"fwrite error\0",
};

const char *zip_strerror(int errnum) {
errnum = -errnum;
if (errnum <= 0 || errnum >= 30) {
return NULL;
}

return zip_errlist[errnum];
}

static const char *zip_basename(const char *name) {
char const *p;
char const *base = name += FILESYSTEM_PREFIX_LEN(name);
Expand Down Expand Up @@ -366,8 +408,9 @@ static inline void zip_archive_finalize(mz_zip_archive *pzip) {
zip_archive_truncate(pzip);
}

static int zip_entry_mark(struct zip_t *zip, struct entry_mark_t *entry_mark,
int n, char *const entries[], const size_t len) {
static int zip_entry_mark(struct zip_t *zip,
struct zip_entry_mark_t *entry_mark, int n,
char *const entries[], const size_t len) {
int err = 0;
if (!zip || !entry_mark || !entries) {
return ZIP_ENOINIT;
Expand Down Expand Up @@ -441,7 +484,7 @@ static int zip_sort(mz_uint64 *local_header_ofs_array, int cur_index) {
return nxt_index;
}

static int zip_index_update(struct entry_mark_t *entry_mark, int last_index,
static int zip_index_update(struct zip_entry_mark_t *entry_mark, int last_index,
int nxt_index) {
for (int j = 0; j < last_index; j++) {
if (entry_mark[j].file_index >= nxt_index) {
Expand All @@ -453,7 +496,8 @@ static int zip_index_update(struct entry_mark_t *entry_mark, int last_index,
}

static int zip_entry_finalize(struct zip_t *zip,
struct entry_mark_t *entry_mark, const int n) {
struct zip_entry_mark_t *entry_mark,
const int n) {

mz_uint64 *local_header_ofs_array = (mz_uint64 *)calloc(n, sizeof(mz_uint64));
if (!local_header_ofs_array) {
Expand Down Expand Up @@ -489,7 +533,7 @@ static int zip_entry_finalize(struct zip_t *zip,
return 0;
}

static int zip_entry_set(struct zip_t *zip, struct entry_mark_t *entry_mark,
static int zip_entry_set(struct zip_t *zip, struct zip_entry_mark_t *entry_mark,
int n, char *const entries[], const size_t len) {
int err = 0;

Expand Down Expand Up @@ -660,7 +704,7 @@ static int zip_central_dir_delete(mz_zip_internal_state *pState,
}

static int zip_entries_delete_mark(struct zip_t *zip,
struct entry_mark_t *entry_mark,
struct zip_entry_mark_t *entry_mark,
int entry_num) {
mz_uint64 writen_num = 0;
mz_uint64 read_num = 0;
Expand Down Expand Up @@ -1383,7 +1427,7 @@ int zip_entries_delete(struct zip_t *zip, char *const entries[],
const size_t len) {
int n = 0;
int err = 0;
struct entry_mark_t *entry_mark = NULL;
struct zip_entry_mark_t *entry_mark = NULL;

if (zip == NULL || (entries == NULL && len != 0)) {
return ZIP_ENOINIT;
Expand All @@ -1395,7 +1439,8 @@ int zip_entries_delete(struct zip_t *zip, char *const entries[],

n = zip_entries_total(zip);

entry_mark = (struct entry_mark_t *)calloc(n, sizeof(struct entry_mark_t));
entry_mark =
(struct zip_entry_mark_t *)calloc(n, sizeof(struct zip_entry_mark_t));
if (!entry_mark) {
return ZIP_EOOMEM;
}
Expand Down
14 changes: 11 additions & 3 deletions src/zip.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ typedef long ssize_t; /* byte count or error */
#define ZIP_EINVMODE -4 // invalid zip mode
#define ZIP_EINVLVL -5 // invalid compression level
#define ZIP_ENOSUP64 -6 // no zip 64 support
#define ZIP_EMEMSET -7 // cannot memset
#define ZIP_EMEMSET -7 // memset error
#define ZIP_EWRTENT -8 // cannot write data to entry
#define ZIP_ETDEFLINIT -9 // cannot initialize tdefl compressor
#define ZIP_EINVIDX -10 // invalid index
Expand All @@ -76,12 +76,20 @@ typedef long ssize_t; /* byte count or error */
#define ZIP_EINVZIPNAME -22 // invalid zip archive name
#define ZIP_EMKDIR -23 // make dir error
#define ZIP_ESYMLINK -24 // symlink error
#define ZIP_ECLSZIP -25 // close the archive error
#define ZIP_ECAPSIZE -26 // capacity size issue
#define ZIP_ECLSZIP -25 // close archive error
#define ZIP_ECAPSIZE -26 // capacity size too small
#define ZIP_EFSEEK -27 // fseek error
#define ZIP_EFREAD -28 // fread error
#define ZIP_EFWRITE -29 // fwrite error

/**
* Looks up the error message string coresponding to an error number.
* @param errnum error number
* @return error message string coresponding to errnum or NULL if error is not
* found.
*/
extern const char *zip_strerror(int errnum);

/**
* @struct zip_t
*
Expand Down
6 changes: 6 additions & 0 deletions test/test_entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,23 @@ MU_TEST(test_entries_delete) {

mu_assert_int_eq(ZIP_ENOENT, zip_entry_open(zip, "delete.me"));
mu_assert_int_eq(0, zip_entry_close(zip));
fprintf(stdout, "delete.me: %s\n", zip_strerror(ZIP_ENOENT));

mu_assert_int_eq(ZIP_ENOENT, zip_entry_open(zip, "_"));
mu_assert_int_eq(0, zip_entry_close(zip));
fprintf(stdout, "_: %s\n", zip_strerror(ZIP_ENOENT));

mu_assert_int_eq(ZIP_ENOENT, zip_entry_open(zip, "delete/file.1"));
mu_assert_int_eq(0, zip_entry_close(zip));
fprintf(stdout, "delete/file.1: %s\n", zip_strerror(ZIP_ENOENT));

mu_assert_int_eq(ZIP_ENOENT, zip_entry_open(zip, "deleteme/file.3"));
mu_assert_int_eq(0, zip_entry_close(zip));
fprintf(stdout, "delete/file.3: %s\n", zip_strerror(ZIP_ENOENT));

mu_assert_int_eq(ZIP_ENOENT, zip_entry_open(zip, "delete/file.2"));
mu_assert_int_eq(0, zip_entry_close(zip));
fprintf(stdout, "delete/file.2: %s\n", zip_strerror(ZIP_ENOENT));

mu_assert_int_eq(total_entries - 5, zip_entries_total(zip));

Expand Down
1 change: 1 addition & 0 deletions test/test_extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ MU_TEST(test_extract_stream) {
zip_extract("non_existing_directory/non_existing_archive.zip", ".", NULL,
NULL));
mu_assert_int_eq(ZIP_ENOINIT, zip_stream_extract("", 0, ".", NULL, NULL));
fprintf(stdout, "zip_stream_extract: %s\n", zip_strerror(ZIP_ENOINIT));

FILE *fp = NULL;
#if defined(_MSC_VER)
Expand Down

0 comments on commit 252d6c1

Please sign in to comment.