diff --git a/ejson/base64.cpp b/ejson/base64.cpp new file mode 100644 index 0000000..0a25388 --- /dev/null +++ b/ejson/base64.cpp @@ -0,0 +1,110 @@ +/** @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 ejson::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 ejson { + namespace base64 { + std::string encode(const uint8_t* _data, int32_t _len); + std::vector decode(const std::string& _data); + } +} diff --git a/lutin_ejson.py b/lutin_ejson.py index 447e472..def3654 100644 --- a/lutin_ejson.py +++ b/lutin_ejson.py @@ -40,6 +40,7 @@ def create(target, module_name): 'ejson/String.cpp', 'ejson/Object.cpp', 'ejson/Value.cpp', + 'ejson/base64.cpp', 'ejson/internal/Document.cpp', 'ejson/internal/Array.cpp', 'ejson/internal/Boolean.cpp', @@ -62,6 +63,7 @@ def create(target, module_name): 'ejson/Object.h', 'ejson/Value.h', 'ejson/iterator.h', + 'ejson/base64.h', 'ejson/details/iterator.hxx', 'ejson/internal/Document.h', 'ejson/internal/Array.h',