FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
PagedHexResult.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 Author: Andreas Würl
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\View;
21 
23 {
24  const BYTES_PER_LINE = 16;
25 
29  private $currentHexText;
30 
34  private $hexTexts;
35 
39  private $charText;
40 
44  private $charCount;
45 
49  private $lineCount;
53  private $highlightState;
54 
59  public function __construct($startOffset, HighlightState $highlightState)
60  {
61  parent::__construct($startOffset);
62  $this->highlightState = $highlightState;
63 
64  $this->resetLineData();
65  $this->lineCount = 0;
66  }
67 
68  public function getText()
69  {
70  $text = parent::getText();
71 
72  if ($this->charCount > 0) {
73  $text .= $this->createHexdumpLine();
74  }
75  return $text;
76  }
77 
78  public function appendMetaText($text)
79  {
80  if (strlen($text) > 0) {
81  $this->charText .= $text;
82  $this->currentHexText .= $text;
83  }
84  }
85 
90  protected function renderContentText($text)
91  {
92  do {
93  $usableCharacters = min(self::BYTES_PER_LINE - $this->charCount, strlen($text));
94  $usedCharacters = substr($text, 0, $usableCharacters);
95  $text = substr($text, $usableCharacters);
96  $escapedText = $this->encodeCharacters($usedCharacters);
97  $this->charText .= preg_replace("/\\s/", "&nbsp;", $escapedText);
98  $asHexStrings = $this->asHexStrings($usedCharacters);
99  if (strlen($this->currentHexText) > 0) {
100  $this->mergeMetaText($asHexStrings, 0);
101  }
102  $this->hexTexts = array_merge($this->hexTexts, $asHexStrings);
103  $this->charCount += strlen($usedCharacters);
104 
105  if ($this->charCount == self::BYTES_PER_LINE) {
106  $this->highlightState->closeOpenElements($this);
107  if (strlen($this->currentHexText) > 0) {
108  $this->mergeMetaText($this->hexTexts, count($this->hexTexts) - 1, false);
109  }
110 
111  $result = $this->createHexdumpLine();
112  parent::appendMetaText($result . "<br/>\n");
113  $this->resetLineData();
114  $this->highlightState->openExistingElements($this);
115  $this->lineCount++;
116  }
117  } while ($text);
118 
119  return "";
120  }
121 
126  private function asHexStrings($text)
127  {
128  $hexValues = array();
129  for ($i = 0; $i < strlen($text); $i ++) {
130  $hexValues[] = sprintf("%02x", ord($text[$i]));
131  }
132  return $hexValues;
133  }
134 
138  protected function createHexdumpLine()
139  {
140  $missingCharacters = self::BYTES_PER_LINE - $this->charCount;
141  $charTextFill = str_repeat("&nbsp;", $missingCharacters);
142  $hexTextFill = str_repeat(" __", $missingCharacters);
143 
144  $hexText = implode(" ", $this->hexTexts) . $hexTextFill;
145  $charText = $this->charText . $charTextFill;
146  $currentOffset = $this->getStartOffset() + $this->lineCount * self::BYTES_PER_LINE;
147  return "0x" . sprintf("%08X", $currentOffset) . " |" . $hexText . "| |" . $charText . "|";
148  }
149 
150  protected function resetLineData()
151  {
152  $this->currentHexText = "";
153  $this->hexTexts = array();
154  $this->charText = "";
155  $this->charCount = 0;
156  }
157 
164  protected function mergeMetaText(&$targetArray, $targetIndex, $prependMeta = true)
165  {
166  $targetArray[$targetIndex] =
167  $prependMeta
168  ? $this->currentHexText . $targetArray[$targetIndex]
169  : $targetArray[$targetIndex] . $this->currentHexText;
170  $this->currentHexText = "";
171  }
172 
177  protected function encodeCharacters($usedCharacters)
178  {
179  $encodedText = "";
180  for ($i = 0; $i < strlen($usedCharacters); $i ++) {
181  $character = $usedCharacters[$i];
182  $encodedText .= ctype_print($character) || ctype_space($character) ? htmlspecialchars($character) : '?';
183  }
184  return $encodedText;
185  }
186 }
FUNCTION int min(int user_perm, int permExternal)
Get the minimum permission level required.
Definition: libfossagent.c:320
mergeMetaText(&$targetArray, $targetIndex, $prependMeta=true)
__construct($startOffset, HighlightState $highlightState)