Merge pull request #1170 from adolfogc/refactor/base32-implementation

Refactor base32 implementation
This commit is contained in:
TheZ3ro 2017-11-17 19:34:32 +01:00 committed by GitHub
commit 8625e2c051
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 102 deletions

View File

@ -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<quint8>(encodedData[i++]);
auto ch = static_cast<quint8>(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<char>((quantum & mask) >> n);
mask >>= 8;
data[o++] = static_cast<char>((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<quint64>(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;

View File

@ -1,93 +0,0 @@
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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 <typename T>
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