aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/unarchiver/ungzip.cpp
blob: 0731a0ef8abf11fef5ae10ea81aafc633655258b (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
/*
 * ungzip.cpp
 * ----------
 * Purpose: Implementation file for extracting modules from .gz archives
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */

#include "stdafx.h"

#include "../common/FileReader.h"
#include "ungzip.h"

#if defined(MPT_WITH_ZLIB) || defined(MPT_WITH_MINIZ)

#if defined(MPT_WITH_ZLIB)
#include <zlib.h>
#elif defined(MPT_WITH_MINIZ)
#include <miniz/miniz.h>
#endif

#endif // MPT_WITH_ZLIB || MPT_WITH_MINIZ


OPENMPT_NAMESPACE_BEGIN


#if defined(MPT_WITH_ZLIB) || defined(MPT_WITH_MINIZ)


CGzipArchive::CGzipArchive(const FileReader &file) : ArchiveBase(file)
{
	inFile.Rewind();
	inFile.ReadStruct(header);

	// Check header data + file size
	if(header.magic1 != GZ_HMAGIC1 || header.magic2 != GZ_HMAGIC2 || header.method != GZ_HMDEFLATE || (header.flags & GZ_FRESERVED) != 0
		|| inFile.GetLength() <= sizeof(GZheader) + sizeof(GZtrailer))
	{
		return;
	}
	ArchiveFileInfo info;
	info.type = ArchiveFileType::Normal;
	contents.push_back(info);
}


CGzipArchive::~CGzipArchive()
{
	return;
}


bool CGzipArchive::ExtractFile(std::size_t index)
{
	if(index >= contents.size())
	{
		return false;
	}

	// Read trailer
	GZtrailer trailer;
	inFile.Seek(inFile.GetLength() - sizeof(GZtrailer));
	inFile.ReadStruct(trailer);

	// Continue reading header
	inFile.Seek(sizeof(GZheader));

	// Extra block present? (skip the extra data)
	if(header.flags & GZ_FEXTRA)
	{
		inFile.Skip(inFile.ReadUint16LE());
	}

	// Filename present? (ignore)
	if(header.flags & GZ_FNAME)
	{
		while(inFile.ReadUint8() != 0);
	}

	// Comment present? (ignore)
	if(header.flags & GZ_FCOMMENT)
	{
		while(inFile.ReadUint8() != 0);
	}

	// CRC16 present? (ignore)
	if(header.flags & GZ_FHCRC)
	{
		inFile.Skip(2);
	}

	// Well, this is a bit small when deflated.
	if(!inFile.CanRead(sizeof(GZtrailer)))
	{
		return false;
	}

	try
	{
		data.reserve(inFile.BytesLeft());
	} catch(...)
	{
		return false;
	}

	// Inflate!
	z_stream strm{};
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;
	strm.avail_in = 0;
	strm.next_in = Z_NULL;
	if(inflateInit2(&strm, -15) != Z_OK)
		return false;
	int retVal = Z_OK;
	uint32 crc = 0;
	auto bytesLeft = inFile.BytesLeft() - sizeof(GZtrailer);
	do
	{
		std::array<char, mpt::IO::BUFFERSIZE_SMALL> inBuffer, outBuffer;
		strm.avail_in = static_cast<uInt>(std::min(static_cast<FileReader::pos_type>(inBuffer.size()), bytesLeft));
		inFile.ReadStructPartial(inBuffer, strm.avail_in);
		strm.next_in = reinterpret_cast<Bytef *>(inBuffer.data());
		bytesLeft -= strm.avail_in;
		do
		{
			strm.avail_out = static_cast<uInt>(outBuffer.size());
			strm.next_out = reinterpret_cast<Bytef *>(outBuffer.data());
			retVal = inflate(&strm, Z_NO_FLUSH);
			const auto output = mpt::as_span(outBuffer.data(), outBuffer.data() + outBuffer.size() - strm.avail_out);
			crc = crc32(crc, reinterpret_cast<Bytef *>(output.data()), static_cast<uInt>(output.size()));
			data.insert(data.end(), output.begin(), output.end());
		} while(strm.avail_out == 0);
	} while(retVal == Z_OK && bytesLeft);
	inflateEnd(&strm);

	// Everything went OK? Check return code, number of written bytes and CRC32.
	return retVal == Z_STREAM_END && trailer.isize == static_cast<uint32>(strm.total_out) && trailer.crc32_ == crc;
}


#endif // MPT_WITH_ZLIB || MPT_WITH_MINIZ


OPENMPT_NAMESPACE_END