Base64.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2022-01-03
  6. * Changed: 2022-02-27
  7. *
  8. * */
  9. #ifndef LS_STD_BASE64_HPP
  10. #define LS_STD_BASE64_HPP
  11. #include <ls_std/encoding/IEncoding.hpp>
  12. #include <bitset>
  13. #include <vector>
  14. #include <unordered_map>
  15. namespace ls_std
  16. {
  17. class Base64 : public ls_std::IEncoding
  18. {
  19. public:
  20. Base64() = default;
  21. ~Base64() = default;
  22. // implementation
  23. std::string encode(const std::string &_sequence) override;
  24. std::string decode(const std::string &_sequence) override;
  25. private:
  26. const std::vector<char> encodingTable
  27. {
  28. 'A','B','C','D','E','F','G','H',
  29. 'I','J','K','L','M','N','O','P',
  30. 'Q','R','S','T','U','V','W','X',
  31. 'Y','Z','a','b','c','d','e','f',
  32. 'g','h','i','j','k','l','m','n',
  33. 'o','p','q','r','s','t','u','v',
  34. 'w','x','y','z','0','1','2','3',
  35. '4','5','6','7','8','9','+','/'
  36. };
  37. static std::string _decodeByteQuadruple(const std::string& _quadruple);
  38. static uint8_t _detectInitialShiftNumber(size_t size);
  39. static uint32_t _generateBitMask(uint32_t _maskValue, uint8_t _shiftValue);
  40. std::string _getEncodingFromBitSequence(uint32_t bitSequence, size_t characterSequenceSize);
  41. static uint32_t _getBitSequenceFromCharacterSequence(const std::string &basicString);
  42. char _getCharacterFromLookUpTable(uint8_t byteBuffer);
  43. std::string _getEncodingFromByteTriple(const std::string& characterSequence);
  44. static std::string _getNextByteTriple(const std::string& _sequence, size_t _index);
  45. static std::string _getNextByteQuadruple(const std::string &_sequence, size_t _index);
  46. static uint32_t _extractBitSequence(uint32_t _bitMask, uint32_t _bitStorage);
  47. static void _mergeBitSequence(uint32_t &_bitStorage, const uint32_t &_bitMask);
  48. static uint32_t _toBitStorage(const std::string &_quadruple);
  49. static std::unordered_map<char, uint8_t> _getDecodingMap();
  50. };
  51. }
  52. #endif