1 /*****************************************************************************
2 * update.c: VLC update checking and downloading
3 *****************************************************************************
4 * Copyright © 2005-2007 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8 * Rémi Duraffort <ivoire at via.ecp.fr>
9 Rafaël Carré <funman@videolanorg>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either release 2 of the License, or
14 * (at your option) any later release.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
28 * This file contains functions related to VLC and plugins update management
31 /* TODO: pgp verification of the status file, and downloaded binaries */
33 /*****************************************************************************
35 *****************************************************************************/
43 #include <vlc_update.h>
44 #include <vlc_stream.h>
45 #include <vlc_interface.h>
47 /*****************************************************************************
49 *****************************************************************************/
52 * Here is the format of these "status files" :
53 * First line is the last version: "X.Y.Ze" where:
54 * * X is the major number
55 * * Y is the minor number
56 * * Z is the revision number
57 * * e is an OPTIONAL extra letter
58 * * AKA "0.8.6d" or "0.9.0"
59 * Second line is an url to the last binary
60 * Third line is a description of the update (it MAY be extended to several lines, but for now it is only one line)
63 #if defined( UNDER_CE )
64 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
65 #elif defined( WIN32 )
66 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
67 #elif defined( __APPLE__ )
68 # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
69 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
71 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
73 #elif defined( SYS_BEOS )
74 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
76 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
80 /*****************************************************************************
82 *****************************************************************************/
83 static void EmptyRelease( update_t *p_update );
84 static void GetUpdateFile( update_t *p_update );
85 static int CompareReleases( const struct update_release_t *p1,
86 const struct update_release_t *p2 );
87 static char * size_str( long int l_size );
90 /*****************************************************************************
92 *****************************************************************************/
94 #define packet_type( c ) ( ( c & 0x3c ) >> 2 ) /* 0x3C = 00111100 */
95 #define packet_header_len( c ) ( ( c & 0x03 ) + 1 ) /* number of bytes in a packet header */
97 static inline int scalar_number( uint8_t *p, int header_len )
101 else if( header_len == 2 )
102 return( (p[0] << 8) + p[1] );
103 else if( header_len == 4 )
104 return( (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3] );
109 /* number of data bytes in a MPI */
110 #define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
113 * fill a public_key_packet_t structure from public key packet data
114 * verify that it is a version 4 public key packet, using DSA
116 static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
117 size_t i_packet_len )
119 if( i_packet_len != 418 )
122 p_key->version = *p_buf++;
123 if( p_key->version != 4 )
126 /* warn when timestamp is > date ? */
127 memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4;
129 p_key->algo = *p_buf++;
130 if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
133 memcpy( p_key->p, p_buf, 2+128 ); p_buf += 2+128;
134 if( mpi_len( p_key->p ) != 128 )
137 memcpy( p_key->q, p_buf, 2+20 ); p_buf += 2+20;
138 if( mpi_len( p_key->q ) != 20 )
141 memcpy( p_key->g, p_buf, 2+128 ); p_buf += 2+128;
142 if( mpi_len( p_key->g ) != 128 )
145 memcpy( p_key->y, p_buf, 2+128 ); p_buf += 2+128;
146 if( mpi_len( p_key->y ) != 128 )
153 * fill a signature_packet_v4_t from signature packet data
154 * verify that it was used with a DSA public key, using SHA-1 digest
156 static int parse_signature_v4_packet( signature_packet_v4_t *p_sig,
157 uint8_t *p_buf, size_t i_sig_len )
162 p_sig->version = *p_buf++;
163 if( p_sig->version != 4 )
166 p_sig->type = *p_buf++;
167 if( p_sig->type < GENERIC_KEY_SIGNATURE ||
168 p_sig->type > POSITIVE_KEY_SIGNATURE )
171 p_sig->public_key_algo = *p_buf++;
172 if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
175 p_sig->digest_algo = *p_buf++;
176 if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
179 memcpy( p_sig->hashed_data_len, p_buf, 2 ); p_buf += 2;
182 size_t i_hashed_data_len = scalar_number( p_sig->hashed_data_len, 2 );
183 i_pos += i_hashed_data_len;
184 if( i_pos > i_sig_len - 48 ) /* r & s are 44 bytes in total,
185 * + the unhashed data length (2 bytes)
186 * + the hash verification (2 bytes) */
189 p_sig->hashed_data = (uint8_t*) malloc( i_hashed_data_len );
190 if( !p_sig->hashed_data )
192 memcpy( p_sig->hashed_data, p_buf, i_hashed_data_len );
193 p_buf += i_hashed_data_len;
195 memcpy( p_sig->unhashed_data_len, p_buf, 2 ); p_buf += 2;
197 size_t i_unhashed_data_len = scalar_number( p_sig->unhashed_data_len, 2 );
198 i_pos += 2 + i_unhashed_data_len;
199 if( i_pos != i_sig_len - 46 )
201 free( p_sig->hashed_data );
205 p_sig->unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
206 if( !p_sig->unhashed_data )
208 free( p_sig->hashed_data );
211 memcpy( p_sig->unhashed_data, p_buf, i_unhashed_data_len );
212 p_buf += i_unhashed_data_len;
214 memcpy( p_sig->hash_verification, p_buf, 2 ); p_buf += 2;
216 memcpy( p_sig->r, p_buf, 22 ); p_buf += 22;
217 if( mpi_len( p_sig->r ) != 20 )
219 free( p_sig->hashed_data );
220 free( p_sig->unhashed_data );
224 memcpy( p_sig->s, p_buf, 22 );
225 if( mpi_len( p_sig->s ) != 20 )
227 free( p_sig->hashed_data );
228 free( p_sig->unhashed_data );
236 * crc_octets() was lamely copied from rfc 2440
237 * Copyright (C) The Internet Society (1998). All Rights Reserved.
239 #define CRC24_INIT 0xB704CEL
240 #define CRC24_POLY 0x1864CFBL
242 static long crc_octets( uint8_t *octets, size_t len )
244 long crc = CRC24_INIT;
248 crc ^= (*octets++) << 16;
249 for (i = 0; i < 8; i++)
256 return crc & 0xFFFFFFL;
260 * Transform an armored document in binary format
261 * Used on public keys and signatures
263 static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
264 uint8_t *p_obuf, size_t i_obuf_len )
266 char *p_ipos = p_ibuf;
267 uint8_t *p_opos = p_obuf;
270 int i_header_skipped = 0;
272 while( !i_end && p_ipos < p_ibuf + i_ibuf_len )
274 if( *p_ipos == '\r' || *p_ipos == '\n' )
280 size_t i_line_len = strcspn( p_ipos, "\r\n" );
281 if( i_line_len == 0 )
284 if( !i_header_skipped )
286 if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
287 i_header_skipped = 1;
289 p_ipos += i_line_len + 1;
293 if( !strncmp( p_ipos, "Version:", 8 ) )
295 p_ipos += i_line_len + 1;
299 if( p_ipos[i_line_len - 1] == '=' )
302 p_ipos[i_line_len - 1] = '\0';
305 p_ipos[i_line_len] = '\0';
307 p_opos += vlc_b64_decode_binary_to_buffer( p_opos,
308 p_obuf - p_opos + i_obuf_len,
311 p_ipos += i_line_len + 1;
314 /* XXX: the CRC is OPTIONAL, really require it ? */
315 if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
319 if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
322 long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
323 long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];
325 return l_crc2 == l_crc ? p_opos - p_obuf : 0;
329 * Download the signature associated to a document or a binary file.
330 * We're given the file's url, we just append ".asc" to it and download
332 static int download_signature( vlc_object_t *p_this,
333 signature_packet_v3_t *p_sig, char *psz_url )
335 char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
339 strcpy( psz_sig, psz_url );
340 strcat( psz_sig, ".asc" );
342 stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
348 int64_t i_size = stream_Size( p_stream );
351 stream_Delete( p_stream );
354 else if( i_size == 65 ) /* binary format signature */
356 int i_read = stream_Read( p_stream, p_sig, (int)i_size );
357 stream_Delete( p_stream );
358 if( i_read != i_size )
364 char *p_buf = (char*)malloc( i_size );
367 stream_Delete( p_stream );
371 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
373 stream_Delete( p_stream );
375 if( i_read != i_size )
381 int i_bytes = pgp_unarmor( p_buf, i_size, (uint8_t*)p_sig, 65 );
391 * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
393 static int verify_signature( vlc_object_t *p_this, uint8_t *p_r, uint8_t *p_s,
394 public_key_packet_t *p_key, uint8_t *p_hash )
396 /* the data to be verified (a SHA-1 hash) */
397 const char *hash_sexp_s = "(data(flags raw)(value %m))";
399 const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
401 const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
404 gcry_mpi_t p, q, g, y, r, s, hash;
405 p = q = g = y = r = s = hash = NULL;
406 gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
407 key_sexp = hash_sexp = sig_sexp = NULL;
409 if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, 128, NULL ) ||
410 gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, 20, NULL ) ||
411 gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, 128, NULL ) ||
412 gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, 128, NULL ) ||
413 gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
416 if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, 20, NULL ) ||
417 gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, 20, NULL ) ||
418 gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
421 if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, 20, NULL ) ||
422 gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
425 if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
431 if( p ) gcry_mpi_release( p );
432 if( q ) gcry_mpi_release( q );
433 if( g ) gcry_mpi_release( g );
434 if( y ) gcry_mpi_release( y );
435 if( r ) gcry_mpi_release( r );
436 if( s ) gcry_mpi_release( s );
437 if( hash ) gcry_mpi_release( hash );
438 if( key_sexp ) gcry_sexp_release( key_sexp );
439 if( sig_sexp ) gcry_sexp_release( sig_sexp );
440 if( hash_sexp ) gcry_sexp_release( hash_sexp );
445 * Return the long id (8 bytes) of the public key used to generate a signature
447 static uint8_t *get_issuer_from_signature_v4( signature_packet_v4_t *p_sig )
449 uint8_t *p = p_sig->unhashed_data;
450 uint8_t *max_pos = p + scalar_number( p_sig->unhashed_data_len, 2 );
454 int i_subpacket_len = *p < 192 ? *p++ :
455 *p < 255 ? ((*p++ - 192) << 8) + *p++ + 192 :
456 ((*++p) << 24) + (*++p << 16) + (*++p << 8) + *++p;
458 if( p >= max_pos - 1 )
461 if( *p == ISSUER_SUBPACKET )
464 p += i_subpacket_len;
470 * fill a public_key_t with public key data, including:
471 * * public key packet
472 * * signature packet issued by key which long id is p_sig_issuer
475 static int parse_public_key( const uint8_t *p_key_data, size_t i_key_len, public_key_t *p_key, const uint8_t *p_sig_issuer )
477 uint8_t *pos = (uint8_t*) p_key_data;
478 uint8_t *max_pos = pos + i_key_len;
481 #define PUBLIC_KEY_FOUND 0x01
482 #define USER_ID_FOUND 0x02
483 #define SIGNATURE_FOUND 0X04
485 uint8_t *p_key_unarmored = NULL;
487 signature_packet_v4_t sig;
489 p_key->psz_username = NULL;
490 p_key->sig.hashed_data = p_key->sig.unhashed_data = NULL;
492 if( !( *pos & 0x80 ) )
493 { /* first byte is ASCII, unarmoring */
494 p_key_unarmored = (uint8_t*)malloc( i_key_len );
495 if( !p_key_unarmored )
497 int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
498 p_key_unarmored, i_key_len );
503 pos = p_key_unarmored;
504 max_pos = pos + i_len;
507 while( pos < max_pos )
509 if( !(*pos & 0x80) || *pos & 0x40 )
512 int i_type = packet_type( *pos );
514 int i_header_len = packet_header_len( *pos++ );
515 if( pos + i_header_len > max_pos )
518 int i_packet_len = scalar_number( pos, i_header_len );
521 if( pos + i_packet_len > max_pos )
528 case PUBLIC_KEY_PACKET:
529 i_status |= PUBLIC_KEY_FOUND;
530 if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
534 case SIGNATURE_PACKET:
535 if( !p_sig_issuer || i_status & SIGNATURE_FOUND ||
536 parse_signature_v4_packet( &sig, pos, i_packet_len ) != VLC_SUCCESS )
538 p_issuer = get_issuer_from_signature_v4( &sig );
539 if( memcmp( p_issuer, p_sig_issuer, 8 ) == 0 )
541 memcpy( &p_key->sig, &sig, sizeof( signature_packet_v4_t ) );
542 i_status |= SIGNATURE_FOUND;
546 free( sig.hashed_data );
547 free( sig.unhashed_data );
552 if( p_key->psz_username ) /* save only the first User ID */
554 i_status |= USER_ID_FOUND;
555 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
556 if( !p_key->psz_username )
559 memcpy( p_key->psz_username, pos, i_packet_len );
560 p_key->psz_username[i_packet_len] = '\0';
568 free( p_key_unarmored );
570 if( !( i_status & ( PUBLIC_KEY_FOUND + USER_ID_FOUND ) ) )
573 if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
579 free( p_key->sig.hashed_data );
580 free( p_key->sig.unhashed_data );
581 free( p_key->psz_username );
582 free( p_key_unarmored );
587 * return a sha1 hash of a file
589 static uint8_t *hash_sha1_from_file( const char *psz_file,
590 signature_packet_v3_t *p_sig )
592 FILE *f = utf8_fopen( psz_file, "r" );
596 uint8_t buffer[4096]; //FIXME
599 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
606 while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
607 gcry_md_write( hd, buffer, i_read );
609 gcry_md_putc( hd, p_sig->type );
610 gcry_md_write( hd, &p_sig->timestamp, 4 );
615 return( (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1) );
619 * download a public key (the last one) from videolan server, and parse it
621 static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid, const uint8_t *p_signature_issuer )
624 if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x.asc",
625 p_longid[0], p_longid[1],
626 p_longid[2], p_longid[3],
627 p_longid[4], p_longid[5],
628 p_longid[6], p_longid[7] ) == -1 )
631 stream_t *p_stream = stream_UrlNew( p_this, psz_url );
636 int64_t i_size = stream_Size( p_stream );
639 stream_Delete( p_stream );
643 uint8_t *p_buf = (uint8_t*)malloc( i_size );
646 stream_Delete( p_stream );
650 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
651 stream_Delete( p_stream );
653 if( i_read != (int)i_size )
659 public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
666 int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
669 if( i_error != VLC_SUCCESS )
679 * Generate a SHA-1 hash on a public key, to verify a signature made on that hash
680 * Note that we need the signature to compute the hash
682 static uint8_t *key_sign_hash( public_key_t *p_pkey )
684 gcry_error_t error = 0;
687 error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
691 gcry_md_putc( hd, 0x99 );
693 gcry_md_putc( hd, (418 >> 8) & 0xff );
694 gcry_md_putc( hd, 418 & 0xff );
696 gcry_md_write( hd, (uint8_t*)&p_pkey->key, 418 );
698 gcry_md_putc( hd, 0xb4 );
700 int i_len = strlen((char*)p_pkey->psz_username);
702 gcry_md_putc( hd, (i_len << 24) & 0xff );
703 gcry_md_putc( hd, (i_len << 16) & 0xff );
704 gcry_md_putc( hd, (i_len << 8) & 0xff );
705 gcry_md_putc( hd, (i_len) & 0xff );
707 gcry_md_write( hd, p_pkey->psz_username, i_len );
709 size_t i_hashed_data_len = scalar_number( p_pkey->sig.hashed_data_len, 2 );
711 gcry_md_putc( hd, p_pkey->sig.version );
712 gcry_md_putc( hd, p_pkey->sig.type );
713 gcry_md_putc( hd, p_pkey->sig.public_key_algo );
714 gcry_md_putc( hd, p_pkey->sig.digest_algo );
715 gcry_md_write( hd, p_pkey->sig.hashed_data_len, 2 );
716 gcry_md_write( hd, p_pkey->sig.hashed_data, i_hashed_data_len );
718 gcry_md_putc( hd, 0x04 );
719 gcry_md_putc( hd, 0xff );
721 i_hashed_data_len += 6; /* hashed data + 6 bytes header */
723 gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
724 gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
725 gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
726 gcry_md_putc( hd, (i_hashed_data_len) & 0xff );
730 uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1);
732 if( p_hash[0] != p_pkey->sig.hash_verification[0] ||
733 p_hash[1] != p_pkey->sig.hash_verification[1] )
743 /*****************************************************************************
745 *****************************************************************************/
748 * Create a new update VLC struct
750 * \param p_this the calling vlc_object
751 * \return pointer to new update_t or NULL
753 update_t *__update_New( vlc_object_t *p_this )
758 p_update = (update_t *)malloc( sizeof( update_t ) );
759 if( !p_update ) return NULL;
761 vlc_mutex_init( p_this, &p_update->lock );
763 p_update->p_libvlc = p_this->p_libvlc;
765 p_update->release.psz_url = NULL;
766 p_update->release.psz_desc = NULL;
772 * Delete an update_t struct
774 * \param p_update update_t* pointer
777 void update_Delete( update_t *p_update )
781 vlc_mutex_destroy( &p_update->lock );
783 FREENULL( p_update->release.psz_url );
784 FREENULL( p_update->release.psz_desc );
790 * Empty the release struct
792 * \param p_update update_t* pointer
795 static void EmptyRelease( update_t *p_update )
797 p_update->release.i_major = 0;
798 p_update->release.i_minor = 0;
799 p_update->release.i_revision = 0;
801 FREENULL( p_update->release.psz_url );
802 FREENULL( p_update->release.psz_desc );
806 * Get the update file and parse it
807 * *p_update has to be locked when calling this function
809 * \param p_update pointer to update struct
812 static void GetUpdateFile( update_t *p_update )
814 stream_t *p_stream = NULL;
819 char *psz_line = NULL;
821 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
824 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
825 UPDATE_VLC_STATUS_URL );
829 /* Try to read three lines */
830 if( !( psz_line = stream_ReadLine( p_stream ) ) )
832 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
833 UPDATE_VLC_STATUS_URL );
837 /* first line : version number */
838 p_update->release.extra = 0;
839 switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
842 p_update->release.extra = extra;
844 p_update->release.i_major = i_major;
845 p_update->release.i_minor = i_minor;
846 p_update->release.i_revision = i_revision;
849 msg_Err( p_update->p_libvlc, "Update version false formated" );
854 /* Second line : URL */
855 if( !( psz_line = stream_ReadLine( p_stream ) ) )
857 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
858 UPDATE_VLC_STATUS_URL );
861 p_update->release.psz_url = psz_line;
864 /* Third line : description */
865 if( !( psz_line = stream_ReadLine( p_stream ) ) )
867 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
868 UPDATE_VLC_STATUS_URL );
871 p_update->release.psz_desc = psz_line;
875 stream_Delete( p_stream );
880 * Struct to launch the check in an other thread
886 void (*pf_callback)( void * );
888 } update_check_thread_t;
890 void update_CheckReal( update_check_thread_t *p_uct );
895 * \param p_update pointer to update struct
896 * \param pf_callback pointer to a function to call when the update_check is finished
897 * \param p_data pointer to some datas to give to the callback
900 void update_Check( update_t *p_update, void (*pf_callback)( void* ), void *p_data )
904 update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
905 sizeof( update_check_thread_t ) );
906 p_uct->p_update = p_update;
907 p_uct->pf_callback = pf_callback;
908 p_uct->p_data = p_data;
910 vlc_thread_create( p_uct, "check for update", update_CheckReal,
911 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
914 void update_CheckReal( update_check_thread_t *p_uct )
916 vlc_mutex_lock( &p_uct->p_update->lock );
918 EmptyRelease( p_uct->p_update );
919 GetUpdateFile( p_uct->p_update );
921 vlc_mutex_unlock( &p_uct->p_update->lock );
923 if( p_uct->pf_callback )
924 (p_uct->pf_callback)( p_uct->p_data );
926 vlc_object_destroy( p_uct );
930 * Compare two release numbers
932 * \param p1 first release
933 * \param p2 second release
934 * \return UpdateReleaseStatus(Older|Equal|Newer)
936 static int CompareReleases( const struct update_release_t *p1,
937 const struct update_release_t *p2 )
940 d = ( p1->i_major << 24 ) + ( p1->i_minor << 16 ) + ( p1->i_revision << 8 )
941 - ( p2->i_major << 24 ) - ( p2->i_minor << 16 ) - ( p2->i_revision << 8 )
942 + ( p1->extra ) - ( p2->extra );
945 return UpdateReleaseStatusOlder;
947 return UpdateReleaseStatusEqual;
949 return UpdateReleaseStatusNewer;
953 * Compare a given release's version number to the current VLC's one
955 * \param p_update structure
956 * \return UpdateReleaseStatus(Older|Equal|Newer)
958 int update_CompareReleaseToCurrent( update_t *p_update )
962 struct update_release_t c;
964 /* get the current version number */
965 c.i_major = *PACKAGE_VERSION_MAJOR - '0';
966 c.i_minor = *PACKAGE_VERSION_MINOR - '0';
967 c.i_revision = *PACKAGE_VERSION_REVISION - '0';
968 c.extra = *PACKAGE_VERSION_EXTRA;
970 return CompareReleases( &p_update->release, &c );
974 * Convert a long int size in bytes to a string
976 * \param l_size the size in bytes
977 * \return the size as a string
979 static char *size_str( long int l_size )
981 char *psz_tmp = NULL;
983 asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
984 else if( l_size >> 20 )
985 asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
986 else if( l_size >> 10 )
987 asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
989 asprintf( &psz_tmp, "%ld B", l_size );
995 * Struct to launch the download in a thread
1002 } update_download_thread_t;
1004 void update_DownloadReal( update_download_thread_t *p_udt );
1007 * Download the file given in the update_t
1009 * \param p_update structure
1010 * \param dir to store the download file
1013 void update_Download( update_t *p_update, char *psz_destdir )
1017 update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
1018 sizeof( update_download_thread_t ) );
1020 p_udt->p_update = p_update;
1021 p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
1023 vlc_thread_create( p_udt, "download update", update_DownloadReal,
1024 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1027 void update_DownloadReal( update_download_thread_t *p_udt )
1031 long int l_downloaded = 0;
1033 char *psz_status = NULL;
1034 char *psz_downloaded = NULL;
1035 char *psz_size = NULL;
1036 char *psz_destfile = NULL;
1037 char *psz_tmpdestfile = NULL;
1039 FILE *p_file = NULL;
1040 stream_t *p_stream = NULL;
1041 void* p_buffer = NULL;
1044 update_t *p_update = p_udt->p_update;
1045 char *psz_destdir = p_udt->psz_destdir;
1047 /* Open the stream */
1048 p_stream = stream_UrlNew( p_udt, p_update->release.psz_url );
1051 msg_Err( p_udt, "Failed to open %s for reading", p_update->release.psz_url );
1055 /* Get the stream size */
1056 l_size = stream_Size( p_stream );
1058 /* Get the file name and open it*/
1059 psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
1060 if( !psz_tmpdestfile )
1062 msg_Err( p_udt, "The URL %s is false formated", p_update->release.psz_url );
1066 if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
1069 p_file = utf8_fopen( psz_destfile, "w" );
1072 msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
1076 /* Create a buffer and fill it with the downloaded file */
1077 p_buffer = (void *)malloc( 1 << 10 );
1081 psz_size = size_str( l_size );
1082 if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done", p_update->release.psz_url, psz_size, 0.0 ) != -1 )
1084 i_progress = intf_UserProgress( p_udt, "Downloading ...", psz_status, 0.0, 0 );
1088 while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1089 !intf_ProgressIsCancelled( p_udt, i_progress ) )
1091 fwrite( p_buffer, i_read, 1, p_file );
1093 l_downloaded += i_read;
1094 psz_downloaded = size_str( l_downloaded );
1095 f_progress = 100.0*(float)l_downloaded/(float)l_size;
1097 if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done", p_update->release.psz_url,
1098 psz_size, psz_downloaded, f_progress ) != -1 )
1100 intf_ProgressUpdate( p_udt, i_progress, psz_status, f_progress, 0 );
1103 free( psz_downloaded );
1106 /* Finish the progress bar or delete the file if the user had canceled */
1109 if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
1111 if( asprintf( &psz_status, "%s\nDone %s (100.0%%)", p_update->release.psz_url, psz_size ) != -1 )
1113 intf_ProgressUpdate( p_udt, i_progress, psz_status, 100.0, 0 );
1118 remove( psz_destfile );
1122 stream_Delete( p_stream );
1125 free( psz_destdir );
1126 free( psz_destfile );
1130 vlc_object_destroy( p_udt );