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