-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor/structure and tightening of accessors.
- Loading branch information
J Ninja
committed
Jun 27, 2019
1 parent
acb1e54
commit 94033fa
Showing
5 changed files
with
91 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
cmake_minimum_required(VERSION 2.6) | ||
project(iching) | ||
cmake_minimum_required(VERSION 3.1) | ||
project(iching VERSION 1.0.0 LANGUAGES CXX) | ||
add_subdirectory(src) | ||
#add_subdirectory(tests) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "util.h" | ||
|
||
#include <iostream> | ||
#include <random> | ||
#include <sstream> | ||
|
||
#include <boost/archive/iterators/base64_from_binary.hpp> | ||
#include <boost/archive/iterators/binary_from_base64.hpp> | ||
#include <boost/archive/iterators/remove_whitespace.hpp> | ||
#include <boost/archive/iterators/transform_width.hpp> | ||
|
||
using namespace boost::archive::iterators; | ||
using std::cerr; | ||
using std::cout; | ||
using std::endl; | ||
|
||
int Util::Random::random_generator(int i) | ||
{ | ||
return std::rand() % i; | ||
} | ||
|
||
string & Util::String::rtrim(const string& s, const char* t) | ||
{ | ||
|
||
return string(s).erase(s.find_last_not_of(t) + 1); | ||
} | ||
|
||
string & Util::String::ltrim(const string& s, const char* t) | ||
{ | ||
return string(s).erase(0, s.find_first_not_of(t)); | ||
} | ||
|
||
string & Util::String::trim(const string& s, const char* t) | ||
{ | ||
return Util::String::ltrim(Util::String::rtrim(s, t), t); | ||
} | ||
|
||
string Util::Base64::b64_decode(const string& msg) | ||
{ | ||
typedef transform_width<binary_from_base64<remove_whitespace<std::string::const_iterator>>, 8, 6> ItBinaryT; | ||
string output = ""; | ||
string padded = msg; | ||
|
||
try { | ||
size_t num_pad_chars((4 - msg.size() % 4) % 4); | ||
padded.append(num_pad_chars, '='); | ||
size_t pad_chars(std::count(padded.begin(), padded.end(), '=')); | ||
std::replace(padded.begin(), padded.end(), '=', 'A'); | ||
std::string output(ItBinaryT(padded.begin()), ItBinaryT(padded.end())); | ||
output.erase(output.end() - pad_chars, output.end()); | ||
return output; | ||
} catch (std::exception const& e) { | ||
cerr << "Exception caught: " << e.what() << endl; | ||
} | ||
|
||
return output; | ||
} | ||
|
||
string Util::Base64::b64_encode(const string& msg) | ||
{ | ||
std::stringstream os; | ||
typedef base64_from_binary<transform_width<const char*, 6, 8>> base64_text; | ||
|
||
std::copy( | ||
base64_text(msg.c_str()), | ||
base64_text(msg.c_str() + msg.size()), | ||
std::ostream_iterator<char>(os)); | ||
return os.str(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,26 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include <random> | ||
#include <sstream> | ||
#include <string> | ||
|
||
#include <boost/archive/iterators/base64_from_binary.hpp> | ||
#include <boost/archive/iterators/binary_from_base64.hpp> | ||
#include <boost/archive/iterators/remove_whitespace.hpp> | ||
#include <boost/archive/iterators/transform_width.hpp> | ||
|
||
using namespace boost::archive::iterators; | ||
using std::cerr; | ||
using std::cout; | ||
using std::endl; | ||
using std::string; | ||
|
||
namespace Util | ||
{ | ||
namespace Random | ||
{ | ||
int random_generator(int i) | ||
{ | ||
return std::rand() % i; | ||
} | ||
int random_generator(int i); | ||
} | ||
|
||
namespace Base64 | ||
{ | ||
string b64_encode(const string& msg) | ||
{ | ||
std::stringstream os; | ||
typedef base64_from_binary<transform_width<const char*, 6, 8>> base64_text; | ||
|
||
std::copy( | ||
base64_text(msg.c_str()), | ||
base64_text(msg.c_str() + msg.size()), | ||
std::ostream_iterator<char>(os)); | ||
return os.str(); | ||
} | ||
|
||
string b64_decode(const string& msg) | ||
{ | ||
typedef transform_width<binary_from_base64<remove_whitespace<std::string::const_iterator>>, 8, 6> ItBinaryT; | ||
string output = ""; | ||
string padded = msg; | ||
|
||
try { | ||
size_t num_pad_chars((4 - msg.size() % 4) % 4); | ||
padded.append(num_pad_chars, '='); | ||
size_t pad_chars(std::count(padded.begin(), padded.end(), '=')); | ||
std::replace(padded.begin(), padded.end(), '=', 'A'); | ||
std::string output(ItBinaryT(padded.begin()), ItBinaryT(padded.end())); | ||
output.erase(output.end() - pad_chars, output.end()); | ||
return output; | ||
} catch (std::exception const& e) { | ||
cerr << "Exception caught: " << e.what() << endl; | ||
} | ||
|
||
return output; | ||
} | ||
string b64_encode(const string& msg); | ||
string b64_decode(const string& msg); | ||
} | ||
|
||
namespace String | ||
{ | ||
const char* ws = " \t\n\r\f\v"; | ||
|
||
inline string& rtrim(const std::string& s, const char* t = ws) | ||
{ | ||
return string(s).erase(s.find_last_not_of(t) + 1); | ||
} | ||
|
||
inline string& ltrim(const string& s, const char* t = ws) | ||
{ | ||
return string(s).erase(0, s.find_first_not_of(t)); | ||
} | ||
|
||
inline string& trim(const string& s, const char* t = ws) | ||
{ | ||
return ltrim(rtrim(s, t), t); | ||
} | ||
inline string& rtrim(const string& s, const char* t = " \t\n\r\f\v"); | ||
inline string& ltrim(const string& s, const char* t = " \t\n\r\f\v"); | ||
inline string& trim(const string& s, const char* t = " \t\n\r\f\v"); | ||
} | ||
} |