#5657 Fix SerialKey whitespace
This commit is contained in:
parent
b5a6ae0a94
commit
c7dc198d82
|
@ -29,27 +29,27 @@
|
|||
using namespace std;
|
||||
|
||||
SerialKey::SerialKey(Edition edition):
|
||||
m_userLimit(1),
|
||||
m_warnTime(ULLONG_MAX),
|
||||
m_expireTime(ULLONG_MAX),
|
||||
m_edition(edition),
|
||||
m_trial(false),
|
||||
m_valid(true)
|
||||
m_userLimit(1),
|
||||
m_warnTime(ULLONG_MAX),
|
||||
m_expireTime(ULLONG_MAX),
|
||||
m_edition(edition),
|
||||
m_trial(false),
|
||||
m_valid(true)
|
||||
{
|
||||
}
|
||||
|
||||
SerialKey::SerialKey(std::string serial) :
|
||||
m_userLimit(1),
|
||||
m_warnTime(0),
|
||||
m_expireTime(0),
|
||||
m_edition(kBasic),
|
||||
m_trial(true),
|
||||
m_valid(false)
|
||||
m_userLimit(1),
|
||||
m_warnTime(0),
|
||||
m_expireTime(0),
|
||||
m_edition(kBasic),
|
||||
m_trial(true),
|
||||
m_valid(false)
|
||||
{
|
||||
string plainText = decode(serial);
|
||||
if (!plainText.empty()) {
|
||||
parse(plainText);
|
||||
}
|
||||
string plainText = decode(serial);
|
||||
if (!plainText.empty()) {
|
||||
parse(plainText);
|
||||
}
|
||||
if (!m_valid) {
|
||||
throw std::runtime_error ("Invalid serial key");
|
||||
}
|
||||
|
@ -58,54 +58,54 @@ SerialKey::SerialKey(std::string serial) :
|
|||
bool
|
||||
SerialKey::isValid(time_t currentTime) const
|
||||
{
|
||||
bool result = false;
|
||||
bool result = false;
|
||||
|
||||
if (m_valid) {
|
||||
if (m_trial) {
|
||||
if (currentTime < m_expireTime) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
if (m_valid) {
|
||||
if (m_trial) {
|
||||
if (currentTime < m_expireTime) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool
|
||||
SerialKey::isExpiring(time_t currentTime) const
|
||||
{
|
||||
bool result = false;
|
||||
bool result = false;
|
||||
|
||||
if (m_valid) {
|
||||
if (m_warnTime <= currentTime && currentTime < m_expireTime) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
if (m_valid) {
|
||||
if (m_warnTime <= currentTime && currentTime < m_expireTime) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool
|
||||
SerialKey::isExpired(time_t currentTime) const
|
||||
{
|
||||
bool result = false;
|
||||
bool result = false;
|
||||
|
||||
if (m_valid) {
|
||||
if (m_expireTime <= currentTime) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
if (m_valid) {
|
||||
if (m_expireTime <= currentTime) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
bool
|
||||
SerialKey::isTrial() const
|
||||
{
|
||||
return m_trial;
|
||||
return m_trial;
|
||||
}
|
||||
|
||||
Edition
|
||||
|
@ -161,15 +161,15 @@ SerialKey::toString() const
|
|||
time_t
|
||||
SerialKey::daysLeft(time_t currentTime) const
|
||||
{
|
||||
unsigned long long timeLeft = 0;
|
||||
unsigned long long const day = 60 * 60 * 24;
|
||||
unsigned long long timeLeft = 0;
|
||||
unsigned long long const day = 60 * 60 * 24;
|
||||
|
||||
if (currentTime < m_expireTime) {
|
||||
timeLeft = m_expireTime - currentTime;
|
||||
}
|
||||
if (currentTime < m_expireTime) {
|
||||
timeLeft = m_expireTime - currentTime;
|
||||
}
|
||||
|
||||
unsigned long long daysLeft = 0;
|
||||
daysLeft = timeLeft % day != 0 ? 1 : 0;
|
||||
unsigned long long daysLeft = 0;
|
||||
daysLeft = timeLeft % day != 0 ? 1 : 0;
|
||||
|
||||
return timeLeft / day + daysLeft;
|
||||
}
|
||||
|
@ -183,95 +183,95 @@ SerialKey::email() const
|
|||
std::string
|
||||
SerialKey::decode(const std::string& serial)
|
||||
{
|
||||
static const char* const lut = "0123456789ABCDEF";
|
||||
string output;
|
||||
size_t len = serial.length();
|
||||
if (len & 1) {
|
||||
return output;
|
||||
}
|
||||
static const char* const lut = "0123456789ABCDEF";
|
||||
string output;
|
||||
size_t len = serial.length();
|
||||
if (len & 1) {
|
||||
return output;
|
||||
}
|
||||
|
||||
output.reserve(len / 2);
|
||||
for (size_t i = 0; i < len; i += 2) {
|
||||
output.reserve(len / 2);
|
||||
for (size_t i = 0; i < len; i += 2) {
|
||||
|
||||
char a = serial[i];
|
||||
char b = serial[i + 1];
|
||||
char a = serial[i];
|
||||
char b = serial[i + 1];
|
||||
|
||||
const char* p = std::lower_bound(lut, lut + 16, a);
|
||||
const char* q = std::lower_bound(lut, lut + 16, b);
|
||||
const char* p = std::lower_bound(lut, lut + 16, a);
|
||||
const char* q = std::lower_bound(lut, lut + 16, b);
|
||||
|
||||
if (*q != b || *p != a) {
|
||||
return output;
|
||||
}
|
||||
if (*q != b || *p != a) {
|
||||
return output;
|
||||
}
|
||||
|
||||
output.push_back(static_cast<char>(((p - lut) << 4) | (q - lut)));
|
||||
}
|
||||
output.push_back(static_cast<char>(((p - lut) << 4) | (q - lut)));
|
||||
}
|
||||
|
||||
return output;
|
||||
return output;
|
||||
}
|
||||
|
||||
void
|
||||
SerialKey::parse(std::string plainSerial)
|
||||
{
|
||||
string parityStart = plainSerial.substr(0, 1);
|
||||
string parityEnd = plainSerial.substr(plainSerial.length() - 1, 1);
|
||||
string parityStart = plainSerial.substr(0, 1);
|
||||
string parityEnd = plainSerial.substr(plainSerial.length() - 1, 1);
|
||||
|
||||
m_valid = false;
|
||||
m_valid = false;
|
||||
|
||||
// check for parity chars { and }, record parity result, then remove them.
|
||||
if (parityStart == "{" && parityEnd == "}") {
|
||||
plainSerial = plainSerial.substr(1, plainSerial.length() - 2);
|
||||
// check for parity chars { and }, record parity result, then remove them.
|
||||
if (parityStart == "{" && parityEnd == "}") {
|
||||
plainSerial = plainSerial.substr(1, plainSerial.length() - 2);
|
||||
|
||||
// tokenize serialised subscription.
|
||||
vector<string> parts;
|
||||
std::string::size_type pos = 0;
|
||||
bool look = true;
|
||||
while (look) {
|
||||
std::string::size_type start = pos;
|
||||
pos = plainSerial.find(";", pos);
|
||||
if (pos == string::npos) {
|
||||
pos = plainSerial.length();
|
||||
look = false;
|
||||
}
|
||||
parts.push_back(plainSerial.substr(start, pos - start));
|
||||
pos += 1;
|
||||
}
|
||||
// tokenize serialised subscription.
|
||||
vector<string> parts;
|
||||
std::string::size_type pos = 0;
|
||||
bool look = true;
|
||||
while (look) {
|
||||
std::string::size_type start = pos;
|
||||
pos = plainSerial.find(";", pos);
|
||||
if (pos == string::npos) {
|
||||
pos = plainSerial.length();
|
||||
look = false;
|
||||
}
|
||||
parts.push_back(plainSerial.substr(start, pos - start));
|
||||
pos += 1;
|
||||
}
|
||||
|
||||
if ((parts.size() == 8)
|
||||
&& (parts.at(0).find("v1") != string::npos)) {
|
||||
// e.g.: {v1;basic;Bob;1;email;company name;1398297600;1398384000}
|
||||
m_edition = parseEdition(parts.at(1));
|
||||
m_name = parts.at(2);
|
||||
m_trial = false;
|
||||
sscanf(parts.at(3).c_str(), "%d", &m_userLimit);
|
||||
m_email = parts.at(4);
|
||||
m_company = parts.at(5);
|
||||
sscanf(parts.at(6).c_str(), "%lld", &m_warnTime);
|
||||
sscanf(parts.at(7).c_str(), "%lld", &m_expireTime);
|
||||
m_valid = true;
|
||||
}
|
||||
else if ((parts.size() == 9)
|
||||
&& (parts.at(0).find("v2") != string::npos)) {
|
||||
// e.g.: {v2;trial;basic;Bob;1;email;company name;1398297600;1398384000}
|
||||
m_trial = parts.at(1) == "trial" ? true : false;
|
||||
m_edition = parseEdition(parts.at(2));
|
||||
m_name = parts.at(3);
|
||||
sscanf(parts.at(4).c_str(), "%d", &m_userLimit);
|
||||
m_email = parts.at(5);
|
||||
m_company = parts.at(6);
|
||||
sscanf(parts.at(7).c_str(), "%lld", &m_warnTime);
|
||||
sscanf(parts.at(8).c_str(), "%lld", &m_expireTime);
|
||||
m_valid = true;
|
||||
}
|
||||
}
|
||||
if ((parts.size() == 8)
|
||||
&& (parts.at(0).find("v1") != string::npos)) {
|
||||
// e.g.: {v1;basic;Bob;1;email;company name;1398297600;1398384000}
|
||||
m_edition = parseEdition(parts.at(1));
|
||||
m_name = parts.at(2);
|
||||
m_trial = false;
|
||||
sscanf(parts.at(3).c_str(), "%d", &m_userLimit);
|
||||
m_email = parts.at(4);
|
||||
m_company = parts.at(5);
|
||||
sscanf(parts.at(6).c_str(), "%lld", &m_warnTime);
|
||||
sscanf(parts.at(7).c_str(), "%lld", &m_expireTime);
|
||||
m_valid = true;
|
||||
}
|
||||
else if ((parts.size() == 9)
|
||||
&& (parts.at(0).find("v2") != string::npos)) {
|
||||
// e.g.: {v2;trial;basic;Bob;1;email;company name;1398297600;1398384000}
|
||||
m_trial = parts.at(1) == "trial" ? true : false;
|
||||
m_edition = parseEdition(parts.at(2));
|
||||
m_name = parts.at(3);
|
||||
sscanf(parts.at(4).c_str(), "%d", &m_userLimit);
|
||||
m_email = parts.at(5);
|
||||
m_company = parts.at(6);
|
||||
sscanf(parts.at(7).c_str(), "%lld", &m_warnTime);
|
||||
sscanf(parts.at(8).c_str(), "%lld", &m_expireTime);
|
||||
m_valid = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Edition
|
||||
SerialKey::parseEdition(std::string const& editionStr)
|
||||
{
|
||||
Edition e = kBasic;
|
||||
if (editionStr == "pro") {
|
||||
e = kPro;
|
||||
}
|
||||
Edition e = kBasic;
|
||||
if (editionStr == "pro") {
|
||||
e = kPro;
|
||||
}
|
||||
|
||||
return e;
|
||||
return e;
|
||||
}
|
||||
|
|
|
@ -26,65 +26,65 @@
|
|||
#endif
|
||||
|
||||
class SerialKey {
|
||||
friend bool operator== (SerialKey const&, SerialKey const&);
|
||||
friend bool operator== (SerialKey const&, SerialKey const&);
|
||||
public:
|
||||
explicit SerialKey(Edition edition = kUnregistered);
|
||||
explicit SerialKey(std::string serial);
|
||||
explicit SerialKey(Edition edition = kUnregistered);
|
||||
explicit SerialKey(std::string serial);
|
||||
|
||||
bool isValid(time_t currentTime) const;
|
||||
bool isExpiring(time_t currentTime) const;
|
||||
bool isExpired(time_t currentTime) const;
|
||||
bool isTrial() const;
|
||||
time_t daysLeft(time_t currentTime) const;
|
||||
bool isValid(time_t currentTime) const;
|
||||
bool isExpiring(time_t currentTime) const;
|
||||
bool isExpired(time_t currentTime) const;
|
||||
bool isTrial() const;
|
||||
time_t daysLeft(time_t currentTime) const;
|
||||
std::string email() const;
|
||||
Edition edition() const;
|
||||
Edition edition() const;
|
||||
std::string toString() const;
|
||||
|
||||
static std::string decode(const std::string& serial);
|
||||
static Edition parseEdition(const std::string& editionStr);
|
||||
static std::string decode(const std::string& serial);
|
||||
static Edition parseEdition(const std::string& editionStr);
|
||||
|
||||
private:
|
||||
void parse(std::string plainSerial);
|
||||
void parse(std::string plainSerial);
|
||||
std::string editionString() const;
|
||||
|
||||
#ifdef TEST_ENV
|
||||
private:
|
||||
FRIEND_TEST(SerialKeyTests, decode_empty_returnEmptyString);
|
||||
FRIEND_TEST(SerialKeyTests, decode_invalidDigit_returnEmptyString);
|
||||
FRIEND_TEST(SerialKeyTests, decode_validSerial_returnPlainText);
|
||||
FRIEND_TEST(SerialKeyTests, parse_noParty_invalid);
|
||||
FRIEND_TEST(SerialKeyTests, parse_invalidPartsLenghth_invalid);
|
||||
FRIEND_TEST(SerialKeyTests, parse_validV1Serial_valid);
|
||||
FRIEND_TEST(SerialKeyTests, parse_validV2Serial_valid);
|
||||
FRIEND_TEST(SerialKeyTests, decode_empty_returnEmptyString);
|
||||
FRIEND_TEST(SerialKeyTests, decode_invalidDigit_returnEmptyString);
|
||||
FRIEND_TEST(SerialKeyTests, decode_validSerial_returnPlainText);
|
||||
FRIEND_TEST(SerialKeyTests, parse_noParty_invalid);
|
||||
FRIEND_TEST(SerialKeyTests, parse_invalidPartsLenghth_invalid);
|
||||
FRIEND_TEST(SerialKeyTests, parse_validV1Serial_valid);
|
||||
FRIEND_TEST(SerialKeyTests, parse_validV2Serial_valid);
|
||||
#endif
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
std::string m_email;
|
||||
std::string m_company;
|
||||
unsigned m_userLimit;
|
||||
unsigned long long m_warnTime;
|
||||
unsigned long long m_expireTime;
|
||||
Edition m_edition;
|
||||
bool m_trial;
|
||||
bool m_valid;
|
||||
std::string m_name;
|
||||
std::string m_email;
|
||||
std::string m_company;
|
||||
unsigned m_userLimit;
|
||||
unsigned long long m_warnTime;
|
||||
unsigned long long m_expireTime;
|
||||
Edition m_edition;
|
||||
bool m_trial;
|
||||
bool m_valid;
|
||||
};
|
||||
|
||||
|
||||
inline bool
|
||||
operator== (SerialKey const& lhs, SerialKey const& rhs) {
|
||||
return (lhs.m_name == rhs.m_name) &&
|
||||
(lhs.m_email == rhs.m_email) &&
|
||||
(lhs.m_company == rhs.m_company) &&
|
||||
(lhs.m_userLimit == rhs.m_userLimit) &&
|
||||
(lhs.m_warnTime == rhs.m_warnTime) &&
|
||||
(lhs.m_expireTime == rhs.m_expireTime) &&
|
||||
(lhs.m_edition == rhs.m_edition) &&
|
||||
(lhs.m_trial == rhs.m_trial) &&
|
||||
(lhs.m_valid == rhs.m_valid);
|
||||
return (lhs.m_name == rhs.m_name) &&
|
||||
(lhs.m_email == rhs.m_email) &&
|
||||
(lhs.m_company == rhs.m_company) &&
|
||||
(lhs.m_userLimit == rhs.m_userLimit) &&
|
||||
(lhs.m_warnTime == rhs.m_warnTime) &&
|
||||
(lhs.m_expireTime == rhs.m_expireTime) &&
|
||||
(lhs.m_edition == rhs.m_edition) &&
|
||||
(lhs.m_trial == rhs.m_trial) &&
|
||||
(lhs.m_valid == rhs.m_valid);
|
||||
}
|
||||
|
||||
inline bool
|
||||
operator!= (SerialKey const& lhs, SerialKey const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue