Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native hash functions instead of obsolete external binaries #198

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions demos/demo.mp3header.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,23 +1080,6 @@ function CreateDeepArray($ArrayPath, $Separator, $Value) {
}
}

if (!function_exists('md5_file')) {
// Allan Hansen <ah@artemis.dk>
// md5_file() exists in PHP 4.2.0.
// The following works under UNIX only, but dies on windows
function md5_file($file) {
if (substr(php_uname(), 0, 7) == 'Windows') {
die('PHP 4.2.0 or newer required for md5_file()');
}

$file = str_replace('`', '\\`', $file);
if (preg_match("#^([0-9a-f]{32})[ \t\n\r]#i", `md5sum "$file"`, $r)) {
return $r[1];
}
return false;
}
}

if (!function_exists('md5_data')) {
// Allan Hansen <ah@artemis.dk>
// md5_data() - returns md5sum for a file from startuing position to absolute end position
Expand Down
99 changes: 16 additions & 83 deletions getid3/getid3.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,107 +751,38 @@ public static function SimpleXMLelement2array($XMLobject) {
}

/**
* self::md5_data() - returns md5sum for a file from startuing position to absolute end position
*
* @author Allan Hansen <ahØartemis*dk>
* Returns checksum for a file from starting position to absolute end position.
*
* @param string $file
* @param int $offset
* @param int $end
* @param string $algorithm
*
* @return string|false
* @throws Exception
* @throws getid3_exception
*/
public static function hash_data($file, $offset, $end, $algorithm) {
static $tempdir = '';
$windows_call = null;
$unix_call = null;
$hash_length = null;
$hash_function = null;
if (!self::intValueSupported($end)) {
return false;
}
switch ($algorithm) {
case 'md5':
$hash_function = 'md5_file';
$unix_call = 'md5sum';
$windows_call = 'md5sum.exe';
$hash_length = 32;
break;

case 'sha1':
$hash_function = 'sha1_file';
$unix_call = 'sha1sum';
$windows_call = 'sha1sum.exe';
$hash_length = 40;
break;

default:
throw new Exception('Invalid algorithm ('.$algorithm.') in self::hash_data()');
break;
if (!in_array($algorithm, array('md5', 'sha1'))) {
throw new getid3_exception('Invalid algorithm ('.$algorithm.') in self::hash_data()');
}
$size = $end - $offset;
while (true) {
if (GETID3_OS_ISWINDOWS) {

// It seems that sha1sum.exe for Windows only works on physical files, does not accept piped data
// Fall back to create-temp-file method:
if ($algorithm == 'sha1') {
break;
}

$RequiredFiles = array('cygwin1.dll', 'head.exe', 'tail.exe', $windows_call);
foreach ($RequiredFiles as $required_file) {
if (!is_readable(GETID3_HELPERAPPSDIR.$required_file)) {
// helper apps not available - fall back to old method
break 2;
}
}
$commandline = GETID3_HELPERAPPSDIR.'head.exe -c '.$end.' '.escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $file)).' | ';
$commandline .= GETID3_HELPERAPPSDIR.'tail.exe -c '.$size.' | ';
$commandline .= GETID3_HELPERAPPSDIR.$windows_call;

} else {

$commandline = 'head -c'.$end.' '.escapeshellarg($file).' | ';
$commandline .= 'tail -c'.$size.' | ';
$commandline .= $unix_call;
$size = $end - $offset;

}
if (preg_match('#(1|ON)#i', ini_get('safe_mode'))) {
//throw new Exception('PHP running in Safe Mode - backtick operator not available, using slower non-system-call '.$algorithm.' algorithm');
break;
}
return substr(`$commandline`, 0, $hash_length);
$fp = fopen($file, 'rb');
fseek($fp, $offset);
$ctx = hash_init($algorithm);
while ($size > 0) {
$buffer = fread($fp, min($size, getID3::FREAD_BUFFER_SIZE));
hash_update($ctx, $buffer);
$size -= getID3::FREAD_BUFFER_SIZE;
}
$hash = hash_final($ctx);
fclose($fp);

if (empty($tempdir)) {
// yes this is ugly, feel free to suggest a better way
require_once(dirname(__FILE__).'/getid3.php');
$getid3_temp = new getID3();
$tempdir = $getid3_temp->tempdir;
unset($getid3_temp);
}
// try to create a temporary file in the system temp directory - invalid dirname should force to system temp dir
if (($data_filename = tempnam($tempdir, 'gI3')) === false) {
// can't find anywhere to create a temp file, just fail
return false;
}

// Init
$result = false;

// copy parts of file
try {
self::CopyFileParts($file, $data_filename, $offset, $end - $offset);
$result = $hash_function($data_filename);
} catch (Exception $e) {
throw new Exception('self::CopyFileParts() failed in getid_lib::hash_data(): '.$e->getMessage());
}
unlink($data_filename);
return $result;
return $hash;
}

/**
Expand All @@ -862,6 +793,8 @@ public static function hash_data($file, $offset, $end, $algorithm) {
*
* @return bool
* @throws Exception
*
* @deprecated Unused, may be removed in future versions of getID3
*/
public static function CopyFileParts($filename_source, $filename_dest, $offset, $length) {
if (!self::intValueSupported($offset + $length)) {
Expand Down
2 changes: 1 addition & 1 deletion getid3/getid3.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public function __construct() {

// Needed for Windows only:
// Define locations of helper applications for Shorten, VorbisComment, MetaFLAC
// as well as other helper functions such as head, tail, md5sum, etc
// as well as other helper functions such as head, etc
// This path cannot contain spaces, but the below code will attempt to get the
// 8.3-equivalent path automatically
// IMPORTANT: This path must include the trailing slash
Expand Down
Binary file removed helperapps/md5sum.exe
Binary file not shown.
8 changes: 3 additions & 5 deletions helperapps/readme.helperapps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ Taken from http://www.cygwin.com/

Taken from http://unxutils.sourceforge.net/
* head.exe
* md5sum.exe
* tail.exe

Taken from http://ebible.org/mpj/software.htm
* sha1sum.exe

Taken from http://www.vorbis.com/download.psp
* vorbiscomment.exe
Expand All @@ -54,3 +49,6 @@ Changelog:

2003.12.29:
* Initial release

2019.07.24:
* Remove obsolete md5sum.exe, sha1sum.exe, tail.exe
Binary file removed helperapps/sha1sum.exe
Binary file not shown.
Binary file removed helperapps/tail.exe
Binary file not shown.