Commit 85410253 authored by jan.koester's avatar jan.koester
Browse files

it works

parent d264c41a
Loading
Loading
Loading
Loading
+113 −28
Changes for src/base64.cpp: 113 added lines, 28 removed lines.
Original line number Diff line number Diff line
@@ -26,17 +26,28 @@
 *******************************************************************************/

#include "base64.h"
#include <array>
#include <algorithm>
#include <cctype>

namespace netplus {
    const std::string basis_64 =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    const std::string basis_64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    static const std::array<unsigned char, 256> B64_REV = [] {
        std::array<unsigned char, 256> t;
        std::fill(t.begin(), t.end(), (unsigned char)-1);

        for (size_t i = 0; i < basis_64.length(); ++i) {
            t[basis_64[i]] =static_cast<signed char>(i);
        }
        return t; // std::array is returned by value, avoiding the stack corruption warning.
    }();
};

netplus::base64::base64() {}

netplus::base64::base64(const base64& data) {
    _data.clear();
    std::copy(data._data.begin(), data._data.end(), std::back_inserter(_data));
}

@@ -45,50 +56,124 @@ void netplus::base64::clear() {
}

netplus::base64& netplus::base64::setEncodedData(const std::vector<char>& encoded_data) {
    _data.clear();
    std::copy(encoded_data.begin(), encoded_data.end(), std::back_inserter(_data));
    return *this;
}

netplus::base64 &netplus::base64::setEncodedData(const std::string& encoded_data){
    _data.clear();
    std::copy(encoded_data.begin(), encoded_data.end(), std::back_inserter(_data));
    return *this;
}

// --- DECODE to vector<char> ---
const netplus::base64& netplus::base64::operator>>(std::vector<char> &src) const {
    unsigned long long val = 0;
    int valb = -8;
    src.clear(); // Ensure the destination string is empty

    // Array to temporarily hold the 4 six-bit index values (0-63)
    unsigned char block[4] = {0};
    int i = 0; // Index for block[4], counts how many valid chars we've collected

    // Iterate over the internal vector<char> data
    for (char c : _data) {
        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));
            valb -= 8;
        if (c == '=') break; // Stop on padding

        // Use the safe reverse lookup table for fast index retrieval
        signed char idx_val = B64_REV[(unsigned char)c];

        if (idx_val == -1 || std::isspace((unsigned char)c)) {
            continue; // Skip invalid or whitespace characters
        }

        // Store the 6-bit index
        block[i++] = (unsigned char)idx_val;

        // Once we have a full block of 4 characters, decode them into 3 bytes
        if (i == 4) {
            // Byte 1: indices[0] (6 bits) + indices[1] (2 highest bits)
            src.push_back((char)((block[0] << 2) | (block[1] >> 4)));

            // Byte 2: indices[1] (4 lowest bits) + indices[2] (4 highest bits)
            src.push_back((char)((block[1] << 4) | (block[2] >> 2)));

            // Byte 3: indices[2] (2 lowest bits) + indices[3] (6 bits)
            src.push_back((char)((block[2] << 6) | block[3]));

            // Reset the index counter for the next block
            i = 0;
        }
    }

    // --- Handle the Final Partial Block (Padding) ---
    // i will be 2 or 3 if padding was present or stream ended partially
    if (i > 0) {
        if (i == 3) {
            // Two full bytes (input ended with '=')
            src.push_back((char)((block[0] << 2) | (block[1] >> 4)));
            src.push_back((char)((block[1] << 4) | (block[2] >> 2)));
        } else if (i == 2) {
            // One full byte (input ended with '==')
            src.push_back((char)((block[0] << 2) | (block[1] >> 4)));
        }
        // If i == 1, the input is corrupt and we ignore it.
    }

    return *this;
}

