BBS水木清华站∶精华区

发信人: thinkin (强强), 信区: Linux        
标  题: Binary pattern match  
发信站: BBS 水木清华站 (Thu Feb 17 11:00:01 2000) 
 
<? 
function findfiletype($filename) { 
     /* 
       The array with the filetype and pattern, separate with a semi-colon. 
       Each pair in the pattern represents a single byte in the input file. 
       The question mark is used to match a single digit but should be used  
on 
       both digits in the byte pair. Originally made to find the filetype of 
 
       an input file uploaded using the POST method, which explains the "non 
e" 
       comparison. 
       - Petter Nilsen <pettern@thule.no>, 
    */ 
    $types = array( 
         "zip;$504B", 
         "lha;$????2D6C68", 
         "gif;$47494638??", 
         "jpg;$????????????4A464946", 
         "exe;$4D5A", 
         "bmp;$424D" 
    ); 
    $len = 0; 
    $match = 0; 
    $ext =  ""; 
    if($filename ==  "none") { 
        return($ext); 
    } 
    $fh = fopen($filename,  "r"); 
    if($fh) { 
        $tmpBuf = fread($fh, 250); 
        if(strlen($tmpBuf) == 250) { 
            for($iOffset = 0; $types[$iOffset]; $iOffset += 1) { 
                list($ext,$pattern,$junk) = explode(  ";",$types[$iOffset]); 
 
                $len = strlen($pattern); 
                if($pattern[0] ==  '$') { 
                    for($n = 1; $n < $len; $n += 2) { 
                        $lowval = 0; $highval = 0; 
                        if($pattern[$n] ==  '?' || $pattern[$n + 1] ==  '?') 
 
                            continue; 
                        $highval = Ord($pattern[$n])  - 48; 
                        if($highval > 9) { 
                            $highval -= 7; 
                        } 
                        $lowval = Ord($pattern[$n + 1]) - 48; 
                        if($lowval > 9) { 
                            $lowval -= 7; 
                        } 
                        if(Ord($tmpBuf[($n - 1) >> 1]) == (($highval << 4) + 
 $lowval)) { 
                            $match = 1; 
                        } 
                        else { 
                            $match = 0; 
                            break; 
                        } 
                    } 
                    if($match) { 
                        break; 
                    } 
                } 
            } 
        } 
        if(!$match) { 
            $ext =  ""; 
        } 
        fclose($fh); 
    } 
    return ($ext); 

?> 
-- 
 
人生到处知何似? 
    应似飞鸿踏雪泥。 
        泥上偶然留指爪, 
            鸿飞那复计东西! 
        
 
※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.37.191] 

BBS水木清华站∶精华区