Apply clang-format

This commit is contained in:
Martchus 2017-05-01 03:25:30 +02:00
parent d4bb111800
commit 29220159fa
16 changed files with 209 additions and 219 deletions

3
.gitignore vendored
View File

@ -42,3 +42,6 @@ Makefile*
# tests # tests
*.backup *.backup
# clang-format
/.clang-format

View File

@ -9,14 +9,14 @@ namespace Io {
/*! /*!
* \brief Constructs a crypto exception. * \brief Constructs a crypto exception.
*/ */
CryptoException::CryptoException(const std::string &openSslErrorQueue) USE_NOTHROW : CryptoException::CryptoException(const std::string &openSslErrorQueue) USE_NOTHROW : runtime_error(openSslErrorQueue)
runtime_error(openSslErrorQueue) {
{} }
/*! /*!
* \brief Destroys the exception. * \brief Destroys the exception.
*/ */
CryptoException::~CryptoException() USE_NOTHROW CryptoException::~CryptoException() USE_NOTHROW
{} {
}
} }

View File

@ -8,13 +8,11 @@
namespace Io { namespace Io {
class PASSWORD_FILE_EXPORT CryptoException : public std::runtime_error class PASSWORD_FILE_EXPORT CryptoException : public std::runtime_error {
{
public: public:
CryptoException(const std::string &openSslErrorQueue) USE_NOTHROW; CryptoException(const std::string &openSslErrorQueue) USE_NOTHROW;
virtual ~CryptoException() USE_NOTHROW; virtual ~CryptoException() USE_NOTHROW;
}; };
} }
#endif // CRYPTOFAILUREEXCEPTION_H #endif // CRYPTOFAILUREEXCEPTION_H

View File

