1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

refactor: use impl prefix for base63 functions

This commit is contained in:
Falk Werner 2020-06-17 16:54:09 +02:00
parent f9079b4c9b
commit 4abacef616
4 changed files with 44 additions and 44 deletions

View File

@ -1,6 +1,6 @@
#include "webfuse_provider/impl/base64.h" #include "webfuse_provider/impl/base64.h"
static const uint8_t wfp_base64_decode_table[256] = { static const uint8_t wfp_impl_base64_decode_table[256] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F // 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 1 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 1
@ -22,12 +22,12 @@ static const uint8_t wfp_base64_decode_table[256] = {
size_t wfp_base64_encoded_size(size_t length) size_t wfp_impl_base64_encoded_size(size_t length)
{ {
return ((length + 2) / 3) * 4; return ((length + 2) / 3) * 4;
} }
size_t wfp_base64_encode( size_t wfp_impl_base64_encode(
uint8_t const * data, uint8_t const * data,
size_t length, size_t length,
char * buffer, char * buffer,
@ -36,7 +36,7 @@ size_t wfp_base64_encode(
// 0 1 2 3 4 5 6 // 0 1 2 3 4 5 6
// 0123456789012345678901234567890123456789012345678901234567890123 // 0123456789012345678901234567890123456789012345678901234567890123
static char const table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static char const table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t const length_needed = wfp_base64_encoded_size(length); size_t const length_needed = wfp_impl_base64_encoded_size(length);
if (buffer_size < length_needed) if (buffer_size < length_needed)
{ {
return 0; return 0;
@ -78,7 +78,7 @@ size_t wfp_base64_encode(
return out_pos; return out_pos;
} }
size_t wfp_base64_decoded_size(char const * data, size_t length) size_t wfp_impl_base64_decoded_size(char const * data, size_t length)
{ {
size_t result = 0; size_t result = 0;
if ((length > 0) && ((length % 4) == 0)) if ((length > 0) && ((length % 4) == 0))
@ -98,14 +98,14 @@ size_t wfp_base64_decoded_size(char const * data, size_t length)
return result; return result;
} }
size_t wfp_base64_decode( size_t wfp_impl_base64_decode(
char const * data, char const * data,
size_t length, size_t length,
uint8_t * buffer, uint8_t * buffer,
size_t buffer_size) size_t buffer_size)
{ {
uint8_t const * table = wfp_base64_decode_table; uint8_t const * table = wfp_impl_base64_decode_table;
size_t needed_size = wfp_base64_decoded_size(data, length); size_t needed_size = wfp_impl_base64_decoded_size(data, length);
if ((0 == needed_size) || (buffer_size < needed_size)) if ((0 == needed_size) || (buffer_size < needed_size))
{ {
return 0; return 0;
@ -146,9 +146,9 @@ size_t wfp_base64_decode(
return out_pos; return out_pos;
} }
extern bool wfp_base64_isvalid(char const * data, size_t length) extern bool wfp_impl_base64_isvalid(char const * data, size_t length)
{ {
uint8_t const * table = wfp_base64_decode_table; uint8_t const * table = wfp_impl_base64_decode_table;
if ((length == 0) || ((length % 4) != 0)) if ((length == 0) || ((length % 4) != 0))
{ {

View File

@ -15,23 +15,23 @@ extern "C"
{ {
#endif #endif
extern size_t wfp_base64_encoded_size(size_t length); extern size_t wfp_impl_base64_encoded_size(size_t length);
extern size_t wfp_base64_encode( extern size_t wfp_impl_base64_encode(
uint8_t const * data, uint8_t const * data,
size_t length, size_t length,
char * buffer, char * buffer,
size_t buffer_size); size_t buffer_size);
extern size_t wfp_base64_decoded_size(char const * data, size_t length); extern size_t wfp_impl_base64_decoded_size(char const * data, size_t length);
extern size_t wfp_base64_decode( extern size_t wfp_impl_base64_decode(
char const * data, char const * data,
size_t length, size_t length,
uint8_t * buffer, uint8_t * buffer,
size_t buffer_size); size_t buffer_size);
extern bool wfp_base64_isvalid(char const * data, size_t length); extern bool wfp_impl_base64_isvalid(char const * data, size_t length);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -54,9 +54,9 @@ void wfp_impl_respond_read(
{ {
if (0 < length) if (0 < length)
{ {
size_t const size = wfp_base64_encoded_size(length) + 1; size_t const size = wfp_impl_base64_encoded_size(length) + 1;
char * buffer = malloc(size); char * buffer = malloc(size);
wfp_base64_encode((uint8_t const *) data, length, buffer, size); wfp_impl_base64_encode((uint8_t const *) data, length, buffer, size);
json_t * result = json_object(); json_t * result = json_object();
json_object_set_new(result, "data", json_string(buffer)); json_object_set_new(result, "data", json_string(buffer));

View File

@ -3,15 +3,15 @@
TEST(Base64, EncodedSize) TEST(Base64, EncodedSize)
{ {
ASSERT_EQ(4, wfp_base64_encoded_size(1)); ASSERT_EQ(4, wfp_impl_base64_encoded_size(1));
ASSERT_EQ(4, wfp_base64_encoded_size(2)); ASSERT_EQ(4, wfp_impl_base64_encoded_size(2));
ASSERT_EQ(4, wfp_base64_encoded_size(3)); ASSERT_EQ(4, wfp_impl_base64_encoded_size(3));
ASSERT_EQ(8, wfp_base64_encoded_size(4)); ASSERT_EQ(8, wfp_impl_base64_encoded_size(4));
ASSERT_EQ(8, wfp_base64_encoded_size(5)); ASSERT_EQ(8, wfp_impl_base64_encoded_size(5));
ASSERT_EQ(8, wfp_base64_encoded_size(6)); ASSERT_EQ(8, wfp_impl_base64_encoded_size(6));
ASSERT_EQ(120, wfp_base64_encoded_size(90)); ASSERT_EQ(120, wfp_impl_base64_encoded_size(90));
} }
TEST(Base64, Encode) TEST(Base64, Encode)
@ -19,17 +19,17 @@ TEST(Base64, Encode)
char buffer[42]; char buffer[42];
std::string in = "Hello"; std::string in = "Hello";
size_t length = wfp_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42); size_t length = wfp_impl_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
ASSERT_EQ(8, length); ASSERT_EQ(8, length);
ASSERT_STREQ("SGVsbG8=", buffer); ASSERT_STREQ("SGVsbG8=", buffer);
in = "Hello\n"; in = "Hello\n";
length = wfp_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42); length = wfp_impl_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
ASSERT_EQ(8, length); ASSERT_EQ(8, length);
ASSERT_STREQ("SGVsbG8K", buffer); ASSERT_STREQ("SGVsbG8K", buffer);
in = "Blue"; in = "Blue";
length = wfp_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42); length = wfp_impl_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
ASSERT_EQ(8, length); ASSERT_EQ(8, length);
ASSERT_STREQ("Qmx1ZQ==", buffer); ASSERT_STREQ("Qmx1ZQ==", buffer);
} }
@ -39,53 +39,53 @@ TEST(Base64, FailedToEncodeBufferTooSmall)
char buffer[1]; char buffer[1];
std::string in = "Hello"; std::string in = "Hello";
size_t length = wfp_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 1); size_t length = wfp_impl_base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 1);
ASSERT_EQ(0, length); ASSERT_EQ(0, length);
} }
TEST(Base64, DecodedSize) TEST(Base64, DecodedSize)
{ {
std::string in = "SGVsbG8="; // Hello std::string in = "SGVsbG8="; // Hello
size_t length = wfp_base64_decoded_size(in.c_str(), in.size()); size_t length = wfp_impl_base64_decoded_size(in.c_str(), in.size());
ASSERT_EQ(5, length); ASSERT_EQ(5, length);
in = "SGVsbG8K"; // Hello\n in = "SGVsbG8K"; // Hello\n
length = wfp_base64_decoded_size(in.c_str(), in.size()); length = wfp_impl_base64_decoded_size(in.c_str(), in.size());
ASSERT_EQ(6, length); ASSERT_EQ(6, length);
in = "Qmx1ZQ=="; // Blue in = "Qmx1ZQ=="; // Blue
length = wfp_base64_decoded_size(in.c_str(), in.size()); length = wfp_impl_base64_decoded_size(in.c_str(), in.size());
ASSERT_EQ(4, length); ASSERT_EQ(4, length);
} }
TEST(Base64, IsValid) TEST(Base64, IsValid)
{ {
std::string in = "SGVsbG8="; // Hello std::string in = "SGVsbG8="; // Hello
ASSERT_TRUE(wfp_base64_isvalid(in.c_str(), in.size())); ASSERT_TRUE(wfp_impl_base64_isvalid(in.c_str(), in.size()));
in = "SGVsbG8K"; // Hello\n in = "SGVsbG8K"; // Hello\n
ASSERT_TRUE(wfp_base64_isvalid(in.c_str(), in.size())); ASSERT_TRUE(wfp_impl_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ=="; // Blue in = "Qmx1ZQ=="; // Blue
ASSERT_TRUE(wfp_base64_isvalid(in.c_str(), in.size())); ASSERT_TRUE(wfp_impl_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ=a"; in = "Qmx1ZQ=a";
ASSERT_FALSE(wfp_base64_isvalid(in.c_str(), in.size())); ASSERT_FALSE(wfp_impl_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ"; in = "Qmx1ZQ";
ASSERT_FALSE(wfp_base64_isvalid(in.c_str(), in.size())); ASSERT_FALSE(wfp_impl_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ="; in = "Qmx1ZQ=";
ASSERT_FALSE(wfp_base64_isvalid(in.c_str(), in.size())); ASSERT_FALSE(wfp_impl_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1Z==="; in = "Qmx1Z===";
ASSERT_FALSE(wfp_base64_isvalid(in.c_str(), in.size())); ASSERT_FALSE(wfp_impl_base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ?="; in = "Qmx1ZQ?=";
ASSERT_FALSE(wfp_base64_isvalid(in.c_str(), in.size())); ASSERT_FALSE(wfp_impl_base64_isvalid(in.c_str(), in.size()));
in = "Qm?1ZQ=="; in = "Qm?1ZQ==";
ASSERT_FALSE(wfp_base64_isvalid(in.c_str(), in.size())); ASSERT_FALSE(wfp_impl_base64_isvalid(in.c_str(), in.size()));
} }
TEST(Base64, Decode) TEST(Base64, Decode)
@ -93,19 +93,19 @@ TEST(Base64, Decode)
char buffer[42]; char buffer[42];
std::string in = "SGVsbG8="; // Hello std::string in = "SGVsbG8="; // Hello
size_t length = wfp_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42); size_t length = wfp_impl_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
ASSERT_EQ(5, length); ASSERT_EQ(5, length);
buffer[length] = '\0'; buffer[length] = '\0';
ASSERT_STREQ("Hello", buffer); ASSERT_STREQ("Hello", buffer);
in = "SGVsbG8K"; // Hello\n in = "SGVsbG8K"; // Hello\n
length = wfp_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42); length = wfp_impl_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
ASSERT_EQ(6, length); ASSERT_EQ(6, length);
buffer[length] = '\0'; buffer[length] = '\0';
ASSERT_STREQ("Hello\n", buffer); ASSERT_STREQ("Hello\n", buffer);
in = "Qmx1ZQ=="; // Blue in = "Qmx1ZQ=="; // Blue
length = wfp_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42); length = wfp_impl_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
ASSERT_EQ(4, length); ASSERT_EQ(4, length);
buffer[length] = '\0'; buffer[length] = '\0';
ASSERT_STREQ("Blue", buffer); ASSERT_STREQ("Blue", buffer);
@ -116,7 +116,7 @@ TEST(Base64, FailToDecodeBufferTooSmall)
char buffer[1]; char buffer[1];
std::string in = "SGVsbG8="; // Hello std::string in = "SGVsbG8="; // Hello
size_t length = wfp_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 1); size_t length = wfp_impl_base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 1);
ASSERT_EQ(0, length); ASSERT_EQ(0, length);
} }