-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgss.cpp
215 lines (168 loc) · 4.56 KB
/
cgss.cpp
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
extern "C"{
#include "php.h"
#include "php_ini.h"
#include "zend.h"
#include "ext/standard/info.h"
#include "Zend/zend_exceptions.h"
}
#include <iostream>
#include <stdlib.h>
#include "php_cgss.hpp"
#include "cgss.hpp"
#include "CAcbFile.h"
#include "common.h"
static int le_cgss;
using namespace std;
using namespace cgss;
zend_class_entry *cgss_exception;
PHP_FUNCTION(hca2wav)
{
zend_string *result;
zend_string *hcaFile;
zend_string *waveFile;
zend_long key1;
zend_long key2;
const char *cHcaFile;
const char *cWaveFile;
ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_STR(hcaFile)
Z_PARAM_STR(waveFile)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(key1)
Z_PARAM_LONG(key2)
ZEND_PARSE_PARAMETERS_END();
cHcaFile = reinterpret_cast<const char*>((unsigned char *) ZSTR_VAL(hcaFile));
cWaveFile = reinterpret_cast<const char*>((unsigned char *) ZSTR_VAL(waveFile));
CHcaDecoderConfig config;
config.decodeFunc = CDefaultWaveGenerator::Decode16BitS;
config.waveHeaderEnabled = TRUE;
if(key1 != NULL){
config.cipherConfig.keyParts.key1 = (uint32_t)key1;
}else{
config.cipherConfig.keyParts.key1 = 0x12345678;
}
if(key2 != NULL){
config.cipherConfig.keyParts.key2 = (uint32_t)key2;
}else{
config.cipherConfig.keyParts.key2 = 0x90abcdef;
}
try{
CFileStream fileIn(cHcaFile, FileMode::OpenExisting, FileAccess::Read), fileOut(cWaveFile, FileMode::Create, FileAccess::Write);
CHcaDecoder hcaDecoder(&fileIn, config);
uint32_t read = 1;
static const uint32_t bufferSize = 1024;
uint8_t buffer[bufferSize];
while(read > 0){
read = hcaDecoder.Read(buffer, bufferSize, 0, bufferSize);
if(read > 0){
fileOut.Write(buffer, bufferSize, 0, read);
}
}
}catch(const CException &ex){
zend_throw_exception_ex(cgss_exception, ex.GetOpResult() TSRMLS_CC, ex.GetExceptionMessage().c_str());
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_FUNCTION(acbunpack)
{
zend_string *acbFile;
const char *filePath;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(acbFile)
ZEND_PARSE_PARAMETERS_END();
filePath = reinterpret_cast<const char*>((unsigned char *) ZSTR_VAL(acbFile));
if(!cgssHelperFileExists(filePath)){
zend_throw_exception_ex(cgss_exception, -1 TSRMLS_CC, "File %s does not exist or cannot be opened.", filePath);
RETURN_FALSE;
}
try{
CFileStream fileStream(filePath, FileMode::OpenExisting, FileAccess::Read);
CAcbFile acb(&fileStream, filePath);
acb.Initialize();
const string extractDir = GetFilePath(filePath) + "_acb_" + GetFileName(filePath) + "/";
MakeDirectories(extractDir);
const auto &fileNames = acb.GetFileNames();
uint32_t i = 0;
for(const auto &fileName : fileNames){
auto s = fileName;
auto isCueNonEmpty = !s.empty();
if(!isCueNonEmpty){
s = CAcbFile::GetSymbolicFileNameFromCueId(i);
}
auto extractPath = extractDir + s;
IStream *stream;
if(isCueNonEmpty){
stream = acb.OpenDataStream(s.c_str());
}else{
stream = acb.OpenDataStream(i);
}
if(stream){
CFileStream fs(extractPath.c_str(), FileMode::Create, FileAccess::Write);
common_utils::CopyStream(stream, &fs);
}else{
zend_throw_exception_ex(cgss_exception, -1 TSRMLS_CC, "Cue #%u (%s) cannot be retrieved.", i + 1, s.c_str());
RETURN_FALSE;
}
delete stream;
++i;
}
}catch (const CException &ex){
zend_throw_exception_ex(cgss_exception, ex.GetOpResult() TSRMLS_CC, ex.GetExceptionMessage().c_str());
RETURN_FALSE;
}
RETURN_TRUE;
}
PHP_MINIT_FUNCTION(cgss)
{
REGISTER_MAIN_LONG_CONSTANT("CGSS_HCA_KEY_1", 0xF27E3B22, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("CGSS_HCA_KEY_2", 0x00003657, CONST_PERSISTENT | CONST_CS);
zend_class_entry cgss_ex;
INIT_CLASS_ENTRY(cgss_ex, "CGSSException", NULL);
cgss_exception = zend_register_internal_class_ex(&cgss_ex, zend_exception_get_default());
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(cgss)
{
return SUCCESS;
}
PHP_RINIT_FUNCTION(cgss)
{
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(cgss)
{
return SUCCESS;
}
PHP_MINFO_FUNCTION(cgss)
{
php_info_print_table_start();
php_info_print_table_header(2, "cgss support", "enabled");
php_info_print_table_end();
}
const zend_function_entry cgss_functions[] = {
PHP_FE(hca2wav, NULL)
PHP_FE(acbunpack, NULL)
PHP_FE_END
};
zend_module_entry cgss_module_entry = {
STANDARD_MODULE_HEADER,
"cgss",
cgss_functions,
PHP_MINIT(cgss),
PHP_MSHUTDOWN(cgss),
PHP_RINIT(cgss),
PHP_RSHUTDOWN(cgss),
PHP_MINFO(cgss),
PHP_CGSS_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_CGSS
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(cgss)
#endif