summaryrefslogtreecommitdiff
path: root/filesystem/packfile.cpp
blob: 5190959aa1fe710ea98c9f28502740ae36eeef24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//===========================================================================//

#include "packfile.h"
#include "zip_utils.h"
#include "tier0/basetypes.h"
#include "tier1/convar.h"
#include "tier1/lzmaDecoder.h"
#include "tier1/utlbuffer.h"
#include "tier1/generichash.h"

ConVar fs_monitor_read_from_pack( "fs_monitor_read_from_pack", "0", 0, "0:Off, 1:Any, 2:Sync only" );

// How many bytes we should decode at a time when doing pseudo-reads to seek forward in a compressed file handle,
// (affects maximum stack allocation by a forward seek)
#define COMPRESSED_SEEK_READ_CHUNK 1024

CPackFile::CPackFile()
{
	m_FileLength = 0;
	m_hPackFileHandleFS = NULL;
	m_fs = NULL;
	m_nBaseOffset = 0;
	m_bIsMapPath = false;
	m_lPackFileTime = 0L;
	m_refCount = 0;
	m_nOpenFiles = 0;
	m_PackFileID = 0;
}

CPackFile::~CPackFile()
{
	if ( m_nOpenFiles )
	{
		Error( "Closing pack file with %d open files!\n", m_nOpenFiles );
	}

	if ( m_hPackFileHandleFS )
	{
		m_fs->FS_fclose( m_hPackFileHandleFS );
		m_hPackFileHandleFS = NULL;
	}

	m_fs->m_ZipFiles.FindAndRemove( this );
}

int CPackFile::GetSectorSize()
{
	if ( m_hPackFileHandleFS )
	{
		return m_fs->FS_GetSectorSize( m_hPackFileHandleFS );
	}
#if defined( SUPPORT_PACKED_STORE )
	else if ( m_hPackFileHandleVPK )
	{
		return 2048;
	}
#endif
	else
	{
		return -1;
	}
}

// Read a bit of the file from the pack file:
int CZipPackFileHandle::Read( void* pBuffer, int nDestSize, int nBytes )
{
	// Clamp nBytes to not go past the end of the file (async is still possible due to nDestSize)
	if ( nBytes + m_nFilePointer > m_nLength )
	{
		nBytes = m_nLength - m_nFilePointer;
	}

	// Seek to the given file pointer and read
	int nBytesRead = m_pOwner->ReadFromPack( m_nIndex, pBuffer, nDestSize, nBytes, m_nBase + m_nFilePointer );

	m_nFilePointer += nBytesRead;

	return nBytesRead;
}

// Seek around inside the pack:
int CZipPackFileHandle::Seek( int nOffset, int nWhence )
{
	if ( nWhence == SEEK_SET )
	{
		m_nFilePointer = nOffset;
	}
	else if ( nWhence == SEEK_CUR )
	{
		m_nFilePointer += nOffset;
	}
	else if ( nWhence == SEEK_END )
	{
		m_nFilePointer = m_nLength + nOffset;
	}

	// Clamp the file pointer to the actual bounds of the file:
	if ( m_nFilePointer > m_nLength )
	{
		m_nFilePointer = m_nLength;
	}

	return m_nFilePointer;
}

//-----------------------------------------------------------------------------
// Open a file inside of a pack file.
//-----------------------------------------------------------------------------
CFileHandle *CZipPackFile::OpenFile( const char *pFileName, const char *pOptions )
{
	int nIndex, nOriginalSize, nCompressedSize;
	int64 nPosition;
	unsigned short nCompressionMethod;

	// find the file's location in the pack
	if ( GetFileInfo( pFileName, nIndex, nPosition, nOriginalSize, nCompressedSize, nCompressionMethod ) )
	{
		m_mutex.Lock();
#if defined( SUPPORT_PACKED_STORE )
		if ( m_nOpenFiles == 0 && m_hPackFileHandleFS == NULL && !m_hPackFileHandleVPK )
#else
		if ( m_nOpenFiles == 0 && m_hPackFileHandleFS == NULL )
#endif
		{
			// Try to open it as a regular file first
			m_hPackFileHandleFS = m_fs->Trace_FOpen( m_ZipName, "rb", 0, NULL );

			// !NOTE! Pack files inside of VPK not supported
		}
		m_nOpenFiles++;
		m_mutex.Unlock();
		CPackFileHandle* ph = NULL;
		if ( nCompressionMethod == ZIP_COMPRESSION_LZMA )
		{
			ph = new CLZMAZipPackFileHandle( this, nPosition, nOriginalSize, nCompressedSize, nIndex );
		}
		else
		{
			AssertMsg( nCompressionMethod == ZIP_COMPRESSION_NONE, "Unsupported compression type in zip pack file" );
			ph = new CZipPackFileHandle( this, nPosition, nOriginalSize, nIndex );
		}
		CFileHandle *fh = new CFileHandle( m_fs );
		fh->m_pPackFileHandle = ph;
		fh->m_nLength = nOriginalSize;

		// The default mode for fopen is text, so require 'b' for binary
		if ( strstr( pOptions, "b" ) == NULL )
		{
			fh->m_type = FT_PACK_TEXT;
		}
		else
		{
			fh->m_type = FT_PACK_BINARY;
		}

#if !defined( _RETAIL )
		fh->SetName( pFileName );
#endif
		return fh;
	}

	return NULL;
}

