DISTRHO Plugin Framework
Base64.hpp
1 /*
2  * DISTRHO Plugin Framework (DPF)
3  * Copyright (C) 2012-2015 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 DISTRHO_BASE64_HPP_INCLUDED
18 #define DISTRHO_BASE64_HPP_INCLUDED
19 
20 #include "../DistrhoUtils.hpp"
21 
22 #include <cctype>
23 #include <vector>
24 
25 // -----------------------------------------------------------------------
26 // base64 stuff, based on http://www.adp-gmbh.ch/cpp/common/base64.html
27 // Copyright (C) 2004-2008 RenĂ© Nyffenegger
28 
29 // -----------------------------------------------------------------------
30 // Helpers
31 
32 #ifndef DOXYGEN
33 namespace DistrhoBase64Helpers {
34 
35 static const char* const kBase64Chars =
36  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
37  "abcdefghijklmnopqrstuvwxyz"
38  "0123456789+/";
39 
40 static inline
41 uint8_t findBase64CharIndex(const char c)
42 {
43  static const uint8_t kBase64CharsLen(static_cast<uint8_t>(std::strlen(kBase64Chars)));
44 
45  for (uint8_t i=0; i<kBase64CharsLen; ++i)
46  {
47  if (kBase64Chars[i] == c)
48  return i;
49  }
50 
51  d_stderr2("findBase64CharIndex('%c') - failed", c);
52  return 0;
53 }
54 
55 static inline
56 bool isBase64Char(const char c)
57 {
58  return (std::isalnum(c) || (c == '+') || (c == '/'));
59 }
60 
61 } // namespace DistrhoBase64Helpers
62 #endif
63 
64 // -----------------------------------------------------------------------
65 
66 static inline
67 std::vector<uint8_t> d_getChunkFromBase64String(const char* const base64string)
68 {
69  DISTRHO_SAFE_ASSERT_RETURN(base64string != nullptr, std::vector<uint8_t>());
70 
71  uint i=0, j=0;
72  uint charArray3[3], charArray4[4];
73 
74  std::vector<uint8_t> ret;
75  ret.reserve(std::strlen(base64string)*3/4 + 4);
76 
77  for (std::size_t l=0, len=std::strlen(base64string); l<len; ++l)
78  {
79  const char c = base64string[l];
80 
81  if (c == '\0' || c == '=')
82  break;
83  if (c == ' ' || c == '\n')
84  continue;
85 
86  DISTRHO_SAFE_ASSERT_CONTINUE(CarlaBase64Helpers::isBase64Char(c));
87 
88  charArray4[i++] = static_cast<uint>(c);
89 
90  if (i == 4)
91  {
92  for (i=0; i<4; ++i)
93  charArray4[i] = DistrhoBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[i]));
94 
95  charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
96  charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
97  charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
98 
99  for (i=0; i<3; ++i)
100  ret.push_back(static_cast<uint8_t>(charArray3[i]));
101 
102  i = 0;
103  }
104  }
105 
106  if (i != 0)
107  {
108  for (j=0; j<i && j<4; ++j)
109  charArray4[j] = DistrhoBase64Helpers::findBase64CharIndex(static_cast<char>(charArray4[j]));
110 
111  for (j=i; j<4; ++j)
112  charArray4[j] = 0;
113 
114  charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
115  charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
116  charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
117 
118  for (j=0; i>0 && j<i-1; j++)
119  ret.push_back(static_cast<uint8_t>(charArray3[j]));
120  }
121 
122  return ret;
123 }
124 
125 // -----------------------------------------------------------------------
126 
127 #endif // DISTRHO_BASE64_HPP_INCLUDED