FOSSology  4.4.0
Open Source License Compliance by Open Source Software
PagedHexResult.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4  Author: Andreas Würl
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\View;
10 
12 {
13  const BYTES_PER_LINE = 16;
14 
18  private $currentHexText;
19 
23  private $hexTexts;
24 
28  private $charText;
29 
33  private $charCount;
34 
38  private $lineCount;
42  private $highlightState;
43 
48  public function __construct($startOffset, HighlightState $highlightState)
49  {
50  parent::__construct($startOffset);
51  $this->highlightState = $highlightState;
52 
53  $this->resetLineData();
54  $this->lineCount = 0;
55  }
56 
57  public function getText()
58  {
59  $text = parent::getText();
60 
61  if ($this->charCount > 0) {
62  $text .= $this->createHexdumpLine();
63  }
64  return $text;
65  }
66 
67  public function appendMetaText($text)
68  {
69  if (strlen($text) > 0) {
70  $this->charText .= $text;
71  $this->currentHexText .= $text;
72  }
73  }
74 
79  protected function renderContentText($text)
80  {
81  do {
82  $usableCharacters = min(self::BYTES_PER_LINE - $this->charCount, strlen($text));
83  $usedCharacters = substr($text, 0, $usableCharacters);
84  $text = substr($text, $usableCharacters);
85  $escapedText = $this->encodeCharacters($usedCharacters);
86  $this->charText .= preg_replace("/\\s/", "&nbsp;", $escapedText);
87  $asHexStrings = $this->asHexStrings($usedCharacters);
88  if (strlen($this->currentHexText) > 0) {
89  $this->mergeMetaText($asHexStrings, 0);
90  }
91  $this->hexTexts = array_merge($this->hexTexts, $asHexStrings);
92  $this->charCount += strlen($usedCharacters);
93 
94  if ($this->charCount == self::BYTES_PER_LINE) {
95  $this->highlightState->closeOpenElements($this);
96  if (strlen($this->currentHexText) > 0) {
97  $this->mergeMetaText($this->hexTexts, count($this->hexTexts) - 1, false);
98  }
99 
100  $result = $this->createHexdumpLine();
101  parent::appendMetaText($result . "<br/>\n");
102  $this->resetLineData();
103  $this->highlightState->openExistingElements($this);
104  $this->lineCount++;
105  }
106  } while ($text);
107 
108  return "";
109  }
110 
115  private function asHexStrings($text)
116  {
117  $hexValues = array();
118  for ($i = 0; $i < strlen($text); $i ++) {
119  $hexValues[] = sprintf("%02x", ord($text[$i]));
120  }
121  return $hexValues;
122  }
123 
127  protected function createHexdumpLine()
128  {
129  $missingCharacters = self::BYTES_PER_LINE - $this->charCount;
130  $charTextFill = str_repeat("&nbsp;", $missingCharacters);
131  $hexTextFill = str_repeat(" __", $missingCharacters);
132 
133  $hexText = implode(" ", $this->hexTexts) . $hexTextFill;
134  $charText = $this->charText . $charTextFill;
135  $currentOffset = $this->getStartOffset() + $this->lineCount * self::BYTES_PER_LINE;
136  return "0x" . sprintf("%08X", $currentOffset) . " |" . $hexText . "| |" . $charText . "|";
137  }
138 
139  protected function resetLineData()
140  {
141  $this->currentHexText = "";
142  $this->hexTexts = array();
143  $this->charText = "";
144  $this->charCount = 0;
145  }
146 
153  protected function mergeMetaText(&$targetArray, $targetIndex, $prependMeta = true)
154  {
155  $targetArray[$targetIndex] =
156  $prependMeta
157  ? $this->currentHexText . $targetArray[$targetIndex]
158  : $targetArray[$targetIndex] . $this->currentHexText;
159  $this->currentHexText = "";
160  }
161 
166  protected function encodeCharacters($usedCharacters)
167  {
168  $encodedText = "";
169  for ($i = 0; $i < strlen($usedCharacters); $i ++) {
170  $character = $usedCharacters[$i];
171  $encodedText .= ctype_print($character) || ctype_space($character) ? htmlspecialchars($character) : '?';
172  }
173  return $encodedText;
174  }
175 }
mergeMetaText(&$targetArray, $targetIndex, $prependMeta=true)
__construct($startOffset, HighlightState $highlightState)
FUNCTION int min(int user_perm, int permExternal)
Get the minimum permission level required.
Definition: libfossagent.c:306