//-----------------------------------------------------------------------------
//	Get a directory entry from a pack's preload section
//-----------------------------------------------------------------------------
ZIP_PreloadDirectoryEntry* CZipPackFile::GetPreloadEntry( int nEntryIndex )
{
	if ( !m_pPreloadHeader )
	{
		return NULL;
	}

	// If this entry doesn't have a corresponding preload entry, fail.
	if ( m_PackFiles[nEntryIndex].m_nPreloadIdx == INVALID_PRELOAD_ENTRY )
	{
		return NULL;
	}

	return &m_pPreloadDirectory[m_PackFiles[nEntryIndex].m_nPreloadIdx];
}

//-----------------------------------------------------------------------------
//	Read a file from the pack
//-----------------------------------------------------------------------------
int CZipPackFile::ReadFromPack( int nEntryIndex, void* pBuffer, int nDestBytes, int nBytes, int64 nOffset )
{
	if ( nEntryIndex >= 0 )
	{
		if ( nBytes <= 0 )
		{
			return 0;
		}

		// X360TBD: This is screwy, it works because m_nBaseOffset is 0 for preload capable zips
		// It comes into play for files out of the embedded bsp zip,
		// this hackery is a pre-bias expecting ReadFromPack() do a symmetric post bias, yuck.

		// Attempt to satisfy request from possible preload section, otherwise fall through
		// A preload entry may be compressed
		ZIP_PreloadDirectoryEntry *pPreloadEntry = GetPreloadEntry( nEntryIndex );
		if ( pPreloadEntry )
		{
			// convert the absolute pack file position to a local file position
			int nLocalOffset = nOffset - m_PackFiles[nEntryIndex].m_nPosition;
			byte *pPreloadData = (byte*)m_pPreloadData + pPreloadEntry->DataOffset;

			if ( CLZMA::IsCompressed( pPreloadData ) )
			{
				unsigned int actualSize = CLZMA::GetActualSize( pPreloadData );
				if ( nLocalOffset + nBytes <= (int)actualSize )
				{
					// satisfy from compressed preload
					if ( fs_monitor_read_from_pack.GetInt() == 1 )
					{
						char szName[MAX_PATH];
						IndexToFilename( nEntryIndex, szName, sizeof( szName ) );
						Msg( "Read From Pack: [Preload] Requested:%d, Compressed:%d, %s\n", nBytes, pPreloadEntry->Length, szName );
					}

					if ( nLocalOffset == 0 && nDestBytes >= (int)actualSize && nBytes == (int)actualSize )
					{
						// uncompress directly into caller's buffer
						CLZMA::Uncompress( (unsigned char *)pPreloadData, (unsigned char *)pBuffer );
						return nBytes;
					}

					// uncompress into temporary memory
					CUtlMemory< byte > tempMemory;
					tempMemory.EnsureCapacity( actualSize );
					CLZMA::Uncompress( pPreloadData, tempMemory.Base() );
					// copy only what caller expects
					V_memcpy( pBuffer, (byte*)tempMemory.Base() + nLocalOffset, nBytes );
					return nBytes;
				}
			}
			else if ( nLocalOffset + nBytes <= (int)pPreloadEntry->Length )
			{
				// satisfy from uncompressed preload
				if ( fs_monitor_read_from_pack.GetInt() == 1 )
				{
					char szName[MAX_PATH];
					IndexToFilename( nEntryIndex, szName, sizeof( szName ) );
					Msg( "Read From Pack: [Preload] Requested:%d, Total:%d, %s\n", nBytes, pPreloadEntry->Length, szName );
				}

				V_memcpy( pBuffer, pPreloadData + nLocalOffset, nBytes );
				return nBytes;
			}
		}
	}

#if defined ( _X360 )
	// fell through as a direct request from within the pack
	// intercept to possible embedded section
	if ( m_pSection )
	{
		// a section is a special update zip that has no files, only preload
		// it has to be in the section
		V_memcpy( pBuffer, (byte*)m_pSection + nOffset, nBytes );
		return nBytes;
	}
#endif

	// Otherwise, do the read from the pack
	m_mutex.Lock();

	if ( fs_monitor_read_from_pack.GetInt() == 1 || ( fs_monitor_read_from_pack.GetInt() == 2 && ThreadInMainThread() ) )
	{
		// spew info about real i/o request
		char szName[MAX_PATH];
		IndexToFilename( nEntryIndex, szName, sizeof( szName ) );
		Msg( "Read From Pack: Sync I/O: Requested:%7d, Offset:0x%16.16llx, %s\n", nBytes, m_nBaseOffset + nOffset, szName );
	}

	int nBytesRead = 0;
	// Seek to the start of the read area and perform the read: TODO: CHANGE THIS INTO A CFileHandle
	if ( m_hPackFileHandleFS )
	{
		m_fs->FS_fseek( m_hPackFileHandleFS, m_nBaseOffset + nOffset, SEEK_SET );
		nBytesRead = m_fs->FS_fread( pBuffer, nDestBytes, nBytes, m_hPackFileHandleFS );
	}
#if defined( SUPPORT_PACKED_STORE )
	else
	{
		// We're a packfile embedded in a VPK
		m_hPackFileHandleVPK.Seek( m_nBaseOffset + nOffset, FILESYSTEM_SEEK_HEAD );
		nBytesRead = m_hPackFileHandleVPK.Read( pBuffer, nBytes );
	}
#endif
	m_mutex.Unlock();

	return nBytesRead;
}

//-----------------------------------------------------------------------------
//	Gets size, position, and index for a file in the pack.
//-----------------------------------------------------------------------------
bool CZipPackFile::GetFileInfo( const char *pFileName, int &nBaseIndex, int64 &nFileOffset, int &nOriginalSize, int &nCompressedSize, unsigned short &nCompressionMethod )
{
	char szCleanName[MAX_FILEPATH];
	Q_strncpy( szCleanName, pFileName, sizeof( szCleanName ) );
#ifdef _WIN32
	Q_strlower( szCleanName );
#endif
	Q_FixSlashes( szCleanName );

	if ( !Q_RemoveDotSlashes( szCleanName, CORRECT_PATH_SEPARATOR, false ) )
	{
		return false;
	}

	CZipPackFile::CPackFileEntry lookup;

	// We may get passed non-canonicalized filenames, so we need to remove the ../ from the path
	char szFixedName[MAX_PATH] = {0};
	V_strcpy_safe( szFixedName, pFileName );
	V_RemoveDotSlashes( szFixedName );

	lookup.m_HashName = HashStringCaselessConventional( szFixedName );

	int idx = m_PackFiles.Find( lookup );
	if ( -1 != idx  )
	{
		nFileOffset = m_PackFiles[idx].m_nPosition;
		nOriginalSize = m_PackFiles[idx].m_nOriginalSize;
		nCompressedSize = m_PackFiles[idx].m_nCompressedSize;
		nBaseIndex = idx;
		nCompressionMethod = m_PackFiles[idx].m_nCompressionMethod;
		return true;
	}

	return false;
}

bool CZipPackFile::IndexToFilename( int nIndex, char *pBuffer, int nBufferSize )
{
	AssertMsg( nIndex >= 0 && nIndex < m_PackFiles.Count(), "Out of bounds vector access in IndexToFilename" );
	if ( nIndex >= 0 )
	{
		m_fs->String( m_PackFiles[nIndex].m_hFileName, pBuffer, nBufferSize );
		return true;
	}

	Q_strncpy( pBuffer, "unknown", nBufferSize );

	return false;
}

//-----------------------------------------------------------------------------
//	Find a file in the pack.
//-----------------------------------------------------------------------------
bool CZipPackFile::ContainsFile( const char *pFileName )
{
	int nIndex, nOriginalSize, nCompressedSize;
	int64 nOffset;
	unsigned short nCompressionMethod;
	bool bFound = GetFileInfo( pFileName, nIndex, nOffset, nOriginalSize, nCompressedSize, nCompressionMethod );
	return bFound;
}

