Skip to content

Instantly share code, notes, and snippets.

@terremoth
Created December 7, 2021 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terremoth/09bf1af5a2a9a1341ed4e37f878d3fbd to your computer and use it in GitHub Desktop.
Save terremoth/09bf1af5a2a9a1341ed4e37f878d3fbd to your computer and use it in GitHub Desktop.
MBR viewer in PHP
<?php
$is_terminal = php_sapi_name() == "cli";
$td_tag_init = "\t".'<td>';
$td_tag_end = '</td>'.PHP_EOL;
$tr_tag_init = '<tr>'.PHP_EOL;
$tr_tag_end = '</tr>'.PHP_EOL;
$to_html_init = '&#';
$to_html_end = ';';
if ($is_terminal) {
$new_line_code = $is_terminal ? PHP_EOL : '<br>'.PHP_EOL;
$td_tag_init = null;
$td_tag_end = null;
$tr_tag_init = null;
$tr_tag_end = PHP_EOL;
$to_html_init = null;
$to_html_end = null;
}
if (!$is_terminal) {
?>
<!DOCTYPE html>
<html>
<head>
<title>Your MBR</title>
<meta charset="utf-8">
<style>html {font-size:1.3em;font-family: monospace;}table{white-space: pre;}</style>
</head>
<body>
<h1>Your MBR</h1>
<table border="1">
<?php
}
$disk = fopen('\\\\.\\C:', 'rb'); //escaping the escape
$bin_data = fread($disk, 512);
//$character_set = mb_detect_encoding($bin_data);
//$bin_data = mb_convert_encoding($bin_data, 'Windows-1252', $character_set);
$hex_data = bin2hex($bin_data);
$splitted_bin = str_split($bin_data, 2);
$splitted_hex = str_split($hex_data, 2);
$chunk_size = 16; // from 0 to 15 (16 bytes)
$data_size = count($splitted_hex);
$number_of_chunks = $data_size / $chunk_size;
$raw_string = '';
fclose($disk);
for ($chunk_counter = 0; $chunk_counter < $number_of_chunks; $chunk_counter++) {
echo $tr_tag_init;
echo $td_tag_init;
$until = $chunk_counter * $chunk_size;
for ($data_counter = $until; $data_counter < $until + $chunk_size; $data_counter++) {
echo $splitted_hex[$data_counter].' ';
}
echo $td_tag_end;
echo $td_tag_init;
for ($data_counter_ascii = $until; $data_counter_ascii < $until + $chunk_size; $data_counter_ascii++) {
$raw_string = $bin_data[$data_counter_ascii];
$ascii_code = ord($raw_string);
$dec_bin = $ascii_code < 32 ? '.' : $to_html_init.($is_terminal ? $raw_string : $ascii_code).$to_html_end;
echo $dec_bin;
}
echo $td_tag_end;
echo $tr_tag_end;
}
if (!$is_terminal) {
?>
</table>
</body>
</html>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment