Browse Source

Fully correct base64 code

tags/1.9.4
falkTX 11 years ago
parent
commit
9baf83181e
3 changed files with 77 additions and 87 deletions
  1. +59
    -74
      source/tests/Base64.cpp
  2. +2
    -2
      source/utils/CarlaBase64Utils.hpp
  3. +16
    -11
      source/utils/CarlaString.hpp

+ 59
- 74
source/tests/Base64.cpp View File

@@ -16,7 +16,7 @@
*/ */


#include "CarlaBase64Utils.hpp" #include "CarlaBase64Utils.hpp"
//#include "CarlaString.hpp"
#include "CarlaString.hpp"


int main() int main()
{ {
@@ -48,86 +48,71 @@ int main()
char s[4]; char s[4];
int i; int i;
double d; double d;

char padding[100];
void* ptr; void* ptr;

Blob()
: s{'1', 's', 't', 0},
i(228),
d(3.33333333333),
padding{0},
ptr((void*)0x500) {}
int32_t i32;
int64_t i64;
bool b;
} blob; } blob;


#if 0
// binary -> base64
void* const test0 = &blob;
size_t test0Len = sizeof(Blob);

char buf0[carla_base64_encoded_len(test0Len) + 1];
carla_base64_encode((const uint8_t*)test0, test0Len, buf0);

printf("BINARY '%s'\n", buf0);

char buf0dec[carla_base64_decoded_max_len(buf0)+1];
carla_zeroMem(buf0dec, sizeof(buf0dec));
carla_base64_decode(buf0, (uint8_t*)buf0dec);

Blob blobTester;
blobTester.s[0] = 0;
blobTester.i = 0;
blobTester.d = 9999.99999999999999;
std::memcpy(&blobTester, buf0dec, sizeof(Blob));

CARLA_ASSERT(std::strcmp(blobTester.s, "1st") == 0);
CARLA_ASSERT(blobTester.i == 228);
CARLA_ASSERT(blobTester.d == 3.33333333333);

// string -> base64
const char* const test1 = "Hello World!";
size_t test1Len = std::strlen(test1);

char buf1[carla_base64_encoded_len(test1Len) + 1];
carla_base64_encode((const uint8_t*)test1, test1Len, buf1);

printf("'%s' => '%s'\n", test1, buf1);

// base64 -> string
const char* const test2 = "SGVsbG8gV29ybGQh";
const size_t blobSize = sizeof(Blob);
carla_zeroStruct<Blob>(blob);


char buf2[carla_base64_decoded_max_len(test2)+1];
carla_zeroMem(buf2, sizeof(buf2));
carla_base64_decode(test2, (uint8_t*)buf2);
blob.s[0] = '1';
blob.s[1] = 's';
blob.s[2] = 't';
blob.i = 228;
blob.d = 3.33333333333;
blob.ptr = (void*)0x500;
blob.i32 = 32;
blob.i64 = 64;
blob.b = true;


printf("'%s' => '%s'\n", test2, buf2);

printf("'%s' == '%s'\n", buf1, test2);
printf("'%s' == '%s'\n", buf2, test1);

CARLA_ASSERT(std::strcmp(buf1, test2) == 0);
CARLA_ASSERT(std::strcmp(buf2, test1) == 0);

// -----------------------------------------------------------------

blob.s[0] = 'X';
blob.i = 92320;
blob.d = 9999887.99999999999999;

CarlaString string;
string.importBinaryAsBase64<Blob>(&blob);

Blob* blob3 = string.exportAsBase64Binary<Blob>();

CARLA_ASSERT(std::strcmp(blob3->s, blob.s) == 0);
CARLA_ASSERT(blob3->i == blob.i);
CARLA_ASSERT(blob3->d == blob.d);
CARLA_ASSERT(blob3->ptr == blob.ptr);
// binary -> base64
char blobEnc[carla_base64_encoded_len(blobSize) + 1];
carla_base64_encode((const uint8_t*)&blob, blobSize, blobEnc);
std::printf("BINARY '%s', size:" P_SIZE "\n", blobEnc, blobSize);


delete blob3;
#endif
{
// base64 -> binary
uint8_t blobDec[carla_base64_decoded_max_len(blobEnc)];
carla_base64_decode(blobEnc, blobDec);

Blob blobTest;
carla_zeroStruct<Blob>(blobTest);

std::memcpy(&blobTest, blobDec, sizeof(Blob));
assert(blobTest.s[0] == '1');
assert(blobTest.s[1] == 's');
assert(blobTest.s[2] == 't');
assert(blobTest.i == 228);
assert(blobTest.d == 3.33333333333);
assert(blobTest.ptr == (void*)0x500);
assert(blobTest.i32 == 32);
assert(blobTest.i64 == 64);
assert(blobTest.b == true);
}


