From dfebad4a54b30e6180b2804f3aa622d66e9e98ff Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Tue, 12 Apr 2011 22:58:55 -0700 Subject: [PATCH] Fix Base64 size calculation behavior to always return the actual conversion size --- .../pfConsole/pfConsoleCommandsNet.cpp | 16 ++++++++-------- .../NucleusLib/pnUtils/Private/pnUtBase64.cpp | 2 +- .../NucleusLib/pnUtils/Private/pnUtBase64.h | 9 ++++++--- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Sources/Plasma/FeatureLib/pfConsole/pfConsoleCommandsNet.cpp b/Sources/Plasma/FeatureLib/pfConsole/pfConsoleCommandsNet.cpp index fd644197..b3145a78 100644 --- a/Sources/Plasma/FeatureLib/pfConsole/pfConsoleCommandsNet.cpp +++ b/Sources/Plasma/FeatureLib/pfConsole/pfConsoleCommandsNet.cpp @@ -1132,7 +1132,7 @@ PF_CONSOLE_CMD( "Set the Auth Server N key" ) { int baseLength = hsStrlen((const char *)params[0]); - if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength)) { + if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength, (const char *)params[0])) { PrintStringF(PrintString, "Invalid key: should be exactly %u bytes", kNetDiffieHellmanKeyBits / 8); return; @@ -1150,7 +1150,7 @@ PF_CONSOLE_CMD( "Set the Auth Server X key" ) { int baseLength = hsStrlen((const char *)params[0]); - if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength)) { + if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength, (const char *)params[0])) { PrintStringF(PrintString, "Invalid key: should be exactly %u bytes", kNetDiffieHellmanKeyBits / 8); return; @@ -1185,7 +1185,7 @@ PF_CONSOLE_CMD( "Set the Csr Server N key" ) { int baseLength = hsStrlen((const char *)params[0]); - if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength)) { + if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength, (const char *)params[0])) { PrintStringF(PrintString, "Invalid key: should be exactly %u bytes", kNetDiffieHellmanKeyBits / 8); return; @@ -1203,7 +1203,7 @@ PF_CONSOLE_CMD( "Set the Csr Server X key" ) { int baseLength = hsStrlen((const char *)params[0]); - if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength)) { + if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength, (const char *)params[0])) { PrintStringF(PrintString, "Invalid key: should be exactly %u bytes", kNetDiffieHellmanKeyBits / 8); return; @@ -1226,7 +1226,7 @@ PF_CONSOLE_CMD( "Set the Game Server N key" ) { int baseLength = hsStrlen((const char *)params[0]); - if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength)) { + if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength, (const char *)params[0])) { PrintStringF(PrintString, "Invalid key: should be exactly %u bytes", kNetDiffieHellmanKeyBits / 8); return; @@ -1244,7 +1244,7 @@ PF_CONSOLE_CMD( "Set the Game Server X key" ) { int baseLength = hsStrlen((const char *)params[0]); - if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength)) { + if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength, (const char *)params[0])) { PrintStringF(PrintString, "Invalid key: should be exactly %u bytes", kNetDiffieHellmanKeyBits / 8); return; @@ -1279,7 +1279,7 @@ PF_CONSOLE_CMD( "Set the GateKeeper Server N key" ) { int baseLength = hsStrlen((const char *)params[0]); - if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength)) { + if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength, (const char *)params[0])) { PrintStringF(PrintString, "Invalid key: should be exactly %u bytes", kNetDiffieHellmanKeyBits / 8); return; @@ -1297,7 +1297,7 @@ PF_CONSOLE_CMD( "Set the GateKeeper Server X key" ) { int baseLength = hsStrlen((const char *)params[0]); - if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength)) { + if ((kNetDiffieHellmanKeyBits / 8) != Base64DecodeSize(baseLength, (const char *)params[0])) { PrintStringF(PrintString, "Invalid key: should be exactly %u bytes", kNetDiffieHellmanKeyBits / 8); return; diff --git a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBase64.cpp b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBase64.cpp index 70b274dc..ac162521 100644 --- a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBase64.cpp +++ b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBase64.cpp @@ -132,7 +132,7 @@ unsigned Base64Decode ( byte * dstData ) { ASSERT(srcData); - ASSERT(dstChars >= Base64DecodeSize(srcChars)); + ASSERT(dstChars >= Base64DecodeSize(srcChars, srcData)); ASSERT(dstData); const byte * dstBase = dstData; diff --git a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBase64.h b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBase64.h index 7794d123..8ca5c6a8 100644 --- a/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBase64.h +++ b/Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBase64.h @@ -46,7 +46,8 @@ const unsigned kBase64EncodeBlock = 4; const unsigned kBase64EncodeMultiple = 3; inline unsigned Base64EncodeSize (unsigned srcChars) { - return srcChars * kBase64EncodeBlock / kBase64EncodeMultiple + kBase64EncodeBlock; + return (srcChars + kBase64EncodeMultiple - 1) / kBase64EncodeMultiple + * kBase64EncodeBlock; } unsigned Base64Encode ( unsigned srcChars, @@ -55,8 +56,10 @@ unsigned Base64Encode ( char * dstData ); -inline unsigned Base64DecodeSize (unsigned srcChars) { - return srcChars * kBase64EncodeMultiple / kBase64EncodeBlock + kBase64EncodeMultiple; +inline unsigned Base64DecodeSize (unsigned srcChars, const char srcData[]) { + return srcChars * kBase64EncodeMultiple / kBase64EncodeBlock + - ((srcChars >= 1 && srcData[srcChars - 1] == '=') ? 1 : 0) + - ((srcChars >= 2 && srcData[srcChars - 2] == '=') ? 1 : 0); } unsigned Base64Decode ( unsigned srcChars,