//-----------------------------------------------------------------------------
//	Build a list of matching files and directories given a FindFirst() style wildcard
//-----------------------------------------------------------------------------
void CZipPackFile::GetFileAndDirLists( const char *pRawWildCard, CUtlStringList &outDirnames, CUtlStringList &outFilenames, bool bSortedOutput )
{
	// See also: VPKlib function with same name.

	CUtlDict<int,int> AddedDirectories; // Used to remove duplicate paths

	char szWildCard[MAX_PATH] = { 0 };
	char szWildCardPath[MAX_PATH] = { 0 };
	char szWildCardBase[MAX_PATH] = { 0 };
	char szWildCardExt[MAX_PATH] = { 0 };

	size_t nLenWildcardPath = 0;
	size_t nLenWildcardBase = 0;

	bool bBaseWildcard = true;
	bool bExtWildcard = true;

	//
	// Parse the wildcard string into a base and extension used for string comparisons
	//
	V_strncpy( szWildCard, pRawWildCard, sizeof( szWildCard ) );
	V_FixSlashes( szWildCard, '/' );
	V_RemoveDotSlashes( szWildCard, '/', /* bRemoveDoubleSlashes */ true );

	// Workaround edge case in crappy path code. ExtractFilePath extracts a/b/ from a/b/c/ but FileBase would return the empty string.
	size_t nLenWildCard = V_strlen( szWildCard );
	if ( nLenWildCard && szWildCard[ nLenWildCard - 1 ] == '/' )
	{
		V_strncpy( szWildCardPath, szWildCard, sizeof( szWildCardPath ) );
	}
	else
	{
		V_ExtractFilePath( szWildCard, szWildCardPath, sizeof( szWildCardPath ) );
	}

	V_FileBase( szWildCard, szWildCardBase, sizeof( szWildCardBase ) );
	bool bWildcardHasExt = !!V_strrchr( szWildCard, '.' );
	V_ExtractFileExtension( szWildCard, szWildCardExt, sizeof( szWildCardExt ) );

	// From the pattern, we now have the directory path up to the file pattern, the filename base, and the filename
	// extension.

	// We don't support partial wildcards here (foo*bar.*). This support is massively inconsistent in our codebase and
	// there's no one point where we implement it, so rather than trying to match one of our broken implementations
	// (windows stdio is the only one I could find that was actually right), I'm going with "you shouldn't use this API
	// for that".
	bBaseWildcard = ( V_strcmp( szWildCardBase, "*" ) == 0 );
	bExtWildcard = ( V_strcmp( szWildCardExt, "*" ) == 0 );

	if ( !bWildcardHasExt && bBaseWildcard )
	{
		// For the special case of just '*' (and not, e.g., '*.') match '*.*'
		bExtWildcard = true;
	}

	nLenWildcardPath = V_strlen( szWildCardPath );
	nLenWildcardBase = V_strlen( szWildCardBase );

	// Generate the list of directories and files that match the wildcard
	//

	// For each candidate we attempt to walk up its path and consider the directories it represents as well (the
	// directories in a zip only exist in that files contain them, there are no empty directories)
	FOR_EACH_VEC( m_PackFiles, filesIdx )
	{
		char szCandidateName[MAX_PATH] = { 0 };
		IndexToFilename( filesIdx, szCandidateName, sizeof( szCandidateName ));

		if ( !szCandidateName[0] )
		{
			continue;
		}

		// Check if this file starts with the wildcard selector's path.
		// Note that we only ensure the prefix is the same. There are no specific entries for directories in a zip, they
		// only exist in that files in the zip reference them, so handle subdirectory matches from filenames as well.
		CUtlDict<int,int> ConsideredDirectories; // Will have duplicate directory matches when multiple files reside in them
		if  ( ( nLenWildcardPath && ( 0 == V_strnicmp( szCandidateName, szWildCardPath, nLenWildcardPath ) ) )
		      || ( !nLenWildcardPath && strchr( szCandidateName, '/' ) ) )
		{
			// Check if we matched because of a sub-directory, e.g. a/b/*.* would match /a/b/c/d/foo (in which case we
			// want to add /a/b/c to the matched directories list, ignoring the actual specific file)
			char szCandidateBaseName[MAX_PATH] = { 0 };
			bool bIsDir = false;
			size_t nSubDirLen = 0;
			char *pSubDirSlash = strchr( szCandidateName + nLenWildcardPath, '/' );
			if ( pSubDirSlash )
			{
				// This is a subdirectory match, drop everything after it and continue with it as the filename
				nSubDirLen = (size_t)( (ptrdiff_t)pSubDirSlash - (ptrdiff_t)( szCandidateName + nLenWildcardPath ) );
				V_strncpy( szCandidateBaseName, szCandidateName + nLenWildcardPath, nSubDirLen + 1 );
				bIsDir = true;

				// Early out if we already considered this exact directory from another file
				if ( ConsideredDirectories.Find( szCandidateBaseName ) != ConsideredDirectories.InvalidIndex() )
				{
					continue;
				}

				ConsideredDirectories.Insert( szCandidateBaseName, 0 );
			}
			else
			{
				V_strncpy( szCandidateBaseName, szCandidateName + nLenWildcardPath, sizeof( szCandidateBaseName ) );
			}

			char *pExt = strchr( szCandidateBaseName, '.' );
			if ( pExt )
			{
				// Null out the . and move to point to the extension
				*pExt = '\0';
				pExt++;
			}

			// Determine if this file matches the wildcart (*.*, *.ext, ext.*)
			bool bBaseMatch = false;
			bool bExtMatch = false;

			// If we have a base dir name, and we have a szWildCardBase to match against
			if ( bBaseWildcard )
				bBaseMatch = true;  // The base is the wildCard ("*"), so whatever we have as the base matches
			else
				bBaseMatch = ( 0 == V_stricmp( szCandidateBaseName, szWildCardBase ) );

			// If we have an extension and we have a szWildCardExtension to mach against
			if ( ( bExtWildcard && pExt ) || ( !pExt && !bWildcardHasExt ) )
				bExtMatch = true;
			else
				bExtMatch = bWildcardHasExt && pExt && ( 0 == V_stricmp( pExt, szWildCardExt ) );

			// If both parts match, then add it to the list
			if ( bBaseMatch && bExtMatch )
			{
				if ( bIsDir )
				{
					// Pull up to the subdir we considered out of szCandidateName
					size_t nMatchSize = nLenWildcardPath + nSubDirLen + 1;
					char *pszFullMatch = new char[ nMatchSize ];
					V_strncpy( pszFullMatch, szCandidateName, nMatchSize );
					outDirnames.AddToTail( pszFullMatch );
				}
				else
				{
					size_t nMatchSize = V_strlen( szCandidateName ) + 1;
					char *pszFullMatch = new char[ nMatchSize ];
					V_strncpy( pszFullMatch, szCandidateName, nMatchSize );
					outFilenames.AddToTail( pszFullMatch );
				}
			}
		}
	}

	// Sort the output if requested
	if ( bSortedOutput )
	{
		outDirnames.Sort( &CUtlStringList::SortFunc );
		outFilenames.Sort( &CUtlStringList::SortFunc );
	}
}


//-----------------------------------------------------------------------------
//	Set up the preload section
//-----------------------------------------------------------------------------
void CZipPackFile::SetupPreloadData()
{
	if ( m_pPreloadHeader || !m_nPreloadSectionSize )
	{
		// already loaded or not available
		return;
	}

	MEM_ALLOC_CREDIT_( "xZip" );

	void *pPreload;
#if defined ( _X360 )
	if ( m_pSection )
	{
		pPreload = (byte*)m_pSection + m_nPreloadSectionOffset;
	}
	else
#endif
	{
		pPreload = malloc( m_nPreloadSectionSize );
		if ( !pPreload )
		{
			return;
		}

		if ( IsX360() )
		{
			// 360 XZips are always dvd aligned
			Assert( ( m_nPreloadSectionSize % XBOX_DVD_SECTORSIZE ) == 0 );
			Assert( ( m_nPreloadSectionOffset % XBOX_DVD_SECTORSIZE ) == 0 );
		}

		// preload data is loaded as a single unbuffered i/o operation
		ReadFromPack( -1, pPreload, -1, m_nPreloadSectionSize, m_nPreloadSectionOffset );
	}

	// setup the header
	m_pPreloadHeader = (ZIP_PreloadHeader *)pPreload;

	// setup the preload directory
	m_pPreloadDirectory = (ZIP_PreloadDirectoryEntry *)((byte *)m_pPreloadHeader + sizeof( ZIP_PreloadHeader ) );

	// setup the remap table
	m_pPreloadRemapTable = (unsigned short *)((byte *)m_pPreloadDirectory + m_pPreloadHeader->PreloadDirectoryEntries * sizeof( ZIP_PreloadDirectoryEntry ) );

	// set the preload data base
	m_pPreloadData = (byte *)m_pPreloadRemapTable + m_pPreloadHeader->DirectoryEntries * sizeof( unsigned short );
}

void CZipPackFile::DiscardPreloadData()
{
	if ( !m_pPreloadHeader )
	{
		// already discarded
		return;
	}

#if defined ( _X360 )
	// a section is an alias, the header becomes an alias, not owned memory
	if ( !m_pSection )
	{
		free( m_pPreloadHeader );
	}
#else
	free( m_pPreloadHeader );
#endif
	m_pPreloadHeader = NULL;
}

//-----------------------------------------------------------------------------
//	Parse the zip file to build the file directory and preload section
//-----------------------------------------------------------------------------
bool CZipPackFile::Prepare( int64 fileLen, int64 nFileOfs )
{
	if ( !fileLen || fileLen < sizeof( ZIP_EndOfCentralDirRecord ) )
	{
		// nonsense zip
		return false;
	}

	// Pack files are always little-endian
	m_swap.ActivateByteSwapping( IsX360() );

	m_FileLength = fileLen;
	m_nBaseOffset = nFileOfs;

	ZIP_EndOfCentralDirRecord rec = { 0 };

	// Find and read the central header directory from its expected position at end of the file
	bool bCentralDirRecord = false;
	int64 offset = fileLen - sizeof( ZIP_EndOfCentralDirRecord );

	// 360 can have an incompatible format
	bool bCompatibleFormat = true;
	if ( IsX360() )
	{
		// 360 has dependable exact zips, backup to handle possible xzip format
		if ( offset - XZIP_COMMENT_LENGTH >= 0 )
		{
			offset -= XZIP_COMMENT_LENGTH;
		}

		// single i/o operation, scanning forward
		char *pTemp = (char *)_alloca( fileLen - offset );
		ReadFromPack( -1, pTemp, -1, fileLen - offset, offset );
		while ( offset <= (int64)(fileLen - sizeof( ZIP_EndOfCentralDirRecord )) )
		{
			memcpy( &rec, pTemp, sizeof( ZIP_EndOfCentralDirRecord ) );
			m_swap.SwapFieldsToTargetEndian( &rec );
			if ( rec.signature == PKID( 5, 6 ) )
			{
				bCentralDirRecord = true;
				if ( rec.commentLength >= 4 )
				{
					char *pComment = pTemp + sizeof( ZIP_EndOfCentralDirRecord );
					if ( !V_strnicmp( pComment, "XZP2", 4 ) )
					{
						bCompatibleFormat = false;
					}
				}
				break;
			}
			offset++;
			pTemp++;
		}
	}
	else
	{
		// scan entire file from expected location for central dir
		for ( ; offset >= 0; offset-- )
		{
			ReadFromPack( -1, (void*)&rec, -1, sizeof( rec ), offset );
			m_swap.SwapFieldsToTargetEndian( &rec );
			if ( rec.signature == PKID( 5, 6 ) )
			{
				bCentralDirRecord = true;
				break;
			}
		}
	}
	Assert( bCentralDirRecord );
	if ( !bCentralDirRecord )
	{
		// no zip directory, bad zip
		return false;
	}

	int numFilesInZip = rec.nCentralDirectoryEntries_Total;
	if ( numFilesInZip <= 0 )
	{
		// empty valid zip
		return true;
	}

	int firstFileIdx = 0;

	MEM_ALLOC_CREDIT();

	// read central directory into memory and parse
	CUtlBuffer zipDirBuff( 0, rec.centralDirectorySize, 0 );
	zipDirBuff.EnsureCapacity( rec.centralDirectorySize );
	zipDirBuff.ActivateByteSwapping( IsX360() );
	ReadFromPack( -1, zipDirBuff.Base(), -1, rec.centralDirectorySize, rec.startOfCentralDirOffset );
	zipDirBuff.SeekPut( CUtlBuffer::SEEK_HEAD, rec.centralDirectorySize );

	ZIP_FileHeader zipFileHeader;
	char filename[MAX_PATH] = { 0 };

	// Check for a preload section, expected to be the first file in the zip
	zipDirBuff.GetObjects( &zipFileHeader );
	zipDirBuff.Get( filename, Min( (size_t)zipFileHeader.fileNameLength, sizeof(filename) - 1 ) );
	if ( !V_stricmp( filename, PRELOAD_SECTION_NAME ) )
	{
		m_nPreloadSectionSize = zipFileHeader.uncompressedSize;
		m_nPreloadSectionOffset = zipFileHeader.relativeOffsetOfLocalHeader +
						  sizeof( ZIP_LocalFileHeader ) +
						  zipFileHeader.fileNameLength +
						  zipFileHeader.extraFieldLength;
		SetupPreloadData();

		// Set up to extract the remaining files
		int nextOffset = bCompatibleFormat ? zipFileHeader.extraFieldLength + zipFileHeader.fileCommentLength : 0;
		zipDirBuff.SeekGet( CUtlBuffer::SEEK_CURRENT, nextOffset );
		firstFileIdx = 1;
	}
	else
	{
		if ( IsX360() )
		{
			// all 360 zip files are expected to have preload sections
			// only during development, maps are allowed to lack them, due to auto-conversion
			if ( !m_bIsMapPath || g_pFullFileSystem->GetDVDMode() == DVDMODE_STRICT )
			{
				Warning( "ZipFile '%s' missing preload section\n", m_ZipName.String() );
			}
		}

		// No preload section, reset buffer pointer
		zipDirBuff.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
	}

	// Parse out central directory and determine absolute file positions of data.
	// Supports uncompressed zip files, with or without preload sections
	bool bSuccess = true;
	char tmpString[MAX_PATH] = { 0 };

	m_PackFiles.EnsureCapacity( numFilesInZip );

	for ( int i = firstFileIdx; i < numFilesInZip; ++i )
	{
		CZipPackFile::CPackFileEntry lookup;
		zipDirBuff.GetObjects( &zipFileHeader );

		if ( zipFileHeader.signature != PKID( 1, 2 ) )
		{
			Warning( "Invalid pack file signature\n" );
			bSuccess = false;
			break;
		}

		if ( zipFileHeader.compressionMethod != ZIP_COMPRESSION_NONE && zipFileHeader.compressionMethod != ZIP_COMPRESSION_LZMA )
		{
			Warning( "Pack file uses unsupported compression method: %hi\n", zipFileHeader.compressionMethod );
			bSuccess = false;
			break;
		}

		Assert( zipFileHeader.fileNameLength < sizeof( tmpString ) );
		unsigned int fileNameLen = Min( (size_t)zipFileHeader.fileNameLength, sizeof( tmpString ) - 1 );
		zipDirBuff.Get( (void *)tmpString, fileNameLen );
		tmpString[fileNameLen] = '\0';
		Q_FixSlashes( tmpString );

		lookup.m_hFileName = m_fs->FindOrAddFileName( tmpString );
		lookup.m_HashName = HashStringCaselessConventional( tmpString );
		lookup.m_nOriginalSize = zipFileHeader.uncompressedSize;
		lookup.m_nCompressedSize = zipFileHeader.compressedSize;
		lookup.m_nPosition = zipFileHeader.relativeOffsetOfLocalHeader +
								sizeof( ZIP_LocalFileHeader ) +
								zipFileHeader.fileNameLength +
								zipFileHeader.extraFieldLength;
		lookup.m_nCompressionMethod = zipFileHeader.compressionMethod;

		// track the index to this file's possible preload directory entry
		if ( m_pPreloadRemapTable )
		{
			lookup.m_nPreloadIdx = m_pPreloadRemapTable[i];
		}
		else
		{
			lookup.m_nPreloadIdx = INVALID_PRELOAD_ENTRY;
		}
		m_PackFiles.InsertNoSort( lookup );

		int nextOffset = bCompatibleFormat ? zipFileHeader.extraFieldLength + zipFileHeader.fileCommentLength : 0;
		zipDirBuff.SeekGet( CUtlBuffer::SEEK_CURRENT, nextOffset );
	}

	m_PackFiles.RedoSort();

	return bSuccess;
}


//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
CZipPackFile::CZipPackFile( CBaseFileSystem* fs, void *pSection )
 : m_PackFiles()
{
	m_fs = fs;
	m_pPreloadDirectory = NULL;
	m_pPreloadData = NULL;
	m_pPreloadHeader = NULL;
	m_pPreloadRemapTable = NULL;
	m_nPreloadSectionOffset = 0;
	m_nPreloadSectionSize = 0;

#if defined( _X360 )
	m_pSection = pSection;
#endif
}

CZipPackFile::~CZipPackFile()
{
	DiscardPreloadData();
}

//-----------------------------------------------------------------------------
// Purpose:
// Input  : src1 -
//			src2 -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CZipPackFile::CPackFileLessFunc::Less( CZipPackFile::CPackFileEntry const& src1, CZipPackFile::CPackFileEntry const& src2, void *pCtx )
{
	return ( src1.m_HashName < src2.m_HashName );
}

//-----------------------------------------------------------------------------
// Purpose: Zip Pack file handle implementation
//-----------------------------------------------------------------------------
CZipPackFileHandle::CZipPackFileHandle( CZipPackFile* pOwner, int64 nBase, unsigned int nLength, unsigned int nIndex, unsigned int nFilePointer )
{
	m_pOwner = pOwner;
	m_nBase = nBase;
	m_nLength = nLength;
	m_nIndex = nIndex;
	m_nFilePointer = nFilePointer;
	pOwner->AddRef();
}

CZipPackFileHandle::~CZipPackFileHandle()
{
	m_pOwner->m_mutex.Lock();
	--m_pOwner->m_nOpenFiles;
	// XXX(johns) this doesn't go here, the hell
	if ( m_pOwner->m_nOpenFiles == 0 && m_pOwner->m_bIsMapPath )
	{
		if ( m_pOwner->m_hPackFileHandleFS )
		{
			m_pOwner->FileSystem()->Trace_FClose( m_pOwner->m_hPackFileHandleFS );
			m_pOwner->m_hPackFileHandleFS = NULL;
		}
	}
	m_pOwner->Release();
	m_pOwner->m_mutex.Unlock();
}

void CZipPackFileHandle::SetBufferSize( int nBytes )
{
	if ( m_pOwner->m_hPackFileHandleFS )
	{
		m_pOwner->FileSystem()->FS_setbufsize( m_pOwner->m_hPackFileHandleFS, nBytes );
	}
}

