diff --git a/.gitignore b/.gitignore index 4581ef2..d1fd08a 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,7 @@ *.exe *.out *.app + +#python temporary file +__pycache__ +*.pyc diff --git a/algue/base64.cpp b/algue/base64.cpp new file mode 100644 index 0000000..4f067ab --- /dev/null +++ b/algue/base64.cpp @@ -0,0 +1,111 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license APACHE v2.0 (see license file) + */ +#include +#include + +static const std::string base64Elements = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +const std::vector& getReverseTable() { + static std::vector table; + if (table.size() == 0) { + table.resize(256,0); + for (size_t iii=0; iii> 2; + bit4[1] = ((bit3[0] & 0x03) << 4) + ((bit3[1] & 0xf0) >> 4); + bit4[2] = ((bit3[1] & 0x0f) << 2) + ((bit3[2] & 0xc0) >> 6); + bit4[3] = bit3[2] & 0x3f; + for(int32_t jjj=0; jjj<4 ; ++jjj) { + out += base64Elements[bit4[jjj]]; + } + } + } + // Residual packing ... + if (iii != 0) { + for(int32_t jjj=iii; jjj<3; ++jjj) { + bit3[jjj] = 0; + } + bit4[0] = (bit3[0] & 0xfc) >> 2; + bit4[1] = ((bit3[0] & 0x03) << 4) + ((bit3[1] & 0xf0) >> 4); + bit4[2] = ((bit3[1] & 0x0f) << 2) + ((bit3[2] & 0xc0) >> 6); + bit4[3] = bit3[2] & 0x3f; + for (int32_t jjj=0; jjj algue::base64::decode(const std::string& _data) { + int32_t len = _data.size(); + int32_t iii = 0; + int32_t id = 0; + uint8_t bit4[4]; + uint8_t bit3[3]; + std::vector out; + const std::vector& base64ElementsReverse = getReverseTable(); + while ( len-- + && _data[id] != '=' + && isBase64(_data[id]) == true) { + bit4[iii++] = _data[id]; + id++; + if (iii == 4) { + iii = 0; + for (int32_t jjj=0; jjj<4; ++jjj) { + bit4[jjj] = base64ElementsReverse[bit4[jjj]]; + } + bit3[0] = (bit4[0] << 2) + ((bit4[1] & 0x30) >> 4); + bit3[1] = ((bit4[1] & 0xf) << 4) + ((bit4[2] & 0x3c) >> 2); + bit3[2] = ((bit4[2] & 0x3) << 6) + bit4[3]; + for (int32_t jjj=0; jjj<3; ++jjj) { + out.push_back(bit3[jjj]); + } + } + } + if (iii != 0) { + for (int32_t jjj=iii; jjj<4; ++jjj) { + bit4[jjj] = 0; + } + for (int32_t jjj = 0; jjj <4; ++jjj) { + bit4[jjj] = base64ElementsReverse[bit4[jjj]]; + } + bit3[0] = (bit4[0] << 2) + ((bit4[1] & 0x30) >> 4); + bit3[1] = ((bit4[1] & 0xf) << 4) + ((bit4[2] & 0x3c) >> 2); + bit3[2] = ((bit4[2] & 0x3) << 6) + bit4[3]; + for (int32_t jjj=0; jjj + +namespace algue { + namespace base64 { + std::string encode(const uint8_t* _data, int32_t _len); + inline std::string encode(const std::vector& _data) { + return algue::base64::encode(&_data[0], _data.size()); + } + inline std::string encode(const std::string& _data) { + return algue::base64::encode(reinterpret_cast(&_data[0]), _data.size()); + } + std::vector decode(const std::string& _data); + } +} diff --git a/algue/debug.cpp b/algue/debug.cpp new file mode 100644 index 0000000..3c4d7b0 --- /dev/null +++ b/algue/debug.cpp @@ -0,0 +1,13 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license APACHE v2.0 (see license file) + */ + +#include + +int32_t algue::getLogId() { + static int32_t g_val = elog::registerInstance("algue"); + return g_val; +} + diff --git a/algue/debug.h b/algue/debug.h new file mode 100644 index 0000000..20743c7 --- /dev/null +++ b/algue/debug.h @@ -0,0 +1,37 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license APACHE v2.0 (see license file) + */ +#pragma once + +#include + +namespace algue { + int32_t getLogId(); +}; +#define ALGUE_BASE(info,data) ELOG_BASE(algue::getLogId(),info,data) + +#define ALGUE_CRITICAL(data) ALGUE_BASE(1, data) +#define ALGUE_ERROR(data) ALGUE_BASE(2, data) +#define ALGUE_WARNING(data) ALGUE_BASE(3, data) +#ifdef DEBUG + #define ALGUE_INFO(data) ALGUE_BASE(4, data) + #define ALGUE_DEBUG(data) ALGUE_BASE(5, data) + #define ALGUE_VERBOSE(data) ALGUE_BASE(6, data) + #define ALGUE_TODO(data) ALGUE_BASE(4, "TODO : " << data) +#else + #define ALGUE_INFO(data) do { } while(false) + #define ALGUE_DEBUG(data) do { } while(false) + #define ALGUE_VERBOSE(data) do { } while(false) + #define ALGUE_TODO(data) do { } while(false) +#endif + +#define ALGUE_ASSERT(cond,data) \ + do { \ + if (!(cond)) { \ + JSON_CRITICAL(data); \ + assert(!#cond); \ + } \ + } while (0) + diff --git a/algue/md5.cpp b/algue/md5.cpp new file mode 100644 index 0000000..0be8f99 --- /dev/null +++ b/algue/md5.cpp @@ -0,0 +1,17 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license APACHE v2.0 (see license file) + */ +#include +#include +#include + +std::vector algue::md5::encode(const uint8_t* _data, int32_t _len) { + std::vector out; + out.resize(MD5_DIGEST_LENGTH); + MD5(_data, _len, &out[0]); + return out; +} + + diff --git a/algue/md5.h b/algue/md5.h new file mode 100644 index 0000000..ff19b8d --- /dev/null +++ b/algue/md5.h @@ -0,0 +1,20 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license APACHE v2.0 (see license file) + */ +#pragma once + +#include + +namespace algue { + namespace md5 { + std::vector encode(const uint8_t* _data, int32_t _len); + inline std::vector encode(const std::vector& _data) { + return algue::md5::encode(&_data[0], _data.size()); + } + inline std::vector encode(const std::string& _data) { + return algue::md5::encode(reinterpret_cast(&_data[0]), _data.size()); + } + } +} diff --git a/algue/sha1.cpp b/algue/sha1.cpp new file mode 100644 index 0000000..e896a94 --- /dev/null +++ b/algue/sha1.cpp @@ -0,0 +1,17 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license APACHE v2.0 (see license file) + */ +#include +#include +#include + +std::vector algue::sha1::encode(const uint8_t* _data, int32_t _len) { + std::vector out; + out.resize(SHA_DIGEST_LENGTH); + SHA1(_data, _len, &out[0]); + return out; +} + + diff --git a/algue/sha1.h b/algue/sha1.h new file mode 100644 index 0000000..b799899 --- /dev/null +++ b/algue/sha1.h @@ -0,0 +1,20 @@ +/** @file + * @author Edouard DUPIN + * @copyright 2011, Edouard DUPIN, all right reserved + * @license APACHE v2.0 (see license file) + */ +#pragma once + +#include + +namespace algue { + namespace sha1 { + std::vector encode(const uint8_t* _data, int32_t _len); + inline std::vector encode(const std::vector& _data) { + return algue::sha1::encode(&_data[0], _data.size()); + } + inline std::vector encode(const std::string& _data) { + return algue::sha1::encode(reinterpret_cast(&_data[0]), _data.size()); + } + } +} diff --git a/lutin_algue.py b/lutin_algue.py new file mode 100644 index 0000000..ffc6705 --- /dev/null +++ b/lutin_algue.py @@ -0,0 +1,45 @@ +#!/usr/bin/python +import lutin.module as module +import lutin.tools as tools + + +def get_type(): + return "LIBRARY" + +def get_desc(): + return "Algorithm generic" + +def get_licence(): + return "APACHE-2" + +def get_compagny_type(): + return "com" + +def get_compagny_name(): + return "atria-soft" + +def get_maintainer(): + return ["Mr DUPIN Edouard "] + +def get_version(): + return [0,1,"dev"] + +def create(target, module_name): + my_module = module.Module(__file__, module_name, get_type()) + my_module.add_module_depend(['elog', 'etk', 'crypto']) + my_module.add_extra_compile_flags() + my_module.add_src_file([ + 'algue/debug.cpp', + 'algue/base64.cpp', + 'algue/sha1.cpp', + 'algue/md5.cpp' + ]) + my_module.add_header_file([ + 'algue/base64.h', + 'algue/sha1.h', + 'algue/md5.h', + ]) + my_module.add_path(tools.get_current_path(__file__)) + return my_module + +