Dusk Audio Framework
Loading...
Searching...
No Matches
Base64.hpp
1/*
2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2024 Filipe Coelho <falktx@falktx.com>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any purpose with
6 * or without fee is hereby granted, provided that the above copyright notice and this
7 * permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef DAF_BASE64_HPP_INCLUDED
18#define DAF_BASE64_HPP_INCLUDED
19
20#include "../DafUtils.hpp"
21
22#include <vector>
23
24// --------------------------------------------------------------------------------------------------------------------
25// base64 stuff, based on http://www.adp-gmbh.ch/cpp/common/base64.html
26
27/*
28 Copyright (C) 2004-2008 René Nyffenegger
29
30 This source code is provided 'as-is', without any express or implied
31 warranty. In no event will the author be held liable for any damages
32 arising from the use of this software.
33
34 Permission is granted to anyone to use this software for any purpose,
35 including commercial applications, and to alter it and redistribute it
36 freely, subject to the following restrictions:
37
38 1. The origin of this source code must not be misrepresented; you must not
39 claim that you wrote the original source code. If you use this source code
40 in a product, an acknowledgment in the product documentation would be
41 appreciated but is not required.
42
43 2. Altered source versions must be plainly marked as such, and must not be
44 misrepresented as being the original source code.
45
46 3. This notice may not be removed or altered from any source distribution.
47
48 René Nyffenegger rene.nyffenegger@adp-gmbh.ch
49*/
50
51// --------------------------------------------------------------------------------------------------------------------
52// Helpers
53
54#ifndef DOXYGEN
55namespace DafBase64Helpers {
56
57static constexpr const char* const kBase64Chars =
58 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
59 "abcdefghijklmnopqrstuvwxyz"
60 "0123456789+/";
61
62static inline
63uint8_t findBase64CharIndex(const char c)
64{
65 static const uint8_t kBase64CharsLen = static_cast<uint8_t>(std::strlen(kBase64Chars));
66
67 for (uint8_t i=0; i<kBase64CharsLen; ++i)
68 {
69 if (kBase64Chars[i] == c)
70 return i;
71 }
72
73 d_stderr2("findBase64CharIndex('%c') - failed", c);
74 return 0;
75}
76
77static constexpr inline
78bool isBase64Char(const char c)
79{
80 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '+' || c == '/';
81}
82
83} // namespace DafBase64Helpers
84#endif
85
86// --------------------------------------------------------------------------------------------------------------------
87
88static inline
89void d_getChunkFromBase64String_impl(std::vector<uint8_t>& vector, const char* const base64string)
90{
91 vector.clear();
92 DAF_SAFE_ASSERT_RETURN(base64string != nullptr,);
93
94 uint i=0, j=0;
95 uint charArray3[3], charArray4[4];
96
97 vector.reserve(std::strlen(base64string)*3/4 + 4);
98
99 for (std::size_t l=0, len=std::strlen(base64string); l<len; ++l)
100 {
101 const char c = base64string[l];
102
103 if (c == '\0' || c == '=')
104 break;
105 if (c == ' ' || c == '\n')
106 continue;
107
108 DAF_SAFE_ASSERT_CONTINUE(DafBase64Helpers::isBase64Char(c));
109
110 charArray4[i++] = static_cast<uint>(c);
111
112 if (i == 4)
113 {
114 for (i=0; i<4; ++i)
115 charArray4[i] = DafBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[i]));
116
117 charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
118 charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
119 charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
120
121 for (i=0; i<3; ++i)
122 vector.push_back(static_cast<uint8_t>(charArray3[i]));
123
124 i = 0;
125 }
126 }
127
128 if (i != 0)
129 {
130 for (j=0; j<i && j<4; ++j)
131 charArray4[j] = DafBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[j]));
132
133 for (j=i; j<4; ++j)
134 charArray4[j] = 0;
135
136 charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
137 charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
138 charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
139
140 for (j=0; i>0 && j<i-1; j++)
141 vector.push_back(static_cast<uint8_t>(charArray3[j]));
142 }
143}
144
145static inline
146std::vector<uint8_t> d_getChunkFromBase64String(const char* const base64string)
147{
148 std::vector<uint8_t> ret;
149 d_getChunkFromBase64String_impl(ret, base64string);
150 return ret;
151}
152
153// --------------------------------------------------------------------------------------------------------------------
154
155#endif // DAF_BASE64_HPP_INCLUDED