⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 update.c

📁 VLC Player Source Code
💻 C
📖 第 1 页 / 共 4 页
字号:
        msg_Warn( p_update->p_libvlc, "Invalid signature version %d",                sign.version);        goto error_hd;    }    gcry_md_final( hd );    uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1 );    if( p_hash[0] != sign.hash_verification[0] ||        p_hash[1] != sign.hash_verification[1] )    {        msg_Warn( p_update->p_libvlc, "Bad SHA1 hash for status file" );        goto error_hd;    }    if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )            != VLC_SUCCESS )    {        msg_Err( p_update->p_libvlc, "BAD SIGNATURE for status file" );        goto error_hd;    }    else    {        msg_Info( p_update->p_libvlc, "Status file authenticated" );        gcry_md_close( hd );        return true;    }error_hd:    gcry_md_close( hd );error:    if( p_stream )        stream_Delete( p_stream );    free( psz_version_line );    return false;}static void* update_CheckReal( vlc_object_t *p_this );/** * Check for updates * * \param p_update pointer to update struct * \param pf_callback pointer to a function to call when the update_check is finished * \param p_data pointer to some datas to give to the callback * \returns nothing */void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void *p_data ){    assert( p_update );    update_check_thread_t *p_uct =        vlc_custom_create( p_update->p_libvlc, sizeof( *p_uct ),                           VLC_OBJECT_GENERIC, "update check" );    if( !p_uct ) return;    p_uct->p_update = p_update;    p_update->p_check = p_uct;    p_uct->pf_callback = pf_callback;    p_uct->p_data = p_data;    vlc_thread_create( p_uct, "check for update", update_CheckReal,                       VLC_THREAD_PRIORITY_LOW, false );}void* update_CheckReal( vlc_object_t* p_this ){    update_check_thread_t *p_uct = (update_check_thread_t *)p_this;    bool b_ret;    vlc_mutex_lock( &p_uct->p_update->lock );    EmptyRelease( p_uct->p_update );    b_ret = GetUpdateFile( p_uct->p_update );    vlc_mutex_unlock( &p_uct->p_update->lock );    if( p_uct->pf_callback )        (p_uct->pf_callback)( p_uct->p_data, b_ret );    p_uct->p_update->p_check = NULL;    vlc_object_release( p_uct );    return NULL;}/** * Compare a given release's version number to the current VLC's one * * \param p_update structure * \return true if we have to upgrade to the given version to be up to date */static bool is_strictly_greater( int * a, int * b, int n){    if( n <= 0 ) return false;    if(a[0] > b[0] ) return true;    if(a[0] == b[0] ) return is_strictly_greater( a+1, b+1, n-1 );    /* a[0] < b[0] */ return false;}bool update_NeedUpgrade( update_t *p_update ){    assert( p_update );    int current_version[] = {        *PACKAGE_VERSION_MAJOR - '0',        *PACKAGE_VERSION_MINOR - '0',        *PACKAGE_VERSION_REVISION - '0',        *PACKAGE_VERSION_EXTRA    };    int latest_version[] = {        p_update->release.i_major,        p_update->release.i_minor,        p_update->release.i_revision,        p_update->release.extra    };    return is_strictly_greater( latest_version, current_version, 4 );}/** * Convert a long int size in bytes to a string * * \param l_size the size in bytes * \return the size as a string */static char *size_str( long int l_size ){    char *psz_tmp = NULL;    int i_retval = 0;    if( l_size >> 30 )        i_retval = asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );    else if( l_size >> 20 )        i_retval = asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );    else if( l_size >> 10 )        i_retval = asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );    else        i_retval = asprintf( &psz_tmp, "%ld B", l_size );    return i_retval == -1 ? NULL : psz_tmp;}void update_WaitDownload( update_t *p_update ){    if(p_update->p_download)        vlc_thread_join( p_update->p_download );    vlc_object_release( p_update->p_download );    p_update->p_download = NULL;}static void* update_DownloadReal( vlc_object_t *p_this );/** * Download the file given in the update_t * * \param p_update structure * \param dir to store the download file * \return nothing */void update_Download( update_t *p_update, const char *psz_destdir ){    assert( p_update );    update_download_thread_t *p_udt =        vlc_custom_create( p_update->p_libvlc, sizeof( *p_udt ),                           VLC_OBJECT_GENERIC, "update download" );    if( !p_udt )        return;    p_udt->p_update = p_update;    p_update->p_download = p_udt;    p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;    vlc_thread_create( p_udt, "download update", update_DownloadReal,                       VLC_THREAD_PRIORITY_LOW, false );}static void* update_DownloadReal( vlc_object_t *p_this ){    update_download_thread_t *p_udt = (update_download_thread_t *)p_this;    int i_progress = 0;    long int l_size;    long int l_downloaded = 0;    float f_progress;    char *psz_status = NULL;    char *psz_downloaded = NULL;    char *psz_size = NULL;    char *psz_destfile = NULL;    char *psz_tmpdestfile = NULL;    FILE *p_file = NULL;    stream_t *p_stream = NULL;    void* p_buffer = NULL;    int i_read;    update_t *p_update = p_udt->p_update;    char *psz_destdir = p_udt->psz_destdir;    msg_Dbg( p_udt, "Opening Stream '%s'", p_update->release.psz_url );    /* Open the stream */    p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );    if( !p_stream )    {        msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );        goto end;    }    /* Get the stream size */    l_size = stream_Size( p_stream );    /* Get the file name and open it*/    psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );    if( !psz_tmpdestfile )    {        msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );        goto end;    }    psz_tmpdestfile++;    if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )        goto end;    p_file = utf8_fopen( psz_destfile, "w" );    if( !p_file )    {        msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );        goto end;    }    /* Create a buffer and fill it with the downloaded file */    p_buffer = (void *)malloc( 1 << 10 );    if( !p_buffer )    {        msg_Err( p_udt, "Can't malloc (1 << 10) bytes! download cancelled." );        goto end;    }    msg_Dbg( p_udt, "Downloading Stream '%s'", p_update->release.psz_url );    psz_size = size_str( l_size );    if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done",        p_update->release.psz_url, psz_size, 0.0 ) != -1 )    {        i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );        free( psz_status );    }    vlc_object_lock( p_udt );    while( vlc_object_alive( p_udt ) &&           ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&           !intf_ProgressIsCancelled( p_udt, i_progress ) )    {        vlc_object_unlock( p_udt );        if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )        {            msg_Err( p_udt, "Failed to write into %s", psz_destfile );            break;        }        l_downloaded += i_read;        psz_downloaded = size_str( l_downloaded );        f_progress = 100.0*(float)l_downloaded/(float)l_size;        if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done",                      p_update->release.psz_url, psz_downloaded, psz_size,                      f_progress ) != -1 )        {            intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );            free( psz_status );        }        free( psz_downloaded );        vlc_object_lock( p_udt );    }    /* Finish the progress bar or delete the file if the user had canceled */    fclose( p_file );    p_file = NULL;    if( vlc_object_alive( p_udt ) &&        !intf_ProgressIsCancelled( p_udt, i_progress ) )    {        vlc_object_unlock( p_udt );        if( asprintf( &psz_status, "%s\nDone %s (100.0%%)",            p_update->release.psz_url, psz_size ) != -1 )        {            intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );            i_progress = 0;            free( psz_status );        }    }    else    {        vlc_object_unlock( p_udt );        utf8_unlink( psz_destfile );        goto end;    }    signature_packet_t sign;    if( download_signature( VLC_OBJECT( p_udt ), &sign,            p_update->release.psz_url ) != VLC_SUCCESS )    {        utf8_unlink( psz_destfile );        intf_UserFatal( p_udt, true, _("File could not be verified"),            _("It was not possible to download a cryptographic signature for "              "the downloaded file \"%s\". Thus, it was deleted."),            psz_destfile );        msg_Err( p_udt, "Couldn't download signature of downloaded file" );        goto end;    }    if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )    {        utf8_unlink( psz_destfile );        msg_Err( p_udt, "Invalid signature issuer" );        intf_UserFatal( p_udt, true, _("Invalid signature"),            _("The cryptographic signature for the downloaded file \"%s\" was "              "invalid and could not be used to securely verify it. Thus, the "              "file was deleted."),            psz_destfile );        goto end;    }    if( sign.type != BINARY_SIGNATURE )    {        utf8_unlink( psz_destfile );        msg_Err( p_udt, "Invalid signature type" );        intf_UserFatal( p_udt, true, _("Invalid signature"),            _("The cryptographic signature for the downloaded file \"%s\" was "              "invalid and could not be used to securely verify it. Thus, the "              "file was deleted."),            psz_destfile );        goto end;    }    uint8_t *p_hash = hash_sha1_from_file( psz_destfile, &sign );    if( !p_hash )    {        msg_Err( p_udt, "Unable to hash %s", psz_destfile );        utf8_unlink( psz_destfile );        intf_UserFatal( p_udt, true, _("File not verifiable"),            _("It was not possible to securely verify the downloaded file"              " \"%s\". Thus, it was VLC deleted."),            psz_destfile );        goto end;    }    if( p_hash[0] != sign.hash_verification[0] ||        p_hash[1] != sign.hash_verification[1] )    {        utf8_unlink( psz_destfile );        intf_UserFatal( p_udt, true, _("File corrupted"),            _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),             psz_destfile );        msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );        free( p_hash );        goto end;    }    if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )            != VLC_SUCCESS )    {        utf8_unlink( psz_destfile );        intf_UserFatal( p_udt, true, _("File corrupted"),            _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),             psz_destfile );        msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );        free( p_hash );        goto end;    }    msg_Info( p_udt, "%s authenticated", psz_destfile );    free( p_hash );end:    if( i_progress )    {        intf_ProgressUpdate( p_udt, i_progress, "Cancelled", 100.0, 0 );    }    if( p_stream )        stream_Delete( p_stream );    if( p_file )        fclose( p_file );    free( psz_destdir );    free( psz_destfile );    free( p_buffer );    free( psz_size );    return NULL;}update_release_t *update_GetRelease( update_t *p_update ){    return &p_update->release;}#elseupdate_t *__update_New( vlc_object_t *p_this ){    (void)p_this;    return NULL;}void update_Delete( update_t *p_update ){    (void)p_update;}void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ),                   void *p_data ){    (void)p_update; (void)pf_callback; (void)p_data;}bool update_NeedUpgrade( update_t *p_update ){    (void)p_update;    return false;}void update_WaitDownload( update_t *p_update ){    (void)p_update;}void update_Download( update_t *p_update, const char *psz_destdir ){    (void)p_update; (void)psz_destdir;}update_release_t *update_GetRelease( update_t *p_update ){    (void)p_update;    return NULL;}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -