Rename a pipe method, add docs
[carla.git] / source / utils / CarlaBase64Utils.hpp
blobafb3657401e3c535441349212b09aff5439f8b18
1 /*
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"
24 #include <cctype>
25 #include <vector>
27 // -----------------------------------------------------------------------
28 // Helpers
30 namespace CarlaBase64Helpers {
32 static const char* const kBase64Chars =
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34 "abcdefghijklmnopqrstuvwxyz"
35 "0123456789+/";
37 static inline
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)
45 return i;
48 carla_stderr2("findBase64CharIndex('%c') - failed", c);
49 return 0;
52 static inline
53 bool isBase64Char(const char c)
55 return (std::isalnum(c) || (c == '+') || (c == '/'));
58 } // namespace CarlaBase64Helpers
60 // -----------------------------------------------------------------------
62 static inline
63 void carla_getChunkFromBase64String_impl(std::vector<uint8_t>& vector, const char* const base64string)
65 CARLA_SAFE_ASSERT_RETURN(base64string != nullptr,);
67 uint i=0, j=0;
68 uint charArray3[3], charArray4[4];
70 vector.clear();
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 == '=')
78 break;
79 if (c == ' ' || c == '\n')
80 continue;
82 CARLA_SAFE_ASSERT_CONTINUE(CarlaBase64Helpers::isBase64Char(c));
84 charArray4[i++] = static_cast<uint>(c);
86 if (i == 4)
88 for (i=0; i<4; ++i)
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];
95 for (i=0; i<3; ++i)
96 vector.push_back(static_cast<uint8_t>(charArray3[i]));
98 i = 0;
102 if (i != 0)
104 for (j=0; j<i && j<4; ++j)
105 charArray4[j] = CarlaBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[j]));
107 for (j=i; j<4; ++j)
108 charArray4[j] = 0;
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]));
120 static inline
121 std::vector<uint8_t> carla_getChunkFromBase64String(const char* const base64string)
123 std::vector<uint8_t> ret;
124 carla_getChunkFromBase64String_impl(ret, base64string);
125 return ret;
128 // -----------------------------------------------------------------------
130 #endif // CARLA_BASE64_UTILS_HPP_INCLUDED