Loading src/base64.cpp +189 Changes for src/base64.cpp: 189 added lines, 0 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" namespace netplus { static const std::string basis_64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; }; netplus::base64::base64(){ } netplus::base64::base64(const base64& data){ std::copy(data._encoded.begin(),data._encoded.end(),std::back_inserter(_encoded)); std::copy(data._decoded.begin(),data._decoded.end(),std::back_inserter(_decoded)); } void netplus::base64::clear(){ _encoded.clear(); _decoded.clear(); } netplus::base64 & netplus::base64::operator>>(const std::vector<char> &data){ int val=0, valb=-8; for (char c : data) { if (basis_64.find(c) == std::string::npos) break; val = (val << 6) + basis_64.find(c); valb += 6; if (valb >= 0) { _decoded.emplace_back(char((val>>valb)&0xFF)); valb -= 8; } } return *this; } netplus::base64 &netplus::base64::operator>>(const std::string &data){ int val=0, valb=-8; for (char c : data) { if (basis_64.find(c) == std::string::npos) break; val = (val << 6) + basis_64.find(c); valb += 6; if (valb >= 0) { _decoded.emplace_back(char((val>>valb)&0xFF)); valb -= 8; } } return *this; } netplus::base64 &netplus::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) { ++ret_size; ++missing; } 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(_encoded)); 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; } 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(_encoded)); return *this; } netplus::base64 &netplus::base64::operator<<(const base64 &data){ std::copy(data._encoded.begin(),data._encoded.end(),std::back_inserter(_encoded)); std::copy(data._decoded.begin(),data._decoded.end(),std::back_inserter(_decoded)); return *this; } const std::vector<char> &netplus::base64::encoded() const{ return _encoded; } const std::vector<char> &netplus::base64::decoded() const{ return _decoded; } Loading
src/base64.cpp +189 Changes for src/base64.cpp: 189 added lines, 0 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" namespace netplus { static const std::string basis_64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; }; netplus::base64::base64(){ } netplus::base64::base64(const base64& data){ std::copy(data._encoded.begin(),data._encoded.end(),std::back_inserter(_encoded)); std::copy(data._decoded.begin(),data._decoded.end(),std::back_inserter(_decoded)); } void netplus::base64::clear(){ _encoded.clear(); _decoded.clear(); } netplus::base64 & netplus::base64::operator>>(const std::vector<char> &data){ int val=0, valb=-8; for (char c : data) { if (basis_64.find(c) == std::string::npos) break; val = (val << 6) + basis_64.find(c); valb += 6; if (valb >= 0) { _decoded.emplace_back(char((val>>valb)&0xFF)); valb -= 8; } } return *this; } netplus::base64 &netplus::base64::operator>>(const std::string &data){ int val=0, valb=-8; for (char c : data) { if (basis_64.find(c) == std::string::npos) break; val = (val << 6) + basis_64.find(c); valb += 6; if (valb >= 0) { _decoded.emplace_back(char((val>>valb)&0xFF)); valb -= 8; } } return *this; } netplus::base64 &netplus::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) { ++ret_size; ++missing; } 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(_encoded)); 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; } 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(_encoded)); return *this; } netplus::base64 &netplus::base64::operator<<(const base64 &data){ std::copy(data._encoded.begin(),data._encoded.end(),std::back_inserter(_encoded)); std::copy(data._decoded.begin(),data._decoded.end(),std::back_inserter(_decoded)); return *this; } const std::vector<char> &netplus::base64::encoded() const{ return _encoded; } const std::vector<char> &netplus::base64::decoded() const{ return _decoded; }