Skip to content

Commit

Permalink
More uniform initialization and AAA style
Browse files Browse the repository at this point in the history
  • Loading branch information
kimci86 committed Feb 25, 2024
1 parent 136cdb6 commit aa9a625
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 314 deletions.
2 changes: 1 addition & 1 deletion include/ConsoleProgress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ConsoleProgress : public Progress
public:
/// Start a thread to print progress
explicit ConsoleProgress(std::ostream& os,
const std::chrono::milliseconds& interval = std::chrono::milliseconds(200));
const std::chrono::milliseconds& interval = std::chrono::milliseconds{200});

/// Notify and stop the printing thread
~ConsoleProgress();
Expand Down
2 changes: 1 addition & 1 deletion include/Progress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Progress
template <typename F>
void log(F f)
{
const std::scoped_lock lock{m_os_mutex};
const auto lock = std::scoped_lock{m_os_mutex};
f(m_os);
}

Expand Down
4 changes: 2 additions & 2 deletions include/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ constexpr auto msb(std::uint32_t x) -> std::uint8_t

/// Constant value for bit masking
template <int begin, int end>
constexpr std::uint32_t mask = ~0u << begin & ~0u >> (32 - end);
constexpr auto mask = std::uint32_t{~0u << begin & ~0u >> (32 - end)};

/// \brief Maximum difference between 32-bits integers A and B[x,32)
/// knowing that A = B + b and b is a byte.
Expand All @@ -48,6 +48,6 @@ constexpr std::uint32_t mask = ~0u << begin & ~0u >> (32 - end);
/// A - B[x,32) = B[0,x) + b
/// A - B[x,32) <= 0xffffffff[0,x) + 0xff
template <int x>
constexpr std::uint32_t maxdiff = mask<0, x> + 0xff;
constexpr auto maxdiff = std::uint32_t{mask<0, x> + 0xff};

#endif // BKCRACK_TYPES_HPP
119 changes: 59 additions & 60 deletions src/Arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace

auto charRange(char first, char last) -> std::bitset<256>
{
std::bitset<256> bitset;
auto bitset = std::bitset<256>{};

do
{
Expand All @@ -33,11 +33,11 @@ auto translateIntParseError(F&& f, const std::string& value)
}
catch (const std::invalid_argument&)
{
throw Arguments::Error("expected an integer, got \"" + value + "\"");
throw Arguments::Error{"expected an integer, got \"" + value + "\""};
}
catch (const std::out_of_range&)
{
throw Arguments::Error("integer value " + value + " is out of range");
throw Arguments::Error{"integer value " + value + " is out of range"};
}
}

Expand All @@ -53,11 +53,11 @@ auto parseSize(const std::string& value) -> std::size_t

