diff --git a/README.md b/README.md index 5999519..58f0983 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,7 @@ using namespace libzippp; int main(int argc, char** argv) { //important to use calloc/malloc for the fromWritableBuffer ! - char* buffer = (char*)calloc(4096, sizeof(char)); + void* buffer = calloc(4096, sizeof(char)); ZipArchive* z1 = ZipArchive::fromWritableBuffer(&buffer, 4096, ZipArchive::New); /* add content to the archive */ diff --git a/src/libzippp.cpp b/src/libzippp.cpp index 4e04bf9..25fedde 100644 --- a/src/libzippp.cpp +++ b/src/libzippp.cpp @@ -109,8 +109,12 @@ namespace Helper { } static void callErrorHandlingCallback(zip* zipHandle, const std::string& msg, ErrorHandlerCallback* callback) { - zip_error_t* error_code = zip_get_error(zipHandle); - callErrorHandlingCallbackFunc(error_code->str, error_code->zip_err, error_code->sys_err, callback); + if(zipHandle!=nullptr) { + zip_error_t* error_code = zip_get_error(zipHandle); + callErrorHandlingCallbackFunc(error_code->str, error_code->zip_err, error_code->sys_err, callback); + } else { + callErrorHandlingCallbackFunc("", -1, -1, callback); + } } static void callErrorHandlingCallback(zip_error_t* error, const std::string& msg, ErrorHandlerCallback* callback) { @@ -433,7 +437,7 @@ int ZipArchive::close(void) { *bufferData = sourceBuffer; bufferLength = totalRead; } else { - Helper::callErrorHandlingCallback(zipHandle, "can't read back from source: changes were not pushed in the buffer\n", errorHandlingCallback); + Helper::callErrorHandlingCallback((zip*)nullptr, "can't read back from source: changes were not pushed in the buffer\n", errorHandlingCallback); return LIBZIPPP_ERROR_HANDLE_FAILURE; } diff --git a/tests/tests.cpp b/tests/tests.cpp index dd152c8..1fa76fd 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -894,6 +894,34 @@ void test23() { }*/ } +void test23_2() { + //important to use calloc/malloc for the fromWritableBuffer ! + void* buffer = calloc(4096, sizeof(char)); + + ZipArchive* z1 = ZipArchive::fromWritableBuffer(&buffer, 4096, ZipArchive::New); + /* add content to the archive */ + + //will update the content of the buffer + z1->close(); + + //length of the buffer content + int bufferContentLength = z1->getBufferLength(); + + ZipArchive::free(z1); + + //read again from the archive: + ZipArchive* z2 = ZipArchive::fromBuffer(buffer, bufferContentLength); + /* read the archive - no modification allowed */ + ZipArchive::free(z2); + + //read again from the archive, for modification: + ZipArchive* z3 = ZipArchive::fromWritableBuffer(&buffer, bufferContentLength); + /* read/write the archive */ + ZipArchive::free(z3); + + free(buffer); +} + static void myErrorHandler(const std::string& message, const std::string& strerror, int zip_error_code, @@ -920,7 +948,7 @@ int main() { test6(); test7(); test8(); test9(); test10(); test11(); test12(); test13(); test14(); test15(); test16(); test17(); test18(); test19(); test20(); - test21(); test22(); test23(); test24(); + test21(); test22(); test23(); test23_2(); test24(); return 0; }