Code consistency improvements (#21)

Fully-qualified namespace accessors
Updated function out param order
reinterpret_cast
This commit is contained in:
Ravin Perera
2019-10-11 20:08:53 +05:30
committed by GitHub
parent f64cdc6ad0
commit ebf13209e1
14 changed files with 235 additions and 241 deletions

View File

@@ -2,19 +2,17 @@
#include <sodium.h>
#include <sstream>
using namespace std;
namespace util
{
/**
* Encodes provided bytes to base64 string.
*
* @param encoded_string String reference to assign the base64 encoded output.
* @param bin Bytes to encode.
* @param bin_len Bytes length.
* @param encoded_string String reference to assign the base64 encoded output.
*/
int base64_encode(const unsigned char *bin, size_t bin_len, string &encoded_string)
int base64_encode(std::string &encoded_string, const unsigned char *bin, size_t bin_len)
{
// Get length of encoded result from sodium.
const size_t base64_len = sodium_base64_encoded_len(bin_len, sodium_base64_VARIANT_ORIGINAL);
@@ -32,23 +30,23 @@ int base64_encode(const unsigned char *bin, size_t bin_len, string &encoded_stri
// Assign the encoded char* onto the provided string reference.
// "base64_len - 1" because sodium include '\0' in the calculated base64 length.
// Therefore we need to omit it when initializing the std::string.
encoded_string = string(base64chars, base64_len - 1);
encoded_string = std::string(base64chars, base64_len - 1);
return 0;
}
/**
* Decodes provided base64 string into bytes.
*
* @param decodedbuf Buffer to assign decoded bytes.
* @param decodedbuf_len Decoded buffer size.
* @param base64_str Base64 string to decode.
* @param decoded Decoded bytes.
* @param decoded_len Decoded bytes length.
*/
int base64_decode(const string &base64_str, unsigned char *decoded, size_t decoded_len)
int base64_decode(unsigned char *decodedbuf, size_t decodedbuf_len, const std::string &base64_str)
{
const char *b64_end;
size_t bin_len;
if (sodium_base642bin(
decoded, decoded_len,
decodedbuf, decodedbuf_len,
base64_str.data(), base64_str.size() + 1,
"", &bin_len, &b64_end,
sodium_base64_VARIANT_ORIGINAL))
@@ -66,9 +64,9 @@ int base64_decode(const string &base64_str, unsigned char *decoded, size_t decod
* v1 > v2 -> returns +1
* Error -> returns -2
*/
int version_compare(const string &x, const string &y)
int version_compare(const std::string &x, const std::string &y)
{
istringstream ix(x), iy(y);
std::istringstream ix(x), iy(y);
while (ix.good() || iy.good())
{
int cx = 0, cy = 0;