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 *****************************************************************************/
44 #include <vlc_update.h>
46 #include <vlc_stream.h>
47 #include <vlc_interface.h>
49 /*****************************************************************************
51 *****************************************************************************/
54 * Here is the format of these "status files" :
55 * First line is the last version: "X.Y.Ze" where:
56 * * X is the major number
57 * * Y is the minor number
58 * * Z is the revision number
59 * * e is an OPTIONAL extra letter
60 * * AKA "0.8.6d" or "0.9.0"
61 * Second line is an url to the last binary
62 * Third line is a description of the update (it MAY be extended to several lines, but for now it is only one line)
65 #if defined( UNDER_CE )
66 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
67 #elif defined( WIN32 )
68 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
69 #elif defined( __APPLE__ )
70 # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
71 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
73 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
75 #elif defined( SYS_BEOS )
76 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
78 # define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status"
81 #define STRDUP( a ) ( a ? strdup( a ) : NULL )
84 /*****************************************************************************
86 *****************************************************************************/
87 static void EmptyRelease( update_t *p_update );
88 static void GetUpdateFile( update_t *p_update );
89 static int CompareReleases( const struct update_release_t *p1,
90 const struct update_release_t *p2 );
91 static char * size_str( long int l_size );
94 /*****************************************************************************
96 *****************************************************************************/
98 #define packet_type( c ) ( ( c & 0x3c ) >> 2 ) /* 0x3C = 00111100 */
99 #define packet_header_len( c ) ( ( c & 0x03 ) + 1 ) /* number of bytes in a packet header */
101 static inline int scalar_number( uint8_t *p, int header_len )
103 if( header_len == 1 )
105 else if( header_len == 2 )
106 return( (p[0] << 8) + p[1] );
107 else if( header_len == 4 )
108 return( (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3] );
113 /* number of data bytes in a MPI */
114 #define mpi_len( mpi ) ( ( scalar_number( mpi, 2 ) + 7 ) / 8 )
117 * fill a public_key_packet_t structure from public key packet data
118 * verify that it is a version 4 public key packet, using DSA
120 static int parse_public_key_packet( public_key_packet_t *p_key, uint8_t *p_buf,
121 size_t i_packet_len )
123 if( i_packet_len != 418 )
126 p_key->version = *p_buf++;
127 if( p_key->version != 4 )
130 /* warn when timestamp is > date ? */
131 memcpy( p_key->timestamp, p_buf, 4 ); p_buf += 4;
133 p_key->algo = *p_buf++;
134 if( p_key->algo != PUBLIC_KEY_ALGO_DSA )
137 memcpy( p_key->p, p_buf, 2+128 ); p_buf += 2+128;
138 if( mpi_len( p_key->p ) != 128 )
141 memcpy( p_key->q, p_buf, 2+20 ); p_buf += 2+20;
142 if( mpi_len( p_key->q ) != 20 )
145 memcpy( p_key->g, p_buf, 2+128 ); p_buf += 2+128;
146 if( mpi_len( p_key->g ) != 128 )
149 memcpy( p_key->y, p_buf, 2+128 ); p_buf += 2+128;
150 if( mpi_len( p_key->y ) != 128 )
157 * fill a signature_packet_v4_t from signature packet data
158 * verify that it was used with a DSA public key, using SHA-1 digest
160 static int parse_signature_v4_packet( signature_packet_v4_t *p_sig,
161 uint8_t *p_buf, size_t i_sig_len )
166 p_sig->version = *p_buf++;
167 if( p_sig->version != 4 )
170 p_sig->type = *p_buf++;
171 if( p_sig->type < GENERIC_KEY_SIGNATURE ||
172 p_sig->type > POSITIVE_KEY_SIGNATURE )
175 p_sig->public_key_algo = *p_buf++;
176 if( p_sig->public_key_algo != PUBLIC_KEY_ALGO_DSA )
179 p_sig->digest_algo = *p_buf++;
180 if( p_sig->digest_algo != DIGEST_ALGO_SHA1 )
183 memcpy( p_sig->hashed_data_len, p_buf, 2 ); p_buf += 2;
186 size_t i_hashed_data_len = scalar_number( p_sig->hashed_data_len, 2 );
187 i_pos += i_hashed_data_len;
188 if( i_pos > i_sig_len - 48 ) /* r & s are 44 bytes in total,
189 * + the unhashed data length (2 bytes)
190 * + the hash verification (2 bytes) */
193 p_sig->hashed_data = (uint8_t*) malloc( i_hashed_data_len );
194 if( !p_sig->hashed_data )
196 memcpy( p_sig->hashed_data, p_buf, i_hashed_data_len );
197 p_buf += i_hashed_data_len;
199 memcpy( p_sig->unhashed_data_len, p_buf, 2 ); p_buf += 2;
201 size_t i_unhashed_data_len = scalar_number( p_sig->unhashed_data_len, 2 );
202 i_pos += 2 + i_unhashed_data_len;
203 if( i_pos != i_sig_len - 46 )
205 free( p_sig->hashed_data );
209 p_sig->unhashed_data = (uint8_t*) malloc( i_unhashed_data_len );
210 if( !p_sig->unhashed_data )
212 free( p_sig->hashed_data );
215 memcpy( p_sig->unhashed_data, p_buf, i_unhashed_data_len );
216 p_buf += i_unhashed_data_len;
218 memcpy( p_sig->hash_verification, p_buf, 2 ); p_buf += 2;
220 memcpy( p_sig->r, p_buf, 22 ); p_buf += 22;
221 if( mpi_len( p_sig->r ) != 20 )
223 free( p_sig->hashed_data );
224 free( p_sig->unhashed_data );
228 memcpy( p_sig->s, p_buf, 22 );
229 if( mpi_len( p_sig->s ) != 20 )
231 free( p_sig->hashed_data );
232 free( p_sig->unhashed_data );
240 * crc_octets() was lamely copied from rfc 2440
241 * Copyright (C) The Internet Society (1998). All Rights Reserved.
243 #define CRC24_INIT 0xB704CEL
244 #define CRC24_POLY 0x1864CFBL
246 static long crc_octets( uint8_t *octets, size_t len )
248 long crc = CRC24_INIT;
252 crc ^= (*octets++) << 16;
253 for (i = 0; i < 8; i++)
260 return crc & 0xFFFFFFL;
264 * Transform an armored document in binary format
265 * Used on public keys and signatures
267 static int pgp_unarmor( char *p_ibuf, size_t i_ibuf_len,
268 uint8_t *p_obuf, size_t i_obuf_len )
270 char *p_ipos = p_ibuf;
271 uint8_t *p_opos = p_obuf;
274 int i_header_skipped = 0;
276 while( !i_end && p_ipos < p_ibuf + i_ibuf_len )
278 if( *p_ipos == '\r' || *p_ipos == '\n' )
284 size_t i_line_len = strcspn( p_ipos, "\r\n" );
285 if( i_line_len == 0 )
288 if( !i_header_skipped )
290 if( !strncmp( p_ipos, "-----BEGIN PGP", 14 ) )
291 i_header_skipped = 1;
293 p_ipos += i_line_len + 1;
297 if( !strncmp( p_ipos, "Version:", 8 ) )
299 p_ipos += i_line_len + 1;
303 if( p_ipos[i_line_len - 1] == '=' )
306 p_ipos[i_line_len - 1] = '\0';
309 p_ipos[i_line_len] = '\0';
311 p_opos += vlc_b64_decode_binary_to_buffer( p_opos,
312 p_obuf - p_opos + i_obuf_len,
315 p_ipos += i_line_len + 1;
318 /* XXX: the CRC is OPTIONAL, really require it ? */
319 if( p_ipos + 5 > p_ibuf + i_ibuf_len || *p_ipos++ != '=' )
323 if( vlc_b64_decode_binary_to_buffer( p_crc, 3, p_ipos ) != 3 )
326 long l_crc = crc_octets( p_obuf, p_opos - p_obuf );
327 long l_crc2 = ( 0 << 24 ) + ( p_crc[0] << 16 ) + ( p_crc[1] << 8 ) + p_crc[2];
329 return l_crc2 == l_crc ? p_opos - p_obuf : 0;
333 * Download the signature associated to a document or a binary file.
334 * We're given the file's url, we just append ".asc" to it and download
336 static int download_signature( vlc_object_t *p_this,
337 signature_packet_v3_t *p_sig, char *psz_url )
339 char *psz_sig = (char*) malloc( strlen( psz_url ) + 4 + 1 ); /* ".asc" + \0 */
343 strcpy( psz_sig, psz_url );
344 strcat( psz_sig, ".asc" );
346 stream_t *p_stream = stream_UrlNew( p_this, psz_sig );
352 int64_t i_size = stream_Size( p_stream );
355 stream_Delete( p_stream );
358 else if( i_size == 65 ) /* binary format signature */
360 int i_read = stream_Read( p_stream, p_sig, (int)i_size );
361 stream_Delete( p_stream );
362 if( i_read != i_size )
368 char *p_buf = (char*)malloc( i_size );
371 stream_Delete( p_stream );
375 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
377 stream_Delete( p_stream );
379 if( i_read != i_size )
385 int i_bytes = pgp_unarmor( p_buf, i_size, (uint8_t*)p_sig, 65 );
395 * Verify an OpenPGP signature made on some SHA-1 hash, with some DSA public key
397 static int verify_signature( vlc_object_t *p_this, uint8_t *p_r, uint8_t *p_s,
398 public_key_packet_t *p_key, uint8_t *p_hash )
400 /* the data to be verified (a SHA-1 hash) */
401 const char *hash_sexp_s = "(data(flags raw)(value %m))";
403 const char *key_sexp_s = "(public-key(dsa(p %m)(q %m)(g %m)(y %m)))";
405 const char *sig_sexp_s = "(sig-val(dsa(r %m )(s %m )))";
408 gcry_mpi_t p, q, g, y, r, s, hash;
409 p = q = g = y = r = s = hash = NULL;
410 gcry_sexp_t key_sexp, hash_sexp, sig_sexp;
411 key_sexp = hash_sexp = sig_sexp = NULL;
413 if( gcry_mpi_scan( &p, GCRYMPI_FMT_USG, p_key->p + 2, 128, NULL ) ||
414 gcry_mpi_scan( &q, GCRYMPI_FMT_USG, p_key->q + 2, 20, NULL ) ||
415 gcry_mpi_scan( &g, GCRYMPI_FMT_USG, p_key->g + 2, 128, NULL ) ||
416 gcry_mpi_scan( &y, GCRYMPI_FMT_USG, p_key->y + 2, 128, NULL ) ||
417 gcry_sexp_build( &key_sexp, &erroff, key_sexp_s, p, q, g, y ) )
420 if( gcry_mpi_scan( &r, GCRYMPI_FMT_USG, p_r + 2, 20, NULL ) ||
421 gcry_mpi_scan( &s, GCRYMPI_FMT_USG, p_s + 2, 20, NULL ) ||
422 gcry_sexp_build( &sig_sexp, &erroff, sig_sexp_s, r, s ) )
425 if( gcry_mpi_scan( &hash, GCRYMPI_FMT_USG, p_hash, 20, NULL ) ||
426 gcry_sexp_build( &hash_sexp, &erroff, hash_sexp_s, hash ) )
429 if( gcry_pk_verify( sig_sexp, hash_sexp, key_sexp ) )
435 if( p ) gcry_mpi_release( p );
436 if( q ) gcry_mpi_release( q );
437 if( g ) gcry_mpi_release( g );
438 if( y ) gcry_mpi_release( y );
439 if( r ) gcry_mpi_release( r );
440 if( s ) gcry_mpi_release( s );
441 if( hash ) gcry_mpi_release( hash );
442 if( key_sexp ) gcry_sexp_release( key_sexp );
443 if( sig_sexp ) gcry_sexp_release( sig_sexp );
444 if( hash_sexp ) gcry_sexp_release( hash_sexp );
449 * Return the long id (8 bytes) of the public key used to generate a signature
451 static uint8_t *get_issuer_from_signature_v4( signature_packet_v4_t *p_sig )
453 uint8_t *p = p_sig->unhashed_data;
454 uint8_t *max_pos = p + scalar_number( p_sig->unhashed_data_len, 2 );
458 int i_subpacket_len = *p < 192 ? *p++ :
459 *p < 255 ? ((*p++ - 192) << 8) + *p++ + 192 :
460 ((*++p) << 24) + (*++p << 16) + (*++p << 8) + *++p;
462 if( p >= max_pos - 1 )
465 if( *p == ISSUER_SUBPACKET )
468 p += i_subpacket_len;
474 * fill a public_key_t with public key data, including:
475 * * public key packet
476 * * signature packet issued by key which long id is p_sig_issuer
479 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 )
481 uint8_t *pos = (uint8_t*) p_key_data;
482 uint8_t *max_pos = pos + i_key_len;
485 #define PUBLIC_KEY_FOUND 0x01
486 #define USER_ID_FOUND 0x02
487 #define SIGNATURE_FOUND 0X04
489 uint8_t *p_key_unarmored = NULL;
491 signature_packet_v4_t sig;
493 p_key->psz_username = NULL;
494 p_key->sig.hashed_data = p_key->sig.unhashed_data = NULL;
496 if( !( *pos & 0x80 ) )
497 { /* first byte is ASCII, unarmoring */
498 p_key_unarmored = (uint8_t*)malloc( i_key_len );
499 if( !p_key_unarmored )
501 int i_len = pgp_unarmor( (char*)p_key_data, i_key_len,
502 p_key_unarmored, i_key_len );
507 pos = p_key_unarmored;
508 max_pos = pos + i_len;
511 while( pos < max_pos )
513 if( !(*pos & 0x80) || *pos & 0x40 )
516 int i_type = packet_type( *pos );
518 int i_header_len = packet_header_len( *pos++ );
519 if( pos + i_header_len > max_pos )
522 int i_packet_len = scalar_number( pos, i_header_len );
525 if( pos + i_packet_len > max_pos )
532 case PUBLIC_KEY_PACKET:
533 i_status |= PUBLIC_KEY_FOUND;
534 if( parse_public_key_packet( &p_key->key, pos, i_packet_len ) != VLC_SUCCESS )
538 case SIGNATURE_PACKET:
539 if( !p_sig_issuer || i_status & SIGNATURE_FOUND ||
540 parse_signature_v4_packet( &sig, pos, i_packet_len ) != VLC_SUCCESS )
542 p_issuer = get_issuer_from_signature_v4( &sig );
543 if( memcmp( p_issuer, p_sig_issuer, 8 ) == 0 )
545 memcpy( &p_key->sig, &sig, sizeof( signature_packet_v4_t ) );
546 i_status |= SIGNATURE_FOUND;
550 free( sig.hashed_data );
551 free( sig.unhashed_data );
556 if( p_key->psz_username ) /* save only the first User ID */
558 i_status |= USER_ID_FOUND;
559 p_key->psz_username = (uint8_t*)malloc( i_packet_len + 1);
560 if( !p_key->psz_username )
563 memcpy( p_key->psz_username, pos, i_packet_len );
564 p_key->psz_username[i_packet_len] = '\0';
572 free( p_key_unarmored );
574 if( !( i_status & ( PUBLIC_KEY_FOUND + USER_ID_FOUND ) ) )
577 if( p_sig_issuer && !( i_status & SIGNATURE_FOUND ) )
583 free( p_key->sig.hashed_data );
584 free( p_key->sig.unhashed_data );
585 free( p_key->psz_username );
586 free( p_key_unarmored );
591 * return a sha1 hash of a file
593 static uint8_t *hash_sha1_from_file( const char *psz_file,
594 signature_packet_v3_t *p_sig )
596 FILE *f = utf8_fopen( psz_file, "r" );
600 uint8_t buffer[4096]; //FIXME
603 if( gcry_md_open( &hd, GCRY_MD_SHA1, 0 ) )
610 while( ( i_read = fread( buffer, 1, sizeof(buffer), f ) ) > 0 )
611 gcry_md_write( hd, buffer, i_read );
613 gcry_md_putc( hd, p_sig->type );
614 gcry_md_write( hd, &p_sig->timestamp, 4 );
619 return( (uint8_t*) gcry_md_read( hd, GCRY_MD_SHA1) );
623 * download a public key (the last one) from videolan server, and parse it
625 static public_key_t *download_key( vlc_object_t *p_this, const uint8_t *p_longid, const uint8_t *p_signature_issuer )
628 if( asprintf( &psz_url, "http://download.videolan.org/pub/keys/%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x.asc",
629 p_longid[0], p_longid[1],
630 p_longid[2], p_longid[3],
631 p_longid[4], p_longid[5],
632 p_longid[6], p_longid[7] ) == -1 )
635 stream_t *p_stream = stream_UrlNew( p_this, psz_url );
640 int64_t i_size = stream_Size( p_stream );
643 stream_Delete( p_stream );
647 uint8_t *p_buf = (uint8_t*)malloc( i_size );
650 stream_Delete( p_stream );
654 int i_read = stream_Read( p_stream, p_buf, (int)i_size );
655 stream_Delete( p_stream );
657 if( i_read != (int)i_size )
663 public_key_t *p_pkey = (public_key_t*) malloc( sizeof( public_key_t ) );
670 int i_error = parse_public_key( p_buf, i_read, p_pkey, p_signature_issuer );
673 if( i_error != VLC_SUCCESS )
683 * Generate a SHA-1 hash on a public key, to verify a signature made on that hash
684 * Note that we need the signature to compute the hash
686 static uint8_t *key_sign_hash( public_key_t *p_pkey )
688 gcry_error_t error = 0;
691 error = gcry_md_open( &hd, GCRY_MD_SHA1, 0 );
695 gcry_md_putc( hd, 0x99 );
697 gcry_md_putc( hd, (418 >> 8) & 0xff );
698 gcry_md_putc( hd, 418 & 0xff );
700 gcry_md_write( hd, (uint8_t*)&p_pkey->key, 418 );
702 gcry_md_putc( hd, 0xb4 );
704 int i_len = strlen((char*)p_pkey->psz_username);
706 gcry_md_putc( hd, (i_len << 24) & 0xff );
707 gcry_md_putc( hd, (i_len << 16) & 0xff );
708 gcry_md_putc( hd, (i_len << 8) & 0xff );
709 gcry_md_putc( hd, (i_len) & 0xff );
711 gcry_md_write( hd, p_pkey->psz_username, i_len );
713 size_t i_hashed_data_len = scalar_number( p_pkey->sig.hashed_data_len, 2 );
715 gcry_md_putc( hd, p_pkey->sig.version );
716 gcry_md_putc( hd, p_pkey->sig.type );
717 gcry_md_putc( hd, p_pkey->sig.public_key_algo );
718 gcry_md_putc( hd, p_pkey->sig.digest_algo );
719 gcry_md_write( hd, p_pkey->sig.hashed_data_len, 2 );
720 gcry_md_write( hd, p_pkey->sig.hashed_data, i_hashed_data_len );
722 gcry_md_putc( hd, 0x04 );
723 gcry_md_putc( hd, 0xff );
725 i_hashed_data_len += 6; /* hashed data + 6 bytes header */
727 gcry_md_putc( hd, (i_hashed_data_len << 24) & 0xff);
728 gcry_md_putc( hd, (i_hashed_data_len << 16) &0xff );
729 gcry_md_putc( hd, (i_hashed_data_len << 8) & 0xff );
730 gcry_md_putc( hd, (i_hashed_data_len) & 0xff );
734 uint8_t *p_hash = gcry_md_read( hd, GCRY_MD_SHA1);
736 if( p_hash[0] != p_pkey->sig.hash_verification[0] ||
737 p_hash[1] != p_pkey->sig.hash_verification[1] )
747 /*****************************************************************************
749 *****************************************************************************/
752 * Create a new update VLC struct
754 * \param p_this the calling vlc_object
755 * \return pointer to new update_t or NULL
757 update_t *__update_New( vlc_object_t *p_this )
761 if( p_this == NULL ) return NULL;
763 p_update = (update_t *)malloc( sizeof( update_t ) );
764 if( !p_update ) return NULL;
766 vlc_mutex_init( p_this, &p_update->lock );
768 p_update->p_libvlc = p_this->p_libvlc;
770 p_update->release.psz_url = NULL;
771 p_update->release.psz_desc = NULL;
773 var_Create( p_this->p_libvlc, "update-notify", VLC_VAR_INTEGER |
780 * Delete an update_t struct
782 * \param p_update update_t* pointer
785 void update_Delete( update_t *p_update )
789 vlc_mutex_destroy( &p_update->lock );
791 var_Destroy( p_update->p_libvlc, "update-notify" );
793 FREENULL( p_update->release.psz_url );
794 FREENULL( p_update->release.psz_desc );
800 * Empty the release struct
802 * \param p_update update_t* pointer
805 static void EmptyRelease( update_t *p_update )
807 p_update->release.i_major = 0;
808 p_update->release.i_minor = 0;
809 p_update->release.i_revision = 0;
811 FREENULL( p_update->release.psz_url );
812 FREENULL( p_update->release.psz_desc );
816 * Get the update file and parse it
817 * *p_update has to be locked when calling this function
819 * \param p_update pointer to update struct
822 static void GetUpdateFile( update_t *p_update )
824 stream_t *p_stream = NULL;
829 char *psz_line = NULL;
831 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
834 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
835 UPDATE_VLC_STATUS_URL );
839 /* Try to read three lines */
840 if( !( psz_line = stream_ReadLine( p_stream ) ) )
842 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
843 UPDATE_VLC_STATUS_URL );
847 /* first line : version number */
848 p_update->release.extra = 0;
849 switch( sscanf( psz_line, "%i.%i.%i%c", &i_major, &i_minor, &i_revision, &extra ) )
852 p_update->release.extra = extra;
854 p_update->release.i_major = i_major;
855 p_update->release.i_minor = i_minor;
856 p_update->release.i_revision = i_revision;
859 msg_Err( p_update->p_libvlc, "Update version false formated" );
864 /* Second line : URL */
865 if( !( psz_line = stream_ReadLine( p_stream ) ) )
867 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
868 UPDATE_VLC_STATUS_URL );
871 p_update->release.psz_url = psz_line;
874 /* Third line : description */
875 if( !( psz_line = stream_ReadLine( p_stream ) ) )
877 msg_Err( p_update->p_libvlc, "Update file %s is corrupted : description missing",
878 UPDATE_VLC_STATUS_URL );
881 p_update->release.psz_desc = psz_line;
885 stream_Delete( p_stream );
890 * Struct to launch the check in an other thread
896 } update_check_thread_t;
898 void update_CheckReal( update_check_thread_t *p_uct );
903 * \param p_update pointer to update struct
906 void update_Check( update_t *p_update )
910 update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
911 sizeof( update_check_thread_t ) );
912 p_uct->p_update = p_update;
914 vlc_thread_create( p_uct, "check for update", update_CheckReal,
915 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
918 void update_CheckReal( update_check_thread_t *p_uct )
920 vlc_mutex_lock( &p_uct->p_update->lock );
922 EmptyRelease( p_uct->p_update );
923 GetUpdateFile( p_uct->p_update );
925 vlc_mutex_unlock( &p_uct->p_update->lock );
927 var_TriggerCallback( p_uct->p_update->p_libvlc, "update-notify" );
931 * Compare two release numbers
933 * \param p1 first release
934 * \param p2 second release
935 * \return UpdateReleaseStatus(Older|Equal|Newer)
937 static int CompareReleases( const struct update_release_t *p1,
938 const struct update_release_t *p2 )
941 d = ( p1->i_major << 24 ) + ( p1->i_minor << 16 ) + ( p1->i_revision << 8 )
942 - ( p2->i_major << 24 ) - ( p2->i_minor << 16 ) - ( p2->i_revision << 8 )
943 + ( p1->extra ) - ( p2->extra );
946 return UpdateReleaseStatusOlder;
948 return UpdateReleaseStatusEqual;
950 return UpdateReleaseStatusNewer;
954 * Compare a given release's version number to the current VLC's one
956 * \param p_update structure
957 * \return UpdateReleaseStatus(Older|Equal|Newer)
959 int update_CompareReleaseToCurrent( update_t *p_update )
963 struct update_release_t c;
965 /* get the current version number */
966 c.i_major = *PACKAGE_VERSION_MAJOR - '0';
967 c.i_minor = *PACKAGE_VERSION_MINOR - '0';
968 c.i_revision = *PACKAGE_VERSION_REVISION - '0';
969 c.extra = *PACKAGE_VERSION_EXTRA;
971 return CompareReleases( &p_update->release, &c );
975 * Convert a long int size in bytes to a string
977 * \param l_size the size in bytes
978 * \return the size as a string
980 static char *size_str( long int l_size )
982 char *psz_tmp = NULL;
984 asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
985 else if( l_size >> 20 )
986 asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
987 else if( l_size >> 10 )
988 asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
990 asprintf( &psz_tmp, "%ld B", l_size );
996 * Struct to launch the download in a thread
1003 } update_download_thread_t;
1005 void update_DownloadReal( update_download_thread_t *p_udt );
1008 * Download the file given in the update_t
1010 * \param p_update structure
1011 * \param dir to store the download file
1014 void update_Download( update_t *p_update, char *psz_destdir )
1018 update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
1019 sizeof( update_download_thread_t ) );
1021 p_udt->p_update = p_update;
1022 p_udt->psz_destdir = STRDUP( psz_destdir );
1024 vlc_thread_create( p_udt, "download update", update_DownloadReal,
1025 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_update->p_libvlc, p_update->release.psz_url );
1051 msg_Err( p_update->p_libvlc, "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_update->p_libvlc, "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_update->p_libvlc, "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_update->p_libvlc, "Downloading ...", psz_status, 0.0, 0 );
1088 while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
1089 !intf_ProgressIsCancelled( p_update->p_libvlc, 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_update->p_libvlc, 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_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 );
1118 remove( psz_destfile );
1122 stream_Delete( p_stream );
1125 free( psz_destdir );
1126 free( psz_destfile );