Changeset c1993b6231c7b60f79fbac60215c9d1e3dd2edd9

Show
Ignore:
Timestamp:
25/02/07 12:39:42 (2 years ago)
Author:
Laurent Aimar <fenrir@videolan.org>
git-committer:
Laurent Aimar <fenrir@videolan.org> 1172403582 +0000
git-parent:

[e732cff5c58c4ef4df7f7869405f7002aed94036]

git-author:
Laurent Aimar <fenrir@videolan.org> 1172403582 +0000
Message:

Added vlc_b64_encode_binary (include/vlc_strings.h is not that a good place).
Use vlc_b64_encode_binary and remove ap_* in rtp.c
Fixed 2 invalid memory access and 1 stack corruption in rtp.c (Fixed
the report of rtp segfault with gcc 4.1 by xxcv)

Files:

Legend:

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

    rdfb78a8 rc1993b6  
    3939VLC_EXPORT( char *, convert_xml_special_chars, ( const char *psz_content ) ); 
    4040 
     41VLC_EXPORT( char *, vlc_b64_encode_binary, ( const uint8_t *, size_t ) ); 
    4142VLC_EXPORT( char *, vlc_b64_encode, ( const char * ) ); 
    4243 
  • modules/stream_out/rtp.c

    r719ff97 rc1993b6  
    437437    { 
    438438        sout_access_out_t *p_grab; 
    439         char *psz_rtpmap, url[NI_MAXHOST + 8], access[17], psz_ttl[5], ipv; 
     439        const char *psz_rtpmap; 
     440        char url[NI_MAXHOST + 8], access[17], psz_ttl[5], ipv; 
    440441 
    441442        if( b_rtsp ) 
     
    937938    } 
    938939    s[2*i_data] = '\0'; 
    939 } 
    940  
    941 static const char basis_64[] = 
    942     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 
    943  
    944 int ap_base64encode_len(int len) 
    945 { 
    946     return ((len + 2) / 3 * 4) + 1; 
    947 } 
    948  
    949 int ap_base64encode_binary(char *encoded, 
    950                                        const unsigned char *string, int len) 
    951 { 
    952     int i; 
    953     char *p; 
    954  
    955     p = encoded; 
    956     for (i = 0; i < len - 2; i += 3) { 
    957         *p++ = basis_64[(string[i] >> 2) & 0x3F]; 
    958         *p++ = basis_64[((string[i] & 0x3) << 4) | 
    959                         ((int) (string[i + 1] & 0xF0) >> 4)]; 
    960         *p++ = basis_64[((string[i + 1] & 0xF) << 2) | 
    961                         ((int) (string[i + 2] & 0xC0) >> 6)]; 
    962         *p++ = basis_64[string[i + 2] & 0x3F]; 
    963     } 
    964     if (i < len) { 
    965         *p++ = basis_64[(string[i] >> 2) & 0x3F]; 
    966         if (i == (len - 1)) { 
    967             *p++ = basis_64[((string[i] & 0x3) << 4)]; 
    968             *p++ = '='; 
    969         } 
    970         else { 
    971             *p++ = basis_64[((string[i] & 0x3) << 4) | 
    972                             ((int) (string[i + 1] & 0xF0) >> 4)]; 
    973             *p++ = basis_64[((string[i + 1] & 0xF) << 2)]; 
    974         } 
    975         *p++ = '='; 
    976     } 
    977  
    978     *p++ = '\0'; 
    979     return p - encoded; 
    980 } 
    981  
    982 int ap_base64encode(char *encoded, const char *string, int len) 
    983 { 
    984     return ap_base64encode_binary(encoded, (const unsigned char *) string, len); 
    985 } 
    986  
    987 char *b64_encode(char *buf, int len) 
    988 { 
    989     int elen; 
    990     char *out; 
    991  
    992     if(len == 0) 
    993         len = strlen(buf); 
    994  
    995     elen = ap_base64encode_len(len); 
    996     out = (char *) malloc(sizeof(char) * (elen + 1)); 
    997  
    998     ap_base64encode(out, buf, len); 
    999  
    1000     return out; 
    1001940} 
    1002941 
     
    11561095            id->psz_rtpmap = strdup( "H264/90000" ); 
    11571096            id->pf_packetize = rtp_packetize_h264; 
     1097            id->psz_fmtp = NULL; 
     1098 
    11581099            if( p_fmt->i_extra > 0 ) 
    11591100            { 
     
    11621103                char    *p_64_sps = NULL; 
    11631104                char    *p_64_pps = NULL; 
    1164                 char    hexa[6]; 
     1105                char    hexa[6+1]; 
    11651106 
    11661107                while( i_buffer > 4 &&  
    1167                     p_buffer[0] == 0 && p_buffer[1] == 0 && 
    1168                     p_buffer[2] == 0 && p_buffer[3] == 1 ) 
     1108                       p_buffer[0] == 0 && p_buffer[1] == 0 && 
     1109                       p_buffer[2] == 0 && p_buffer[3] == 1 ) 
    11691110                { 
    11701111                    const int i_nal_type = p_buffer[4]&0x1f; 
    1171                     int i_offset    = 1
     1112                    int i_offset
    11721113                    int i_size      = 0; 
    1173                     int i_startcode = 0; 
    1174                     int i_encoded   = 0; 
    11751114 
    11761115                    msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type ); 
    11771116 
    1178                     for( i_offset = 1; i_offset+3 < i_buffer ; i_offset++) 
     1117                    i_size = i_buffer; 
     1118                    for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++) 
    11791119                    { 
    11801120                        if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 ) 
    11811121                        { 
    11821122                            /* we found another startcode */ 
    1183                             i_startcode = i_offset; 
     1123                            i_size = i_offset; 
    11841124                            break; 
    11851125                        }  
    11861126                    } 
    1187                     i_size = i_startcode ? i_startcode : i_buffer; 
    11881127                    if( i_nal_type == 7 ) 
    11891128                    { 
    1190                         p_64_sps = (char *)malloc( ap_base64encode_len( i_size - 4) ); 
    1191                         i_encoded = ap_base64encode_binary( p_64_sps, &p_buffer[4], i_size - 4 ); 
    1192                         p_64_sps[i_encoded] = '\0'; 
     1129                        p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 ); 
    11931130                        sprintf_hexa( hexa, &p_buffer[5], 3 ); 
    1194                         hexa[6] = '\0';  
    11951131                    } 
    1196                     if( i_nal_type == 8 ) 
     1132                    else if( i_nal_type == 8 ) 
    11971133                    { 
    1198                         p_64_pps = (char *)malloc( ap_base64encode_len( i_size - 4) ); 
    1199                         i_encoded = ap_base64encode_binary( p_64_pps, &p_buffer[4], i_size - 4 ); 
    1200                         p_64_pps[i_encoded] = '\0'; 
     1134                        p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 ); 
    12011135                    } 
    12021136                    i_buffer -= i_size; 
    12031137                    p_buffer += i_size; 
    12041138                } 
    1205                 /* FIXME: All this is a bit unsafe */ 
    1206                 asprintf( &id->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps ); 
    1207                 free( p_64_sps ); 
    1208                 free( p_64_pps ); 
     1139                /* */ 
     1140                if( p_64_sps && p_64_pps ) 
     1141                    asprintf( &id->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps ); 
     1142                if( p_64_sps ) 
     1143                    free( p_64_sps ); 
     1144                if( p_64_pps ) 
     1145                    free( p_64_pps ); 
    12091146            } 
    1210             else 
     1147            if( !id->psz_fmtp ) 
    12111148                id->psz_fmtp = strdup( "packetization-mode=1" ); 
    1212 if( p_fmt->i_extra > 0 ) 
    1213 msg_Dbg( p_stream, "WE HAVE %d bytes extra data", p_fmt->i_extra ); 
    12141149            break; 
    12151150 
  • src/text/strings.c

    rdfb78a8 rc1993b6  
    330330 
    331331/* Base64 encoding */ 
    332 char *vlc_b64_encode( const char *src ) 
     332char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src ) 
    333333{ 
    334334    static const char b64[] = 
    335335           "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 
    336     size_t len = strlen( src ); 
    337     const uint8_t *in = (const uint8_t *)src; 
    338  
    339     char *ret; 
    340     char *dst = (char *)malloc( ( len + 4 ) * 4 / 3 ); 
     336 
     337    char *ret = malloc( ( i_src + 4 ) * 4 / 3 ); 
     338    char *dst = ret; 
     339 
    341340    if( dst == NULL ) 
    342341        return NULL; 
    343342 
    344     ret = dst; 
    345  
    346     while( len > 0 ) 
     343    while( i_src > 0 ) 
    347344    { 
    348345        /* pops (up to) 3 bytes of input, push 4 bytes */ 
    349         uint32_t v = *in++ << 24; // 1/3 
    350         *dst++ = b64[v >> 26]; // 1/4 
     346        uint32_t v; 
     347 
     348        /* 1/3 -> 1/4 */ 
     349        v = *src++ << 24; 
     350        *dst++ = b64[v >> 26]; 
    351351        v = v << 6; 
    352352 
    353         if( len >= 2 ) 
    354             v |= *in++ << 22; // 2/3 
    355         *dst++ = b64[v >> 26]; // 2/4 
     353        /* 2/3 -> 2/4 */ 
     354        if( i_src >= 2 ) 
     355            v |= *src++ << 22; 
     356        *dst++ = b64[v >> 26]; 
    356357        v = v << 6; 
    357358 
    358         if( len >= 3 ) 
    359             v |= *in++ << 20; // 3/3 
    360         *dst++ = ( len >= 2 ) ? b64[v >> 26] : '='; // 3/4 
     359        /* 3/3 -> 3/4 */ 
     360        if( i_src >= 3 ) 
     361            v |= *src++ << 20; // 3/3 
     362        *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4 
    361363        v = v << 6; 
    362364 
    363         *dst++ = ( len >= 3 ) ? b64[v >> 26] : '='; // 4/4 
    364  
    365         len--; 
    366         if( len > 0 ) 
    367         { 
    368             len--; 
    369             if( len > 0 ) 
    370                 len--; 
    371         } 
     365        /* -> 4/4 */ 
     366        *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4 
     367 
     368        if( i_src <= 3 ) 
     369            break; 
     370        i_src -= 3; 
    372371    } 
    373372 
     
    375374 
    376375    return ret; 
     376} 
     377 
     378char *vlc_b64_encode( const char *src ) 
     379{ 
     380    if( src ) 
     381        return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) ); 
     382    else 
     383        return vlc_b64_encode_binary( (const uint8_t*)"", 0 ); 
    377384} 
    378385