Path: blob/master/src/applications/files/document/PhabricatorHexdumpDocumentEngine.php
12241 views
<?php12final class PhabricatorHexdumpDocumentEngine3extends PhabricatorDocumentEngine {45const ENGINEKEY = 'hexdump';67public function getViewAsLabel(PhabricatorDocumentRef $ref) {8return pht('View as Hexdump');9}1011protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) {12return 'fa-microchip';13}1415protected function getByteLengthLimit() {16return (1024 * 1024 * 1);17}1819protected function getContentScore(PhabricatorDocumentRef $ref) {20return 500;21}2223protected function canRenderDocumentType(PhabricatorDocumentRef $ref) {24return true;25}2627protected function canRenderPartialDocument(PhabricatorDocumentRef $ref) {28return true;29}3031protected function newDocumentContent(PhabricatorDocumentRef $ref) {32$limit = $this->getByteLengthLimit();33$length = $ref->getByteLength();3435$is_partial = false;36if ($limit) {37if ($length > $limit) {38$is_partial = true;39$length = $limit;40}41}4243$content = $ref->loadData(null, $length);4445$output = array();46$offset = 0;4748$lines = str_split($content, 16);49foreach ($lines as $line) {50$output[] = sprintf(51'%08x %- 23s %- 23s %- 16s',52$offset,53$this->renderHex(substr($line, 0, 8)),54$this->renderHex(substr($line, 8)),55$this->renderBytes($line));5657$offset += 16;58}5960$output = implode("\n", $output);6162$container = phutil_tag(63'div',64array(65'class' => 'document-engine-hexdump PhabricatorMonospaced',66),67$output);6869$message = null;70if ($is_partial) {71$message = $this->newMessage(72pht(73'This document is too large to be completely rendered inline. The '.74'first %s bytes are shown.',75new PhutilNumber($limit)));76}7778return array(79$message,80$container,81);82}8384private function renderHex($bytes) {85$length = strlen($bytes);8687$output = array();88for ($ii = 0; $ii < $length; $ii++) {89$output[] = sprintf('%02x', ord($bytes[$ii]));90}9192return implode(' ', $output);93}9495private function renderBytes($bytes) {96$length = strlen($bytes);9798$output = array();99for ($ii = 0; $ii < $length; $ii++) {100$chr = $bytes[$ii];101$ord = ord($chr);102103if ($ord < 0x20 || $ord >= 0x7F) {104$chr = '.';105}106107$output[] = $chr;108}109110return implode('', $output);111}112113}114115116