Ticket #390: fix_base64.patch

File fix_base64.patch, 3.9 KB (added by namonai@…, 21 years ago)

Ported base64 code from wget

  • libmpdemux/http.h

    old new  
    77#ifndef __HTTP_H
    88#define __HTTP_H
    99
     10#define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
     11
    1012typedef struct HTTP_field_type {
    1113        char *field_name;
    1214        struct HTTP_field_type *next;
     
    4850
    4951void            http_debug_hdr( HTTP_header_t *http_hdr );
    5052
    51 int             base64_encode(const void *enc, int encLen, char *out, int outMax);
     53void            base64_encode(const void *enc, int encLen, char *out);
    5254#endif // __HTTP_H
  • libmpdemux/http.c

    old new  
    586586int
    587587http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
    588588        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;
    590590        if( http_hdr==NULL || username==NULL ) return -1;
    591591
    592592        if( password!=NULL ) {
     
    600600        }
    601601
    602602        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);
    607606        if( b64_usr_pass==NULL ) {
    608607                mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
    609608                return -1;
    610609        }
    611610
    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);
    617612
    618         b64_usr_pass[out_len]='\0';
    619        
    620613        auth = (char*)malloc(encoded_len+22);
    621614        if( auth==NULL ) {
    622615                mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
     
    657650        mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n");
    658651}
    659652
    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         }
     653void
     654base64_encode (const void *enc, int encLen, char *out) {
    703655
    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';
    706686}
    707687
    708688static int http_streaming_start(stream_t *stream, int* file_format) {