now supports Mac and Windows newlines everywhere
[xonotic/darkplaces.git] / fs.c
1 /*
2         DarkPlaces file system
3
4         Copyright (C) 2003-2005 Mathieu Olivier
5         Copyright (C) 1999,2000  contributors of the QuakeForge project
6
7         This program is free software; you can redistribute it and/or
8         modify it under the terms of the GNU General Public License
9         as published by the Free Software Foundation; either version 2
10         of the License, or (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
16         See the GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with this program; if not, write to:
20
21                 Free Software Foundation, Inc.
22                 59 Temple Place - Suite 330
23                 Boston, MA  02111-1307, USA
24 */
25
26 #include "quakedef.h"
27
28 #include <limits.h>
29 #include <fcntl.h>
30
31 #ifdef WIN32
32 # include <direct.h>
33 # include <io.h>
34 #else
35 # include <pwd.h>
36 # include <sys/stat.h>
37 # include <unistd.h>
38 #endif
39
40 #include "fs.h"
41
42 // Win32 requires us to add O_BINARY, but the other OSes don't have it
43 #ifndef O_BINARY
44 # define O_BINARY 0
45 #endif
46
47
48 /*
49
50 All of Quake's data access is through a hierchal file system, but the contents
51 of the file system can be transparently merged from several sources.
52
53 The "base directory" is the path to the directory holding the quake.exe and
54 all game directories.  The sys_* files pass this to host_init in
55 quakeparms_t->basedir.  This can be overridden with the "-basedir" command
56 line parm to allow code debugging in a different directory.  The base
57 directory is only used during filesystem initialization.
58
59 The "game directory" is the first tree on the search path and directory that
60 all generated files (savegames, screenshots, demos, config files) will be
61 saved to.  This can be overridden with the "-game" command line parameter.
62 The game directory can never be changed while quake is executing.  This is a
63 precaution against having a malicious server instruct clients to write files
64 over areas they shouldn't.
65
66 */
67
68
69 /*
70 =============================================================================
71
72 CONSTANTS
73
74 =============================================================================
75 */
76
77 // Magic numbers of a ZIP file (big-endian format)
78 #define ZIP_DATA_HEADER 0x504B0304  // "PK\3\4"
79 #define ZIP_CDIR_HEADER 0x504B0102  // "PK\1\2"
80 #define ZIP_END_HEADER  0x504B0506  // "PK\5\6"
81
82 // Other constants for ZIP files
83 #define ZIP_MAX_COMMENTS_SIZE           ((unsigned short)0xFFFF)
84 #define ZIP_END_CDIR_SIZE                       22
85 #define ZIP_CDIR_CHUNK_BASE_SIZE        46
86 #define ZIP_LOCAL_CHUNK_BASE_SIZE       30
87
88 // Zlib constants (from zlib.h)
89 #define Z_SYNC_FLUSH    2
90 #define MAX_WBITS               15
91 #define Z_OK                    0
92 #define Z_STREAM_END    1
93 #define ZLIB_VERSION    "1.1.4"
94
95
96 /*
97 =============================================================================
98
99 TYPES
100
101 =============================================================================
102 */
103
104 // Zlib stream (from zlib.h)
105 // Warning: some pointers we don't use directly have
106 // been cast to "void*" for a matter of simplicity
107 typedef struct
108 {
109         qbyte                   *next_in;       // next input byte
110         unsigned int    avail_in;       // number of bytes available at next_in
111         unsigned long   total_in;       // total nb of input bytes read so far
112
113         qbyte                   *next_out;      // next output byte should be put there
114         unsigned int    avail_out;      // remaining free space at next_out
115         unsigned long   total_out;      // total nb of bytes output so far
116
117         char                    *msg;           // last error message, NULL if no error
118         void                    *state;         // not visible by applications
119
120         void                    *zalloc;        // used to allocate the internal state
121         void                    *zfree;         // used to free the internal state
122         void                    *opaque;        // private data object passed to zalloc and zfree
123
124         int                             data_type;      // best guess about the data type: ascii or binary
125         unsigned long   adler;          // adler32 value of the uncompressed data
126         unsigned long   reserved;       // reserved for future use
127 } z_stream;
128
129
130 typedef enum
131 {
132         QFILE_FLAG_NONE         = 0,
133         QFILE_FLAG_PACKED       = (1 << 0),     // inside a package (PAK or PK3)
134         QFILE_FLAG_DEFLATED     = (1 << 1)      // file is compressed using the deflate algorithm (PK3 only)
135 } qfile_flags_t;
136
137 #define FILE_BUFF_SIZE 2048
138 typedef struct
139 {
140         z_stream        zstream;
141         size_t          comp_length;                    // length of the compressed file
142         size_t          in_ind, in_len;                 // input buffer current index and length
143         size_t          in_position;                    // position in the compressed file
144         qbyte           input [FILE_BUFF_SIZE];
145 } ztoolkit_t;
146
147 struct qfile_s
148 {
149         qfile_flags_t   flags;
150         int                             handle;                                 // file descriptor
151         size_t                  real_length;                    // uncompressed file size (for files opened in "read" mode)
152         size_t                  position;                               // current position in the file
153         size_t                  offset;                                 // offset into the package (0 if external file)
154         int                             ungetc;                                 // single stored character from ungetc, cleared to EOF when read
155
156         // Contents buffer
157         size_t                  buff_ind, buff_len;             // buffer current index and length
158         qbyte                   buff [FILE_BUFF_SIZE];
159
160         // For zipped files
161         ztoolkit_t*             ztk;
162 };
163
164
165 // ------ PK3 files on disk ------ //
166
167 // You can get the complete ZIP format description from PKWARE website
168
169 typedef struct
170 {
171         unsigned int signature;
172         unsigned short disknum;
173         unsigned short cdir_disknum;    // number of the disk with the start of the central directory
174         unsigned short localentries;    // number of entries in the central directory on this disk
175         unsigned short nbentries;               // total number of entries in the central directory on this disk
176         unsigned int cdir_size;                 // size of the central directory
177         unsigned int cdir_offset;               // with respect to the starting disk number
178         unsigned short comment_size;
179 } pk3_endOfCentralDir_t;
180
181
182 // ------ PAK files on disk ------ //
183 typedef struct
184 {
185         char name[56];
186         int filepos, filelen;
187 } dpackfile_t;
188
189 typedef struct
190 {
191         char id[4];
192         int dirofs;
193         int dirlen;
194 } dpackheader_t;
195
196
197 // Packages in memory
198 typedef enum
199 {
200         PACKFILE_FLAG_NONE              = 0,
201         PACKFILE_FLAG_TRUEOFFS  = (1 << 0),     // the offset in packfile_t is the true contents offset
202         PACKFILE_FLAG_DEFLATED  = (1 << 1)      // file compressed using the deflate algorithm
203 } packfile_flags_t;
204
205 typedef struct
206 {
207         char name [MAX_QPATH];
208         packfile_flags_t flags;
209         size_t offset;
210         size_t packsize;        // size in the package
211         size_t realsize;        // real file size (uncompressed)
212 } packfile_t;
213
214 typedef struct pack_s
215 {
216         char filename [MAX_OSPATH];
217         int handle;
218         int ignorecase;  // PK3 ignores case
219         int numfiles;
220         packfile_t *files;
221         mempool_t *mempool;
222         struct pack_s *next;
223 } pack_t;
224
225
226 // Search paths for files (including packages)
227 typedef struct searchpath_s
228 {
229         // only one of filename / pack will be used
230         char filename[MAX_OSPATH];
231         pack_t *pack;
232         struct searchpath_s *next;
233 } searchpath_t;
234
235
236 /*
237 =============================================================================
238
239 FUNCTION PROTOTYPES
240
241 =============================================================================
242 */
243
244 void FS_Dir_f(void);
245 void FS_Ls_f(void);
246
247 static packfile_t* FS_AddFileToPack (const char* name, pack_t* pack,
248                                                                          size_t offset, size_t packsize,
249                                                                          size_t realsize, packfile_flags_t flags);
250
251
252 /*
253 =============================================================================
254
255 VARIABLES
256
257 =============================================================================
258 */
259
260 mempool_t *fs_mempool;
261 mempool_t *pak_mempool;
262
263 int fs_filesize;
264
265 pack_t *packlist = NULL;
266
267 searchpath_t *fs_searchpaths = NULL;
268
269 #define MAX_FILES_IN_PACK       65536
270
271 char fs_gamedir[MAX_OSPATH];
272 char fs_basedir[MAX_OSPATH];
273
274 qboolean fs_modified;   // set true if using non-id files
275
276
277 /*
278 =============================================================================
279
280 PRIVATE FUNCTIONS - PK3 HANDLING
281
282 =============================================================================
283 */
284
285 // Functions exported from zlib
286 #ifdef WIN32
287 # define ZEXPORT WINAPI
288 #else
289 # define ZEXPORT
290 #endif
291
292 static int (ZEXPORT *qz_inflate) (z_stream* strm, int flush);
293 static int (ZEXPORT *qz_inflateEnd) (z_stream* strm);
294 static int (ZEXPORT *qz_inflateInit2_) (z_stream* strm, int windowBits, const char *version, int stream_size);
295 static int (ZEXPORT *qz_inflateReset) (z_stream* strm);
296
297 #define qz_inflateInit2(strm, windowBits) \
298         qz_inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
299
300 static dllfunction_t zlibfuncs[] =
301 {
302         {"inflate",                     (void **) &qz_inflate},
303         {"inflateEnd",          (void **) &qz_inflateEnd},
304         {"inflateInit2_",       (void **) &qz_inflateInit2_},
305         {"inflateReset",        (void **) &qz_inflateReset},
306         {NULL, NULL}
307 };
308
309 // Handle for Zlib DLL
310 static dllhandle_t zlib_dll = NULL;
311
312
313 /*
314 ====================
315 PK3_CloseLibrary
316
317 Unload the Zlib DLL
318 ====================
319 */
320 void PK3_CloseLibrary (void)
321 {
322         Sys_UnloadLibrary (&zlib_dll);
323 }
324
325
326 /*
327 ====================
328 PK3_OpenLibrary
329
330 Try to load the Zlib DLL
331 ====================
332 */
333 qboolean PK3_OpenLibrary (void)
334 {
335         const char* dllnames [] =
336         {
337 #ifdef WIN32
338                 "zlib.dll",
339 #elif defined(MACOSX)
340                 "libz.dylib",
341 #else
342                 "libz.so.1",
343                 "libz.so",
344 #endif
345                 NULL
346         };
347
348         // Already loaded?
349         if (zlib_dll)
350                 return true;
351
352         // Load the DLL
353         if (! Sys_LoadLibrary (dllnames, &zlib_dll, zlibfuncs))
354         {
355                 Con_Printf ("Compressed files support disabled\n");
356                 return false;
357         }
358
359         Con_Printf ("Compressed files support enabled\n");
360         return true;
361 }
362
363
364 /*
365 ====================
366 PK3_GetEndOfCentralDir
367
368 Extract the end of the central directory from a PK3 package
369 ====================
370 */
371 qboolean PK3_GetEndOfCentralDir (const char *packfile, int packhandle, pk3_endOfCentralDir_t *eocd)
372 {
373         long filesize, maxsize;
374         qbyte *buffer, *ptr;
375         int ind;
376
377         // Get the package size
378         filesize = lseek (packhandle, 0, SEEK_END);
379         if (filesize < ZIP_END_CDIR_SIZE)
380                 return false;
381
382         // Load the end of the file in memory
383         if (filesize < ZIP_MAX_COMMENTS_SIZE + ZIP_END_CDIR_SIZE)
384                 maxsize = filesize;
385         else
386                 maxsize = ZIP_MAX_COMMENTS_SIZE + ZIP_END_CDIR_SIZE;
387         buffer = Mem_Alloc (tempmempool, maxsize);
388         lseek (packhandle, filesize - maxsize, SEEK_SET);
389         if (read (packhandle, buffer, maxsize) != (ssize_t) maxsize)
390         {
391                 Mem_Free (buffer);
392                 return false;
393         }
394
395         // Look for the end of central dir signature around the end of the file
396         maxsize -= ZIP_END_CDIR_SIZE;
397         ptr = &buffer[maxsize];
398         ind = 0;
399         while (BuffBigLong (ptr) != ZIP_END_HEADER)
400         {
401                 if (ind == maxsize)
402                 {
403                         Mem_Free (buffer);
404                         return false;
405                 }
406
407                 ind++;
408                 ptr--;
409         }
410
411         memcpy (eocd, ptr, ZIP_END_CDIR_SIZE);
412         eocd->signature = LittleLong (eocd->signature);
413         eocd->disknum = LittleShort (eocd->disknum);
414         eocd->cdir_disknum = LittleShort (eocd->cdir_disknum);
415         eocd->localentries = LittleShort (eocd->localentries);
416         eocd->nbentries = LittleShort (eocd->nbentries);
417         eocd->cdir_size = LittleLong (eocd->cdir_size);
418         eocd->cdir_offset = LittleLong (eocd->cdir_offset);
419         eocd->comment_size = LittleShort (eocd->comment_size);
420
421         Mem_Free (buffer);
422
423         return true;
424 }
425
426
427 /*
428 ====================
429 PK3_BuildFileList
430
431 Extract the file list from a PK3 file
432 ====================
433 */
434 int PK3_BuildFileList (pack_t *pack, const pk3_endOfCentralDir_t *eocd)
435 {
436         qbyte *central_dir, *ptr;
437         unsigned int ind;
438         int remaining;
439
440         // Load the central directory in memory
441         central_dir = Mem_Alloc (tempmempool, eocd->cdir_size);
442         lseek (pack->handle, eocd->cdir_offset, SEEK_SET);
443         read (pack->handle, central_dir, eocd->cdir_size);
444
445         // Extract the files properties
446         // The parsing is done "by hand" because some fields have variable sizes and
447         // the constant part isn't 4-bytes aligned, which makes the use of structs difficult
448         remaining = eocd->cdir_size;
449         pack->numfiles = 0;
450         ptr = central_dir;
451         for (ind = 0; ind < eocd->nbentries; ind++)
452         {
453                 size_t namesize, count;
454
455                 // Checking the remaining size
456                 if (remaining < ZIP_CDIR_CHUNK_BASE_SIZE)
457                 {
458                         Mem_Free (central_dir);
459                         return -1;
460                 }
461                 remaining -= ZIP_CDIR_CHUNK_BASE_SIZE;
462
463                 // Check header
464                 if (BuffBigLong (ptr) != ZIP_CDIR_HEADER)
465                 {
466                         Mem_Free (central_dir);
467                         return -1;
468                 }
469
470                 namesize = BuffLittleShort (&ptr[28]);  // filename length
471
472                 // Check encryption, compression, and attributes
473                 // 1st uint8  : general purpose bit flag
474                 //    Check bits 0 (encryption), 3 (data descriptor after the file), and 5 (compressed patched data (?))
475                 // 2nd uint8 : external file attributes
476                 //    Check bits 3 (file is a directory) and 5 (file is a volume (?))
477                 if ((ptr[8] & 0x29) == 0 && (ptr[38] & 0x18) == 0)
478                 {
479                         // Still enough bytes for the name?
480                         if ((size_t) remaining < namesize || namesize >= sizeof (*pack->files))
481                         {
482                                 Mem_Free (central_dir);
483                                 return -1;
484                         }
485
486                         // WinZip doesn't use the "directory" attribute, so we need to check the name directly
487                         if (ptr[ZIP_CDIR_CHUNK_BASE_SIZE + namesize - 1] != '/')
488                         {
489                                 char filename [sizeof (pack->files[0].name)];
490                                 size_t offset, packsize, realsize;
491                                 packfile_flags_t flags;
492
493                                 // Extract the name (strip it if necessary)
494                                 if (namesize >= sizeof (filename))
495                                         namesize = sizeof (filename) - 1;
496                                 memcpy (filename, &ptr[ZIP_CDIR_CHUNK_BASE_SIZE], namesize);
497                                 filename[namesize] = '\0';
498
499                                 if (BuffLittleShort (&ptr[10]))
500                                         flags = PACKFILE_FLAG_DEFLATED;
501                                 else
502                                         flags = 0;
503                                 offset = BuffLittleLong (&ptr[42]);
504                                 packsize = BuffLittleLong (&ptr[20]);
505                                 realsize = BuffLittleLong (&ptr[24]);
506                                 FS_AddFileToPack (filename, pack, offset, packsize, realsize, flags);
507                         }
508                 }
509
510                 // Skip the name, additionnal field, and comment
511                 // 1er uint16 : extra field length
512                 // 2eme uint16 : file comment length
513                 count = namesize + BuffLittleShort (&ptr[30]) + BuffLittleShort (&ptr[32]);
514                 ptr += ZIP_CDIR_CHUNK_BASE_SIZE + count;
515                 remaining -= count;
516         }
517
518         // If the package is empty, central_dir is NULL here
519         if (central_dir != NULL)
520                 Mem_Free (central_dir);
521         return pack->numfiles;
522 }
523
524
525 /*
526 ====================
527 FS_LoadPackPK3
528
529 Create a package entry associated with a PK3 file
530 ====================
531 */
532 pack_t *FS_LoadPackPK3 (const char *packfile)
533 {
534         int packhandle;
535         pk3_endOfCentralDir_t eocd;
536         pack_t *pack;
537         int real_nb_files;
538
539         packhandle = open (packfile, O_RDONLY | O_BINARY);
540         if (packhandle < 0)
541                 return NULL;
542
543         if (! PK3_GetEndOfCentralDir (packfile, packhandle, &eocd))
544                 Sys_Error ("%s is not a PK3 file", packfile);
545
546         // Multi-volume ZIP archives are NOT allowed
547         if (eocd.disknum != 0 || eocd.cdir_disknum != 0)
548                 Sys_Error ("%s is a multi-volume ZIP archive", packfile);
549
550         // We only need to do this test if MAX_FILES_IN_PACK is lesser than 65535
551         // since eocd.nbentries is an unsigned 16 bits integer
552 #if MAX_FILES_IN_PACK < 65535
553         if (eocd.nbentries > MAX_FILES_IN_PACK)
554                 Sys_Error ("%s contains too many files (%hu)", packfile, eocd.nbentries);
555 #endif
556
557         // Create a package structure in memory
558         pack = Mem_Alloc (pak_mempool, sizeof (pack_t));
559         pack->ignorecase = true; // PK3 ignores case
560         strlcpy (pack->filename, packfile, sizeof (pack->filename));
561         pack->handle = packhandle;
562         pack->numfiles = eocd.nbentries;
563         pack->mempool = Mem_AllocPool (packfile, 0, NULL);
564         pack->files = Mem_Alloc (pack->mempool, eocd.nbentries * sizeof(packfile_t));
565         pack->next = packlist;
566         packlist = pack;
567
568         real_nb_files = PK3_BuildFileList (pack, &eocd);
569         if (real_nb_files < 0)
570                 Sys_Error ("%s is not a valid PK3 file", packfile);
571
572         Con_Printf("Added packfile %s (%i files)\n", packfile, real_nb_files);
573         return pack;
574 }
575
576
577 /*
578 ====================
579 PK3_GetTrueFileOffset
580
581 Find where the true file data offset is
582 ====================
583 */
584 void PK3_GetTrueFileOffset (packfile_t *pfile, pack_t *pack)
585 {
586         qbyte buffer [ZIP_LOCAL_CHUNK_BASE_SIZE];
587         size_t count;
588
589         // Already found?
590         if (pfile->flags & PACKFILE_FLAG_TRUEOFFS)
591                 return;
592
593         // Load the local file description
594         lseek (pack->handle, pfile->offset, SEEK_SET);
595         count = read (pack->handle, buffer, ZIP_LOCAL_CHUNK_BASE_SIZE);
596         if (count != ZIP_LOCAL_CHUNK_BASE_SIZE || BuffBigLong (buffer) != ZIP_DATA_HEADER)
597                 Sys_Error ("Can't retrieve file %s in package %s", pfile->name, pack->filename);
598
599         // Skip name and extra field
600         pfile->offset += BuffLittleShort (&buffer[26]) + BuffLittleShort (&buffer[28]) + ZIP_LOCAL_CHUNK_BASE_SIZE;
601
602         pfile->flags |= PACKFILE_FLAG_TRUEOFFS;
603 }
604
605
606 /*
607 =============================================================================
608
609 OTHER PRIVATE FUNCTIONS
610
611 =============================================================================
612 */
613
614
615 /*
616 ====================
617 FS_AddFileToPack
618
619 Add a file to the list of files contained into a package
620 ====================
621 */
622 static packfile_t* FS_AddFileToPack (const char* name, pack_t* pack,
623                                                                          size_t offset, size_t packsize,
624                                                                          size_t realsize, packfile_flags_t flags)
625 {
626         int (*strcmp_funct) (const char* str1, const char* str2);
627         int left, right, middle;
628         packfile_t *pfile;
629
630         strcmp_funct = pack->ignorecase ? strcasecmp : strcmp;
631
632         // Look for the slot we should put that file into (binary search)
633         left = 0;
634         right = pack->numfiles - 1;
635         while (left <= right)
636         {
637                 int diff;
638
639                 middle = (left + right) / 2;
640                 diff = strcmp_funct (pack->files[middle].name, name);
641
642                 // If we found the file, there's a problem
643                 if (!diff)
644                         Sys_Error ("Package %s contains the file %s several times\n",
645                                            pack->filename, name);
646
647                 // If we're too far in the list
648                 if (diff > 0)
649                         right = middle - 1;
650                 else
651                         left = middle + 1;
652         }
653
654         // We have to move the right of the list by one slot to free the one we need
655         pfile = &pack->files[left];
656         memmove (pfile + 1, pfile, (pack->numfiles - left) * sizeof (*pfile));
657         pack->numfiles++;
658
659         strlcpy (pfile->name, name, sizeof (pfile->name));
660         pfile->offset = offset;
661         pfile->packsize = packsize;
662         pfile->realsize = realsize;
663         pfile->flags = flags;
664
665         return pfile;
666 }
667
668
669 /*
670 ============
671 FS_CreatePath
672
673 Only used for FS_Open.
674 ============
675 */
676 void FS_CreatePath (char *path)
677 {
678         char *ofs, save;
679
680         for (ofs = path+1 ; *ofs ; ofs++)
681         {
682                 if (*ofs == '/' || *ofs == '\\')
683                 {
684                         // create the directory
685                         save = *ofs;
686                         *ofs = 0;
687                         FS_mkdir (path);
688                         *ofs = save;
689                 }
690         }
691 }
692
693
694 /*
695 ============
696 FS_Path_f
697
698 ============
699 */
700 void FS_Path_f (void)
701 {
702         searchpath_t *s;
703
704         Con_Print("Current search path:\n");
705         for (s=fs_searchpaths ; s ; s=s->next)
706         {
707                 if (s->pack)
708                 {
709                         Con_Printf("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
710                 }
711                 else
712                         Con_Printf("%s\n", s->filename);
713         }
714 }
715
716
717 /*
718 =================
719 FS_LoadPackPAK
720
721 Takes an explicit (not game tree related) path to a pak file.
722
723 Loads the header and directory, adding the files at the beginning
724 of the list so they override previous pack files.
725 =================
726 */
727 pack_t *FS_LoadPackPAK (const char *packfile)
728 {
729         dpackheader_t header;
730         int i, numpackfiles;
731         int packhandle;
732         pack_t *pack;
733         dpackfile_t *info;
734
735         packhandle = open (packfile, O_RDONLY | O_BINARY);
736         if (packhandle < 0)
737                 return NULL;
738         read (packhandle, (void *)&header, sizeof(header));
739         if (memcmp(header.id, "PACK", 4))
740                 Sys_Error ("%s is not a packfile", packfile);
741         header.dirofs = LittleLong (header.dirofs);
742         header.dirlen = LittleLong (header.dirlen);
743
744         if (header.dirlen % sizeof(dpackfile_t))
745                 Sys_Error ("%s has an invalid directory size", packfile);
746
747         numpackfiles = header.dirlen / sizeof(dpackfile_t);
748
749         if (numpackfiles > MAX_FILES_IN_PACK)
750                 Sys_Error ("%s has %i files", packfile, numpackfiles);
751
752         pack = Mem_Alloc(pak_mempool, sizeof (pack_t));
753         pack->ignorecase = false; // PAK is case sensitive
754         strlcpy (pack->filename, packfile, sizeof (pack->filename));
755         pack->handle = packhandle;
756         pack->numfiles = 0;
757         pack->mempool = Mem_AllocPool(packfile, 0, NULL);
758         pack->files = Mem_Alloc(pack->mempool, numpackfiles * sizeof(packfile_t));
759         pack->next = packlist;
760         packlist = pack;
761
762         info = Mem_Alloc(tempmempool, sizeof(*info) * numpackfiles);
763         lseek (packhandle, header.dirofs, SEEK_SET);
764         read (packhandle, (void *)info, header.dirlen);
765
766         // parse the directory
767         for (i = 0;i < numpackfiles;i++)
768         {
769                 size_t offset = LittleLong (info[i].filepos);
770                 size_t size = LittleLong (info[i].filelen);
771
772                 FS_AddFileToPack (info[i].name, pack, offset, size, size, PACKFILE_FLAG_TRUEOFFS);
773         }
774
775         Mem_Free(info);
776
777         Con_Printf("Added packfile %s (%i files)\n", packfile, numpackfiles);
778         return pack;
779 }
780
781
782 /*
783 ================
784 FS_AddGameDirectory
785
786 Sets fs_gamedir, adds the directory to the head of the path,
787 then loads and adds pak1.pak pak2.pak ...
788 ================
789 */
790 void FS_AddGameDirectory (const char *dir)
791 {
792         stringlist_t *list, *current;
793         searchpath_t *search;
794         pack_t *pak;
795         char pakfile[MAX_OSPATH];
796
797         strlcpy (fs_gamedir, dir, sizeof (fs_gamedir));
798
799         list = listdirectory(dir);
800
801         // add any PAK package in the directory
802         for (current = list;current;current = current->next)
803         {
804                 if (matchpattern(current->text, "*.pak", true))
805                 {
806                         snprintf (pakfile, sizeof (pakfile), "%s/%s", dir, current->text);
807                         pak = FS_LoadPackPAK (pakfile);
808                         if (pak)
809                         {
810                                 search = Mem_Alloc(pak_mempool, sizeof(searchpath_t));
811                                 search->pack = pak;
812                                 search->next = fs_searchpaths;
813                                 fs_searchpaths = search;
814                         }
815                         else
816                                 Con_Printf("unable to load pak \"%s\"\n", pakfile);
817                 }
818         }
819
820         // add any PK3 package in the director
821         for (current = list;current;current = current->next)
822         {
823                 if (matchpattern(current->text, "*.pk3", true))
824                 {
825                         snprintf (pakfile, sizeof (pakfile), "%s/%s", dir, current->text);
826                         pak = FS_LoadPackPK3 (pakfile);
827                         if (pak)
828                         {
829                                 search = Mem_Alloc(pak_mempool, sizeof(searchpath_t));
830                                 search->pack = pak;
831                                 search->next = fs_searchpaths;
832                                 fs_searchpaths = search;
833                         }
834                         else
835                                 Con_Printf("unable to load pak \"%s\"\n", pakfile);
836                 }
837         }
838         freedirectory(list);
839
840         // Add the directory to the search path
841         // (unpacked files have the priority over packed files)
842         search = Mem_Alloc(pak_mempool, sizeof(searchpath_t));
843         strlcpy (search->filename, dir, sizeof (search->filename));
844         search->next = fs_searchpaths;
845         fs_searchpaths = search;
846 }
847
848
849 /*
850 ================
851 FS_AddGameHierarchy
852 ================
853 */
854 void FS_AddGameHierarchy (const char *dir)
855 {
856         const char *homedir;
857
858         strlcpy (com_modname, dir, sizeof (com_modname));
859
860         // Add the common game directory
861         FS_AddGameDirectory (va("%s/%s", fs_basedir, dir));
862
863         // Add the personal game directory
864         homedir = getenv ("HOME");
865         if (homedir != NULL && homedir[0] != '\0')
866                 FS_AddGameDirectory (va("%s/.%s/%s", homedir, gameuserdirname, dir));
867 }
868
869
870 /*
871 ============
872 FS_FileExtension
873 ============
874 */
875 static const char *FS_FileExtension (const char *in)
876 {
877         const char *separator, *backslash, *colon, *dot;
878
879         separator = strrchr(in, '/');
880         backslash = strrchr(in, '\\');
881         if (separator < backslash)
882                 separator = backslash;
883         colon = strrchr(in, ':');
884         if (separator < colon)
885                 separator = colon;
886
887         dot = strrchr(in, '.');
888         if (dot == NULL || dot < separator)
889                 return "";
890
891         return dot + 1;
892 }
893
894
895 /*
896 ================
897 FS_Init
898 ================
899 */
900 void FS_Init (void)
901 {
902         int i;
903         searchpath_t *search;
904
905         fs_mempool = Mem_AllocPool("file management", 0, NULL);
906         pak_mempool = Mem_AllocPool("paks", 0, NULL);
907
908         Cvar_RegisterVariable (&scr_screenshot_name);
909
910         Cmd_AddCommand ("path", FS_Path_f);
911         Cmd_AddCommand ("dir", FS_Dir_f);
912         Cmd_AddCommand ("ls", FS_Ls_f);
913
914         strcpy(fs_basedir, ".");
915         strcpy(fs_gamedir, ".");
916
917         PK3_OpenLibrary ();
918
919         // -basedir <path>
920         // Overrides the system supplied base directory (under GAMENAME)
921 // COMMANDLINEOPTION: Filesystem: -basedir <path> chooses what base directory the game data is in, inside this there should be a data directory for the game (for example id1)
922         i = COM_CheckParm ("-basedir");
923         if (i && i < com_argc-1)
924         {
925                 strlcpy (fs_basedir, com_argv[i+1], sizeof (fs_basedir));
926                 i = strlen (fs_basedir);
927                 if (i > 0 && (fs_basedir[i-1] == '\\' || fs_basedir[i-1] == '/'))
928                         fs_basedir[i-1] = 0;
929         }
930
931         // -path <dir or packfile> [<dir or packfile>] ...
932         // Fully specifies the exact search path, overriding the generated one
933 // COMMANDLINEOPTION: Filesystem: -path <path ..> specifies the full search path manually, overriding the generated one, example: -path c:\quake\id1 c:\quake\pak0.pak c:\quake\pak1.pak (not recommended)
934         i = COM_CheckParm ("-path");
935         if (i)
936         {
937                 fs_modified = true;
938                 while (++i < com_argc)
939                 {
940                         if (!com_argv[i] || com_argv[i][0] == '+' || com_argv[i][0] == '-')
941                                 break;
942
943                         search = Mem_Alloc(pak_mempool, sizeof(searchpath_t));
944                         if (!strcasecmp (FS_FileExtension(com_argv[i]), "pak"))
945                         {
946                                 search->pack = FS_LoadPackPAK (com_argv[i]);
947                                 if (!search->pack)
948                                         Sys_Error ("Couldn't load packfile: %s", com_argv[i]);
949                         }
950                         else if (!strcasecmp (FS_FileExtension (com_argv[i]), "pk3"))
951                         {
952                                 search->pack = FS_LoadPackPK3 (com_argv[i]);
953                                 if (!search->pack)
954                                         Sys_Error ("Couldn't load packfile: %s", com_argv[i]);
955                         }
956                         else
957                                 strlcpy (search->filename, com_argv[i], sizeof (search->filename));
958                         search->next = fs_searchpaths;
959                         fs_searchpaths = search;
960                 }
961                 return;
962         }
963
964         // add the game-specific paths
965         // gamedirname1 (typically id1)
966         FS_AddGameHierarchy (gamedirname1);
967         Cvar_SetQuick (&scr_screenshot_name, gamescreenshotname);
968
969         // add the game-specific path, if any
970         if (gamedirname2)
971         {
972                 fs_modified = true;
973                 FS_AddGameHierarchy (gamedirname2);
974         }
975
976         // -game <gamedir>
977         // Adds basedir/gamedir as an override game
978         // LordHavoc: now supports multiple -game directories
979         for (i = 1;i < com_argc;i++)
980         {
981                 if (!com_argv[i])
982                         continue;
983                 if (!strcmp (com_argv[i], "-game") && i < com_argc-1)
984                 {
985                         i++;
986                         fs_modified = true;
987                         FS_AddGameHierarchy (com_argv[i]);
988                         Cvar_SetQuick (&scr_screenshot_name, com_modname);
989                 }
990         }
991
992         // If "-condebug" is in the command line, remove the previous log file
993         if (COM_CheckParm ("-condebug") != 0)
994                 unlink (va("%s/qconsole.log", fs_gamedir));
995 }
996
997 /*
998 ================
999 FS_Shutdown
1000 ================
1001 */
1002 void FS_Shutdown (void)
1003 {
1004         Mem_FreePool (&pak_mempool);
1005         Mem_FreePool (&fs_mempool);
1006 }
1007
1008 /*
1009 ====================
1010 FS_SysOpen
1011
1012 Internal function used to create a qfile_t and open the relevant non-packed file on disk
1013 ====================
1014 */
1015 static qfile_t* FS_SysOpen (const char* filepath, const char* mode)
1016 {
1017         qfile_t* file;
1018         int mod, opt;
1019         unsigned int ind;
1020
1021         // Parse the mode string
1022         switch (mode[0])
1023         {
1024                 case 'r':
1025                         mod = O_RDONLY;
1026                         opt = 0;
1027                         break;
1028                 case 'w':
1029                         mod = O_WRONLY;
1030                         opt = O_CREAT | O_TRUNC;
1031                         break;
1032                 case 'a':
1033                         mod = O_WRONLY;
1034                         opt = O_CREAT | O_APPEND;
1035                         break;
1036                 default:
1037                         Con_Printf ("FS_SysOpen(%s, %s): invalid mode\n", filepath, mode);
1038                         return NULL;
1039         }
1040         for (ind = 1; mode[ind] != '\0'; ind++)
1041         {
1042                 switch (mode[ind])
1043                 {
1044                         case '+':
1045                                 mod = O_RDWR;
1046                                 break;
1047                         case 'b':
1048                                 opt |= O_BINARY;
1049                                 break;
1050                         default:
1051                                 Con_Printf ("FS_SysOpen(%s, %s): unknown character in mode (%c)\n",
1052                                                         filepath, mode, mode[ind]);
1053                 }
1054         }
1055
1056         file = Mem_Alloc (fs_mempool, sizeof (*file));
1057         memset (file, 0, sizeof (*file));
1058         file->ungetc = EOF;
1059
1060         file->handle = open (filepath, mod | opt, 0666);
1061         if (file->handle < 0)
1062         {
1063                 Mem_Free (file);
1064                 return NULL;
1065         }
1066
1067         // For files opened in read mode, we now need to get the length
1068         if (mod == O_RDONLY)
1069         {
1070                 file->real_length = lseek (file->handle, 0, SEEK_END);
1071                 lseek (file->handle, 0, SEEK_SET);
1072         }
1073
1074         return file;
1075 }
1076
1077
1078 /*
1079 ===========
1080 FS_OpenPackedFile
1081
1082 Open a packed file using its package file descriptor
1083 ===========
1084 */
1085 qfile_t *FS_OpenPackedFile (pack_t* pack, int pack_ind)
1086 {
1087         packfile_t *pfile;
1088         int dup_handle;
1089         qfile_t* file;
1090
1091         pfile = &pack->files[pack_ind];
1092
1093         // If we don't have the true offset, get it now
1094         if (! (pfile->flags & PACKFILE_FLAG_TRUEOFFS))
1095                 PK3_GetTrueFileOffset (pfile, pack);
1096
1097         // No Zlib DLL = no compressed files
1098         if (!zlib_dll && (pfile->flags & PACKFILE_FLAG_DEFLATED))
1099         {
1100                 Con_Printf("WARNING: can't open the compressed file %s\n"
1101                                         "You need the Zlib DLL to use compressed files\n",
1102                                         pfile->name);
1103                 fs_filesize = -1;
1104                 return NULL;
1105         }
1106
1107         dup_handle = dup (pack->handle);
1108         if (dup_handle < 0)
1109                 Sys_Error ("FS_OpenPackedFile: can't dup package's handle (pack: %s)", pack->filename);
1110
1111         file = Mem_Alloc (fs_mempool, sizeof (*file));
1112         memset (file, 0, sizeof (*file));
1113         file->handle = dup_handle;
1114         file->flags = QFILE_FLAG_PACKED;
1115         file->real_length = pfile->realsize;
1116         file->offset = pfile->offset;
1117         file->position = 0;
1118         file->ungetc = EOF;
1119
1120         if (lseek (file->handle, file->offset, SEEK_SET) == -1)
1121                 Sys_Error ("FS_OpenPackedFile: can't lseek to %s in %s (offset: %d)",
1122                                         pfile->name, pack->filename, file->offset);
1123
1124         if (pfile->flags & PACKFILE_FLAG_DEFLATED)
1125         {
1126                 ztoolkit_t *ztk;
1127
1128                 file->flags |= QFILE_FLAG_DEFLATED;
1129
1130                 // We need some more variables
1131                 ztk = Mem_Alloc (fs_mempool, sizeof (*ztk));
1132
1133                 ztk->comp_length = pfile->packsize;
1134
1135                 // Initialize zlib stream
1136                 ztk->zstream.next_in = ztk->input;
1137                 ztk->zstream.avail_in = 0;
1138
1139                 /* From Zlib's "unzip.c":
1140                  *
1141                  * windowBits is passed < 0 to tell that there is no zlib header.
1142                  * Note that in this case inflate *requires* an extra "dummy" byte
1143                  * after the compressed stream in order to complete decompression and
1144                  * return Z_STREAM_END.
1145                  * In unzip, i don't wait absolutely Z_STREAM_END because I known the
1146                  * size of both compressed and uncompressed data
1147                  */
1148                 if (qz_inflateInit2 (&ztk->zstream, -MAX_WBITS) != Z_OK)
1149                         Sys_Error ("FS_OpenPackedFile: inflate init error (file: %s)", pfile->name);
1150
1151                 ztk->zstream.next_out = file->buff;
1152                 ztk->zstream.avail_out = sizeof (file->buff);
1153
1154                 file->ztk = ztk;
1155         }
1156
1157         fs_filesize = pfile->realsize;
1158
1159         return file;
1160 }
1161
1162 /*
1163 ====================
1164 FS_CheckNastyPath
1165
1166 Return true if the path should be rejected due to one of the following:
1167 1: path elements that are non-portable
1168 2: path elements that would allow access to files outside the game directory,
1169    or are just not a good idea for a mod to be using.
1170 ====================
1171 */
1172 int FS_CheckNastyPath (const char *path)
1173 {
1174         // Windows: don't allow \ in filenames (windows-only), period.
1175         // (on Windows \ is a directory separator, but / is also supported)
1176         if (strstr(path, "\\"))
1177                 return 1; // non-portable
1178
1179         // Mac: don't allow Mac-only filenames - : is a directory separator
1180         // instead of /, but we rely on / working already, so there's no reason to
1181         // support a Mac-only path
1182         // Amiga and Windows: : tries to go to root of drive
1183         if (strstr(path, ":"))
1184                 return 1; // non-portable attempt to go to root of drive
1185
1186         // Amiga: // is parent directory
1187         if (strstr(path, "//"))
1188                 return 1; // non-portable attempt to go to parent directory
1189
1190         // all: don't allow going to current directory (./) or parent directory (../ or /../)
1191         if (strstr(path, "./"))
1192                 return 2; // attempt to go outside the game directory
1193
1194         // Windows and UNIXes: don't allow absolute paths
1195         if (path[0] == '/')
1196                 return 2; // attempt to go outside the game directory
1197
1198         // after all these checks we're pretty sure it's a / separated filename
1199         // and won't do much if any harm
1200         return false;
1201 }
1202
1203
1204 /*
1205 ====================
1206 FS_FindFile
1207
1208 Look for a file in the packages and in the filesystem
1209
1210 Return the searchpath where the file was found (or NULL)
1211 and the file index in the package if relevant
1212 ====================
1213 */
1214 static searchpath_t *FS_FindFile (const char *name, int* index, qboolean quiet)
1215 {
1216         searchpath_t *search;
1217         pack_t *pak;
1218
1219         // search through the path, one element at a time
1220         for (search = fs_searchpaths;search;search = search->next)
1221         {
1222                 // is the element a pak file?
1223                 if (search->pack)
1224                 {
1225                         int (*strcmp_funct) (const char* str1, const char* str2);
1226                         int left, right, middle;
1227
1228                         pak = search->pack;
1229                         strcmp_funct = pak->ignorecase ? strcasecmp : strcmp;
1230
1231                         // Look for the file (binary search)
1232                         left = 0;
1233                         right = pak->numfiles - 1;
1234                         while (left <= right)
1235                         {
1236                                 int diff;
1237
1238                                 middle = (left + right) / 2;
1239                                 diff = strcmp_funct (pak->files[middle].name, name);
1240
1241                                 // Found it
1242                                 if (!diff)
1243                                 {
1244                                         if (!quiet)
1245                                                 Con_DPrintf("FS_FindFile: %s in %s\n",
1246                                                                         pak->files[middle].name, pak->filename);
1247
1248                                         if (index != NULL)
1249                                                 *index = middle;
1250                                         return search;
1251                                 }
1252
1253                                 // If we're too far in the list
1254                                 if (diff > 0)
1255                                         right = middle - 1;
1256                                 else
1257                                         left = middle + 1;
1258                         }
1259                 }
1260                 else
1261                 {
1262                         char netpath[MAX_OSPATH];
1263                         snprintf(netpath, sizeof(netpath), "%s/%s", search->filename, name);
1264                         if (FS_SysFileExists (netpath))
1265                         {
1266                                 if (!quiet)
1267                                         Con_DPrintf("FS_FindFile: %s\n", netpath);
1268
1269                                 if (index != NULL)
1270                                         *index = -1;
1271                                 return search;
1272                         }
1273                 }
1274         }
1275
1276         if (!quiet)
1277                 Con_DPrintf("FS_FindFile: can't find %s\n", name);
1278
1279         if (index != NULL)
1280                 *index = -1;
1281         return NULL;
1282 }
1283
1284
1285 /*
1286 ===========
1287 FS_OpenReadFile
1288
1289 Look for a file in the search paths and open it in read-only mode
1290
1291 Sets fs_filesize
1292 ===========
1293 */
1294 qfile_t *FS_OpenReadFile (const char *filename, qboolean quiet)
1295 {
1296         searchpath_t *search;
1297         int pack_ind;
1298
1299         search = FS_FindFile (filename, &pack_ind, quiet);
1300
1301         // Not found?
1302         if (search == NULL)
1303         {
1304                 fs_filesize = -1;
1305                 return NULL;
1306         }
1307
1308         // Found in the filesystem?
1309         if (pack_ind < 0)
1310         {
1311                 char path [MAX_OSPATH];
1312                 snprintf (path, sizeof (path), "%s/%s", search->filename, filename);
1313                 return FS_SysOpen (path, "rb");
1314         }
1315
1316         // So, we found it in a package...
1317         return FS_OpenPackedFile (search->pack, pack_ind);
1318 }
1319
1320
1321 /*
1322 =============================================================================
1323
1324 MAIN PUBLIC FUNCTIONS
1325
1326 =============================================================================
1327 */
1328
1329 /*
1330 ====================
1331 FS_Open
1332
1333 Open a file. The syntax is the same as fopen
1334 ====================
1335 */
1336 qfile_t* FS_Open (const char* filepath, const char* mode, qboolean quiet)
1337 {
1338         qfile_t* file;
1339
1340         if (FS_CheckNastyPath(filepath))
1341         {
1342                 Con_Printf("FS_Open(\"%s\", \"%s\", %s): nasty filename rejected\n", filepath, mode, quiet ? "true" : "false");
1343                 return NULL;
1344         }
1345
1346         // If the file is opened in "write", "append", or "read/write" mode
1347         if (mode[0] == 'w' || mode[0] == 'a' || strchr (mode, '+'))
1348         {
1349                 char real_path [MAX_OSPATH];
1350
1351                 // Open the file on disk directly
1352                 snprintf (real_path, sizeof (real_path), "%s/%s", fs_gamedir, filepath);
1353
1354                 // Create directories up to the file
1355                 FS_CreatePath (real_path);
1356
1357                 return FS_SysOpen (real_path, mode);
1358         }
1359
1360         // Else, we look at the various search paths and open the file in read-only mode
1361         file = FS_OpenReadFile (filepath, quiet);
1362         if (file != NULL)
1363                 fs_filesize = file->real_length;
1364
1365         return file;
1366 }
1367
1368
1369 /*
1370 ====================
1371 FS_Close
1372
1373 Close a file
1374 ====================
1375 */
1376 int FS_Close (qfile_t* file)
1377 {
1378         if (close (file->handle))
1379                 return EOF;
1380
1381         if (file->ztk)
1382         {
1383                 qz_inflateEnd (&file->ztk->zstream);
1384                 Mem_Free (file->ztk);
1385         }
1386
1387         Mem_Free (file);
1388         return 0;
1389 }
1390
1391
1392 /*
1393 ====================
1394 FS_Write
1395
1396 Write "datasize" bytes into a file
1397 ====================
1398 */
1399 size_t FS_Write (qfile_t* file, const void* data, size_t datasize)
1400 {
1401         ssize_t result = write (file->handle, data, datasize);
1402         if (result < 0)
1403                 return 0;
1404
1405         return result;
1406 }
1407
1408
1409 /*
1410 ====================
1411 FS_Read
1412
1413 Read up to "buffersize" bytes from a file
1414 ====================
1415 */
1416 size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
1417 {
1418         size_t count, done;
1419
1420         // First, we copy as many bytes as we can from "buff"
1421         if (file->buff_ind < file->buff_len)
1422         {
1423                 count = file->buff_len - file->buff_ind;
1424
1425                 done = (buffersize > count) ? count : buffersize;
1426                 memcpy (buffer, &file->buff[file->buff_ind], done);
1427                 file->buff_ind += done;
1428
1429                 buffersize -= done;
1430                 if (buffersize == 0)
1431                         return done;
1432         }
1433         else
1434                 done = 0;
1435
1436         // NOTE: at this point, the read buffer is always empty
1437
1438         // If the file isn't compressed
1439         if (! (file->flags & QFILE_FLAG_DEFLATED))
1440         {
1441                 int nb;
1442
1443                 // We must take care to not read after the end of the file
1444                 count = file->real_length - file->position;
1445
1446                 // If we have a lot of data to get, put them directly into "buffer"
1447                 if (buffersize > sizeof (file->buff) / 2)
1448                 {
1449                         if (count > buffersize)
1450                                 count = buffersize;
1451                         lseek (file->handle, file->offset + file->position, SEEK_SET);
1452                         nb = read (file->handle, &((qbyte*)buffer)[done], count);
1453                         if (nb > 0)
1454                         {
1455                                 done += nb;
1456                                 file->position += nb;
1457
1458                                 // Invalidate the output data (for FS_Seek)
1459                                 file->buff_len = 0;
1460                                 file->buff_ind = 0;
1461                         }
1462                 }
1463                 else
1464                 {
1465                         if (count > sizeof (file->buff))
1466                                 count = sizeof (file->buff);
1467                         lseek (file->handle, file->offset + file->position, SEEK_SET);
1468                         nb = read (file->handle, file->buff, count);
1469                         if (nb > 0)
1470                         {
1471                                 file->buff_len = nb;
1472                                 file->position += nb;
1473
1474                                 // Copy the requested data in "buffer" (as much as we can)
1475                                 count = (buffersize > file->buff_len) ? file->buff_len : buffersize;
1476                                 memcpy (&((qbyte*)buffer)[done], file->buff, count);
1477                                 file->buff_ind = count;
1478                                 done += count;
1479                         }
1480                 }
1481
1482                 return done;
1483         }
1484
1485         // If the file is compressed, it's more complicated...
1486         // We cycle through a few operations until we have read enough data
1487         while (buffersize > 0)
1488         {
1489                 ztoolkit_t *ztk = file->ztk;
1490                 int error;
1491
1492                 // NOTE: at this point, the read buffer is always empty
1493
1494                 // If "input" is also empty, we need to refill it
1495                 if (ztk->in_ind == ztk->in_len)
1496                 {
1497                         // If we are at the end of the file
1498                         if (file->position == file->real_length)
1499                                 return done;
1500
1501                         count = ztk->comp_length - ztk->in_position;
1502                         if (count > sizeof (ztk->input))
1503                                 count = sizeof (ztk->input);
1504                         lseek (file->handle, file->offset + ztk->in_position, SEEK_SET);
1505                         if (read (file->handle, ztk->input, count) != (ssize_t)count)
1506                                 Sys_Error ("FS_Read: unexpected end of file");
1507
1508                         ztk->in_ind = 0;
1509                         ztk->in_len = count;
1510                         ztk->in_position += count;
1511                 }
1512
1513                 ztk->zstream.next_in = &ztk->input[ztk->in_ind];
1514                 ztk->zstream.avail_in = ztk->in_len - ztk->in_ind;
1515
1516                 // Now that we are sure we have compressed data available, we need to determine
1517                 // if it's better to inflate it in "file->buff" or directly in "buffer"
1518
1519                 // Inflate the data in "file->buff"
1520                 if (buffersize < sizeof (file->buff) / 2)
1521                 {
1522                         ztk->zstream.next_out = file->buff;
1523                         ztk->zstream.avail_out = sizeof (file->buff);
1524                         error = qz_inflate (&ztk->zstream, Z_SYNC_FLUSH);
1525                         if (error != Z_OK && error != Z_STREAM_END)
1526                                 Sys_Error ("Can't inflate file");
1527                         ztk->in_ind = ztk->in_len - ztk->zstream.avail_in;
1528
1529                         file->buff_len = sizeof (file->buff) - ztk->zstream.avail_out;
1530                         file->position += file->buff_len;
1531
1532                         // Copy the requested data in "buffer" (as much as we can)
1533                         count = (buffersize > file->buff_len) ? file->buff_len : buffersize;
1534                         memcpy (&((qbyte*)buffer)[done], file->buff, count);
1535                         file->buff_ind = count;
1536                 }
1537
1538                 // Else, we inflate directly in "buffer"
1539                 else
1540                 {
1541                         ztk->zstream.next_out = &((qbyte*)buffer)[done];
1542                         ztk->zstream.avail_out = buffersize;
1543                         error = qz_inflate (&ztk->zstream, Z_SYNC_FLUSH);
1544                         if (error != Z_OK && error != Z_STREAM_END)
1545                                 Sys_Error ("Can't inflate file");
1546                         ztk->in_ind = ztk->in_len - ztk->zstream.avail_in;
1547
1548                         // How much data did it inflate?
1549                         count = buffersize - ztk->zstream.avail_out;
1550                         file->position += count;
1551
1552                         // Invalidate the output data (for FS_Seek)
1553                         file->buff_len = 0;
1554                         file->buff_ind = 0;
1555                 }
1556
1557                 done += count;
1558                 buffersize -= count;
1559         }
1560
1561         return done;
1562 }
1563
1564
1565 /*
1566 ====================
1567 FS_Print
1568
1569 Print a string into a file
1570 ====================
1571 */
1572 int FS_Print (qfile_t* file, const char *msg)
1573 {
1574         return FS_Write (file, msg, strlen (msg));
1575 }
1576
1577 /*
1578 ====================
1579 FS_Printf
1580
1581 Print a string into a file
1582 ====================
1583 */
1584 int FS_Printf(qfile_t* file, const char* format, ...)
1585 {
1586         int result;
1587         va_list args;
1588
1589         va_start (args, format);
1590         result = FS_VPrintf (file, format, args);
1591         va_end (args);
1592
1593         return result;
1594 }
1595
1596
1597 /*
1598 ====================
1599 FS_VPrintf
1600
1601 Print a string into a file
1602 ====================
1603 */
1604 int FS_VPrintf (qfile_t* file, const char* format, va_list ap)
1605 {
1606         int len;
1607         char tempstring [1024];
1608
1609         len = vsnprintf (tempstring, sizeof(tempstring), format, ap);
1610         if (len >= sizeof (tempstring))
1611         {
1612                 char *temp = Mem_Alloc (tempmempool, len + 1);
1613                 len = vsnprintf (temp, len + 1, format, ap);
1614                 len = write (file->handle, temp, len);
1615                 Mem_Free (temp);
1616                 return len;
1617         }
1618
1619         return write (file->handle, tempstring, len);
1620 }
1621
1622
1623 /*
1624 ====================
1625 FS_Getc
1626
1627 Get stored ungetc character or the next character of a file
1628 ====================
1629 */
1630 int FS_Getc (qfile_t* file)
1631 {
1632         char c;
1633
1634         if (file->ungetc != EOF)
1635         {
1636                 c = file->ungetc;
1637                 file->ungetc = EOF;
1638         }
1639         else if (FS_Read (file, &c, 1) != 1)
1640                 return EOF;
1641
1642         return c;
1643 }
1644
1645
1646 /*
1647 ====================
1648 FS_UnGetc
1649
1650 Put a character back into the Getc buffer (only supports one character!)
1651 ====================
1652 */
1653 void FS_UnGetc (qfile_t* file, unsigned char c)
1654 {
1655         file->ungetc = c;
1656 }
1657
1658
1659 /*
1660 ====================
1661 FS_Seek
1662
1663 Move the position index in a file
1664 ====================
1665 */
1666 int FS_Seek (qfile_t* file, long offset, int whence)
1667 {
1668         ztoolkit_t *ztk;
1669         qbyte* buffer;
1670         size_t buffersize;
1671
1672         // Compute the file offset
1673         switch (whence)
1674         {
1675                 case SEEK_CUR:
1676                         offset += file->position - file->buff_len + file->buff_ind;
1677                         break;
1678
1679                 case SEEK_SET:
1680                         break;
1681
1682                 case SEEK_END:
1683                         offset += file->real_length;
1684                         break;
1685
1686                 default:
1687                         return -1;
1688         }
1689         if (offset < 0 || offset > (long) file->real_length)
1690                 return -1;
1691
1692         // If we have the data in our read buffer, we don't need to actually seek
1693         if (file->position - file->buff_len <= (size_t)offset &&
1694                 (size_t)offset <= file->position)
1695         {
1696                 file->buff_ind = offset + file->buff_len - file->position;
1697                 return 0;
1698         }
1699
1700         // Invalidate the read buffer contents
1701         file->buff_ind = 0;
1702         file->buff_len = 0;
1703
1704         // Unpacked or uncompressed files can seek directly
1705         if (! (file->flags & QFILE_FLAG_DEFLATED))
1706         {
1707                 if (lseek (file->handle, file->offset + offset, SEEK_SET) == -1)
1708                         return -1;
1709                 file->position = offset;
1710                 return 0;
1711         }
1712
1713         // Seeking in compressed files is more a hack than anything else,
1714         // but we need to support it, so here we go.
1715         ztk = file->ztk;
1716
1717         // If we have to go back in the file, we need to restart from the beginning
1718         if ((size_t)offset <= file->position)
1719         {
1720                 ztk->in_ind = 0;
1721                 ztk->in_len = 0;
1722                 ztk->in_position = 0;
1723                 file->position = 0;
1724                 lseek (file->handle, file->offset, SEEK_SET);
1725
1726                 // Reset the Zlib stream
1727                 ztk->zstream.next_in = ztk->input;
1728                 ztk->zstream.avail_in = 0;
1729                 qz_inflateReset (&ztk->zstream);
1730         }
1731
1732         // We need a big buffer to force inflating into it directly
1733         buffersize = 2 * sizeof (file->buff);
1734         buffer = Mem_Alloc (tempmempool, buffersize);
1735
1736         // Skip all data until we reach the requested offset
1737         while ((size_t)offset > file->position)
1738         {
1739                 size_t diff = offset - file->position;
1740                 size_t count, len;
1741
1742                 count = (diff > buffersize) ? buffersize : diff;
1743                 len = FS_Read (file, buffer, count);
1744                 if (len != count)
1745                 {
1746                         Mem_Free (buffer);
1747                         return -1;
1748                 }
1749         }
1750
1751         Mem_Free (buffer);
1752         return 0;
1753 }
1754
1755
1756 /*
1757 ====================
1758 FS_Tell
1759
1760 Give the current position in a file
1761 ====================
1762 */
1763 long FS_Tell (qfile_t* file)
1764 {
1765         return file->position - file->buff_len + file->buff_ind;
1766 }
1767
1768
1769 /*
1770 ============
1771 FS_LoadFile
1772
1773 Filename are relative to the quake directory.
1774 Always appends a 0 byte.
1775 ============
1776 */
1777 qbyte *FS_LoadFile (const char *path, mempool_t *pool, qboolean quiet)
1778 {
1779         qfile_t *file;
1780         qbyte *buf;
1781
1782         file = FS_Open (path, "rb", quiet);
1783         if (!file)
1784                 return NULL;
1785
1786         buf = Mem_Alloc (pool, fs_filesize + 1);
1787         buf[fs_filesize] = '\0';
1788
1789         FS_Read (file, buf, fs_filesize);
1790         FS_Close (file);
1791
1792         return buf;
1793 }
1794
1795
1796 /*
1797 ============
1798 FS_WriteFile
1799
1800 The filename will be prefixed by the current game directory
1801 ============
1802 */
1803 qboolean FS_WriteFile (const char *filename, void *data, int len)
1804 {
1805         qfile_t *file;
1806
1807         file = FS_Open (filename, "wb", false);
1808         if (!file)
1809         {
1810                 Con_Printf("FS_WriteFile: failed on %s\n", filename);
1811                 return false;
1812         }
1813
1814         Con_DPrintf("FS_WriteFile: %s\n", filename);
1815         FS_Write (file, data, len);
1816         FS_Close (file);
1817         return true;
1818 }
1819
1820
1821 /*
1822 =============================================================================
1823
1824 OTHERS PUBLIC FUNCTIONS
1825
1826 =============================================================================
1827 */
1828
1829 /*
1830 ============
1831 FS_StripExtension
1832 ============
1833 */
1834 void FS_StripExtension (const char *in, char *out, size_t size_out)
1835 {
1836         char *last = NULL;
1837
1838         if (size_out == 0)
1839                 return;
1840
1841         while (*in && size_out > 1)
1842         {
1843                 if (*in == '.')
1844                         last = out;
1845                 else if (*in == '/' || *in == '\\' || *in == ':')
1846                         last = NULL;
1847                 *out++ = *in++;
1848                 size_out--;
1849         }
1850         if (last)
1851                 *last = 0;
1852         else
1853                 *out = 0;
1854 }
1855
1856
1857 /*
1858 ==================
1859 FS_DefaultExtension
1860 ==================
1861 */
1862 void FS_DefaultExtension (char *path, const char *extension, size_t size_path)
1863 {
1864         const char *src;
1865
1866         // if path doesn't have a .EXT, append extension
1867         // (extension should include the .)
1868         src = path + strlen(path) - 1;
1869
1870         while (*src != '/' && src != path)
1871         {
1872                 if (*src == '.')
1873                         return;                 // it has an extension
1874                 src--;
1875         }
1876
1877         strlcat (path, extension, size_path);
1878 }
1879
1880
1881 /*
1882 ==================
1883 FS_FileExists
1884
1885 Look for a file in the packages and in the filesystem
1886 ==================
1887 */
1888 qboolean FS_FileExists (const char *filename)
1889 {
1890         return (FS_FindFile (filename, NULL, true) != NULL);
1891 }
1892
1893
1894 /*
1895 ==================
1896 FS_SysFileExists
1897
1898 Look for a file in the filesystem only
1899 ==================
1900 */
1901 qboolean FS_SysFileExists (const char *path)
1902 {
1903 #if WIN32
1904         int desc;
1905
1906         // TODO: use another function instead, to avoid opening the file
1907         desc = open (path, O_RDONLY | O_BINARY);
1908         if (desc < 0)
1909                 return false;
1910
1911         close (desc);
1912         return true;
1913 #else
1914         struct stat buf;
1915
1916         if (stat (path,&buf) == -1)
1917                 return false;
1918
1919         return true;
1920 #endif
1921 }
1922
1923 void FS_mkdir (const char *path)
1924 {
1925 #if WIN32
1926         _mkdir (path);
1927 #else
1928         mkdir (path, 0777);
1929 #endif
1930 }
1931
1932 /*
1933 ===========
1934 FS_Search
1935
1936 Allocate and fill a search structure with information on matching filenames.
1937 ===========
1938 */
1939 fssearch_t *FS_Search(const char *pattern, int caseinsensitive, int quiet)
1940 {
1941         fssearch_t *search;
1942         searchpath_t *searchpath;
1943         pack_t *pak;
1944         int i, basepathlength, numfiles, numchars;
1945         stringlist_t *dir, *dirfile, *liststart, *listcurrent, *listtemp;
1946         const char *slash, *backslash, *colon, *separator;
1947         char *basepath;
1948         char netpath[MAX_OSPATH];
1949         char temp[MAX_OSPATH];
1950
1951         while(!strncmp(pattern, "./", 2))
1952                 pattern += 2;
1953         while(!strncmp(pattern, ".\\", 2))
1954                 pattern += 2;
1955
1956         search = NULL;
1957         liststart = NULL;
1958         listcurrent = NULL;
1959         listtemp = NULL;
1960         slash = strrchr(pattern, '/');
1961         backslash = strrchr(pattern, '\\');
1962         colon = strrchr(pattern, ':');
1963         separator = pattern;
1964         if (separator < slash)
1965                 separator = slash;
1966         if (separator < backslash)
1967                 separator = backslash;
1968         if (separator < colon)
1969                 separator = colon;
1970         basepathlength = separator - pattern;
1971         basepath = Mem_Alloc (tempmempool, basepathlength + 1);
1972         if (basepathlength)
1973                 memcpy(basepath, pattern, basepathlength);
1974         basepath[basepathlength] = 0;
1975
1976         // search through the path, one element at a time
1977         for (searchpath = fs_searchpaths;searchpath;searchpath = searchpath->next)
1978         {
1979                 // is the element a pak file?
1980                 if (searchpath->pack)
1981                 {
1982                         // look through all the pak file elements
1983                         pak = searchpath->pack;
1984                         for (i = 0;i < pak->numfiles;i++)
1985                         {
1986                                 strcpy(temp, pak->files[i].name);
1987                                 while (temp[0])
1988                                 {
1989                                         if (matchpattern(temp, (char *)pattern, true))
1990                                         {
1991                                                 for (listtemp = liststart;listtemp;listtemp = listtemp->next)
1992                                                         if (!strcmp(listtemp->text, temp))
1993                                                                 break;
1994                                                 if (listtemp == NULL)
1995                                                 {
1996                                                         listcurrent = stringlistappend(listcurrent, temp);
1997                                                         if (liststart == NULL)
1998                                                                 liststart = listcurrent;
1999                                                         if (!quiet)
2000                                                                 Con_DPrintf("SearchPackFile: %s : %s\n", pak->filename, temp);
2001                                                 }
2002                                         }
2003                                         // strip off one path element at a time until empty
2004                                         // this way directories are added to the listing if they match the pattern
2005                                         slash = strrchr(temp, '/');
2006                                         backslash = strrchr(temp, '\\');
2007                                         colon = strrchr(temp, ':');
2008                                         separator = temp;
2009                                         if (separator < slash)
2010                                                 separator = slash;
2011                                         if (separator < backslash)
2012                                                 separator = backslash;
2013                                         if (separator < colon)
2014                                                 separator = colon;
2015                                         *((char *)separator) = 0;
2016                                 }
2017                         }
2018                 }
2019                 else
2020                 {
2021                         // get a directory listing and look at each name
2022                         snprintf(netpath, sizeof (netpath), "%s/%s", searchpath->filename, basepath);
2023                         if ((dir = listdirectory(netpath)))
2024                         {
2025                                 for (dirfile = dir;dirfile;dirfile = dirfile->next)
2026                                 {
2027                                         snprintf(temp, sizeof(temp), "%s/%s", basepath, dirfile->text);
2028                                         if (matchpattern(temp, (char *)pattern, true))
2029                                         {
2030                                                 for (listtemp = liststart;listtemp;listtemp = listtemp->next)
2031                                                         if (!strcmp(listtemp->text, temp))
2032                                                                 break;
2033                                                 if (listtemp == NULL)
2034                                                 {
2035                                                         listcurrent = stringlistappend(listcurrent, temp);
2036                                                         if (liststart == NULL)
2037                                                                 liststart = listcurrent;
2038                                                         if (!quiet)
2039                                                                 Con_DPrintf("SearchDirFile: %s\n", temp);
2040                                                 }
2041                                         }
2042                                 }
2043                                 freedirectory(dir);
2044                         }
2045                 }
2046         }
2047
2048         if (liststart)
2049         {
2050                 liststart = stringlistsort(liststart);
2051                 numfiles = 0;
2052                 numchars = 0;
2053                 for (listtemp = liststart;listtemp;listtemp = listtemp->next)
2054                 {
2055                         numfiles++;
2056                         numchars += strlen(listtemp->text) + 1;
2057                 }
2058                 search = Z_Malloc(sizeof(fssearch_t) + numchars + numfiles * sizeof(char *));
2059                 search->filenames = (char **)((char *)search + sizeof(fssearch_t));
2060                 search->filenamesbuffer = (char *)((char *)search + sizeof(fssearch_t) + numfiles * sizeof(char *));
2061                 search->numfilenames = numfiles;
2062                 numfiles = 0;
2063                 numchars = 0;
2064                 for (listtemp = liststart;listtemp;listtemp = listtemp->next)
2065                 {
2066                         search->filenames[numfiles] = search->filenamesbuffer + numchars;
2067                         strcpy(search->filenames[numfiles], listtemp->text);
2068                         numfiles++;
2069                         numchars += strlen(listtemp->text) + 1;
2070                 }
2071                 if (liststart)
2072                         stringlistfree(liststart);
2073         }
2074
2075         Mem_Free(basepath);
2076         return search;
2077 }
2078
2079 void FS_FreeSearch(fssearch_t *search)
2080 {
2081         Z_Free(search);
2082 }
2083
2084 extern int con_linewidth;
2085 int FS_ListDirectory(const char *pattern, int oneperline)
2086 {
2087         int numfiles;
2088         int numcolumns;
2089         int numlines;
2090         int columnwidth;
2091         int linebufpos;
2092         int i, j, k, l;
2093         const char *name;
2094         char linebuf[4096];
2095         fssearch_t *search;
2096         search = FS_Search(pattern, true, true);
2097         if (!search)
2098                 return 0;
2099         numfiles = search->numfilenames;
2100         if (!oneperline)
2101         {
2102                 // FIXME: the names could be added to one column list and then
2103                 // gradually shifted into the next column if they fit, and then the
2104                 // next to make a compact variable width listing but it's a lot more
2105                 // complicated...
2106                 // find width for columns
2107                 columnwidth = 0;
2108                 for (i = 0;i < numfiles;i++)
2109                 {
2110                         l = strlen(search->filenames[i]);
2111                         if (columnwidth < l)
2112                                 columnwidth = l;
2113                 }
2114                 // count the spacing character
2115                 columnwidth++;
2116                 // calculate number of columns
2117                 numcolumns = con_linewidth / columnwidth;
2118                 // don't bother with the column printing if it's only one column
2119                 if (numcolumns >= 2)
2120                 {
2121                         numlines = (numfiles + numcolumns - 1) / numcolumns;
2122                         for (i = 0;i < numlines;i++)
2123                         {
2124                                 linebufpos = 0;
2125                                 for (k = 0;k < numcolumns;k++)
2126                                 {
2127                                         l = i * numcolumns + k;
2128                                         if (l < numfiles)
2129                                         {
2130                                                 name = search->filenames[l];
2131                                                 for (j = 0;name[j] && j < (int)sizeof(linebuf) - 1;j++)
2132                                                         linebuf[linebufpos++] = name[j];
2133                                                 // space out name unless it's the last on the line
2134                                                 if (k < (numcolumns - 1) && l < (numfiles - 1))
2135                                                         for (;j < columnwidth && j < (int)sizeof(linebuf) - 1;j++)
2136                                                                 linebuf[linebufpos++] = ' ';
2137                                         }
2138                                 }
2139                                 linebuf[linebufpos] = 0;
2140                                 Con_Printf("%s\n", linebuf);
2141                         }
2142                 }
2143                 else
2144                         oneperline = true;
2145         }
2146         if (oneperline)
2147                 for (i = 0;i < numfiles;i++)
2148                         Con_Printf("%s\n", search->filenames[i]);
2149         FS_FreeSearch(search);
2150         return numfiles;
2151 }
2152
2153 static void FS_ListDirectoryCmd (const char* cmdname, int oneperline)
2154 {
2155         const char *pattern;
2156         if (Cmd_Argc() > 3)
2157         {
2158                 Con_Printf("usage:\n%s [path/pattern]\n", cmdname);
2159                 return;
2160         }
2161         if (Cmd_Argc() == 2)
2162                 pattern = Cmd_Argv(1);
2163         else
2164                 pattern = "*";
2165         if (!FS_ListDirectory(pattern, oneperline))
2166                 Con_Print("No files found.\n");
2167 }
2168
2169 void FS_Dir_f(void)
2170 {
2171         FS_ListDirectoryCmd("dir", true);
2172 }
2173
2174 void FS_Ls_f(void)
2175 {
2176         FS_ListDirectoryCmd("ls", false);
2177 }
2178