1 /* $Id: update.c,v 1.7 2005/03/26 23:04:14 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
9 #define HB_URL "handbrake.fr"
10 #define HB_QUERY "GET /appcast.xml HTTP/1.0\r\nHost: " HB_URL "\r\n\r\n"
19 static void UpdateFunc( void * );
21 hb_thread_t * hb_update_init( int * build, char * version )
23 hb_update_t * data = calloc( sizeof( hb_update_t ), 1 );
25 data->version = version;
27 return hb_thread_init( "update", UpdateFunc, data,
31 static void UpdateFunc( void * _data )
33 hb_update_t * data = (hb_update_t *) _data;
38 char * cur, * end, * p;
41 char stable_str[16], unstable_str[16];
44 if( !( net = hb_net_open( HB_URL, 80 ) ) )
49 if( hb_net_send( net, HB_QUERY ) < 0 )
56 memset( buf, 0, 1024 );
59 ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
69 end = &buf[sizeof( buf )];
71 /* Make sure we got it */
73 if( size < 15 || strncmp( cur, "200 OK", 6 ) )
75 /* Something went wrong */
80 /* Find the end of the headers and the beginning of the content */
81 for( ; &cur[3] < end; cur++ )
83 if( cur[0] == '\r' && cur[1] == '\n' &&
84 cur[2] == '\r' && cur[3] == '\n' )
97 // FIND THE STABLE VERSION INFORMATION ###################################################
100 * Find the <cli-stable> tag
101 * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
104 for(i=0 ; &cur[3] < end; i++, cur++ )
106 if( cur[0] == '<' && cur[1] == 'c' && cur[2] == 'l' && cur[3] == 'i' )
112 // If the CLI tag has not been found in the first 510 characters, or the end is reached, something bad happened.
113 if (( i > 510) || ( cur >= end ))
125 * Ok, The above code didn't position cur, it only found <cli so we need to shift cur along 11 places.
126 * After which, the next 10 characters are the build number
135 stable = strtol( cur, &cur, 10 );
143 * The Version number is 2 places after the build, so shift cur, 2 places.
144 * Get all the characters in cur until the point where " is found.
152 memset( stable_str, 0, sizeof( stable_str ) );
153 for( i = 0; i < sizeof( stable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
155 stable_str[i] = *cur;
157 // If the version number is longer than 7 characters, or the end is reached, something has gone wrong.
158 if (( i > 7) || ( cur >= end ))
169 hb_log( "latest stable: %s, build %d", stable_str, stable );
171 // END OF STABLE INFO ###################################################
174 // FIND THE UNSTABLE INFO ###############################################
176 * Find the <cli-unstable> tag
177 * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
180 for(i =0 ; &cur[3] < end; i++, cur++ )
182 if( cur[0] == '<' && cur[1] == 'c' && cur[2] == 'l' && cur[3] == 'i' )
188 // If the second CLI tag is more than 25 characters forward, or the end is reached, something went wrong.
189 if (( i > 25) || ( cur >= end ))
196 * Now we need to handle the unstable build information
197 * Unstable build number is 29 Characters after the last position used.
207 unstable = strtol( cur, &p, 10 );
215 * Now we need to get the unstable version number.
216 * First move the cur pointer 12 places.
217 * Then iterate over cur until " is found. Thats the end of the version number.
226 memset( unstable_str, 0, sizeof( unstable_str ) );
227 for( i = 0; i < sizeof( unstable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
229 unstable_str[i] = *cur;
231 // If the version number is greater than 7 chars or the end is reached, something went wrong.
232 if (( i > 7) || ( cur >= end ))
238 hb_log( "latest unstable: %s, build %d", unstable_str, unstable );
240 // END OF UNSTABLE INFO ###################################################
243 * Handle the update checking as normal.
244 * This code is unchanged.
248 /* We are runnning an unstable build */
249 if( unstable > HB_BUILD )
251 memcpy( data->version, unstable_str, sizeof( unstable_str ) );
252 *(data->build) = unstable;
257 /* We are runnning an stable build */
258 if( stable > HB_BUILD )
260 memcpy( data->version, stable_str, sizeof( stable_str ) );
261 *(data->build) = stable;