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 "static_assert.h"
31#include "exception.h"
32#include "error_handler.h"
33#include "type_traits.h"
34#include "enum_type.h"
35#include "integral_limits.h"
36
37#include <stdint.h>
38
39/**************************************************************************************************************************************************************************
40* See https://en.wikipedia.org/wiki/Base64
41*
42* Encoding Encoding characters Separate encoding of lines Decoding non-encoding characters
43* 62nd 63rd Pad Separators Length Checksum
44* RFC 1421 : Base64 for Privacy - Enhanced Mail(deprecated) + / = mandatory CR + LF 64, or lower for the last line No No
45* RFC 2045 : Base64 transfer encoding for MIME + / = mandatory CR + LF At most 76 No Discarded
46* RFC 2152 : Base64 for UTF - 7 + / No No No
47* RFC 3501 : Base64 encoding for IMAP mailbox names + , No No No
48* RFC 4648 : base64(standard)[a] + / = optional No No
49* RFC 4648 : base64url(URL - and filename - safe standard) - _ = optional No No
50**************************************************************************************************************************************************************************/
51
52namespace etl
53{
54 //***************************************************************************
56 //***************************************************************************
66
67 //***************************************************************************
69 //***************************************************************************
71 {
72 public:
73
75 : base64_exception(ETL_ERROR_TEXT("base64:overflow", ETL_BASE64_FILE_ID"A"), file_name_, line_number_)
76 {
77 }
78 };
79
80 //***************************************************************************
82 //***************************************************************************
84 {
85 public:
86
88 : base64_exception(ETL_ERROR_TEXT("base64:invalid data", ETL_BASE64_FILE_ID"B"), file_name_, line_number_)
89 {
90 }
91 };
92
93 //***************************************************************************
95 //***************************************************************************
97 {
98 public:
99
101 : base64_exception(ETL_ERROR_TEXT("base64:invalid decode input length", ETL_BASE64_FILE_ID"C"), file_name_, line_number_)
102 {
103 }
104 };
105
106 //***************************************************************************
108 //***************************************************************************
109 class base64
110 {
111 public:
112
113 struct Encoding
114 {
115 enum enum_type
116 {
117 //RFC_1421, // Not implemented
118 //RFC_2045, // Not implemented
119 RFC_2152,
120 RFC_3501,
121 RFC_4648,
122 RFC_4648_PADDING,
123 RFC_4648_URL,
124 RFC_4648_URL_PADDING,
125 };
126
127 ETL_DECLARE_ENUM_TYPE(Encoding, int)
128 //ETL_ENUM_TYPE(RFC_1421, "RFC_1421") // Not implemented
129 //ETL_ENUM_TYPE(RFC_2045, "RFC_2045") // Not implemented
130 ETL_ENUM_TYPE(RFC_2152, "RFC_2152")
131 ETL_ENUM_TYPE(RFC_3501, "RFC_3501")
132 ETL_ENUM_TYPE(RFC_4648, "RFC_4648")
133 ETL_ENUM_TYPE(RFC_4648_PADDING, "RFC_4648_PADDING")
134 ETL_ENUM_TYPE(RFC_4648_URL, "RFC_4648_URL")
135 ETL_ENUM_TYPE(RFC_4648_URL_PADDING, "RFC_4648_URL_PADDING")
136 ETL_END_ENUM_TYPE
137 };
138
139 struct Padding
140 {
141 enum enum_type
142 {
143 No_Padding = 0,
144 Use_Padding = 1
145 };
146
147 ETL_DECLARE_ENUM_TYPE(Padding, bool)
148 ETL_ENUM_TYPE(No_Padding, "No_Padding")
149 ETL_ENUM_TYPE(Use_Padding, "Use_Padding")
150 ETL_END_ENUM_TYPE
151 };
152
154 {
155 enum enum_type
156 {
157 Ignore = 0,
158 Reject = 1
159 };
160
161 ETL_DECLARE_ENUM_TYPE(Non_Coding_Characters, bool)
162 ETL_ENUM_TYPE(Ignore, "Ignore")
163 ETL_ENUM_TYPE(Reject, "Reject")
164 ETL_END_ENUM_TYPE
165 };
166
167 enum
168 {
169 Invalid_Data = etl::integral_limits<int>::max,
170 Min_Encode_Buffer_Size = 4,
171 Min_Decode_Buffer_Size = 3
172 };
173
174 protected:
175
176 ETL_CONSTEXPR14
177 base64(const char* encoder_table_,
178 bool use_padding_)
179 : encoder_table(encoder_table_)
180 , use_padding(use_padding_)
181 {
182 }
183
184 //*************************************************************************
185 // Character set for RFC-1421, RFC-2045, RFC-2152 and RFC-4648
186 //*************************************************************************
187 static
188 ETL_CONSTEXPR14
189 const char* character_set_1()
190 {
191 return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
192 }
193
194 //*************************************************************************
195 // Character set for RFC-4648-URL
196 //*************************************************************************
197 static
198 ETL_CONSTEXPR14
199 const char* character_set_2()
200 {
201 return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
202 }
203
204 //*************************************************************************
205 // Character set for RFC-3501-URL
206 //*************************************************************************
207 static
208 ETL_CONSTEXPR14
209 const char* character_set_3()
210 {
211 return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
212 }
213
214 const char* encoder_table;
215 const bool use_padding;
216 };
217}
218#endif
Exception base for base64.
Definition base64.h:58
Illegal character exception.
Definition base64.h:84
Invalid decode input length exception.
Definition base64.h:97
buffer overflow exception.
Definition base64.h:71
Common Base64 definitions.
Definition base64.h:110
Definition exception.h:47
Definition integral_limits.h:516
bitset_ext
Definition absolute.h:38
Definition base64.h:114
Definition base64.h:154
Definition base64.h:140
pair holds two objects of arbitrary type
Definition utility.h:164