Skip to content

Instantly share code, notes, and snippets.

@brcontainer
Last active February 16, 2018 14:35
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 brcontainer/24366009821b46862f1dfebc15b30a30 to your computer and use it in GitHub Desktop.
Save brcontainer/24366009821b46862f1dfebc15b30a30 to your computer and use it in GitHub Desktop.
<?php
/*
* @param string $filename Path from windows executable
* @param string $encoding You need single-byte-character output, use ISO-8859-1
* @param int $limit increase limit if you tried read a big file and it returns NULL
* @return null|array
*/
function getFileVersionInfo($filename, $encoding = 'UTF-8', $limit = 15728640)
{
$res = null;
$data = file_get_contents($filename, false, null, 0, $limit);
if (!$data) { //Read error return NULL
return $res;
}
if ($pos = strpos($data, mb_convert_encoding('VS_VERSION_INFO', 'UTF-16LE'))) {
$pos -= 6;
$six = unpack('v*', substr($data, $pos, 6));
$data = substr($data, $pos, $six[1]);
if ($pos = strpos($data, mb_convert_encoding('StringFileInfo', 'UTF-16LE'))) {
$pos += 54;
$res = [];
$six = unpack('v*', substr($data, $pos, 6));
$nuls = "\0\0\0";
while ($six[2]) {
$nul = strpos($data, $nuls, $pos+6)+1;
$key = mb_convert_encoding(substr($data, $pos+6, $nul-$pos-6), $encoding, 'UTF-16LE');
$val = mb_convert_encoding(substr($data, ceil(($nul+2)/4)*4, $six[2]*2-2), $encoding, 'UTF-16LE');
$res[$key] = $val;
$pos+= ceil($six[1]/4)*4;
$six = unpack('v*', substr($data, $pos, 6));
}
}
}
$data = null; //"helps" free memory
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment