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 *****************************************************************************/
41 #include <ctype.h> /* tolower() */
45 #include <vlc_update.h>
47 #include <vlc_block.h>
48 #include <vlc_stream.h>
49 #include <vlc_interface.h>
50 #include <vlc_charset.h>
52 /*****************************************************************************
54 *****************************************************************************/
57 * Here is the format of these "status files" :
58 * First line is the last version: "X.Y.Ze" where:
59 * * X is the major number
60 * * Y is the minor number
61 * * Z is the revision number
62 * * e is an OPTIONAL extra letter
63 * * AKA "0.8.6d" or "0.9.0"
64 * Second line is an url to the last binary
65 * Third line is a description of the update (it MAY be extended to several lines, but for now it is only one line)
68 #if defined( UNDER_CE )
69 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
70 #elif defined( WIN32 )
71 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
72 #elif defined( __APPLE__ )
73 # define UPDATE_VLC_OS "macosx"
74 # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
75 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
77 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
79 #elif defined( SYS_BEOS )
80 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
82 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
85 #define STRDUP( a ) ( a ? strdup( a ) : NULL )
88 /*****************************************************************************
90 *****************************************************************************/
91 static void EmptyRelease( update_t *p_update );
92 static void GetUpdateFile( update_t *p_update );
93 static int CompareReleases( const struct update_release_t *p1,
94 const struct update_release_t *p2 );
95 static char * size_str( long int l_size );
98 /*****************************************************************************
100 *****************************************************************************/
102 #define packet_type( c ) ( ( c & 0x3c ) >> 2 ) /* 0x3C = 00111100 */
103 #define packet_header_len( c ) ( ( c & 0x03 ) + 1 ) /* number of bytes in a packet header */
105 static inline int scalar_number( uint8_t *p, int header_len )
107 if( header_len == 1 )
109 else if( header_len == 2 )
110 return( (p[0] << 8) + p[1] );
111 else if( header_len == 4 )
112 return( (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3] );
117 /* number of data bytes in a MPI */
118 #define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
121 * fill a public_key_packet_t structure from public key packet data
122 * verify that it is a version 4 public key packet, using DSA
124 static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
125 size_t i_packet_len )
127 if( i_packet_len != 418 )
130 p_key->version = *p_buf++;
131 if( p_key->version != 4 )
134 /* warn when timestamp is > date ? */
135 memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4;
137 p_key->algo = *p_buf++;
138 if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
141 memcpy( p_key->p, p_buf, 2+128 ); p_buf += 2+128;
142 if( mpi_len( p_key->p ) != 128 )
145 memcpy( p_key->q, p_buf, 2+20 ); p_buf += 2+20;
146 if( mpi_len( p_key->q ) != 20 )
149 memcpy( p_key->g, p_buf, 2+128 ); p_buf += 2+128;
150 if( mpi_len( p_key->g ) != 128 )
153 memcpy( p_key->y, p_buf, 2+128 ); p_buf += 2+128;
154 if( mpi_len( p_key->y ) != 128 )
161 * fill a signature_packet_v4_t from signature packet data
162 * verify that it was used with a DSA public key, using SHA-1 digest
164 static int parse_signature_v4_packet( signature_packet_v4_t *p_sig,
165 uint8_t *p_buf, size_t i_sig_len )
170 p_sig->version = *p_buf++;
171 if( p_sig->version != 4 )
174 p_sig->type = *p_buf++;
175 if( p_sig->type < GENERIC_KEY_SIGNATURE ||
176 p_sig->type > POSITIVE_KEY_SIGNATURE )
179 p_sig->public_key_algo = *p_buf++;
180 if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
183 p_sig->digest_algo = *p_buf++;
184 if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
187 memcpy( p_sig->hashed_data_len, p_buf, 2 ); p_buf += 2;
190 size_t i_hashed_data_len = scalar_number( p_sig->hashed_data_len, 2 );
191 i_pos += i_hashed_data_len;
192 if( i_pos > i_sig_len - 48 ) /* r & s are 44 bytes in total,
193 * + the unhashed data length (2 bytes)
194 * + the hash verification (2 bytes) */
197 p_sig->hashed_data = (uint8_t*) malloc( i_hashed_data_len );
198 if( !p_sig->hashed_data )
200 memcpy( p_sig->hashed_data, p_buf, i_hashed_data_len );
201 p_buf += i_hashed_data_len;
203 memcpy( p_sig->unhashed_data_len, p_buf, 2 ); p_buf += 2;
205 size_t i_unhashed_data_len = scalar_number( p_sig->unhashed_data_len, 2 );
206 i_pos += 2 + i_unhashed_data_len;
207 if( i_pos != i_sig_len - 46 )
209 free( p_sig->hashed_data );
213 p_sig->unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
214 if( !p_sig->unhashed_data )
216 free( p_sig->hashed_data );
219 memcpy( p_sig->unhashed_data, p_buf, i_unhashed_data_len );
220 p_buf += i_unhashed_data_len;
222 memcpy( p_sig->hash_verification, p_buf, 2 ); p_buf += 2;
224 memcpy( p_sig->r, p_buf, 22 ); p_buf += 22;
225 if( mpi_len( p_sig->r ) != 20 )
227 free( p_sig->hashed_data );
228 free( p_sig->unhashed_data );
232 memcpy( p_sig->s, p_buf, 22 );
233 if( mpi_len( p_sig->s ) != 20 )
235 free( p_sig->hashed_data );
236 free( p_sig->unhashed_data );
244 * crc_octets() was lamely copied from rfc 2440
245 * Copyright (C) The Internet Society (1998). All Rights Reserved.
247 #define CRC24_INIT 0xB704CEL
248 #define CRC24_POLY 0x1864CFBL
250 static long crc_octets( uint8_t *octets, size_t len )
252 long crc = CRC24_INIT;
256 crc ^= (*octets++) << 16;
257 for (i = 0; i < 8; i++)
264 return crc & 0xFFFFFFL;
268 * Transform an armored document in binary format
269 * Used on public keys and signatures
271 static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
272 uint8_t *p_obuf, size_t i_obuf_len )
274 char *p_ipos = p_ibuf;
275 uint8_t *p_opos = p_obuf;
278 int i_header_skipped = 0;
280 while( !i_end && p_ipos < p_ibuf + i_ibuf_len )
282 if( *p_ipos == '\r' || *p_ipos == '\n' )
288 size_t i_line_len = strcspn( p_ipos, "\r\n" );
289 if( i_line_len == 0 )
292 if( !i_header_skipped )
294 if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
295 i_header_skipped = 1;
297 p_ipos += i_line_len + 1;
301 if( !strncmp( p_ipos, "Version:", 8 ) )
303 p_ipos += i_line_len + 1;
307 if( p_ipos[i_line_len - 1] == '=' )
310 p_ipos[i_line_len - 1] = '\0';
313 p_ipos[i_line_len] = '\0';
315 p_opos += vlc_b64_decode_binary_to_buffer( p_opos,
316 p_obuf - p_opos + i_obuf_len,
319 p_ipos += i_line_len + 1;
322 /* XXX: the CRC is OPTIONAL, really require it ? */
323 if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
327 if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
330 long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
331 long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];
333 return l_crc2 == l_crc ? p_opos - p_obuf : 0;
337 * Download the signature associated to a document or a binary file.
338 * We're given the file's url, we just append ".asc" to it and download
340 static int download_signature( vlc_object_t *p_this,
341 signature_packet_v3_t *p_sig, char *psz_url )
343 char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
347 strcpy( psz_sig, psz_url );
348 strcat( psz_sig, ".asc" );
350 stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
356 int64_t i_size = stream_Size( p_stream );
359 stream_Delete( p_stream );
362 else if( i_size == 65 ) /* binary format signature */
364 int i_read = stream_Read( p_stream, p_sig, (int)i_size );
365 stream_Delete( p_stream );
366 if( i_read != i_size )
372 char *p_buf = (char*)malloc( i_size );
375 stream_Delete( p_stream );
379 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
381 stream_Delete( p_stream );
383 if( i_read != i_size )
389 int i_bytes = pgp_unarmor( p_buf, i_size, (uint8_t*)p_sig, 65 );
399 * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
401 static int verify_signature( vlc_object_t *p_this, uint8_t *p_r, uint8_t *p_s,
402 public_key_packet_t *p_key, uint8_t *p_hash )
404 /* the data to be verified (a SHA-1 hash) */
405 const char *hash_sexp_s = "(data(flags raw)(value %m))";
407 const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
409 const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
412 gcry_mpi_t p, q, g, y, r, s, hash;
413 p = q = g = y = r = s = hash = NULL;
414 gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
415 key_sexp = hash_sexp = sig_sexp = NULL;
417 if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, 128, NULL ) ||
418 gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, 20, NULL ) ||
419 gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, 128, NULL ) ||
420 gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, 128, NULL ) ||
421 gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
424 if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, 20, NULL ) ||
425 gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, 20, NULL ) ||
426 gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
429 if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, 20, NULL ) ||
430 gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
433 if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
439 if( p ) gcry_mpi_release( p );
440 if( q ) gcry_mpi_release( q );
441 if( g ) gcry_mpi_release( g );
442 if( y ) gcry_mpi_release( y );
443 if( r ) gcry_mpi_release( r );
444 if( s ) gcry_mpi_release( s );
445 if( hash ) gcry_mpi_release( hash );
446 if( key_sexp ) gcry_sexp_release( key_sexp );
447 if( sig_sexp ) gcry_sexp_release( sig_sexp );
448 if( hash_sexp ) gcry_sexp_release( hash_sexp );
453 * Return the long id (8 bytes) of the public key used to generate a signature
455 static uint8_t *get_issuer_from_signature_v4( signature_packet_v4_t *p_sig )
457 uint8_t *p = p_sig->unhashed_data;
458 uint8_t *max_pos = p + scalar_number( p_sig->unhashed_data_len, 2 );
462 int i_subpacket_len = *p < 192 ? *p++ :
463 *p < 255 ? ((*p++ - 192) << 8) + *p++ + 192 :
464 ((*++p) << 24) + (*++p << 16) + (*++p << 8) + *++p;
466 if( p >= max_pos - 1 )
469 if( *p == ISSUER_SUBPACKET )
472 p += i_subpacket_len;
478 * fill a public_key_t with public key data, including:
479 * * public key packet
480 * * signature packet issued by key which long id is p_sig_issuer
483 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 )
485 uint8_t *pos = (uint8_t*) p_key_data;
486 uint8_t *max_pos = pos + i_key_len;
489 #define PUBLIC_KEY_FOUND 0x01
490 #define USER_ID_FOUND 0x02
491 #define SIGNATURE_FOUND 0X04
493 uint8_t *p_key_unarmored = NULL;
495 signature_packet_v4_t sig;
497 p_key->psz_username = NULL;
498 p_key->sig.hashed_data = p_key->sig.unhashed_data = NULL;
500 if( !( *pos & 0x80 ) )
501 { /* first byte is ASCII, unarmoring */
502 p_key_unarmored = (uint8_t*)malloc( i_key_len );
503 if( !p_key_unarmored )
505 int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
506 p_key_unarmored, i_key_len );
511 pos = p_key_unarmored;
512 max_pos = pos + i_len;
515 while( pos < max_pos )
517 if( !(*pos & 0x80) || *pos & 0x40 )
520 int i_type = packet_type( *pos );
522 int i_header_len = packet_header_len( *pos++ );
523 if( pos + i_header_len > max_pos )
526 int i_packet_len = scalar_number( pos, i_header_len );
529 if( pos + i_packet_len > max_pos )
536 case PUBLIC_KEY_PACKET:
537 i_status |= PUBLIC_KEY_FOUND;
538 if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
542 case SIGNATURE_PACKET:
543 if( !p_sig_issuer || i_status & SIGNATURE_FOUND ||
544 parse_signature_v4_packet( &sig, pos, i_packet_len ) != VLC_SUCCESS )
546 p_issuer = get_issuer_from_signature_v4( &sig );
547 if( memcmp( p_issuer, p_sig_issuer, 8 ) == 0 )
549 memcpy( &p_key->sig, &sig, sizeof( signature_packet_v4_t ) );
550 i_status |= SIGNATURE_FOUND;
554 free( sig.hashed_data );
555 free( sig.unhashed_data );
560 if( p_key->psz_username ) /* save only the first User ID */
562 i_status |= USER_ID_FOUND;
563 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
564 if( !p_key->psz_username )
567 memcpy( p_key->psz_username, pos, i_packet_len );
568 p_key->psz_username[i_packet_len] = '\0';
576 free( p_key_unarmored );
578 if( !( i_status & ( PUBLIC_KEY_FOUND + USER_ID_FOUND ) ) )
581 if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
587 free( p_key->sig.hashed_data );
588 free( p_key->sig.unhashed_data );
589 free( p_key->psz_username );
590 free( p_key_unarmored );
595 * return a sha1 hash of a file
597 static uint8_t *hash_sha1_from_file( const char *psz_file,
598 signature_packet_v3_t *p_sig )
600 FILE *f = utf8_fopen( psz_file, "r" );
604 uint8_t buffer[4096]; //FIXME
607 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
614 while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
615 gcry_md_write( hd, buffer, i_read );
617 gcry_md_putc( hd, p_sig->type );
618 gcry_md_write( hd, &p_sig->timestamp, 4 );
623 return( (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1) );
627 * download a public key (the last one) from videolan server, and parse it
629 static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid, const uint8_t *p_signature_issuer )
632 if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x.asc",
633 p_longid[0], p_longid[1],
634 p_longid[2], p_longid[3],
635 p_longid[4], p_longid[5],
636 p_longid[6], p_longid[7] ) == -1 )
639 stream_t *p_stream = stream_UrlNew( p_this, psz_url );
644 int64_t i_size = stream_Size( p_stream );
647 stream_Delete( p_stream );
651 uint8_t *p_buf = (uint8_t*)malloc( i_size );
654 stream_Delete( p_stream );
658 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
659 stream_Delete( p_stream );
661 if( i_read != (int)i_size )
667 public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
674 int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
677 if( i_error != VLC_SUCCESS )
687 * Generate a SHA-1 hash on a public key, to verify a signature made on that hash
688 * Note that we need the signature to compute the hash
690 static uint8_t *key_sign_hash( public_key_t *p_pkey )
692 gcry_error_t error = 0;
695 error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
699 gcry_md_putc( hd, 0x99 );
701 gcry_md_putc( hd, (418 >> 8) & 0xff );
702 gcry_md_putc( hd, 418 & 0xff );
704 gcry_md_write( hd, (uint8_t*)&p_pkey->key, 418 );
706 gcry_md_putc( hd, 0xb4 );
708 int i_len = strlen((char*)p_pkey->psz_username);
710 gcry_md_putc( hd, (i_len << 24) & 0xff );
711 gcry_md_putc( hd, (i_len << 16) & 0xff );
712 gcry_md_putc( hd, (i_len << 8) & 0xff );
713 gcry_md_putc( hd, (i_len) & 0xff );
715 gcry_md_write( hd, p_pkey->psz_username, i_len );
717 size_t i_hashed_data_len = scalar_number( p_pkey->sig.hashed_data_len, 2 );
719 gcry_md_putc( hd, p_pkey->sig.version );
720 gcry_md_putc( hd, p_pkey->sig.type );
721 gcry_md_putc( hd, p_pkey->sig.public_key_algo );
722 gcry_md_putc( hd, p_pkey->sig.digest_algo );
723 gcry_md_write( hd, p_pkey->sig.hashed_data_len, 2 );
724 gcry_md_write( hd, p_pkey->sig.hashed_data, i_hashed_data_len );
726 gcry_md_putc( hd, 0x04 );
727 gcry_md_putc( hd, 0xff );
729 i_hashed_data_len += 6; /* hashed data + 6 bytes header */
731 gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
732 gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
733 gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
734 gcry_md_putc( hd, (i_hashed_data_len) & 0xff );
738 uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1);
740 if( p_hash[0] != p_pkey->sig.hash_verification[0] ||
741 p_hash[1] != p_pkey->sig.hash_verification[1] )
751 /*****************************************************************************
753 *****************************************************************************/
756 * Create a new update VLC struct
758 * \param p_this the calling vlc_object
759 * \return pointer to new update_t or NULL
761 update_t *__update_New( vlc_object_t *p_this )
765 if( p_this == NULL ) return NULL;
767 p_update = (update_t *)malloc( sizeof( update_t ) );
768 if( !p_update ) return NULL;
770 vlc_mutex_init( p_this, &p_update->lock );
772 p_update->p_libvlc = p_this->p_libvlc;
774 p_update->release.psz_url = NULL;
775 p_update->release.psz_desc = NULL;
777 var_Create( p_this->p_libvlc, "update-notify", VLC_VAR_INTEGER |
784 * Delete an update_t struct
786 * \param p_update update_t* pointer
789 void update_Delete( update_t *p_update )
793 vlc_mutex_destroy( &p_update->lock );
795 var_Destroy( p_update->p_libvlc, "update-notify" );
797 FREENULL( p_update->release.psz_url );
798 FREENULL( p_update->release.psz_desc );
804 * Empty the release struct
806 * \param p_update update_t* pointer
809 static void EmptyRelease( update_t *p_update )
811 p_update->release.i_major = 0;
812 p_update->release.i_minor = 0;
813 p_update->release.i_revision = 0;
815 FREENULL( p_update->release.psz_url );
816 FREENULL( p_update->release.psz_desc );
820 * Get the update file and parse it
821 * *p_update has to be locked when calling this function
823 * \param p_update pointer to update struct
826 static void GetUpdateFile( update_t *p_update )
828 stream_t *p_stream = NULL;
833 char *psz_line = NULL;
835 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
838 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
839 UPDATE_VLC_STATUS_URL );
843 /* Try to read three lines */
844 if( !( psz_line = stream_ReadLine( p_stream ) ) )
846 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
847 UPDATE_VLC_STATUS_URL );
851 /* first line : version number */
852 p_update->release.extra = 0;
853 switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
856 p_update->release.extra = extra;
858 p_update->release.i_major = i_major;
859 p_update->release.i_minor = i_minor;
860 p_update->release.i_revision = i_revision;
863 msg_Err( p_update->p_libvlc, "Update version false formated" );
868 /* Second line : URL */
869 if( !( psz_line = stream_ReadLine( p_stream ) ) )
871 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
872 UPDATE_VLC_STATUS_URL );
875 p_update->release.psz_url = psz_line;
878 /* Third line : description */
879 if( !( psz_line = stream_ReadLine( p_stream ) ) )
881 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
882 UPDATE_VLC_STATUS_URL );
885 p_update->release.psz_desc = psz_line;
889 stream_Delete( p_stream );
894 * Struct to launch the check in an other thread
900 } update_check_thread_t;
902 void update_CheckReal( update_check_thread_t *p_uct );
907 * \param p_update pointer to update struct
910 void update_Check( update_t *p_update )
914 update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
915 sizeof( update_check_thread_t ) );
916 p_uct->p_update = p_update;
918 vlc_thread_create( p_uct, "check for update", update_CheckReal,
919 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
922 void update_CheckReal( update_check_thread_t *p_uct )
924 vlc_mutex_lock( &p_uct->p_update->lock );
926 EmptyRelease( p_uct->p_update );
927 GetUpdateFile( p_uct->p_update );
929 vlc_mutex_unlock( &p_uct->p_update->lock );
931 var_TriggerCallback( p_uct->p_update->p_libvlc, "update-notify" );
935 * Compare two release numbers
937 * \param p1 first release
938 * \param p2 second release
939 * \return UpdateReleaseStatus(Older|Equal|Newer)
941 static int CompareReleases( const struct update_release_t *p1,
942 const struct update_release_t *p2 )
945 d = ( p1->i_major << 24 ) + ( p1->i_minor << 16 ) + ( p1->i_revision << 8 )
946 - ( p2->i_major << 24 ) - ( p2->i_minor << 16 ) - ( p2->i_revision << 8 )
947 + ( p1->extra ) - ( p2->extra );
950 return UpdateReleaseStatusOlder;
952 return UpdateReleaseStatusEqual;
954 return UpdateReleaseStatusNewer;
958 * Compare a given release's version number to the current VLC's one
960 * \param p_update structure
961 * \return UpdateReleaseStatus(Older|Equal|Newer)
963 int update_CompareReleaseToCurrent( update_t *p_update )
967 struct update_release_t c;
969 /* get the current version number */
970 c.i_major = *PACKAGE_VERSION_MAJOR - '0';
971 c.i_minor = *PACKAGE_VERSION_MINOR - '0';
972 c.i_revision = *PACKAGE_VERSION_REVISION - '0';
973 c.extra = *PACKAGE_VERSION_EXTRA;
975 return CompareReleases( &p_update->release, &c );
979 * Convert a long int size in bytes to a string
981 * \param l_size the size in bytes
982 * \return the size as a string
984 static char *size_str( long int l_size )
986 char *psz_tmp = NULL;
988 asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
989 else if( l_size >> 20 )
990 asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
991 else if( l_size >> 10 )
992 asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
994 asprintf( &psz_tmp, "%ld B", l_size );
1000 * Struct to launch the download in a thread
1007 } update_download_thread_t;
1009 void update_DownloadReal( update_download_thread_t *p_udt );
1012 * Download the file given in the update_t
1014 * \param p_update structure
1015 * \param dir to store the download file
1018 void update_Download( update_t *p_update, char *psz_destdir )
1022 update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
1023 sizeof( update_download_thread_t ) );
1025 p_udt->p_update = p_update;
1026 p_udt->psz_destdir = STRDUP( psz_destdir );
1028 vlc_thread_create( p_udt, "download update", update_DownloadReal,
1029 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1031 void update_DownloadReal( update_download_thread_t *p_udt )
1035 long int l_downloaded = 0;
1037 char *psz_downloaded;
1040 char *psz_tmpdestfile;
1042 FILE *p_file = NULL;
1047 update_t *p_update = p_udt->p_update;
1048 char *psz_destdir = p_udt->psz_destdir;
1050 /* Open the stream */
1051 p_stream = stream_UrlNew( p_update->p_libvlc, p_update->release.psz_url );
1055 msg_Err( p_update->p_libvlc, "Failed to open %s for reading", p_update->release.psz_url );
1059 /* Get the stream size and open the output file */
1060 l_size = stream_Size( p_stream );
1062 psz_tmpdestfile = strrchr( p_update->release.psz_url, '/' );
1063 if( !psz_tmpdestfile )
1065 msg_Err( p_update->p_libvlc, "The URL %s is false formated", p_update->release.psz_url );
1071 if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) != -1 )
1073 p_file = utf8_fopen( psz_destfile, "w" );
1076 msg_Err( p_update->p_libvlc, "Failed to open %s for writing", psz_destfile );
1080 /* Create a buffer and fill it with the downloaded file */
1081 p_buffer = (void *)malloc( 1 << 10 );
1084 psz_size = size_str( l_size );
1085 if( asprintf( &psz_status, "%s\nDownloading... O.O/%s %.1f%% done", p_update->release.psz_url, psz_size, 0.0 ) != -1 )
1087 i_progress = intf_UserProgress( p_update->p_libvlc, "Downloading ...", psz_status, 0.0, 0 );
1091 while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1092 !intf_ProgressIsCancelled( p_update->p_libvlc, i_progress ) )
1094 fwrite( p_buffer, i_read, 1, p_file );
1096 l_downloaded += i_read;
1097 psz_downloaded = size_str( l_downloaded );
1098 if( asprintf( &psz_status, "%s\nDonwloading... %s/%s %.1f%% done", p_update->release.psz_url,
1099 psz_size, psz_downloaded, 100.0*(float)l_downloaded/(float)l_size ) != -1 )
1101 intf_ProgressUpdate( p_update->p_libvlc, i_progress, psz_status, 10.0, 0 );
1104 free( psz_downloaded );
1108 /* If the user cancelled the download */
1109 if( !intf_ProgressIsCancelled( p_update->p_libvlc, i_progress ) )
1111 if( asprintf( &psz_status, "%s\nDone %s (100.0%%)", p_update->release.psz_url, psz_size ) != -1 )
1113 intf_ProgressUpdate( p_update->p_libvlc, i_progress, psz_status, 100.0, 0 );
1120 if( intf_ProgressIsCancelled( p_update->p_libvlc, i_progress ) )
1121 remove( psz_destfile );
1123 free( psz_destfile );
1127 free( psz_destdir );
1128 stream_Delete( p_stream );