Skip to content

Commit

Permalink
Update miniz (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand authored Dec 26, 2023
1 parent d90b988 commit 0c9d916
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ add_library(odr
"src/odr/internal/util/string_util.cpp"
"src/odr/internal/util/xml_util.cpp"

"src/odr/internal/zip/zip_util.cpp"
"src/odr/internal/zip/zip_archive.cpp"
"src/odr/internal/zip/zip_exceptions.cpp"
"src/odr/internal/zip/zip_util.cpp"
)
set_target_properties(odr PROPERTIES OUTPUT_NAME odr)
if (EXISTS "${PROJECT_SOURCE_DIR}/.git")
Expand Down
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class OpenDocumentCoreConan(ConanFile):

exports_sources = ["cli/*", "cmake/*", "src/*", "CMakeLists.txt"]

requires = ["pugixml/1.14", "cryptopp/8.8.0", "miniz/2.1.0", "nlohmann_json/3.11.3",
requires = ["pugixml/1.14", "cryptopp/8.8.0", "miniz/3.0.2", "nlohmann_json/3.11.3",
"vincentlaucsb-csv-parser/2.1.3", "uchardet/0.0.7"]
build_requires = ["gtest/1.14.0"]
generators = "cmake_paths", "cmake_find_package"
Expand Down
11 changes: 6 additions & 5 deletions src/odr/internal/zip/zip_archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <odr/exceptions.hpp>

#include <odr/internal/abstract/file.hpp>
#include <odr/internal/zip/zip_exceptions.hpp>
#include <odr/internal/zip/zip_util.hpp>

#include <chrono>
Expand Down Expand Up @@ -211,7 +212,7 @@ void ZipArchive::save(std::ostream &out) const {
};
state = mz_zip_writer_init(&archive, 0);
if (!state) {
throw ZipSaveError();
throw MinizSaveError(archive);
}

for (auto &&entry : *this) {
Expand All @@ -225,13 +226,13 @@ void ZipArchive::save(std::ostream &out) const {
state = util::append_file(archive, path.string(), *istream, size, time,
"", entry.compression_level());
if (!state) {
throw ZipSaveError();
throw MinizSaveError(archive);
}
} else if (entry.is_directory()) {
state = mz_zip_writer_add_mem(&archive, (path.string() + "/").c_str(),
nullptr, 0, 0);
if (!state) {
throw ZipSaveError();
throw MinizSaveError(archive);
}
} else {
throw ZipSaveError();
Expand All @@ -240,11 +241,11 @@ void ZipArchive::save(std::ostream &out) const {

state = mz_zip_writer_finalize_archive(&archive);
if (!state) {
throw ZipSaveError();
throw MinizSaveError(archive);
}
state = mz_zip_writer_end(&archive);
if (!state) {
throw ZipSaveError();
throw MinizSaveError(archive);
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/odr/internal/zip/zip_exceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <odr/internal/zip/zip_exceptions.hpp>

namespace odr::internal::zip {

MinizSaveError::MinizSaveError(mz_zip_archive &archive) {
error = mz_zip_get_last_error(&archive);
error_string = mz_zip_get_error_string(error);
}

MinizSaveError::MinizSaveError(mz_zip_error _error) : error{_error} {
error_string = mz_zip_get_error_string(_error);
}

const char *MinizSaveError::what() const noexcept { return error_string; }

} // namespace odr::internal::zip
22 changes: 22 additions & 0 deletions src/odr/internal/zip/zip_exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef ODR_INTERNAL_ZIP_ZIP_EXCEPTIONS_H
#define ODR_INTERNAL_ZIP_ZIP_EXCEPTIONS_H

#include <odr/exceptions.hpp>

#include <miniz.h>

namespace odr::internal::zip {

struct MinizSaveError : public ZipSaveError {
mz_zip_error error{mz_zip_error::MZ_ZIP_NO_ERROR};
const char *error_string{nullptr};

MinizSaveError(mz_zip_archive &archive);
MinizSaveError(mz_zip_error error_code);

const char *what() const noexcept override;
};

} // namespace odr::internal::zip

#endif // ODR_INTERNAL_ZIP_ZIP_EXCEPTIONS_H
6 changes: 3 additions & 3 deletions src/odr/internal/zip/zip_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ bool append_file(mz_zip_archive &archive, const std::string &path,
const std::time_t &time, const std::string &comment,
const std::uint32_t level_and_flags) {
auto read_callback = [](void *opaque, std::uint64_t /*offset*/, void *buffer,
std::size_t size) {
std::size_t size) -> std::size_t {
auto istream = static_cast<std::istream *>(opaque);
istream->read(static_cast<char *>(buffer), size);
return size;
return istream->gcount();
};

return mz_zip_writer_add_read_buf_callback(
&archive, path.c_str(), read_callback, &istream, size, &time,
comment.c_str(), comment.size(), level_and_flags, nullptr, 0, nullptr, 0);
comment.c_str(), comment.size(), level_and_flags, "", 0, "", 0);
}

} // namespace odr::internal::zip::util
7 changes: 3 additions & 4 deletions test/src/internal/zip/miniz_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ TEST(miniz, create) {
EXPECT_TRUE(state);

auto append_file = [&](const char *path, const std::string &content) {
auto read_callback = [](void *opaque, std::uint64_t offset, void *buffer,
std::size_t size) {
auto read_callback = [](void *opaque, std::uint64_t /*offset*/,
void *buffer, std::size_t size) -> std::size_t {
auto istream = static_cast<std::istream *>(opaque);
EXPECT_EQ(offset, istream->tellg());
istream->read(static_cast<char *>(buffer), size);
return size;
return istream->gcount();
};

std::istringstream istream(content);
Expand Down

0 comments on commit 0c9d916

Please sign in to comment.