added b64 encode/decode to MString.

This commit is contained in:
Brad Arant committed 2025-02-06 14:34:11 -08:00
1 parent d1c450fe48
commit 5e6d162a05
8 files changed
+61 -35

No files matched your search

+24 -22
View File
@@ -12,9 +12,12 @@ namespace coreutils {
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::string Base64::encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
MString Base64::encode(ZString data) {
data.push();
data.reset();
MString ret;
int in_len = data.getLength();
int i = 0;
int j = 0;
unsigned char char_array_3[3];
@@ -22,7 +25,7 @@ namespace coreutils {
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
char_array_3[i++] = data.nextChar();
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
@@ -31,10 +34,9 @@ namespace coreutils {
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
ret.write(base64_chars[char_array_4[i]]);
i = 0;
}
}
}
if (i) {
@@ -47,29 +49,28 @@ namespace coreutils {
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
ret.write(base64_chars[char_array_4[j]]);
while((i++ < 3))
ret += '=';
ret.write('=');
}
data.pop();
return ret;
}
std::string Base64::decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
MString Base64::decode(ZString b64data) {
b64data.push();
b64data.reset();
int in_len = b64data.getLength();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
MString ret;
while (in_len-- && ( encoded_string[in_] != '=') && isBase64(encoded_string[in_])) {
while (in_len-- && (b64data.getChar() != '=') && isBase64(b64data.getChar())) {
char_array_4[i++] = encoded_string[in_]; in_++;
char_array_4[i++] = b64data.nextChar();
if (i ==4) {
for (i = 0; i <4; i++)
@@ -80,7 +81,7 @@ namespace coreutils {
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
ret.write(char_array_3[i]);
i = 0;
}
@@ -94,9 +95,10 @@ namespace coreutils {
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
for (j = 0; (j < i - 1); j++)
ret.write(char_array_3[j]);
}
b64data.pop();
return ret;
}