3 final class PhabricatorHexdumpDocumentEngine
4 extends PhabricatorDocumentEngine
{
6 const ENGINEKEY
= 'hexdump';
8 public function getViewAsLabel(PhabricatorDocumentRef
$ref) {
9 return pht('View as Hexdump');
12 protected function getDocumentIconIcon(PhabricatorDocumentRef
$ref) {
13 return 'fa-microchip';
16 protected function getByteLengthLimit() {
17 return (1024 * 1024 * 1);
20 protected function getContentScore(PhabricatorDocumentRef
$ref) {
24 protected function canRenderDocumentType(PhabricatorDocumentRef
$ref) {
28 protected function canRenderPartialDocument(PhabricatorDocumentRef
$ref) {
32 protected function newDocumentContent(PhabricatorDocumentRef
$ref) {
33 $limit = $this->getByteLengthLimit();
34 $length = $ref->getByteLength();
38 if ($length > $limit) {
44 $content = $ref->loadData(null, $length);
49 $lines = str_split($content, 16);
50 foreach ($lines as $line) {
52 '%08x %- 23s %- 23s %- 16s',
54 $this->renderHex(substr($line, 0, 8)),
55 $this->renderHex(substr($line, 8)),
56 $this->renderBytes($line));
61 $output = implode("\n", $output);
63 $container = phutil_tag(
66 'class' => 'document-engine-hexdump PhabricatorMonospaced',
72 $message = $this->newMessage(
74 'This document is too large to be completely rendered inline. The '.
75 'first %s bytes are shown.',
76 new PhutilNumber($limit)));
85 private function renderHex($bytes) {
86 $length = strlen($bytes);
89 for ($ii = 0; $ii < $length; $ii++
) {
90 $output[] = sprintf('%02x', ord($bytes[$ii]));
93 return implode(' ', $output);
96 private function renderBytes($bytes) {
97 $length = strlen($bytes);
100 for ($ii = 0; $ii < $length; $ii++
) {
104 if ($ord < 0x20 ||
$ord >= 0x7F) {
111 return implode('', $output);