2 * Carla base64 utils, based on http://www.adp-gmbh.ch/cpp/common/base64.html
3 * Copyright (C) 2004-2008 René Nyffenegger
4 * Copyright (C) 2014 Filipe Coelho <falktx@falktx.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
19 #ifndef CARLA_BASE64_UTILS_HPP_INCLUDED
20 #define CARLA_BASE64_UTILS_HPP_INCLUDED
22 #include "CarlaUtils.hpp"
27 // -----------------------------------------------------------------------
30 namespace CarlaBase64Helpers
{
32 static const char* const kBase64Chars
=
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34 "abcdefghijklmnopqrstuvwxyz"
38 uint8_t findBase64CharIndex(const char c
)
40 static const uint8_t kBase64CharsLen(static_cast<uint8_t>(std::strlen(kBase64Chars
)));
42 for (uint8_t i
=0; i
<kBase64CharsLen
; ++i
)
44 if (kBase64Chars
[i
] == c
)
48 carla_stderr2("findBase64CharIndex('%c') - failed", c
);
53 bool isBase64Char(const char c
)
55 return (std::isalnum(c
) || (c
== '+') || (c
== '/'));
58 } // namespace CarlaBase64Helpers
60 // -----------------------------------------------------------------------
63 void carla_getChunkFromBase64String_impl(std::vector
<uint8_t>& vector
, const char* const base64string
)
65 CARLA_SAFE_ASSERT_RETURN(base64string
!= nullptr,);
68 uint charArray3
[3], charArray4
[4];
71 vector
.reserve(std::strlen(base64string
)*3/4 + 4);
73 for (std::size_t l
=0, len
=std::strlen(base64string
); l
<len
; ++l
)
75 const char c
= base64string
[l
];
77 if (c
== '\0' || c
== '=')
79 if (c
== ' ' || c
== '\n')
82 CARLA_SAFE_ASSERT_CONTINUE(CarlaBase64Helpers::isBase64Char(c
));
84 charArray4
[i
++] = static_cast<uint
>(c
);
89 charArray4
[i
] = CarlaBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4
[i
]));
91 charArray3
[0] = (charArray4
[0] << 2) + ((charArray4
[1] & 0x30) >> 4);
92 charArray3
[1] = ((charArray4
[1] & 0xf) << 4) + ((charArray4
[2] & 0x3c) >> 2);
93 charArray3
[2] = ((charArray4
[2] & 0x3) << 6) + charArray4
[3];
96 vector
.push_back(static_cast<uint8_t>(charArray3
[i
]));
104 for (j
=0; j
<i
&& j
<4; ++j
)
105 charArray4
[j
] = CarlaBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4
[j
]));
110 charArray3
[0] = (charArray4
[0] << 2) + ((charArray4
[1] & 0x30) >> 4);
111 charArray3
[1] = ((charArray4
[1] & 0xf) << 4) + ((charArray4
[2] & 0x3c) >> 2);
112 charArray3
[2] = ((charArray4
[2] & 0x3) << 6) + charArray4
[3];
114 for (j
=0; i
>0 && j
<i
-1; j
++)
115 vector
.push_back(static_cast<uint8_t>(charArray3
[j
]));
121 std::vector
<uint8_t> carla_getChunkFromBase64String(const char* const base64string
)
123 std::vector
<uint8_t> ret
;
124 carla_getChunkFromBase64String_impl(ret
, base64string
);
128 // -----------------------------------------------------------------------
130 #endif // CARLA_BASE64_UTILS_HPP_INCLUDED