// -----------------------------------------------------------------
{
// CarlaString usage
CarlaString string;
string.importBinaryAsBase64<Blob>(&blob);
assert(std::strcmp(blobEnc, (const char*)string) == 0);
assert(std::strlen(blobEnc) == string.length());

Blob* blobNew = string.exportAsBase64Binary<Blob>();

assert(blobNew->s[0] == '1');
assert(blobNew->s[1] == 's');
assert(blobNew->s[2] == 't');
assert(blobNew->i == 228);
assert(blobNew->d == 3.33333333333);
assert(blobNew->ptr == (void*)0x500);
assert(blobNew->i32 == 32);
assert(blobNew->i64 == 64);
assert(blobNew->b == true);

delete blobNew;
}


return 0; return 0;
} }

+ 2
- 2
source/utils/CarlaBase64Utils.hpp View File

@@ -89,7 +89,7 @@ void carla_base64_encode(const uint8_t* const raw, const size_t len, char* const


*(encodedBytes++) = '\0'; *(encodedBytes++) = '\0';


carla_debug("Base64-encoded to \"%s\":\n", encoded);
carla_debug("Base64-encoded to \"%s\"\n", encoded);
CARLA_ASSERT(std::strlen(encoded) == carla_base64_encoded_len(len)); CARLA_ASSERT(std::strlen(encoded) == carla_base64_encoded_len(len));
} }


@@ -173,7 +173,7 @@ unsigned int carla_base64_decode(const char* const encoded, uint8_t* const raw)


unsigned int len = bit/8; unsigned int len = bit/8;


carla_debug("Base64-decoded \"%s\"", encoded);
carla_debug("Base64-decoded from \"%s\"", encoded);
CARLA_ASSERT(len <= carla_base64_decoded_max_len(encoded)); CARLA_ASSERT(len <= carla_base64_decoded_max_len(encoded));


/* Return length in bytes */ /* Return length in bytes */


+ 16
- 11
source/utils/CarlaString.hpp View File

@@ -282,28 +282,32 @@ public:


void fromBase64() void fromBase64()
{ {
uint8_t buffer2[carla_base64_decoded_max_len(buffer)];
uint8_t buffer2[carla_base64_decoded_max_len(buffer)];


if (unsigned int len = carla_base64_decode(buffer, buffer2))
_dup((char*)buffer2, len);
else
clear();
if (unsigned int len = carla_base64_decode(buffer, buffer2))
{
char bufDecoded[len+1];
std::strncpy(bufDecoded, (char*)buffer2, len);
bufDecoded[len] = '\0';
_dup(bufDecoded, len);
}
else
clear();
} }


void importBinaryAsBase64(const uint8_t* const raw, const size_t rawLen) void importBinaryAsBase64(const uint8_t* const raw, const size_t rawLen)
{ {
const size_t rawBufferSize = carla_base64_encoded_len(rawLen) + 1;
char rawBuffer[rawBufferSize];
const size_t rawBufferSize = carla_base64_encoded_len(rawLen);
char rawBuffer[rawBufferSize+1];
carla_base64_encode(raw, rawLen, rawBuffer); carla_base64_encode(raw, rawLen, rawBuffer);


_dup(rawBuffer, rawBufferSize-1);
_dup(rawBuffer, rawBufferSize);
} }


template<typename T> template<typename T>
void importBinaryAsBase64(const T* const t) void importBinaryAsBase64(const T* const t)
{ {
const size_t tSize = sizeof(T);
importBinaryAsBase64((const uint8_t*)t, tSize);
importBinaryAsBase64((const uint8_t*)t, sizeof(T));
} }


size_t exportAsBase64Binary(uint8_t** const rawPtr) size_t exportAsBase64Binary(uint8_t** const rawPtr)
@@ -319,6 +323,7 @@ public:
return len; return len;
} }


*rawPtr = nullptr;
return 0; return 0;
} }


@@ -332,7 +337,7 @@ public:
CARLA_ASSERT_INT2(len == sizeof(T), len, sizeof(T)); CARLA_ASSERT_INT2(len == sizeof(T), len, sizeof(T));


T* const t = new T(); T* const t = new T();
std::memcpy(t, binaryBuffer, len);
std::memcpy(t, binaryBuffer, sizeof(T));


return t; return t;
} }


Loading…
Cancel
Save