added b64 encode/decode to MString.

This commit is contained in:
Brad Arant 2025-02-06 14:34:11 -08:00
parent d1c450fe48
commit 5e6d162a05
8 changed files with 61 additions and 35 deletions

View File

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

View File

@ -1,20 +1,22 @@
#ifndef __Base64_h__ #ifndef __Base64_h__
#define __Base64_h__ #define __Base64_h__
#include <string> #include "ZString.h"
#include "MString.h"
namespace coreutils { namespace coreutils {
/// ///
/// /// Use the Base64 object when you need to translate character string
/// to/from base64 encoded strings.
/// ///
class Base64 { class Base64 {
public: public:
bool isBase64(unsigned char c); bool isBase64(unsigned char c);
std::string encode(unsigned char const* bytes_to_encode, unsigned int in_len); MString encode(ZString data);
std::string decode(std::string const& encoded_string); MString decode(ZString b64data);
}; };

View File

@ -2,6 +2,7 @@
#include "Log.h" #include "Log.h"
#include "uuid/uuid.h" #include "uuid/uuid.h"
#include "Exception.h" #include "Exception.h"
#include "Base64.h"
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <format> #include <format>
@ -365,21 +366,21 @@ namespace coreutils {
} }
MString MString::toBase64() { MString MString::toBase64() {
MString target; Base64 b64;
return target; return b64.encode(*this);
} }
MString MString::fromBase64() { MString MString::fromBase64() {
MString target; Base64 b64;
return target; return b64.decode(*this);
} }
MString MString::toUpper() { MString MString::toUpper() {
MString target; MString target;
for(int ix = 0; ix < length; ++ix) for(int ix = 0; ix < length; ++ix)
write(std::toupper(data[ix])); target.write(std::toupper(data[ix]));
return target; return target;
} }
@ -387,7 +388,7 @@ namespace coreutils {
MString MString::toLower() { MString MString::toLower() {
MString target; MString target;
for(int ix = 0; ix < length; ++ix) for(int ix = 0; ix < length; ++ix)
write(std::tolower(data[ix])); target.write(std::tolower(data[ix]));
return target; return target;
} }

View File

@ -614,7 +614,11 @@ namespace coreutils {
} }
return *this; return *this;
} }
char ZString::getChar() {
return *cursor;
}
char ZString::nextChar() { char ZString::nextChar() {
char temp = 0; char temp = 0;
if(!eod()) if(!eod())

View File

@ -434,6 +434,12 @@ namespace coreutils {
bool trimCRLF(); bool trimCRLF();
///
/// Returns the character at the cursor and returns. Cursor is not changed.
///
char getChar();
/// ///
/// Returns the character at the cursor and advances the cursor one /// Returns the character at the cursor and advances the cursor one
/// position until end of data. /// position until end of data.

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash
g++ -g -std=c++20 -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils g++ -g -std=c++20 -o zstring_test zstring_test.cpp -I.. -L.. -lCoreUtils
g++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils g++ -g -std=c++20 -o mstring_test mstring_test.cpp -I.. -L.. -lCoreUtils -lb64
g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils g++ -o jstring_test jstring_test.cpp -I.. -L.. -lCoreUtils

Binary file not shown.

View File

@ -95,8 +95,19 @@ int main(int argc, char **argv) {
test28 = test29; test28 = test29;
std::cout << "int: " << test28 << std::endl; std::cout << "int: " << test28 << std::endl;
coreutils::MString test30 = "ABCDEF"; coreutils::MString test30 = "ABCDEF0123";
std::cout << test30 << " to hex " << test30.toHex() << " from hex " << test30.toHex().fromHex() << std::endl; std::cout << test30 << " to hex " << test30.toHex() << " from hex " << test30.toHex().fromHex() << std::endl;
std::cout << test30 << " to lower " << test30.toLower() << std::endl;
coreutils::MString test31 = "abcdef0123";
std::cout << test31 << " to upper " << test31.toUpper() << std::endl;
coreutils::MString test32 = "This is a test of converting this string to base64";
std::cout << test32 << " to base64 " << test32.toBase64() << std::endl;
coreutils::MString test33 = test32.toBase64();
std::cout << test33 << " from base64 " << test33.fromBase64() << std::endl;
return 0; return 0;
} }