Skip to content

Instantly share code, notes, and snippets.

@sfinktah
Created June 24, 2016 18:55
Show Gist options
  • Save sfinktah/f43967e504acdbd7942b46afb01a8eee to your computer and use it in GitHub Desktop.
Save sfinktah/f43967e504acdbd7942b46afb01a8eee to your computer and use it in GitHub Desktop.
<?php
define("EOF", false);
class DeMap {
var $tail = false;
var $debug = false;
var $fileName = false;
var $fileHandle = false;
var $global_header;
var $eof = false;
function DeMap($fn, $start = SEEK_CUR, $pos = 0x78) {
$this->fileName = $fn;
$this->fileHandle = fopen($this->fileName, "r");
if ($this->fileHandle === false) {
fprintf(STDERR, "File {$this->fileName} not found\n");
die();
}
$this->setpos(0x10);
$buf = $this->fread($this->fileHandle, 1<<17);
$clearText = gzinflate($buf);
if (!$clearText) {
die("Error inflating file");
}
$this->fileName .= ".clear";
file_put_contents($this->fileName, $clearText);
$this->fileHandle = fopen($this->fileName, "r");
if ($this->fileHandle === false) {
fprintf(STDERR, "File {$this->fileName} not found\n");
die();
}
if ($pos) {
$this->setpos($pos);
}
if (!$this->fileHandle) {
die("Couldn't open file {$this->fileName}");
return false;
}
while ($ymapDataBlock = $this->fread($this->fileHandle, 0x80)) {
if (strlen($ymapDataBlock) < 0x80) {
die("EOF");
}
$entity = array();
$entity["hash"] = dechex($this->strint( substr($ymapDataBlock, 0, 4)));
$entity["x"] = $this->strfloat(substr($ymapDataBlock, 0x18, 4));
$entity["y"] = $this->strfloat(substr($ymapDataBlock, 0x1c, 4));
$entity["z"] = $this->strfloat(substr($ymapDataBlock, 0x20, 4));
if ($this->debug) {
echo HexDump::make($ymapDataBlock);
print_r($entity);
}
printf("%08s\t%4.4f\t%4.4f\t%4.4f\n",
$entity["hash"],
$entity["x"][1],
$entity["y"][1],
$entity["z"][1]
);
}
}
function setpos($pos) {
return fseek($this->fileHandle, $pos, SEEK_SET);
}
function getpos() {
$fileLoc = ftell($this->fileHandle);
return $fileLoc;
}
function fread($fh, $len) {
if ($len == 0) {
fprintf(STDERR, "ZERO LEN REQUEST\n");
return "";
}
if ($this->tail) {
$fileLoc = ftell($fh);
}
do {
if ($this->tail) {
fseek($fh, $fileLoc); // Best not use lastReadPos or we might double-read opening line
}
$pkt = fread($fh, $len);
if (empty($pkt)) {
$this->eof = true;
fprintf(STDERR, "Empty Packet - EOF? tail:%d\n", $this->tail);
if ($this->tail) sleep(1);
}
} while (empty($pkt) && $this->tail);
return $pkt;
}
function strint32($binstring) {
for ($i=0; $i<4; $i++) {
$dword += ord(substr($binstring, $i, 1)) << (8*$i);
}
return (int)$dword;
}
function struint($binstring, $len = false, $reverse = false) {
if ($len === false) $len = strlen($binstring);
$r = "";
for ($i=0; $i<$len; $i++) {
$c = ord(substr($binstring, $i, 1));
$h = dechex($c);
$r = $reverse ? "$r$h" : "$h$r";
}
return hexdec($r);
}
function strfloat($binstring, $len = false, $reverse = false) {
return unpack('f', $binstring);
}
function strint($binstring, $len = false, $reverse = false) {
if ($len === false) $len = strlen($binstring);
$dword = 0;
if ($reverse) {
// I'm sure there's a clever clogs way to do this in one smooth move,
// but I don't care at 6am.
$j = $len - 1;
for ($i=0; $i<$len; $i++) {
$dword += ord(substr($binstring, $i, 1)) << (8*($j--));
}
} else {
for ($i=0; $i<$len; $i++) {
$dword += ord(substr($binstring, $i, 1)) << (8*$i);
}
}
return $dword;
}
}
if (!empty($argc) && strstr($argv[0], basename(__FILE__))) {
$d = new DeMap($argc > 1 ? $argv[1] : "bh1_21_strm_0.ymap");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment