Skip to content

Commit 779f700

Browse files
committed
Refactoring.
1 parent 2a7d80f commit 779f700

File tree

2 files changed

+86
-100
lines changed

2 files changed

+86
-100
lines changed

Changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Versioning guidelines for SemVer can be found at: http://www.semver.org/
77

88
=== Changes made since last versioned release ===
99

10-
(none)
10+
- [2018.06.28; Sub-minor code change; Maikuolan]: Refactoring.
1111

1212
=== Version/Release 0.2.0 ===
1313
MINOR RELEASE.

sigtool.php

Lines changed: 85 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* SigTool v0.2.1 (last modified: 2018.06.20).
3+
* SigTool v0.2.1 (last modified: 2018.06.28).
44
* Generates signatures for phpMussel using main.cvd and daily.cvd from ClamAV.
55
*
66
* Package location: GitHub <https://github.com/phpMussel/SigTool>.
@@ -16,7 +16,10 @@
1616
class SigTool
1717
{
1818
/** Script version. */
19-
public $Ver = '0.2.0';
19+
public $Ver = '0.2.1';
20+
21+
/** Last modified date. */
22+
public $Modified = '2018.06.28';
2023

2124
/** Script user agent. */
2225
public $UA = 'SigTool v%s (https://github.com/phpMussel/SigTool)';
@@ -391,22 +394,25 @@ public function fixPath($Path) {
391394
/** Fetch arguments. */
392395
$RunMode = !empty($argv[1]) ? strtolower($argv[1]) : '';
393396

397+
/** Initialise SigTool object. */
398+
$SigTool = new SigTool();
399+
394400
/** L10N. */
395401
$L10N = [
396-
'Help' =>
397-
" SigTool v0.2.0-DEV (last modified: 2017.09.05).\n" .
398-
" Generates signatures for phpMussel using main.cvd and daily.cvd from ClamAV.\n\n" .
399-
" Syntax:\n" .
400-
" \$ php sigtool.php [arguments]\n" .
401-
" Example:\n" .
402-
" php sigtool.php xpmd\n" .
403-
" Arguments (all are OFF by default; include to turn ON):\n" .
404-
" - No arguments: Display this help information.\n" .
405-
" - x Extract signature files from daily.cvd and main.cvd.\n" .
406-
" - p Process signature files for use with phpMussel.\n" .
407-
" - m Download main.cvd before processing.\n" .
408-
" - d Download daily.cvd before processing.\n" .
409-
" - u Update SigTool (redownloads sigtool.php and dies; no checks performed).\n\n",
402+
'Help' => sprintf(
403+
' SigTool v%1$s (last modified: %2$s).%3$s%4$s%5$s%6$s%7$s%8$s%9$s%10$s%11$s',
404+
$SigTool->Ver,
405+
$SigTool->Modified,
406+
"\n Generates signatures for phpMussel using main.cvd and daily.cvd from ClamAV.\n\n",
407+
" Syntax:\n \$ php sigtool.php [arguments]\n Example:\n php sigtool.php xpmd\n",
408+
" Arguments (all are OFF by default; include to turn ON):\n",
409+
" - No arguments: Display this help information.\n",
410+
" - x Extract signature files from daily.cvd and main.cvd.\n",
411+
" - p Process signature files for use with phpMussel.\n",
412+
" - m Download main.cvd before processing.\n",
413+
" - d Download daily.cvd before processing.\n",
414+
" - u Update SigTool (redownloads sigtool.php and dies; no checks performed).\n\n"
415+
),
410416
'Accessing' => ' Accessing %s ...',
411417
'Deleting' => ' Deleting %s ...',
412418
'Done' => " Done!\n",
@@ -432,9 +438,6 @@ public function fixPath($Path) {
432438
die($L10N['Help']);
433439
}
434440

435-
/** Initialise SigTool object. */
436-
$SigTool = new SigTool();
437-
438441
/**
439442
* We'll use Zürich time for our timezone (closest approximate to CET, and
440443
* required for our "Y.z.B" dates to actually make sense).
@@ -450,11 +453,10 @@ public function fixPath($Path) {
450453
$Terminate();
451454
}
452455
echo $L10N['Done'] . sprintf($L10N['Writing'], 'sigtool.php');
453-
if (file_put_contents($SigTool->fixPath(__DIR__ . '/sigtool.php'), $Data)) {
454-
echo $L10N['Done'];
455-
} else {
456+
if (!file_put_contents($SigTool->fixPath(__DIR__ . '/sigtool.php'), $Data)) {
456457
$Terminate();
457458
}
459+
echo $L10N['Done'];
458460
die;
459461
}
460462

@@ -467,11 +469,10 @@ public function fixPath($Path) {
467469
$Terminate();
468470
}
469471
echo $L10N['Done'] . sprintf($L10N['Writing'], 'main.cvd');
470-
if (file_put_contents($SigTool->fixPath(__DIR__ . '/main.cvd'), $Data)) {
471-
echo $L10N['Done'];
472-
} else {
472+
if (!file_put_contents($SigTool->fixPath(__DIR__ . '/main.cvd'), $Data)) {
473473
$Terminate();
474474
}
475+
echo $L10N['Done'];
475476
unset($Data);
476477
}
477478

@@ -484,11 +485,10 @@ public function fixPath($Path) {
484485
$Terminate();
485486
}
486487
echo $L10N['Done'] . sprintf($L10N['Writing'], 'daily.cvd');
487-
if (file_put_contents($SigTool->fixPath(__DIR__ . '/daily.cvd'), $Data)) {
488-
echo $L10N['Done'];
489-
} else {
488+
if (!file_put_contents($SigTool->fixPath(__DIR__ . '/daily.cvd'), $Data)) {
490489
$Terminate();
491490
}
491+
echo $L10N['Done'];
492492
unset($Data);
493493
}
494494

@@ -624,8 +624,8 @@ public function fixPath($Path) {
624624

625625
/** Main sequence. */
626626
foreach ([
627-
['daily.hdb', 'main.hdb', '~([0-9a-f]{32}\:[0-9]+\:)([^\n]+)\n~', "\\1\x1A\x20\x10\x10\\2\n", 'clamav.hdb', "\x20", 16777216],
628-
['daily.mdb', 'main.mdb', '~([0-9]+\:[0-9a-f]{32}\:)([^\n]+)\n~', "\\1\x1A\x20\x10\x10\\2\n", 'clamav.mdb', "\xA0", 16777216],
627+
['daily.hdb', 'main.hdb', '~([\da-f]{32}\:\d+\:)([^\n]+)\n~', "\\1\x1A\x20\x10\x10\\2\n", 'clamav.hdb', "\x20", 16777216],
628+
['daily.mdb', 'main.mdb', '~(\d+\:[\da-f]{32}\:)([^\n]+)\n~', "\\1\x1A\x20\x10\x10\\2\n", 'clamav.mdb', "\xA0", 16777216],
629629
['daily.ndb', 'main.ndb', '~^([^:\n]+\:)~m', "\x1A\x20\x10\x10\\1", 'clamav.ndb', false, 0],
630630
] as $Set) {
631631

@@ -769,6 +769,54 @@ public function fixPath($Path) {
769769
$SigsThis = 0;
770770
$Percent = '';
771771

772+
/** Signature type to standard signature file pointer correlations. */
773+
$CorrelationsStandard = [
774+
'clamav.db',
775+
'clamav_exe.db',
776+
'clamav_ole.db',
777+
'clamav.htdb',
778+
'clamav_email.db',
779+
'clamav_graphics.db',
780+
'clamav_elf.db',
781+
'clamav.ndb',
782+
'clamav_macho.db',
783+
'clamav_pdf.db',
784+
'clamav_swf.db',
785+
'clamav_java.db'
786+
];
787+
788+
/** Signature type to regex signature file pointer correlations. */
789+
$CorrelationsRegex = [
790+
'clamav_regex.db',
791+
'clamav_exe_regex.db',
792+
'clamav_ole_regex.db',
793+
'clamav_regex.htdb',
794+
'clamav_email_regex.db',
795+
'clamav_graphics_regex.db',
796+
'clamav_elf_regex.db',
797+
'clamav_regex.ndb',
798+
'clamav_macho_regex.db',
799+
'clamav_pdf_regex.db',
800+
'clamav_swf_regex.db',
801+
'clamav_java_regex.db'
802+
];
803+
804+
/** Target guess to signature type correlations. */
805+
$CorrelationsTargetGuess = [
806+
"\x11" => 1,
807+
"\x12" => 1,
808+
"\x13" => 1,
809+
"\x14" => 6,
810+
"\x15" => 9,
811+
"\x17" => 4,
812+
"\x19" => 12,
813+
"\x1B" => 5,
814+
"\x1C" => 2,
815+
"\x1D" => 3,
816+
"\x25" => 10,
817+
"\x26" => 11
818+
];
819+
772820
while (($Pos = strpos($FileData, "\n", $Offset)) !== false) {
773821
$Last = $Percent;
774822
$Percent = number_format(($SigsThis / $SigsNDB) * 100, 2) . '%';
@@ -821,31 +869,13 @@ public function fixPath($Path) {
821869
/** Try to avoid dumping into general signatures whenever possible. */
822870
if ($SigType === 0) {
823871
$TargetGuess = substr($SigName, 2, 1);
824-
if ($TargetGuess === "\x11" || $TargetGuess === "\x12" || $TargetGuess === "\x13") {
825-
$SigType = 1;
826-
} elseif ($TargetGuess === "\x14") {
827-
$SigType = 6;
828-
} elseif ($TargetGuess === "\x15") {
829-
$SigType = 9;
830-
} elseif ($TargetGuess === "\x17") {
831-
$SigType = 4;
832-
} elseif ($TargetGuess === "\x19") {
833-
$SigType = 12;
834-
} elseif ($TargetGuess === "\x1B") {
835-
$SigType = 5;
836-
} elseif ($TargetGuess === "\x1C") {
837-
$SigType = 2;
838-
} elseif ($TargetGuess === "\x1D") {
839-
$SigType = 3;
840-
} elseif ($TargetGuess === "\x25") {
841-
$SigType = 10;
842-
} elseif ($TargetGuess === "\x26") {
843-
$SigType = 11;
872+
if (!empty($CorrelationsTargetGuess[$TargetGuess])) {
873+
$SigType = $CorrelationsTargetGuess[$TargetGuess];
844874
}
845875
}
846876

847877
/** Assign to the appropriate signature file (regex). */
848-
if (preg_match('/[^a-f0-9*]/i', $SigHex)) {
878+
if (preg_match('/[^a-f\d*]/i', $SigHex)) {
849879

850880
/**
851881
* Handle PCRE conversion here (ClamAV to phpMussel formats).
@@ -892,30 +922,8 @@ public function fixPath($Path) {
892922
$ThisLine = $SigName . ':' . $SigHex . $StartStop . "\n";
893923

894924
/** Add to file based on signature type (regex). */
895-
if ($SigType === 0) {
896-
$FileSets['clamav_regex.db'] .= $ThisLine;
897-
} elseif ($SigType === 1) {
898-
$FileSets['clamav_exe_regex.db'] .= $ThisLine;
899-
} elseif ($SigType === 2) {
900-
$FileSets['clamav_ole_regex.db'] .= $ThisLine;
901-
} elseif ($SigType === 3) {
902-
$FileSets['clamav_regex.htdb'] .= $ThisLine;
903-
} elseif ($SigType === 4) {
904-
$FileSets['clamav_email_regex.db'] .= $ThisLine;
905-
} elseif ($SigType === 5) {
906-
$FileSets['clamav_graphics_regex.db'] .= $ThisLine;
907-
} elseif ($SigType === 6) {
908-
$FileSets['clamav_elf_regex.db'] .= $ThisLine;
909-
} elseif ($SigType === 7) {
910-
$FileSets['clamav_regex.ndb'] .= $ThisLine;
911-
} elseif ($SigType === 9) {
912-
$FileSets['clamav_macho_regex.db'] .= $ThisLine;
913-
} elseif ($SigType === 10) {
914-
$FileSets['clamav_pdf_regex.db'] .= $ThisLine;
915-
} elseif ($SigType === 11) {
916-
$FileSets['clamav_swf_regex.db'] .= $ThisLine;
917-
} elseif ($SigType === 12) {
918-
$FileSets['clamav_java_regex.db'] .= $ThisLine;
925+
if (!empty($CorrelationsRegex[$SigType])) {
926+
$FileSets[$CorrelationsRegex[$SigType]] .= $ThisLine;
919927
}
920928

921929
/** Assign to the appropriate signature file (non-regex). */
@@ -928,30 +936,8 @@ public function fixPath($Path) {
928936
$ThisLine = $SigName . ':' . $SigHex . $StartStop . "\n";
929937

930938
/** Add to file based on signature type (non-regex). */
931-
if ($SigType === 0) {
932-
$FileSets['clamav.db'] .= $ThisLine;
933-
} elseif ($SigType === 1) {
934-
$FileSets['clamav_exe.db'] .= $ThisLine;
935-
} elseif ($SigType === 2) {
936-
$FileSets['clamav_ole.db'] .= $ThisLine;
937-
} elseif ($SigType === 3) {
938-
$FileSets['clamav.htdb'] .= $ThisLine;
939-
} elseif ($SigType === 4) {
940-
$FileSets['clamav_email.db'] .= $ThisLine;
941-
} elseif ($SigType === 5) {
942-
$FileSets['clamav_graphics.db'] .= $ThisLine;
943-
} elseif ($SigType === 6) {
944-
$FileSets['clamav_elf.db'] .= $ThisLine;
945-
} elseif ($SigType === 7) {
946-
$FileSets['clamav.ndb'] .= $ThisLine;
947-
} elseif ($SigType === 9) {
948-
$FileSets['clamav_macho.db'] .= $ThisLine;
949-
} elseif ($SigType === 10) {
950-
$FileSets['clamav_pdf.db'] .= $ThisLine;
951-
} elseif ($SigType === 11) {
952-
$FileSets['clamav_swf.db'] .= $ThisLine;
953-
} elseif ($SigType === 12) {
954-
$FileSets['clamav_java.db'] .= $ThisLine;
939+
if (!empty($CorrelationsStandard[$SigType])) {
940+
$FileSets[$CorrelationsStandard[$SigType]] .= $ThisLine;
955941
}
956942

957943
}

0 commit comments

Comments
 (0)