added b64 encode/decode to MString.
This commit is contained in:
parent
d1c450fe48
commit
5e6d162a05
46
Base64.cpp
46
Base64.cpp
@ -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;
|
||||
}
|
||||
|
||||
|
10
Base64.h
10
Base64.h
@ -1,20 +1,22 @@
|
||||
#ifndef __Base64_h__
|
||||
#define __Base64_h__
|
||||
|
||||
#include <string>
|
||||
#include "ZString.h"
|
||||
#include "MString.h"
|
||||
|
||||
namespace coreutils {
|
||||
|
||||
///
|
||||
///
|
||||
/// Use the Base64 object when you need to translate character string
|
||||
/// to/from base64 encoded strings.
|
||||
///
|
||||
|
||||
class Base64 {
|
||||
|
||||
public:
|
||||
bool isBase64(unsigned char c);
|
||||
std::string encode(unsigned char const* bytes_to_encode, unsigned int in_len);
|
||||
std::string decode(std::string const& encoded_string);
|
||||
MString encode(ZString data);
|
||||
MString decode(ZString b64data);
|
||||
|
||||
};
|
||||
|
||||
|
13
MString.cpp
13
MString.cpp
@ -2,6 +2,7 @@
|
||||
#include "Log.h"
|
||||
#include "uuid/uuid.h"
|
||||
#include "Exception.h"
|
||||
#include "Base64.h"
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <format>
|
||||
@ -365,21 +366,21 @@ namespace coreutils {
|
||||
}
|
||||
|
||||
MString MString::toBase64() {
|
||||
MString target;
|
||||
return target;
|
||||
Base64 b64;
|
||||
return b64.encode(*this);
|
||||
}
|
||||
|
||||
|
||||
MString MString::fromBase64() {
|
||||
MString target;
|
||||
return target;
|
||||
Base64 b64;
|
||||
return b64.decode(*this);
|
||||
}
|
||||
|
||||
|
||||
MString MString::toUpper() {
|
||||
MString target;
|
||||
for(int ix = 0; ix < length; ++ix)
|
||||
write(std::toupper(data[ix]));
|
||||
target.write(std::toupper(data[ix]));
|
||||
return target;
|
||||
}
|
||||
|
||||
@ -387,7 +388,7 @@ namespace coreutils {
|
||||
MString MString::toLower() {
|
||||
MString target;
|
||||
for(int ix = 0; ix < length; ++ix)
|
||||
write(std::tolower(data[ix]));
|
||||
target.write(std::tolower(data[ix]));
|
||||
return target;
|
||||
}
|
||||
|
||||
|
@ -614,7 +614,11 @@ namespace coreutils {
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
char ZString::getChar() {
|
||||
return *cursor;
|
||||
}
|
||||
|
||||
char ZString::nextChar() {
|
||||
char temp = 0;
|
||||
if(!eod())
|
||||
|
@ -434,6 +434,12 @@ namespace coreutils {
|
||||
|
||||
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
|
||||
/// position until end of data.
|
||||
|
@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
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
|
||||
|
Binary file not shown.
@ -95,8 +95,19 @@ int main(int argc, char **argv) {
|
||||
test28 = test29;
|
||||
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 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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user