Base64.cpp 4.5 KB

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