1
0
mirror of https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git synced 2025-07-17 02:51:27 +00:00

Fix Base64 size calculation behavior to always return the actual conversion size

This commit is contained in:
2011-04-12 22:58:55 -07:00
parent d4ffe82237
commit dfebad4a54
3 changed files with 15 additions and 12 deletions

View File

@ -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;

View File

@ -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,