-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip_json_unpacker.php
70 lines (58 loc) · 2.12 KB
/
gzip_json_unpacker.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
<?php
$tpl_main = <<<EOT
<style>
.error {border: 3px solid red; background-color: #fcc; padding: 2em; border-radius: 1em; font-weight: 900}
</style>
<h1>Binary String Unpacker</h1>
<p>This tool is to convert the hexadecimal representation of a json-encoded and then gzipped content into human-readable JSON format.</p>
<p>For example, copy the content of one record from the following result set into the below field and press the JSON Format Only button:<br>
<pre>SELECT hex(selection) FROM bet_intercept</pre>
</p>
<form action="" method="post">
Hexadecimal Data (in fce2...ff format, case-insensitive):
<input type="text" name="hex_string" value="#1#" placeholde="01FCE2...033C, paste the hexadecimal digits here" autofocus="autofocus"><br>
<input type="submit" name="btn_json_only" value="JSON Format Only">
<input type="submit" name="btn_all" value="All formats">
</form>
EOT;
$tpl_error_message = <<<EOT
<div class="error">#1#</div>
EOT;
$json_only = isset($_POST["btn_json_only"]);
$hex_string = trim($_POST["hex_string"]??"");
$html = str_replace("#1#", $hex_string, $tpl_main);
try {
if ($hex_string!="") {
$bin = hex2bin($hex_string);
if ($bin===false) {
throw new Exception("Error: wrong hexadecimal string. It must contain even number of hexadecimal digits.");
}
$uncompressed = gzuncompress($bin);
if ($uncompressed===false) {
throw new Exception("Error: uncompressing error.");
}
$json_decoded = json_decode($uncompressed);
if ($json_decoded===null) {
throw new Exception("Error: uncompressed content cannot be JSON-decoded.");
}
$json_formatted = json_encode($json_decoded, JSON_PRETTY_PRINT);
if ($json_only) {
header("Content-type: application/json");
print $uncompressed;
} else {
print $html;
print "<h2>Binary:</h2>";
var_dump($bin);
print "<h2>Uncompressed:</h2>";
var_dump($uncompressed);
print "<h2>JSON-decoded:</h2>";
print "<pre>";
print_r($json_formatted);
}
} else {
print $html;
}
} catch (Exception $e) {
print $html;
print str_replace("#1#", $e->getMessage(), $tpl_error_message);
}