1 // Copyright (c) 2012- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 #include "zlib.h"
19 
20 #include "Common/CommonTypes.h"
21 #include "Core/Debugger/MemBlockInfo.h"
22 #include "Core/HLE/HLE.h"
23 #include "Core/HLE/FunctionWrappers.h"
24 #include "Core/HLE/sceDeflt.h"
25 #include "Core/MemMap.h"
26 
27 // All the decompress functions are identical with only differing window bits.
CommonDecompress(int windowBits,u32 OutBuffer,int OutBufferLength,u32 InBuffer,u32 Crc32Addr)28 static int CommonDecompress(int windowBits, u32 OutBuffer, int OutBufferLength, u32 InBuffer, u32 Crc32Addr) {
29 	if (!Memory::IsValidAddress(OutBuffer) || !Memory::IsValidAddress(InBuffer)) {
30 		return hleLogError(HLE, 0, "bad address");
31 	}
32 
33 	auto crc32Addr = PSPPointer<u32_le>::Create(Crc32Addr);
34 	if (Crc32Addr && !crc32Addr.IsValid()) {
35 		return hleLogError(HLE, 0, "bad crc32 address");
36 	}
37 
38 	z_stream stream{};
39 	u8 *outBufferPtr = Memory::GetPointer(OutBuffer);
40 	stream.next_in = (Bytef*)Memory::GetPointer(InBuffer);
41 	// We don't know the available length, just let it use as much as it wants.
42 	stream.avail_in = (uInt)Memory::ValidSize(InBuffer, Memory::g_MemorySize);
43 	stream.next_out = outBufferPtr;
44 	stream.avail_out = (uInt)OutBufferLength;
45 
46 	int err = inflateInit2(&stream, windowBits);
47 	if (err != Z_OK) {
48 		return hleLogError(HLE, 0, "inflateInit2 failed %08x", err);
49 	}
50 	err = inflate(&stream, Z_FINISH);
51 	inflateEnd(&stream);
52 
53 	if (err != Z_STREAM_END) {
54 		return hleLogError(HLE, 0, "inflate failed %08x", err);
55 	}
56 	if (crc32Addr.IsValid()) {
57 		uLong crc = crc32(0L, Z_NULL, 0);
58 		*crc32Addr = crc32(crc, outBufferPtr, stream.total_out);
59 	}
60 
61 	const std::string tag = "sceDeflt/" + GetMemWriteTagAt(InBuffer, stream.total_in);
62 	NotifyMemInfo(MemBlockFlags::READ, InBuffer, stream.total_in, tag.c_str(), tag.size());
63 	NotifyMemInfo(MemBlockFlags::WRITE, OutBuffer, stream.total_out, tag.c_str(), tag.size());
64 
65 	return hleLogSuccessI(HLE, stream.total_out);
66 }
67 
sceDeflateDecompress(u32 OutBuffer,int OutBufferLength,u32 InBuffer,u32 Crc32Addr)68 static int sceDeflateDecompress(u32 OutBuffer, int OutBufferLength, u32 InBuffer, u32 Crc32Addr) {
69 	return CommonDecompress(-MAX_WBITS, OutBuffer, OutBufferLength, InBuffer, Crc32Addr);
70 }
71 
sceGzipDecompress(u32 OutBuffer,int OutBufferLength,u32 InBuffer,u32 Crc32Addr)72 static int sceGzipDecompress(u32 OutBuffer, int OutBufferLength, u32 InBuffer, u32 Crc32Addr) {
73 	return CommonDecompress(16 + MAX_WBITS, OutBuffer, OutBufferLength, InBuffer, Crc32Addr);
74 }
75 
sceZlibDecompress(u32 OutBuffer,int OutBufferLength,u32 InBuffer,u32 Crc32Addr)76 static int sceZlibDecompress(u32 OutBuffer, int OutBufferLength, u32 InBuffer, u32 Crc32Addr) {
77 	return CommonDecompress(MAX_WBITS, OutBuffer, OutBufferLength, InBuffer, Crc32Addr);
78 }
79 
80 const HLEFunction sceDeflt[] = {
81 	{0X0BA3B9CC, nullptr,                            "sceGzipGetCompressedData", '?', ""    },
82 	{0X106A3552, nullptr,                            "sceGzipGetName",           '?', ""    },
83 	{0X1B5B82BC, nullptr,                            "sceGzipIsValid",           '?', ""    },
84 	{0X2EE39A64, nullptr,                            "sceZlibAdler32",           '?', ""    },
85 	{0X44054E03, &WrapI_UIUU<sceDeflateDecompress>,  "sceDeflateDecompress",     'i', "xixp"},
86 	{0X6A548477, nullptr,                            "sceZlibGetCompressedData", '?', ""    },
87 	{0X6DBCF897, &WrapI_UIUU<sceGzipDecompress>,     "sceGzipDecompress",        'i', "xixp"},
88 	{0X8AA82C92, nullptr,                            "sceGzipGetInfo",           '?', ""    },
89 	{0XA9E4FB28, &WrapI_UIUU<sceZlibDecompress>,     "sceZlibDecompress",        'i', "xixp"},
90 	{0XAFE01FD3, nullptr,                            "sceZlibGetInfo",           '?', ""    },
91 	{0XB767F9A0, nullptr,                            "sceGzipGetComment",        '?', ""    },
92 	{0XE46EB986, nullptr,                            "sceZlibIsValid",           '?', ""    },
93 };
94 
Register_sceDeflt()95 void Register_sceDeflt() {
96 	RegisterModule("sceDeflt", ARRAY_SIZE(sceDeflt), sceDeflt);
97 }
98