| | 386 | /* Base64 decoding */ |
|---|
| | 387 | size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src ) |
|---|
| | 388 | { |
|---|
| | 389 | static const int b64[256] = { |
|---|
| | 390 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */ |
|---|
| | 391 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */ |
|---|
| | 392 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */ |
|---|
| | 393 | 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */ |
|---|
| | 394 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */ |
|---|
| | 395 | 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */ |
|---|
| | 396 | -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */ |
|---|
| | 397 | 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */ |
|---|
| | 398 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */ |
|---|
| | 399 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */ |
|---|
| | 400 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */ |
|---|
| | 401 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */ |
|---|
| | 402 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */ |
|---|
| | 403 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */ |
|---|
| | 404 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */ |
|---|
| | 405 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */ |
|---|
| | 406 | }; |
|---|
| | 407 | uint8_t *p_start = p_dst; |
|---|
| | 408 | uint8_t *p = (uint8_t *)p_src; |
|---|
| | 409 | |
|---|
| | 410 | int i_level; |
|---|
| | 411 | int i_last; |
|---|
| | 412 | |
|---|
| | 413 | for( i_level = 0, i_last = 0; i_dst > 0 && *p != '\0'; i_dst--, p++ ) |
|---|
| | 414 | { |
|---|
| | 415 | const int c = b64[(unsigned int)*p]; |
|---|
| | 416 | if( c == -1 ) |
|---|
| | 417 | continue; |
|---|
| | 418 | |
|---|
| | 419 | switch( i_level ) |
|---|
| | 420 | { |
|---|
| | 421 | case 0: |
|---|
| | 422 | i_level++; |
|---|
| | 423 | break; |
|---|
| | 424 | case 1: |
|---|
| | 425 | *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 ); |
|---|
| | 426 | i_level++; |
|---|
| | 427 | break; |
|---|
| | 428 | case 2: |
|---|
| | 429 | *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f ); |
|---|
| | 430 | i_level++; |
|---|
| | 431 | break; |
|---|
| | 432 | case 3: |
|---|
| | 433 | *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c; |
|---|
| | 434 | i_level = 0; |
|---|
| | 435 | } |
|---|
| | 436 | i_last = c; |
|---|
| | 437 | } |
|---|
| | 438 | |
|---|
| | 439 | return p_dst - p_start; |
|---|
| | 440 | } |
|---|
| | 441 | size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src ) |
|---|
| | 442 | { |
|---|
| | 443 | const int i_src = strlen( psz_src ); |
|---|
| | 444 | uint8_t *p_dst; |
|---|
| | 445 | |
|---|
| | 446 | *pp_dst = p_dst = malloc( i_src ); |
|---|
| | 447 | if( !p_dst ) |
|---|
| | 448 | return 0; |
|---|
| | 449 | return vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src ); |
|---|
| | 450 | } |
|---|
| | 451 | char *vlc_b64_decode( const char *psz_src ) |
|---|
| | 452 | { |
|---|
| | 453 | const int i_src = strlen( psz_src ); |
|---|
| | 454 | char *p_dst = malloc( i_src + 1 ); |
|---|
| | 455 | size_t i_dst; |
|---|
| | 456 | if( !p_dst ) |
|---|
| | 457 | return NULL; |
|---|
| | 458 | |
|---|
| | 459 | i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src ); |
|---|
| | 460 | p_dst[i_dst] = '\0'; |
|---|
| | 461 | |
|---|
| | 462 | return p_dst; |
|---|
| | 463 | } |
|---|
| | 464 | |
|---|