Repository navigation
Expand file tree
/
Copy pathbytecode-info
More file actions
executable file
·103 lines (92 loc) · 2.9 KB
/
Copy pathbytecode-info
File metadata and controls
executable file
·103 lines (92 loc) · 2.9 KB
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
#!/usr/bin/env php
<?php
function fail(string $message, int $code = 1): never
{
fwrite(STDERR, $message . PHP_EOL);
exit($code);
}
function u32le(string $bytes, int &$offset): int
{
if (strlen($bytes) < $offset + 4) {
fail('truncated container');
}
$value = unpack('V', substr($bytes, $offset, 4))[1];
$offset += 4;
return $value;
}
function u64le(string $bytes, int &$offset): int
{
if (strlen($bytes) < $offset + 8) {
fail('truncated container');
}
$parts = unpack('Vlo/Vhi', substr($bytes, $offset, 8));
$offset += 8;
return $parts['lo'] + ($parts['hi'] << 32);
}
function take(string $bytes, int &$offset, int $length): string
{
if ($length < 0 || strlen($bytes) < $offset + $length) {
fail('truncated container');
}
$value = substr($bytes, $offset, $length);
$offset += $length;
return $value;
}
function bytc_info(string $path): array
{
$bytes = file_get_contents($path);
if ($bytes === false) {
fail("cannot read {$path}");
}
if (substr($bytes, 0, 4) !== 'BYTC') {
fail("not a BYTC container: {$path}");
}
$offset = 4;
$version = u32le($bytes, $offset);
if ($version !== 1 && $version !== 2) {
fail("{$path}: unsupported container version {$version}");
}
$phpVersionId = u32le($bytes, $offset);
$backendId = take($bytes, $offset, u32le($bytes, $offset));
$backendFormat = take($bytes, $offset, u32le($bytes, $offset));
$cipherId = null;
$keyId = '';
if ($version === 2) {
$cipherId = u32le($bytes, $offset);
$keyId = take($bytes, $offset, u32le($bytes, $offset));
}
$headerLength = $offset;
$nonce = take($bytes, $offset, u32le($bytes, $offset));
$tag = take($bytes, $offset, u32le($bytes, $offset));
$ciphertextLength = u64le($bytes, $offset);
$ciphertext = take($bytes, $offset, $ciphertextLength);
if ($offset !== strlen($bytes)) {
fail('container has trailing bytes');
}
return [
'path' => $path,
'container' => 'BYTC' . $version,
'container_version' => $version,
'php_version_id' => $phpVersionId,
'backend' => $backendId,
'backend_format' => $backendFormat,
'cipher_id' => $cipherId,
'key_id' => $keyId === '' ? null : bin2hex($keyId),
'header_size' => $headerLength,
'nonce_size' => strlen($nonce),
'tag_size' => strlen($tag),
'ciphertext_size' => strlen($ciphertext),
'size' => strlen($bytes),
'sha256' => hash('sha256', $bytes),
];
}
if ($argc !== 2 || in_array($argv[1] ?? '', ['-h', '--help'], true)) {
fwrite(STDERR, "Usage: bytecode-info <encoded-file>\n");
exit(($argc === 2 && in_array($argv[1], ['-h', '--help'], true)) ? 0 : 2);
}
foreach (bytc_info($argv[1]) as $key => $value) {
if ($value === null) {
continue;
}
echo $key, ': ', $value, PHP_EOL;
}