auto parseInterval(const std::string& value) -> std::variant<Arguments::LengthInterval, std::size_t>
{
const std::string separator = "..";
const auto separator = std::string{".."};

if (const auto minEnd = value.find(separator); minEnd != std::string::npos)
{
Arguments::LengthInterval interval;
auto interval = Arguments::LengthInterval{};

if (0 < minEnd)
interval.minLength = parseSize(value.substr(0, minEnd));
Expand All @@ -74,7 +74,7 @@ auto parseInterval(const std::string& value) -> std::variant<Arguments::LengthIn
} // namespace

Arguments::Error::Error(const std::string& description)
: BaseError("Arguments error", description)
: BaseError{"Arguments error", description}
{
}

Expand All @@ -98,57 +98,57 @@ Arguments::Arguments(int argc, const char* argv[])
if (keys)
{
if (!decipheredFile && !changePassword && !changeKeys && !bruteforce)
throw Error("-d, -U, --change-keys or --bruteforce parameter is missing (required by -k)");
throw Error{"-d, -U, --change-keys or --bruteforce parameter is missing (required by -k)"};
}
else if (!password)
{
if (cipherFile && cipherIndex)
throw Error("-c and --cipher-index cannot be used at the same time");
throw Error{"-c and --cipher-index cannot be used at the same time"};
if (plainFile && plainIndex)
throw Error("-p and --plain-index cannot be used at the same time");
throw Error{"-p and --plain-index cannot be used at the same time"};

if (!cipherFile && !cipherIndex)
throw Error("-c or --cipher-index parameter is missing");
throw Error{"-c or --cipher-index parameter is missing"};
if (!plainFile && !plainIndex && extraPlaintext.empty())
throw Error("-p, --plain-index or -x parameter is missing");
throw Error{"-p, --plain-index or -x parameter is missing"};

if (plainArchive && !plainFile && !plainIndex)
throw Error("-p or --plain-index parameter is missing (required by -P)");
throw Error{"-p or --plain-index parameter is missing (required by -P)"};

if (cipherIndex && !cipherArchive)
throw Error("-C parameter is missing (required by --cipher-index)");
throw Error{"-C parameter is missing (required by --cipher-index)"};
if (plainIndex && !plainArchive)
throw Error("-P parameter is missing (required by --plain-index)");
throw Error{"-P parameter is missing (required by --plain-index)"};

constexpr int minimumOffset = -static_cast<int>(Data::encryptionHeaderSize);
constexpr auto minimumOffset = -static_cast<int>(Data::encryptionHeaderSize);
if (offset < minimumOffset)
throw Error("plaintext offset " + std::to_string(offset) + " is too small (minimum is " +
std::to_string(minimumOffset) + ")");
throw Error{"plaintext offset " + std::to_string(offset) + " is too small (minimum is " +
std::to_string(minimumOffset) + ")"};
}

if (decipheredFile && !cipherFile && !cipherIndex)
throw Error("-c or --cipher-index parameter is missing (required by -d)");
throw Error{"-c or --cipher-index parameter is missing (required by -d)"};
if (decipheredFile && !cipherArchive && decipheredFile == cipherFile)
throw Error("-c and -d parameters must point to different files");
throw Error{"-c and -d parameters must point to different files"};

if (changePassword && !cipherArchive)
throw Error("-C parameter is missing (required by -U)");
throw Error{"-C parameter is missing (required by -U)"};
if (changePassword && changePassword->unlockedArchive == cipherArchive)
throw Error("-C and -U parameters must point to different files");
throw Error{"-C and -U parameters must point to different files"};

if (changeKeys && !cipherArchive)
throw Error("-C parameter is missing (required by --change-keys)");
throw Error{"-C parameter is missing (required by --change-keys)"};
if (changeKeys && changeKeys->unlockedArchive == cipherArchive)
throw Error("-C and --change-keys parameters must point to different files");
throw Error{"-C and --change-keys parameters must point to different files"};

if (length && !bruteforce)
throw Error("--bruteforce parameter is missing (required by --length)");
throw Error{"--bruteforce parameter is missing (required by --length)"};
}

