aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/frontend/zipfs.cpp
blob: 42df0520f68df22f1b7d9573a7031a4dca14c686 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "zipfs.h"

namespace zen {

//////////////////////////////////////////////////////////////////////////
namespace {

#if ZEN_COMPILER_MSC
#	pragma warning(push)
#	pragma warning(disable : 4200)
#endif

	using ZipInt16 = uint16_t;

	struct ZipInt32
	{
				 operator uint32_t() const { return *(uint32_t*)Parts; }
		uint16_t Parts[2];
	};

	struct EocdRecord
	{
		enum : uint32_t
		{
			Magic = 0x0605'4b50,
		};
		ZipInt32 Signature;
		ZipInt16 ThisDiskIndex;
		ZipInt16 CdStartDiskIndex;
		ZipInt16 CdRecordThisDiskCount;
		ZipInt16 CdRecordCount;
		ZipInt32 CdSize;
		ZipInt32 CdOffset;
		ZipInt16 CommentSize;
		char	 Comment[];
	};

	struct CentralDirectoryRecord
	{
		enum : uint32_t
		{
			Magic = 0x0201'4b50,
		};

		ZipInt32 Signature;
		ZipInt16 VersionMadeBy;
		ZipInt16 VersionRequired;
		ZipInt16 Flags;
		ZipInt16 CompressionMethod;
		ZipInt16 LastModTime;
		ZipInt16 LastModDate;
		ZipInt32 Crc32;
		ZipInt32 CompressedSize;
		ZipInt32 OriginalSize;
		ZipInt16 FileNameLength;
		ZipInt16 ExtraFieldLength;
		ZipInt16 CommentLength;
		ZipInt16 DiskIndex;
		ZipInt16 InternalFileAttr;
		ZipInt32 ExternalFileAttr;
		ZipInt32 Offset;
		char	 FileName[];
	};

	struct LocalFileHeader
	{
		enum : uint32_t
		{
			Magic = 0x0403'4b50,
		};

		ZipInt32 Signature;
		ZipInt16 VersionRequired;
		ZipInt16 Flags;
		ZipInt16 CompressionMethod;
		ZipInt16 LastModTime;
		ZipInt16 LastModDate;
		ZipInt32 Crc32;
		ZipInt32 CompressedSize;
		ZipInt32 OriginalSize;
		ZipInt16 FileNameLength;
		ZipInt16 ExtraFieldLength;
		char	 FileName[];
	};

#if ZEN_COMPILER_MSC
#	pragma warning(pop)
#endif

}  // namespace

//////////////////////////////////////////////////////////////////////////
ZipFs::ZipFs(IoBuffer&& Buffer)
{
	MemoryView View = Buffer.GetView();

	uint8_t* Cursor = (uint8_t*)(View.GetData()) + View.GetSize();
	if (View.GetSize() < sizeof(EocdRecord))
	{
		return;
	}

	const auto* EocdCursor = (EocdRecord*)(Cursor - sizeof(EocdRecord));

	// It is more correct to search backwards for EocdRecord::Magic as the
	// comment can be of a variable length. But here we're not going to support
	// zip files with comments.
	if (EocdCursor->Signature != EocdRecord::Magic)
	{
		return;
	}

	// Zip64 isn't supported either
	if (EocdCursor->ThisDiskIndex == 0xffff)
	{
		return;
	}

	Cursor = (uint8_t*)EocdCursor - uint32_t(EocdCursor->CdOffset) - uint32_t(EocdCursor->CdSize);

	const auto* CdCursor = (CentralDirectoryRecord*)(Cursor + EocdCursor->CdOffset);
	for (int i = 0, n = EocdCursor->CdRecordCount; i < n; ++i)
	{
		const CentralDirectoryRecord& Cd = *CdCursor;

		bool Acceptable = true;
		Acceptable &= (Cd.OriginalSize > 0);		// has some content
		Acceptable &= (Cd.CompressionMethod == 0);	// is stored uncomrpessed
		if (Acceptable)
		{
			const uint8_t* Lfh = Cursor + Cd.Offset;
			if (uintptr_t(Lfh - Cursor) < View.GetSize())
			{
				std::string_view FileName(Cd.FileName, Cd.FileNameLength);
				m_Files.insert(std::make_pair(FileName, FileItem{Lfh, size_t(0)}));
			}
		}

		uint32_t ExtraBytes = Cd.FileNameLength + Cd.ExtraFieldLength + Cd.CommentLength;
		CdCursor			= (CentralDirectoryRecord*)(Cd.FileName + ExtraBytes);
	}

	m_Buffer = std::move(Buffer);
}

//////////////////////////////////////////////////////////////////////////
IoBuffer
ZipFs::GetFile(const std::string_view& FileName) const
{
	{
		RwLock::SharedLockScope _(m_FilesLock);

		FileMap::const_iterator Iter = m_Files.find(FileName);
		if (Iter == m_Files.end())
		{
			return {};
		}

		const FileItem& Item = Iter->second;
		if (Item.GetSize() > 0)
		{
			return IoBuffer(IoBuffer::Wrap, Item.GetData(), Item.GetSize());
		}
	}

	RwLock::ExclusiveLockScope _(m_FilesLock);

	FileItem& Item = m_Files.find(FileName)->second;
	if (Item.GetSize() > 0)
	{
		return IoBuffer(IoBuffer::Wrap, Item.GetData(), Item.GetSize());
	}

	const auto* Lfh = (LocalFileHeader*)(Item.GetData());
	Item			= MemoryView(Lfh->FileName + Lfh->FileNameLength + Lfh->ExtraFieldLength, Lfh->OriginalSize);
	return IoBuffer(IoBuffer::Wrap, Item.GetData(), Item.GetSize());
}

}  // namespace zen