Embedded Template Library 1.0
Loading...
Searching...
No Matches
base64.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5Embedded Template Library.
6https://github.com/ETLCPP/etl
7https://www.etlcpp.com
8Copyright(c) 2024 John Wellbelove
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files(the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions :
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24******************************************************************************/
25
26#ifndef ETL_BASE64_INCLUDED
27#define ETL_BASE64_INCLUDED
28
29#include "platform.h"
30#include "enum_type.h"
31#include "error_handler.h"
32#include "exception.h"
33#include "integral_limits.h"
34#include "type_traits.h"
35
36/**************************************************************************************************************************************************************************
37 * See https://en.wikipedia.org/wiki/Base64
38 *
39 * Encoding Encoding characters
40 *Separate encoding of lines Decoding non-encoding characters 62nd 63rd Pad
41 *Separators Length Checksum RFC 1421 : Base64 for
42 *Privacy - Enhanced Mail(deprecated) + / = mandatory CR + LF 64,
43 *or lower for the last line No No RFC 2045 : Base64 transfer encoding for MIME
44 *+ / = mandatory CR + LF At most 76 No Discarded RFC 2152 : Base64
45 *for UTF - 7 + / No No No RFC
46 *3501 : Base64 encoding for IMAP mailbox names + , No
47 *No No RFC 4648 : base64(standard)[a] + / = optional No No RFC 4648 :
48 *base64url(URL - and filename - safe standard) - _ = optional No No
49 **************************************************************************************************************************************************************************/
50
51namespace etl
52{
53 //***************************************************************************
55 //***************************************************************************
56 class base64_exception : public etl::exception
57 {
58 public:
59
60 base64_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
61 : exception(reason_, file_name_, line_number_)
62 {
63 }
64 };
65
66 //***************************************************************************
68 //***************************************************************************
69 class base64_overflow : public base64_exception
70 {
71 public:
72
73 base64_overflow(string_type file_name_, numeric_type line_number_)
74 : base64_exception(ETL_ERROR_TEXT("base64:overflow", ETL_BASE64_FILE_ID"A"), file_name_, line_number_)
75 {
76 }
77 };
78
79 //***************************************************************************
81 //***************************************************************************
82 class base64_invalid_data : public base64_exception
83 {
84 public:
85
86 base64_invalid_data(string_type file_name_, numeric_type line_number_)
87 : base64_exception(ETL_ERROR_TEXT("base64:invalid data", ETL_BASE64_FILE_ID"B"), file_name_, line_number_)
88 {
89 }
90 };
91
92 //***************************************************************************
94 //***************************************************************************
95 class base64_invalid_decode_input_length : public base64_exception
96 {
97 public:
98
99 base64_invalid_decode_input_length(string_type file_name_, numeric_type line_number_)
100 : base64_exception(ETL_ERROR_TEXT("base64:invalid decode input length", ETL_BASE64_FILE_ID"C"), file_name_, line_number_)
101 {
102 }
103 };
104
105 //***************************************************************************
107 //***************************************************************************
108 class base64
109 {
110 public:
111
112 struct Encoding
113 {
114 enum enum_type
115 {
116 // RFC_1421, // Not implemented
117 // RFC_2045, // Not implemented
118 RFC_2152,
119 RFC_3501,
120 RFC_4648,
121 RFC_4648_PADDING,
122 RFC_4648_URL,
123 RFC_4648_URL_PADDING,
124 };
125
127 // ETL_ENUM_TYPE(RFC_1421, "RFC_1421") // Not implemented
128 // ETL_ENUM_TYPE(RFC_2045, "RFC_2045") // Not implemented
129 ETL_ENUM_TYPE(RFC_2152, "RFC_2152")
130 ETL_ENUM_TYPE(RFC_3501, "RFC_3501")
131 ETL_ENUM_TYPE(RFC_4648, "RFC_4648")
132 ETL_ENUM_TYPE(RFC_4648_PADDING, "RFC_4648_PADDING")
133 ETL_ENUM_TYPE(RFC_4648_URL, "RFC_4648_URL")
134 ETL_ENUM_TYPE(RFC_4648_URL_PADDING, "RFC_4648_URL_PADDING")
135 ETL_END_ENUM_TYPE
136 };
137
138 struct Padding
139 {
140 enum enum_type
141 {
142 No_Padding = 0,
143 Use_Padding = 1
144 };
145
147 ETL_ENUM_TYPE(No_Padding, "No_Padding")
148 ETL_ENUM_TYPE(Use_Padding, "Use_Padding")
149 ETL_END_ENUM_TYPE
150 };
151
153 {
154 enum enum_type
155 {
156 Ignore = 0,
157 Reject = 1
158 };
159
161 ETL_ENUM_TYPE(Ignore, "Ignore")
162 ETL_ENUM_TYPE(Reject, "Reject")
163 ETL_END_ENUM_TYPE
164 };
165
166 enum
167 {
168 Invalid_Data = etl::integral_limits<int>::max,
169 Min_Encode_Buffer_Size = 4,
170 Min_Decode_Buffer_Size = 3
171 };
172
173 protected:
174
175 ETL_CONSTEXPR14 base64(const char* encoder_table_, bool use_padding_)
176 : encoder_table(encoder_table_)
177 , use_padding(use_padding_)
178 {
179 }
180
181 //*************************************************************************
182 // Character set for RFC-1421, RFC-2045, RFC-2152 and RFC-4648
183 //*************************************************************************
184 static ETL_CONSTEXPR14 const char* character_set_1()
185 {
186 return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
187 }
188
189 //*************************************************************************
190 // Character set for RFC-4648-URL
191 //*************************************************************************
192 static ETL_CONSTEXPR14 const char* character_set_2()
193 {
194 return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
195 }
196
197 //*************************************************************************
198 // Character set for RFC-3501-URL
199 //*************************************************************************
200 static ETL_CONSTEXPR14 const char* character_set_3()
201 {
202 return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
203 }
204
205 const char* encoder_table;
206 const bool use_padding;
207 };
208} // namespace etl
209#endif
Common Base64 definitions.
Definition base64.h:109
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
Definition exception.h:59
Definition integral_limits.h:517
#define ETL_DECLARE_ENUM_TYPE(TypeName, ValueType)
Definition enum_type.h:90
Definition absolute.h:40
Definition base64.h:113
Definition base64.h:153
Definition base64.h:139