1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-11-08 03:22:25 +03:00

Improve base64 implementation.

Different encoded strings could be generated based on compiler optimizations. Even though decoding was still successful the encoded strings did not match the standard.
This commit is contained in:
David Steele
2017-11-18 22:49:43 -05:00
parent 500d6b4b66
commit 9395ad7043
3 changed files with 37 additions and 18 deletions

View File

@@ -14,11 +14,11 @@ void testRun()
char destinationEncode[256];
encodeToStr(encodeBase64, encode, 1, destinationEncode);
TEST_RESULT_STR(destinationEncode, "c3==", "1 character encode");
TEST_RESULT_STR(destinationEncode, "cw==", "1 character encode");
TEST_RESULT_INT(encodeToStrSize(encodeBase64, 1), strlen(destinationEncode), "check size");
encodeToStr(encodeBase64, encode, 2, destinationEncode);
TEST_RESULT_STR(destinationEncode, "c3R=", "2 character encode");
TEST_RESULT_STR(destinationEncode, "c3Q=", "2 character encode");
TEST_RESULT_INT(encodeToStrSize(encodeBase64, 2), strlen(destinationEncode), "check size");
encodeToStr(encodeBase64, encode, 3, destinationEncode);
@@ -34,7 +34,7 @@ void testRun()
TEST_RESULT_INT(encodeToStrSize(encodeBase64, strlen((char *)encode)), strlen(destinationEncode), "check size");
encodeToStr(encodeBase64, encode, strlen((char *)encode) + 1, destinationEncode);
TEST_RESULT_STR(destinationEncode, "c3RyaW5nX3RvX2VuY29kZQ0KAG==", "encode full string with \\r\\n and null");
TEST_RESULT_STR(destinationEncode, "c3RyaW5nX3RvX2VuY29kZQ0KAA==", "encode full string with \\r\\n and null");
TEST_RESULT_INT(encodeToStrSize(encodeBase64, strlen((char *)encode) + 1), strlen(destinationEncode), "check size");
TEST_ERROR(encodeToStr(999, encode, strlen((char *)encode), destinationEncode), AssertError, "invalid encode type 999");
@@ -44,7 +44,7 @@ void testRun()
unsigned char destinationDecode[256];
memset(destinationDecode, 0xFF, sizeof(destinationDecode));
const char *decode = "c3RyaW5nX3RvX2VuY29kZQ0KAG==";
const char *decode = "c3RyaW5nX3RvX2VuY29kZQ0KAA==";
decodeToBin(encodeBase64, decode, destinationDecode);
TEST_RESULT_STR(destinationDecode, encode, "full string with \\r\\n and null decode");
TEST_RESULT_INT(destinationDecode[strlen((char *)encode) + 1], 0xFF, "check for overrun");
@@ -72,14 +72,14 @@ void testRun()
TEST_RESULT_INT(decodeToBinSize(encodeBase64, decode), 3, "check size");
memset(destinationDecode, 0xFF, sizeof(destinationDecode));
decode = "c3R=";
decode = "c3Q=";
decodeToBin(encodeBase64, decode, destinationDecode);
TEST_RESULT_INT(memcmp(destinationDecode, encode, 2), 0, "2 character decode");
TEST_RESULT_INT(destinationDecode[2], 0xFF, "check for overrun");
TEST_RESULT_INT(decodeToBinSize(encodeBase64, decode), 2, "check size");
memset(destinationDecode, 0xFF, sizeof(destinationDecode));
decode = "c3==";
decode = "cw==";
decodeToBin(encodeBase64, decode, destinationDecode);
TEST_RESULT_INT(memcmp(destinationDecode, encode, 1), 0, "1 character decode");
TEST_RESULT_INT(destinationDecode[1], 0xFF, "check for overrun");