@ -4,8 +4,8 @@
#include <c++utilities/io/binaryreader.h> #include <c++utilities/io/binaryreader.h>
#include <c++utilities/io/binarywriter.h> #include <c++utilities/io/binarywriter.h>
#include <sstream>
#include <algorithm> #include <algorithm>
#include <sstream>
using namespace std; using namespace std;
using namespace IoUtilities; using namespace IoUtilities;
@ -29,9 +29,9 @@ namespace Io {
/*! /*!
* \brief Constructs a new entry with the specified \a label and \a parent. * \brief Constructs a new entry with the specified \a label and \a parent.
*/ */
Entry::Entry(const string &label, NodeEntry *parent) : Entry::Entry(const string &label, NodeEntry *parent)
m_parent(nullptr), : m_parent(nullptr)
m_index(-1) , m_index(-1)
{ {
setParent(parent); setParent(parent);
setLabel(label); setLabel(label);
@ -42,11 +42,12 @@ Entry::Entry(const string &label, NodeEntry *parent) :
* \remarks The copy will be parentless and thus not be embedded in the hierarchy * \remarks The copy will be parentless and thus not be embedded in the hierarchy
* of \a other. Child entries will be copied as well. * of \a other. Child entries will be copied as well.
*/ */
Entry::Entry(const Entry &other) : Entry::Entry(const Entry &other)
m_label(other.m_label), : m_label(other.m_label)
m_parent(nullptr), , m_parent(nullptr)
m_index(-1) , m_index(-1)
{} {
}
/*! /*!
* \brief Destroys the entry. * \brief Destroys the entry.
@ -102,8 +103,7 @@ void Entry::setParent(NodeEntry *parent, int index)
m_index = parent->m_children.size(); m_index = parent->m_children.size();
parent->m_children.push_back(this); parent->m_children.push_back(this);
} else { } else {
for(auto i = parent->m_children.insert(parent->m_children.begin() + index, this) + 1; for (auto i = parent->m_children.insert(parent->m_children.begin() + index, this) + 1; i != parent->m_children.end(); ++i) {
i != parent->m_children.end(); ++i) {
(*i)->m_index += 1; (*i)->m_index += 1;
} }
m_index = index; m_index = index;
@ -192,24 +192,26 @@ Entry *Entry::parse(istream &stream)
/*! /*!
* \brief Constructs a new node entry. * \brief Constructs a new node entry.
*/ */
NodeEntry::NodeEntry() : NodeEntry::NodeEntry()
Entry(), : Entry()
m_expandedByDefault(true) , m_expandedByDefault(true)
{} {
}
/*! /*!
* \brief Constructs a new node entry with the specified \a label and \a parent. * \brief Constructs a new node entry with the specified \a label and \a parent.
*/ */
NodeEntry::NodeEntry(const string &label, NodeEntry *parent) : NodeEntry::NodeEntry(const string &label, NodeEntry *parent)
Entry(label, parent), : Entry(label, parent)
m_expandedByDefault(true) , m_expandedByDefault(true)
{} {
}
/*! /*!
* \brief Constructs a new node entry which is deserialized from the specified \a stream. * \brief Constructs a new node entry which is deserialized from the specified \a stream.
*/ */
NodeEntry::NodeEntry(istream &stream) : NodeEntry::NodeEntry(istream &stream)
m_expandedByDefault(true) : m_expandedByDefault(true)
{ {
BinaryReader reader(&stream); BinaryReader reader(&stream);
byte version = reader.readByte(); byte version = reader.readByte();
@ -242,8 +244,8 @@ NodeEntry::NodeEntry(istream &stream) :
* \remarks The copy will be parentless and thus not be embedded in the hierarchy * \remarks The copy will be parentless and thus not be embedded in the hierarchy
* of \a other. Child entries will be copied as well. * of \a other. Child entries will be copied as well.
*/ */
NodeEntry::NodeEntry(const NodeEntry &other) : NodeEntry::NodeEntry(const NodeEntry &other)
Entry(other) : Entry(other)
{ {
for (Entry *otherChild : other.m_children) { for (Entry *otherChild : other.m_children) {
Entry *clonedChild = otherChild->clone(); Entry *clonedChild = otherChild->clone();
@ -373,14 +375,16 @@ NodeEntry *NodeEntry::clone() const
*/ */
AccountEntry::AccountEntry() AccountEntry::AccountEntry()
{} {
}
/*! /*!
* \brief Constructs a new account entry with the specified \a label and \a parent. * \brief Constructs a new account entry with the specified \a label and \a parent.
*/ */
AccountEntry::AccountEntry(const string &label, NodeEntry *parent) : AccountEntry::AccountEntry(const string &label, NodeEntry *parent)
Entry(label, parent) : Entry(label, parent)
{} {
}
/*! /*!
* \brief Constructs a new account entry which is deserialized from the specified \a stream. * \brief Constructs a new account entry which is deserialized from the specified \a stream.
@ -415,8 +419,8 @@ AccountEntry::AccountEntry(istream &stream)
* \remarks The copy will be parentless and thus not be embedded in the hierarchy * \remarks The copy will be parentless and thus not be embedded in the hierarchy
* of \a other. Child entries will be copied as well. * of \a other. Child entries will be copied as well.
*/ */
AccountEntry::AccountEntry(const AccountEntry &other) : AccountEntry::AccountEntry(const AccountEntry &other)
Entry(other) : Entry(other)
{ {
m_fields = other.m_fields; m_fields = other.m_fields;
} }
@ -425,7 +429,8 @@ AccountEntry::AccountEntry(const AccountEntry &other) :
* \brief Destroys the entry. * \brief Destroys the entry.
*/ */
AccountEntry::~AccountEntry() AccountEntry::~AccountEntry()
{} {
}
void AccountEntry::make(ostream &stream) const void AccountEntry::make(ostream &stream) const
{ {
@ -446,5 +451,4 @@ AccountEntry *AccountEntry::clone() const
{ {
return new AccountEntry(*this); return new AccountEntry(*this);
} }
} }

View File

@ -6,26 +6,25 @@
#include <c++utilities/conversion/types.h> #include <c++utilities/conversion/types.h>
#include <iostream> #include <iostream>
#include <list>
#include <string> #include <string>
#include <vector> #include <vector>
#include <list>
namespace Io { namespace Io {
/*! /*!
* \brief Specifies the entry type. * \brief Specifies the entry type.
*/ */
enum class EntryType : int enum class EntryType : int {
{
Node, /**< denotes a NodeEntry */ Node, /**< denotes a NodeEntry */
Account /**< denotes an AccountEntry */ Account /**< denotes an AccountEntry */
}; };
class NodeEntry; class NodeEntry;
class PASSWORD_FILE_EXPORT Entry class PASSWORD_FILE_EXPORT Entry {
{
friend class NodeEntry; friend class NodeEntry;
public: public:
virtual ~Entry(); virtual ~Entry();
Entry &operator=(const Entry &other) = delete; Entry &operator=(const Entry &other) = delete;
@ -55,7 +54,6 @@ private:
protected: protected:
std::string m_extendedData; std::string m_extendedData;
}; };
/*! /*!
@ -94,9 +92,9 @@ inline int Entry::index() const
return m_index; return m_index;
} }
class PASSWORD_FILE_EXPORT NodeEntry : public Entry class PASSWORD_FILE_EXPORT NodeEntry : public Entry {
{
friend class Entry; friend class Entry;
public: public:
NodeEntry(); NodeEntry();
NodeEntry(const std::string &label, NodeEntry *parent = nullptr); NodeEntry(const std::string &label, NodeEntry *parent = nullptr);
@ -144,8 +142,7 @@ inline bool Entry::denotesNodeEntry(byte version)
return (version & 0x80) == 0; return (version & 0x80) == 0;
} }
class PASSWORD_FILE_EXPORT AccountEntry : public Entry class PASSWORD_FILE_EXPORT AccountEntry : public Entry {
{
public: public:
AccountEntry(); AccountEntry();
AccountEntry(const std::string &label, NodeEntry *parent = nullptr); AccountEntry(const std::string &label, NodeEntry *parent = nullptr);
@ -158,6 +155,7 @@ public:
std::vector<Field> &fields(); std::vector<Field> &fields();
virtual void make(std::ostream &stream) const; virtual void make(std::ostream &stream) const;
virtual AccountEntry *clone() const; virtual AccountEntry *clone() const;
private: private:
std::vector<Field> m_fields; std::vector<Field> m_fields;
}; };
@ -176,7 +174,6 @@ inline std::vector<Field> &AccountEntry::fields()
{ {
return m_fields; return m_fields;
} }
} }
#endif // ENTRY_H #endif // ENTRY_H

View File

@ -20,12 +20,13 @@ namespace Io {
* \brief Constructs a new account entry for the specified account with the specified \a name * \brief Constructs a new account entry for the specified account with the specified \a name
* and \a value. * and \a value.
*/ */
Field::Field(AccountEntry *tiedAccount, const string &name, const string &value) : Field::Field(AccountEntry *tiedAccount, const string &name, const string &value)
m_name(name), : m_name(name)
m_value(value), , m_value(value)
m_type(FieldType::Normal), , m_type(FieldType::Normal)
m_tiedAccount(tiedAccount) , m_tiedAccount(tiedAccount)
{} {
}
/*! /*!
* \brief Constructs a new account entry for the specified account which is deserialize from * \brief Constructs a new account entry for the specified account which is deserialize from
@ -71,5 +72,4 @@ void Field::make(ostream &stream) const
writer.writeString(m_extendedData); writer.writeString(m_extendedData);
} }
} }
} }

View File

@ -8,16 +8,11 @@
namespace Io { namespace Io {
enum class FieldType : int enum class FieldType : int { Normal, Password };
{
Normal,
Password
};
class AccountEntry; class AccountEntry;
class PASSWORD_FILE_EXPORT Field class PASSWORD_FILE_EXPORT Field {
{
public: public:
Field(AccountEntry *tiedAccount, const std::string &name = std::string(), const std::string &value = std::string()); Field(AccountEntry *tiedAccount, const std::string &name = std::string(), const std::string &value = std::string());
Field(AccountEntry *tiedAccount, std::istream &stream); Field(AccountEntry *tiedAccount, std::istream &stream);
@ -41,7 +36,6 @@ private:
protected: protected:
std::string m_extendedData; std::string m_extendedData;
}; };
/*! /*!
@ -115,7 +109,6 @@ inline bool Field::isValidType(int number)
{ {
return number >= 0 && number <= 1; return number >= 0 && number <= 1;
} }
} }
#endif // FIELD_H #endif // FIELD_H

View File

@ -10,14 +10,14 @@ namespace Io {
/*! /*!
* \brief Constructs a parsing exception. * \brief Constructs a parsing exception.
*/ */
ParsingException::ParsingException(const std::string &message) USE_NOTHROW : ParsingException::ParsingException(const std::string &message) USE_NOTHROW : runtime_error(message)
runtime_error(message) {
{} }
/*! /*!
* \brief Destroys the exception. * \brief Destroys the exception.
*/ */
ParsingException::~ParsingException() USE_NOTHROW ParsingException::~ParsingException() USE_NOTHROW
{} {
}
} }

View File

@ -8,13 +8,11 @@
namespace Io { namespace Io {
class PASSWORD_FILE_EXPORT ParsingException : public std::runtime_error class PASSWORD_FILE_EXPORT ParsingException : public std::runtime_error {
{
public: public:
ParsingException(const std::string &message = std::string()) USE_NOTHROW; ParsingException(const std::string &message = std::string()) USE_NOTHROW;
virtual ~ParsingException() USE_NOTHROW; virtual ~ParsingException() USE_NOTHROW;
}; };
} }
#endif // PARSINGEXCEPTION_H #endif // PARSINGEXCEPTION_H

View File

@ -1,7 +1,7 @@
#include "./passwordfile.h" #include "./passwordfile.h"
#include "./cryptoexception.h" #include "./cryptoexception.h"
#include "./parsingexception.h"
#include "./entry.h" #include "./entry.h"
#include "./parsingexception.h"
#include <c++utilities/io/catchiofailure.h> #include <c++utilities/io/catchiofailure.h>
@ -12,11 +12,11 @@
#include <zlib.h> #include <zlib.h>
#include <streambuf>
#include <sstream>
#include <cstring> #include <cstring>
#include <memory>
#include <functional> #include <functional>
#include <memory>
#include <sstream>
#include <streambuf>
using namespace std; using namespace std;
using namespace IoUtilities; using namespace IoUtilities;
@ -34,9 +34,9 @@ const unsigned int aes256cbcIvSize = 16U;
/*! /*!
* \brief Constructs a new password file. * \brief Constructs a new password file.
*/ */
PasswordFile::PasswordFile() : PasswordFile::PasswordFile()
m_freader(BinaryReader(&m_file)), : m_freader(BinaryReader(&m_file))
m_fwriter(BinaryWriter(&m_file)) , m_fwriter(BinaryWriter(&m_file))
{ {
m_file.exceptions(ios_base::failbit | ios_base::badbit); m_file.exceptions(ios_base::failbit | ios_base::badbit);
clearPassword(); clearPassword();
@ -45,9 +45,9 @@ PasswordFile::PasswordFile() :
/*! /*!
* \brief Constructs a new password file with the specified \a path and \a password. * \brief Constructs a new password file with the specified \a path and \a password.
*/ */
PasswordFile::PasswordFile(const string &path, const string &password) : PasswordFile::PasswordFile(const string &path, const string &password)
m_freader(BinaryReader(&m_file)), : m_freader(BinaryReader(&m_file))
m_fwriter(BinaryWriter(&m_file)) , m_fwriter(BinaryWriter(&m_file))
{ {
m_file.exceptions(ios_base::failbit | ios_base::badbit); m_file.exceptions(ios_base::failbit | ios_base::badbit);
setPath(path); setPath(path);
@ -57,10 +57,10 @@ PasswordFile::PasswordFile(const string &path, const string &password) :
/*! /*!
* \brief Constructs a copy of another password file. * \brief Constructs a copy of another password file.
*/ */
PasswordFile::PasswordFile(const PasswordFile &other) : PasswordFile::PasswordFile(const PasswordFile &other)
m_path(other.m_path), : m_path(other.m_path)
m_freader(BinaryReader(&m_file)), , m_freader(BinaryReader(&m_file))
m_fwriter(BinaryWriter(&m_file)) , m_fwriter(BinaryWriter(&m_file))
{ {
m_file.exceptions(ios_base::failbit | ios_base::badbit); m_file.exceptions(ios_base::failbit | ios_base::badbit);
setPath(other.path()); setPath(other.path());
@ -189,7 +189,9 @@ void PasswordFile::load()
int outlen1, outlen2; int outlen1, outlen2;
if ((ctx = EVP_CIPHER_CTX_new()) == nullptr if ((ctx = EVP_CIPHER_CTX_new()) == nullptr
|| EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, reinterpret_cast<unsigned const char *>(m_password), iv) != 1 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, reinterpret_cast<unsigned const char *>(m_password), iv) != 1
|| EVP_DecryptUpdate(ctx, reinterpret_cast<unsigned char *>(decbuff.data()), &outlen1, reinterpret_cast<unsigned char *>(rawbuff.data()), size) != 1 || EVP_DecryptUpdate(
ctx, reinterpret_cast<unsigned char *>(decbuff.data()), &outlen1, reinterpret_cast<unsigned char *>(rawbuff.data()), size)
!= 1
|| EVP_DecryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(decbuff.data()) + outlen1, &outlen2) != 1) { || EVP_DecryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(decbuff.data()) + outlen1, &outlen2) != 1) {
if (ctx) { if (ctx) {
EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_CTX_free(ctx);
@ -220,7 +222,8 @@ void PasswordFile::load()
} }
uLongf decompressedSize = ConversionUtilities::LE::toUInt64(decbuff.data()); uLongf decompressedSize = ConversionUtilities::LE::toUInt64(decbuff.data());
rawbuff.resize(decompressedSize); rawbuff.resize(decompressedSize);
switch(uncompress(reinterpret_cast<Bytef *>(rawbuff.data()), &decompressedSize, reinterpret_cast<Bytef *>(decbuff.data() + 8), size - static_cast<fstream::pos_type>(8))) { switch (uncompress(reinterpret_cast<Bytef *>(rawbuff.data()), &decompressedSize, reinterpret_cast<Bytef *>(decbuff.data() + 8),
size - static_cast<fstream::pos_type>(8))) {
case Z_MEM_ERROR: case Z_MEM_ERROR:
throw ParsingException("Decompressing failed. The source buffer was too small."); throw ParsingException("Decompressing failed. The source buffer was too small.");
case Z_BUF_ERROR: case Z_BUF_ERROR:
@ -318,10 +321,11 @@ void PasswordFile::save(bool useEncryption, bool useCompression)
unsigned char iv[aes256cbcIvSize]; unsigned char iv[aes256cbcIvSize];
int outlen1, outlen2; int outlen1, outlen2;
encbuff.resize(size + static_cast<fstream::pos_type>(32)); encbuff.resize(size + static_cast<fstream::pos_type>(32));
if (RAND_bytes(iv, aes256cbcIvSize) != 1 if (RAND_bytes(iv, aes256cbcIvSize) != 1 || (ctx = EVP_CIPHER_CTX_new()) == nullptr
|| (ctx = EVP_CIPHER_CTX_new()) == nullptr
|| EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, reinterpret_cast<unsigned const char *>(m_password), iv) != 1 || EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), nullptr, reinterpret_cast<unsigned const char *>(m_password), iv) != 1
|| EVP_EncryptUpdate(ctx, reinterpret_cast<unsigned char *>(encbuff.data()), &outlen1, reinterpret_cast<unsigned char *>(decbuff.data()), size) != 1 || EVP_EncryptUpdate(
ctx, reinterpret_cast<unsigned char *>(encbuff.data()), &outlen1, reinterpret_cast<unsigned char *>(decbuff.data()), size)
!= 1
|| EVP_EncryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(encbuff.data()) + outlen1, &outlen2) != 1) { || EVP_EncryptFinal_ex(ctx, reinterpret_cast<unsigned char *>(encbuff.data()) + outlen1, &outlen2) != 1) {
if (ctx) { if (ctx) {
EVP_CIPHER_CTX_free(ctx); EVP_CIPHER_CTX_free(ctx);
@ -566,5 +570,4 @@ size_t PasswordFile::size()
m_file.seekg(0, ios::end); m_file.seekg(0, ios::end);
return m_file.tellg(); return m_file.tellg();
} }
} }

View File

@ -6,17 +6,16 @@
#include <c++utilities/io/binaryreader.h> #include <c++utilities/io/binaryreader.h>
#include <c++utilities/io/binarywriter.h> #include <c++utilities/io/binarywriter.h>
#include <string>
#include <iostream>
#include <fstream> #include <fstream>
#include <iostream>
#include <memory> #include <memory>
#include <string>
namespace Io { namespace Io {
class NodeEntry; class NodeEntry;
class PASSWORD_FILE_EXPORT PasswordFile class PASSWORD_FILE_EXPORT PasswordFile {
{
public: public:
explicit PasswordFile(); explicit PasswordFile();
explicit PasswordFile(const std::string &path, const std::string &password); explicit PasswordFile(const std::string &path, const std::string &password);
@ -44,6 +43,7 @@ public:
bool isEncryptionUsed(); bool isEncryptionUsed();
bool isOpen() const; bool isOpen() const;
size_t size(); size_t size();
private: private:
std::string m_path; std::string m_path;
char m_password[32]; char m_password[32];
@ -54,7 +54,6 @@ private:
IoUtilities::BinaryReader m_freader; IoUtilities::BinaryReader m_freader;
IoUtilities::BinaryWriter m_fwriter; IoUtilities::BinaryWriter m_fwriter;
}; };
} }
#endif // PASSWORDFILE_H #endif // PASSWORDFILE_H

View File

@ -35,7 +35,5 @@ void clean()
// remove error strings // remove error strings
ERR_free_strings(); ERR_free_strings();
} }
} }
} }

View File

@ -9,9 +9,7 @@ namespace OpenSsl {
void PASSWORD_FILE_EXPORT init(); void PASSWORD_FILE_EXPORT init();
void PASSWORD_FILE_EXPORT clean(); void PASSWORD_FILE_EXPORT clean();
} }
} }
#endif // OPENSSL_H #endif // OPENSSL_H

View File

@ -4,8 +4,8 @@
#include <c++utilities/conversion/binaryconversion.h> #include <c++utilities/conversion/binaryconversion.h>
#include <openssl/rand.h>
#include <openssl/err.h> #include <openssl/err.h>
#include <openssl/rand.h>
#include <string> #include <string>
@ -27,12 +27,14 @@ namespace Util {
* \brief Constructs a new random device. * \brief Constructs a new random device.
*/ */
OpenSslRandomDevice::OpenSslRandomDevice() OpenSslRandomDevice::OpenSslRandomDevice()
{} {
}
/*! /*!
* \brief Generates a new random number. * \brief Generates a new random number.
*/ */
uint32 OpenSslRandomDevice::operator ()() const { uint32 OpenSslRandomDevice::operator()() const
{
unsigned char buf[4]; unsigned char buf[4];
if (RAND_bytes(buf, sizeof(buf))) { if (RAND_bytes(buf, sizeof(buf))) {
return ConversionUtilities::LE::toUInt32(reinterpret_cast<char *>(buf)); return ConversionUtilities::LE::toUInt32(reinterpret_cast<char *>(buf));
@ -57,5 +59,4 @@ bool OpenSslRandomDevice::status() const
{ {
return RAND_status(); return RAND_status();
} }
} }

View File

@ -7,14 +7,12 @@
namespace Util { namespace Util {
class PASSWORD_FILE_EXPORT OpenSslRandomDevice class PASSWORD_FILE_EXPORT OpenSslRandomDevice {
{
public: public:
OpenSslRandomDevice(); OpenSslRandomDevice();
uint32 operator()() const; uint32 operator()() const;
bool status() const; bool status() const;
}; };
} }
#endif // OPENSSLRANDOMDEVICE_H #endif // OPENSSLRANDOMDEVICE_H