11<?php
22/**
3- * SigTool v0.2.1 (last modified: 2018.10.20 ).
3+ * SigTool v0.2.2 (last modified: 2018.10.23 ).
44 * Generates signatures for phpMussel using main.cvd and daily.cvd from ClamAV.
55 *
66 * Package location: GitHub <https://github.com/phpMussel/SigTool>.
1616class SigTool
1717{
1818 /** Script version. */
19- public $ Ver = '0.2.1 ' ;
19+ public $ Ver = '0.2.2 ' ;
2020
2121 /** Last modified date. */
22- public $ Modified = '2018.10.20 ' ;
22+ public $ Modified = '2018.10.23 ' ;
2323
2424 /** Script user agent. */
2525 public $ UA = 'SigTool v%s (https://github.com/phpMussel/SigTool) ' ;
@@ -378,7 +378,7 @@ public function shorthand(&$Data) {
378378 '~^[^\:\n]+\:[^\n]+[\[\]][^\n]*$~m ' ,
379379
380380 /** PCRE trips over capture groups at this range sometimes. Let's play it safe and ditch the affected signatures. */
381- '~^.*\{-? (?:\d{4,})\}.*$\n~m ' ,
381+ '~^.*\{(?:-? \d{4,}|\d{4,}- )\}.*$\n~m ' ,
382382
383383 /** Not needed in the final generated signature files. */
384384 '~^.*This ClamAV version has reached End of Life.*$\n~im '
@@ -886,12 +886,15 @@ public function fixPath($Path) {
886886 }
887887 }
888888
889+ /** Normalise to lower-case. */
890+ $ SigHex = strtolower ($ SigHex );
891+
889892 /** Assign to the appropriate signature file (regex). */
890893 if (preg_match ('/[^a-f\d*]/i ' , $ SigHex )) {
891894
892895 /** Convert from ClamAV's pattern syntax to PCRE syntax. */
893896 $ SigHex = preg_replace ([
894- '~^.*\{-? (?:\d{4,})\}.*$~ ' ,
897+ '~^.*\{(?:-? \d{4,}|\d{4,}- )\}.*$~ ' ,
895898 '~\{(\d+)-(?:\d{4,})?\}~ ' ,
896899 '~\{(\d+)-(\d+)\}~ ' ,
897900 '~\{-(\d+)\}~ ' ,
@@ -946,13 +949,13 @@ public function fixPath($Path) {
946949 } else {
947950 $ Replacement = $ InnerCharCount === 10 ? $ Char . '[\da] ' : $ Char . '[\da- ' . $ FinalLast . '] ' ;
948951 }
949- $ SigHex = str_replace ($ Replacer , $ Replacement , $ SigHex );
952+ $ SigHex = str_ireplace ($ Replacer , $ Replacement , $ SigHex );
950953 }
951954 }
952955
953956 /** Upper-lower case stuff, and further simplification. */
954957 foreach ($ CharRange as $ Char ) {
955- $ SigHex = str_replace ([
958+ $ SigHex = str_ireplace ([
956959 '(4 ' . $ Char . '|6 ' . $ Char . ') ' ,
957960 '(6 ' . $ Char . '|4 ' . $ Char . ') ' ,
958961 '(5 ' . $ Char . '|7 ' . $ Char . ') ' ,
@@ -973,6 +976,27 @@ public function fixPath($Path) {
973976 ], $ SigHex );
974977 }
975978
979+ /** Reduce footprint. */
980+ foreach ($ CharRange as $ Char ) {
981+ $ Matches = [];
982+ $ Lengths = [];
983+ if (preg_match_all ('~ ' . $ Char . '{16,}~ ' , $ SigHex , $ Matches ) !== false && isset ($ Matches [0 ])) {
984+ foreach ($ Matches [0 ] as $ Match ) {
985+ $ Lengths [] = strlen ($ Match );
986+ }
987+ rsort ($ Lengths );
988+ }
989+ foreach ($ Lengths as $ Length ) {
990+ $ SigHex = preg_replace_callback (
991+ '~(?P<_before>[^ ' . $ Char . ']) ' . $ Char . '{ ' . $ Length . '}(?P<_after>[^ ' . $ Char . '])~ ' ,
992+ function ($ Matches ) use ($ Char , $ Length ) {
993+ return $ Matches ['_before ' ] . $ Char . '{ ' . $ Length . '} ' . $ Matches ['_after ' ];
994+ },
995+ $ SigHex
996+ );
997+ }
998+ }
999+
9761000 /** Newly formatted signature line. */
9771001 $ ThisLine = $ SigName . ': ' . $ SigHex . $ StartStop . "\n" ;
9781002
0 commit comments