int CZipPackFileHandle::GetSectorSize()
{
	return m_pOwner->GetSectorSize();
}

int64 CZipPackFileHandle::AbsoluteBaseOffset()
{
	return m_pOwner->GetPackFileBaseOffset() + m_nBase;
}

#if defined( _DEBUG ) && !defined( OSX )
#include <atomic>
static std::atomic<int> sLZMAPackFileHandles( 0 );
#endif // defined( _DEBUG ) && !defined( OSX )

CLZMAZipPackFileHandle::CLZMAZipPackFileHandle( CZipPackFile* pOwner, int64 nBase, unsigned int nOriginalSize, unsigned int nCompressedSize,
                                                unsigned int nIndex, unsigned int nFilePointer )
	: CZipPackFileHandle( pOwner, nBase, nCompressedSize, nIndex, nFilePointer ),
	  m_BackSeekBuffer( 0, PACKFILE_COMPRESSED_FILEHANDLE_SEEK_BUFFER ),
	  m_ReadBuffer( 0, PACKFILE_COMPRESSED_FILEHANDLE_READ_BUFFER ),
	  m_pLZMAStream( NULL ), m_nSeekPosition( 0 ), m_nOriginalSize( nOriginalSize )
{
	Reset();
#if defined( _DEBUG ) && !defined( OSX )
	if ( ++sLZMAPackFileHandles == PACKFILE_COMPRESSED_FILE_HANDLES_WARNING )
	{
		// By my count a live filehandle is currently around 270k, mostly due to the LZMA dictionary (256k) with the
		// rest being the read/seek buffers.
		Warning( "More than %u compressed file handles in use. "
		         "These carry large buffers around, and can cause high memory usage\n",
		         PACKFILE_COMPRESSED_FILE_HANDLES_WARNING );
	}
#endif // defined( _DEBUG ) && !defined( OSX )
}

CLZMAZipPackFileHandle::~CLZMAZipPackFileHandle()
{
	delete m_pLZMAStream;
	m_pLZMAStream = NULL;
#if defined( _DEBUG ) && !defined( OSX )
	sLZMAPackFileHandles--;
	Assert( sLZMAPackFileHandles >= 0 );
#endif // defined( _DEBUG ) && !defined( OSX )
}

int CLZMAZipPackFileHandle::Read( void* pBuffer, int nDestSize, int nBytes )
{
	int nMaxRead = Min( Min( nDestSize, nBytes ), Size() - Tell() );
	int nBytesRead = 0;

	// If we have seeked backwards into our buffer, read from there first
	int nBackSeek = m_BackSeekBuffer.TellPut() - m_BackSeekBuffer.TellGet();
	Assert( nBackSeek >= 0 );
	if ( nBackSeek > 0 )
	{
		int nBackSeekRead = Min( nBackSeek, nMaxRead );
		m_BackSeekBuffer.Get( pBuffer, nBackSeekRead );
		nBytesRead += nBackSeekRead;
	}

	// Done if nothing to read
	if ( nMaxRead - nBytesRead <= 0 )
	{
		m_nSeekPosition += nBytesRead;
		return nBytesRead;
	}

	// Read bytes not fulfilled by backbuffer
	Assert( m_BackSeekBuffer.TellPut() == m_BackSeekBuffer.TellGet() );
	while ( nBytesRead < nMaxRead )
	{
		// refill read buffer if empty
		int nRemainingReadBuffer = FillReadBuffer();

		// Consume from read buffer
		unsigned int nCompressedBytesRead = 0;
		unsigned int nOutputBytesWritten = 0;
		bool bSuccess = m_pLZMAStream->Read( (unsigned char *)m_ReadBuffer.PeekGet(), nRemainingReadBuffer,
		                                     (unsigned char *)pBuffer + nBytesRead, nMaxRead - nBytesRead,
		                                     nCompressedBytesRead, nOutputBytesWritten );

		if ( bSuccess )
		{
			// fixup get position
			m_ReadBuffer.SeekGet( CUtlBuffer::SEEK_CURRENT, nCompressedBytesRead );

			nBytesRead += nOutputBytesWritten;

			AssertMsg( nCompressedBytesRead == (unsigned int)nRemainingReadBuffer || nBytesRead == nMaxRead,
			           "Should have consumed the readbuffer or reached nMaxRead" );

			if ( nCompressedBytesRead == 0 && nOutputBytesWritten == 0 )
			{
				AssertMsg( nCompressedBytesRead > 0 || nOutputBytesWritten > 0,
				           "Stuck progress in read loop, aborting. Stream may be defunct." );
				break;
			}
		}
		else
		{
			Warning( "Pack file: reading from LZMA stream failed\n" );
			break;
		}
	}

	// Finally, store last bytes output to the backseek buffer

	// If we read less than BackSeekBuffer.Size() bytes, shift the end of the old backseek buffer up
	int nOldBackSeek = m_BackSeekBuffer.TellPut();
	int nReuseBackSeek = Max( Min( m_BackSeekBuffer.Size() - nBytesRead, nOldBackSeek ), 0 );
	if ( nReuseBackSeek )
	{
		// Shift the reused chunk to the front
		V_memmove( m_BackSeekBuffer.Base(),
		           (unsigned char *)m_BackSeekBuffer.Base() + m_BackSeekBuffer.TellPut() - nReuseBackSeek,
		           nReuseBackSeek );
	}

	// Update get/put position
	m_BackSeekBuffer.SeekPut( CUtlBuffer::SEEK_HEAD, nReuseBackSeek );
	m_BackSeekBuffer.SeekGet( CUtlBuffer::SEEK_HEAD, nReuseBackSeek );

	// Fill in remainder from what we just read
	int nReadIntoBackSeek = Min( m_BackSeekBuffer.Size() - nReuseBackSeek, nBytesRead );
	m_BackSeekBuffer.Put( (unsigned char *)pBuffer + nBytesRead - nReadIntoBackSeek, nReadIntoBackSeek );
	m_BackSeekBuffer.SeekGet( CUtlBuffer::SEEK_CURRENT, nReadIntoBackSeek );

	m_nSeekPosition += nBytesRead;
	return nBytesRead;
}

