|
@@ -3,13 +3,12 @@
|
|
* Company: Lynar Studios
|
|
* Company: Lynar Studios
|
|
* E-Mail: webmaster@lynarstudios.com
|
|
* E-Mail: webmaster@lynarstudios.com
|
|
* Created: 2022-01-03
|
|
* Created: 2022-01-03
|
|
- * Changed: 2022-01-08
|
|
|
|
|
|
+ * Changed: 2022-01-16
|
|
*
|
|
*
|
|
* */
|
|
* */
|
|
|
|
|
|
#include <ls_std/encoding/base64/Base64.hpp>
|
|
#include <ls_std/encoding/base64/Base64.hpp>
|
|
#include <bitset>
|
|
#include <bitset>
|
|
-#include <sstream>
|
|
|
|
|
|
|
|
std::string ls_std::Base64::encode(const std::string &_sequence)
|
|
std::string ls_std::Base64::encode(const std::string &_sequence)
|
|
{
|
|
{
|
|
@@ -18,7 +17,7 @@ std::string ls_std::Base64::encode(const std::string &_sequence)
|
|
for(size_t index = 0 ; index < _sequence.size() ; index += 3)
|
|
for(size_t index = 0 ; index < _sequence.size() ; index += 3)
|
|
{
|
|
{
|
|
subSequence = ls_std::Base64::_getNextByteTriple(_sequence, index);
|
|
subSequence = ls_std::Base64::_getNextByteTriple(_sequence, index);
|
|
- encodedString += ls_std::Base64::_getEncodingFromSubSequence(subSequence);
|
|
|
|
|
|
+ encodedString += ls_std::Base64::_getEncodingFromCharacterSequence(subSequence);
|
|
}
|
|
}
|
|
|
|
|
|
return encodedString;
|
|
return encodedString;
|
|
@@ -29,21 +28,54 @@ std::string ls_std::Base64::decode(const std::string &_sequence)
|
|
return "";
|
|
return "";
|
|
}
|
|
}
|
|
|
|
|
|
-std::bitset<24> ls_std::Base64::_getBitSequenceFromSequence(const std::string &basicString)
|
|
|
|
|
|
+uint8_t ls_std::Base64::_detectInitialShiftNumber(size_t size)
|
|
|
|
+{
|
|
|
|
+ return size * 8 - 6;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+std::string ls_std::Base64::_getEncodingFromBitSequence(uint32_t bitSequence, size_t characterSequenceSize)
|
|
|
|
+{
|
|
|
|
+ std::string encodedString{};
|
|
|
|
+ uint8_t shiftByBits = ls_std::Base64::_detectInitialShiftNumber(characterSequenceSize);
|
|
|
|
+ uint32_t buffer;
|
|
|
|
+
|
|
|
|
+ for (uint8_t index = 0 ; index < 3 ; index++)
|
|
|
|
+ {
|
|
|
|
+ buffer = bitSequence >> shiftByBits;
|
|
|
|
+ encodedString += this->_getCharacterFromLookUpTable((uint8_t) buffer, shiftByBits);
|
|
|
|
+ shiftByBits -= 6;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return encodedString;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+uint32_t ls_std::Base64::_getBitSequenceFromCharacterSequence(const std::string &basicString)
|
|
{
|
|
{
|
|
uint32_t bits{};
|
|
uint32_t bits{};
|
|
- std::stringstream stringStream{basicString};
|
|
|
|
- stringStream >> bits;
|
|
|
|
- std::bitset<32> bitSequenceBuffer{bits};
|
|
|
|
- std::bitset<24> bitSequence{};
|
|
|
|
|
|
|
|
- return bitSequence;
|
|
|
|
|
|
+ for (const char &letter : basicString)
|
|
|
|
+ {
|
|
|
|
+ bits = bits | (uint8_t) letter;
|
|
|
|
+ bits = bits << 8;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ bits = bits >> 8;
|
|
|
|
+ return bits;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+char ls_std::Base64::_getCharacterFromLookUpTable(uint8_t byteBuffer, uint8_t shiftByBits)
|
|
|
|
+{
|
|
|
|
+ std::bitset<8> bitSequence{byteBuffer};
|
|
|
|
+ bitSequence.set(6, false);
|
|
|
|
+ bitSequence.set(7, false);
|
|
|
|
+
|
|
|
|
+ return this->table[bitSequence.to_ulong()];
|
|
}
|
|
}
|
|
|
|
|
|
-std::string ls_std::Base64::_getEncodingFromSubSequence(const std::string& basicString)
|
|
|
|
|
|
+std::string ls_std::Base64::_getEncodingFromCharacterSequence(const std::string& characterSequence)
|
|
{
|
|
{
|
|
- std::string encodingString{};
|
|
|
|
- std::bitset<24> bitSequence = ls_std::Base64::_getBitSequenceFromSequence(basicString);
|
|
|
|
|
|
+ uint32_t bitSequence = ls_std::Base64::_getBitSequenceFromCharacterSequence(characterSequence);
|
|
|
|
+ std::string encodingString = this->_getEncodingFromBitSequence(bitSequence, characterSequence.size());
|
|
|
|
|
|
return encodingString;
|
|
return encodingString;
|
|
}
|
|
}
|