Code: Select all
Index: update.c[quote]
===================================================================
--- update.c (revision 1402)
+++ update.c (working copy)
@@ -7,7 +7,7 @@
#include "hb.h"
#define HB_URL "handbrake.fr"
-#define HB_QUERY "GET /LATEST HTTP/1.0\r\nHost: " HB_URL "\r\n\r\n"
+#define HB_QUERY "GET /appcast.xml HTTP/1.0\r\nHost: " HB_URL "\r\n\r\n"
typedef struct
{
@@ -92,45 +92,157 @@
{
goto error;
}
+ [/quote]
+
+ // FIND THE STABLE VERSION INFORMATION ###################################################
+
+ /*
+ * Find the <cli-stable> tag
+ * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
+ */
- stable = strtol( cur, &p, 10 );
- if( cur == p )
+ for(i=0 ; &cur[3] < end; i++, cur++ )
+ {
+ if( cur[0] == '<' && cur[1] == 'c' && cur[2] == 'l' && cur[3] == 'i' )
+ {
+ cur += 1;
+ break;
+ }
+
+ // If the CLI tag has not been found in the first 510 characters, or the end is reached, something bad happened.
+ if (( i > 510) || ( cur >= end ))
+ {
+ goto error;
+ }
+ }
+
+ if( cur >= end )
{
goto error;
}
- cur = p + 1;
- memset( stable_str, 0, sizeof( stable_str ) );
- for( i = 0;
- i < sizeof( stable_str ) - 1 && cur < end && *cur != '\n';
- i++, cur++ )
+
+ /*
+ * Ok, The above code didn't position cur, it only found <cli so we need to shift cur along 11 places.
+ * After which, the next 10 characters are the build number
+ */
+ cur += 11;
+
+ if( cur >= end )
{
- stable_str[i] = *cur;
+ goto error;
}
-
- hb_log( "latest stable: %s, build %d", stable_str, stable );
-
- cur++;
- if( cur >= end )
+
+ stable = strtol( cur, &cur, 10 );
+
+ if( cur >= end )
{
goto error;
}
+
+ /*
+ * The Version number is 2 places after the build, so shift cur, 2 places.
+ * Get all the characters in cur until the point where " is found.
+ */
+ cur += 2;
+
+ if( cur >= end )
+ {
+ goto error;
+ }
+ memset( stable_str, 0, sizeof( stable_str ) );
+ for( i = 0; i < sizeof( stable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
+ {
+ stable_str[i] = *cur;
+
+ // If the version number is longer than 7 characters, or the end is reached, something has gone wrong.
+ if (( i > 7) || ( cur >= end ))
+ {
+ goto error;
+ }
+ }
+
+ if( cur >= end )
+ {
+ goto error;
+ }
+
+ hb_log( "latest stable: %s, build %d", stable_str, stable );
+
+ // END OF STABLE INFO ###################################################
+
- unstable = strtol( cur, &p, 10 );
- if( cur == p )
+ // FIND THE UNSTABLE INFO ###############################################
+ /*
+ * Find the <cli-unstable> tag
+ * Scan though each character of the buffer until we find that the first 4 characters of "cur" are "<cli"
+ */
+
+ for(i =0 ; &cur[3] < end; i++, cur++ )
+ {
+ if( cur[0] == '<' && cur[1] == 'c' && cur[2] == 'l' && cur[3] == 'i' )
+ {
+ cur += 1;
+ break;
+ }
+
+ // If the second CLI tag is more than 25 characters forward, or the end is reached, something went wrong.
+ if (( i > 25) || ( cur >= end ))
+ {
+ goto error;
+ }
+ }
+
+ /*
+ * Now we need to handle the unstable build information
+ * Unstable build number is 29 Characters after the last position used.
+ */
+
+ cur += 13;
+
+ if( cur >= end )
{
goto error;
+ }
+
+ unstable = strtol( cur, &p, 10 );
+
+ if( cur >= end )
+ {
+ goto error;
}
- cur = p + 1;
- memset( unstable_str, 0, sizeof( unstable_str ) );
- for( i = 0;
- i < sizeof( unstable_str ) - 1 && cur < end && *cur != '\n';
- i++, cur++ )
+
+ /*
+ * Now we need to get the unstable version number.
+ * First move the cur pointer 12 places.
+ * Then iterate over cur until " is found. Thats the end of the version number.
+ */
+ cur += 12;
+
+ if( cur >= end )
{
- unstable_str[i] = *cur;
+ goto error;
}
+
+ memset( unstable_str, 0, sizeof( unstable_str ) );
+ for( i = 0; i < sizeof( unstable_str ) - 1 && cur < end && *cur != '"'; i++, cur++ )
+ {
+ unstable_str[i] = *cur;
+
+ // If the version number is greater than 7 chars or the end is reached, something went wrong.
+ if (( i > 7) || ( cur >= end ))
+ {
+ goto error;
+ }
+ }
hb_log( "latest unstable: %s, build %d", unstable_str, unstable );
+
+ // END OF UNSTABLE INFO ###################################################
+ /*
+ * Handle the update checking as normal.
+ * This code is unchanged.
+ */
if( HB_BUILD % 100 )
{
/* We are runnning an unstable build */
- Added the if ( cur >= end ) { goto error; } lines back in.
- Also added them to the for loops + a check to make sure we didn't try to take in too many characters for a version number, which could happen if " was missing.
- Added the memset lines back in since saintdev made them sound important.
Probably good places to use strtok (or strtok_r). Using hard-coded positions isn't really the greatest idea.
True, but that's what was used previously, The file format is not going to change now so it's effort which won't make a great deal of difference. Once the first CLI tag is found, nothing will change that the searching code doesn't already handle.
A little more proper-like XML, IMO.
The reason I didn't do that, is because it's a pain to sort out my GUI to handle it. It's easier just to have the version number as a single string.
They make sure the memory contained therin is initalized to something, other than being random leftovers from whatever was there last.
Sounds important. I've re-added those lines in.
Now for eddyg's comments.
You should check that cur + 11 is not greater than end here to avoid going off into the woods.
Done. ( I think) ALong with the rest of the checks have been re-added.
Are we sure that the buffer is null terminated? Otherwise strtol could walk off of the end of it.
*shrugs*. The old update.c used strtol, so that's why I used it. I saw no reason to change what worked. I suppose I could do something like
http://www.daniweb.com/forums/post254197-3.html
but tbh, I can't really be bothered when this seems to work fine.
Also - when searching you should be able to handle not finding what you are looking for gracefully.
Yes, I've sorted this now. I've set a limit on the number of loops that can happen, and also make sure that the code hasn't reached the end of cur (if some really really weird something happens)