Ticket #390: fix_base64.patch
| File fix_base64.patch, 3.9 KB (added by , 21 years ago) |
|---|
-
libmpdemux/http.h
old new 7 7 #ifndef __HTTP_H 8 8 #define __HTTP_H 9 9 10 #define BASE64_LENGTH(len) (4 * (((len) + 2) / 3)) 11 10 12 typedef struct HTTP_field_type { 11 13 char *field_name; 12 14 struct HTTP_field_type *next; … … 48 50 49 51 void http_debug_hdr( HTTP_header_t *http_hdr ); 50 52 51 int base64_encode(const void *enc, int encLen, char *out, int outMax);53 void base64_encode(const void *enc, int encLen, char *out); 52 54 #endif // __HTTP_H -
libmpdemux/http.c
old new 586 586 int 587 587 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { 588 588 char *auth, *usr_pass, *b64_usr_pass; 589 int encoded_len, pass_len=0, out_len;589 int encoded_len, source_len, pass_len=0, out_len; 590 590 if( http_hdr==NULL || username==NULL ) return -1; 591 591 592 592 if( password!=NULL ) { … … 600 600 } 601 601 602 602 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); 603 604 // Base 64 encode with at least 33% more data than the original size 605 encoded_len = strlen(usr_pass)*2; 606 b64_usr_pass = (char*)malloc(encoded_len); 603 source_len = strlen(usr_pass); 604 encoded_len = BASE64_LENGTH(source_len); 605 b64_usr_pass = (char*)malloc(encoded_len + 1); 607 606 if( b64_usr_pass==NULL ) { 608 607 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); 609 608 return -1; 610 609 } 611 610 612 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len); 613 if( out_len<0 ) { 614 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n"); 615 return -1; 616 } 611 base64_encode( usr_pass, source_len, b64_usr_pass); 617 612 618 b64_usr_pass[out_len]='\0';619 620 613 auth = (char*)malloc(encoded_len+22); 621 614 if( auth==NULL ) { 622 615 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); … … 657 650 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n"); 658 651 } 659 652 660 int 661 base64_encode(const void *enc, int encLen, char *out, int outMax) { 662 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; 663 664 unsigned char *encBuf; 665 int outLen; 666 unsigned int bits; 667 unsigned int shift; 668 669 encBuf = (unsigned char*)enc; 670 outLen = 0; 671 bits = 0; 672 shift = 0; 673 674 while( outLen<outMax ) { 675 if( encLen>0 ) { 676 // Shift in byte 677 bits <<= 8; 678 bits |= *encBuf; 679 shift += 8; 680 // Next byte 681 encBuf++; 682 encLen--; 683 } else if( shift>0 ) { 684 // Pad last bits to 6 bits - will end next loop 685 bits <<= 6 - shift; 686 shift = 6; 687 } else { 688 // Terminate with Mime style '=' 689 *out = '='; 690 outLen++; 691 692 return outLen; 693 } 694 695 // Encode 6 bit segments 696 while( shift>=6 ) { 697 shift -= 6; 698 *out = b64[ (bits >> shift) & 0x3F ]; 699 out++; 700 outLen++; 701 } 702 } 653 void 654 base64_encode (const void *enc, int encLen, char *out) { 703 655 704 // Output overflow 705 return -1; 656 /* Conversion table. */ 657 static const char tbl[64] = { 658 'A','B','C','D','E','F','G','H', 659 'I','J','K','L','M','N','O','P', 660 'Q','R','S','T','U','V','W','X', 661 'Y','Z','a','b','c','d','e','f', 662 'g','h','i','j','k','l','m','n', 663 'o','p','q','r','s','t','u','v', 664 'w','x','y','z','0','1','2','3', 665 '4','5','6','7','8','9','+','/' 666 }; 667 int i; 668 unsigned char *p = (unsigned char *)out; 669 const char *s = (const char *)enc; 670 671 /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ 672 for (i = 0; i < encLen; i += 3) { 673 *p++ = tbl[s[0] >> 2]; 674 *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; 675 *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; 676 *p++ = tbl[s[2] & 0x3f]; 677 s += 3; 678 } 679 /* Pad the result if necessary... */ 680 if (i == encLen + 1) 681 *(p - 1) = '='; 682 else if (i == encLen + 2) 683 *(p - 1) = *(p - 2) = '='; 684 /* ...and zero-terminate it. */ 685 *p = '\0'; 706 686 } 707 687 708 688 static int http_streaming_start(stream_t *stream, int* file_format) {
