CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HLE/sceDeflt.cpp
Views: 1401
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.
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(Log::HLE, 0, "bad address");
31
}
32
33
auto crc32Addr = PSPPointer<u32_le>::Create(Crc32Addr);
34
if (Crc32Addr && !crc32Addr.IsValid()) {
35
return hleLogError(Log::HLE, 0, "bad crc32 address");
36
}
37
38
z_stream stream{};
39
u8 *outBufferPtr = Memory::GetPointerWrite(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(Log::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(Log::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
if (MemBlockInfoDetailed(stream.total_in, stream.total_out)) {
62
char tagData[128];
63
size_t tagSize = FormatMemWriteTagAt(tagData, sizeof(tagData), "sceDeflt/", InBuffer, stream.total_in);
64
NotifyMemInfo(MemBlockFlags::READ, InBuffer, stream.total_in, tagData, tagSize);
65
NotifyMemInfo(MemBlockFlags::WRITE, OutBuffer, stream.total_out, tagData, tagSize);
66
}
67
68
return hleLogSuccessI(Log::HLE, stream.total_out);
69
}
70
71
static int sceDeflateDecompress(u32 OutBuffer, int OutBufferLength, u32 InBuffer, u32 Crc32Addr) {
72
return CommonDecompress(-MAX_WBITS, OutBuffer, OutBufferLength, InBuffer, Crc32Addr);
73
}
74
75
static int sceGzipDecompress(u32 OutBuffer, int OutBufferLength, u32 InBuffer, u32 Crc32Addr) {
76
return CommonDecompress(16 + MAX_WBITS, OutBuffer, OutBufferLength, InBuffer, Crc32Addr);
77
}
78
79
static int sceZlibDecompress(u32 OutBuffer, int OutBufferLength, u32 InBuffer, u32 Crc32Addr) {
80
return CommonDecompress(MAX_WBITS, OutBuffer, OutBufferLength, InBuffer, Crc32Addr);
81
}
82
83
const HLEFunction sceDeflt[] = {
84
{0X0BA3B9CC, nullptr, "sceGzipGetCompressedData", '?', "" },
85
{0X106A3552, nullptr, "sceGzipGetName", '?', "" },
86
{0X1B5B82BC, nullptr, "sceGzipIsValid", '?', "" },
87
{0X2EE39A64, nullptr, "sceZlibAdler32", '?', "" },
88
{0X44054E03, &WrapI_UIUU<sceDeflateDecompress>, "sceDeflateDecompress", 'i', "xixp"},
89
{0X6A548477, nullptr, "sceZlibGetCompressedData", '?', "" },
90
{0X6DBCF897, &WrapI_UIUU<sceGzipDecompress>, "sceGzipDecompress", 'i', "xixp"},
91
{0X8AA82C92, nullptr, "sceGzipGetInfo", '?', "" },
92
{0XA9E4FB28, &WrapI_UIUU<sceZlibDecompress>, "sceZlibDecompress", 'i', "xixp"},
93
{0XAFE01FD3, nullptr, "sceZlibGetInfo", '?', "" },
94
{0XB767F9A0, nullptr, "sceGzipGetComment", '?', "" },
95
{0XE46EB986, nullptr, "sceZlibIsValid", '?', "" },
96
};
97
98
void Register_sceDeflt() {
99
RegisterModule("sceDeflt", ARRAY_SIZE(sceDeflt), sceDeflt);
100
}
101
102