Files
hpcore/src/util.cpp
2019-10-09 22:26:08 +05:30

84 lines
1.9 KiB
C++

#include <string>
#include <sodium.h>
using namespace std;
namespace util
{
void replace_string_contents(string &str, const char *bytes, size_t bytes_len)
{
if (str.length() > 0)
str.clear();
str.append(bytes, bytes_len);
}
int base64_encode(const unsigned char *bin, size_t bin_len, string &encoded_string)
{
// Get length of encoded result from sodium.
const size_t base64_len = sodium_base64_encoded_len(bin_len, sodium_base64_VARIANT_ORIGINAL);
char base64chars[base64_len];
// Get encoded string.
const char *encoded_str_char = sodium_bin2base64(
base64chars, base64_len,
bin, bin_len,
sodium_base64_VARIANT_ORIGINAL);
if (encoded_str_char == NULL)
return -1;
// Assign the encoded char* onto the provided string reference.
replace_string_contents(encoded_string, base64chars, base64_len);
return 0;
}
int base64_decode(const string &base64_str, unsigned char *decoded, size_t decoded_len)
{
const char *b64_end;
size_t bin_len;
if (sodium_base642bin(
decoded, decoded_len,
base64_str.data(), base64_str.size() + 1,
"", &bin_len, &b64_end,
sodium_base64_VARIANT_ORIGINAL))
{
return -1;
}
return 0;
}
// v1 < v2 -> -1
// v1 == v2 -> 0
// v1 > v2 -> +1
int version_compare(const string &v1, const string &v2)
{
size_t i = 0, j = 0;
while (i < v1.length() || j < v2.length())
{
int acc1 = 0, acc2 = 0;
while (i < v1.length() && v1[i] != '.')
{
acc1 = acc1 * 10 + (v1[i] - '0');
i++;
}
while (j < v2.length() && v2[j] != '.')
{
acc2 = acc2 * 10 + (v2[j] - '0');
j++;
}
if (acc1 < acc2)
return -1;
if (acc1 > acc2)
return +1;
++i;
++j;
}
return 0;
}
} // namespace util