// --- DECODE to string ---
const netplus::base64& netplus::base64::operator>>(std::string& src) const {
    unsigned long long val = 0;
    int valb = -8;
    src.clear(); // Ensure the destination string is empty

    // Array to temporarily hold the 4 six-bit index values (0-63)
    unsigned char block[4] = {0};
    int i = 0; // Index for block[4], counts how many valid chars we've collected

    // Iterate over the internal vector<char> data
    for (char c : _data) {
        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));
            valb -= 8;
        if (c == '=') break; // Stop on padding

        // Use the safe reverse lookup table for fast index retrieval
        signed char idx_val = B64_REV[(unsigned char)c];

        if (idx_val == -1 || std::isspace((unsigned char)c)) {
            continue; // Skip invalid or whitespace characters
        }

        // Store the 6-bit index
        block[i++] = (unsigned char)idx_val;

        // Once we have a full block of 4 characters, decode them into 3 bytes
        if (i == 4) {
            // Byte 1: indices[0] (6 bits) + indices[1] (2 highest bits)
            src.push_back((char)((block[0] << 2) | (block[1] >> 4)));

            // Byte 2: indices[1] (4 lowest bits) + indices[2] (4 highest bits)
            src.push_back((char)((block[1] << 4) | (block[2] >> 2)));

            // Byte 3: indices[2] (2 lowest bits) + indices[3] (6 bits)
            src.push_back((char)((block[2] << 6) | block[3]));

            // Reset the index counter for the next block
            i = 0;
        }
    }

    // --- Handle the Final Partial Block (Padding) ---
    // i will be 2 or 3 if padding was present or stream ended partially
    if (i > 0) {
        if (i == 3) {
            // Two full bytes (input ended with '=')
            src.push_back((char)((block[0] << 2) | (block[1] >> 4)));
            src.push_back((char)((block[1] << 4) | (block[2] >> 2)));
        } else if (i == 2) {
            // One full byte (input ended with '==')
            src.push_back((char)((block[0] << 2) | (block[1] >> 4)));
        }
        // If i == 1, the input is corrupt and we ignore it.
    }

    return *this;
}

@@ -142,15 +227,15 @@ netplus::base64 &netplus::base64::operator<<(const base64 &data) {
    return *this;
}

const std::vector<char> &netplus::base64::encoded() const {
const std::vector<unsigned char> &netplus::base64::encoded() const {
    return _data;
}

// --- OUTPUT encoded string ---
std::ostream& operator<<(std::ostream& os, const netplus::base64& src) {
    const std::vector<char>& enc = src.encoded();
    const std::vector<unsigned char>& enc = src.encoded();
    if (!enc.empty())
        os.write(enc.data(), static_cast<std::streamsize>(enc.size()));
        os.write(reinterpret_cast<const char*>(enc.data()), static_cast<std::streamsize>(enc.size()));
    return os;
}

@@ -158,7 +243,7 @@ std::ostream& operator<<(std::ostream& os, const netplus::base64& src) {
std::istream& operator>>(std::istream& is, netplus::base64& dest) {
    std::string input;
    if (is >> input) {
        dest << input;
        dest >> input;
    }
    return is;
}
+2 −2
Changes for src/base64.h: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -51,9 +51,9 @@ namespace netplus {
        base64 &operator<<(const std::string &data);
        base64 &operator<<(const base64 &data);

        const std::vector<char> &encoded() const;
        const std::vector<unsigned char> &encoded() const;
    private:
        std::vector<char> _data;
        std::vector<unsigned char> _data;
    };
}

+0 −1
Changes for src/posix/socket.cpp: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -58,7 +58,6 @@ netplus::socket::socket(){
}

netplus::socket::~socket(){
    int zero = 0;
    if(_SocketInfo){
            free(((struct addrinfo*)_SocketInfo)->ai_addr);
            free((void*)_SocketInfo);
+0 −1
Changes for src/posix/udp.cpp: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -117,7 +117,6 @@ netplus::udp::udp(const std::string &addr, int port,int maxconnections,int socko
}

netplus::udp::~udp(){
    int zero = 0;
    if (_Socket!=-1)
        ::close(_Socket);
}
+7 −11
Changes for test/base64.cpp: 7 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -31,17 +31,14 @@

int main(int argc,char *argv[]){
    netplus::base64 enc;
    std::vector<char> dec;

    if(argc<=1){
        std::cin >> enc;
        enc >> dec;

        const std::vector<char> &encdec=enc.encoded();

        std::cout << "Encoded: ";
        std::cout.write(encdec.data(),encdec.size())<< std::endl;
        std::cout << "Decoded: " << enc << std::endl;
        std::cout<< enc << std::endl;
        std::string dec;
        enc >> dec;
        std::cout << "Decoded: " << dec << std::endl;

        return 0;
    }
@@ -50,13 +47,12 @@ int main(int argc,char *argv[]){
        enc.clear();

        enc << argv[i];
        std::string dec;
        enc >> dec;

        const std::vector<char> &encdec=enc.encoded();

        std::cout << "Encoded: ";
        std::cout.write(encdec.data(),encdec.size())<< std::endl;
        std::cout << "Decoded: " << enc << std::endl;
        std::cout << enc << std::endl;
        std::cout << "Decoded: " << dec << std::endl;
    }
    return 0;
}