OSDN Git Service

Reverts r1475 until it plays nice with Xcode + make
[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 #define HB_URL   "handbrake.fr"
10 #define HB_QUERY "GET /appcast.xml HTTP/1.0\r\nHost: " HB_URL "\r\n\r\n"
11
12 typedef struct
13 {
14     int  * build;
15     char * version;
16
17 } hb_update_t;
18
19 static void UpdateFunc( void * );
20
21 hb_thread_t * hb_update_init( int * build, char * version )
22 {
23     hb_update_t * data = calloc( sizeof( hb_update_t ), 1 );
24     data->build   = build;
25     data->version = version;
26
27     return hb_thread_init( "update", UpdateFunc, data,
28                            HB_NORMAL_PRIORITY );
29 }
30
31 static void UpdateFunc( void * _data )
32 {
33     hb_update_t * data = (hb_update_t *) _data;
34
35     hb_net_t * net;
36     int        ret;
37     char       buf[1024];
38     char     * cur, * end, * p;
39     int        size;
40     int        stable, unstable;
41     char       stable_str[16], unstable_str[16];
42     int        i;
43
44     if( !( net = hb_net_open( HB_URL, 80 ) ) )
45     {
46         goto error;
47     }
48
49     if( hb_net_send( net, HB_QUERY ) < 0 )
50     {
51         hb_net_close( &net );
52         goto error;
53     }
54
55     size = 0;
56     memset( buf, 0, 1024 );
57     for( ;; )
58     {
59         ret = hb_net_recv( net, &buf[size], sizeof( buf ) - size );
60         if( ret < 1 )
61         {
62             hb_net_close( &net );
63             break;
64         }
65         size += ret;
66     }
67
68     cur = buf;
69     end = &buf[sizeof( buf )];
70
71     /* Make sure we got it */
72     cur += 9;
73     if( size < 15 || strncmp( cur, "200 OK", 6 ) )
74     {
75         /* Something went wrong */
76         goto error;
77     }
78     cur += 6;
79
80     /* Find the end of the headers and the beginning of the content */
81     for( ; &cur[3] < end; cur++ )
82     {
83         if( cur[0] == '\r' && cur[1] == '\n' &&
84             cur[2] == '\r' && cur[3] == '\n' )
85         {
86             cur += 4;
87             break;
88         }
89     }
90
91     if( cur >= end )
92     {
93         goto error;
94     }
95         
96         
97         // FIND THE STABLE VERSION INFORMATION ###################################################
98         
99         /*
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"
102          */
103
104      for(i=0 ; &cur[3] < end; i++, cur++ )
105      {
106         if( cur[0] == '<' && cur[1] == 'c' && cur[2] == 'l' && cur[3] == 'i' )
107          {
108             cur += 1;
109             break;
110          }
111                  
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 ))
114                  {
115                         goto error;
116                  }
117      }
118          
119         if( cur >= end )
120     {
121         goto error;
122     }
123         
124         /*
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
127          */
128     cur += 11;
129         
130         if( cur >= end )
131     {
132         goto error;
133     }
134         
135         stable = strtol( cur, &cur, 10 );
136         
137         if( cur >= end )
138     {
139         goto error;
140     }
141         
142         /*
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.
145          */
146         cur += 2;
147         
148         if( cur >= end )
149     {
150         goto error;
151     }
152         memset( stable_str, 0, sizeof( stable_str ) );
153         for( i = 0;   i < sizeof( stable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
154         {
155                 stable_str[i] = *cur;
156                 
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 ))
159                 {
160                         goto error;
161                 }
162         }
163         
164         if( cur >= end )
165     {
166         goto error;
167     }
168         
169     hb_log( "latest stable: %s, build %d", stable_str, stable );
170         
171         // END OF STABLE INFO ###################################################
172         
173
174         // FIND THE UNSTABLE INFO ###############################################
175         /*
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"
178          */
179
180      for(i =0 ; &cur[3] < end; i++, cur++ )
181      {
182         if( cur[0] == '<' && cur[1] == 'c' && cur[2] == 'l' && cur[3] == 'i' )
183          {
184             cur += 1;
185             break;
186          }
187                  
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 ))
190                  {
191                         goto error;
192                  }
193      }
194          
195         /*
196          * Now we need to handle the unstable build information
197          * Unstable build number is 29 Characters after the last position used.
198          */
199          
200          cur += 13;
201              
202         if( cur >= end )
203     {
204         goto error;
205     } 
206         
207          unstable = strtol( cur, &p, 10 );
208          
209         if( cur >= end )
210     {
211         goto error;
212     }
213         
214         /*
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.
218          */
219         cur += 12;
220         
221         if( cur >= end )
222     {
223         goto error;
224     }
225         
226         memset( unstable_str, 0, sizeof( unstable_str ) );
227         for( i = 0;   i < sizeof( unstable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
228         {
229                 unstable_str[i] = *cur;
230                 
231                 // If the version number is greater than 7 chars or the end is reached, something went wrong.
232                 if (( i > 7) || ( cur >= end ))
233                 {
234                         goto error;
235                 }
236         }
237
238     hb_log( "latest unstable: %s, build %d", unstable_str, unstable );
239         
240         // END OF UNSTABLE INFO ###################################################
241
242         /*
243          * Handle the update checking as normal.
244          * This code is unchanged.
245          */
246     if( HB_BUILD % 100 )
247     {
248         /* We are runnning an unstable build */
249         if( unstable > HB_BUILD )
250         {
251             memcpy( data->version, unstable_str, sizeof( unstable_str ) );
252             *(data->build) = unstable;
253         }
254     }
255     else
256     {
257         /* We are runnning an stable build */
258         if( stable > HB_BUILD )
259         {
260             memcpy( data->version, stable_str, sizeof( stable_str ) );
261             *(data->build) = stable;
262         }
263     }
264
265 error:
266     free( data );
267     return;
268 }
269