123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103 |
- /* mz_zip_rw.c -- Zip reader/writer
- Version 2.9.2, February 12, 2020
- part of the MiniZip project
- Copyright (C) 2010-2020 Nathan Moinvaziri
- https://github.com/nmoinvaz/minizip
- This program is distributed under the terms of the same license as zlib.
- See the accompanying LICENSE file for the full text of the license.
- */
- #include "mz.h"
- #include "mz_crypt.h"
- #include "mz_os.h"
- #include "mz_strm.h"
- #include "mz_strm_buf.h"
- #include "mz_strm_mem.h"
- #include "mz_strm_os.h"
- #include "mz_strm_split.h"
- #include "mz_strm_wzaes.h"
- #include "mz_zip.h"
- #include "mz_zip_rw.h"
- /***************************************************************************/
- #define MZ_DEFAULT_PROGRESS_INTERVAL (1000u)
- #define MZ_ZIP_CD_FILENAME ("__cdcd__")
- /***************************************************************************/
- typedef struct mz_zip_reader_s {
- void *zip_handle;
- void *file_stream;
- void *buffered_stream;
- void *split_stream;
- void *mem_stream;
- void *hash;
- uint16_t hash_algorithm;
- uint16_t hash_digest_size;
- mz_zip_file *file_info;
- const char *pattern;
- uint8_t pattern_ignore_case;
- const char *password;
- void *overwrite_userdata;
- mz_zip_reader_overwrite_cb
- overwrite_cb;
- void *password_userdata;
- mz_zip_reader_password_cb
- password_cb;
- void *progress_userdata;
- mz_zip_reader_progress_cb
- progress_cb;
- uint32_t progress_cb_interval_ms;
- void *entry_userdata;
- mz_zip_reader_entry_cb
- entry_cb;
- uint8_t raw;
- uint8_t buffer[UINT16_MAX];
- int32_t encoding;
- uint8_t sign_required;
- uint8_t cd_verified;
- uint8_t cd_zipped;
- uint8_t entry_verified;
- } mz_zip_reader;
- /***************************************************************************/
- int32_t mz_zip_reader_is_open(void *handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- if (reader == NULL)
- return MZ_PARAM_ERROR;
- if (reader->zip_handle == NULL)
- return MZ_PARAM_ERROR;
- return MZ_OK;
- }
- int32_t mz_zip_reader_open(void *handle, void *stream)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- reader->cd_verified = 0;
- reader->cd_zipped = 0;
- mz_zip_create(&reader->zip_handle);
- mz_zip_set_recover(reader->zip_handle, 1);
- err = mz_zip_open(reader->zip_handle, stream, MZ_OPEN_MODE_READ);
- if (err != MZ_OK)
- {
- mz_zip_reader_close(handle);
- return err;
- }
- mz_zip_reader_unzip_cd(reader);
- return MZ_OK;
- }
- int32_t mz_zip_reader_open_file(void *handle, const char *path)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- mz_zip_reader_close(handle);
- mz_stream_os_create(&reader->file_stream);
- mz_stream_buffered_create(&reader->buffered_stream);
- mz_stream_split_create(&reader->split_stream);
- mz_stream_set_base(reader->buffered_stream, reader->file_stream);
- mz_stream_set_base(reader->split_stream, reader->buffered_stream);
- err = mz_stream_open(reader->split_stream, path, MZ_OPEN_MODE_READ);
- if (err == MZ_OK)
- err = mz_zip_reader_open(handle, reader->split_stream);
- return err;
- }
- int32_t mz_zip_reader_open_file_in_memory(void *handle, const char *path)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- void *file_stream = NULL;
- int64_t file_size = 0;
- int32_t err = 0;
- mz_zip_reader_close(handle);
- mz_stream_os_create(&file_stream);
- err = mz_stream_os_open(file_stream, path, MZ_OPEN_MODE_READ);
- if (err != MZ_OK)
- {
- mz_stream_os_delete(&file_stream);
- mz_zip_reader_close(handle);
- return err;
- }
- mz_stream_os_seek(file_stream, 0, MZ_SEEK_END);
- file_size = mz_stream_os_tell(file_stream);
- mz_stream_os_seek(file_stream, 0, MZ_SEEK_SET);
- if ((file_size <= 0) || (file_size > UINT32_MAX))
- {
- /* Memory size is too large or too small */
- mz_stream_os_close(file_stream);
- mz_stream_os_delete(&file_stream);
- mz_zip_reader_close(handle);
- return MZ_MEM_ERROR;
- }
- mz_stream_mem_create(&reader->mem_stream);
- mz_stream_mem_set_grow_size(reader->mem_stream, (int32_t)file_size);
- mz_stream_mem_open(reader->mem_stream, NULL, MZ_OPEN_MODE_CREATE);
- err = mz_stream_copy(reader->mem_stream, file_stream, (int32_t)file_size);
- mz_stream_os_close(file_stream);
- mz_stream_os_delete(&file_stream);
- if (err == MZ_OK)
- err = mz_zip_reader_open(handle, reader->mem_stream);
- if (err != MZ_OK)
- mz_zip_reader_close(handle);
- return err;
- }
- int32_t mz_zip_reader_open_buffer(void *handle, uint8_t *buf, int32_t len, uint8_t copy)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- mz_zip_reader_close(handle);
- mz_stream_mem_create(&reader->mem_stream);
- if (copy)
- {
- mz_stream_mem_set_grow_size(reader->mem_stream, len);
- mz_stream_mem_open(reader->mem_stream, NULL, MZ_OPEN_MODE_CREATE);
- mz_stream_mem_write(reader->mem_stream, buf, len);
- mz_stream_mem_seek(reader->mem_stream, 0, MZ_SEEK_SET);
- }
- else
- {
- mz_stream_mem_open(reader->mem_stream, NULL, MZ_OPEN_MODE_READ);
- mz_stream_mem_set_buffer(reader->mem_stream, buf, len);
- }
- if (err == MZ_OK)
- err = mz_zip_reader_open(handle, reader->mem_stream);
- return err;
- }
- int32_t mz_zip_reader_close(void *handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- if (reader->zip_handle != NULL)
- {
- err = mz_zip_close(reader->zip_handle);
- mz_zip_delete(&reader->zip_handle);
- }
- if (reader->split_stream != NULL)
- {
- mz_stream_split_close(reader->split_stream);
- mz_stream_split_delete(&reader->split_stream);
- }
- if (reader->buffered_stream != NULL)
- mz_stream_buffered_delete(&reader->buffered_stream);
- if (reader->file_stream != NULL)
- mz_stream_os_delete(&reader->file_stream);
- if (reader->mem_stream != NULL)
- {
- mz_stream_mem_close(reader->mem_stream);
- mz_stream_mem_delete(&reader->mem_stream);
- }
- return err;
- }
- /***************************************************************************/
- int32_t mz_zip_reader_unzip_cd(void *handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- mz_zip_file *cd_info = NULL;
- void *cd_mem_stream = NULL;
- void *new_cd_stream = NULL;
- void *file_extra_stream = NULL;
- uint64_t number_entry = 0;
- int32_t err = MZ_OK;
- err = mz_zip_reader_goto_first_entry(handle);
- if (err != MZ_OK)
- return err;
- err = mz_zip_reader_entry_get_info(handle, &cd_info);
- if (err != MZ_OK)
- return err;
- if (strcmp(cd_info->filename, MZ_ZIP_CD_FILENAME) != 0)
- return mz_zip_reader_goto_first_entry(handle);
- err = mz_zip_reader_entry_open(handle);
- if (err != MZ_OK)
- return err;
- mz_stream_mem_create(&file_extra_stream);
- mz_stream_mem_set_buffer(file_extra_stream, (void *)cd_info->extrafield, cd_info->extrafield_size);
- err = mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_CDCD, NULL);
- if (err == MZ_OK)
- err = mz_stream_read_uint64(file_extra_stream, &number_entry);
- mz_stream_mem_delete(&file_extra_stream);
- if (err != MZ_OK)
- return err;
- mz_zip_get_cd_mem_stream(reader->zip_handle, &cd_mem_stream);
- if (mz_stream_mem_is_open(cd_mem_stream) != MZ_OK)
- mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
- err = mz_stream_seek(cd_mem_stream, 0, MZ_SEEK_SET);
- if (err == MZ_OK)
- err = mz_stream_copy_stream(cd_mem_stream, NULL, handle, mz_zip_reader_entry_read,
- (int32_t)cd_info->uncompressed_size);
- if (err == MZ_OK)
- {
- reader->cd_zipped = 1;
- mz_zip_set_cd_stream(reader->zip_handle, 0, cd_mem_stream);
- mz_zip_set_number_entry(reader->zip_handle, number_entry);
- err = mz_zip_reader_goto_first_entry(handle);
- }
- reader->cd_verified = reader->entry_verified;
- mz_stream_mem_delete(&new_cd_stream);
- return err;
- }
- /***************************************************************************/
- static int32_t mz_zip_reader_locate_entry_cb(void *handle, void *userdata, mz_zip_file *file_info)
- {
- mz_zip_reader *reader = (mz_zip_reader *)userdata;
- int32_t result = 0;
- MZ_UNUSED(handle);
- result = mz_path_compare_wc(file_info->filename, reader->pattern, reader->pattern_ignore_case);
- return result;
- }
- int32_t mz_zip_reader_goto_first_entry(void *handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- if (mz_zip_reader_is_open(handle) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (mz_zip_entry_is_open(reader->zip_handle) == MZ_OK)
- mz_zip_reader_entry_close(handle);
- if (reader->pattern == NULL)
- err = mz_zip_goto_first_entry(reader->zip_handle);
- else
- err = mz_zip_locate_first_entry(reader->zip_handle, reader, mz_zip_reader_locate_entry_cb);
- reader->file_info = NULL;
- if (err == MZ_OK)
- err = mz_zip_entry_get_info(reader->zip_handle, &reader->file_info);
- return err;
- }
- int32_t mz_zip_reader_goto_next_entry(void *handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- if (mz_zip_reader_is_open(handle) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (mz_zip_entry_is_open(reader->zip_handle) == MZ_OK)
- mz_zip_reader_entry_close(handle);
- if (reader->pattern == NULL)
- err = mz_zip_goto_next_entry(reader->zip_handle);
- else
- err = mz_zip_locate_next_entry(reader->zip_handle, reader, mz_zip_reader_locate_entry_cb);
- reader->file_info = NULL;
- if (err == MZ_OK)
- err = mz_zip_entry_get_info(reader->zip_handle, &reader->file_info);
- return err;
- }
- int32_t mz_zip_reader_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- if (mz_zip_entry_is_open(reader->zip_handle) == MZ_OK)
- mz_zip_reader_entry_close(handle);
- err = mz_zip_locate_entry(reader->zip_handle, filename, ignore_case);
- reader->file_info = NULL;
- if (err == MZ_OK)
- err = mz_zip_entry_get_info(reader->zip_handle, &reader->file_info);
- return err;
- }
- /***************************************************************************/
- int32_t mz_zip_reader_entry_open(void *handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- const char *password = NULL;
- char password_buf[120];
- reader->entry_verified = 0;
- if (mz_zip_reader_is_open(reader) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (reader->file_info == NULL)
- return MZ_PARAM_ERROR;
- /* If the entry isn't open for reading, open it */
- if (mz_zip_entry_is_open(reader->zip_handle) == MZ_OK)
- return MZ_OK;
- password = reader->password;
- /* Check if we need a password and ask for it if we need to */
- if ((reader->file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) &&
- (reader->password_cb != NULL))
- {
- reader->password_cb(handle, reader->password_userdata, reader->file_info,
- password_buf, sizeof(password_buf));
- password = password_buf;
- }
- err = mz_zip_entry_read_open(reader->zip_handle, reader->raw, password);
- #ifndef MZ_ZIP_NO_ENCRYPTION
- if (err != MZ_OK)
- return err;
- if (mz_zip_reader_entry_get_first_hash(handle, &reader->hash_algorithm, &reader->hash_digest_size) == MZ_OK)
- {
- mz_crypt_sha_create(&reader->hash);
- if (reader->hash_algorithm == MZ_HASH_SHA1)
- mz_crypt_sha_set_algorithm(reader->hash, MZ_HASH_SHA1);
- else if (reader->hash_algorithm == MZ_HASH_SHA256)
- mz_crypt_sha_set_algorithm(reader->hash, MZ_HASH_SHA256);
- else
- err = MZ_SUPPORT_ERROR;
- if (err == MZ_OK)
- mz_crypt_sha_begin(reader->hash);
- #ifdef MZ_ZIP_SIGNING
- if (err == MZ_OK)
- {
- if (mz_zip_reader_entry_has_sign(handle) == MZ_OK)
- {
- err = mz_zip_reader_entry_sign_verify(handle);
- if (err == MZ_OK)
- reader->entry_verified = 1;
- }
- else if (reader->sign_required && !reader->cd_verified)
- err = MZ_SIGN_ERROR;
- }
- #endif
- }
- else if (reader->sign_required && !reader->cd_verified)
- err = MZ_SIGN_ERROR;
- #endif
- return err;
- }
- int32_t mz_zip_reader_entry_close(void *handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- int32_t err_close = MZ_OK;
- #ifndef MZ_ZIP_NO_ENCRYPTION
- int32_t err_hash = MZ_OK;
- uint8_t computed_hash[MZ_HASH_MAX_SIZE];
- uint8_t expected_hash[MZ_HASH_MAX_SIZE];
- if (reader->hash != NULL)
- {
- mz_crypt_sha_end(reader->hash, computed_hash, sizeof(computed_hash));
- mz_crypt_sha_delete(&reader->hash);
- err_hash = mz_zip_reader_entry_get_hash(handle, reader->hash_algorithm, expected_hash,
- reader->hash_digest_size);
- if (err_hash == MZ_OK)
- {
- /* Verify expected hash against computed hash */
- if (memcmp(computed_hash, expected_hash, reader->hash_digest_size) != 0)
- err = MZ_CRC_ERROR;
- }
- }
- #endif
- err_close = mz_zip_entry_close(reader->zip_handle);
- if (err == MZ_OK)
- err = err_close;
- return err;
- }
- int32_t mz_zip_reader_entry_read(void *handle, void *buf, int32_t len)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t read = 0;
- read = mz_zip_entry_read(reader->zip_handle, buf, len);
- #ifndef MZ_ZIP_NO_ENCRYPTION
- if ((read > 0) && (reader->hash != NULL))
- mz_crypt_sha_update(reader->hash, buf, read);
- #endif
- return read;
- }
- int32_t mz_zip_reader_entry_has_sign(void *handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- if (reader == NULL || mz_zip_entry_is_open(reader->zip_handle) != MZ_OK)
- return MZ_PARAM_ERROR;
- return mz_zip_extrafield_contains(reader->file_info->extrafield,
- reader->file_info->extrafield_size, MZ_ZIP_EXTENSION_SIGN, NULL);
- }
- #if !defined(MZ_ZIP_NO_ENCRYPTION) && defined(MZ_ZIP_SIGNING)
- int32_t mz_zip_reader_entry_sign_verify(void *handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- void *file_extra_stream = NULL;
- int32_t err = MZ_OK;
- uint8_t *signature = NULL;
- uint16_t signature_size = 0;
- uint8_t hash[MZ_HASH_MAX_SIZE];
- if (reader == NULL || mz_zip_entry_is_open(reader->zip_handle) != MZ_OK)
- return MZ_PARAM_ERROR;
- mz_stream_mem_create(&file_extra_stream);
- mz_stream_mem_set_buffer(file_extra_stream, (void *)reader->file_info->extrafield,
- reader->file_info->extrafield_size);
- err = mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_SIGN, &signature_size);
- if ((err == MZ_OK) && (signature_size > 0))
- {
- signature = (uint8_t *)MZ_ALLOC(signature_size);
- if (mz_stream_read(file_extra_stream, signature, signature_size) != signature_size)
- err = MZ_READ_ERROR;
- }
- mz_stream_mem_delete(&file_extra_stream);
- if (err == MZ_OK)
- {
- /* Get most secure hash to verify signature against */
- err = mz_zip_reader_entry_get_hash(handle, reader->hash_algorithm, hash, reader->hash_digest_size);
- }
- if (err == MZ_OK)
- {
- /* Verify the pkcs signature */
- err = mz_crypt_sign_verify(hash, reader->hash_digest_size, signature, signature_size);
- }
- if (signature != NULL)
- MZ_FREE(signature);
- return err;
- }
- #endif
- int32_t mz_zip_reader_entry_get_hash(void *handle, uint16_t algorithm, uint8_t *digest, int32_t digest_size)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- void *file_extra_stream = NULL;
- int32_t err = MZ_OK;
- int32_t return_err = MZ_EXIST_ERROR;
- uint16_t cur_algorithm = 0;
- uint16_t cur_digest_size = 0;
- mz_stream_mem_create(&file_extra_stream);
- mz_stream_mem_set_buffer(file_extra_stream, (void *)reader->file_info->extrafield,
- reader->file_info->extrafield_size);
- do
- {
- err = mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_HASH, NULL);
- if (err != MZ_OK)
- break;
- err = mz_stream_read_uint16(file_extra_stream, &cur_algorithm);
- if (err == MZ_OK)
- err = mz_stream_read_uint16(file_extra_stream, &cur_digest_size);
- if ((err == MZ_OK) && (cur_algorithm == algorithm) && (cur_digest_size <= digest_size) &&
- (cur_digest_size <= MZ_HASH_MAX_SIZE))
- {
- /* Read hash digest */
- if (mz_stream_read(file_extra_stream, digest, digest_size) == cur_digest_size)
- return_err = MZ_OK;
- break;
- }
- else
- {
- err = mz_stream_seek(file_extra_stream, cur_digest_size, MZ_SEEK_CUR);
- }
- }
- while (err == MZ_OK);
- mz_stream_mem_delete(&file_extra_stream);
- return return_err;
- }
- int32_t mz_zip_reader_entry_get_first_hash(void *handle, uint16_t *algorithm, uint16_t *digest_size)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- void *file_extra_stream = NULL;
- int32_t err = MZ_OK;
- uint16_t cur_algorithm = 0;
- uint16_t cur_digest_size = 0;
- if (reader == NULL || algorithm == NULL)
- return MZ_PARAM_ERROR;
- mz_stream_mem_create(&file_extra_stream);
- mz_stream_mem_set_buffer(file_extra_stream, (void *)reader->file_info->extrafield,
- reader->file_info->extrafield_size);
- err = mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_HASH, NULL);
- if (err == MZ_OK)
- err = mz_stream_read_uint16(file_extra_stream, &cur_algorithm);
- if (err == MZ_OK)
- err = mz_stream_read_uint16(file_extra_stream, &cur_digest_size);
- if (algorithm != NULL)
- *algorithm = cur_algorithm;
- if (digest_size != NULL)
- *digest_size = cur_digest_size;
- mz_stream_mem_delete(&file_extra_stream);
- return err;
- }
- int32_t mz_zip_reader_entry_get_info(void *handle, mz_zip_file **file_info)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- if (file_info == NULL || mz_zip_reader_is_open(handle) != MZ_OK)
- return MZ_PARAM_ERROR;
- *file_info = reader->file_info;
- if (*file_info == NULL)
- return MZ_EXIST_ERROR;
- return err;
- }
- int32_t mz_zip_reader_entry_is_dir(void *handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- if (mz_zip_reader_is_open(handle) != MZ_OK)
- return MZ_PARAM_ERROR;
- return mz_zip_entry_is_dir(reader->zip_handle);
- }
- int32_t mz_zip_reader_entry_save_process(void *handle, void *stream, mz_stream_write_cb write_cb)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- int32_t read = 0;
- int32_t written = 0;
- if (mz_zip_reader_is_open(reader) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (reader->file_info == NULL)
- return MZ_PARAM_ERROR;
- if (write_cb == NULL)
- return MZ_PARAM_ERROR;
- /* If the entry isn't open for reading, open it */
- if (mz_zip_entry_is_open(reader->zip_handle) != MZ_OK)
- err = mz_zip_reader_entry_open(handle);
- if (err != MZ_OK)
- return err;
- /* Unzip entry in zip file */
- read = mz_zip_reader_entry_read(handle, reader->buffer, sizeof(reader->buffer));
- if (read == 0)
- {
- /* If we are done close the entry */
- err = mz_zip_reader_entry_close(handle);
- if (err != MZ_OK)
- return err;
- return MZ_END_OF_STREAM;
- }
- if (read > 0)
- {
- /* Write the data to the specified stream */
- written = write_cb(stream, reader->buffer, read);
- if (written != read)
- return MZ_WRITE_ERROR;
- }
- return read;
- }
- int32_t mz_zip_reader_entry_save(void *handle, void *stream, mz_stream_write_cb write_cb)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- uint64_t current_time = 0;
- uint64_t update_time = 0;
- int64_t current_pos = 0;
- int64_t update_pos = 0;
- int32_t err = MZ_OK;
- int32_t written = 0;
- if (mz_zip_reader_is_open(reader) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (reader->file_info == NULL)
- return MZ_PARAM_ERROR;
- /* Update the progress at the beginning */
- if (reader->progress_cb != NULL)
- reader->progress_cb(handle, reader->progress_userdata, reader->file_info, current_pos);
- /* Write data to stream until done */
- while (err == MZ_OK)
- {
- written = mz_zip_reader_entry_save_process(handle, stream, write_cb);
- if (written == MZ_END_OF_STREAM)
- break;
- if (written > 0)
- current_pos += written;
- if (written < 0)
- err = written;
- /* Update progress if enough time have passed */
- current_time = mz_os_ms_time();
- if ((current_time - update_time) > reader->progress_cb_interval_ms)
- {
- if (reader->progress_cb != NULL)
- reader->progress_cb(handle, reader->progress_userdata, reader->file_info, current_pos);
- update_pos = current_pos;
- update_time = current_time;
- }
- }
- /* Update the progress at the end */
- if (reader->progress_cb != NULL && update_pos != current_pos)
- reader->progress_cb(handle, reader->progress_userdata, reader->file_info, current_pos);
- return err;
- }
- int32_t mz_zip_reader_entry_save_file(void *handle, const char *path)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- void *stream = NULL;
- uint32_t target_attrib = 0;
- int32_t err_attrib = 0;
- int32_t err = MZ_OK;
- int32_t err_cb = MZ_OK;
- char pathwfs[512];
- char directory[512];
- if (mz_zip_reader_is_open(reader) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (reader->file_info == NULL || path == NULL)
- return MZ_PARAM_ERROR;
- /* Convert to forward slashes for unix which doesn't like backslashes */
- strncpy(pathwfs, path, sizeof(pathwfs) - 1);
- pathwfs[sizeof(pathwfs) - 1] = 0;
- mz_path_convert_slashes(pathwfs, MZ_PATH_SLASH_UNIX);
- if (reader->entry_cb != NULL)
- reader->entry_cb(handle, reader->entry_userdata, reader->file_info, pathwfs);
- strncpy(directory, pathwfs, sizeof(directory) - 1);
- directory[sizeof(directory) - 1] = 0;
- mz_path_remove_filename(directory);
- /* If it is a directory entry then create a directory instead of writing file */
- if ((mz_zip_entry_is_dir(reader->zip_handle) == MZ_OK) &&
- (mz_zip_entry_is_symlink(reader->zip_handle) != MZ_OK))
- {
- err = mz_dir_make(directory);
- return err;
- }
- /* Check if file exists and ask if we want to overwrite */
- if ((mz_os_file_exists(pathwfs) == MZ_OK) && (reader->overwrite_cb != NULL))
- {
- err_cb = reader->overwrite_cb(handle, reader->overwrite_userdata, reader->file_info, pathwfs);
- if (err_cb != MZ_OK)
- return err;
- /* We want to overwrite the file so we delete the existing one */
- mz_os_unlink(pathwfs);
- }
- /* If symbolic link then properly construct destination path and link path */
- if (mz_zip_entry_is_symlink(reader->zip_handle) == MZ_OK)
- {
- mz_path_remove_slash(pathwfs);
- mz_path_remove_filename(directory);
- }
- /* Create the output directory if it doesn't already exist */
- if (mz_os_is_dir(directory) != MZ_OK)
- {
- err = mz_dir_make(directory);
- if (err != MZ_OK)
- return err;
- }
- /* If it is a symbolic link then create symbolic link instead of writing file */
- if (mz_zip_entry_is_symlink(reader->zip_handle) == MZ_OK)
- {
- mz_os_make_symlink(pathwfs, reader->file_info->linkname);
- /* Don't check return value because we aren't validating symbolic link target */
- return err;
- }
- /* Create the file on disk so we can save to it */
- mz_stream_os_create(&stream);
- err = mz_stream_os_open(stream, pathwfs, MZ_OPEN_MODE_CREATE);
- if (err == MZ_OK)
- err = mz_zip_reader_entry_save(handle, stream, mz_stream_write);
- mz_stream_close(stream);
- mz_stream_delete(&stream);
- if (err == MZ_OK)
- {
- /* Set the time of the file that has been created */
- mz_os_set_file_date(pathwfs, reader->file_info->modified_date,
- reader->file_info->accessed_date, reader->file_info->creation_date);
- }
- if (err == MZ_OK)
- {
- /* Set file attributes for the correct system */
- err_attrib = mz_zip_attrib_convert(MZ_HOST_SYSTEM(reader->file_info->version_madeby),
- reader->file_info->external_fa, MZ_VERSION_MADEBY_HOST_SYSTEM, &target_attrib);
- if (err_attrib == MZ_OK)
- mz_os_set_file_attribs(pathwfs, target_attrib);
- }
- return err;
- }
- int32_t mz_zip_reader_entry_save_buffer(void *handle, void *buf, int32_t len)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- void *mem_stream = NULL;
- int32_t err = MZ_OK;
- if (mz_zip_reader_is_open(reader) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (reader->file_info == NULL)
- return MZ_PARAM_ERROR;
- if (reader->file_info->uncompressed_size > INT32_MAX)
- return MZ_PARAM_ERROR;
- if (len != (int32_t)reader->file_info->uncompressed_size)
- return MZ_BUF_ERROR;
- /* Create a memory stream backed by our buffer and save to it */
- mz_stream_mem_create(&mem_stream);
- mz_stream_mem_set_buffer(mem_stream, buf, len);
- err = mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_READ);
- if (err == MZ_OK)
- err = mz_zip_reader_entry_save(handle, mem_stream, mz_stream_mem_write);
- mz_stream_mem_delete(&mem_stream);
- return err;
- }
- int32_t mz_zip_reader_entry_save_buffer_length(void *handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- if (mz_zip_reader_is_open(reader) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (reader->file_info == NULL)
- return MZ_PARAM_ERROR;
- if (reader->file_info->uncompressed_size > INT32_MAX)
- return MZ_PARAM_ERROR;
- /* Get the maximum size required for the save buffer */
- return (int32_t)reader->file_info->uncompressed_size;
- }
- /***************************************************************************/
- int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- int32_t err = MZ_OK;
- uint8_t *utf8_string = NULL;
- char path[512];
- char utf8_name[256];
- char resolved_name[256];
- err = mz_zip_reader_goto_first_entry(handle);
- if (err == MZ_END_OF_LIST)
- return err;
- while (err == MZ_OK)
- {
- /* Construct output path */
- path[0] = 0;
- strncpy(utf8_name, reader->file_info->filename, sizeof(utf8_name) - 1);
- utf8_name[sizeof(utf8_name) - 1] = 0;
- if ((reader->encoding > 0) && (reader->file_info->flag & MZ_ZIP_FLAG_UTF8) == 0)
- {
- utf8_string = mz_os_utf8_string_create(reader->file_info->filename, reader->encoding);
- if (utf8_string)
- {
- strncpy(utf8_name, (char *)utf8_string, sizeof(utf8_name) - 1);
- utf8_name[sizeof(utf8_name) - 1] = 0;
- mz_os_utf8_string_delete(&utf8_string);
- }
- }
- err = mz_path_resolve(utf8_name, resolved_name, sizeof(resolved_name));
- if (err != MZ_OK)
- break;
- if (destination_dir != NULL)
- mz_path_combine(path, destination_dir, sizeof(path));
- mz_path_combine(path, resolved_name, sizeof(path));
- /* Save file to disk */
- err = mz_zip_reader_entry_save_file(handle, path);
- if (err == MZ_OK)
- err = mz_zip_reader_goto_next_entry(handle);
- }
- if (err == MZ_END_OF_LIST)
- return MZ_OK;
- return err;
- }
- /***************************************************************************/
- void mz_zip_reader_set_pattern(void *handle, const char *pattern, uint8_t ignore_case)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- reader->pattern = pattern;
- reader->pattern_ignore_case = ignore_case;
- }
- void mz_zip_reader_set_password(void *handle, const char *password)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- reader->password = password;
- }
- void mz_zip_reader_set_raw(void *handle, uint8_t raw)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- reader->raw = raw;
- }
- int32_t mz_zip_reader_get_raw(void *handle, uint8_t *raw)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- if (raw == NULL)
- return MZ_PARAM_ERROR;
- *raw = reader->raw;
- return MZ_OK;
- }
- int32_t mz_zip_reader_get_zip_cd(void *handle, uint8_t *zip_cd)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- if (zip_cd == NULL)
- return MZ_PARAM_ERROR;
- *zip_cd = reader->cd_zipped;
- return MZ_OK;
- }
- int32_t mz_zip_reader_get_comment(void *handle, const char **comment)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- if (mz_zip_reader_is_open(reader) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (comment == NULL)
- return MZ_PARAM_ERROR;
- return mz_zip_get_comment(reader->zip_handle, comment);
- }
- void mz_zip_reader_set_encoding(void *handle, int32_t encoding)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- reader->encoding = encoding;
- }
- void mz_zip_reader_set_sign_required(void *handle, uint8_t sign_required)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- reader->sign_required = sign_required;
- }
- void mz_zip_reader_set_overwrite_cb(void *handle, void *userdata, mz_zip_reader_overwrite_cb cb)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- reader->overwrite_cb = cb;
- reader->overwrite_userdata = userdata;
- }
- void mz_zip_reader_set_password_cb(void *handle, void *userdata, mz_zip_reader_password_cb cb)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- reader->password_cb = cb;
- reader->password_userdata = userdata;
- }
- void mz_zip_reader_set_progress_cb(void *handle, void *userdata, mz_zip_reader_progress_cb cb)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- reader->progress_cb = cb;
- reader->progress_userdata = userdata;
- }
- void mz_zip_reader_set_progress_interval(void *handle, uint32_t milliseconds)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- reader->progress_cb_interval_ms = milliseconds;
- }
- void mz_zip_reader_set_entry_cb(void *handle, void *userdata, mz_zip_reader_entry_cb cb)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- reader->entry_cb = cb;
- reader->entry_userdata = userdata;
- }
- int32_t mz_zip_reader_get_zip_handle(void *handle, void **zip_handle)
- {
- mz_zip_reader *reader = (mz_zip_reader *)handle;
- if (zip_handle == NULL)
- return MZ_PARAM_ERROR;
- *zip_handle = reader->zip_handle;
- if (*zip_handle == NULL)
- return MZ_EXIST_ERROR;
- return MZ_OK;
- }
- /***************************************************************************/
- void *mz_zip_reader_create(void **handle)
- {
- mz_zip_reader *reader = NULL;
- reader = (mz_zip_reader *)MZ_ALLOC(sizeof(mz_zip_reader));
- if (reader != NULL)
- {
- memset(reader, 0, sizeof(mz_zip_reader));
- reader->progress_cb_interval_ms = MZ_DEFAULT_PROGRESS_INTERVAL;
- *handle = reader;
- }
- return reader;
- }
- void mz_zip_reader_delete(void **handle)
- {
- mz_zip_reader *reader = NULL;
- if (handle == NULL)
- return;
- reader = (mz_zip_reader *)*handle;
- if (reader != NULL)
- {
- mz_zip_reader_close(reader);
- MZ_FREE(reader);
- }
- *handle = NULL;
- }
- /***************************************************************************/
- typedef struct mz_zip_writer_s {
- void *zip_handle;
- void *file_stream;
- void *buffered_stream;
- void *split_stream;
- void *sha256;
- void *mem_stream;
- void *file_extra_stream;
- mz_zip_file file_info;
- void *overwrite_userdata;
- mz_zip_writer_overwrite_cb
- overwrite_cb;
- void *password_userdata;
- mz_zip_writer_password_cb
- password_cb;
- void *progress_userdata;
- mz_zip_writer_progress_cb
- progress_cb;
- uint32_t progress_cb_interval_ms;
- void *entry_userdata;
- mz_zip_writer_entry_cb
- entry_cb;
- const char *password;
- const char *comment;
- uint8_t *cert_data;
- int32_t cert_data_size;
- const char *cert_pwd;
- uint16_t compress_method;
- int16_t compress_level;
- uint8_t follow_links;
- uint8_t store_links;
- uint8_t zip_cd;
- uint8_t aes;
- uint8_t raw;
- uint8_t buffer[UINT16_MAX];
- } mz_zip_writer;
- /***************************************************************************/
- int32_t mz_zip_writer_zip_cd(void *handle)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- mz_zip_file cd_file;
- uint64_t number_entry = 0;
- int64_t cd_mem_length = 0;
- int32_t err = MZ_OK;
- int32_t extrafield_size = 0;
- void *file_extra_stream = NULL;
- void *cd_mem_stream = NULL;
- memset(&cd_file, 0, sizeof(cd_file));
- mz_zip_get_number_entry(writer->zip_handle, &number_entry);
- mz_zip_get_cd_mem_stream(writer->zip_handle, &cd_mem_stream);
- mz_stream_seek(cd_mem_stream, 0, MZ_SEEK_END);
- cd_mem_length = (uint32_t)mz_stream_tell(cd_mem_stream);
- mz_stream_seek(cd_mem_stream, 0, MZ_SEEK_SET);
- cd_file.filename = MZ_ZIP_CD_FILENAME;
- cd_file.modified_date = time(NULL);
- cd_file.version_madeby = MZ_VERSION_MADEBY;
- cd_file.compression_method = writer->compress_method;
- cd_file.uncompressed_size = (int32_t)cd_mem_length;
- cd_file.flag = MZ_ZIP_FLAG_UTF8;
- if (writer->password != NULL)
- cd_file.flag |= MZ_ZIP_FLAG_ENCRYPTED;
- mz_stream_mem_create(&file_extra_stream);
- mz_stream_mem_open(file_extra_stream, NULL, MZ_OPEN_MODE_CREATE);
- mz_zip_extrafield_write(file_extra_stream, MZ_ZIP_EXTENSION_CDCD, 8);
- mz_stream_write_uint64(file_extra_stream, number_entry);
- mz_stream_mem_get_buffer(file_extra_stream, (const void **)&cd_file.extrafield);
- mz_stream_mem_get_buffer_length(file_extra_stream, &extrafield_size);
- cd_file.extrafield_size = (uint16_t)extrafield_size;
- err = mz_zip_writer_entry_open(handle, &cd_file);
- if (err == MZ_OK)
- {
- mz_stream_copy_stream(handle, mz_zip_writer_entry_write, cd_mem_stream,
- NULL, (int32_t)cd_mem_length);
- mz_stream_seek(cd_mem_stream, 0, MZ_SEEK_SET);
- mz_stream_mem_set_buffer_limit(cd_mem_stream, 0);
- err = mz_zip_writer_entry_close(writer);
- }
- mz_stream_mem_delete(&file_extra_stream);
- return err;
- }
- /***************************************************************************/
- int32_t mz_zip_writer_is_open(void *handle)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- if (writer == NULL)
- return MZ_PARAM_ERROR;
- if (writer->zip_handle == NULL)
- return MZ_PARAM_ERROR;
- return MZ_OK;
- }
- static int32_t mz_zip_writer_open_int(void *handle, void *stream, int32_t mode)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- int32_t err = MZ_OK;
- mz_zip_create(&writer->zip_handle);
- err = mz_zip_open(writer->zip_handle, stream, mode);
- if (err != MZ_OK)
- {
- mz_zip_writer_close(handle);
- return err;
- }
- return MZ_OK;
- }
- int32_t mz_zip_writer_open(void *handle, void *stream)
- {
- return mz_zip_writer_open_int(handle, stream, MZ_OPEN_MODE_WRITE);
- }
- int32_t mz_zip_writer_open_file(void *handle, const char *path, int64_t disk_size, uint8_t append)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- int32_t mode = MZ_OPEN_MODE_READWRITE;
- int32_t err = MZ_OK;
- int32_t err_cb = 0;
- char directory[320];
- mz_zip_writer_close(handle);
- if (mz_os_file_exists(path) != MZ_OK)
- {
- /* If the file doesn't exist, we don't append file */
- mode |= MZ_OPEN_MODE_CREATE;
- /* Create destination directory if it doesn't already exist */
- if (strchr(path, '/') != NULL || strrchr(path, '\\') != NULL)
- {
- strncpy(directory, path, sizeof(directory));
- mz_path_remove_filename(directory);
- if (mz_os_file_exists(directory) != MZ_OK)
- mz_dir_make(directory);
- }
- }
- else if (append)
- {
- mode |= MZ_OPEN_MODE_APPEND;
- }
- else
- {
- if (writer->overwrite_cb != NULL)
- err_cb = writer->overwrite_cb(handle, writer->overwrite_userdata, path);
- if (err_cb == MZ_INTERNAL_ERROR)
- return err;
- if (err_cb == MZ_OK)
- mode |= MZ_OPEN_MODE_CREATE;
- else
- mode |= MZ_OPEN_MODE_APPEND;
- }
- mz_stream_os_create(&writer->file_stream);
- mz_stream_buffered_create(&writer->buffered_stream);
- mz_stream_split_create(&writer->split_stream);
- mz_stream_set_base(writer->buffered_stream, writer->file_stream);
- mz_stream_set_base(writer->split_stream, writer->buffered_stream);
- mz_stream_split_set_prop_int64(writer->split_stream, MZ_STREAM_PROP_DISK_SIZE, disk_size);
- err = mz_stream_open(writer->split_stream, path, mode);
- if (err == MZ_OK)
- err = mz_zip_writer_open_int(handle, writer->split_stream, mode);
- return err;
- }
- int32_t mz_zip_writer_open_file_in_memory(void *handle, const char *path)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- void *file_stream = NULL;
- int64_t file_size = 0;
- int32_t err = 0;
- mz_zip_writer_close(handle);
- mz_stream_os_create(&file_stream);
- err = mz_stream_os_open(file_stream, path, MZ_OPEN_MODE_READ);
- if (err != MZ_OK)
- {
- mz_stream_os_delete(&file_stream);
- mz_zip_writer_close(handle);
- return err;
- }
- mz_stream_os_seek(file_stream, 0, MZ_SEEK_END);
- file_size = mz_stream_os_tell(file_stream);
- mz_stream_os_seek(file_stream, 0, MZ_SEEK_SET);
- if ((file_size <= 0) || (file_size > UINT32_MAX))
- {
- /* Memory size is too large or too small */
- mz_stream_os_close(file_stream);
- mz_stream_os_delete(&file_stream);
- mz_zip_writer_close(handle);
- return MZ_MEM_ERROR;
- }
- mz_stream_mem_create(&writer->mem_stream);
- mz_stream_mem_set_grow_size(writer->mem_stream, (int32_t)file_size);
- mz_stream_mem_open(writer->mem_stream, NULL, MZ_OPEN_MODE_CREATE);
- err = mz_stream_copy(writer->mem_stream, file_stream, (int32_t)file_size);
- mz_stream_os_close(file_stream);
- mz_stream_os_delete(&file_stream);
- if (err == MZ_OK)
- err = mz_zip_writer_open(handle, writer->mem_stream);
- if (err != MZ_OK)
- mz_zip_writer_close(handle);
- return err;
- }
- int32_t mz_zip_writer_close(void *handle)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- int32_t err = MZ_OK;
- if (writer->zip_handle != NULL)
- {
- mz_zip_set_version_madeby(writer->zip_handle, MZ_VERSION_MADEBY);
- if (writer->comment)
- mz_zip_set_comment(writer->zip_handle, writer->comment);
- if (writer->zip_cd)
- mz_zip_writer_zip_cd(writer);
- err = mz_zip_close(writer->zip_handle);
- mz_zip_delete(&writer->zip_handle);
- }
- if (writer->split_stream != NULL)
- {
- mz_stream_split_close(writer->split_stream);
- mz_stream_split_delete(&writer->split_stream);
- }
- if (writer->buffered_stream != NULL)
- mz_stream_buffered_delete(&writer->buffered_stream);
- if (writer->file_stream != NULL)
- mz_stream_os_delete(&writer->file_stream);
- if (writer->mem_stream != NULL)
- {
- mz_stream_mem_close(writer->mem_stream);
- mz_stream_mem_delete(&writer->mem_stream);
- }
- return err;
- }
- /***************************************************************************/
- int32_t mz_zip_writer_entry_open(void *handle, mz_zip_file *file_info)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- int32_t err = MZ_OK;
- const char *password = NULL;
- char password_buf[120];
- /* Copy file info to access data upon close */
- memcpy(&writer->file_info, file_info, sizeof(mz_zip_file));
- if (writer->entry_cb != NULL)
- writer->entry_cb(handle, writer->entry_userdata, &writer->file_info);
- password = writer->password;
- /* Check if we need a password and ask for it if we need to */
- if ((writer->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) &&
- (writer->password_cb != NULL))
- {
- writer->password_cb(handle, writer->password_userdata, &writer->file_info,
- password_buf, sizeof(password_buf));
- password = password_buf;
- }
- #ifndef MZ_ZIP_NO_ENCRYPTION
- if (mz_zip_attrib_is_dir(writer->file_info.external_fa, writer->file_info.version_madeby) != MZ_OK)
- {
- /* Start calculating sha256 */
- mz_crypt_sha_create(&writer->sha256);
- mz_crypt_sha_set_algorithm(writer->sha256, MZ_HASH_SHA256);
- mz_crypt_sha_begin(writer->sha256);
- }
- #endif
- /* Open entry in zip */
- err = mz_zip_entry_write_open(writer->zip_handle, &writer->file_info, writer->compress_level,
- writer->raw, password);
- return err;
- }
- #if !defined(MZ_ZIP_NO_ENCRYPTION) && defined(MZ_ZIP_SIGNING)
- int32_t mz_zip_writer_entry_sign(void *handle, uint8_t *message, int32_t message_size,
- uint8_t *cert_data, int32_t cert_data_size, const char *cert_pwd)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- int32_t err = MZ_OK;
- int32_t signature_size = 0;
- uint8_t *signature = NULL;
- if (writer == NULL || cert_data == NULL || cert_data_size <= 0)
- return MZ_PARAM_ERROR;
- if (mz_zip_entry_is_open(writer->zip_handle) != MZ_OK)
- return MZ_PARAM_ERROR;
- /* Sign message with certificate */
- err = mz_crypt_sign(message, message_size, cert_data, cert_data_size, cert_pwd,
- &signature, &signature_size);
- if ((err == MZ_OK) && (signature != NULL))
- {
- /* Write signature zip extra field */
- err = mz_zip_extrafield_write(writer->file_extra_stream, MZ_ZIP_EXTENSION_SIGN,
- (uint16_t)signature_size);
- if (err == MZ_OK)
- {
- if (mz_stream_write(writer->file_extra_stream, signature, signature_size) != signature_size)
- err = MZ_WRITE_ERROR;
- }
- MZ_FREE(signature);
- }
- return err;
- }
- #endif
- int32_t mz_zip_writer_entry_close(void *handle)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- int32_t err = MZ_OK;
- #ifndef MZ_ZIP_NO_ENCRYPTION
- const uint8_t *extrafield = NULL;
- int32_t extrafield_size = 0;
- int16_t field_length_hash = 0;
- uint8_t sha256[MZ_HASH_SHA256_SIZE];
- if (writer->sha256 != NULL)
- {
- mz_crypt_sha_end(writer->sha256, sha256, sizeof(sha256));
- mz_crypt_sha_delete(&writer->sha256);
- /* Copy extrafield so we can append our own fields before close */
- mz_stream_mem_create(&writer->file_extra_stream);
- mz_stream_mem_open(writer->file_extra_stream, NULL, MZ_OPEN_MODE_CREATE);
- /* Write sha256 hash to extrafield */
- field_length_hash = 4 + MZ_HASH_SHA256_SIZE;
- err = mz_zip_extrafield_write(writer->file_extra_stream, MZ_ZIP_EXTENSION_HASH, field_length_hash);
- if (err == MZ_OK)
- err = mz_stream_write_uint16(writer->file_extra_stream, MZ_HASH_SHA256);
- if (err == MZ_OK)
- err = mz_stream_write_uint16(writer->file_extra_stream, MZ_HASH_SHA256_SIZE);
- if (err == MZ_OK)
- {
- if (mz_stream_write(writer->file_extra_stream, sha256, sizeof(sha256)) != MZ_HASH_SHA256_SIZE)
- err = MZ_WRITE_ERROR;
- }
- #ifdef MZ_ZIP_SIGNING
- if ((err == MZ_OK) && (writer->cert_data != NULL) && (writer->cert_data_size > 0))
- {
- /* Sign entry if not zipping cd or if it is cd being zipped */
- if (!writer->zip_cd || strcmp(writer->file_info.filename, MZ_ZIP_CD_FILENAME) == 0)
- {
- err = mz_zip_writer_entry_sign(handle, sha256, sizeof(sha256),
- writer->cert_data, writer->cert_data_size, writer->cert_pwd);
- }
- }
- #endif
- if ((writer->file_info.extrafield != NULL) && (writer->file_info.extrafield_size > 0))
- mz_stream_mem_write(writer->file_extra_stream, writer->file_info.extrafield,
- writer->file_info.extrafield_size);
- /* Update extra field for central directory after adding extra fields */
- mz_stream_mem_get_buffer(writer->file_extra_stream, (const void **)&extrafield);
- mz_stream_mem_get_buffer_length(writer->file_extra_stream, &extrafield_size);
- mz_zip_entry_set_extrafield(writer->zip_handle, extrafield, (uint16_t)extrafield_size);
- }
- #endif
- if (err == MZ_OK)
- {
- if (writer->raw)
- err = mz_zip_entry_close_raw(writer->zip_handle, writer->file_info.uncompressed_size,
- writer->file_info.crc);
- else
- err = mz_zip_entry_close(writer->zip_handle);
- }
- if (writer->file_extra_stream != NULL)
- mz_stream_mem_delete(&writer->file_extra_stream);
- return err;
- }
- int32_t mz_zip_writer_entry_write(void *handle, const void *buf, int32_t len)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- int32_t written = 0;
- written = mz_zip_entry_write(writer->zip_handle, buf, len);
- #ifndef MZ_ZIP_NO_ENCRYPTION
- if ((written > 0) && (writer->sha256 != NULL))
- mz_crypt_sha_update(writer->sha256, buf, written);
- #endif
- return written;
- }
- /***************************************************************************/
- int32_t mz_zip_writer_add_process(void *handle, void *stream, mz_stream_read_cb read_cb)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- int32_t read = 0;
- int32_t written = 0;
- int32_t err = MZ_OK;
- if (mz_zip_writer_is_open(writer) != MZ_OK)
- return MZ_PARAM_ERROR;
- /* If the entry isn't open for writing, open it */
- if (mz_zip_entry_is_open(writer->zip_handle) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (read_cb == NULL)
- return MZ_PARAM_ERROR;
- read = read_cb(stream, writer->buffer, sizeof(writer->buffer));
- if (read == 0)
- return MZ_END_OF_STREAM;
- if (read < 0)
- {
- err = read;
- return err;
- }
- written = mz_zip_writer_entry_write(handle, writer->buffer, read);
- if (written != read)
- return MZ_WRITE_ERROR;
- return written;
- }
- int32_t mz_zip_writer_add(void *handle, void *stream, mz_stream_read_cb read_cb)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- uint64_t current_time = 0;
- uint64_t update_time = 0;
- int64_t current_pos = 0;
- int64_t update_pos = 0;
- int32_t err = MZ_OK;
- int32_t written = 0;
- /* Update the progress at the beginning */
- if (writer->progress_cb != NULL)
- writer->progress_cb(handle, writer->progress_userdata, &writer->file_info, current_pos);
- /* Write data to stream until done */
- while (err == MZ_OK)
- {
- written = mz_zip_writer_add_process(handle, stream, read_cb);
- if (written == MZ_END_OF_STREAM)
- break;
- if (written > 0)
- current_pos += written;
- if (written < 0)
- err = written;
- /* Update progress if enough time have passed */
- current_time = mz_os_ms_time();
- if ((current_time - update_time) > writer->progress_cb_interval_ms)
- {
- if (writer->progress_cb != NULL)
- writer->progress_cb(handle, writer->progress_userdata, &writer->file_info, current_pos);
- update_pos = current_pos;
- update_time = current_time;
- }
- }
- /* Update the progress at the end */
- if (writer->progress_cb != NULL && update_pos != current_pos)
- writer->progress_cb(handle, writer->progress_userdata, &writer->file_info, current_pos);
- return err;
- }
- int32_t mz_zip_writer_add_info(void *handle, void *stream, mz_stream_read_cb read_cb, mz_zip_file *file_info)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- int32_t err = MZ_OK;
- if (mz_zip_writer_is_open(handle) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (file_info == NULL)
- return MZ_PARAM_ERROR;
- /* Add to zip */
- err = mz_zip_writer_entry_open(handle, file_info);
- if (err != MZ_OK)
- return err;
- if (stream != NULL)
- {
- if (mz_zip_attrib_is_dir(writer->file_info.external_fa, writer->file_info.version_madeby) != MZ_OK)
- {
- err = mz_zip_writer_add(handle, stream, read_cb);
- if (err != MZ_OK)
- return err;
- }
- }
- err = mz_zip_writer_entry_close(handle);
- return err;
- }
- int32_t mz_zip_writer_add_buffer(void *handle, void *buf, int32_t len, mz_zip_file *file_info)
- {
- void *mem_stream = NULL;
- int32_t err = MZ_OK;
- if (mz_zip_writer_is_open(handle) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (buf == NULL)
- return MZ_PARAM_ERROR;
- /* Create a memory stream backed by our buffer and add from it */
- mz_stream_mem_create(&mem_stream);
- mz_stream_mem_set_buffer(mem_stream, buf, len);
- err = mz_stream_mem_open(mem_stream, NULL, MZ_OPEN_MODE_READ);
- if (err == MZ_OK)
- err = mz_zip_writer_add_info(handle, mem_stream, mz_stream_mem_read, file_info);
- mz_stream_mem_delete(&mem_stream);
- return err;
- }
- int32_t mz_zip_writer_add_file(void *handle, const char *path, const char *filename_in_zip)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- mz_zip_file file_info;
- uint32_t target_attrib = 0;
- uint32_t src_attrib = 0;
- int32_t err = MZ_OK;
- uint8_t src_sys = 0;
- void *stream = NULL;
- char link_path[1024];
- const char *filename = filename_in_zip;
- if (mz_zip_writer_is_open(handle) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (path == NULL)
- return MZ_PARAM_ERROR;
- if (filename == NULL)
- {
- err = mz_path_get_filename(path, &filename);
- if (err != MZ_OK)
- return err;
- }
- memset(&file_info, 0, sizeof(file_info));
- /* The path name saved, should not include a leading slash. */
- /* If it did, windows/xp and dynazip couldn't read the zip file. */
- while (filename[0] == '\\' || filename[0] == '/')
- filename += 1;
- /* Get information about the file on disk so we can store it in zip */
- file_info.version_madeby = MZ_VERSION_MADEBY;
- file_info.compression_method = writer->compress_method;
- file_info.filename = filename;
- file_info.uncompressed_size = mz_os_get_file_size(path);
- file_info.flag = MZ_ZIP_FLAG_UTF8;
- if (writer->zip_cd)
- file_info.flag |= MZ_ZIP_FLAG_MASK_LOCAL_INFO;
- if (writer->aes)
- file_info.aes_version = MZ_AES_VERSION;
- mz_os_get_file_date(path, &file_info.modified_date, &file_info.accessed_date,
- &file_info.creation_date);
- mz_os_get_file_attribs(path, &src_attrib);
- src_sys = MZ_HOST_SYSTEM(file_info.version_madeby);
- if ((src_sys != MZ_HOST_SYSTEM_MSDOS) && (src_sys != MZ_HOST_SYSTEM_WINDOWS_NTFS))
- {
- /* High bytes are OS specific attributes, low byte is always DOS attributes */
- if (mz_zip_attrib_convert(src_sys, src_attrib, MZ_HOST_SYSTEM_MSDOS, &target_attrib) == MZ_OK)
- file_info.external_fa = target_attrib;
- file_info.external_fa |= (src_attrib << 16);
- }
- else
- {
- file_info.external_fa = src_attrib;
- }
- if (writer->store_links && mz_os_is_symlink(path) == MZ_OK)
- {
- err = mz_os_read_symlink(path, link_path, sizeof(link_path));
- if (err == MZ_OK)
- file_info.linkname = link_path;
- }
- if (mz_os_is_dir(path) != MZ_OK)
- {
- mz_stream_os_create(&stream);
- err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
- }
- if (err == MZ_OK)
- err = mz_zip_writer_add_info(handle, stream, mz_stream_read, &file_info);
- if (stream != NULL)
- {
- mz_stream_close(stream);
- mz_stream_delete(&stream);
- }
- return err;
- }
- int32_t mz_zip_writer_add_path(void *handle, const char *path, const char *root_path,
- uint8_t include_path, uint8_t recursive)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- DIR *dir = NULL;
- struct dirent *entry = NULL;
- int32_t err = MZ_OK;
- int16_t is_dir = 0;
- const char *filename = NULL;
- const char *filenameinzip = path;
- char *wildcard_ptr = NULL;
- char full_path[1024];
- char path_dir[1024];
- if (strrchr(path, '*') != NULL)
- {
- strncpy(path_dir, path, sizeof(path_dir) - 1);
- path_dir[sizeof(path_dir) - 1] = 0;
- mz_path_remove_filename(path_dir);
- wildcard_ptr = path_dir + strlen(path_dir) + 1;
- root_path = path = path_dir;
- }
- else
- {
- if (mz_os_is_dir(path) == MZ_OK)
- is_dir = 1;
- /* Construct the filename that our file will be stored in the zip as */
- if (root_path == NULL)
- root_path = path;
- /* Should the file be stored with any path info at all? */
- if (!include_path)
- {
- if (!is_dir && root_path == path)
- {
- if (mz_path_get_filename(filenameinzip, &filename) == MZ_OK)
- filenameinzip = filename;
- }
- else
- {
- filenameinzip += strlen(root_path);
- }
- }
- if (!writer->store_links && !writer->follow_links)
- {
- if (mz_os_is_symlink(path) == MZ_OK)
- return err;
- }
- if (*filenameinzip != 0)
- err = mz_zip_writer_add_file(handle, path, filenameinzip);
- if (!is_dir)
- return err;
- if (writer->store_links)
- {
- if (mz_os_is_symlink(path) == MZ_OK)
- return err;
- }
- }
- dir = mz_os_open_dir(path);
- if (dir == NULL)
- return MZ_EXIST_ERROR;
- while ((entry = mz_os_read_dir(dir)) != NULL)
- {
- if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
- continue;
- full_path[0] = 0;
- mz_path_combine(full_path, path, sizeof(full_path));
- mz_path_combine(full_path, entry->d_name, sizeof(full_path));
- if (!recursive && mz_os_is_dir(full_path) == MZ_OK)
- continue;
- if ((wildcard_ptr != NULL) && (mz_path_compare_wc(entry->d_name, wildcard_ptr, 1) != MZ_OK))
- continue;
- err = mz_zip_writer_add_path(handle, full_path, root_path, include_path, recursive);
- if (err != MZ_OK)
- return err;
- }
- mz_os_close_dir(dir);
- return MZ_OK;
- }
- int32_t mz_zip_writer_copy_from_reader(void *handle, void *reader)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- mz_zip_file *file_info = NULL;
- int64_t compressed_size = 0;
- int64_t uncompressed_size = 0;
- uint32_t crc32 = 0;
- int32_t err = MZ_OK;
- uint8_t original_raw = 0;
- void *reader_zip_handle = NULL;
- void *writer_zip_handle = NULL;
- if (mz_zip_reader_is_open(reader) != MZ_OK)
- return MZ_PARAM_ERROR;
- if (mz_zip_writer_is_open(writer) != MZ_OK)
- return MZ_PARAM_ERROR;
- err = mz_zip_reader_entry_get_info(reader, &file_info);
- if (err != MZ_OK)
- return err;
- mz_zip_reader_get_zip_handle(reader, &reader_zip_handle);
- mz_zip_writer_get_zip_handle(writer, &writer_zip_handle);
- /* Open entry for raw reading */
- err = mz_zip_entry_read_open(reader_zip_handle, 1, NULL);
- if (err == MZ_OK)
- {
- /* Write entry raw, save original raw value */
- original_raw = writer->raw;
- writer->raw = 1;
- err = mz_zip_writer_entry_open(writer, file_info);
- if ((err == MZ_OK) &&
- (mz_zip_attrib_is_dir(writer->file_info.external_fa, writer->file_info.version_madeby) != MZ_OK))
- {
- err = mz_zip_writer_add(writer, reader_zip_handle, mz_zip_entry_read);
- }
- if (err == MZ_OK)
- {
- err = mz_zip_entry_read_close(reader_zip_handle, &crc32, &compressed_size, &uncompressed_size);
- if (err == MZ_OK)
- err = mz_zip_entry_write_close(writer_zip_handle, crc32, compressed_size, uncompressed_size);
- }
- if (mz_zip_entry_is_open(reader_zip_handle) == MZ_OK)
- mz_zip_entry_close(reader_zip_handle);
- if (mz_zip_entry_is_open(writer_zip_handle) == MZ_OK)
- mz_zip_entry_close(writer_zip_handle);
- writer->raw = original_raw;
- }
- return err;
- }
- /***************************************************************************/
- void mz_zip_writer_set_password(void *handle, const char *password)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->password = password;
- }
- void mz_zip_writer_set_comment(void *handle, const char *comment)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->comment = comment;
- }
- void mz_zip_writer_set_raw(void *handle, uint8_t raw)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->raw = raw;
- }
- int32_t mz_zip_writer_get_raw(void *handle, uint8_t *raw)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- if (raw == NULL)
- return MZ_PARAM_ERROR;
- *raw = writer->raw;
- return MZ_OK;
- }
- void mz_zip_writer_set_aes(void *handle, uint8_t aes)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->aes = aes;
- }
- void mz_zip_writer_set_compress_method(void *handle, uint16_t compress_method)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->compress_method = compress_method;
- }
- void mz_zip_writer_set_compress_level(void *handle, int16_t compress_level)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->compress_level = compress_level;
- }
- void mz_zip_writer_set_follow_links(void *handle, uint8_t follow_links)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->follow_links = follow_links;
- }
- void mz_zip_writer_set_store_links(void *handle, uint8_t store_links)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->store_links = store_links;
- }
- void mz_zip_writer_set_zip_cd(void *handle, uint8_t zip_cd)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->zip_cd = zip_cd;
- }
- int32_t mz_zip_writer_set_certificate(void *handle, const char *cert_path, const char *cert_pwd)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- void *cert_stream = NULL;
- uint8_t *cert_data = NULL;
- int32_t cert_data_size = 0;
- int32_t err = MZ_OK;
- if (cert_path == NULL)
- return MZ_PARAM_ERROR;
- cert_data_size = (int32_t)mz_os_get_file_size(cert_path);
- if (cert_data_size == 0)
- return MZ_PARAM_ERROR;
- if (writer->cert_data != NULL)
- {
- MZ_FREE(writer->cert_data);
- writer->cert_data = NULL;
- }
- cert_data = (uint8_t *)MZ_ALLOC(cert_data_size);
- /* Read pkcs12 certificate from disk */
- mz_stream_os_create(&cert_stream);
- err = mz_stream_os_open(cert_stream, cert_path, MZ_OPEN_MODE_READ);
- if (err == MZ_OK)
- {
- if (mz_stream_os_read(cert_stream, cert_data, cert_data_size) != cert_data_size)
- err = MZ_READ_ERROR;
- mz_stream_os_close(cert_stream);
- }
- mz_stream_os_delete(&cert_stream);
- if (err == MZ_OK)
- {
- writer->cert_data = cert_data;
- writer->cert_data_size = cert_data_size;
- writer->cert_pwd = cert_pwd;
- }
- else
- {
- MZ_FREE(cert_data);
- }
- return err;
- }
- void mz_zip_writer_set_overwrite_cb(void *handle, void *userdata, mz_zip_writer_overwrite_cb cb)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->overwrite_cb = cb;
- writer->overwrite_userdata = userdata;
- }
- void mz_zip_writer_set_password_cb(void *handle, void *userdata, mz_zip_writer_password_cb cb)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->password_cb = cb;
- writer->password_userdata = userdata;
- }
- void mz_zip_writer_set_progress_cb(void *handle, void *userdata, mz_zip_writer_progress_cb cb)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->progress_cb = cb;
- writer->progress_userdata = userdata;
- }
- void mz_zip_writer_set_progress_interval(void *handle, uint32_t milliseconds)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->progress_cb_interval_ms = milliseconds;
- }
- void mz_zip_writer_set_entry_cb(void *handle, void *userdata, mz_zip_writer_entry_cb cb)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- writer->entry_cb = cb;
- writer->entry_userdata = userdata;
- }
- int32_t mz_zip_writer_get_zip_handle(void *handle, void **zip_handle)
- {
- mz_zip_writer *writer = (mz_zip_writer *)handle;
- if (zip_handle == NULL)
- return MZ_PARAM_ERROR;
- *zip_handle = writer->zip_handle;
- if (*zip_handle == NULL)
- return MZ_EXIST_ERROR;
- return MZ_OK;
- }
- /***************************************************************************/
- void *mz_zip_writer_create(void **handle)
- {
- mz_zip_writer *writer = NULL;
- writer = (mz_zip_writer *)MZ_ALLOC(sizeof(mz_zip_writer));
- if (writer != NULL)
- {
- memset(writer, 0, sizeof(mz_zip_writer));
- #if defined(HAVE_WZAES)
- writer->aes = 1;
- #endif
- #if defined(HAVE_ZLIB) || defined(HAVE_LIBCOMP)
- writer->compress_method = MZ_COMPRESS_METHOD_DEFLATE;
- #elif defined(HAVE_BZIP2)
- writer->compress_method = MZ_COMPRESS_METHOD_BZIP2;
- #elif defined(HAVE_LZMA)
- writer->compress_method = MZ_COMPRESS_METHOD_LZMA;
- #else
- writer->compress_method = MZ_COMPRESS_METHOD_STORE;
- #endif
- writer->compress_level = MZ_COMPRESS_LEVEL_BEST;
- writer->progress_cb_interval_ms = MZ_DEFAULT_PROGRESS_INTERVAL;
- *handle = writer;
- }
- return writer;
- }
- void mz_zip_writer_delete(void **handle)
- {
- mz_zip_writer *writer = NULL;
- if (handle == NULL)
- return;
- writer = (mz_zip_writer *)*handle;
- if (writer != NULL)
- {
- mz_zip_writer_close(writer);
- if (writer->cert_data != NULL)
- MZ_FREE(writer->cert_data);
- writer->cert_data = NULL;
- writer->cert_data_size = 0;
- MZ_FREE(writer);
- }
- *handle = NULL;
- }
- /***************************************************************************/
|