Verified Commit 39656742 authored by Alvarez, Gonzalo's avatar Alvarez, Gonzalo
Browse files

PsiBase64: better interface

parent 6ef8ba85
......@@ -42,9 +42,9 @@ int main()
const PsimagLite::String s = "ADP GmbH\nAnalyse Design & Programmierung"
"\nGesellschaft mit beschränkter Haftung" ;
PsimagLite::PsiBase64 base64;
PsimagLite::String encoded = base64.encode(s);
PsimagLite::PsiBase64::Encode base64encode(s);
PsimagLite::String encoded = base64encode();
std::cout<<"encoded: "<<encoded<<"\n";
std::cout<<"decoded: "<<base64.decode(encoded)<<"\n";
std::cout<<"decoded: "<<PsimagLite::PsiBase64::Decode(encoded)()<<"\n";
}
......@@ -196,7 +196,7 @@ public:
continue;
}
if (str = end) {
if (str == end) {
mode = 2;
break;
}
......@@ -217,8 +217,8 @@ public:
assert(mode == 2);
PsiBase64 base;
data_ = base.decode(buffer);
PsiBase64::Decode base64decode(buffer);
data_ = base64decode();
check();
if (verbose_) {
......
......@@ -42,107 +42,124 @@ namespace PsimagLite {
class PsiBase64 {
static const String base64Chars_;
public:
const String& encode(const String& str)
{
return encode(reinterpret_cast<const unsigned char*>(str.c_str()),str.length());
}
const String& encode(unsigned char const* bytesToEncode, unsigned int inLen)
{
buffer_ = "";
int i = 0;
int j = 0;
unsigned char charArray3[3];
unsigned char charArray4[4];
while (inLen--) {
charArray3[i++] = *(bytesToEncode++);
if (i == 3) {
class Encode {
public:
Encode(const String& str)
{
encode_(reinterpret_cast<const unsigned char*>(str.c_str()),str.length());
}
Encode(unsigned char const* bytesToEncode, unsigned int inLen)
{
encode_(bytesToEncode,inLen);
}
const String& operator()() const { return buffer_; }
private:
void encode_(unsigned char const* bytesToEncode, unsigned int inLen)
{
buffer_ = "";
int i = 0;
int j = 0;
unsigned char charArray3[3];
unsigned char charArray4[4];
while (inLen--) {
charArray3[i++] = *(bytesToEncode++);
if (i == 3) {
charArray4[0] = (charArray3[0] & 0xfc) >> 2;
charArray4[1] = ((charArray3[0] & 0x03)<<4) + ((charArray3[1] & 0xf0)>>4);
charArray4[2] = ((charArray3[1] & 0x0f)<<2) + ((charArray3[2] & 0xc0)>>6);
charArray4[3] = charArray3[2] & 0x3f;
for (i = 0; (i <4) ; i++)
buffer_ += base64Chars_[charArray4[i]];
i = 0;
}
}
if (i) {
for (j = i; j < 3; j++)
charArray3[j] = '\0';
charArray4[0] = (charArray3[0] & 0xfc) >> 2;
charArray4[1] = ((charArray3[0] & 0x03)<<4) + ((charArray3[1] & 0xf0)>>4);
charArray4[2] = ((charArray3[1] & 0x0f)<<2) + ((charArray3[2] & 0xc0)>>6);
charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
charArray4[3] = charArray3[2] & 0x3f;
for (i = 0; (i <4) ; i++)
buffer_ += base64Chars_[charArray4[i]];
i = 0;
for (j = 0; (j < i + 1); j++)
buffer_ += base64Chars_[charArray4[j]];
while ((i++ < 3)) buffer_ += '=';
}
}
if (i) {
for (j = i; j < 3; j++)
charArray3[j] = '\0';
String buffer_;
}; // class Encode
charArray4[0] = (charArray3[0] & 0xfc) >> 2;
charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
charArray4[3] = charArray3[2] & 0x3f;
class Decode {
for (j = 0; (j < i + 1); j++)
buffer_ += base64Chars_[charArray4[j]];
public:
while ((i++ < 3)) buffer_ += '=';
}
Decode(const String& encodedString)
{
buffer_ = "";
int inLen = encodedString.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char charArray4[4], charArray3[3];
while (inLen-- && (encodedString[in_] != '=') && isBase64(encodedString[in_])) {
charArray4[i++] = encodedString[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
charArray4[i] = base64Chars_.find(charArray4[i]);
charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
return buffer_;
}
for (i = 0; (i < 3); i++)
buffer_ += charArray3[i];
i = 0;
}
}
const String& decode(const String& encodedString)
{
buffer_ = "";
int inLen = encodedString.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char charArray4[4], charArray3[3];
if (i) {
for (j = i; j <4; j++)
charArray4[j] = 0;
while (inLen-- && (encodedString[in_] != '=') && isBase64(encodedString[in_])) {
charArray4[i++] = encodedString[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
charArray4[i] = base64Chars_.find(charArray4[i]);
for (j = 0; j <4; j++)
charArray4[j] = base64Chars_.find(charArray4[j]);
charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
for (i = 0; (i < 3); i++)
buffer_ += charArray3[i];
i = 0;
for (j = 0; (j < i - 1); j++) buffer_ += charArray3[j];
}
}
if (i) {
for (j = i; j <4; j++)
charArray4[j] = 0;
for (j = 0; j <4; j++)
charArray4[j] = base64Chars_.find(charArray4[j]);
const String& operator()() const { return buffer_; }
charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
private:
for (j = 0; (j < i - 1); j++) buffer_ += charArray3[j];
static bool isBase64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
return buffer_;
}
const String& result() const { return buffer_; }
private:
static bool isBase64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
static const String base64Chars_;
String buffer_;
};
String buffer_;
}; // class Decode
}; // class PsiBase64
}
#endif // PSIBASE64_H
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment