[Namazu-cvs 1697] r278 - MMagic/trunk

cvsd @ vaj.namazu.org cvsd @ vaj.namazu.org
2007年 10月 2日 (火) 16:07:57 JST


Author: knok
Date: 2007-10-02 16:07:57 +0900 (Tue, 02 Oct 2007)
New Revision: 278

Modified:
   MMagic/trunk/ChangeLog
   MMagic/trunk/MMagic.pm
Log:
Added 'regex' and 'search' type for magic entry.
Modified for stability.


Modified: MMagic/trunk/ChangeLog
===================================================================
--- MMagic/trunk/ChangeLog	2007-03-20 04:33:36 UTC (rev 277)
+++ MMagic/trunk/ChangeLog	2007-10-02 07:07:57 UTC (rev 278)
@@ -1,3 +1,9 @@
+2007-10-02  NOKUBI Takatsugu  <knok @ daionet.gr.jp>
+
+	* MMagic.pm: Added 'regex' and 'search' support for magic entry,
+	  and fixed fatal error when unknown type in magic entry.
+	  (The patch made by Man Min Yan, thank you).
+
 2006-09-19  NOKUBI Takatsugu  <knok @ daionet.gr.jp>
 
 	* MMagic.pm (readMagicLine): Added Hex representation support.

Modified: MMagic/trunk/MMagic.pm
===================================================================
--- MMagic/trunk/MMagic.pm	2007-03-20 04:33:36 UTC (rev 277)
+++ MMagic/trunk/MMagic.pm	2007-10-02 07:07:57 UTC (rev 278)
@@ -786,6 +786,7 @@
     # this saves time otherwise wasted parsing unused subtests.
     if (@$item == 3){
         my $tmp = readMagicLine(@$item);
+	return unless defined($tmp);
         @$item = @$tmp;
     }
 
@@ -824,7 +825,7 @@
 	$fh->seek($offset,0) or return;
     }
 
-    if ($type =~ /^string/) {
+    if ($type =~ /^string/ || $type =~ /^regex/) {
 	# read the length of the match string unless the
 	# comparison is '>' ($numbytes == 0), in which case 
 	# read to the next null or "\n". (that's what BSD's file does)
@@ -849,6 +850,10 @@
 	elsif ($op eq '>') {
 	    $match = ($data gt $testval);
 	}
+	elsif ($op eq 'match') {
+	    $data = ($data || '') ? $data : '';
+	    $match = $data =~ /$testval/;
+	}
 	# else bogus op, but don't die, just skip
 
 	if ($checkMagic) {
@@ -856,6 +861,9 @@
 	}
 
     }
+    elsif ($type =~ /^search\//)
+    {
+    }
     else {
 	#numeric
 
@@ -984,7 +992,7 @@
 	$str = substr($str, $offset);
     }
 
-    if ($type =~ /^string/) {
+    if ($type =~ /^string/ || $type =~ /^regex/) {
 	# read the length of the match string unless the
 	# comparison is '>' ($numbytes == 0), in which case 
 	# read to the next null or "\n". (that's what BSD's file does)
@@ -1006,6 +1014,10 @@
 	elsif ($op eq '>') {
 	    $match = ($data gt $testval);
 	}
+	elsif ($op eq 'match') {
+	    $match = eval {($data || '') =~ /$testval/};
+	}
+
 	# else bogus op, but don't die, just skip
 
 	if ($checkMagic) {
@@ -1013,6 +1025,9 @@
 	}
 
     }
+    elsif ($type =~ /^search\//)
+    {
+    }
     else {
 	#numeric
 
@@ -1247,7 +1262,7 @@
     }
     
     # check if type is valid
-    if (!exists($TEMPLATES{$type}) && $type !~ /^string/) {
+    if (!exists($TEMPLATES{$type}) && $type !~ /^string/ && $type !~ /^regex/ && $type !~ /^search\//) {
 	warn "Invalid type '$type' at line $line_num\n";
 	return;
     }
@@ -1265,16 +1280,22 @@
     $line =~ s/\n$//o;
 
     # get the operator.  if 'x', must be alone.  default is '='.
-    if ($line =~ s/^([><&^=!])//o) {
-	$operator = $1;
+    if ($type !~ /regex/)
+    {
+	if ($line =~ s/^([><&^=!])//o) {
+	    $operator = $1;
+	}
+	elsif ($line eq 'x') {
+	    $operator = 'x';
+	}
+	else { $operator = '='; }
+    } else {
+	$operator = 'match';
     }
-    elsif ($line eq 'x') {
-	$operator = 'x';
-    }
-    else { $operator = '='; }
     
 
-    if ($type =~ /string/) {
+    if ($type =~ /^string/ || $type =~ /^regex/)
+    {
 	$testval = $line;
 
 	# do octal/hex conversion
@@ -1301,6 +1322,11 @@
 	    $numbytes = length($testval);
 	    $operator = '=';
 	}
+	elsif ($operator eq 'match') {
+	    eval {"" =~ /$testval/};	# Check the regex
+	    warn "Invalid regex at line $line_num - $@\n" if ($@);
+	    $numbytes = 0;
+	}
 	else {
 	    # there's a bug in my magic file where there's
 	    # a line that says "0	string	^!<arc..." and the BSD
@@ -1311,6 +1337,9 @@
 	    return;
 	}
     }
+    elsif ($type =~ /^search\//)
+    {
+    }
     else {
 	# numeric
 	if ($operator ne 'x') {
@@ -1349,6 +1378,7 @@
 	# delayed evaluation.
         if (@$entry == 3){
             my $tmp = readMagicLine(@$entry);
+	    next if (! $tmp);
             @$entry = @$tmp;
         }
 




Namazu-cvs メーリングリストの案内