Base64.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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-20
  7. *
  8. * */
  9. #ifndef LS_STD_BASE64_HPP
  10. #define LS_STD_BASE64_HPP
  11. #include <ls_std/core/interface/IEncoding.hpp>
  12. #include <bitset>
  13. #include <vector>
  14. #include <unordered_map>
  15. namespace ls
  16. {
  17. namespace std
  18. {
  19. namespace encoding
  20. {
  21. class Base64 : public ls::std::core::interface_type::IEncoding
  22. {
  23. public:
  24. Base64() = default;
  25. ~Base64() = default;
  26. // implementation
  27. ::std::string encode(const ::std::string &_sequence) override;
  28. ::std::string decode(const ::std::string &_sequence) override;
  29. private:
  30. static ::std::string _applyEndingRule(::std::string _encodedString, size_t _sequenceSize);
  31. static ::std::string _decodeByteQuadruple(const ::std::string &_quadruple);
  32. static ::std::string _encodeByteTriple(const ::std::string &_byteTriple);
  33. static uint32_t _extractBitSequence(uint32_t _bitMask, uint32_t _bitStorage);
  34. static uint32_t _generateBitMask(uint32_t _maskValue, uint8_t _shiftValue);
  35. static ::std::unordered_map<char, uint8_t> _getDecodingMap();
  36. static ::std::unordered_map<uint8_t, char> _getEncodingMap();
  37. static ::std::string _getNextByteQuadruple(const ::std::string &_sequence, size_t _index);
  38. static ::std::string _getNextByteTriple(const ::std::string &_sequence, size_t _index);
  39. static void _mergeBitSequence(uint32_t &_bitStorage, const uint32_t &_bitMask);
  40. static uint32_t _toDecodingBitStorage(const ::std::string &_quadruple);
  41. static uint32_t _toEncodingBitStorage(const ::std::string &_triple);
  42. };
  43. }
  44. }
  45. }
  46. #endif