/src/pistache/include/pistache/base64.h
Line | Count | Source |
1 | | /* |
2 | | * SPDX-FileCopyrightText: 2019 Kip Warner |
3 | | * |
4 | | * SPDX-License-Identifier: Apache-2.0 |
5 | | */ |
6 | | |
7 | | // Multiple include protection... |
8 | | #ifndef _BASE_64_H_ |
9 | | #define _BASE_64_H_ |
10 | | |
11 | | // Includes... |
12 | | |
13 | | // Build environment configuration... |
14 | | #include <pistache/config.h> |
15 | | |
16 | | // Standard C++ / POSIX system headers... |
17 | | #include <cstddef> |
18 | | #include <string> |
19 | | #include <vector> |
20 | | |
21 | | #if __cplusplus < 201703L |
22 | | namespace std |
23 | | { |
24 | | typedef uint8_t byte; |
25 | | } |
26 | | #endif |
27 | | |
28 | | // A class for performing decoding to raw bytes from base 64 encoding... |
29 | | class Base64Decoder |
30 | | { |
31 | | // Public methods... |
32 | | public: |
33 | | // Constructor... |
34 | | explicit Base64Decoder(const std::string& Base64EncodedString) |
35 | 1.23k | : m_Base64EncodedString(Base64EncodedString) |
36 | 1.23k | { } |
37 | | |
38 | | // Calculate length of decoded raw bytes from that would be generated if |
39 | | // the base 64 encoded input buffer was decoded. This is not a static |
40 | | // method because we need to examine the string... |
41 | | std::vector<std::byte>::size_type CalculateDecodedSize() const; |
42 | | |
43 | | // Decode base 64 encoding into raw bytes... |
44 | | const std::vector<std::byte>& Decode(); |
45 | | |
46 | | // Get raw decoded data... |
47 | | const std::vector<std::byte>& GetRawDecodedData() const noexcept |
48 | 0 | { |
49 | 0 | return m_DecodedData; |
50 | 0 | } |
51 | | |
52 | | // Protected methods... |
53 | | protected: |
54 | | // Convert an octet character to corresponding sextet, provided it can |
55 | | // safely be represented as such. Otherwise return 0xff... |
56 | | std::byte DecodeCharacter(const unsigned char Character) const; |
57 | | |
58 | | // Protected attributes... |
59 | | protected: |
60 | | // Base 64 encoded string to decode... |
61 | | const std::string& m_Base64EncodedString; |
62 | | |
63 | | // Decoded raw data... |
64 | | std::vector<std::byte> m_DecodedData; |
65 | | }; |
66 | | |
67 | | // A class for performing base 64 encoding from raw bytes... |
68 | | class Base64Encoder |
69 | | { |
70 | | // Public methods... |
71 | | public: |
72 | | // Construct encoder to encode from a raw input buffer... |
73 | | explicit Base64Encoder(const std::vector<std::byte>& InputBuffer) |
74 | 567 | : m_InputBuffer(InputBuffer) |
75 | 567 | { } |
76 | | |
77 | | // Calculate length of base 64 string that would need to be generated |
78 | | // for raw data of a given length... |
79 | | static std::string::size_type CalculateEncodedSize( |
80 | | const std::vector<std::byte>::size_type DecodedSize) noexcept; |
81 | | |
82 | | // Encode raw data input buffer to base 64... |
83 | | const std::string& Encode() noexcept; |
84 | | |
85 | | // Encode a string into base 64 format... |
86 | | static std::string EncodeString(const std::string& StringInput); |
87 | | |
88 | | // Get the encoded data... |
89 | | const std::string& GetBase64EncodedString() const noexcept |
90 | 0 | { |
91 | 0 | return m_Base64EncodedString; |
92 | 0 | } |
93 | | |
94 | | // Protected methods... |
95 | | protected: |
96 | | // Encode single binary byte to 6-bit base 64 character... |
97 | | unsigned char EncodeByte(const std::byte Byte) const; |
98 | | |
99 | | // Protected attributes... |
100 | | protected: |
101 | | // Raw bytes to encode to base 64 string... |
102 | | const std::vector<std::byte>& m_InputBuffer; |
103 | | |
104 | | // Base64 encoded string... |
105 | | std::string m_Base64EncodedString; |
106 | | }; |
107 | | |
108 | | // Multiple include protection... |
109 | | #endif |