+static void ReadUnusedBytesFromFile(FILE *file, unsigned long bytes)
+{
+ while (bytes--)
+ fgetc(file);
+}
+
+static void WriteUnusedBytesToFile(FILE *file, unsigned long bytes)
+{
+ while (bytes--)
+ fputc(0, file);
+}
+
+static void ReadChunk_VERS(FILE *file, int *file_version, int *game_version)
+{
+ int file_version_major, file_version_minor, file_version_patch;
+ int game_version_major, game_version_minor, game_version_patch;
+
+ file_version_major = fgetc(file);
+ file_version_minor = fgetc(file);
+ file_version_patch = fgetc(file);
+ fgetc(file); /* not used */
+
+ game_version_major = fgetc(file);
+ game_version_minor = fgetc(file);
+ game_version_patch = fgetc(file);
+ fgetc(file); /* not used */
+
+ *file_version = VERSION_IDENT(file_version_major,
+ file_version_minor,
+ file_version_patch);
+
+ *game_version = VERSION_IDENT(game_version_major,
+ game_version_minor,
+ game_version_patch);
+}
+
+static void WriteChunk_VERS(FILE *file, int file_version, int game_version)
+{
+ int file_version_major = VERSION_MAJOR(file_version);
+ int file_version_minor = VERSION_MINOR(file_version);
+ int file_version_patch = VERSION_PATCH(file_version);
+ int game_version_major = VERSION_MAJOR(game_version);
+ int game_version_minor = VERSION_MINOR(game_version);
+ int game_version_patch = VERSION_PATCH(game_version);
+
+ fputc(file_version_major, file);
+ fputc(file_version_minor, file);
+ fputc(file_version_patch, file);
+ fputc(0, file); /* not used */
+
+ fputc(game_version_major, file);
+ fputc(game_version_minor, file);
+ fputc(game_version_patch, file);
+ fputc(0, file); /* not used */
+}
+
+static int getFileVersionFromCookieString(const char *cookie)
+{
+ const char *ptr_cookie1, *ptr_cookie2;
+ const char *pattern1 = "_FILE_VERSION_";
+ const char *pattern2 = "?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_pattern1 = strlen(pattern1);
+ const int len_pattern2 = strlen(pattern2);
+ const int len_pattern = len_pattern1 + len_pattern2;
+ int version_major, version_minor;
+
+ if (len_cookie <= len_pattern)
+ return -1;
+
+ ptr_cookie1 = &cookie[len_cookie - len_pattern];
+ ptr_cookie2 = &cookie[len_cookie - len_pattern2];
+
+ if (strncmp(ptr_cookie1, pattern1, len_pattern1) != 0)
+ return -1;
+
+ if (ptr_cookie2[0] < '0' || ptr_cookie2[0] > '9' ||
+ ptr_cookie2[1] != '.' ||
+ ptr_cookie2[2] < '0' || ptr_cookie2[2] > '9')
+ return -1;
+
+ version_major = ptr_cookie2[0] - '0';
+ version_minor = ptr_cookie2[2] - '0';
+
+ return VERSION_IDENT(version_major, version_minor, 0);
+}
+
+boolean checkCookieString(const char *cookie, const char *template)
+{
+ const char *pattern = "_FILE_VERSION_?.?";
+ const int len_cookie = strlen(cookie);
+ const int len_template = strlen(template);
+ const int len_pattern = strlen(pattern);
+
+ if (len_cookie != len_template)
+ return FALSE;
+
+ if (strncmp(cookie, template, len_cookie - len_pattern) != 0)
+ return FALSE;
+
+ return TRUE;
+}
+