Base64.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2022-01-03
  6. * Changed: 2023-05-15
  7. *
  8. * */
  9. #include <bitset>
  10. #include <ls-std/encoding/Base64.hpp>
  11. using ls::std::encoding::Base64;
  12. using std::string;
  13. using std::string_view;
  14. using std::unordered_map;
  15. using std::vector;
  16. Base64::Base64() = default;
  17. Base64::~Base64() noexcept = default;
  18. string Base64::encode(const string &_sequence)
  19. {
  20. string encodedString{};
  21. for (size_t index = 0; index < _sequence.size(); index += 3)
  22. {
  23. string_view byteTriple = Base64::_getNextByteTriple(_sequence, index);
  24. encodedString += Base64::_encodeByteTriple(byteTriple);
  25. }
  26. return Base64::_applyEndingRule(encodedString, _sequence.size());
  27. }
  28. string Base64::decode(const string &_sequence)
  29. {
  30. string decodedString{};
  31. for (int index{}; index < _sequence.size(); index += 4)
  32. {
  33. string_view quadruple = Base64::_getNextByteQuadruple(string_view{_sequence}, index);
  34. decodedString += Base64::_decodeByteQuadruple(quadruple);
  35. }
  36. return decodedString;
  37. }
  38. string Base64::_applyEndingRule(string _encodedString, size_t _sequenceSize)
  39. {
  40. size_t size = _encodedString.size();
  41. if (_sequenceSize % 3 == 1)
  42. {
  43. _encodedString[size - 2] = '=';
  44. _encodedString[size - 1] = '=';
  45. }
  46. if (_sequenceSize % 3 == 2)
  47. {
  48. _encodedString[size - 1] = '=';
  49. }
  50. return _encodedString;
  51. }
  52. string Base64::_decodeByteQuadruple(string_view _quadruple)
  53. {
  54. string decodedText{};
  55. uint8_t shiftValue = 16;
  56. uint32_t bitStorage = Base64::_toDecodingBitStorage(_quadruple);
  57. for (uint8_t index = 0; index < ((uint8_t) _quadruple.size() - 1); index++)
  58. {
  59. uint32_t bitMask = Base64::_generateBitMask(255, shiftValue);
  60. uint32_t bitSequence = Base64::_extractBitSequence(bitMask, bitStorage);
  61. bitSequence = bitSequence >> shiftValue;
  62. decodedText += (char) bitSequence;
  63. shiftValue -= 8;
  64. }
  65. return decodedText;
  66. }
  67. string Base64::_encodeByteTriple(string_view _byteTriple)
  68. {
  69. string encodedText{};
  70. uint32_t bitStorage = Base64::_toEncodingBitStorage(_byteTriple);
  71. static vector<uint32_t> bitMaskStorage = {16515072, 258048, 4032, 63};
  72. static unordered_map<uint8_t, char> encodingMap = Base64::_getEncodingMap();
  73. uint8_t shiftValue = 18;
  74. for (uint8_t bitMaskIndex = 0; bitMaskIndex < 4; bitMaskIndex++)
  75. {
  76. uint32_t extractedBitSequence = Base64::_extractBitSequence(bitMaskStorage[bitMaskIndex], bitStorage);
  77. extractedBitSequence = extractedBitSequence >> shiftValue;
  78. encodedText += encodingMap[(uint8_t) extractedBitSequence];
  79. shiftValue -= 6;
  80. }
  81. return encodedText;
  82. }
  83. uint32_t Base64::_extractBitSequence(uint32_t _bitMask, uint32_t _bitStorage)
  84. {
  85. return _bitStorage & _bitMask;
  86. }
  87. uint32_t Base64::_generateBitMask(uint32_t _maskValue, uint8_t _shiftValue)
  88. {
  89. uint32_t bitmask = _maskValue << _shiftValue;
  90. if (_shiftValue == 0)
  91. {
  92. bitmask = _maskValue;
  93. }
  94. return bitmask;
  95. }
  96. unordered_map<char, uint8_t> Base64::_getDecodingMap()
  97. {
  98. static 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},
  99. {'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}};
  100. return decodingMap;
  101. }
  102. unordered_map<uint8_t, char> Base64::_getEncodingMap()
  103. {
  104. static unordered_map<uint8_t, char> encodingMap = {
  105. {0, 'A'}, {1, 'B'}, {2, 'C'}, {3, 'D'}, {4, 'E'}, {5, 'F'}, {6, 'G'}, {7, 'H'}, {8, 'I'}, {9, 'J'}, {10, 'K'}, {11, 'L'}, {12, 'M'}, {13, 'N'}, {14, 'O'}, {15, 'P'}, {16, 'Q'}, {17, 'R'}, {18, 'S'}, {19, 'T'}, {20, 'U'}, {21, 'V'}, {22, 'W'}, {23, 'X'}, {24, 'Y'}, {25, 'Z'}, {26, 'a'}, {27, 'b'}, {28, 'c'}, {29, 'd'}, {30, 'e'}, {31, 'f'}, {32, 'g'}, {33, 'h'}, {34, 'i'}, {35, 'j'}, {36, 'k'}, {37, 'l'}, {38, 'm'}, {39, 'n'}, {40, 'o'}, {41, 'p'}, {42, 'q'}, {43, 'r'}, {44, 's'}, {45, 't'}, {46, 'u'}, {47, 'v'}, {48, 'w'}, {49, 'x'}, {50, 'y'}, {51, 'z'}, {52, '0'}, {53, '1'}, {54, '2'}, {55, '3'}, {56, '4'}, {57, '5'}, {58, '6'}, {59, '7'}, {60, '8'}, {61, '9'}, {62, '+'}, {63, '/'},
  106. };
  107. return encodingMap;
  108. }
  109. string_view Base64::_getNextByteQuadruple(string_view _sequence, size_t _index)
  110. {
  111. return _sequence.substr(_index, 4);
  112. }
  113. string_view Base64::_getNextByteTriple(string_view _sequence, size_t _index)
  114. {
  115. return _sequence.substr(_index, 3);
  116. }
  117. void Base64::_mergeBitSequence(uint32_t &_bitStorage, const uint32_t &_bitMask)
  118. {
  119. _bitStorage = _bitStorage | _bitMask;
  120. }
  121. uint32_t Base64::_toDecodingBitStorage(string_view _quadruple)
  122. {
  123. uint32_t bitStorage{};
  124. uint8_t letterCounter = 1;
  125. unordered_map<char, uint8_t> decodingMap = Base64::_getDecodingMap();
  126. for (char letter : _quadruple)
  127. {
  128. uint32_t bitMask = Base64::_generateBitMask(decodingMap[letter], (4 - letterCounter) * 6); // must be hardcoded - even in case of less than 4 characters, so that conversion is correct
  129. Base64::_mergeBitSequence(bitStorage, bitMask);
  130. ++letterCounter;
  131. }
  132. return bitStorage;
  133. }
  134. uint32_t Base64::_toEncodingBitStorage(string_view _triple)
  135. {
  136. uint32_t bitStorage{};
  137. uint8_t shiftValue = 16;
  138. for (char letter : _triple)
  139. {
  140. uint32_t bitMask = Base64::_generateBitMask((uint8_t) letter, shiftValue);
  141. Base64::_mergeBitSequence(bitStorage, bitMask);
  142. shiftValue -= 8;
  143. }
  144. return bitStorage;
  145. }