Changeset 64874cf7fcea79c26dd7303af6bd4d4576a8f425
- 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
| r299ef0c |
r64874cf |
|
| 218 | 218 | * |
|---|
| 219 | 219 | *****************************************************************************/ |
|---|
| 220 | | static inline char *vlc_b64_encode( char *src ) |
|---|
| | 220 | static inline char *vlc_b64_encode( const char *src ) |
|---|
| 221 | 221 | { |
|---|
| 222 | 222 | static const char b64[] = |
|---|
| … | … | |
| 233 | 233 | while( len > 0 ) |
|---|
| 234 | 234 | { |
|---|
| 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; |
|---|
| 237 | 239 | |
|---|
| 238 | 240 | 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 ) |
|---|
| 257 | 257 | len--; |
|---|
| 258 | | break; |
|---|
| 259 | | |
|---|
| 260 | | case 2: |
|---|
| 261 | | *dst++ = '='; |
|---|
| 262 | | len -= 2; |
|---|
| 263 | | break; |
|---|
| 264 | | |
|---|
| 265 | | default: |
|---|
| 266 | | len -= 3; |
|---|
| 267 | 258 | } |
|---|
| 268 | 259 | } |
|---|
| r2be9be2 |
r64874cf |
|
| 26 | 26 | #include <stdlib.h> |
|---|
| 27 | 27 | |
|---|
| 28 | | void test_decode (const char *in, const char *out) |
|---|
| | 28 | typedef char * (*conv_t) (const char *); |
|---|
| | 29 | |
|---|
| | 30 | static void test (conv_t f, const char *in, const char *out) |
|---|
| 29 | 31 | { |
|---|
| 30 | 32 | char *res; |
|---|
| 31 | 33 | |
|---|
| 32 | 34 | printf ("\"%s\" -> \"%s\" ?\n", in, out); |
|---|
| 33 | | res = decode_URI_duplicate (in); |
|---|
| | 35 | res = f (in); |
|---|
| 34 | 36 | if (res == NULL) |
|---|
| 35 | 37 | exit (1); |
|---|
| 36 | 38 | |
|---|
| 37 | 39 | if (strcmp (res, out)) |
|---|
| | 40 | { |
|---|
| | 41 | printf (" ERROR: got \"%s\"\n", res); |
|---|
| 38 | 42 | exit (2); |
|---|
| | 43 | } |
|---|
| 39 | 44 | |
|---|
| 40 | 45 | free (res); |
|---|
| | 46 | } |
|---|
| | 47 | |
|---|
| | 48 | static inline void test_decode (const char *in, const char *out) |
|---|
| | 49 | { |
|---|
| | 50 | test (decode_URI_duplicate, in, out); |
|---|
| | 51 | } |
|---|
| | 52 | |
|---|
| | 53 | static inline void test_b64 (const char *in, const char *out) |
|---|
| | 54 | { |
|---|
| | 55 | test (vlc_b64_encode, in, out); |
|---|
| 41 | 56 | } |
|---|
| 42 | 57 | |
|---|
| … | … | |
| 65 | 80 | test_decode ("%C1%94%C3%a9l%c3%A9vision", "??élévision"); /* overlong */ |
|---|
| 66 | 81 | |
|---|
| | 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 | |
|---|
| 67 | 89 | return 0; |
|---|
| 68 | 90 | } |
|---|