X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=pak.c;h=9781c53aa2f8a9bcc3a603809cc3d48812e7c840;hp=35e605ca59c5aae11b553ecf91e6900ee4ae9ddf;hb=41a76ab91dc6f872b03cd64f51aba86578f330c0;hpb=e6c1d66c35267392527a5177a82c48737ce9831b diff --git a/pak.c b/pak.c index 35e605c..9781c53 100644 --- a/pak.c +++ b/pak.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 + * Copyright (C) 2013, 2014, 2015 * Dale Weiler * * Permission is hereby granted, free of charge, to any person obtaining a copy of @@ -124,7 +124,7 @@ static void pak_tree_build(const char *entry) { typedef struct { pak_directory_t *directories; pak_header_t header; - FILE *handle; + fs_file_t *handle; bool insert; } pak_file_t; @@ -145,7 +145,10 @@ static pak_file_t *pak_open_read(const char *file) { memset (&pak->header, 0, sizeof(pak_header_t)); fs_file_read (&pak->header, sizeof(pak_header_t), 1, pak->handle); - util_endianswap(&pak->header, 1, sizeof(pak_header_t)); + + util_endianswap(&pak->header.magic, 1, sizeof(pak->header.magic)); + util_endianswap(&pak->header.diroff, 1, sizeof(pak->header.diroff)); + util_endianswap(&pak->header.dirlen, 1, sizeof(pak->header.dirlen)); /* * Every PAK file has "PACK" stored as FOURCC data in the @@ -162,7 +165,7 @@ static pak_file_t *pak_open_read(const char *file) { * Time to read in the directory handles and prepare the directories * vector. We're going to be reading some the file inwards soon. */ - fs_file_seek(pak->handle, pak->header.diroff, SEEK_SET); + fs_file_seek(pak->handle, pak->header.diroff, FS_FILE_SEEK_SET); /* * Read in all directories from the PAK file. These are considered @@ -171,7 +174,10 @@ static pak_file_t *pak_open_read(const char *file) { for (itr = 0; itr < pak->header.dirlen / 64; itr++) { pak_directory_t dir; fs_file_read (&dir, sizeof(pak_directory_t), 1, pak->handle); - util_endianswap(&dir, 1, sizeof(pak_directory_t)); + + /* Don't translate name - it's just an array of bytes. */ + util_endianswap(&dir.pos, 1, sizeof(dir.pos)); + util_endianswap(&dir.len, 1, sizeof(dir.len)); vec_push(pak->directories, dir); } @@ -266,15 +272,14 @@ static bool pak_extract_one(pak_file_t *pak, const char *file, const char *outdi pak_directory_t *dir = NULL; unsigned char *dat = NULL; char *local = NULL; - FILE *out; + fs_file_t *out = NULL; if (!pak_exists(pak, file, &dir)) { return false; } - if (!(dat = (unsigned char *)mem_a(dir->len))) { - return false; - } + if (!(dat = (unsigned char *)mem_a(dir->len))) + goto err; /* * Generate the directory structure / tree that will be required @@ -289,28 +294,27 @@ static bool pak_extract_one(pak_file_t *pak, const char *file, const char *outdi * Now create the file, if this operation fails. Then abort * It shouldn't fail though. */ - if (!(out = fs_file_open(local, "wb"))) { - mem_d(dat); - return false; - } + if (!(out = fs_file_open(local, "wb"))) + goto err; /* free memory for directory string */ mem_d(local); /* read */ - fs_file_seek (pak->handle, dir->pos, SEEK_SET); - fs_file_read (dat, 1, dir->len, pak->handle); + if (fs_file_seek (pak->handle, dir->pos, FS_FILE_SEEK_SET) != 0) + goto err; - /* write */ + fs_file_read (dat, 1, dir->len, pak->handle); fs_file_write(dat, 1, dir->len, out); - - /* close */ fs_file_close(out); - /* free */ mem_d(dat); - return true; + +err: + if (dat) mem_d(dat); + if (out) fs_file_close(out); + return false; } static bool pak_extract_all(pak_file_t *pak, const char *dir) { @@ -334,7 +338,8 @@ static bool pak_extract_all(pak_file_t *pak, const char *dir) { static bool pak_insert_one(pak_file_t *pak, const char *file) { pak_directory_t dir; unsigned char *dat; - FILE *fp; + long len; + fs_file_t *fp; /* * We don't allow insertion on files that already exist within the @@ -352,20 +357,20 @@ static bool pak_insert_one(pak_file_t *pak, const char *file) { * the directory entry, and the actual contents of the file * to the PAK file itself. */ - fs_file_seek(fp, 0, SEEK_END); - dir.len = fs_file_tell(fp); - fs_file_seek(fp, 0, SEEK_SET); + if (fs_file_seek(fp, 0, FS_FILE_SEEK_END) != 0 || ((len = fs_file_tell(fp)) < 0)) + goto err; + if (fs_file_seek(fp, 0, FS_FILE_SEEK_SET) != 0) + goto err; + dir.len = len; dir.pos = fs_file_tell(pak->handle); /* * We're limited to 56 bytes for a file name string, that INCLUDES * the directory and '/' seperators. */ - if (strlen(file) >= 56) { - fs_file_close(fp); - return false; - } + if (strlen(file) >= 56) + goto err; util_strncpy(dir.name, file, strlen(file)); @@ -373,10 +378,8 @@ static bool pak_insert_one(pak_file_t *pak, const char *file) { * Allocate some memory for loading in the data that will be * redirected into the PAK file. */ - if (!(dat = (unsigned char *)mem_a(dir.len))) { - fs_file_close(fp); - return false; - } + if (!(dat = (unsigned char *)mem_a(dir.len))) + goto err; fs_file_read (dat, dir.len, 1, fp); fs_file_close(fp); @@ -389,13 +392,18 @@ static bool pak_insert_one(pak_file_t *pak, const char *file) { vec_push(pak->directories, dir); return true; + +err: + fs_file_close(fp); + return false; } /* * Like pak_insert_one, except this collects files in all directories * from a root directory, and inserts them all. */ -bool pak_insert_all(pak_file_t *pak, const char *dir) { +#if 0 +static bool pak_insert_all(pak_file_t *pak, const char *dir) { DIR *dp; struct dirent *dirp; @@ -415,9 +423,11 @@ bool pak_insert_all(pak_file_t *pak, const char *dir) { fs_dir_close(dp); return true; } +#endif /*!if 0 renable when ready to use */ static bool pak_close(pak_file_t *pak) { size_t itr; + long tell; if (!pak) return false; @@ -427,19 +437,24 @@ static bool pak_close(pak_file_t *pak) { * our directory entries at the end of the file. */ if (pak->insert) { + if ((tell = fs_file_tell(pak->handle)) != 0) + goto err; + pak->header.dirlen = vec_size(pak->directories) * 64; - pak->header.diroff = ftell(pak->handle); + pak->header.diroff = tell; /* patch header */ - fs_file_seek (pak->handle, 0, SEEK_SET); + if (fs_file_seek (pak->handle, 0, FS_FILE_SEEK_SET) != 0) + goto err; + fs_file_write(&(pak->header), sizeof(pak_header_t), 1, pak->handle); /* write directories */ - fs_file_seek (pak->handle, pak->header.diroff, SEEK_SET); + if (fs_file_seek (pak->handle, pak->header.diroff, FS_FILE_SEEK_SET) != 0) + goto err; - for (itr = 0; itr < vec_size(pak->directories); itr++) { + for (itr = 0; itr < vec_size(pak->directories); itr++) fs_file_write(&(pak->directories[itr]), sizeof(pak_directory_t), 1, pak->handle); - } } vec_free (pak->directories); @@ -447,6 +462,13 @@ static bool pak_close(pak_file_t *pak) { mem_d (pak); return true; + +err: + vec_free (pak->directories); + fs_file_close(pak->handle); + mem_d (pak); + + return false; } /* @@ -479,6 +501,7 @@ static bool parsecmd(const char *optname, int *argc_, char ***argv_, char **out, return true; } +#include int main(int argc, char **argv) { bool extract = true; char *redirout = (char*)stdout;