From 0754760b19311009e31fdee87e766cb8f1b55006 Mon Sep 17 00:00:00 2001 From: Marius Kittler Date: Wed, 13 Mar 2019 19:08:30 +0100 Subject: [PATCH] Adapt to c++utilities v5 --- CMakeLists.txt | 24 ++++++++---------------- io/entry.cpp | 22 +++++++++++----------- io/entry.h | 11 +++++------ io/field.cpp | 6 +++--- io/passwordfile.cpp | 28 +++++++++++++--------------- io/passwordfile.h | 13 +++++++------ tests/passwordfiletests.cpp | 6 +++--- util/opensslrandomdevice.h | 5 ++--- 8 files changed, 52 insertions(+), 63 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a20b985..f3d6d4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,29 +35,21 @@ set(META_APP_NAME "Passwordfile library") set(META_APP_AUTHOR "Martchus") set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}") set(META_APP_DESCRIPTION "C++ library to read/write passwords from/to encrypted files") -set(META_VERSION_MAJOR 4) +set(META_VERSION_MAJOR 5) set(META_VERSION_MINOR 0) -set(META_VERSION_PATCH 1) -set(META_PUBLIC_SHARED_LIB_DEPENDS c++utilities) -set(META_PUBLIC_STATIC_LIB_DEPENDS c++utilities_static) +set(META_VERSION_PATCH 0) set(META_ADD_DEFAULT_CPP_UNIT_TEST_APPLICATION ON) # find c++utilities -find_package(c++utilities 4.17.0 REQUIRED) -use_cpp_utilities() +set(CONFIGURATION_PACKAGE_SUFFIX "" + CACHE STRING "sets the suffix for find_package() calls to packages configured via c++utilities") +find_package(c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.0.0 REQUIRED) +use_cpp_utilities(VISIBILITY PUBLIC) # find 3rd party libraries include(3rdParty) -# zlib -use_external_library_from_package(z ZLIB ANY_VERSION ZLIB_INCLUDE_DIRS ZLIB_LIBRARIES AUTO_LINKAGE REQUIRED) -# crypto library from OpenSSL -use_external_library_from_package(crypto - OpenSSL - ANY_VERSION - OPENSSL_INCLUDE_DIR - OPENSSL_CRYPTO_LIBRARY - AUTO_LINKAGE - REQUIRED) +use_zlib() +use_crypto() # include modules to apply configuration include(BasicConfig) diff --git a/io/entry.cpp b/io/entry.cpp index 8c91991..29cec65 100644 --- a/io/entry.cpp +++ b/io/entry.cpp @@ -176,7 +176,7 @@ void Entry::path(std::list &res) const */ Entry *Entry::parse(istream &stream) { - const auto version = static_cast(stream.peek()); + const auto version = static_cast(stream.peek()); if (denotesNodeEntry(version)) { return new NodeEntry(stream); } else { @@ -231,7 +231,7 @@ NodeEntry::NodeEntry(istream &stream) : m_expandedByDefault(true) { BinaryReader reader(&stream); - const byte version = reader.readByte(); + const std::uint8_t version = reader.readByte(); if (!denotesNodeEntry(version)) { throw ParsingException("Node entry expected."); } @@ -241,16 +241,16 @@ NodeEntry::NodeEntry(istream &stream) setLabel(reader.readLengthPrefixedString()); // read extended header for version 0x1 if (version == 0x1) { - uint16 extendedHeaderSize = reader.readUInt16BE(); + std::uint16_t extendedHeaderSize = reader.readUInt16BE(); if (extendedHeaderSize >= 1) { - byte flags = reader.readByte(); + std::uint8_t flags = reader.readByte(); m_expandedByDefault = flags & 0x80; extendedHeaderSize -= 1; } m_extendedData = reader.readString(extendedHeaderSize); } - const uint32 childCount = reader.readUInt32BE(); - for (uint32 i = 0; i != childCount; ++i) { + const std::uint32_t childCount = reader.readUInt32BE(); + for (std::uint32_t i = 0; i != childCount; ++i) { Entry::parse(stream)->setParent(this); } } @@ -400,7 +400,7 @@ void NodeEntry::make(ostream &stream) const writer.writeLengthPrefixedString(label()); if (!isExpandedByDefault() || !m_extendedData.empty()) { writer.writeUInt16BE(1 + m_extendedData.size()); // extended header is 1 byte long - byte flags = 0x00; + std::uint8_t flags = 0x00; if (isExpandedByDefault()) { flags |= 0x80; } @@ -452,7 +452,7 @@ AccountEntry::AccountEntry(const string &label, NodeEntry *parent) AccountEntry::AccountEntry(istream &stream) { BinaryReader reader(&stream); - byte version = reader.readByte(); + std::uint8_t version = reader.readByte(); if (denotesNodeEntry(version)) { throw ParsingException("Account entry expected."); } @@ -463,12 +463,12 @@ AccountEntry::AccountEntry(istream &stream) setLabel(reader.readLengthPrefixedString()); // read extended header for version 0x1 if (version == 0x1) { - const uint16 extendedHeaderSize = reader.readUInt16BE(); + const std::uint16_t extendedHeaderSize = reader.readUInt16BE(); // currently there's nothing to read here m_extendedData = reader.readString(extendedHeaderSize); } - const uint32 fieldCount = reader.readUInt32BE(); - for (uint32 i = 0; i != fieldCount; ++i) { + const std::uint32_t fieldCount = reader.readUInt32BE(); + for (std::uint32_t i = 0; i != fieldCount; ++i) { m_fields.push_back(Field(this, stream)); } } diff --git a/io/entry.h b/io/entry.h index 21dae44..8a6a95a 100644 --- a/io/entry.h +++ b/io/entry.h @@ -3,8 +3,7 @@ #include "./field.h" -#include - +#include #include #include #include @@ -49,8 +48,8 @@ public: EntryStatistics computeStatistics() const; virtual void accumulateStatistics(EntryStatistics &stats) const = 0; static Entry *parse(std::istream &stream); - static bool denotesNodeEntry(byte version); - static constexpr EntryType denotedEntryType(byte version); + static bool denotesNodeEntry(std::uint8_t version); + static constexpr EntryType denotedEntryType(std::uint8_t version); protected: Entry(const std::string &label = std::string(), NodeEntry *parent = nullptr); @@ -158,12 +157,12 @@ inline void NodeEntry::setExpandedByDefault(bool expandedByDefault) m_expandedByDefault = expandedByDefault; } -inline bool Entry::denotesNodeEntry(byte version) +inline bool Entry::denotesNodeEntry(std::uint8_t version) { return (version & 0x80) == 0; } -constexpr EntryType Entry::denotedEntryType(byte version) +constexpr EntryType Entry::denotedEntryType(std::uint8_t version) { return (version & 0x80) == 0 ? EntryType::Node : EntryType::Account; } diff --git a/io/field.cpp b/io/field.cpp index c97361f..6168933 100644 --- a/io/field.cpp +++ b/io/field.cpp @@ -42,14 +42,14 @@ Field::Field(AccountEntry *tiedAccount, istream &stream) } m_name = reader.readLengthPrefixedString(); m_value = reader.readLengthPrefixedString(); - byte type = reader.readByte(); + std::uint8_t type = reader.readByte(); if (!isValidType(type)) { throw ParsingException("Field type is not supported."); } m_type = static_cast(type); // read extended header for version 0x1 if (version == 0x1) { - const uint16 extendedHeaderSize = reader.readUInt16BE(); + const std::uint16_t extendedHeaderSize = reader.readUInt16BE(); // currently there's nothing to read here m_extendedData = reader.readString(extendedHeaderSize); } @@ -65,7 +65,7 @@ void Field::make(ostream &stream) const writer.writeByte(m_extendedData.empty() ? 0x0 : 0x1); // version writer.writeLengthPrefixedString(m_name); writer.writeLengthPrefixedString(m_value); - writer.writeByte(static_cast(m_type)); + writer.writeByte(static_cast(m_type)); if (!m_extendedData.empty()) { writer.writeUInt16BE(m_extendedData.size()); writer.writeString(m_extendedData); diff --git a/io/passwordfile.cpp b/io/passwordfile.cpp index dd01b00..b928f8b 100644 --- a/io/passwordfile.cpp +++ b/io/passwordfile.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -118,7 +117,7 @@ void PasswordFile::open(PasswordFileOpenFlags options) { close(); if (m_path.empty()) { - throwIoFailure("Unable to open file because path is emtpy."); + throw std::ios_base::failure("Unable to open file because path is emtpy."); } m_file.open( m_path, options & PasswordFileOpenFlags::ReadOnly ? ios_base::in | ios_base::binary : ios_base::in | ios_base::out | ios_base::binary); @@ -135,7 +134,7 @@ void PasswordFile::opened() { m_file.seekg(0, ios_base::end); if (m_file.tellg() == 0) { - throwIoFailure("File is empty."); + throw std::ios_base::failure("File is empty."); } else { m_file.seekg(0); } @@ -159,7 +158,7 @@ void PasswordFile::create() { close(); if (m_path.empty()) { - throwIoFailure("Unable to create file because path is empty."); + throw std::ios_base::failure("Unable to create file because path is empty."); } m_file.open(m_path, fstream::out | fstream::trunc | fstream::binary); } @@ -216,7 +215,7 @@ void PasswordFile::load() // (the extended header might be used in further versions to // add additional information without breaking compatibility) if (m_version >= 0x4U) { - uint16 extendedHeaderSize = m_freader.readUInt16BE(); + std::uint16_t extendedHeaderSize = m_freader.readUInt16BE(); m_extendedHeader = m_freader.readString(extendedHeaderSize); } else { m_extendedHeader.clear(); @@ -362,12 +361,11 @@ void PasswordFile::load() m_encryptedExtendedHeader.clear(); } m_rootEntry.reset(new NodeEntry(decryptedStream)); - } catch (...) { - const char *const what = catchIoFailure(); + } catch (const std::ios_base::failure &failure) { if (decryptedStream.eof()) { throw ParsingException("The file seems to be truncated."); } - throw ParsingException(argsToString("An IO error occurred when reading internal buffer: ", what)); + throw ParsingException(argsToString("An IO error occurred when reading internal buffer: ", failure.what())); } } @@ -375,7 +373,7 @@ void PasswordFile::load() * \brief Returns the minimum file version required to write the current instance with the specified \a options. * \remarks This version will be used by save() and write() when passing the same \a options. */ -uint32 PasswordFile::mininumVersion(PasswordFileSaveFlags options) const +std::uint32_t PasswordFile::mininumVersion(PasswordFileSaveFlags options) const { if (options & PasswordFileSaveFlags::PasswordHashing) { return 0x6U; // password hashing requires at least version 6 @@ -434,7 +432,7 @@ void PasswordFile::write(PasswordFileSaveFlags options) m_fwriter.writeUInt32LE(version); // write flags - byte flags = 0x00; + std::uint8_t flags = 0x00; if (options & PasswordFileSaveFlags::Encryption) { flags |= 0x80 | 0x40; } @@ -445,10 +443,10 @@ void PasswordFile::write(PasswordFileSaveFlags options) // write extened header if (version >= 0x4U) { - if (m_extendedHeader.size() > numeric_limits::max()) { + if (m_extendedHeader.size() > numeric_limits::max()) { throw runtime_error("Extended header exceeds maximum size."); } - m_fwriter.writeUInt16BE(static_cast(m_extendedHeader.size())); + m_fwriter.writeUInt16BE(static_cast(m_extendedHeader.size())); m_fwriter.writeString(m_extendedHeader); } @@ -458,11 +456,11 @@ void PasswordFile::write(PasswordFileSaveFlags options) // write encrypted extened header if (version >= 0x5U) { - if (m_encryptedExtendedHeader.size() > numeric_limits::max()) { + if (m_encryptedExtendedHeader.size() > numeric_limits::max()) { throw runtime_error("Encrypted extended header exceeds maximum size."); } BinaryWriter buffstrWriter(&buffstr); - buffstrWriter.writeUInt16BE(static_cast(m_encryptedExtendedHeader.size())); + buffstrWriter.writeUInt16BE(static_cast(m_encryptedExtendedHeader.size())); buffstrWriter.writeString(m_encryptedExtendedHeader); } m_rootEntry->make(buffstr); @@ -479,7 +477,7 @@ void PasswordFile::write(PasswordFileSaveFlags options) if (options & PasswordFileSaveFlags::Compression) { uLongf compressedSize = compressBound(size); encryptedData.resize(8 + compressedSize); - ConversionUtilities::LE::getBytes(static_cast(size), encryptedData.data()); + ConversionUtilities::LE::getBytes(static_cast(size), encryptedData.data()); switch ( compress(reinterpret_cast(encryptedData.data() + 8), &compressedSize, reinterpret_cast(decryptedData.data()), size)) { case Z_MEM_ERROR: diff --git a/io/passwordfile.h b/io/passwordfile.h index 4e70cde..1d24f89 100644 --- a/io/passwordfile.h +++ b/io/passwordfile.h @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -16,7 +17,7 @@ namespace Io { class NodeEntry; -enum class PasswordFileOpenFlags : uint64 { +enum class PasswordFileOpenFlags : std::uint64_t { None = 0, ReadOnly = 1, Default = None, @@ -42,7 +43,7 @@ constexpr bool operator&(PasswordFileOpenFlags lhs, PasswordFileOpenFlags rhs) static_cast::type>(lhs) & static_cast::type>(rhs)); } -enum class PasswordFileSaveFlags : uint64 { +enum class PasswordFileSaveFlags : std::uint64_t { None = 0, Encryption = 1, Compression = 2, @@ -84,7 +85,7 @@ public: void create(); void close(); void load(); - uint32 mininumVersion(PasswordFileSaveFlags options) const; + std::uint32_t mininumVersion(PasswordFileSaveFlags options) const; void save(PasswordFileSaveFlags options = PasswordFileSaveFlags::Default); void write(PasswordFileSaveFlags options = PasswordFileSaveFlags::Default); void clearEntries(); @@ -108,7 +109,7 @@ public: std::string &encryptedExtendedHeader(); const std::string &encryptedExtendedHeader() const; std::size_t size(); - uint32 version() const; + std::uint32_t version() const; PasswordFileOpenFlags openOptions() const; PasswordFileSaveFlags saveOptions() const; std::string summary(PasswordFileSaveFlags saveOptions) const; @@ -122,7 +123,7 @@ private: IoUtilities::NativeFileStream m_file; IoUtilities::BinaryReader m_freader; IoUtilities::BinaryWriter m_fwriter; - uint32 m_version; + std::uint32_t m_version; PasswordFileOpenFlags m_openOptions; PasswordFileSaveFlags m_saveOptions; }; @@ -228,7 +229,7 @@ inline const std::string &PasswordFile::encryptedExtendedHeader() const * \brief Returns the file version used the last time when saving the file (the version of the file as it is on the disk). * \remarks The version might change when re-saving with different options. See mininumVersion(). */ -inline uint32 PasswordFile::version() const +inline std::uint32_t PasswordFile::version() const { return m_version; } diff --git a/tests/passwordfiletests.cpp b/tests/passwordfiletests.cpp index 1f3e7c6..9aa96a9 100644 --- a/tests/passwordfiletests.cpp +++ b/tests/passwordfiletests.cpp @@ -124,17 +124,17 @@ void PasswordFileTests::testReading(const string &context, const string &testfil const NodeEntry *const rootEntry2 = file.rootEntry(); if (testfilesMod) { if (extendedHeaderMod) { - CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast(6), file.version()); + CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast(6), file.version()); CPPUNIT_ASSERT_EQUAL_MESSAGE(context, "encryption, password hashing"s, flagsToString(file.saveOptions())); } else { - CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast(3), file.version()); + CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast(3), file.version()); CPPUNIT_ASSERT_EQUAL_MESSAGE(context, "encryption"s, flagsToString(file.saveOptions())); } CPPUNIT_ASSERT_EQUAL_MESSAGE(context, "testfile2 - modified"s, rootEntry2->label()); CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 2_st, rootEntry2->children().size()); CPPUNIT_ASSERT_EQUAL_MESSAGE(context, "newAccount"s, rootEntry2->children()[1]->label()); } else { - CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast(3), file.version()); + CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast(3), file.version()); CPPUNIT_ASSERT_EQUAL_MESSAGE(context, "testfile2"s, rootEntry2->label()); CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 1_st, rootEntry2->children().size()); } diff --git a/util/opensslrandomdevice.h b/util/opensslrandomdevice.h index 7133fc4..81ab670 100644 --- a/util/opensslrandomdevice.h +++ b/util/opensslrandomdevice.h @@ -3,15 +3,14 @@ #include "../global.h" -#include - +#include #include namespace Util { class PASSWORD_FILE_EXPORT OpenSslRandomDevice { public: - using result_type = uint32; + using result_type = std::uint32_t; OpenSslRandomDevice(); result_type operator()() const;