Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/document/PhabricatorHexdumpDocumentEngine.php
12241 views
1
<?php
2
3
final class PhabricatorHexdumpDocumentEngine
4
extends PhabricatorDocumentEngine {
5
6
const ENGINEKEY = 'hexdump';
7
8
public function getViewAsLabel(PhabricatorDocumentRef $ref) {
9
return pht('View as Hexdump');
10
}
11
12
protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) {
13
return 'fa-microchip';
14
}
15
16
protected function getByteLengthLimit() {
17
return (1024 * 1024 * 1);
18
}
19
20
protected function getContentScore(PhabricatorDocumentRef $ref) {
21
return 500;
22
}
23
24
protected function canRenderDocumentType(PhabricatorDocumentRef $ref) {
25
return true;
26
}
27
28
protected function canRenderPartialDocument(PhabricatorDocumentRef $ref) {
29
return true;
30
}
31
32
protected function newDocumentContent(PhabricatorDocumentRef $ref) {
33
$limit = $this->getByteLengthLimit();
34
$length = $ref->getByteLength();
35
36
$is_partial = false;
37
if ($limit) {
38
if ($length > $limit) {
39
$is_partial = true;
40
$length = $limit;
41
}
42
}
43
44
$content = $ref->loadData(null, $length);
45
46
$output = array();
47
$offset = 0;
48
49
$lines = str_split($content, 16);
50
foreach ($lines as $line) {
51
$output[] = sprintf(
52
'%08x %- 23s %- 23s %- 16s',
53
$offset,
54
$this->renderHex(substr($line, 0, 8)),
55
$this->renderHex(substr($line, 8)),
56
$this->renderBytes($line));
57
58
$offset += 16;
59
}
60
61
$output = implode("\n", $output);
62
63
$container = phutil_tag(
64
'div',
65
array(
66
'class' => 'document-engine-hexdump PhabricatorMonospaced',
67
),
68
$output);
69
70
$message = null;
71
if ($is_partial) {
72
$message = $this->newMessage(
73
pht(
74
'This document is too large to be completely rendered inline. The '.
75
'first %s bytes are shown.',
76
new PhutilNumber($limit)));
77
}
78
79
return array(
80
$message,
81
$container,
82
);
83
}
84
85
private function renderHex($bytes) {
86
$length = strlen($bytes);
87
88
$output = array();
89
for ($ii = 0; $ii < $length; $ii++) {
90
$output[] = sprintf('%02x', ord($bytes[$ii]));
91
}
92
93
return implode(' ', $output);
94
}
95
96
private function renderBytes($bytes) {
97
$length = strlen($bytes);
98
99
$output = array();
100
for ($ii = 0; $ii < $length; $ii++) {
101
$chr = $bytes[$ii];
102
$ord = ord($chr);
103
104
if ($ord < 0x20 || $ord >= 0x7F) {
105
$chr = '.';
106
}
107
108
$output[] = $chr;
109
}
110
111
return implode('', $output);
112
}
113
114
}
115
116