Pārlūkot izejas kodu

Add Base64 encode implementation (incomplete)

Patrick-Christopher Mattulat 3 gadi atpakaļ
vecāks
revīzija
602ca3a91c

+ 6 - 3
include/ls_std/encoding/base64/Base64.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-01-03
- * Changed:         2022-01-08
+ * Changed:         2022-01-16
  *
  * */
 
@@ -42,8 +42,11 @@ namespace ls_std
         '4','5','6','7','8','9','+','/'
       };
 
-      static std::bitset<24> _getBitSequenceFromSequence(const std::string &basicString);
-      static std::string _getEncodingFromSubSequence(const std::string& basicString);
+      static uint8_t _detectInitialShiftNumber(size_t size);
+      std::string _getEncodingFromBitSequence(uint32_t bitSequence, size_t characterSequenceSize);
+      static uint32_t _getBitSequenceFromCharacterSequence(const std::string &basicString);
+      char _getCharacterFromLookUpTable(uint8_t byteBuffer, uint8_t shiftByBits);
+      std::string _getEncodingFromCharacterSequence(const std::string& characterSequence);
       static std::string _getNextByteTriple(const std::string& _sequence, size_t index);
   };
 }

+ 44 - 12
source/ls_std/encoding/base64/Base64.cpp

@@ -3,13 +3,12 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2022-01-03
- * Changed:         2022-01-08
+ * Changed:         2022-01-16
  *
  * */
 
 #include <ls_std/encoding/base64/Base64.hpp>
 #include <bitset>
-#include <sstream>
 
 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)
   {
     subSequence = ls_std::Base64::_getNextByteTriple(_sequence, index);
-    encodedString += ls_std::Base64::_getEncodingFromSubSequence(subSequence);
+    encodedString += ls_std::Base64::_getEncodingFromCharacterSequence(subSequence);
   }
 
   return encodedString;
@@ -29,21 +28,54 @@ std::string ls_std::Base64::decode(const std::string &_sequence)
   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{};
-  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;
 }

+ 3 - 0
test/cases/encoding/base64/Base64Test.cpp

@@ -26,6 +26,9 @@ namespace
       {}
   };
 
+  /*
+   * abc -> 01100001 01100010 01100011 -> 6382179
+   * */
   TEST_F(Base64Test, encode)
   {
     ls_std::Base64 base64{};