Commit 64f01804 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont
Browse files

*Really* fix base 64 encoding

parent f8343052
...@@ -222,6 +222,7 @@ static inline char *vlc_b64_encode( const char *src ) ...@@ -222,6 +222,7 @@ static inline char *vlc_b64_encode( const char *src )
static const char b64[] = static const char b64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t len = strlen( src ); size_t len = strlen( src );
const uint8_t *in = (const uint8_t *)src;
char *ret; char *ret;
char *dst = (char *)malloc( ( len + 4 ) * 4 / 3 ); char *dst = (char *)malloc( ( len + 4 ) * 4 / 3 );
...@@ -233,17 +234,17 @@ static inline char *vlc_b64_encode( const char *src ) ...@@ -233,17 +234,17 @@ static inline char *vlc_b64_encode( const char *src )
while( len > 0 ) while( len > 0 )
{ {
/* pops (up to) 3 bytes of input, push 4 bytes */ /* pops (up to) 3 bytes of input, push 4 bytes */
uint32_t v = *src++ << 24; // 1/3 uint32_t v = *in++ << 24; // 1/3
*dst++ = b64[v >> 26]; // 1/4 *dst++ = b64[v >> 26]; // 1/4
v = v << 6; v = v << 6;
if( len >= 2 ) if( len >= 2 )
v |= *src++ << 16; // 2/3 v |= *in++ << 22; // 2/3
*dst++ = b64[v >> 26]; // 2/4 *dst++ = b64[v >> 26]; // 2/4
v = v << 6; v = v << 6;
if( len >= 3 ) if( len >= 3 )
v |= *src++ << 8; // 3/3 v |= *in++ << 20; // 3/3
*dst++ = ( len >= 2 ) ? b64[v >> 26] : '='; // 3/4 *dst++ = ( len >= 2 ) ? b64[v >> 26] : '='; // 3/4
v = v << 6; v = v << 6;
......
...@@ -82,9 +82,9 @@ int main (void) ...@@ -82,9 +82,9 @@ int main (void)
/* Base 64 tests */ /* Base 64 tests */
test_b64 ("", ""); test_b64 ("", "");
test_b64 ("d", "ZA=="); test_b64 ("d", "ZA==");
test_b64 ("ab", "YQG="); test_b64 ("ab", "YWI=");
test_b64 ("abc", "YQGI"); test_b64 ("abc", "YWJj");
test_b64 ("abcd", "YQGIZA=="); test_b64 ("abcd", "YWJjZA==");
return 0; return 0;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment