Adapt to c++utilities v5

This commit is contained in:
Marius Kittler 2019-03-13 19:08:30 +01:00 committed by Martchus
parent 986ceaf567
commit 0754760b19
8 changed files with 52 additions and 63 deletions

View File

@ -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)

View File

@ -176,7 +176,7 @@ void Entry::path(std::list<string> &res) const
*/
Entry *Entry::parse(istream &stream)
{
const auto version = static_cast<byte>(stream.peek());
const auto version = static_cast<std::uint8_t>(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));
}
}

View File

@ -3,8 +3,7 @@
#include "./field.h"
#include <c++utilities/conversion/types.h>
#include <cstdint>
#include <iostream>
#include <list>
#include <string>
@ -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;
}

View File

@ -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<FieldType>(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<byte>(m_type));
writer.writeByte(static_cast<std::uint8_t>(m_type));
if (!m_extendedData.empty()) {
writer.writeUInt16BE(m_extendedData.size());
writer.writeString(m_extendedData);

View File

@ -8,7 +8,6 @@
#include <c++utilities/conversion/stringbuilder.h>
#include <c++utilities/conversion/stringconversion.h>
#include <c++utilities/io/catchiofailure.h>
#include <openssl/conf.h>
#include <openssl/err.h>
@ -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<uint16>::max()) {
if (m_extendedHeader.size() > numeric_limits<std::uint16_t>::max()) {
throw runtime_error("Extended header exceeds maximum size.");
}
m_fwriter.writeUInt16BE(static_cast<uint16>(m_extendedHeader.size()));
m_fwriter.writeUInt16BE(static_cast<std::uint16_t>(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<uint16>::max()) {
if (m_encryptedExtendedHeader.size() > numeric_limits<std::uint16_t>::max()) {
throw runtime_error("Encrypted extended header exceeds maximum size.");
}
BinaryWriter buffstrWriter(&buffstr);
buffstrWriter.writeUInt16BE(static_cast<uint16>(m_encryptedExtendedHeader.size()));
buffstrWriter.writeUInt16BE(static_cast<std::uint16_t>(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<uint64>(size), encryptedData.data());
ConversionUtilities::LE::getBytes(static_cast<std::uint64_t>(size), encryptedData.data());
switch (
compress(reinterpret_cast<Bytef *>(encryptedData.data() + 8), &compressedSize, reinterpret_cast<Bytef *>(decryptedData.data()), size)) {
case Z_MEM_ERROR:

View File

@ -7,6 +7,7 @@
#include <c++utilities/io/binarywriter.h>
#include <c++utilities/io/nativefilestream.h>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <memory>
@ -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<std::underlying_type<PasswordFileOpenFlags>::type>(lhs) & static_cast<std::underlying_type<PasswordFileOpenFlags>::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;
}

View File

@ -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<uint32>(6), file.version());
CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast<std::uint32_t>(6), file.version());
CPPUNIT_ASSERT_EQUAL_MESSAGE(context, "encryption, password hashing"s, flagsToString(file.saveOptions()));
} else {
CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast<uint32>(3), file.version());
CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast<std::uint32_t>(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<uint32>(3), file.version());
CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast<std::uint32_t>(3), file.version());
CPPUNIT_ASSERT_EQUAL_MESSAGE(context, "testfile2"s, rootEntry2->label());
CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 1_st, rootEntry2->children().size());
}

View File

@ -3,15 +3,14 @@
#include "../global.h"
#include <c++utilities/conversion/types.h>
#include <cstdint>
#include <limits>
namespace Util {
class PASSWORD_FILE_EXPORT OpenSslRandomDevice {
public:
using result_type = uint32;
using result_type = std::uint32_t;
OpenSslRandomDevice();
result_type operator()() const;