OSDN Git Service

SunOS support fixed with new ffmpeg and x264
[handbrake-jp/handbrake-jp-git.git] / libhb / update.c
1 /* $Id: update.c,v 1.7 2005/03/26 23:04:14 titer Exp $
2
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. */
6
7 #include "hb.h"
8
9 static void UpdateFunc( void * );
10
11 typedef struct
12 {
13     int  * build;
14     char * version;
15
16 } hb_update_t;
17
18 hb_thread_t * hb_update_init( int * build, char * version )
19 {
20     hb_update_t * data = calloc( sizeof( hb_update_t ), 1 );
21     data->build   = build;
22     data->version = version;
23
24     return hb_thread_init( "update", UpdateFunc, data,
25                            HB_NORMAL_PRIORITY );
26 }
27
28
29 static void UpdateFunc( void * _data )
30 {
31
32     hb_update_t * data = (hb_update_t *) _data;
33         
34         char     * hb_query, * hb_query_two;
35         hb_net_t * net;
36     int        ret;
37     char       buf[4096];
38     char     * cur, * end;
39     int        size;
40     int        stable, unstable;
41     char       stable_str[16], unstable_str[16];
42     int        i;
43         
44     /* Setup hb_query and hb_query_two with the correct appcast file */
45     if( HB_BUILD % 100 )
46     {   
47         hb_log("Using http://handbrake.fr/appcast_unstable.xml (primary)");
48         hb_log("Using http://handbrake.fr/appcast.xml (secondary)");
49     }
50         else 
51         {
52         hb_log("Using http://handbrake.fr/appcast.xml (primary)");
53         hb_log("Using http://handbrake.fr/appcast_unstable.xml (secondary)");
54     }
55         
56         hb_query = "GET /appcast.xml HTTP/1.0\r\nHost: handbrake.fr\r\n\r\n";
57     hb_query_two = "GET /appcast_unstable.xml HTTP/1.0\r\nHost: handbrake.fr\r\n\r\n";
58
59         /* Grab the data from the web server */
60     if( !( net = hb_net_open( "handbrake.fr", 80 ) ) )
61     {
62         goto error;
63     }
64
65     if( hb_net_send( net, hb_query ) < 0 )
66     {
67         hb_log("Error: Unable to connect to server");
68         hb_net_close( &net );
69         goto error;
70     }
71
72     size = 0;
73     memset( buf, 0, 4096 );
74     for( ;; )
75     {
76         ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
77         if( ret < 1 )
78         {
79             hb_net_close( &net );
80             break;
81         }
82         size += ret;
83     }
84
85     cur = buf;
86     end = &buf[sizeof( buf )];
87         
88     /* Make sure we got it */
89     cur += 9;
90     if( size < 15 || strncmp( cur, "200 OK", 6 ) )
91     {
92         hb_log("Error: We did not get a 200 OK from the server. \n");
93         goto error;
94     }
95     cur += 6;
96
97     /* Find the end of the headers and the beginning of the content */
98     for( ; &cur[3] < end; cur++ )
99     {
100         if( cur[0] == '\r' && cur[1] == '\n' &&
101             cur[2] == '\r' && cur[3] == '\n' )
102         {
103             cur += 4;
104             break;
105         }
106     }
107
108     if( cur >= end )
109     {
110         hb_log("Error: Found the end of the buffer before the end of the HTTP header information! \n");
111         goto error;
112     }
113         
114     /*
115      * Find the <cli> tag
116      * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
117      */
118     for(i=0 ; &cur[3] < end; i++, cur++ )
119     {
120         if( cur[0] == 'c' && cur[1] == 'l' && cur[2] == 'i' && cur[3] == '>' )
121         {
122             cur += 1;
123             break;
124         }
125                  
126         /* If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.*/
127         if (( i > 768) || ( cur >= end ))
128                 {
129             hb_log("Error: Did not find the <cli> tag in the expected maximum amount of characters into the file. \n");
130             goto error;
131                 }
132     }
133          
134     if( cur >= end )
135     {
136         goto error;
137     }
138         
139     /*
140      * Ok, The above code didn't position cur, it only found <cli so we need to shift cur along 3 places.
141      * After which, the next 10 characters are the build number
142      */
143     cur += 3;
144         
145     if( cur >= end )
146     {
147         hb_log("Error: Unexpected end of buffer! Could not find the build information. \n");
148         goto error;
149     }
150         
151         /* Stable HB_BUILD */
152     stable = strtol( cur, &cur, 10 );
153                 
154     if( cur >= end )
155     {
156      hb_log("Error: Unexpected end of buffer! \n");
157         goto error;
158     }
159         
160     /*
161      * The Version number is 2 places after the build, so shift cur, 2 places.
162      * Get all the characters in cur until the point where " is found.
163      */
164     cur += 2;
165         
166     if( cur >= end )
167     {
168         hb_log("Error: Unexpected end of buffer! Could not get version number. \n");
169         goto error;
170     }
171     memset( stable_str, 0, sizeof( stable_str ) );
172     for( i = 0;   i < sizeof( stable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
173     {
174         stable_str[i] = *cur;
175                 
176         /* If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.*/
177         if (( i > 7) || ( cur >= end ))
178         {
179             hb_log("Error: Version number too long, or end of buffer reached. \n");
180             goto error;
181         }
182     }
183         
184     if( cur >= end )
185     {
186         goto error;
187     }
188         
189         /* HANDLE THE SECOND APPCAST FILE NOW */
190     /* Grab the data from the web server */
191     if( !( net = hb_net_open( "handbrake.fr", 80 ) ) )
192     {
193         goto error;
194     }
195         
196         if( hb_net_send( net, hb_query_two ) < 0 )
197     {
198         hb_log("Error: Unable to connect to server");
199         hb_net_close( &net );
200         goto error;
201     }
202
203     size = 0;
204     memset( buf, 0, 4096 );
205     for( ;; )
206     {
207         ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
208         if( ret < 1 )
209         {
210             hb_net_close( &net );
211             break;
212         }
213         size += ret;
214     }
215
216     cur = buf;
217     end = &buf[sizeof( buf )];
218         
219     /* Make sure we got it */
220     cur += 9;
221     if( size < 15 || strncmp( cur, "200 OK", 6 ) )
222     {
223         /* Something went wrong */
224         hb_log("Error: We did not get a 200 OK from the server. \n");
225         goto error;
226     }
227     cur += 6;
228
229     /* Find the end of the headers and the beginning of the content */
230     for( ; &cur[3] < end; cur++ )
231     {
232         if( cur[0] == '\r' && cur[1] == '\n' &&
233             cur[2] == '\r' && cur[3] == '\n' )
234         {
235             cur += 4;
236             break;
237         }
238     }
239
240     if( cur >= end )
241     {
242         hb_log("Error: Found the end of the buffer before the end of the HTTP header information! \n");
243         goto error;
244     }
245         
246     /*
247      * Find the <cli> tag
248      * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
249      */
250     for(i=0 ; &cur[3] < end; i++, cur++ )
251     {
252         if( cur[0] == 'c' && cur[1] == 'l' && cur[2] == 'i' && cur[3] == '>' )
253         {
254             cur += 1;
255             break;
256         }
257                  
258         // If the CLI tag has not been found in the first 768 characters, or the end is reached, something bad happened.
259         if (( i > 768) || ( cur >= end ))
260                 {
261             hb_log("Error: Did not find the <cli> tag in the expected maximum amount of characters into the file. \n");
262             goto error;
263                 }
264     }
265          
266     if( cur >= end )
267     {
268         goto error;
269     }
270         
271     /*
272      * Ok, The above code didn't position cur, it only found <cli so we need to shift cur along 3 places.
273      * After which, the next 10 characters are the build number
274      */
275     cur += 3;
276         
277     if( cur >= end )
278     {
279         hb_log("Error: Unexpected end of buffer! Could not find the build information. \n");
280         goto error;
281     }
282         
283         /* UnStable HB_BUILD */
284     unstable = strtol( cur, &cur, 10 );
285                 
286     if( cur >= end )
287     {
288      hb_log("Error: Unexpected end of buffer! \n");
289         goto error;
290     }
291         
292     /*
293      * The Version number is 2 places after the build, so shift cur, 2 places.
294      * Get all the characters in cur until the point where " is found.
295      */
296     cur += 2;
297         
298     if( cur >= end )
299     {
300         hb_log("Error: Unexpected end of buffer! Could not get version number. \n");
301         goto error;
302     }
303     memset( unstable_str, 0, sizeof( unstable_str ) );
304     for( i = 0;   i < sizeof( unstable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
305     {
306         unstable_str[i] = *cur;
307                 
308         // If the version number is longer than 7 characters, or the end is reached, something has gone wrong.
309         if (( i > 7) || ( cur >= end ))
310         {
311             hb_log("Error: Version number too long, or end of buffer reached. \n");
312             goto error;
313         }
314     }
315         
316     if( cur >= end )
317     {
318         goto error;
319     }
320         
321     /* Print the version information */
322     hb_log( "latest stable: %s, build %d", stable_str, stable );
323         hb_log( "latest unstable: %s, build %d", unstable_str, unstable );
324         
325     /* Return the build information */
326         if( HB_BUILD % 100 )
327     {
328         /* We are runnning an unstable build */
329         if( unstable > HB_BUILD )
330         {
331             memcpy( data->version, unstable_str, sizeof( unstable_str ) );
332             *(data->build) = unstable;
333         }
334     }
335     else
336     {
337         /* We are runnning an stable build */
338         if( stable > HB_BUILD )
339         {
340             memcpy( data->version, stable_str, sizeof( stable_str ) );
341             *(data->build) = stable;
342         }
343     }
344
345 error:
346     free( data );
347     return;
348 }