using Qseek on a pak sub-file should work better. this should make oggs in

paks work.
This commit is contained in:
Bill Currie 2002-09-27 04:27:19 +00:00
parent 5397ca66ca
commit 9d8d570269
7 changed files with 78 additions and 91 deletions

View file

@ -64,7 +64,6 @@ void COM_WriteBuffers (const char *filename, int count, ...);
int _COM_FOpenFile (const char *filename, QFile **gzfile, char *foundname, int zip);
int COM_FOpenFile (const char *filename, QFile **gzfile);
void COM_CloseFile (QFile *h);
int COM_filelength (QFile *f);
void COM_FileBase (const char *in, char *out);
void COM_DefaultExtension (char *path, char *extension);
const char *COM_SkipPath (const char *pathname);

View file

@ -41,6 +41,7 @@ int Qrename(const char *old, const char *new);
int Qfilesize (QFile *file);
QFile *Qopen(const char *path, const char *mode);
QFile *Qdopen(int fd, const char *mode);
QFile *Qsubopen (const char *path, int offs, int len, int zip);
void Qclose(QFile *file);
int Qread(QFile *file, void *buf, int count);
int Qwrite(QFile *file, const void *buf, int count);

View file

@ -620,7 +620,7 @@ Cmd_Exec_File (cbuf_t *cbuf, const char *path)
if (!path || !*path)
return;
if ((file = Qopen (path, "r")) != NULL) {
len = COM_filelength (file);
len = Qfilesize (file);
f = (char *) malloc (len + 1);
if (f) {
f[len] = 0;

View file

@ -151,20 +151,6 @@ COM_FileBase (const char *in, char *out)
}
}
int
COM_filelength (QFile *f)
{
int end, pos;
pos = Qtell (f);
Qseek (f, 0, SEEK_END);
end = Qtell (f);
Qseek (f, pos, SEEK_SET);
return end;
}
int
COM_FileOpenRead (char *path, QFile **hndl)
{
@ -177,7 +163,7 @@ COM_FileOpenRead (char *path, QFile **hndl)
}
*hndl = f;
return COM_filelength (f);
return Qfilesize (f);
}
QFile *
@ -228,7 +214,7 @@ LoadFile (const char *filename, void **bufferptr)
QFile *f;
f = COM_SafeOpenRead (filename);
length = COM_filelength (f);
length = Qfilesize (f);
buffer = malloc (length + 1);
SYS_CHECKMEM (buffer);
((char *) buffer)[length] = 0;
@ -377,41 +363,21 @@ COM_CopyFile (char *netpath, char *cachepath)
QFile *
COM_OpenRead (const char *path, int offs, int len, int zip)
{
unsigned char id[2], len_bytes[4];
int fd = open (path, O_RDONLY);
QFile *file;
if (fd == -1) {
if (offs < 0 || len < 0)
file = Qopen (path, zip ? "rbz" : "rb");
else
file = Qsubopen (path, offs, len, zip);
if (!file) {
Sys_Error ("Couldn't open %s", path);
return 0;
}
#ifdef WIN32
setmode (fd, O_BINARY);
#endif
if (offs < 0 || len < 0) {
// normal file
offs = 0;
len = lseek (fd, 0, SEEK_END);
lseek (fd, 0, SEEK_SET);
}
lseek (fd, offs, SEEK_SET);
if (zip) {
read (fd, id, 2);
if (id[0] == 0x1f && id[1] == 0x8b) {
lseek (fd, offs + len - 4, SEEK_SET);
read (fd, len_bytes, 4);
len = ((len_bytes[3] << 24)
| (len_bytes[2] << 16)
| (len_bytes[1] << 8)
| (len_bytes[0]));
}
}
lseek (fd, offs, SEEK_SET);
com_filesize = len;
if (zip)
return Qdopen (fd, "rbz");
else
return Qdopen (fd, "rb");
com_filesize = Qfilesize (file);
return file;
}
char *

View file

@ -78,6 +78,7 @@ struct QFile_s {
gzFile *gzfile;
#endif
off_t size;
off_t start;
};
@ -133,6 +134,35 @@ Qfilesize (QFile *file)
return file->size;
}
static int
check_file (int fd, int offs, int len, int *zip)
{
unsigned char id[2], len_bytes[4];
if (offs < 0 || len < 0) {
// normal file
offs = 0;
len = lseek (fd, 0, SEEK_END);
lseek (fd, 0, SEEK_SET);
}
if (*zip) {
lseek (fd, offs, SEEK_SET);
read (fd, id, 2);
if (id[0] == 0x1f && id[1] == 0x8b) {
lseek (fd, offs + len - 4, SEEK_SET);
read (fd, len_bytes, 4);
len = ((len_bytes[3] << 24)
| (len_bytes[2] << 16)
| (len_bytes[1] << 8)
| (len_bytes[0]));
} else {
*zip = 0;
}
}
lseek (fd, offs, SEEK_SET);
return len;
}
QFile *
Qopen (const char *path, const char *mode)
{
@ -166,21 +196,7 @@ Qopen (const char *path, const char *mode)
if (reading) {
int fd = open (path, O_RDONLY);
if (fd != -1) {
size = lseek (fd, 0, SEEK_END);
lseek (fd, 0, SEEK_SET);
#ifdef HAVE_ZLIB
if (zip) {
byte id[2];
read (fd, id, 2);
if (id[0] == 0x1f && id[1] == 0x8b) {
lseek (fd, -4, SEEK_END);
read (fd, &size, 4);
size = LittleLong (size);
}
lseek (fd, 0, SEEK_SET);
}
#endif
size = check_file (fd, -1, -1, &zip);
close (fd);
}
}
@ -215,6 +231,10 @@ Qdopen (int fd, const char *mode)
char m[80], *p;
int zip = 0;
#ifdef WIN32
if (file->file)
setmode (_fileno (file->file), O_BINARY);
#endif
for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
if (*mode == 'z') {
zip = 1;
@ -244,10 +264,25 @@ Qdopen (int fd, const char *mode)
return 0;
}
}
return file;
}
QFile *
Qsubopen (const char *path, int offs, int len, int zip)
{
int fd = open (path, O_RDONLY);
QFile *file;
if (fd == -1)
return 0;
#ifdef WIN32
if (file->file)
setmode (_fileno (file->file), O_BINARY);
setmode (fd, O_BINARY);
#endif
len = check_file (fd, offs, len, &zip);
file = Qdopen (fd, zip ? "rbz" : "rb");
file->size = len;
file->start = offs;
return file;
}
@ -372,11 +407,14 @@ Qputc (QFile *file, int c)
int
Qseek (QFile *file, long offset, int whence)
{
if (file->file)
return fseek (file->file, offset, whence);
if (file->file) {
return fseek (file->file, file->start + offset, whence);
}
#ifdef HAVE_ZLIB
else
else {
// libz seems to keep track of the true start position itself
return gzseek (file->gzfile, offset, whence);
}
#else
return -1;
#endif

View file

@ -648,16 +648,13 @@ linker_add_lib (const char *libname)
do {
did_something = 0;
for (i = 0; i < pack->numfiles; i++) {
int fd;
QFile *f;
qfo_t *qfo;
fd = open (libname, O_RDONLY);
lseek (fd, pack->files[i].filepos, SEEK_SET);
f = Qdopen (fd, "rbz");
f = Qsubopen (libname, pack->files[i].filepos,
pack->files[i].filelen, 1);
qfo = qfo_read (f);
Qclose (f);
close (fd);
if (!qfo)
return 1;

View file

@ -87,28 +87,14 @@ static hashtab_t *func_tab;
static QFile *
open_file (const char *path, int *len)
{
int fd = open (path, O_RDONLY);
unsigned char id[2];
unsigned char len_bytes[4];
QFile *file = Qopen (path, "rbz");
if (fd == -1) {
if (!file) {
perror (path);
return 0;
}
read (fd, id, 2);
if (id[0] == 0x1f && id[1] == 0x8b) {
lseek (fd, -4, SEEK_END);
read (fd, len_bytes, 4);
*len = ((len_bytes[3] << 24)
| (len_bytes[2] << 16)
| (len_bytes[1] << 8)
| (len_bytes[0]));
} else {
*len = lseek (fd, 0, SEEK_END);
}
lseek (fd, 0, SEEK_SET);
return Qdopen (fd, "rbz");
*len = Qfilesize (file);
return file;
}
static void *