replace base64encode in src/futils.cpp

main
clanmills 5 years ago
parent 55bdaafebf
commit c7bcfcfbf8

@ -213,50 +213,69 @@ namespace Exiv2 {
return 1; /* indicate success */ return 1; /* indicate success */
} // base64encode } // base64encode
long base64decode(const char *in, char *out, size_t out_size) { // https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c
static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW" static const std::string base64_chars =
"$$$$$$XYZ[\\]^_`abcdefghijklmnopq"; "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
long len; "abcdefghijklmnopqrstuvwxyz"
long i; "0123456789+/";
long done = 0;
unsigned char v;
unsigned char quad[4]; static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
while (*in) { }
len = 0;
for (i = 0; i < 4 && *in; i++) { static std::string base64_decode(std::string const& encoded_string) {
v = 0; int in_len = encoded_string.size();
while (*in && !v) { int i = 0;
v = *in++; int j = 0;
v = (v < 43 || v > 122) ? 0 : decode[v - 43]; int in_ = 0;
if (v) unsigned char char_array_4[4], char_array_3[3];
v = (v == '$') ? 0 : v - 61; std::string ret;
if (*in) {
len++; while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
if (v) char_array_4[i++] = encoded_string[in_]; in_++;
quad[i] = v - 1; if (i ==4) {
} else for (i = 0; i <4; i++)
quad[i] = 0; char_array_4[i] = base64_chars.find(char_array_4[i]);
}
} char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
if (!len) char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
continue; char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
if (out_size < (size_t) (done + len - 1))
/* out buffer is too small */ for (i = 0; (i < 3); i++)
return -1; ret += char_array_3[i];
if (len >= 2) i = 0;
*out++ = quad[0] << 2 | quad[1] >> 4; }
if (len >= 3) }
*out++ = quad[1] << 4 | quad[2] >> 2;
if (len >= 4) if (i) {
*out++ = ((quad[2] << 6) & 0xc0) | quad[3]; for (j = i; j <4; j++)
done += len - 1; char_array_4[j] = 0;
}
if ((size_t)(done + 1) >= out_size) for (j = 0; j <4; j++)
return -1; char_array_4[j] = base64_chars.find(char_array_4[j]);
*out++ = '\0';
return done; char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
} // base64decode char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
}
return ret;
}
long base64decode(const char *in, char *out, size_t out_size)
{
std::string in_string(in);
std::string out_string = base64_decode(in_string);
long result = (long) out_string.size();
if ( (long) out_size > result ) {
memcpy(out,out_string.c_str(),result);
out[result] = 0;
} else result = -1 ;
return result;
}
Protocol fileProtocol(const std::string& path) { Protocol fileProtocol(const std::string& path) {
Protocol result = pFile ; Protocol result = pFile ;

Loading…
Cancel
Save