From 9baf83181ef5b65f52bb4f782a101e203801829f Mon Sep 17 00:00:00 2001 From: falkTX Date: Thu, 14 Mar 2013 19:26:45 +0000 Subject: [PATCH] Fully correct base64 code --- source/tests/Base64.cpp | 133 +++++++++++++----------------- source/utils/CarlaBase64Utils.hpp | 4 +- source/utils/CarlaString.hpp | 27 +++--- 3 files changed, 77 insertions(+), 87 deletions(-) diff --git a/source/tests/Base64.cpp b/source/tests/Base64.cpp index 3283017d3..82cdb339b 100644 --- a/source/tests/Base64.cpp +++ b/source/tests/Base64.cpp @@ -16,7 +16,7 @@ */ #include "CarlaBase64Utils.hpp" -//#include "CarlaString.hpp" +#include "CarlaString.hpp" int main() { @@ -48,86 +48,71 @@ int main() char s[4]; int i; double d; - - char padding[100]; 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; -#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); - 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* blob3 = string.exportAsBase64Binary(); - - 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(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); + assert(std::strcmp(blobEnc, (const char*)string) == 0); + assert(std::strlen(blobEnc) == string.length()); + + Blob* blobNew = string.exportAsBase64Binary(); + + 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; } diff --git a/source/utils/CarlaBase64Utils.hpp b/source/utils/CarlaBase64Utils.hpp index fa44d4639..b9dfc73f3 100644 --- a/source/utils/CarlaBase64Utils.hpp +++ b/source/utils/CarlaBase64Utils.hpp @@ -89,7 +89,7 @@ void carla_base64_encode(const uint8_t* const raw, const size_t len, char* const *(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)); } @@ -173,7 +173,7 @@ unsigned int carla_base64_decode(const char* const encoded, uint8_t* const raw) 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)); /* Return length in bytes */ diff --git a/source/utils/CarlaString.hpp b/source/utils/CarlaString.hpp index 4dd39e3d5..16956aeef 100644 --- a/source/utils/CarlaString.hpp +++ b/source/utils/CarlaString.hpp @@ -282,28 +282,32 @@ public: 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) { - 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); - _dup(rawBuffer, rawBufferSize-1); + _dup(rawBuffer, rawBufferSize); } template 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) @@ -319,6 +323,7 @@ public: return len; } + *rawPtr = nullptr; return 0; } @@ -332,7 +337,7 @@ public: CARLA_ASSERT_INT2(len == sizeof(T), len, sizeof(T)); T* const t = new T(); - std::memcpy(t, binaryBuffer, len); + std::memcpy(t, binaryBuffer, sizeof(T)); return t; }