auto Arguments::loadData() const -> Data
{
// load known plaintext
std::vector<std::uint8_t> plaintext;
auto plaintext = std::vector<std::uint8_t>{};
if (plainArchive)
{
const auto archive = Zip{*plainArchive};
Expand All @@ -160,14 +160,14 @@ auto Arguments::loadData() const -> Data
plaintext = loadFile(*plainFile, plainFilePrefix);

// load ciphertext needed by the attack
std::size_t needed = Data::encryptionHeaderSize;
auto needed = Data::encryptionHeaderSize;
if (!plaintext.empty())
needed = std::max(needed, Data::encryptionHeaderSize + offset + plaintext.size());
if (!extraPlaintext.empty())
needed = std::max(needed, Data::encryptionHeaderSize + extraPlaintext.rbegin()->first + 1);

std::vector<std::uint8_t> ciphertext;
std::optional<std::map<int, std::uint8_t>> extraPlaintextWithCheckByte;
auto ciphertext = std::vector<std::uint8_t>{};
auto extraPlaintextWithCheckByte = std::optional<std::map<int, std::uint8_t>>{};
if (cipherArchive)
{
const auto archive = Zip{*cipherArchive};
Expand All @@ -184,8 +184,7 @@ auto Arguments::loadData() const -> Data
else
ciphertext = loadFile(*cipherFile, needed);

return Data(std::move(ciphertext), std::move(plaintext), offset,
extraPlaintextWithCheckByte.value_or(extraPlaintext));
return {std::move(ciphertext), std::move(plaintext), offset, extraPlaintextWithCheckByte.value_or(extraPlaintext)};
}

auto Arguments::LengthInterval::operator&(const Arguments::LengthInterval& other) const -> Arguments::LengthInterval
Expand Down Expand Up @@ -228,8 +227,8 @@ void Arguments::parseArgument()
break;
case Option::extraPlaintext:
{
int i = readInt("offset");
for (const std::uint8_t b : readHex("data"))
auto i = readInt("offset");
for (const auto b : readHex("data"))
extraPlaintext[i++] = b;
break;
}
Expand All @@ -255,7 +254,7 @@ void Arguments::parseArgument()
changePassword = {readString("unlockedzip"), readString("password")};
break;
case Option::changeKeys:
changeKeys = {readString("unlockedzip"), Keys{readKey("X"), readKey("Y"), readKey("Z")}};
changeKeys = {readString("unlockedzip"), {readKey("X"), readKey("Y"), readKey("Z")}};
break;
case Option::bruteforce:
bruteforce = readCharset();
Expand Down Expand Up @@ -312,7 +311,7 @@ void Arguments::parseArgument()
auto Arguments::readString(const std::string& description) -> std::string
{
if (finished())
throw Error("expected " + description + ", got nothing");
throw Error{"expected " + description + ", got nothing"};

return *m_current++;
}
Expand All @@ -323,7 +322,7 @@ auto Arguments::readOption(const std::string& description) -> Arguments::Option
#define PAIR(string, option) {#string, Option::option}
#define PAIRS(short, long, option) PAIR(short, option), PAIR(long, option)

static const std::map<std::string, Option> stringToOption = {
static const auto stringToOption = std::map<std::string, Option>{
PAIRS(-c, --cipher-file, cipherFile),
PAIR ( --cipher-index, cipherIndex),
PAIRS(-C, --cipher-zip, cipherArchive),
Expand Down Expand Up @@ -356,9 +355,9 @@ auto Arguments::readOption(const std::string& description) -> Arguments::Option
#undef PAIR
#undef PAIRS

const std::string str = readString(description);
const auto str = readString(description);
if (const auto it = stringToOption.find(str); it == stringToOption.end())
throw Error("unknown option " + str);
throw Error{"unknown option " + str};
else
return it->second;
}
Expand All @@ -375,46 +374,46 @@ auto Arguments::readSize(const std::string& description) -> std::size_t

auto Arguments::readHex(const std::string& description) -> std::vector<std::uint8_t>
{
std::string str = readString(description);
const auto str = readString(description);

if (str.size() % 2)
throw Error("expected an even-length string, got " + str);
if (!std::all_of(str.begin(), str.end(), [](unsigned char c) { return std::isxdigit(c); }))
throw Error("expected " + description + " in hexadecimal, got " + str);
throw Error{"expected an even-length string, got " + str};
if (!std::all_of(str.begin(), str.end(), [](char c) { return std::isxdigit(static_cast<unsigned char>(c)); }))
throw Error{"expected " + description + " in hexadecimal, got " + str};

std::vector<std::uint8_t> data;
for (std::size_t i = 0; i < str.length(); i += 2)
auto data = std::vector<std::uint8_t>{};
for (auto i = std::size_t{}; i < str.length(); i += 2)
data.push_back(static_cast<std::uint8_t>(std::stoul(str.substr(i, 2), nullptr, 16)));

return data;
}

auto Arguments::readKey(const std::string& description) -> std::uint32_t
{
std::string str = readString(description);
const auto str = readString(description);

if (str.size() > 8)
throw Error("expected a string of length 8 or less, got " + str);
if (!std::all_of(str.begin(), str.end(), [](unsigned char c) { return std::isxdigit(c); }))
throw Error("expected " + description + " in hexadecimal, got " + str);
throw Error{"expected a string of length 8 or less, got " + str};
if (!std::all_of(str.begin(), str.end(), [](char c) { return std::isxdigit(static_cast<unsigned char>(c)); }))
throw Error{"expected " + description + " in hexadecimal, got " + str};

return static_cast<std::uint32_t>(std::stoul(str, nullptr, 16));
}

auto Arguments::readCharset() -> std::vector<std::uint8_t>
{
const std::bitset<256> lowercase = charRange('a', 'z');
const std::bitset<256> uppercase = charRange('A', 'Z');
const std::bitset<256> digits = charRange('0', '9');
const std::bitset<256> alphanum = lowercase | uppercase | digits;
const std::bitset<256> printable = charRange(' ', '~');
const std::bitset<256> punctuation = printable & ~alphanum;

const std::string charsetArg = readString("charset");
const auto lowercase = charRange('a', 'z');
const auto uppercase = charRange('A', 'Z');
const auto digits = charRange('0', '9');
const auto alphanum = lowercase | uppercase | digits;
const auto printable = charRange(' ', '~');
const auto punctuation = printable & ~alphanum;

const auto charsetArg = readString("charset");
if (charsetArg.empty())
throw Error("the charset for password recovery is empty");
throw Error{"the charset for password recovery is empty"};

std::bitset<256> charset;
auto charset = std::bitset<256>{};

for (auto it = charsetArg.begin(); it != charsetArg.end(); ++it)
{
Expand All @@ -439,15 +438,15 @@ auto Arguments::readCharset() -> std::vector<std::uint8_t>
case '?': charset.set('?'); break;
// clang-format on
default:
throw Error(std::string("unknown charset ?") + *it);
throw Error{std::string{"unknown charset ?"} + *it};
}
}
else
charset.set(*it);
}

std::vector<std::uint8_t> result;
for (int c = 0; c < 256; c++)
auto result = std::vector<std::uint8_t>{};
for (auto c = 0; c < 256; c++)
if (charset[c])
result.push_back(c);

Expand Down
Loading

0 comments on commit aa9a625

Please sign in to comment.