Subversion Repositories checksum-tools

Compare Revisions

Regard whitespace Rev 1 → Rev 2

/trunk/PHP/md5_generate.php
0,0 → 1,94
#!/usr/bin/php
<?php
 
/*
Copyright 2020 Daniel Marschall, ViaThinkSoft
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
 
// This script generates MD5 files
// If there is already an MD5 file existing, only new files get appended to the existing MD5 files.
 
function _rec($directory) {
if (!is_dir($directory)) {
die("Invalid directory path $directory\n");
}
 
$md5_file = $directory.'/'.basename($directory).'.md5';
 
if (file_exists($md5_file)) {
$existing_files = md5_get_files($md5_file);
} else {
$existing_files = array();
}
 
foreach (scandir($directory) as $file) {
if ($file === '.') continue;
if ($file === '..') continue;
if (strtolower($file) === 'thumbs.db') continue;
if (substr($file, -4) === '.md5') continue;
if (substr($file, -4) === '.sfv') continue;
 
$fullpath = $directory . '/' . $file;
if (is_dir($fullpath)) {
_rec($fullpath);
} else {
### echo "$fullpath\n";
$dir = pathinfo($fullpath, PATHINFO_DIRNAME);
 
if (!file_exists($md5_file)) {
file_put_contents($md5_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM
}
 
if (!in_array($file, $existing_files)) {
$md5 = strtolower(md5_file($fullpath));
file_put_contents($md5_file, "$md5 *$file\r\n", FILE_APPEND);
}
}
}
}
 
function md5_get_files($filename) {
$out = array();
$lines = file($filename);
foreach ($lines as $line) {
$line = str_replace("\xEF\xBB\xBF",'',$line);
$line = trim($line);
if ($line == '') continue;
$line = str_replace('*', ' ', $line);
$line = str_replace("\t", ' ', $line);
list($checksum, $origname) = explode(' ', $line, 2);
$origname = dirname($filename) . '/' . trim($origname);
$checksum = trim($checksum);
$out[] = $origname;
}
 
return $out;
}
 
# ---
 
if ($argc != 2) {
echo "Syntax: $argv[0] <directory>\n";
exit(2);
}
 
if (!is_dir($argv[1])) {
echo "Directory not found\n";
exit(1);
}
 
_rec($argv[1]);
 
echo "Done.\n";
/trunk/PHP/md5_verify.php
0,0 → 1,93
#!/usr/bin/php
<?php
 
/*
Copyright 2020 Daniel Marschall, ViaThinkSoft
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
 
function testmd5($file) {
// TODO: warn if an entry is multiple times (with different checksums) in a single file
if (!file_exists($file)) {
echo "ERROR: File $file does not exist.\n";
return;
}
 
$lines = file($file);
foreach ($lines as $line) {
$line = str_replace("\xEF\xBB\xBF",'',$line);
$line = trim($line);
if ($line == '') continue;
$line = str_replace('*', ' ', $line);
$line = str_replace("\t", ' ', $line);
list($checksum, $origname) = explode(' ', $line, 2);
$origname = dirname($file) . '/' . trim($origname);
$checksum = trim($checksum);
if (!file_exists($origname)) {
echo "WARNING: File vanished : $origname\n";
} else {
$checksum2 = md5_file($origname);
if (strtolower($checksum) != strtolower($checksum2)) {
echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n";
} else {
//echo "OK: $origname\n";
}
}
// TODO: Also warn about extra files which are not indexed
}
}
 
function _rec($directory) {
if (!is_dir($directory)) {
exit("Invalid directory path $directory\n");
}
 
if ($dont_add_files = count(glob("$directory/*.md5")) == 0) {
// echo "Directory $directory has no MD5 file. Skipping.\n";
} else {
$out = array();
 
// echo "Check $directory\n";
$md5files = glob('*.md5');
foreach ($md5files as $md5file) {
testmd5($md5file);
}
}
 
foreach (scandir($directory) as $file) {
if ($file !== '.' && $file !== '..') {
$file = $directory . '/' . $file;
if (is_dir($file)) {
_rec($file);
}
}
}
}
 
 
# ---
 
if ($argc != 2) {
echo "Syntax: $argv[0] <directory>\n";
exit(2);
}
 
if (!is_dir($argv[1])) {
echo "Directory not found\n";
exit(1);
}
 
_rec($argv[1]);
 
echo "Done.\n";
/trunk/PHP/sfv_generate.php
0,0 → 1,103
#!/usr/bin/php
<?php
 
/*
Copyright 2020 Daniel Marschall, ViaThinkSoft
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
 
// This script generates SFV files
// If there is already an SFV file existing, only new files get appended to the existing SFV files.
 
function _rec($directory) {
if (!is_dir($directory)) {
die("Invalid directory path $directory\n");
}
 
$sfv_file = $directory.'/'.basename($directory).'.sfv';
 
if (file_exists($sfv_file)) {
$existing_files = sfv_get_files($sfv_file);
} else {
$existing_files = array();
}
 
foreach (scandir($directory) as $file) {
if ($file === '.') continue;
if ($file === '..') continue;
if (strtolower($file) === 'thumbs.db') continue;
if (substr($file, -4) === '.md5') continue;
if (substr($file, -4) === '.sfv') continue;
 
$fullpath = $directory . '/' . $file;
if (is_dir($fullpath)) {
_rec($fullpath);
} else {
### echo "$fullpath\n";
$dir = pathinfo($fullpath, PATHINFO_DIRNAME);
 
if (!file_exists($sfv_file)) {
file_put_contents($sfv_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM
}
 
if (!in_array($file, $existing_files)) {
$crc32 = strtoupper(crc32_file($fullpath));
file_put_contents($sfv_file, "$file $crc32\r\n", FILE_APPEND);
}
}
}
}
 
function sfv_get_files($filename) {
$out = array();
$lines = file($filename);
foreach ($lines as $line) {
$line = rtrim($line);
if ($line == '') continue;
if (substr($line,0,1) == ';') continue;
$out[] = substr($line, 0, strrpos($line, ' '));
 
}
return $out;
}
 
function swapEndianness($hex) {
return implode('', array_reverse(str_split($hex, 2)));
}
 
function crc32_file($filename, $rawOutput = false) {
$out = bin2hex(hash_file ('crc32b', $filename , true));
if (hash('crc32b', 'TEST') == 'b893eaee') {
// hash_file() in PHP 5.2 has the wrong Endianess!
// https://bugs.php.net/bug.php?id=47467
$out = swapEndianness($out);
}
return $out;
}
 
# ---
 
if ($argc != 2) {
echo "Syntax: $argv[0] <directory>\n";
exit(2);
}
 
if (!is_dir($argv[1])) {
echo "Directory not found\n";
exit(1);
}
 
_rec($argv[1]);
 
echo "Done.\n";
/trunk/PHP/sfv_verify.php
0,0 → 1,105
#!/usr/bin/php
<?php
 
/*
Copyright 2020 Daniel Marschall, ViaThinkSoft
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
 
function testsfv($file) {
// TODO: warn if an entry is multiple times (with different checksums) in a single file
if (!file_exists($file)) {
echo "ERROR: File $file does not exist.\n";
return;
}
 
$lines = file($file);
foreach ($lines as $line) {
$line = str_replace("\xEF\xBB\xBF",'',$line);
$line = rtrim($line);
if ($line == '') continue;
$checksum = substr($line,-8);
$origname = rtrim(substr($line,0,strlen($line)-8));
$origname = dirname($file) . '/' . trim($origname);
if (!file_exists($origname)) {
echo "WARNING: File vanished : $origname\n";
} else {
$checksum2 = crc32_file($origname);
if (strtolower($checksum) != strtolower($checksum2)) {
echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n";
} else {
//echo "OK: $origname\n";
}
}
// TODO: Also warn about extra files which are not indexed
}
}
 
function swapEndianness($hex) {
return implode('', array_reverse(str_split($hex, 2)));
}
 
function crc32_file($filename, $rawOutput = false) {
$out = bin2hex(hash_file ('crc32b', $filename , true));
if (hash('crc32b', 'TEST') == 'b893eaee') {
// hash_file() in PHP 5.2 has the wrong Endianess!
// https://bugs.php.net/bug.php?id=47467
$out = swapEndianness($out);
}
return $out;
}
 
function _rec($directory) {
if (!is_dir($directory)) {
exit("Invalid directory path $directory\n");
}
 
if ($dont_add_files = count(glob("$directory/*.sfv")) == 0) {
// echo "Directory $directory has no SFV file. Skipping.\n";
} else {
$out = array();
 
// echo "Check $directory\n";
$sfvfiles = glob('*.sfv');
foreach ($sfvfiles as $sfvfile) {
testsfv($sfvfile);
}
}
 
foreach (scandir($directory) as $file) {
if ($file !== '.' && $file !== '..') {
$file = $directory . '/' . $file;
if (is_dir($file)) {
_rec($file);
}
}
}
}
 
 
# ---
 
if ($argc != 2) {
echo "Syntax: $argv[0] <directory>\n";
exit(2);
}
 
if (!is_dir($argv[1])) {
echo "Directory not found\n";
exit(1);
}
 
_rec($argv[1]);
 
echo "Done.\n";