From 308e2d271fb2636e0f8100605691deede9713857 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 28 May 2021 13:55:11 +0200 Subject: [PATCH] Skip rest of file if encoding of files in EPUB is broken. Standard says UTF-8. I don't want to deal with weird Windows-encodings or whatever this is. Closes: https://schlomp.space/tastytea/epubgrep/issues/7 --- src/zip.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/zip.cpp b/src/zip.cpp index e4765d6..28be71f 100644 --- a/src/zip.cpp +++ b/src/zip.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -45,7 +46,18 @@ std::vector list(const fs::path &filepath) std::vector toc; while (archive_read_next_header(zipfile, &entry) == ARCHIVE_OK) { - toc.emplace_back(archive_entry_pathname_utf8(entry)); + const auto *in_epub_filepath{archive_entry_pathname_utf8(entry)}; + if (in_epub_filepath == nullptr) + { // If the encoding is broken, we skip the file. + std::cerr << translate("WARNING: ") + << format(translate("{0:s} is damaged. " + "Skipping rest of file.\n") + .str() + .data(), + filepath); + break; + } + toc.emplace_back(in_epub_filepath); archive_read_data_skip(zipfile); }