Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/odr/internal/odf/odf_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ DecoderEngine OpenDocumentFile::decoder_engine() const noexcept {
}

DocumentType OpenDocumentFile::document_type() const {
return m_file_meta.document_meta->document_type;
return m_file_meta.document_meta.value().document_type;
}

DocumentMeta OpenDocumentFile::document_meta() const {
return *m_file_meta.document_meta;
return m_file_meta.document_meta.value();
}

bool OpenDocumentFile::password_encrypted() const noexcept {
Expand Down
4 changes: 2 additions & 2 deletions src/odr/internal/oldms/oldms_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ DecoderEngine LegacyMicrosoftFile::decoder_engine() const noexcept {
}

DocumentType LegacyMicrosoftFile::document_type() const {
return m_file_meta.document_meta->document_type;
return m_file_meta.document_meta.value().document_type;
}

DocumentMeta LegacyMicrosoftFile::document_meta() const {
return *m_file_meta.document_meta;
return m_file_meta.document_meta.value();
}

bool LegacyMicrosoftFile::password_encrypted() const noexcept {
Expand Down
14 changes: 10 additions & 4 deletions src/odr/internal/oldms_wvware/wvware_oldms_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ void WvWareLegacyMicrosoftFile::open() {

int ret = wvInitParser_gsf(&m_parser_state->ps, m_parser_state->gsf_input);

m_file_meta.type = FileType::legacy_word_document;
m_file_meta.document_meta = DocumentMeta();
m_file_meta.document_meta->document_type = DocumentType::text;

// check if encrypted
if ((ret & 0x8000) != 0) {
m_file_meta.password_encrypted = true;
m_encryption_state = EncryptionState::encrypted;
m_parser_state->encryption_flag = ret & 0x7fff;

Expand Down Expand Up @@ -82,7 +87,7 @@ FileType WvWareLegacyMicrosoftFile::file_type() const noexcept {
}

FileMeta WvWareLegacyMicrosoftFile::file_meta() const noexcept {
return {file_type(), password_encrypted(), document_meta()};
return m_file_meta;
}

DecoderEngine WvWareLegacyMicrosoftFile::decoder_engine() const noexcept {
Expand All @@ -93,11 +98,12 @@ DocumentType WvWareLegacyMicrosoftFile::document_type() const {
return DocumentType::text;
}

DocumentMeta WvWareLegacyMicrosoftFile::document_meta() const { return {}; }
DocumentMeta WvWareLegacyMicrosoftFile::document_meta() const {
return m_file_meta.document_meta.value();
}

bool WvWareLegacyMicrosoftFile::password_encrypted() const noexcept {
return m_encryption_state == EncryptionState::encrypted ||
m_encryption_state == EncryptionState::decrypted;
return m_file_meta.password_encrypted;
}

EncryptionState WvWareLegacyMicrosoftFile::encryption_state() const noexcept {
Expand Down
1 change: 1 addition & 0 deletions src/odr/internal/oldms_wvware/wvware_oldms_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class WvWareLegacyMicrosoftFile final : public abstract::DocumentFile {
std::shared_ptr<abstract::File> m_file;
std::shared_ptr<ParserState> m_parser_state;

FileMeta m_file_meta;
EncryptionState m_encryption_state{EncryptionState::unknown};

void open();
Expand Down
4 changes: 2 additions & 2 deletions src/odr/internal/ooxml/ooxml_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ DecoderEngine OfficeOpenXmlFile::decoder_engine() const noexcept {
}

DocumentType OfficeOpenXmlFile::document_type() const {
return m_file_meta.document_meta->document_type;
return m_file_meta.document_meta.value().document_type;
}

DocumentMeta OfficeOpenXmlFile::document_meta() const {
return *m_file_meta.document_meta;
return m_file_meta.document_meta.value();
}

bool OfficeOpenXmlFile::password_encrypted() const noexcept {
Expand Down
3 changes: 1 addition & 2 deletions src/odr/internal/ooxml/ooxml_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ FileMeta parse_file_meta(abstract::ReadableFilesystem &filesystem) {
};

FileMeta result;
result.document_meta = DocumentMeta();

if (filesystem.is_file(common::Path("/EncryptionInfo")) &&
filesystem.is_file(common::Path("/EncryptedPackage"))) {
Expand All @@ -34,8 +35,6 @@ FileMeta parse_file_meta(abstract::ReadableFilesystem &filesystem) {
return result;
}

result.document_meta.emplace();

for (auto &&t : types) {
if (filesystem.is_file(t.first)) {
result.type = t.second.file_type;
Expand Down
6 changes: 4 additions & 2 deletions src/odr/internal/pdf/pdf_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace odr::internal {

PdfFile::PdfFile(std::shared_ptr<abstract::File> file)
: m_file{std::move(file)} {}
: m_file{std::move(file)} {
m_file_meta.type = FileType::portable_document_format;
}

std::shared_ptr<abstract::File> PdfFile::file() const noexcept {
return m_file;
}

FileMeta PdfFile::file_meta() const noexcept { return {}; }
FileMeta PdfFile::file_meta() const noexcept { return m_file_meta; }

DecoderEngine PdfFile::decoder_engine() const noexcept {
return DecoderEngine::odr;
Expand Down
1 change: 1 addition & 0 deletions src/odr/internal/pdf/pdf_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PdfFile final : public abstract::PdfFile {

private:
std::shared_ptr<abstract::File> m_file;
FileMeta m_file_meta;
};

} // namespace odr::internal
28 changes: 28 additions & 0 deletions src/odr/odr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,34 @@ odr::file_category_by_file_type(const FileType type) noexcept {
}
}

odr::DocumentType
odr::document_type_by_file_type(const FileType type) noexcept {
switch (type) {
case FileType::opendocument_text:
return DocumentType::text;
case FileType::opendocument_presentation:
return DocumentType::presentation;
case FileType::opendocument_spreadsheet:
return DocumentType::spreadsheet;
case FileType::opendocument_graphics:
return DocumentType::drawing;
case FileType::office_open_xml_document:
return DocumentType::text;
case FileType::office_open_xml_presentation:
return DocumentType::presentation;
case FileType::office_open_xml_workbook:
return DocumentType::spreadsheet;
case FileType::legacy_word_document:
return DocumentType::text;
case FileType::legacy_powerpoint_presentation:
return DocumentType::presentation;
case FileType::legacy_excel_worksheets:
return DocumentType::spreadsheet;
default:
return DocumentType::unknown;
}
}

std::string odr::file_type_to_string(const FileType type) noexcept {
switch (type) {
case FileType::unknown:
Expand Down
4 changes: 4 additions & 0 deletions src/odr/odr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ file_type_by_file_extension(const std::string &extension) noexcept;
/// @param type The file type.
/// @return The file category.
[[nodiscard]] FileCategory file_category_by_file_type(FileType type) noexcept;
/// @brief Get the document type by the file type.
/// @param type The file type.
/// @return The document type.
[[nodiscard]] DocumentType document_type_by_file_type(FileType type) noexcept;
/// @brief Get the file type as a string.
/// @param type The file type.
/// @return The file type as a string.
Expand Down
38 changes: 36 additions & 2 deletions test/src/html_output_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ using namespace odr::internal::common;
using namespace odr::test;
namespace fs = std::filesystem;

FileType expected_file_type_pre_decryption(const TestFile &test_file) {
if (test_file.password.has_value()) {
if (test_file.type == FileType::office_open_xml_document ||
test_file.type == FileType::office_open_xml_presentation ||
test_file.type == FileType::office_open_xml_workbook) {
return FileType::office_open_xml_encrypted;
}
}

return test_file.type;
}

FileType expected_file_type_post_decryption(const TestFile &test_file) {
return test_file.type;
}

struct TestParams {
TestFile test_file;
std::string path;
Expand All @@ -43,9 +59,11 @@ TEST_P(HtmlOutputTests, html_meta) {
const std::string &output_path = params.output_path;
const std::string &output_path_prefix = params.output_path_prefix;

std::cout << test_file.short_path << " to " << output_path << std::endl;
const FileCategory file_category = file_category_by_file_type(test_file.type);

// TODO compare guessed file type VS actual file type
ODR_INFO(*logger, "Testing file: " << test_file.short_path << " with engine: "
<< odr::decoder_engine_to_string(engine)
<< " output to: " << output_path);

// these files cannot be opened
if (util::string::ends_with(test_file.short_path, ".sxw") ||
Expand All @@ -71,6 +89,14 @@ TEST_P(HtmlOutputTests, html_meta) {

FileMeta file_meta = file.file_meta();

EXPECT_EQ(file_meta.type, expected_file_type_pre_decryption(test_file));
if (file_category == FileCategory::document) {
EXPECT_TRUE(file_meta.document_meta.has_value());
EXPECT_EQ(file_meta.document_meta->document_type,
document_type_by_file_type(
expected_file_type_pre_decryption(test_file)));
}

fs::create_directories(output_path);

{
Expand Down Expand Up @@ -110,6 +136,14 @@ TEST_P(HtmlOutputTests, html_meta) {
// After decryption, the file meta may change
file_meta = file.file_meta();

EXPECT_EQ(file_meta.type, expected_file_type_post_decryption(test_file));
if (file_category == FileCategory::document) {
EXPECT_TRUE(file_meta.document_meta.has_value());
EXPECT_EQ(file_meta.document_meta->document_type,
document_type_by_file_type(
expected_file_type_post_decryption(test_file)));
}

{
const std::string meta_output = output_path + "/meta-decrypted.json";
const nlohmann::json json =
Expand Down