Base64.hpp 2.1 KB

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