Changeset 64874cf7fcea79c26dd7303af6bd4d4576a8f425

Show
Ignore:
Timestamp:
08/30/06 16:58:18 (2 years ago)
Author:
Rémi Denis-Courmont <rem@videolan.org>
git-committer:
Rémi Denis-Courmont <rem@videolan.org> 1156949898 +0000
git-parent:

[ac52d273a4a8bf91ed5c6d233a78c3eb96e52f01]

git-author:
Rémi Denis-Courmont <rem@videolan.org> 1156949898 +0000
Message:

Fix base64 encoding and add regression test

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • include/vlc_url.h

    r299ef0c r64874cf  
    218218 * 
    219219 *****************************************************************************/ 
    220 static inline char *vlc_b64_encode( char *src ) 
     220static inline char *vlc_b64_encode( const char *src ) 
    221221{ 
    222222    static const char b64[] = 
     
    233233    while( len > 0 ) 
    234234    { 
    235         /* pops (up to) 3 bytes of input */ 
    236         uint32_t v = *src++ << 24; 
     235        /* pops (up to) 3 bytes of input, push 4 bytes */ 
     236        uint32_t v = *src++ << 24; // 1/3 
     237        *dst++ = b64[v >> 26]; // 1/4 
     238        v = v << 6; 
    237239 
    238240        if( len >= 2 ) 
    239         { 
    240             v |= *src++ << 16; 
    241             if( len >= 3 ) 
    242                 v |= *src++ << 8; 
    243         } 
    244  
    245         /* pushes (up to) 4 bytes of output */ 
    246         while( v ) 
    247         { 
    248             *dst++ = b64[v >> 26]; 
    249             v = v << 6; 
    250         } 
    251  
    252         switch( len ) 
    253         { 
    254             case 1: 
    255                 *dst++ = '='; 
    256                 *dst++ = '='; 
     241            v |= *src++ << 16; // 2/3 
     242        *dst++ = b64[v >> 26]; // 2/4 
     243        v = v << 6; 
     244 
     245        if( len >= 3 ) 
     246            v |= *src++ << 8; // 3/3 
     247        *dst++ = ( len >= 2 ) ? b64[v >> 26] : '='; // 3/4 
     248        v = v << 6; 
     249 
     250        *dst++ = ( len >= 3 ) ? b64[v >> 26] : '='; // 4/4 
     251 
     252        len--; 
     253        if( len > 0 ) 
     254        { 
     255            len--; 
     256            if( len > 0 ) 
    257257                len--; 
    258                 break; 
    259  
    260             case 2: 
    261                 *dst++ = '='; 
    262                 len -= 2; 
    263                 break; 
    264  
    265             default: 
    266                 len -= 3; 
    267258        } 
    268259    } 
  • src/test/url.c

    r2be9be2 r64874cf  
    2626#include <stdlib.h> 
    2727 
    28 void test_decode (const char *in, const char *out) 
     28typedef char * (*conv_t) (const char *); 
     29 
     30static void test (conv_t f, const char *in, const char *out) 
    2931{ 
    3032    char *res; 
    3133 
    3234    printf ("\"%s\" -> \"%s\" ?\n", in, out); 
    33     res = decode_URI_duplicate (in); 
     35    res = f (in); 
    3436    if (res == NULL) 
    3537        exit (1); 
    3638 
    3739    if (strcmp (res, out)) 
     40    { 
     41        printf (" ERROR: got \"%s\"\n", res); 
    3842        exit (2); 
     43    } 
    3944 
    4045    free (res); 
     46} 
     47 
     48static inline void test_decode (const char *in, const char *out) 
     49{ 
     50    test (decode_URI_duplicate, in, out); 
     51} 
     52 
     53static inline void test_b64 (const char *in, const char *out) 
     54{ 
     55    test (vlc_b64_encode, in, out); 
    4156} 
    4257 
     
    6580    test_decode ("%C1%94%C3%a9l%c3%A9vision", "??élévision"); /* overlong */ 
    6681 
     82    /* Base 64 tests */ 
     83    test_b64 ("", ""); 
     84    test_b64 ("d", "ZA=="); 
     85    test_b64 ("ab", "YQG="); 
     86    test_b64 ("abc", "YQGI"); 
     87    test_b64 ("abcd", "YQGIZA=="); 
     88 
    6789    return 0; 
    6890}