Skip to content

Commit 0c9d916

Browse files
authored
Update miniz (#340)
1 parent d90b988 commit 0c9d916

7 files changed

Lines changed: 53 additions & 14 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ add_library(odr
157157
"src/odr/internal/util/string_util.cpp"
158158
"src/odr/internal/util/xml_util.cpp"
159159

160-
"src/odr/internal/zip/zip_util.cpp"
161160
"src/odr/internal/zip/zip_archive.cpp"
161+
"src/odr/internal/zip/zip_exceptions.cpp"
162+
"src/odr/internal/zip/zip_util.cpp"
162163
)
163164
set_target_properties(odr PROPERTIES OUTPUT_NAME odr)
164165
if (EXISTS "${PROJECT_SOURCE_DIR}/.git")

conanfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class OpenDocumentCoreConan(ConanFile):
2121

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

24-
requires = ["pugixml/1.14", "cryptopp/8.8.0", "miniz/2.1.0", "nlohmann_json/3.11.3",
24+
requires = ["pugixml/1.14", "cryptopp/8.8.0", "miniz/3.0.2", "nlohmann_json/3.11.3",
2525
"vincentlaucsb-csv-parser/2.1.3", "uchardet/0.0.7"]
2626
build_requires = ["gtest/1.14.0"]
2727
generators = "cmake_paths", "cmake_find_package"

src/odr/internal/zip/zip_archive.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <odr/exceptions.hpp>
44

55
#include <odr/internal/abstract/file.hpp>
6+
#include <odr/internal/zip/zip_exceptions.hpp>
67
#include <odr/internal/zip/zip_util.hpp>
78

89
#include <chrono>
@@ -211,7 +212,7 @@ void ZipArchive::save(std::ostream &out) const {
211212
};
212213
state = mz_zip_writer_init(&archive, 0);
213214
if (!state) {
214-
throw ZipSaveError();
215+
throw MinizSaveError(archive);
215216
}
216217

217218
for (auto &&entry : *this) {
@@ -225,13 +226,13 @@ void ZipArchive::save(std::ostream &out) const {
225226
state = util::append_file(archive, path.string(), *istream, size, time,
226227
"", entry.compression_level());
227228
if (!state) {
228-
throw ZipSaveError();
229+
throw MinizSaveError(archive);
229230
}
230231
} else if (entry.is_directory()) {
231232
state = mz_zip_writer_add_mem(&archive, (path.string() + "/").c_str(),
232233
nullptr, 0, 0);
233234
if (!state) {
234-
throw ZipSaveError();
235+
throw MinizSaveError(archive);
235236
}
236237
} else {
237238
throw ZipSaveError();
@@ -240,11 +241,11 @@ void ZipArchive::save(std::ostream &out) const {
240241

241242
state = mz_zip_writer_finalize_archive(&archive);
242243
if (!state) {
243-
throw ZipSaveError();
244+
throw MinizSaveError(archive);
244245
}
245246
state = mz_zip_writer_end(&archive);
246247
if (!state) {
247-
throw ZipSaveError();
248+
throw MinizSaveError(archive);
248249
}
249250
}
250251

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <odr/internal/zip/zip_exceptions.hpp>
2+
3+
namespace odr::internal::zip {
4+
5+
MinizSaveError::MinizSaveError(mz_zip_archive &archive) {
6+
error = mz_zip_get_last_error(&archive);
7+
error_string = mz_zip_get_error_string(error);
8+
}
9+
10+
MinizSaveError::MinizSaveError(mz_zip_error _error) : error{_error} {
11+
error_string = mz_zip_get_error_string(_error);
12+
}
13+
14+
const char *MinizSaveError::what() const noexcept { return error_string; }
15+
16+
} // namespace odr::internal::zip
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef ODR_INTERNAL_ZIP_ZIP_EXCEPTIONS_H
2+
#define ODR_INTERNAL_ZIP_ZIP_EXCEPTIONS_H
3+
4+
#include <odr/exceptions.hpp>
5+
6+
#include <miniz.h>
7+
8+
namespace odr::internal::zip {
9+
10+
struct MinizSaveError : public ZipSaveError {
11+
mz_zip_error error{mz_zip_error::MZ_ZIP_NO_ERROR};
12+
const char *error_string{nullptr};
13+
14+
MinizSaveError(mz_zip_archive &archive);
15+
MinizSaveError(mz_zip_error error_code);
16+
17+
const char *what() const noexcept override;
18+
};
19+
20+
} // namespace odr::internal::zip
21+
22+
#endif // ODR_INTERNAL_ZIP_ZIP_EXCEPTIONS_H

src/odr/internal/zip/zip_util.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ bool append_file(mz_zip_archive &archive, const std::string &path,
154154
const std::time_t &time, const std::string &comment,
155155
const std::uint32_t level_and_flags) {
156156
auto read_callback = [](void *opaque, std::uint64_t /*offset*/, void *buffer,
157-
std::size_t size) {
157+
std::size_t size) -> std::size_t {
158158
auto istream = static_cast<std::istream *>(opaque);
159159
istream->read(static_cast<char *>(buffer), size);
160-
return size;
160+
return istream->gcount();
161161
};
162162

163163
return mz_zip_writer_add_read_buf_callback(
164164
&archive, path.c_str(), read_callback, &istream, size, &time,
165-
comment.c_str(), comment.size(), level_and_flags, nullptr, 0, nullptr, 0);
165+
comment.c_str(), comment.size(), level_and_flags, "", 0, "", 0);
166166
}
167167

168168
} // namespace odr::internal::zip::util

test/src/internal/zip/miniz_test.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ TEST(miniz, create) {
4646
EXPECT_TRUE(state);
4747

4848
auto append_file = [&](const char *path, const std::string &content) {
49-
auto read_callback = [](void *opaque, std::uint64_t offset, void *buffer,
50-
std::size_t size) {
49+
auto read_callback = [](void *opaque, std::uint64_t /*offset*/,
50+
void *buffer, std::size_t size) -> std::size_t {
5151
auto istream = static_cast<std::istream *>(opaque);
52-
EXPECT_EQ(offset, istream->tellg());
5352
istream->read(static_cast<char *>(buffer), size);
54-
return size;
53+
return istream->gcount();
5554
};
5655

5756
std::istringstream istream(content);

0 commit comments

Comments
 (0)