| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include "mz.h" |
| 14 |
#include "mz_strm.h" |
| 15 |
#include "mz_strm_mem.h" |
| 16 |
#include "mz_zip.h" |
| 17 |
|
| 18 |
#ifdef __cplusplus |
| 19 |
extern "C" { |
| 20 |
#endif |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#define MZ_FUZZ_TEST_PWD "test123" |
| 25 |
#define MZ_FUZZ_TEST_FILENAME "foo" |
| 26 |
#define MZ_FUZZ_TEST_FILENAMEUC "FOO" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 31 |
mz_zip_file *file_info = NULL; |
| 32 |
void *stream = NULL; |
| 33 |
void *handle = NULL; |
| 34 |
const char *archive_comment = NULL; |
| 35 |
char buffer[1024]; |
| 36 |
uint16_t version_madeby = 0; |
| 37 |
uint64_t num_entries = 0; |
| 38 |
int64_t entry_pos = 0; |
| 39 |
int32_t err = MZ_OK; |
| 40 |
uint8_t encrypted = 0; |
| 41 |
|
| 42 |
stream = mz_stream_mem_create(); |
| 43 |
if (!stream) |
| 44 |
return 1; |
| 45 |
|
| 46 |
mz_stream_mem_set_buffer(stream, (void *)data, (int32_t)size); |
| 47 |
|
| 48 |
handle = mz_zip_create(); |
| 49 |
if (!handle) |
| 50 |
return 1; |
| 51 |
|
| 52 |
mz_zip_set_recover(handle, (size & 0xE0) == 0xE0); |
| 53 |
err = mz_zip_open(handle, stream, MZ_OPEN_MODE_READ); |
| 54 |
|
| 55 |
if (err == MZ_OK) { |
| 56 |
|
| 57 |
mz_zip_get_comment(handle, &archive_comment); |
| 58 |
mz_zip_get_version_madeby(handle, &version_madeby); |
| 59 |
mz_zip_get_number_entry(handle, &num_entries); |
| 60 |
|
| 61 |
err = mz_zip_goto_first_entry(handle); |
| 62 |
while (err == MZ_OK) { |
| 63 |
err = mz_zip_entry_get_info(handle, &file_info); |
| 64 |
if (err != MZ_OK) |
| 65 |
break; |
| 66 |
|
| 67 |
encrypted = (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED); |
| 68 |
|
| 69 |
err = mz_zip_entry_read_open(handle, 0, encrypted ? MZ_FUZZ_TEST_PWD : NULL); |
| 70 |
if (err != MZ_OK) |
| 71 |
break; |
| 72 |
|
| 73 |
err = mz_zip_entry_is_open(handle); |
| 74 |
if (err != MZ_OK) |
| 75 |
break; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
mz_zip_entry_is_dir(handle); |
| 81 |
entry_pos = mz_zip_get_entry(handle); |
| 82 |
if (entry_pos < 0) |
| 83 |
break; |
| 84 |
|
| 85 |
err = mz_zip_entry_read(handle, buffer, sizeof(buffer)); |
| 86 |
if (err < 0) |
| 87 |
break; |
| 88 |
|
| 89 |
err = mz_zip_entry_close(handle); |
| 90 |
if (err != MZ_OK) |
| 91 |
break; |
| 92 |
|
| 93 |
err = mz_zip_goto_next_entry(handle); |
| 94 |
} |
| 95 |
|
| 96 |
mz_zip_entry_close(handle); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
mz_zip_locate_entry(handle, MZ_FUZZ_TEST_FILENAME, 0); |
| 102 |
mz_zip_locate_entry(handle, MZ_FUZZ_TEST_FILENAMEUC, 0); |
| 103 |
mz_zip_locate_entry(handle, MZ_FUZZ_TEST_FILENAME, 1); |
| 104 |
mz_zip_locate_entry(handle, MZ_FUZZ_TEST_FILENAMEUC, 1); |
| 105 |
|
| 106 |
mz_zip_close(handle); |
| 107 |
} |
| 108 |
|
| 109 |
mz_zip_delete(&handle); |
| 110 |
mz_stream_mem_delete(&stream); |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
#ifdef __cplusplus |
| 118 |
} |
| 119 |
#endif |