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