int CLZMAZipPackFileHandle::Seek( int nOffset, int nWhence )
{
	int nNewPosition = m_nSeekPosition;

	if ( nWhence == SEEK_CUR )
	{
		nNewPosition = m_nSeekPosition + nOffset;
	}
	else if ( nWhence == SEEK_END )
	{
		nNewPosition = Size() + nOffset;
	}
	else if ( nWhence == SEEK_SET )
	{
		nNewPosition = nOffset;
	}
	else
	{
		AssertMsg( false, "Unknown seek type" );
	}

	nNewPosition = Min( Size(), nNewPosition );
	nNewPosition = Max( 0, nNewPosition );

	if ( nNewPosition == m_nSeekPosition )
	{
		return nNewPosition;
	}

	// Backwards seek
	if ( nNewPosition < m_nSeekPosition )
	{
		int nBackSeekAvailable = m_BackSeekBuffer.TellGet();
		int nDesiredBackSeek = m_nSeekPosition - nNewPosition;

		if ( nBackSeekAvailable >= nDesiredBackSeek )
		{
			// Move get backwards into backseek buffer to account for seek
			m_BackSeekBuffer.SeekGet( CUtlBuffer::SEEK_CURRENT, -nDesiredBackSeek );
			m_nSeekPosition = nNewPosition;
		}
		else
		{
			// Seeking backwards beyond our backseek buffer. Have to restart stream. This kills the performance.
			Warning( "LZMA file handle: seeking backwards beyond backseek buffer size ( %u ), "
			         "replaying read & decompression of %u bytes. Should avoid large back seeks in compressed files or "
			         "increase backseek buffer sizing.",
			         m_BackSeekBuffer.Size(), nNewPosition );

			// Reset to beginning of underlying stream
			Reset();

			// Fall through to performing a forward seek
		}
	}

	// Forward seek
	if ( nNewPosition > m_nSeekPosition )
	{
		// Can't actually seek forward without making decode progress. Issue fake reads until we've reached our target.
		unsigned char dummyBuffer[COMPRESSED_SEEK_READ_CHUNK];
		while ( nNewPosition > m_nSeekPosition )
		{
			int nReadSize = Min( nNewPosition - m_nSeekPosition, COMPRESSED_SEEK_READ_CHUNK );
			unsigned int nBytesRead = Read( &dummyBuffer, sizeof(dummyBuffer), nReadSize );
			m_nSeekPosition += nBytesRead;
			if ( !nBytesRead )
			{
				Warning( "LZMA file handle: failed reading forward to desired seek position\n" );
				break;
			}
		}
	}

	return m_nSeekPosition;
}

int CLZMAZipPackFileHandle::Tell()
{
	return m_nSeekPosition;
}

int CLZMAZipPackFileHandle::Size()
{
	return m_nOriginalSize;
}

int CLZMAZipPackFileHandle::FillReadBuffer()
{
	int nRemainingReadBuffer = m_ReadBuffer.TellPut() - m_ReadBuffer.TellGet();
	int nRemainingCompressedBytes = CZipPackFileHandle::Size() - CZipPackFileHandle::Tell();

	if ( nRemainingReadBuffer > 0 || nRemainingCompressedBytes <= 0 )
	{
		// No action if read buffer isn't empty
		return nRemainingReadBuffer;
	}

	// Reset empty read buffer
	m_ReadBuffer.SeekPut( CUtlBuffer::SEEK_HEAD, 0 );
	m_ReadBuffer.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
	int nRefillSize = Min( nRemainingCompressedBytes, m_ReadBuffer.Size() );
	int nRefillResult = CZipPackFileHandle::Read( m_ReadBuffer.PeekPut(), m_ReadBuffer.Size(), nRefillSize );
	AssertMsg( nRefillSize == nRefillResult, "Don't expect to fail to read here" );

	// Fixup put pointer after writing into buffer's memory
	m_ReadBuffer.SeekPut( CUtlBuffer::SEEK_CURRENT, nRefillResult );

	return nRefillResult;
}

void CLZMAZipPackFileHandle::Reset()
{
	// Seek underlying stream back to start
	CZipPackFileHandle::Seek( SEEK_SET, 0 );

	delete m_pLZMAStream;
	m_pLZMAStream = new CLZMAStream();
	m_pLZMAStream->InitZIPHeader( CZipPackFileHandle::Size(), m_nOriginalSize );
	m_nSeekPosition = 0;
	m_BackSeekBuffer.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
	m_BackSeekBuffer.SeekPut( CUtlBuffer::SEEK_HEAD, 0 );
	m_ReadBuffer.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
	m_ReadBuffer.SeekPut( CUtlBuffer::SEEK_HEAD, 0 );
}