-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
splash_oplus.php
497 lines (395 loc) · 14.8 KB
/
splash_oplus.php
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
/**
*
* Copyright (C) 2002-2025 NekoYuzu (MlgmXyysd) All Rights Reserved.
* Copyright (C) 2013-2025 MeowCat Studio All Rights Reserved.
* Copyright (C) 2020-2025 Meow Mobile All Rights Reserved.
*
*/
/**
*
* Magic Splash Logo! Wand (for OPlus Qualcomm devices)
*
* https://github.com/MlgmXyysd/Magic-Splash-Wand
*
* Tool for unpacking and packaging splash image for OPlus Qualcomm devices
*
* Environment requirement:
* - PHP 8.1.0+
* - GD Extension
*
* @author NekoYuzu (MlgmXyysd)
* @version 1.2
*
* All copyright in the software is not allowed to be deleted
* or changed without permission.
*
*/
/***************************************
* WARNING *
* Do NOT modify the codes below *
* WARNING *
***************************************/
/*************************
* Constants Start *
*************************/
$magic_header = "SPLASH LOGO!";
/***********************
* Constants End *
***********************/
/*************************
* Functions Start *
*************************/
/**
* Formatted log
* @param $m string optional Message
* @param $c string optional Color
* @param $p string optional Indicator
* @param $t string optional Type (Level)
* @author NekoYuzu (MlgmXyysd)
* @date 2024/04/14 14:00:03
*/
function logf(string $m = "", string $c = "", string $p = "-", string $t = "I"): void
{
$c = match (strtoupper($c)) {
"R" => "\033[31m",
"G" => "\033[32m",
"Y" => "\033[33m",
default => "",
};
$t = match (strtoupper($t)) {
"W" => "WARN",
"E" => "ERROR",
default => "INFO",
};
print(date("[Y-m-d] [H:i:s]") . " [" . $t . "] " . $p . " " . $c . $m . "\033[0m" . PHP_EOL);
}
/**
* Merge array slice to string with specified offset & size
* @param $payload array required Array slices
* @param $offset int optional Offset
* @param $size int optional Expected size
* @return string Merged string
* @author NekoYuzu (MlgmXyysd)
* @date 2024/04/14 14:02:35
*/
function merge(array $payload, int $offset = 0, int $size = 0): string
{
$end = $offset + $size - 1;
if ($size === 0) {
$end = count($payload) - 1;
}
$result = "";
for ($i = $offset; $i <= $end; $i++) {
if (count($payload) <= $i) {
$result .= chr(0);
} else {
$result .= $payload[$i];
}
}
return $result;
}
/**
* Transforms integer to little-endian unsigned format
* @param $data string|int required Data
* @return string Result
* @author NekoYuzu (MlgmXyysd)
* @date 2021/10/12 13:02:38
*/
function write_unsigned(string|int $data): string
{
return hex2bin(merge(array_reverse(str_split(sprintf("%08s", dechex((int)$data)), 2))));
}
/**
* Read a little-endian unsigned integer from the specified array slices & offset
* @param $payload array required Array slices
* @param $offset int required Offset
* @return int Result
* @author NekoYuzu (MlgmXyysd)
* @date 2021/10/12 09:32:50
*/
function read_unsigned(array $payload, int $offset): int
{
return hexdec(merge(array_reverse(str_split(bin2hex(merge($payload, $offset, 4)), 2))));
}
/**
* Print usage
* @param $self string required Self name
* @author NekoYuzu (MlgmXyysd)
* @date 2024/04/14 20:35:43
*/
function usage(string $self): void
{
logf("Usage:", "y", "*");
logf($self . " unpack <splash_img> [output_dir]", "y", "*");
logf(" Unpack a splash image", "y", "*");
logf($self . " repack <splash_dir> [output_img]", "y", "*");
logf(" Repack a splash image", "y", "*");
logf("Notes:", "y", "*");
logf("1. The default output dir is \"output\" and the", "y", "*");
logf(" default output image is \"splash.img\";", "y", "*");
logf("2. Supported formats are AVIF, BMP, GD2, GD, GIF,", "y", "*");
logf(" JPEG, PNG, TGA, WBMP, WEBP, XBM, XPM. But if", "y", "*");
logf(" it is a GIF, only get the first frame.", "y", "*");
}
/***********************
* Functions End *
***********************/
/**********************
* Banner Start *
**********************/
logf("******************************************", "g");
logf("* Magic Splash Logo! Wand *", "g");
logf("* for OPlus Qualcomm devices *", "g");
logf("* By NekoYuzu (MlgmXyysd) Version 1.2 *", "g");
logf("******************************************", "g");
logf("GitHub: https://github.com/MlgmXyysd");
logf("XDA: https://xdaforums.com/m/mlgmxyysd.8430637");
logf("X (Twitter): https://x.com/realMlgmXyysd");
logf("PayPal: https://paypal.me/MlgmXyysd");
logf("My Blog: https://www.neko.ink/");
logf("******************************************", "g");
/********************
* Banner End *
********************/
/********************
* Main Logic *
********************/
if ($argc < 2) {
usage($argv[0]);
exit();
}
ini_set("memory_limit", "-1");
if (strcasecmp($argv[1], "unpack") === 0) {
logf("Operation: Unpack splash image", "y");
if ($argc < 3) {
logf("No payload specified!", "r", "!", "e");
usage($argv[0]);
exit();
}
if (!is_readable($argv[2]) || !is_file($argv[2])) {
logf("Specified payload is inaccessible!", "r", "!", "e");
exit();
}
if ($argc > 3) {
$output = $argv[3];
} else {
$output = "output";
}
if (!file_exists($output)) {
@mkdir($output, 0777, true);
}
if (!is_writable($output)) {
logf("Output dir is inaccessible!", "r", "!", "e");
exit();
}
logf("Loading splash payload...");
$splash_contents = str_split(file_get_contents($argv[2]));
$splash_padding = merge($splash_contents, 0, 16384);
$splash_logo_header = str_split(merge($splash_contents, 16384, 16384));
$splash_payload_data = str_split(merge($splash_contents, 32768));
logf("Analyzing payload header...");
$splash_magic = merge($splash_logo_header, 0, 12);
if (strcmp($splash_magic, $magic_header) !== 0) {
logf("Magic header mismatched, except $magic_header, but found $splash_magic", "r", "!", "e");
exit();
}
logf("Magic header matched.");
$descriptions = array(rtrim(merge($splash_logo_header, 12, 64)), rtrim(merge($splash_logo_header, 76, 64)), rtrim(merge($splash_logo_header, 140, 64)), rtrim(merge($splash_logo_header, 204, 64)));
$splash_count = read_unsigned($splash_logo_header, 268);
$splash_version = read_unsigned($splash_logo_header, 272);
$splash_width = read_unsigned($splash_logo_header, 276);
$splash_height = read_unsigned($splash_logo_header, 280);
$splash_compress = read_unsigned($splash_logo_header, 284) === 1;
if ($splash_count <= 0) {
logf("Payload doesn't contain any images.", "r", "!", "e");
exit();
}
logf("Splash description: " . json_encode($descriptions));
logf("Splash version: $splash_version");
logf("Splash size: " . $splash_width . "x" . $splash_height);
logf("GZIP compression enabled: " . ($splash_compress ? "true" : "false"));
logf("Found $splash_count images in payload.");
$cursor = 288;
$splash_array = array();
$splash_names = array();
for ($i = 0; $i < $splash_count; $i++) {
$splash_offset = read_unsigned($splash_logo_header, $cursor);
$cursor += 4;
$splash_size = read_unsigned($splash_logo_header, $cursor);
$cursor += 4;
$splash_data = read_unsigned($splash_logo_header, $cursor);
$cursor += 4;
$splash_name = trim(merge($splash_logo_header, $cursor, 116));
$cursor += 116;
$splash_array[] = array($splash_offset, $splash_size, $splash_data, $splash_name);
$splash_names[] = $splash_name;
}
$type = $splash_compress ? "compressed" : "raw";
foreach ($splash_array as $data) {
logf("Extracting $type splash " . $data[3] . "...");
$payload = merge($splash_payload_data, $data[0], $data[2]);
if ($splash_compress) {
$payload = gzdecode($payload);
}
$size = strlen($payload);
if ($size !== $data[1]) {
logf(" Splash " . $data[3] . " size mismatched, except " . $data[1] . ", but found $size", "y", "-", "w");
} else {
logf(" Splash size matched.");
}
file_put_contents($output . DIRECTORY_SEPARATOR . $data[3] . ".bmp", $payload);
}
logf("Extracted " . count($splash_array) . " images.");
logf("Writing out payload metadata...");
$splash_description = array();
foreach ($descriptions as $desc) {
if (strcasecmp($desc, "") !== 0) {
$splash_description[] = base64_encode($desc);
}
}
$splash_metadata = array("c" => $splash_compress, "d" => $splash_description, "h" => $splash_height, "p" => base64_encode(gzencode($splash_padding)), "s" => $splash_names, "v" => $splash_version, "w" => $splash_width);
file_put_contents($output . DIRECTORY_SEPARATOR . ".metadata", json_encode($splash_metadata, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK));
} else if (strcasecmp($argv[1], "repack") === 0) {
logf("Operation: Repack splash image", "y");
if ($argc < 3) {
logf("No folder specified!", "r", "!", "e");
usage($argv[0]);
exit();
}
if (!is_readable($argv[2]) || !is_dir($argv[2])) {
logf("Specified folder is inaccessible!", "r", "!", "e");
exit();
}
$input = $argv[2];
if (!str_ends_with($input, DIRECTORY_SEPARATOR)) {
$input .= DIRECTORY_SEPARATOR;
}
if ($argc > 3) {
$output = $argv[3];
} else {
$output = "splash.img";
}
if (file_exists($output) && (!is_writable($output) || !is_file($output))) {
logf("Output file is inaccessible!", "r", "!", "e");
exit();
}
$metadata = $input . ".metadata";
if (!is_readable($metadata) || !is_file($metadata)) {
logf("Payload metadata file inaccessible!", "r", "!", "e");
exit();
}
logf("Loading payload metadata...");
$metadata = json_decode(file_get_contents($metadata), true);
if (!isset($metadata["c"], $metadata["d"], $metadata["h"], $metadata["p"], $metadata["s"], $metadata["v"], $metadata["w"])) {
logf("Invalid metadata format!", "r", "!", "e");
exit();
}
logf("Metadata contains " . count($metadata["s"]) . " registered images.");
logf("Processing input folder...");
$dir = @opendir($argv[2]);
if (!$dir) {
logf("Can't open specified folder!", "r", "!", "e");
exit();
}
$image_line = array();
while (($file = readdir($dir)) !== false) {
if (strcasecmp($file, ".") !== 0 && strcasecmp($file, "..") !== 0 && strcasecmp($file, ".metadata") !== 0) {
$file = $input . $file;
$file_info = pathinfo($file);
$image_info = @getimagesize($file);
$image_type = false;
if ($image_info) {
$image_type = match ($image_info[2]) {
IMAGETYPE_GIF => "GIF",
IMAGETYPE_JPEG, IMAGETYPE_JPEG2000 => "JPEG",
IMAGETYPE_PNG => "PNG",
IMAGETYPE_BMP => "BMP",
IMAGETYPE_WBMP => "WBMP",
IMAGETYPE_XBM => "XBM",
IMAGETYPE_WEBP => "WEBP",
IMAGETYPE_AVIF => "AVIF",
default => strtoupper($file_info["extension"])
};
}
if (isset($image_line[$file_info["filename"]]) && strcasecmp($image_line[$file_info["filename"]][1], "BMP") === 0) {
continue;
}
$image_line[$file_info["filename"]] = array($file_info["extension"], $image_type);
}
}
logf("Found " . count($image_line) . " images.");
$valid_images = array();
foreach ($metadata["s"] as $name) {
if (isset($image_line[$name])) {
$valid_images[] = array_merge(array($name), $image_line[$name]);
}
}
logf("Totally " . count($valid_images) . " images will be added.");
@closedir($dir);
$splash_count = count($valid_images);
$data_header = "";
$data_payload = "";
foreach ($valid_images as $data) {
logf("Parsing " . $data[0] . "...");
$file = $input . $data[0] . "." . $data[1];
$image = match ($data[2]) {
"AVIF" => imagecreatefromavif($file),
"BMP" => true,
"GD2" => imagecreatefromgd2($file),
"GD" => imagecreatefromgd($file),
"GIF" => imagecreatefromgif($file),
"JPEG" => imagecreatefromjpeg($file),
"PNG" => imagecreatefrompng($file),
"TGA" => imagecreatefromtga($file),
"WBMP" => imagecreatefromwbmp($file),
"WEBP" => imagecreatefromwebp($file),
"XBM" => imagecreatefromxbm($file),
"XPM" => imagecreatefromxpm($file),
default => imagecreatefromstring(file_get_contents($file))
};
if (!$image) {
logf(" Can't open image or image not supported, skipping...", "y", "-", "w");
$splash_count--;
continue;
}
if ($image === true) {
$image = file_get_contents($file);
} else {
logf(" Converting to BMP format...");
ob_start();
imagebmp($image);
imagedestroy($image);
$image = ob_get_clean();
}
if ($metadata["c"]) {
logf(" Compressing with GZIP...");
$splash_output = gzencode($image, 9);
} else {
$splash_output = $image;
}
$data_header .= write_unsigned(strlen($data_payload)) . write_unsigned(strlen($image)) . write_unsigned(strlen($splash_output)) . str_pad(substr($data[0], 0, 116), 116, chr(0));
$data_payload .= $splash_output;
}
logf("Constructing payload header...");
$payload = gzdecode(base64_decode($metadata["p"])) . $magic_header;
for ($i = 0; $i < count($metadata["d"]); $i++) {
if ($i >= 4) {
break;
}
$payload .= str_pad(substr(base64_decode($metadata["d"][$i]), 0, 64), 64, chr(0));
}
if (count($metadata["d"]) < 4) {
$payload .= str_pad("", 64 * (4 - count($metadata["d"])), chr(0));
}
$payload .= write_unsigned($splash_count) . write_unsigned($metadata["v"]) . write_unsigned($metadata["w"]) . write_unsigned($metadata["h"]) . write_unsigned($metadata["c"] ? 1 : 0);
logf("Merging payload...");
$payload = str_pad($payload . $data_header, 32768, chr(0)) . $data_payload;
logf("Writing out image...");
file_put_contents($output, $payload);
} else {
usage($argv[0]);
logf("Unknown operation: " . $argv[1], "r", "!", "e");
exit();
}
logf("All done, output is \"$output\".", "g");