diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index b9f41c49..ff10c55b 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -69,6 +69,7 @@ jobs: -DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake" -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_BUILD_TYPE=Release + -DCMAKE_CXX_FLAGS="-Werror" -DCMAKE_INSTALL_PREFIX=install -DODR_TEST=ON - name: build diff --git a/cli/src/translate.cpp b/cli/src/translate.cpp index 24a80102..dc027844 100644 --- a/cli/src/translate.cpp +++ b/cli/src/translate.cpp @@ -16,25 +16,25 @@ int main(int argc, char **argv) { password = argv[3]; } - File file{input}; + DecodedFile decoded_file{input}; - HtmlConfig config; - config.editable = true; - - PasswordCallback password_callback = [&]() { - if (!password) { + if (decoded_file.is_document_file()) { + DocumentFile document_file = decoded_file.document_file(); + if (document_file.password_encrypted() && !password) { std::cerr << "document encrypted but no password given" << std::endl; - std::exit(2); + return 2; + } + if (document_file.password_encrypted() && + !document_file.decrypt(*password)) { + std::cerr << "wrong password" << std::endl; + return 1; } - return *password; - }; - - try { - html::translate(file, output, config, password_callback); - } catch (const WrongPassword &e) { - std::cerr << "wrong password" << std::endl; - return 1; } + HtmlConfig config; + config.editable = true; + + html::translate(decoded_file, output, config); + return 0; } diff --git a/src/odr/internal/html/document_element.cpp b/src/odr/internal/html/document_element.cpp index c7185b67..2220d66c 100644 --- a/src/odr/internal/html/document_element.cpp +++ b/src/odr/internal/html/document_element.cpp @@ -232,6 +232,8 @@ void html::translate_master_page(MasterPage masterPage, HtmlWriter &out, void html::translate_text(const Element element, HtmlWriter &out, const HtmlConfig &config, const HtmlResourceLocator &resourceLocator) { + (void)resourceLocator; + Text text = element.text(); out.write_element_begin( @@ -253,6 +255,7 @@ void html::translate_line_break(Element element, HtmlWriter &out, const HtmlConfig &config, const HtmlResourceLocator &resourceLocator) { (void)config; + (void)resourceLocator; LineBreak line_break = element.line_break(); @@ -319,6 +322,9 @@ void html::translate_link(Element element, HtmlWriter &out, void html::translate_bookmark(Element element, HtmlWriter &out, const HtmlConfig &config, const HtmlResourceLocator &resourceLocator) { + (void)config; + (void)resourceLocator; + Bookmark bookmark = element.bookmark(); out.write_element_begin("a", diff --git a/src/odr/internal/json/json_util.cpp b/src/odr/internal/json/json_util.cpp index 020eb0e9..0f8b14ca 100644 --- a/src/odr/internal/json/json_util.cpp +++ b/src/odr/internal/json/json_util.cpp @@ -6,7 +6,8 @@ namespace odr::internal { void json::check_json_file(std::istream &in) { // TODO limit check size - (void)nlohmann::json::parse(in); + auto json = nlohmann::json::parse(in); + (void)json; // TODO check if that even works } diff --git a/src/odr/internal/pdf/pdf_cmap_parser.cpp b/src/odr/internal/pdf/pdf_cmap_parser.cpp index 262c3ad2..ec338556 100644 --- a/src/odr/internal/pdf/pdf_cmap_parser.cpp +++ b/src/odr/internal/pdf/pdf_cmap_parser.cpp @@ -29,18 +29,19 @@ std::variant CMapParser::read_token() const { m_parser.read_string()); } if (m_parser.peek_name()) { - return m_parser.read_name(); + return Object(m_parser.read_name()); } if (m_parser.peek_dictionary()) { - return m_parser.read_dictionary(); + return Object(m_parser.read_dictionary()); } std::string token; while (true) { - int_type c = m_parser.geti(); - if (c == eof) { + int_type i = m_parser.geti(); + if (i == eof) { return token; } + auto c = static_cast(i); if (ObjectParser::is_whitespace(c)) { return token; } @@ -50,6 +51,8 @@ std::variant CMapParser::read_token() const { } void CMapParser::read_codespacerange(std::uint32_t n, CMap &cmap) const { + (void)cmap; + m_parser.skip_whitespace(); for (std::uint32_t i = 0; i < n; ++i) { auto from_glyph = m_parser.read_object(); @@ -86,6 +89,8 @@ void CMapParser::read_bfchar(std::uint32_t n, CMap &cmap) const { } void CMapParser::read_bfrange(std::uint32_t n, CMap &cmap) const { + (void)cmap; + m_parser.skip_whitespace(); for (std::uint32_t i = 0; i < n; ++i) { auto from_glyph = m_parser.read_object(); diff --git a/src/odr/internal/pdf/pdf_document_parser.cpp b/src/odr/internal/pdf/pdf_document_parser.cpp index de2df0ed..56a75b6c 100644 --- a/src/odr/internal/pdf/pdf_document_parser.cpp +++ b/src/odr/internal/pdf/pdf_document_parser.cpp @@ -26,7 +26,7 @@ pdf::Font *parse_font(DocumentParser &parser, const ObjectReference &reference, font->type = Type::font; font->object_reference = reference; - font->object = dictionary; + font->object = Object(dictionary); if (dictionary.has_key("ToUnicode")) { IndirectObject to_unicode_obj = @@ -43,12 +43,12 @@ pdf::Font *parse_font(DocumentParser &parser, const ObjectReference &reference, pdf::Resources *parse_resources(DocumentParser &parser, const Object &object, Document &document) { - Resources *resources = document.create_element(); + auto *resources = document.create_element(); Dictionary dictionary = parser.resolve_object_copy(object).as_dictionary(); resources->type = Type::resources; - resources->object = dictionary; + resources->object = Object(dictionary); if (!dictionary["Font"].is_null()) { Dictionary font_table = @@ -64,14 +64,14 @@ pdf::Resources *parse_resources(DocumentParser &parser, const Object &object, pdf::Annotation *parse_annotation(DocumentParser &parser, const ObjectReference &reference, Document &document) { - Annotation *annotation = document.create_element(); + auto *annotation = document.create_element(); IndirectObject object = parser.read_object(reference); const Dictionary &dictionary = object.object.as_dictionary(); annotation->type = Type::annotation; annotation->object_reference = reference; - annotation->object = dictionary; + annotation->object = Object(dictionary); return annotation; } @@ -85,7 +85,7 @@ pdf::Page *parse_page(DocumentParser &parser, const ObjectReference &reference, page->type = Type::page; page->object_reference = reference; - page->object = dictionary; + page->object = Object(dictionary); page->parent = dynamic_cast(parent); page->resources = parse_resources(parser, dictionary["Resources"], document); @@ -112,14 +112,14 @@ pdf::Page *parse_page(DocumentParser &parser, const ObjectReference &reference, pdf::Pages *parse_pages(DocumentParser &parser, const ObjectReference &reference, Document &document) { - Pages *pages = document.create_element(); + auto *pages = document.create_element(); IndirectObject object = parser.read_object(reference); const Dictionary &dictionary = object.object.as_dictionary(); pages->type = Type::pages; pages->object_reference = reference; - pages->object = dictionary; + pages->object = Object(dictionary); pages->count = dictionary["Count"].as_integer(); for (const Object &kid : dictionary["Kids"].as_array()) { @@ -151,7 +151,7 @@ pdf::Element *parse_page_or_pages(DocumentParser &parser, pdf::Catalog *parse_catalog(DocumentParser &parser, const ObjectReference &reference, Document &document) { - Catalog *catalog = document.create_element(); + auto *catalog = document.create_element(); IndirectObject object = parser.read_object(reference); const Dictionary &dictionary = object.object.as_dictionary(); @@ -159,7 +159,7 @@ pdf::Catalog *parse_catalog(DocumentParser &parser, catalog->type = Type::catalog; catalog->object_reference = reference; - catalog->object = dictionary; + catalog->object = Object(dictionary); catalog->pages = parse_pages(parser, pages_reference, document); return catalog; diff --git a/src/odr/internal/pdf/pdf_graphics_operator_parser.cpp b/src/odr/internal/pdf/pdf_graphics_operator_parser.cpp index 4e8e6d43..0635b2dd 100644 --- a/src/odr/internal/pdf/pdf_graphics_operator_parser.cpp +++ b/src/odr/internal/pdf/pdf_graphics_operator_parser.cpp @@ -141,17 +141,17 @@ GraphicsOperator GraphicsOperatorParser::read_operator() const { while (true) { if (m_parser.peek_number()) { - std::visit([&](auto v) { result.arguments.push_back(v); }, + std::visit([&](auto v) { result.arguments.emplace_back(v); }, m_parser.read_integer_or_real()); } else if (m_parser.peek_name()) { - result.arguments.push_back(m_parser.read_name()); + result.arguments.emplace_back(m_parser.read_name()); } else if (m_parser.peek_string()) { - std::visit([&](auto s) { result.arguments.push_back(std::move(s)); }, + std::visit([&](auto s) { result.arguments.emplace_back(std::move(s)); }, m_parser.read_string()); } else if (m_parser.peek_array()) { - result.arguments.push_back(m_parser.read_array()); + result.arguments.emplace_back(m_parser.read_array()); } else if (m_parser.peek_dictionary()) { - result.arguments.push_back(m_parser.read_dictionary()); + result.arguments.emplace_back(m_parser.read_dictionary()); } else { m_parser.skip_whitespace(); break; diff --git a/src/odr/internal/pdf/pdf_object.hpp b/src/odr/internal/pdf/pdf_object.hpp index afd738a3..1e367e14 100644 --- a/src/odr/internal/pdf/pdf_object.hpp +++ b/src/odr/internal/pdf/pdf_object.hpp @@ -19,31 +19,28 @@ using Boolean = bool; struct StandardString { std::string string; - StandardString(std::string _string) : string{std::move(_string)} {} + explicit StandardString(std::string _string) : string{std::move(_string)} {} void to_stream(std::ostream &) const; - std::string to_string() const; - friend std::ostream &operator<<(std::ostream &, const StandardString &); + [[nodiscard]] std::string to_string() const; }; struct HexString { std::string string; - HexString(std::string _string) : string{std::move(_string)} {} + explicit HexString(std::string _string) : string{std::move(_string)} {} void to_stream(std::ostream &) const; - std::string to_string() const; - friend std::ostream &operator<<(std::ostream &, const HexString &); + [[nodiscard]] std::string to_string() const; }; struct Name { std::string string; - Name(std::string _string) : string{std::move(_string)} {} + explicit Name(std::string _string) : string{std::move(_string)} {} void to_stream(std::ostream &) const; - std::string to_string() const; - friend std::ostream &operator<<(std::ostream &, const Name &); + [[nodiscard]] std::string to_string() const; }; struct ObjectReference { @@ -58,8 +55,7 @@ struct ObjectReference { [[nodiscard]] std::size_t hash() const noexcept; void to_stream(std::ostream &) const; - std::string to_string() const; - friend std::ostream &operator<<(std::ostream &, const ObjectReference &); + [[nodiscard]] std::string to_string() const; }; class Array; @@ -70,47 +66,53 @@ class Object { using Holder = std::any; Object() = default; - Object(Boolean boolean) : m_holder{boolean} {} - Object(Integer integer) : m_holder{integer} {} - Object(Real real) : m_holder{real} {} - Object(StandardString string) : m_holder{std::move(string)} {} - Object(HexString string) : m_holder{std::move(string)} {} - Object(Name name) : m_holder{std::move(name)} {} - Object(Array); - Object(Dictionary); - Object(ObjectReference reference) : m_holder{std::move(reference)} {} - - Holder &holder() { return m_holder; } - const Holder &holder() const { return m_holder; } - - bool is_null() const { return !m_holder.has_value(); } - bool is_bool() const { return is(); } - bool is_integer() const { return is(); } - bool is_real() const { return is() || is_integer(); } - bool is_standard_string() const { return is(); } - bool is_hex_string() const { return is(); } - bool is_name() const { return is(); } - bool is_string() const { + explicit Object(Boolean boolean) : m_holder{boolean} {} + explicit Object(Integer integer) : m_holder{integer} {} + explicit Object(Real real) : m_holder{real} {} + explicit Object(StandardString string) : m_holder{std::move(string)} {} + explicit Object(HexString string) : m_holder{std::move(string)} {} + explicit Object(Name name) : m_holder{std::move(name)} {} + explicit Object(Array); + explicit Object(Dictionary); + explicit Object(ObjectReference reference) : m_holder{reference} {} + + [[nodiscard]] Holder &holder() { return m_holder; } + [[nodiscard]] const Holder &holder() const { return m_holder; } + + [[nodiscard]] bool is_null() const { return !m_holder.has_value(); } + [[nodiscard]] bool is_bool() const { return is(); } + [[nodiscard]] bool is_integer() const { return is(); } + [[nodiscard]] bool is_real() const { return is() || is_integer(); } + [[nodiscard]] bool is_standard_string() const { return is(); } + [[nodiscard]] bool is_hex_string() const { return is(); } + [[nodiscard]] bool is_name() const { return is(); } + [[nodiscard]] bool is_string() const { return is_standard_string() || is_hex_string() || is_name(); } - bool is_array() const { return is(); } - bool is_dictionary() const { return is(); } - bool is_reference() const { return is(); } - - Boolean as_bool() const { return as(); } - Integer as_integer() const { return as(); } - Real as_real() const { return is() ? as() : as_integer(); } - const std::string &as_standard_string() const { + [[nodiscard]] bool is_array() const { return is(); } + [[nodiscard]] bool is_dictionary() const { return is(); } + [[nodiscard]] bool is_reference() const { return is(); } + + [[nodiscard]] Boolean as_bool() const { return as(); } + [[nodiscard]] Integer as_integer() const { return as(); } + [[nodiscard]] Real as_real() const { + return is() ? as() : as_integer(); + } + [[nodiscard]] const std::string &as_standard_string() const { return as().string; } - const std::string &as_hex_string() const { + [[nodiscard]] const std::string &as_hex_string() const { return as().string; } - const std::string &as_name() const { return as().string; } - const std::string &as_string() const; - const Array &as_array() const & { return as(); } - const Dictionary &as_dictionary() const & { return as(); } - const ObjectReference &as_reference() const { + [[nodiscard]] const std::string &as_name() const { + return as().string; + } + [[nodiscard]] const std::string &as_string() const; + [[nodiscard]] const Array &as_array() const & { return as(); } + [[nodiscard]] const Dictionary &as_dictionary() const & { + return as(); + } + [[nodiscard]] const ObjectReference &as_reference() const { return as(); } @@ -123,8 +125,7 @@ class Object { } void to_stream(std::ostream &) const; - std::string to_string() const; - friend std::ostream &operator<<(std::ostream &, const Object &); + [[nodiscard]] std::string to_string() const; private: Holder m_holder; @@ -149,21 +150,22 @@ class Array { Array &operator=(const Array &) = default; Array &operator=(Array &&) = default; - Holder &holder() { return m_holder; } - const Holder &holder() const { return m_holder; } + [[nodiscard]] Holder &holder() { return m_holder; } + [[nodiscard]] const Holder &holder() const { return m_holder; } - std::size_t size() const { return m_holder.size(); } - Holder::iterator begin() { return m_holder.begin(); } - Holder::iterator end() { return m_holder.end(); } - Holder::const_iterator begin() const { return m_holder.cbegin(); } - Holder::const_iterator end() const { return m_holder.cend(); } + [[nodiscard]] std::size_t size() const { return m_holder.size(); } + [[nodiscard]] Holder::iterator begin() { return m_holder.begin(); } + [[nodiscard]] Holder::iterator end() { return m_holder.end(); } + [[nodiscard]] Holder::const_iterator begin() const { + return m_holder.cbegin(); + } + [[nodiscard]] Holder::const_iterator end() const { return m_holder.cend(); } Object &operator[](std::size_t i) { return m_holder.at(i); } const Object &operator[](std::size_t i) const { return m_holder.at(i); } void to_stream(std::ostream &) const; - std::string to_string() const; - friend std::ostream &operator<<(std::ostream &, const Array &); + [[nodiscard]] std::string to_string() const; private: Holder m_holder; @@ -177,31 +179,40 @@ class Dictionary { explicit Dictionary(Holder holder) : m_holder{std::move(holder)} {} Holder &holder() { return m_holder; } - const Holder &holder() const { return m_holder; } + [[nodiscard]] const Holder &holder() const { return m_holder; } - std::size_t size() const { return m_holder.size(); } - Holder::iterator begin() { return m_holder.begin(); } - Holder::iterator end() { return m_holder.end(); } - Holder::const_iterator begin() const { return m_holder.cbegin(); } - Holder::const_iterator end() const { return m_holder.cend(); } + [[nodiscard]] std::size_t size() const { return m_holder.size(); } + [[nodiscard]] Holder::iterator begin() { return m_holder.begin(); } + [[nodiscard]] Holder::iterator end() { return m_holder.end(); } + [[nodiscard]] Holder::const_iterator begin() const { + return m_holder.cbegin(); + } + [[nodiscard]] Holder::const_iterator end() const { return m_holder.cend(); } Object &operator[](const std::string &name) { return m_holder[name]; } const Object &operator[](const std::string &name) const { return m_holder.at(name); } - bool has_key(const std::string &name) const { + [[nodiscard]] bool has_key(const std::string &name) const { return m_holder.find(name) != std::end(m_holder); } void to_stream(std::ostream &) const; - std::string to_string() const; - friend std::ostream &operator<<(std::ostream &, const Dictionary &); + [[nodiscard]] std::string to_string() const; private: Holder m_holder; }; +std::ostream &operator<<(std::ostream &, const StandardString &); +std::ostream &operator<<(std::ostream &, const HexString &); +std::ostream &operator<<(std::ostream &, const Name &); +std::ostream &operator<<(std::ostream &, const ObjectReference &); +std::ostream &operator<<(std::ostream &, const Object &); +std::ostream &operator<<(std::ostream &, const Array &); +std::ostream &operator<<(std::ostream &, const Dictionary &); + } // namespace odr::internal::pdf template <> struct std::hash { diff --git a/src/odr/internal/pdf/pdf_object_parser.cpp b/src/odr/internal/pdf/pdf_object_parser.cpp index 8e5b5782..de968708 100644 --- a/src/odr/internal/pdf/pdf_object_parser.cpp +++ b/src/odr/internal/pdf/pdf_object_parser.cpp @@ -30,7 +30,7 @@ ObjectParser::char_type ObjectParser::getc() const { in().setstate(std::ios::eofbit); throw std::runtime_error("unexpected stream exhaust"); } - return c; + return static_cast(c); } ObjectParser::char_type ObjectParser::bumpc() const { @@ -39,12 +39,13 @@ ObjectParser::char_type ObjectParser::bumpc() const { in().setstate(std::ios::eofbit); throw std::runtime_error("unexpected stream exhaust"); } - return c; + return static_cast(c); } std::string ObjectParser::bumpnc(std::size_t n) const { std::string result(n, '\0'); - if (sb().sgetn(result.data(), result.size()) != result.size()) { + auto m = static_cast(n); + if (sb().sgetn(result.data(), m) != m) { throw std::runtime_error("unexpected stream exhaust"); } return result; @@ -72,14 +73,16 @@ ObjectParser::int_type ObjectParser::hex_char_to_int(char_type c) { ObjectParser::char_type ObjectParser::two_hex_to_char(char_type first, char_type second) { - return hex_char_to_int(first) * 16 + hex_char_to_int(second); + return static_cast(hex_char_to_int(first) * 16 + + hex_char_to_int(second)); } ObjectParser::char_type ObjectParser::three_octet_to_char(char_type first, char_type second, char_type third) { - return octet_char_to_int(first) * 64 + octet_char_to_int(second) * 8 + - octet_char_to_int(third); + return static_cast(octet_char_to_int(first) * 64 + + octet_char_to_int(second) * 8 + + octet_char_to_int(third)); } bool ObjectParser::is_whitespace(char c) { @@ -89,7 +92,7 @@ bool ObjectParser::is_whitespace(char c) { bool ObjectParser::peek_whitespace() const { int_type c = geti(); - return c != eof && is_whitespace(c); + return c != eof && is_whitespace(static_cast(c)); } void ObjectParser::skip_whitespace() const { @@ -98,7 +101,7 @@ void ObjectParser::skip_whitespace() const { if (c == eof) { return; } - if (!is_whitespace(c)) { + if (!is_whitespace(static_cast(c))) { return; } bumpc(); @@ -142,7 +145,7 @@ UnsignedInteger ObjectParser::read_unsigned_integer() const { } Integer ObjectParser::read_integer() const { - std::int8_t sign = 1; + Integer sign = 1; char_type c = getc(); if (c == '+') { @@ -154,7 +157,7 @@ Integer ObjectParser::read_integer() const { bumpc(); } - return sign * read_unsigned_integer(); + return sign * static_cast(read_unsigned_integer()); } Real ObjectParser::read_number() const { @@ -178,12 +181,13 @@ std::variant ObjectParser::read_integer_or_real() const { UnsignedInteger i2 = read_unsigned_integer(); pos_type end = in().tellg(); - return i + i2 * std::pow(10.0, begin - end); + return static_cast(i) + + static_cast(i2) * std::pow(10.0, begin - end); } bool ObjectParser::peek_name() const { int_type c = geti(); - return c != eof && c == '/'; + return c == '/'; } void ObjectParser::read_name(std::ostream &out) const { @@ -192,11 +196,12 @@ void ObjectParser::read_name(std::ostream &out) const { } while (true) { - int_type c = geti(); + int_type i = geti(); - if (c == eof) { + if (i == eof) { return; } + auto c = static_cast(i); if (c < 0x21 || c > 0x7e || c == '/' || c == '%' || c == '(' || c == ')' || c == '<' || c == '>' || c == '[' || c == ']' || c == '{' || c == '}') { return; @@ -217,7 +222,7 @@ void ObjectParser::read_name(std::ostream &out) const { Name ObjectParser::read_name() const { std::stringstream ss; read_name(ss); - return ss.str(); + return Name(ss.str()); } bool ObjectParser::peek_null() const { @@ -228,6 +233,7 @@ bool ObjectParser::peek_null() const { void ObjectParser::read_null() const { auto tmp = bumpnc<4>(); // TODO check ignore case + (void)tmp; } bool ObjectParser::peek_boolean() const { @@ -241,6 +247,7 @@ Boolean ObjectParser::read_boolean() const { if (c == 't' || c == 'T') { auto tmp = bumpnc<4>(); // TODO check ignore case + (void)tmp; return true; } @@ -248,6 +255,7 @@ Boolean ObjectParser::read_boolean() const { if (c == 'f' || c == 'F') { auto tmp = bumpnc<5>(); // TODO check ignore case + (void)tmp; return false; } @@ -318,7 +326,7 @@ std::variant ObjectParser::read_string() const { bool ObjectParser::peek_array() const { int_type c = geti(); - return c != eof && c == '['; + return c == '['; } Array ObjectParser::read_array() const { @@ -353,10 +361,11 @@ Array ObjectParser::read_array() const { } bool ObjectParser::peek_dictionary() const { - int_type c = geti(); - if (c == eof) { + int_type i = geti(); + if (i == eof) { return false; } + auto c = static_cast(i); if (c == '<') { bumpc(); c = getc(); @@ -400,7 +409,7 @@ Dictionary ObjectParser::read_dictionary() const { skip_whitespace(); UnsignedInteger id = value.as_integer(); - value = ObjectReference{id, gen}; + value = Object(ObjectReference{id, gen}); } result.emplace(std::move(name.string), std::move(value)); @@ -415,23 +424,24 @@ Object ObjectParser::read_object() const { return {}; } if (peek_boolean()) { - return read_boolean(); + return Object(read_boolean()); } if (peek_number()) { - return std::visit([](auto v) -> Object { return v; }, + return std::visit([](auto v) -> Object { return Object(v); }, read_integer_or_real()); } if (peek_name()) { - return read_name(); + return Object(read_name()); } if (peek_string()) { - return std::visit([](auto v) -> Object { return v; }, read_string()); + return std::visit([](auto v) -> Object { return Object(v); }, + read_string()); } if (peek_array()) { - return read_array(); + return Object(read_array()); } if (peek_dictionary()) { - return read_dictionary(); + return Object(read_dictionary()); } throw std::runtime_error("unknown object"); diff --git a/src/odr/open_document_reader.cpp b/src/odr/open_document_reader.cpp index e4121cd3..7ee3d06a 100644 --- a/src/odr/open_document_reader.cpp +++ b/src/odr/open_document_reader.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -176,14 +177,36 @@ Html OpenDocumentReader::html(const std::string &path, const PasswordCallback &password_callback, const std::string &output_path, const HtmlConfig &config) { - return html(File(path), password_callback, output_path, config); + auto decoded_file = DecodedFile(path); + + if (decoded_file.is_document_file()) { + DocumentFile document_file = decoded_file.document_file(); + if (document_file.password_encrypted()) { + if (!document_file.decrypt(password_callback())) { + throw WrongPassword(); + } + } + } + + return html(decoded_file, output_path, config); } Html OpenDocumentReader::html(const File &file, const PasswordCallback &password_callback, const std::string &output_path, const HtmlConfig &config) { - return html::translate(file, output_path, config, password_callback); + auto decoded_file = DecodedFile(file); + + if (decoded_file.is_document_file()) { + DocumentFile document_file = decoded_file.document_file(); + if (document_file.password_encrypted()) { + if (!document_file.decrypt(password_callback())) { + throw WrongPassword(); + } + } + } + + return html(decoded_file, output_path, config); } Html OpenDocumentReader::html(const DecodedFile &file, diff --git a/test/src/test_util.cpp b/test/src/test_util.cpp index 0d7ff48c..ca9b9e91 100644 --- a/test/src/test_util.cpp +++ b/test/src/test_util.cpp @@ -32,20 +32,20 @@ TestFile get_test_file(std::string input) { return {std::move(input), type, encrypted, std::move(password)}; } -std::vector get_test_files(const std::string &input) { - if (fs::is_regular_file(input)) { - return {get_test_file(input)}; +std::vector get_test_files(const std::string &input_path) { + if (fs::is_regular_file(input_path)) { + return {get_test_file(input_path)}; } - if (!fs::is_directory(input)) { + if (!fs::is_directory(input_path)) { return {}; } std::vector result; - const std::string index = input + "/index.csv"; - if (fs::is_regular_file(index)) { - for (auto &&row : csv::CSVReader(index)) { - const std::string path = input + "/" + row["path"].get<>(); + const std::string index_path = input_path + "/index.csv"; + if (fs::is_regular_file(index_path)) { + for (const auto &row : csv::CSVReader(index_path)) { + const std::string path = input_path + "/" + row["path"].get<>(); const FileType type = OpenDocumentReader::type_by_extension(row["type"].get<>()); std::string password = row["password"].get<>(); @@ -60,12 +60,12 @@ std::vector get_test_files(const std::string &input) { } // TODO this will also recurse `.git` - for (auto &&p : fs::recursive_directory_iterator(input)) { + for (auto &&p : fs::recursive_directory_iterator(input_path)) { if (!p.is_regular_file()) { continue; } const std::string path = p.path().string(); - if (path == index) { + if (path == index_path) { continue; }