-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.cpp
337 lines (259 loc) · 9.21 KB
/
addon.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/************************************************************************************************
Kioo Media Player: A Player With Clear Crystal Sound, Extra Sharp Video, with a Beautiful Design.
Copyright (C) 2017 - 2020 Kioo Media Player <[email protected]>.
Homepage: https://www.kiooplayer.com
Developer: Godfrey E Laswai <[email protected]>
All rights reserved.
Use of this source code is governed by a BSD-3-Clause license that can be
found in the BSD-LICENSE file or see it here <https://opensource.org/licenses/BSD-3-Clause>.
*************************************************************************************************/
#include "addon.h"
//AddOn::AddOn(QObject *parent) : QObject(parent)
//{
// theHash = "";
//}
void AddOn::downloadSub(const QString &url,const QString &fileName, const QString &filePath) {
if (!m_isReady)
return;
m_isReady = false;
QString rFilePath = filePath;
rFilePath = rFilePath.left(rFilePath.lastIndexOf("/")).replace("file:","");
#if defined(Q_OS_UNIX)
if(rFilePath.startsWith("//")) {
rFilePath.replace("//","");
}
#elif defined(Q_OS_WIN)
if(rFilePath.startsWith("///")) {
rFilePath.replace("///","");
}
#endif
theSubFile = rFilePath +"/"+ fileName; // your filePath should end with a forward slash "/"
m_file = new QFile();
m_file->setFileName(theSubFile);
m_file->open(QIODevice::WriteOnly);
if (!m_file->isOpen()) {
// qDebug() << "Failed to open device";
m_isReady = true;
return; // TODO: permission check?
}
QNetworkAccessManager *manager = new QNetworkAccessManager;
QNetworkRequest request;
request.setUrl(QUrl(url));
QSslConfiguration configSsl = QSslConfiguration::defaultConfiguration();
configSsl.setProtocol(QSsl::AnyProtocol);
request.setSslConfiguration(configSsl);
// qDebug() << "The url is:"+url;
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(onSubComplete(QNetworkReply *)));
connect(manager, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)),this, SLOT(onSslError(QNetworkReply*,QList<QSslError>)));
manager->get(request);
}
void AddOn::setSubFile(QString ufile) {
// qDebug() << "Full path is: " +ufile;
QStringList list = ufile.split("|");
downloadSub(list[0],list[1],list[2]);
}
void AddOn::setSourceUrl(const QString &a) {
QString ab = a;
ab.replace("file:","");
#if defined(Q_OS_UNIX)
if(ab.startsWith("//")) {
ab.replace("//","");
}
#elif defined(Q_OS_WIN)
if(ab.startsWith("///")) {
ab.replace("///","");
}
#endif
QByteArray ba = ab.toLatin1();
const char *ch = ba.data();
ifstream f;
uint64_t myhash;
f.open(ch, ios::in|ios::binary|ios::ate);
if (!f.is_open()) {
cerr << "Error opening file" << endl;
}
myhash = compute_hash(f);
f.close();
theHash = QString::number( myhash, 16 );
emit sourceUrlChanged();
}
QString AddOn::sourceUrl() const {
return theHash;
}
QString AddOn::subFile() const {
return theSubFile;
}
void AddOn::onSslError(QNetworkReply *reply, const QList<QSslError> &errors)
{
reply->ignoreSslErrors();
}
void AddOn::onSubComplete(QNetworkReply *reply) {
if (!m_file->isWritable()) {
m_isReady = true;
return; // TODO: error check
}
QByteArray uncompressed_data = "";
QByteArray compressed_data = reply->readAll();
gzipDecompress(compressed_data,uncompressed_data);
m_file->write(uncompressed_data);
m_file->close(); // TODO: delete the file from the system later on
m_isReady = true;
emit subFileChanged();
}
bool AddOn::gzipDecompress(QByteArray input, QByteArray &output)
{
// Prepare output
output.clear();
// Is there something to do?
if(input.length() > 0)
{
// Prepare inflater status
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
// Initialize inflater
int ret = inflateInit2(&strm, GZIP_WINDOWS_BIT);
if (ret != Z_OK)
return(false);
// Extract pointer to input data
char *input_data = input.data();
int input_data_left = input.length();
// Decompress data until available
do {
// Determine current chunk size
int chunk_size = qMin(GZIP_CHUNK_SIZE, input_data_left);
// Check for termination
if(chunk_size <= 0)
break;
// Set inflater references
strm.next_in = (unsigned char*)input_data;
strm.avail_in = chunk_size;
// Update interval variables
input_data += chunk_size;
input_data_left -= chunk_size;
// Inflate chunk and cumulate output
do {
// Declare vars
char out[GZIP_CHUNK_SIZE];
// Set inflater references
strm.next_out = (unsigned char*)out;
strm.avail_out = GZIP_CHUNK_SIZE;
// Try to inflate chunk
ret = inflate(&strm, Z_NO_FLUSH);
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR;
case Z_DATA_ERROR:
case Z_MEM_ERROR:
case Z_STREAM_ERROR:
// Clean-up
inflateEnd(&strm);
// Return
return(false);
}
// Determine decompressed size
int have = (GZIP_CHUNK_SIZE - strm.avail_out);
// Cumulate result
if(have > 0)
output.append((char*)out, have);
} while (strm.avail_out == 0);
} while (ret != Z_STREAM_END);
// Clean-up
inflateEnd(&strm);
// Return
return (ret == Z_STREAM_END);
}
else
return(true);
}
bool AddOn::gzipCompress(QByteArray input, QByteArray &output, int level)
{
// Prepare output
output.clear();
// Is there something to do?
if(input.length())
{
// Declare vars
int flush = 0;
// Prepare deflater status
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
// Initialize deflater
int ret = deflateInit2(&strm, qMax(-1, qMin(9, level)), Z_DEFLATED, GZIP_WINDOWS_BIT, 8, Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
return(false);
// Prepare output
output.clear();
// Extract pointer to input data
char *input_data = input.data();
int input_data_left = input.length();
// Compress data until available
do {
// Determine current chunk size
int chunk_size = qMin(GZIP_CHUNK_SIZE, input_data_left);
// Set deflater references
strm.next_in = (unsigned char*)input_data;
strm.avail_in = chunk_size;
// Update interval variables
input_data += chunk_size;
input_data_left -= chunk_size;
// Determine if it is the last chunk
flush = (input_data_left <= 0 ? Z_FINISH : Z_NO_FLUSH);
// Deflate chunk and cumulate output
do {
// Declare vars
char out[GZIP_CHUNK_SIZE];
// Set deflater references
strm.next_out = (unsigned char*)out;
strm.avail_out = GZIP_CHUNK_SIZE;
// Try to deflate chunk
ret = deflate(&strm, flush);
// Check errors
if(ret == Z_STREAM_ERROR)
{
// Clean-up
deflateEnd(&strm);
// Return
return(false);
}
// Determine compressed size
int have = (GZIP_CHUNK_SIZE - strm.avail_out);
// Cumulate result
if(have > 0)
output.append((char*)out, have);
} while (strm.avail_out == 0);
} while (flush != Z_FINISH);
// Clean-up
(void)deflateEnd(&strm);
// Return
return(ret == Z_STREAM_END);
}
else
return(true);
}
int AddOn::MAX(int x, int y)
{
if((x) > (y))
return x;
else
return y;
}
uint64_t AddOn::compute_hash(ifstream &f)
{
uint64_t hash, fsize;
f.seekg(0, ios::end);
fsize = f.tellg();
f.seekg(0, ios::beg);
hash = fsize;
for(uint64_t tmp = 0, i = 0; i < 65536/sizeof(tmp) && f.read((char*)&tmp, sizeof(tmp)); i++, hash += tmp);
f.seekg(MAX(0, (uint64_t)fsize - 65536), ios::beg);
for(uint64_t tmp = 0, i = 0; i < 65536/sizeof(tmp) && f.read((char*)&tmp, sizeof(tmp)); i++, hash += tmp);
return hash;
}