Continued work.

This commit is contained in:
Brad Arant 2020-03-03 15:46:24 -08:00
parent 4286a7b449
commit 947eef21e2
12 changed files with 323 additions and 95 deletions

103
Base64.cpp Normal file
View File

@ -0,0 +1,103 @@
#include "Base64.h"
#include <iostream>
namespace coreutils {
const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
bool Base64::isBase64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::string Base64::encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i) {
for(j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
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]];
while((i++ < 3))
ret += '=';
}
return ret;
}
std::string Base64::decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && isBase64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) {
for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]);
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[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret += char_array_3[i];
i = 0;
}
}
if (i) {
for (j = 0; j < i; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]);
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];
}
return ret;
}
}

19
Base64.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef __Base64_h__
#define __Base64_h__
#include <string>
namespace coreutils {
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);
};
}
#endif

View File

@ -1,8 +1,8 @@
#ifndef __Directory_h__
#define __Directory_h__
# define __Directory_h__
#include "includes"
#include "DirectoryEntry.h"
# include "includes"
# include "DirectoryEntry.h"
namespace coreutils {
@ -43,9 +43,6 @@ namespace coreutils {
std::map<std::string, DirectoryEntry> directory;
std::map<std::string, DirectoryEntry>::iterator cursor;
};
}

View File

@ -1,7 +1,7 @@
#ifndef __DirectoryEntry_h__
#define __DirectoryEntry_h__
# define __DirectoryEntry_h__
#include "includes"
# include "includes"
namespace coreutils {

View File

@ -40,4 +40,8 @@ namespace coreutils {
::write(fd, data.c_str(), data.length());
}
std::string File::asString() {
return std::string(buffer, size);
}
}

1
File.h
View File

@ -19,6 +19,7 @@ namespace coreutils {
void setBufferSize(size_t size);
void read();
void write(std::string data);
std::string asString();
char *buffer;
size_t size;

View File

@ -9,8 +9,10 @@ namespace coreutils {
IMFHeader::IMFHeader(PString &in) {
key = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-");
if(!in.ifNext(":"))
if(!in.ifNext(":")) {
printf("%02X\n", in.str()[0]);
throw coreutils::Exception("Invalid character in expected header token.");
}
in.skipWhitespace();
value = in.getTokenExclude("\r\n");
if(!in.ifNext("\r\n"))

View File

@ -2,6 +2,7 @@
#include "IMFHeader.h"
#include "IMFMultipart.h"
#include "IMFPlainText.h"
#include "Log.h"
namespace coreutils {
@ -12,11 +13,13 @@ namespace coreutils {
}
void IMFMessage::parse(PString &in) {
coreutils::Log(coreutils::LOG_DEBUG_4) << "[" << in.str() << "]";
if(in.size() == 0)
return;
while(!in.ifNext("\r\n"))
while(!in.ifNext("\r\n") && !in.eod()) {
headers.emplace_back(in);
}
std::string type = getHeader("Content-Type");
@ -94,5 +97,4 @@ namespace coreutils {
return body;
}
}

View File

@ -1,5 +1,6 @@
#include "IMFRequest.h"
#include "Exception.h"
#include "Log.h"
namespace coreutils {
@ -13,6 +14,9 @@ namespace coreutils {
if(!in.ifNext(" "))
throw coreutils::Exception("Expecting space after URI.");
protocol = in.getTokenExclude("\r\n");
coreutils::Log(coreutils::LOG_DEBUG_4) << "[" << in.str() << "]";
if(!in.ifNext("\r\n"))
throw coreutils::Exception("Expecting CRLF after protocol.");
}

View File

@ -62,11 +62,11 @@ namespace coreutils {
if(output) {
mtx.lock();
std::stringstream out;
out << seq << "." << this->str() << std::endl;;
mtx.lock();
if(logListener)
logListener->logSend(out.str());

View File

@ -10,14 +10,25 @@ namespace coreutils {
this->pstring = pstring;
}
std::vector<PString> & PString::getList() {
return list;
}
std::string PString::str() {
return pstring.substr(cursor);
}
std::vector<PString> PString::split(std::string delimiter, int maxSize) {
std::vector<PString> list;
size_t end,
pos = cursor;
list.clear();
if(pstring.size() == 0) {
list.push_back(PString(""));
return list;
}
size_t end;
size_t pos = cursor;
while(pos < pstring.length()) {
end = pstring.find(delimiter, pos);
if(end == std::string::npos)
@ -50,6 +61,14 @@ namespace coreutils {
return pstring.substr(start, cursor - start);
}
coreutils::PString PString::operator[](int index) {
return list[index];
}
bool PString::eod() {
return cursor >= pstring.size();
}
bool PString::ifNext(std::string value) {
bool test = (value == pstring.substr(cursor, value.length()));
if(test)

View File

@ -5,23 +5,100 @@
namespace coreutils {
///
/// Use the PString object when advanced parsing algorithms are required to simplify
/// parsing. Two methods of parsing are available within the PString object. One
/// method involves using the split method to divide the string into several PString
/// objects. Split separates the string based upon a delimiter. Another method employs
/// the use of the getToken methods provided to move to through the string and assign
/// values based upon the followed path.
///
class PString {
public:
///
///
///
PString();
///
///
///
PString(std::string pstring);
///
///
///
std::vector<PString> & getList();
///
///
///
std::string str();
///
///
///
std::vector<PString> split(std::string delimiter, int maxSize = 0);
///
///
///
std::string getTokenInclude(std::string include);
///
///
///
std::string getTokenExclude(std::string exclude);
///
///
///
coreutils::PString operator[](int index);
///
///
///
bool eod();
///
///
///
bool ifNext(std::string value);
///
///
///
void skipWhitespace();
///
///
///
int cursor = 0;
///
///
///
int size();
private:
std::string pstring;
std::vector<PString> list;
};