Browse Source

Implement Base64 encode functionality (incomplete)

Patrick-Christopher Mattulat 2 years ago
parent
commit
38c685d6cf

+ 5 - 20
include/ls_std/encoding/base64/Base64.hpp

@@ -31,31 +31,16 @@ namespace ls_std
 
     private:
 
-      const std::vector<char> encodingTable
-      {
-        'A','B','C','D','E','F','G','H',
-        'I','J','K','L','M','N','O','P',
-        'Q','R','S','T','U','V','W','X',
-        'Y','Z','a','b','c','d','e','f',
-        'g','h','i','j','k','l','m','n',
-        'o','p','q','r','s','t','u','v',
-        'w','x','y','z','0','1','2','3',
-        '4','5','6','7','8','9','+','/'
-      };
-
       static std::string _decodeByteQuadruple(const std::string& _quadruple);
-      static uint8_t _detectInitialShiftNumber(size_t size);
+      std::string _encodeByteTriple(const std::string& characterSequence);
+      static uint32_t _extractBitSequence(uint32_t _bitMask, uint32_t _bitStorage);
       static uint32_t _generateBitMask(uint32_t _maskValue, uint8_t _shiftValue);
-      std::string _getEncodingFromBitSequence(uint32_t bitSequence, size_t characterSequenceSize);
-      static uint32_t _getBitSequenceFromCharacterSequence(const std::string &basicString);
-      char _getCharacterFromLookUpTable(uint8_t byteBuffer);
-      std::string _getEncodingFromByteTriple(const std::string& characterSequence);
-      static std::string _getNextByteTriple(const std::string& _sequence, size_t _index);
+      static std::unordered_map<char, uint8_t> _getDecodingMap();
+      static std::vector<char> _getEncodingField();
       static std::string _getNextByteQuadruple(const std::string &_sequence, size_t _index);
-      static uint32_t _extractBitSequence(uint32_t _bitMask, uint32_t _bitStorage);
+      static std::string _getNextByteTriple(const std::string& _sequence, size_t _index);
       static void _mergeBitSequence(uint32_t &_bitStorage, const uint32_t &_bitMask);
       static uint32_t _toBitStorage(const std::string &_quadruple);
-      static std::unordered_map<char, uint8_t> _getDecodingMap();
   };
 }
 

+ 34 - 62
source/ls_std/encoding/base64/Base64.cpp

@@ -12,12 +12,12 @@
 
 std::string ls_std::Base64::encode(const std::string &_sequence)
 {
-  std::string encodedString, byteTriple{};
+  std::string encodedString{};
 
   for(size_t index = 0 ; index < _sequence.size() ; index += 3)
   {
-    byteTriple = ls_std::Base64::_getNextByteTriple(_sequence, index);
-    encodedString += ls_std::Base64::_getEncodingFromByteTriple(byteTriple);
+    std::string byteTriple = ls_std::Base64::_getNextByteTriple(_sequence, index);
+    encodedString += ls_std::Base64::_encodeByteTriple(byteTriple);
   }
 
   return encodedString;
@@ -55,9 +55,11 @@ std::string ls_std::Base64::_decodeByteQuadruple(const std::string& _quadruple)
   return decodedText;
 }
 
-uint8_t ls_std::Base64::_detectInitialShiftNumber(size_t size)
+std::string ls_std::Base64::_encodeByteTriple(const std::string& characterSequence)
 {
-  return size * 8 - 6;
+  std::string encodedText{};
+
+  return encodedText;
 }
 
 uint32_t ls_std::Base64::_extractBitSequence(uint32_t _bitMask, uint32_t _bitStorage)
@@ -80,68 +82,43 @@ uint32_t ls_std::Base64::_generateBitMask(uint32_t _maskValue, uint8_t _shiftVal
   return _maskValue << _shiftValue;
 }
 
-std::string ls_std::Base64::_getEncodingFromBitSequence(uint32_t bitSequence, size_t characterSequenceSize)
+std::unordered_map<char, uint8_t> ls_std::Base64::_getDecodingMap()
 {
-  std::string encodedString{};
-  uint8_t shiftByBits = ls_std::Base64::_detectInitialShiftNumber(characterSequenceSize);
-  uint32_t buffer;
-
-  for (uint8_t index = 0 ; index < 3 ; index++)
+  static std::unordered_map<char, uint8_t> decodingMap =
   {
-    buffer = bitSequence >> shiftByBits;
-    encodedString += this->_getCharacterFromLookUpTable((uint8_t) buffer);
-    shiftByBits -= 6;
-  }
+    {'A', 0}, {'B', 1}, {'C', 2}, {'D', 3}, {'E', 4}, {'F', 5}, {'G', 6}, {'H', 7},
+    {'I', 8}, {'J', 9}, {'K', 10}, {'L', 11}, {'M', 12}, {'N', 13}, {'O', 14}, {'P', 15},
+    {'Q', 16}, {'R', 17}, {'S', 18}, {'T', 19}, {'U', 20}, {'V', 21}, {'W', 22}, {'X', 23},
+    {'Y', 24}, {'Z', 25}, {'a', 26}, {'b', 27}, {'c', 28}, {'d', 29}, {'e', 30}, {'f', 31},
+    {'g', 32}, {'h', 33}, {'i', 34}, {'j', 35}, {'k', 36}, {'l', 37}, {'m', 38}, {'n', 39},
+    {'o', 40}, {'p', 41}, {'q', 42}, {'r', 43}, {'s', 44}, {'t', 45}, {'u', 46}, {'v', 47},
+    {'w', 48}, {'x', 49}, {'y', 50}, {'z', 51}, {'0', 52}, {'1', 53}, {'2', 54}, {'3', 55},
+    {'4', 56}, {'5', 57}, {'6', 58}, {'7', 59}, {'8', 60}, {'9', 61}, {'+', 62}, {'/', 63}
+  };
 
