From 1cd2b4cafa4bb22004b85fc783111803f20e5f2e Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 28 Oct 2022 14:18:18 +0200 Subject: [PATCH] base64: add possibility for the decoding strings to not have padding characters Add Base64::RequirePaddingOnDecodedStrings(bool) method. while here: - add Decode(...) methods taking wide characters as the input string --- src/base64.cpp | 76 +++++++++++++++++-------------------------- src/base64.h | 87 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 112 insertions(+), 51 deletions(-) diff --git a/src/base64.cpp b/src/base64.cpp index 8f7deb0..deec14e 100644 --- a/src/base64.cpp +++ b/src/base64.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2012, Tomasz Sowa + * Copyright (c) 2012-2022, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -50,6 +50,7 @@ Base64::Base64() padding = '='; tabbase64_len = 64; not_existing = 256; // first 8 bits are zero + require_padding_on_decoded_strings = true; } @@ -64,6 +65,14 @@ void Base64::SetPadding(char c) } +void Base64::RequirePaddingOnDecodedStrings(bool require) +{ + this->require_padding_on_decoded_strings = require; +} + + + + void Base64::Convert3to4Pieces(const char * s, size_t len, unsigned int * tab4) { const unsigned char * tab3 = reinterpret_cast(s); @@ -155,20 +164,6 @@ bool Base64::CharFromBase64(unsigned int from, unsigned int & to) -bool Base64::Make4pieces(const char * s, unsigned int * tab4) -{ - for(size_t i=0 ; i<4 ; ++i) - { - if( s[i] == padding ) - tab4[i] = not_existing; - else - if( !CharFromBase64(s[i], tab4[i]) ) - return false; - } - -return true; -} - @@ -185,15 +180,6 @@ void Base64::Save4PiecesToString(std::string & s, const unsigned int * tab4) -void Base64::Save3PiecesToString(std::string & s, const unsigned int * tab3) -{ - for(int i=0 ; i<3 ; ++i) - { - if( tab3[i] != not_existing ) - s += tab3[i]; - } -} - @@ -229,31 +215,9 @@ void Base64::Encode(const std::string & in, std::string & out) - - - bool Base64::Decode(const char * in, size_t len, std::string & out) { -unsigned int tab3[3]; -unsigned int tab4[4]; - - out.clear(); - size_t new_size = len - len/3 + 3; - out.reserve(new_size); - - if( len % 4 != 0 ) - return false; - - for(size_t i=0 ; i