Commit 907f220a authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Rewrite base64 encoder

(it was not padding properly the end of encoded stream)
parent 695cb26a
...@@ -306,38 +306,52 @@ static inline int vlc_UrlIsNotEncoded( const char *psz_url ) ...@@ -306,38 +306,52 @@ static inline int vlc_UrlIsNotEncoded( const char *psz_url )
static inline char *vlc_b64_encode( char *src ) static inline char *vlc_b64_encode( char *src )
{ {
static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t len = strlen( src );
char *dst = (char *)malloc( ( len + 4 ) * 4 / 3 );
if( dst == NULL )
return NULL;
char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
char *ret = dst; char *ret = dst;
unsigned i_bits = 0;
unsigned i_shift = 0;
for( ;; ) while( len > 0 )
{ {
if( *src ) /* pops (up to) 3 bytes of input */
uint32_t v = *src++ << 24;
if( len >= 2 )
{ {
i_bits = ( i_bits << 8 )|( *src++ ); v |= *src++ << 16;
i_shift += 8; if( len >= 3 )
v |= *src++ << 8;
} }
else if( i_shift > 0 )
/* pushes (up to) 4 bytes of output */
while( v )
{ {
i_bits <<= 6 - i_shift; *dst++ = b64[v >> 26];
i_shift = 6; v = v << 6;
} }
else
switch( len )
{ {
case 1:
*dst++ = '='; *dst++ = '=';
*dst++ = '=';
len--;
break; break;
}
while( i_shift >= 6 ) case 2:
{ *dst++ = '=';
i_shift -= 6; len -= 2;
*dst++ = b64[(i_bits >> i_shift)&0x3f]; break;
default:
len -= 3;
} }
} }
*dst++ = '\0'; *dst = '\0';
return ret; return ret;
} }
......
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