-  return encodedString;
+  return decodingMap;
 }
 
-uint32_t ls_std::Base64::_getBitSequenceFromCharacterSequence(const std::string &basicString)
+std::vector<char> ls_std::Base64::_getEncodingField()
 {
-  uint32_t bits{};
-
-  for (const char &letter : basicString)
+  static std::vector<char> encodingTable
   {
-    bits = bits | (uint8_t) letter;
-    bits = bits << 8;
-  }
-
-  bits = bits >> 8;
-  return bits;
-}
-
-char ls_std::Base64::_getCharacterFromLookUpTable(uint8_t byteBuffer)
-{
-  std::bitset<8> bitSequence{byteBuffer};
-  bitSequence.set(6, false);
-  bitSequence.set(7, false);
-
-  return this->encodingTable[bitSequence.to_ulong()];
+    'A','B','C','D','E','F','G','H',
+    'I','J','K','L','M','N','O','P',
+    'Q','R','S','T','U','V','W','X',
+    'Y','Z','a','b','c','d','e','f',
+    'g','h','i','j','k','l','m','n',
+    'o','p','q','r','s','t','u','v',
+    'w','x','y','z','0','1','2','3',
+    '4','5','6','7','8','9','+','/'
+  };
+
+  return encodingTable;
 }
 
-std::unordered_map<char, uint8_t> ls_std::Base64::_getDecodingMap()
-{
-  static std::unordered_map<char, uint8_t> decodingMap =
-      {
-          {'A', 0}, {'B', 1}, {'C', 2}, {'D', 3}, {'E', 4}, {'F', 5}, {'G', 6}, {'H', 7},
-          {'I', 8}, {'J', 9}, {'K', 10}, {'L', 11}, {'M', 12}, {'N', 13}, {'O', 14}, {'P', 15},
-          {'Q', 16}, {'R', 17}, {'S', 18}, {'T', 19}, {'U', 20}, {'V', 21}, {'W', 22}, {'X', 23},
-          {'Y', 24}, {'Z', 25}, {'a', 26}, {'b', 27}, {'c', 28}, {'d', 29}, {'e', 30}, {'f', 31},
-          {'g', 32}, {'h', 33}, {'i', 34}, {'j', 35}, {'k', 36}, {'l', 37}, {'m', 38}, {'n', 39},
-          {'o', 40}, {'p', 41}, {'q', 42}, {'r', 43}, {'s', 44}, {'t', 45}, {'u', 46}, {'v', 47},
-          {'w', 48}, {'x', 49}, {'y', 50}, {'z', 51}, {'0', 52}, {'1', 53}, {'2', 54}, {'3', 55},
-          {'4', 56}, {'5', 57}, {'6', 58}, {'7', 59}, {'8', 60}, {'9', 61}, {'+', 62}, {'/', 63}
-      };
-
-  return decodingMap;
-}
-
-std::string ls_std::Base64::_getEncodingFromByteTriple(const std::string& characterSequence)
+std::string ls_std::Base64::_getNextByteQuadruple(const std::string &_sequence, size_t _index)
 {
-  uint32_t bitSequence = ls_std::Base64::_getBitSequenceFromCharacterSequence(characterSequence);
-  std::string encodingString = this->_getEncodingFromBitSequence(bitSequence, characterSequence.size());
-
-  return encodingString;
+  return _sequence.substr(_index, 4);
 }
 
 std::string ls_std::Base64::_getNextByteTriple(const std::string &_sequence, size_t _index)
@@ -149,11 +126,6 @@ std::string ls_std::Base64::_getNextByteTriple(const std::string &_sequence, siz
   return _sequence.substr(_index, 3);
 }
 
-std::string ls_std::Base64::_getNextByteQuadruple(const std::string &_sequence, size_t _index)
-{
-  return _sequence.substr(_index, 4);
-}
-
 void ls_std::Base64::_mergeBitSequence(uint32_t &_bitStorage, const uint32_t &_bitMask)
 {
   _bitStorage = _bitStorage | _bitMask;

+ 1 - 2
test/cases/encoding/base64/Base64Test.cpp

@@ -29,9 +29,8 @@ namespace
   TEST_F(Base64Test, encode)
   {
     ls_std::Base64 base64{};
-    std::string sequence = "abc";
 
-    ASSERT_STREQ("YWJj", base64.encode(sequence).c_str());
+    ASSERT_STREQ("YWJj", base64.encode("abc").c_str());
   }
 
   TEST_F(Base64Test, decode)