From 960d08c643faa503f0cdc57e8754dcf740e58f0b Mon Sep 17 00:00:00 2001 From: onefang Date: Thu, 23 Apr 2020 01:01:04 +1000 Subject: Fix up Base64 encoding / decoding to actually work. --- src/sledjchisl/sledjchisl.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/sledjchisl/sledjchisl.c b/src/sledjchisl/sledjchisl.c index 87aac15..cc11565 100644 --- a/src/sledjchisl/sledjchisl.c +++ b/src/sledjchisl/sledjchisl.c @@ -83,7 +83,7 @@ GLOBALS( -// Duplicate some small amount of code from qLibc, coz / and = are not good choices, and the standard says we can pick those. +// Duplicate some small amount of code from qLibc, coz /, + and, = are not good choices, and the standard says we can pick those. /** * Encode data using BASE64 algorithm. * @@ -118,8 +118,7 @@ char *qB64_encode(const void *bin, size_t size) { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', // 00-0F 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', // 10-1F 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v', // 20-2F -// 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' // 30-3F - 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','_' // 30-3F + 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' // 30-3F }; if (size == 0) { @@ -151,15 +150,17 @@ char *qB64_encode(const void *bin, size_t size) { (nIdxOfThree >= 1) ? B64CHARTBL[(((szIn[1] & 0x0F) << 2) | ((szIn[2] & 0xC0) >> 6))] : -// '='; - '-'; -// *pszB64Pt++ = (nIdxOfThree >= 2) ? B64CHARTBL[(szIn[2] & 0x3F)] : '='; - *pszB64Pt++ = (nIdxOfThree >= 2) ? B64CHARTBL[(szIn[2] & 0x3F)] : '-'; + '='; + *pszB64Pt++ = (nIdxOfThree >= 2) ? B64CHARTBL[(szIn[2] & 0x3F)] : '='; memset((void *) szIn, 0, sizeof(szIn)); } *pszB64Pt = '\0'; + pszB64 = qstrreplace("tr", pszB64, "+", "~"); + pszB64 = qstrreplace("tr", pszB64, "/", "_"); + pszB64 = qstrreplace("tr", pszB64, "=", "^"); + return pszB64; } @@ -180,11 +181,11 @@ size_t qB64_decode(char *str) { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // 00-0F 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // 10-1F 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, // 20-2F - 52, 53, 54, 55, 56, 57, 58, 59, 60, 45, 64, 64, 64, 64, 64, 64, // 30-3F + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, // 30-3F 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 40-4F 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, // 50-5F 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 60-6F - 41, 42, 43, 44, 45, 46, 95, 48, 49, 50, 51, 64, 64, 64, 64, 64, // 70-7F + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64, // 70-7F 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // 80-8F 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // 90-9F 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, // A0-AF @@ -195,6 +196,10 @@ size_t qB64_decode(char *str) { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 // F0-FF }; + str = qstrreplace("tr", str, "~", "+"); + str = qstrreplace("tr", str, "_", "/"); + str = qstrreplace("tr", str, "^", "="); + char *pEncPt, *pBinPt = str; int nIdxOfFour = 0; char cLastByte = 0; -- cgit v1.1