Skip to content

Instantly share code, notes, and snippets.

@kraftb
Created February 27, 2014 19:45
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 kraftb/9257850 to your computer and use it in GitHub Desktop.
Save kraftb/9257850 to your computer and use it in GitHub Desktop.
Debugging a T3D File
#!/usr/bin/php
<?php
/**
* Returns the next content part form the fileresource (t3d), $fd
*
* @param pointer $fd File pointer
* @param boolean $unserialize If set, the returned content is unserialized into an array, otherwise you get the raw string
* @param string $name For error messages this indicates the section of the problem.
* @return string Data string
* @access private
* @see loadFile()
* @todo Define visibility
*/
function getNextFilePart($fd, $unserialize = 0, $name = '') {
$initStrLen = 32 + 1 + 1 + 1 + 10 + 1;
// Getting header data
$initStr = fread($fd, $initStrLen);
$initStrDat = explode(':', $initStr);
if (strstr($initStrDat[0], 'Warning') == FALSE) {
if ((string)$initStrDat[3] === '') {
$datString = fread($fd, (int)$initStrDat[2]);
fread($fd, 1);
if (md5($datString) === $initStrDat[0]) {
if ($initStrDat[1]) {
$datString = gzuncompress($datString);
}
return $unserialize ? unserialize($datString) : $datString;
} else {
die('MD5 check failed (' . $name . ')');
}
} else {
die('File read error: InitString had a wrong length. (' . $name . ')');
}
} else {
die('File read error: Warning message in file. (' . $initStr . fgets($fd) . ')');
}
}
$fd = fopen('test.t3d', 'rb');
$data = getNextFilePart($fd, 1);
print_r($data);
$data = getNextFilePart($fd, 1);
print_r($data);
$data = getNextFilePart($fd, 1);
// print_r($data);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment