Loading src/base64.cpp +44 −115 Changes for src/base64.cpp: 44 added lines, 115 removed lines. Original line number Diff line number Diff line /******************************************************************************* * Copyright (c) 2025, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <algorithm> #include "base64.h" #include <algorithm> #include <cctype> namespace netplus { static const std::string basis_64 = const std::string base64::basis_64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; }; netplus::base64::base64(){ } netplus::base64::base64(const base64& data){ using namespace netplus; base64::base64() {} base64::base64(const base64& data) { std::copy(data._data.begin(), data._data.end(), std::back_inserter(_data)); } void netplus::base64::clear(){ void base64::clear() { _data.clear(); } const netplus::base64 &netplus::base64::operator>>(std::vector<char> &src) const{ unsigned long long val=0, valb=-8; // --- DECODE to vector<char> --- const base64 &base64::operator>>(std::vector<char> &src) const { unsigned long long val = 0; int valb = -8; for (char c : _data) { if (basis_64.find(c) == std::string::npos) break; val = (val << 6) + basis_64.find(c); if (c == '=') break; if (std::isspace(static_cast<unsigned char>(c))) continue; size_t idx = basis_64.find(c); if (idx == std::string::npos) continue; val = (val << 6) + idx; valb += 6; if (valb >= 0) { src.emplace_back(char((val >> valb) & 0xFF)); Loading @@ -62,12 +37,16 @@ const netplus::base64 &netplus::base64::operator>>(std::vector<char> &src) const return *this; } const netplus::base64 &netplus::base64::operator>>(std::string &src) const{ unsigned long long val=0, valb=-8; // --- DECODE to string --- const base64 &base64::operator>>(std::string &src) const { unsigned long long val = 0; int valb = -8; for (char c : _data) { if (basis_64.find(c) == std::string::npos) break; val = (val << 6) + basis_64.find(c); if (c == '=') break; if (std::isspace(static_cast<unsigned char>(c))) continue; size_t idx = basis_64.find(c); if (idx == std::string::npos) continue; val = (val << 6) + idx; valb += 6; if (valb >= 0) { src.push_back(char((val >> valb) & 0xFF)); Loading @@ -77,123 +56,73 @@ const netplus::base64 &netplus::base64::operator>>(std::string &src) const{ return *this; } netplus::base64 &netplus::base64::operator << (const std::vector<char> &data){ // --- ENCODE from vector<char> --- base64 &base64::operator<<(const std::vector<char> &data) { size_t missing = 0; size_t ret_size = data.size(); std::vector<char> ret; while ((ret_size % 3) != 0) { while ((ret_size % 3) != 0) { ++ret_size; ++missing; } ret_size = 4 * ret_size / 3; ret.reserve(ret_size); for (unsigned int i=0; i<ret_size/4; ++i) { for (unsigned int i = 0; i < ret_size / 4; ++i) { size_t index = i * 3; char b3[3]; b3[0] = (index + 0 < data.size()) ? data[index + 0] : 0; b3[1] = (index + 1 < data.size()) ? data[index + 1] : 0; b3[2] = (index + 2 < data.size()) ? data[index + 2] : 0; // Transform into four base 64 characters char b4[4]; b4[0] = ((b3[0] & 0xfc) >> 2); b4[1] = ((b3[0] & 0x03) << 4) + ((b3[1] & 0xf0) >> 4); b4[2] = ((b3[1] & 0x0f) << 2) + ((b3[2] & 0xc0) >> 6); b4[3] = ((b3[2] & 0x3f) << 0); // Add the base 64 characters to the return value ret.emplace_back(basis_64[b4[0]]); ret.emplace_back(basis_64[b4[1]]); ret.emplace_back(basis_64[b4[2]]); ret.emplace_back(basis_64[b4[3]]); } // Replace data that is invalid (always as many as there are missing bytes) for (size_t i = 0; i < missing; ++i) ret[ret_size - i - 1] = '='; std::copy(ret.begin(), ret.end(), std::back_inserter(_data)); return *this; } netplus::base64 &netplus::base64::operator << (const std::string &data){ size_t missing = 0; size_t ret_size = data.size(); std::vector<char> ret; while ((ret_size % 3) != 0) { ++ret_size; ++missing; // --- ENCODE from string --- base64 &base64::operator<<(const std::string &data) { std::vector<char> vec(data.begin(), data.end()); return (*this << vec); } ret_size = 4*ret_size/3; ret.reserve(ret_size); for (unsigned int i=0; i<ret_size/4; ++i) { size_t index = i*3; char b3[3]; b3[0] = (index+0 < data.size()) ? data[index+0] : 0; b3[1] = (index+1 < data.size()) ? data[index+1] : 0; b3[2] = (index+2 < data.size()) ? data[index+2] : 0; // Transform into four base 64 characters char b4[4]; b4[0] = ((b3[0] & 0xfc) >> 2); b4[1] = ((b3[0] & 0x03) << 4) + ((b3[1] & 0xf0) >> 4); b4[2] = ((b3[1] & 0x0f) << 2) + ((b3[2] & 0xc0) >> 6); b4[3] = ((b3[2] & 0x3f) << 0); // Add the base 64 characters to the return value ret.emplace_back(basis_64[b4[0]]); ret.emplace_back(basis_64[b4[1]]); ret.emplace_back(basis_64[b4[2]]); ret.emplace_back(basis_64[b4[3]]); } // Replace data that is invalid (always as many as there are missing bytes) for (size_t i=0; i<missing; ++i) ret[ret_size - i - 1] = '='; std::copy(ret.begin(),ret.end(),std::back_inserter(_data)); return *this; } netplus::base64 &netplus::base64::operator<<(const base64 &data){ base64 &base64::operator<<(const base64 &data) { std::copy(data._data.begin(), data._data.end(), std::back_inserter(_data)); return *this; } const std::vector<char> &netplus::base64::encoded() const{ const std::vector<char> &base64::encoded() const { return _data; } // --- OUTPUT encoded string --- std::ostream& operator<<(std::ostream& os, const netplus::base64& src) { std::vector<char> buf; src >> buf; for(auto &c : buf){ os << c; } const std::vector<char>& enc = src.encoded(); if (!enc.empty()) os.write(enc.data(), static_cast<std::streamsize>(enc.size())); return os; } // --- INPUT: read plain text and encode it --- std::istream& operator>>(std::istream& is, netplus::base64& dest) { std::string c; if(is >> c){ dest << c; std::string input; if (is >> input) { dest << input; } return is; } src/base64.h +7 −3 Changes for src/base64.h: 7 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -32,11 +32,15 @@ #pragma once namespace netplus { class base64 { public: base64(); base64(const base64& data); void clear(); // encode / decode operators const base64 &operator>>(std::vector<char> &src) const; const base64 &operator>>(std::string &src) const; Loading @@ -44,13 +48,13 @@ namespace netplus { base64 &operator<<(const std::string &data); base64 &operator<<(const base64 &data); void clear(); const std::vector<char> &encoded() const; private: std::vector<char> _data; static const std::string basis_64; }; }; } std::ostream& operator<<(std::ostream& os, const netplus::base64& src); std::istream& operator>>(std::istream& is, netplus::base64& dest); Loading
src/base64.cpp +44 −115 Changes for src/base64.cpp: 44 added lines, 115 removed lines. Original line number Diff line number Diff line /******************************************************************************* * Copyright (c) 2025, Jan Koester jan.koester@gmx.net * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the <organization> nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <algorithm> #include "base64.h" #include <algorithm> #include <cctype> namespace netplus { static const std::string basis_64 = const std::string base64::basis_64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; }; netplus::base64::base64(){ } netplus::base64::base64(const base64& data){ using namespace netplus; base64::base64() {} base64::base64(const base64& data) { std::copy(data._data.begin(), data._data.end(), std::back_inserter(_data)); } void netplus::base64::clear(){ void base64::clear() { _data.clear(); } const netplus::base64 &netplus::base64::operator>>(std::vector<char> &src) const{ unsigned long long val=0, valb=-8; // --- DECODE to vector<char> --- const base64 &base64::operator>>(std::vector<char> &src) const { unsigned long long val = 0; int valb = -8; for (char c : _data) { if (basis_64.find(c) == std::string::npos) break; val = (val << 6) + basis_64.find(c); if (c == '=') break; if (std::isspace(static_cast<unsigned char>(c))) continue; size_t idx = basis_64.find(c); if (idx == std::string::npos) continue; val = (val << 6) + idx; valb += 6; if (valb >= 0) { src.emplace_back(char((val >> valb) & 0xFF)); Loading @@ -62,12 +37,16 @@ const netplus::base64 &netplus::base64::operator>>(std::vector<char> &src) const return *this; } const netplus::base64 &netplus::base64::operator>>(std::string &src) const{ unsigned long long val=0, valb=-8; // --- DECODE to string --- const base64 &base64::operator>>(std::string &src) const { unsigned long long val = 0; int valb = -8; for (char c : _data) { if (basis_64.find(c) == std::string::npos) break; val = (val << 6) + basis_64.find(c); if (c == '=') break; if (std::isspace(static_cast<unsigned char>(c))) continue; size_t idx = basis_64.find(c); if (idx == std::string::npos) continue; val = (val << 6) + idx; valb += 6; if (valb >= 0) { src.push_back(char((val >> valb) & 0xFF)); Loading @@ -77,123 +56,73 @@ const netplus::base64 &netplus::base64::operator>>(std::string &src) const{ return *this; } netplus::base64 &netplus::base64::operator << (const std::vector<char> &data){ // --- ENCODE from vector<char> --- base64 &base64::operator<<(const std::vector<char> &data) { size_t missing = 0; size_t ret_size = data.size(); std::vector<char> ret; while ((ret_size % 3) != 0) { while ((ret_size % 3) != 0) { ++ret_size; ++missing; } ret_size = 4 * ret_size / 3; ret.reserve(ret_size); for (unsigned int i=0; i<ret_size/4; ++i) { for (unsigned int i = 0; i < ret_size / 4; ++i) { size_t index = i * 3; char b3[3]; b3[0] = (index + 0 < data.size()) ? data[index + 0] : 0; b3[1] = (index + 1 < data.size()) ? data[index + 1] : 0; b3[2] = (index + 2 < data.size()) ? data[index + 2] : 0; // Transform into four base 64 characters char b4[4]; b4[0] = ((b3[0] & 0xfc) >> 2); b4[1] = ((b3[0] & 0x03) << 4) + ((b3[1] & 0xf0) >> 4); b4[2] = ((b3[1] & 0x0f) << 2) + ((b3[2] & 0xc0) >> 6); b4[3] = ((b3[2] & 0x3f) << 0); // Add the base 64 characters to the return value ret.emplace_back(basis_64[b4[0]]); ret.emplace_back(basis_64[b4[1]]); ret.emplace_back(basis_64[b4[2]]); ret.emplace_back(basis_64[b4[3]]); } // Replace data that is invalid (always as many as there are missing bytes) for (size_t i = 0; i < missing; ++i) ret[ret_size - i - 1] = '='; std::copy(ret.begin(), ret.end(), std::back_inserter(_data)); return *this; } netplus::base64 &netplus::base64::operator << (const std::string &data){ size_t missing = 0; size_t ret_size = data.size(); std::vector<char> ret; while ((ret_size % 3) != 0) { ++ret_size; ++missing; // --- ENCODE from string --- base64 &base64::operator<<(const std::string &data) { std::vector<char> vec(data.begin(), data.end()); return (*this << vec); } ret_size = 4*ret_size/3; ret.reserve(ret_size); for (unsigned int i=0; i<ret_size/4; ++i) { size_t index = i*3; char b3[3]; b3[0] = (index+0 < data.size()) ? data[index+0] : 0; b3[1] = (index+1 < data.size()) ? data[index+1] : 0; b3[2] = (index+2 < data.size()) ? data[index+2] : 0; // Transform into four base 64 characters char b4[4]; b4[0] = ((b3[0] & 0xfc) >> 2); b4[1] = ((b3[0] & 0x03) << 4) + ((b3[1] & 0xf0) >> 4); b4[2] = ((b3[1] & 0x0f) << 2) + ((b3[2] & 0xc0) >> 6); b4[3] = ((b3[2] & 0x3f) << 0); // Add the base 64 characters to the return value ret.emplace_back(basis_64[b4[0]]); ret.emplace_back(basis_64[b4[1]]); ret.emplace_back(basis_64[b4[2]]); ret.emplace_back(basis_64[b4[3]]); } // Replace data that is invalid (always as many as there are missing bytes) for (size_t i=0; i<missing; ++i) ret[ret_size - i - 1] = '='; std::copy(ret.begin(),ret.end(),std::back_inserter(_data)); return *this; } netplus::base64 &netplus::base64::operator<<(const base64 &data){ base64 &base64::operator<<(const base64 &data) { std::copy(data._data.begin(), data._data.end(), std::back_inserter(_data)); return *this; } const std::vector<char> &netplus::base64::encoded() const{ const std::vector<char> &base64::encoded() const { return _data; } // --- OUTPUT encoded string --- std::ostream& operator<<(std::ostream& os, const netplus::base64& src) { std::vector<char> buf; src >> buf; for(auto &c : buf){ os << c; } const std::vector<char>& enc = src.encoded(); if (!enc.empty()) os.write(enc.data(), static_cast<std::streamsize>(enc.size())); return os; } // --- INPUT: read plain text and encode it --- std::istream& operator>>(std::istream& is, netplus::base64& dest) { std::string c; if(is >> c){ dest << c; std::string input; if (is >> input) { dest << input; } return is; }
src/base64.h +7 −3 Changes for src/base64.h: 7 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -32,11 +32,15 @@ #pragma once namespace netplus { class base64 { public: base64(); base64(const base64& data); void clear(); // encode / decode operators const base64 &operator>>(std::vector<char> &src) const; const base64 &operator>>(std::string &src) const; Loading @@ -44,13 +48,13 @@ namespace netplus { base64 &operator<<(const std::string &data); base64 &operator<<(const base64 &data); void clear(); const std::vector<char> &encoded() const; private: std::vector<char> _data; static const std::string basis_64; }; }; } std::ostream& operator<<(std::ostream& os, const netplus::base64& src); std::istream& operator>>(std::istream& is, netplus::base64& dest);