Base64.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2022-01-03
  6. * Changed: 2023-02-06
  7. *
  8. * */
  9. #ifndef LS_STD_BASE64_HPP
  10. #define LS_STD_BASE64_HPP
  11. #include <bitset>
  12. #include <ls-std/core/interface/IEncoding.hpp>
  13. #include <ls-std/os/dynamic-goal.hpp>
  14. #include <unordered_map>
  15. #include <vector>
  16. namespace ls::std::encoding
  17. {
  18. class LS_STD_DYNAMIC_GOAL Base64 : public ls::std::core::interface_type::IEncoding
  19. {
  20. public:
  21. Base64();
  22. ~Base64() override;
  23. // implementation
  24. [[nodiscard]] ::std::string encode(const ::std::string &_sequence) override;
  25. [[nodiscard]] ::std::string decode(const ::std::string &_sequence) override;
  26. private:
  27. [[nodiscard]] static ::std::string _applyEndingRule(::std::string _encodedString, size_t _sequenceSize);
  28. [[nodiscard]] static ::std::string _decodeByteQuadruple(const ::std::string &_quadruple);
  29. [[nodiscard]] static ::std::string _encodeByteTriple(const ::std::string &_byteTriple);
  30. [[nodiscard]] static uint32_t _extractBitSequence(uint32_t _bitMask, uint32_t _bitStorage);
  31. [[nodiscard]] static uint32_t _generateBitMask(uint32_t _maskValue, uint8_t _shiftValue);
  32. [[nodiscard]] static ::std::unordered_map<char, uint8_t> _getDecodingMap();
  33. [[nodiscard]] static ::std::unordered_map<uint8_t, char> _getEncodingMap();
  34. [[nodiscard]] static ::std::string _getNextByteQuadruple(const ::std::string &_sequence, size_t _index);
  35. [[nodiscard]] static ::std::string _getNextByteTriple(const ::std::string &_sequence, size_t _index);
  36. static void _mergeBitSequence(uint32_t &_bitStorage, const uint32_t &_bitMask);
  37. [[nodiscard]] static uint32_t _toDecodingBitStorage(const ::std::string &_quadruple);
  38. [[nodiscard]] static uint32_t _toEncodingBitStorage(const ::std::string &_triple);
  39. };
  40. }
  41. #endif