예제 #1
0
 /**
  * Generate a HexBlock from the current position in the file stream.
  *
  * The pointer will be returned to the initial position when done, essentially leaving the file stream untouched.
  *
  * @param resource|string $data       a resource handle or a string.
  * @param int             $bytes      number of bytes to print. -1 will dump the entirety of the file.
  * @param bool            $encodeHTML Encode the special characters that may occur on the right panel.
  *
  * @return string  The generated Hex Block.
  */
 public static function createBlock($data, $bytes, $encodeHTML = true)
 {
     $out = "";
     /** @var resource $handle */
     $handle = null;
     $isLocalHandle = false;
     if (is_string($data)) {
         $handle = DebugHelpers::str2resource($data);
         $isLocalHandle = true;
     } else {
         if (is_resource($data)) {
             $handle = $data;
         } else {
             return $out;
         }
     }
     $stat = fstat($handle);
     $length = $stat['size'];
     $pos = ftell($handle);
     if ($bytes === -1) {
         $bytes = $length - $pos;
     }
     $endPos = $pos + $bytes;
     $realPos = $pos & 0xfffffff0;
     fseek($handle, $realPos, SEEK_SET);
     $isRangeOverreached = $pos + $bytes > $length;
     $rDigits = strlen(dechex($isRangeOverreached ? $length : $pos + $bytes));
     $rDigits += $rDigits % 2;
     if ($rDigits < 2) {
         $rDigits = 2;
     }
     $lDigits = strlen(dechex($bytes));
     $lDigits += $lDigits % 2;
     if ($lDigits < 2) {
         $lDigits = 2;
     }
     $out .= "Start: 0x" . str_pad(strtoupper(dechex($pos)), $rDigits, "0", STR_PAD_LEFT);
     $out .= "; Length: " . $bytes . " (0x" . str_pad(strtoupper(dechex($bytes)), $lDigits, "0", STR_PAD_LEFT) . ")\n";
     if ($isRangeOverreached) {
         $or = $pos + $bytes - $length;
         $out .= "* Requested length overreach by " . $or . " bytes, only " . ($bytes - $or) . " available in stream.\n";
         if ($bytes - $or <= 0) {
             fseek($handle, $pos, SEEK_SET);
             return $out;
         }
     }
     while (ftell($handle) < $endPos && !(feof($handle) || ftell($handle) === $length)) {
         $lsp = ftell($handle);
         $out .= str_pad(strtoupper(dechex($lsp)), $rDigits, "0", STR_PAD_LEFT) . ": ";
         $rp = "| ";
         for ($j = 0; $j < 16; $j++) {
             if ($j == 8) {
                 $rp .= " ";
                 $out .= " ";
             }
             if ($lsp + $j < $pos) {
                 $rp .= "-";
                 $out .= "-- ";
                 fread($handle, 1);
             } elseif ($lsp + $j >= $endPos || (feof($handle) || ftell($handle) === $length)) {
                 $rp .= "-";
                 $out .= "-- ";
             } else {
                 $d = fread($handle, 1);
                 $out .= bin2hex($d) . " ";
                 $od = ord($d);
                 if ($od < 0x20 || $od >= 0x7f) {
                     $d = '.';
                 }
                 $rp .= $encodeHTML ? htmlspecialchars($d) : $d;
             }
         }
         $out .= $rp . "\n";
     }
     $out .= "\n";
     if ($isLocalHandle) {
         fclose($handle);
     } else {
         fseek($handle, $pos, SEEK_SET);
     }
     return $out;
 }