Vuo 2.4.4
Loading...
Searching...
No Matches
VuoBase64.cc
Go to the documentation of this file.
1
28#include "VuoBase64.h"
29#include <iostream>
30
31#ifdef VUO_COMPILER
33 "title" : "VuoBase64"
34 });
35#endif
36
40static const std::string base64_chars =
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
42 "abcdefghijklmnopqrstuvwxyz"
43 "0123456789+/";
44
48static inline bool is_base64(unsigned char c) {
49 return (isalnum(c) || (c == '+') || (c == '/'));
50}
51
57char *VuoBase64_encode(long long in_len, char const *bytes_to_encode)
58{
59 std::string ret;
60 int i = 0;
61 int j = 0;
62 unsigned char char_array_3[3];
63 unsigned char char_array_4[4];
64
65 while (in_len--) {
66 char_array_3[i++] = *(bytes_to_encode++);
67 if (i == 3) {
68 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
69 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
70 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
71 char_array_4[3] = char_array_3[2] & 0x3f;
72
73 for(i = 0; (i <4) ; i++)
74 ret += base64_chars[char_array_4[i]];
75 i = 0;
76 }
77 }
78
79 if (i)
80 {
81 for(j = i; j < 3; j++)
82 char_array_3[j] = '\0';
83
84 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
85 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
86 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
87 char_array_4[3] = char_array_3[2] & 0x3f;
88
89 for (j = 0; (j < i + 1); j++)
90 ret += base64_chars[char_array_4[j]];
91
92 while((i++ < 3))
93 ret += '=';
94
95 }
96
97 return strdup(ret.c_str());
98}
99
105char *VuoBase64_decode(char const *encoded_string, long long *outputLength)
106{
107 int in_len = strlen(encoded_string);
108 int i = 0;
109 int j = 0;
110 int in_ = 0;
111 unsigned char char_array_4[4], char_array_3[3];
112 std::string ret;
113
114 while (in_len-- && ( encoded_string[in_] != '=')) {
115 if (encoded_string[in_] == '\\' || encoded_string[in_] == '\n')
116 {
117 in_++;
118 continue;
119 }
120
121 if (!is_base64(encoded_string[in_]))
122 break;
123
124 char_array_4[i++] = encoded_string[in_]; in_++;
125 if (i ==4) {
126 for (i = 0; i <4; i++)
127 char_array_4[i] = base64_chars.find(char_array_4[i]);
128
129 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
130 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
131 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
132
133 for (i = 0; (i < 3); i++)
134 ret += char_array_3[i];
135 i = 0;
136 }
137 }
138
139 if (i) {
140 for (j = i; j <4; j++)
141 char_array_4[j] = 0;
142
143 for (j = 0; j <4; j++)
144 char_array_4[j] = base64_chars.find(char_array_4[j]);
145
146 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
147 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
148 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
149
150 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
151 }
152
153 *outputLength = ret.length();
154 char *data = (char *)malloc(*outputLength);
155 memcpy(data, ret.data(), *outputLength);
156 return data;
157}