static pak_file_t *pak_open_read(const char *file) { pak_file_t *pak; size_t itr; if (!(pak = (pak_file_t*)mem_a(sizeof(pak_file_t)))) return NULL; if (!(pak->handle = fs_file_open(file, "rb"))) { mem_d(pak); return NULL; } pak->directories = NULL; pak->insert = false; /* read doesn't allow insert */ memset (&pak->header, 0, sizeof(pak_header_t)); fs_file_read (&pak->header, sizeof(pak_header_t), 1, pak->handle); 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 * header. If this data cannot compare (as checked here), it's * probably not a PAK file. */ if (pak->header.magic != PAK_FOURCC) { fs_file_close(pak->handle); mem_d (pak); return NULL; } /* * 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, FS_FILE_SEEK_SET); /* * Read in all directories from the PAK file. These are considered * to be the "file entries". */ for (itr = 0; itr < pak->header.dirlen / 64; itr++) { pak_directory_t dir; fs_file_read (&dir, sizeof(pak_directory_t), 1, pak->handle); /* 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); } return pak; }
static int ehandler_cat(dentry_t * entry) { if (IS_FREE(entry)) return 0; if (ATTR_DIRECTORY(entry) || ATTR_ARCHIVE(entry)) if (strncmp(entry->name, buffer+4, strlen(buffer+4)) == 0) { fs_file_read(entry, shandler_cat); ENTER(); return 1; } return 0; }
int sys_readpos(int fp, unsigned char *buf, size_t count) { if(!buf) return -EINVAL; struct file *f = file_get(fp); if(!f) return -EBADF; if(!(f->flags & _FREAD)) { file_put(f); return -EACCES; } int ret = fs_file_read(f, buf, count); file_put(f); return ret; }
/* * Extraction abilities. These work as you expect them to. */ static bool pak_extract_one(pak_file_t *pak, const char *file, const char *outdir) { pak_directory_t *dir = NULL; unsigned char *dat = NULL; char *local = NULL; FILE *out = NULL; if (!pak_exists(pak, file, &dir)) { return false; } if (!(dat = (unsigned char *)mem_a(dir->len))) goto err; /* * Generate the directory structure / tree that will be required * to store the extracted file. */ pak_tree_build(file); /* TODO portable path seperators */ util_asprintf(&local, "%s/%s", outdir, file); /* * Now create the file, if this operation fails. Then abort * It shouldn't fail though. */ if (!(out = fs_file_open(local, "wb"))) goto err; /* free memory for directory string */ mem_d(local); /* read */ if (fs_file_seek (pak->handle, dir->pos, SEEK_SET) != 0) goto err; fs_file_read (dat, 1, dir->len, pak->handle); fs_file_write(dat, 1, dir->len, out); fs_file_close(out); mem_d(dat); return true; err: if (dat) mem_d(dat); if (out) fs_file_close(out); return false; }
/* * Insertion functions (the opposite of extraction). Yes for generating * PAKs. */ static bool pak_insert_one(pak_file_t *pak, const char *file) { pak_directory_t dir; unsigned char *dat; long len; FILE *fp; /* * We don't allow insertion on files that already exist within the * pak file. Weird shit can happen if we allow that ;). We also * don't allow insertion if the pak isn't opened in write mode. */ if (!pak || !file || !pak->insert || pak_exists(pak, file, NULL)) return false; if (!(fp = fs_file_open(file, "rb"))) return false; /* * Calculate the total file length, since it will be wrote to * the directory entry, and the actual contents of the file * to the PAK file itself. */ if (fs_file_seek(fp, 0, SEEK_END) != 0 || ((len = fs_file_tell(fp)) < 0)) goto err; if (fs_file_seek(fp, 0, 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) goto err; util_strncpy(dir.name, file, strlen(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))) goto err; fs_file_read (dat, dir.len, 1, fp); fs_file_close(fp); fs_file_write(dat, dir.len, 1, pak->handle); /* * Now add the directory to the directories vector, so pak_close * can actually write it. */ vec_push(pak->directories, dir); return true; err: fs_file_close(fp); return false; }