From c9895dd5c71261430e77b77ad9e86ba6255e35ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20E=2E=20Garc=C3=ADa?= Date: Tue, 7 Nov 2017 00:08:01 -0600 Subject: [PATCH 1/2] Improve Base32.cpp --- src/core/Base32.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/core/Base32.cpp b/src/core/Base32.cpp index 13228a37f..78448ffb5 100644 --- a/src/core/Base32.cpp +++ b/src/core/Base32.cpp @@ -52,8 +52,9 @@ QVariant Base32::decode(const QByteArray& encodedData) int nPads = 0; for (int i = -1; i > -7; --i) { - if ('=' == encodedData[encodedData.size() + i]) + if ('=' == encodedData[encodedData.size() + i]) { ++nPads; + } } int specialOffset; @@ -95,11 +96,12 @@ QVariant Base32::decode(const QByteArray& encodedData) int nQuantumBytes = 5; for (int n = 0; n < 8; ++n) { - quint8 ch = static_cast(encodedData[i++]); + auto ch = static_cast(encodedData[i++]); if ((ASCII_A <= ch && ch <= ASCII_Z) || (ASCII_a <= ch && ch <= ASCII_z)) { ch -= ASCII_A; - if (ch >= ALPH_POS_2) + if (ch >= ALPH_POS_2) { ch -= ASCII_a - ASCII_A; + } } else { if (ASCII_2 <= ch && ch <= ASCII_7) { ch -= ASCII_2; @@ -126,8 +128,8 @@ QVariant Base32::decode(const QByteArray& encodedData) const int offset = (nQuantumBytes - 1) * 8; quint64 mask = quint64(0xFF) << offset; for (int n = offset; n >= 0 && o < nBytes; n -= 8) { - data[o++] = static_cast((quantum & mask) >> n); - mask >>= 8; + data[o++] = static_cast((quantum & mask) >> n); + mask >>= 8; } } @@ -147,7 +149,7 @@ QByteArray Base32::encode(const QByteArray& data) const int rBits = nBits % 40; // in {0, 8, 16, 24, 32} const int nQuanta = nBits / 40 + (rBits > 0 ? 1 : 0); const int nBytes = nQuanta * 8; - QByteArray encodedData(nQuanta * 8, Qt::Uninitialized); + QByteArray encodedData(nBytes, Qt::Uninitialized); int i = 0; int o = 0; @@ -166,6 +168,7 @@ QByteArray Base32::encode(const QByteArray& data) int index; for (n = 35; n >= 0; n -= 5) { index = (quantum & mask) >> n; + Q_ASSERT(0 <= index && index <= 31); encodedData[o++] = alphabet[index]; mask >>= 5; } @@ -173,10 +176,11 @@ QByteArray Base32::encode(const QByteArray& data) // < 40-bits of input at final input group if (i < data.size()) { - Q_ASSERT(rBits > 0); + Q_ASSERT(8 <= rBits && rBits <= 32); quantum = 0; - for (n = rBits - 8; n >= 0; n -= 8) + for (n = rBits - 8; n >= 0; n -= 8) { quantum |= static_cast(data[i++]) << n; + } switch (rBits) { case 8: // expand to 10 bits @@ -195,7 +199,7 @@ QByteArray Base32::encode(const QByteArray& data) n = 20; break; default: // expand to 35 bits - Q_ASSERT(rBits == 32); + Q_ASSERT(32 == rBits); quantum <<= 3; mask = MASK_35BIT; n = 30; @@ -203,6 +207,7 @@ QByteArray Base32::encode(const QByteArray& data) while (n >= 0) { int index = (quantum & mask) >> n; + Q_ASSERT(0 <= index && index <= 31); encodedData[o++] = alphabet[index]; mask >>= 5; n -= 5; From cc6be754f724fe6efdc6099da785100c3c825cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20E=2E=20Garc=C3=ADa?= Date: Tue, 7 Nov 2017 00:10:38 -0600 Subject: [PATCH 2/2] Remove deprecated Optional.h --- src/core/Optional.h | 93 --------------------------------------------- 1 file changed, 93 deletions(-) delete mode 100644 src/core/Optional.h diff --git a/src/core/Optional.h b/src/core/Optional.h deleted file mode 100644 index fb800198a..000000000 --- a/src/core/Optional.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2017 KeePassXC Team - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 or (at your option) - * version 3 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef OPTIONAL_H -#define OPTIONAL_H - -/* - * This utility class is for providing basic support for an option type. - * It can be replaced by std::optional (C++17) or - * std::experimental::optional (C++11) when they become fully supported - * by all the compilers. - */ - -template -class Optional -{ -public: - - // None - Optional() : - m_hasValue(false), - m_value() - { }; - - // Some T - Optional(const T& value) : - m_hasValue(true), - m_value(value) - { }; - - // Copy - Optional(const Optional& other) : - m_hasValue(other.m_hasValue), - m_value(other.m_value) - { }; - - const Optional& operator=(const Optional& other) - { - m_hasValue = other.m_hasValue; - m_value = other.m_value; - return *this; - } - - bool operator==(const Optional& other) const - { - if(m_hasValue) - return other.m_hasValue && m_value == other.m_value; - else - return !other.m_hasValue; - } - - bool operator!=(const Optional& other) const - { - return !(*this == other); - } - - bool hasValue() const - { - return m_hasValue; - } - - T valueOr(const T& other) const - { - return m_hasValue ? m_value : other; - } - - Optional static makeOptional(const T& value) - { - return Optional(value); - } - - -private: - - bool m_hasValue; - T m_value; -}; - -#endif // OPTIONAL_H