📄 00000017.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: thinkin (强强), 信区: Linux <BR>标 题: Binary pattern match <BR>发信站: BBS 水木清华站 (Thu Feb 17 11:00:01 2000) <BR> <BR><? <BR>function findfiletype($filename) { <BR> /* <BR> The array with the filetype and pattern, separate with a semi-colon. <BR> Each pair in the pattern represents a single byte in the input file. <BR> The question mark is used to match a single digit but should be used <BR>on <BR> both digits in the byte pair. Originally made to find the filetype of <BR> <BR> an input file uploaded using the POST method, which explains the "non <BR>e" <BR> comparison. <BR> - Petter Nilsen <<A HREF="mailto:pettern@thule.no>,">pettern@thule.no>,</A> <BR> */ <BR> $types = array( <BR> "zip;$504B", <BR> "lha;$????2D6C68", <BR> "gif;$47494638??", <BR> "jpg;$????????????4A464946", <BR> "exe;$4D5A", <BR> "bmp;$424D" <BR> ); <BR> $len = 0; <BR> $match = 0; <BR> $ext = ""; <BR> if($filename == "none") { <BR> return($ext); <BR> } <BR> $fh = fopen($filename, "r"); <BR> if($fh) { <BR> $tmpBuf = fread($fh, 250); <BR> if(strlen($tmpBuf) == 250) { <BR> for($iOffset = 0; $types[$iOffset]; $iOffset += 1) { <BR> list($ext,$pattern,$junk) = explode( ";",$types[$iOffset]); <BR> <BR> $len = strlen($pattern); <BR> if($pattern[0] == '$') { <BR> for($n = 1; $n < $len; $n += 2) { <BR> $lowval = 0; $highval = 0; <BR> if($pattern[$n] == '?' || $pattern[$n + 1] == '?') <BR> <BR> continue; <BR> $highval = Ord($pattern[$n]) - 48; <BR> if($highval > 9) { <BR> $highval -= 7; <BR> } <BR> $lowval = Ord($pattern[$n + 1]) - 48; <BR> if($lowval > 9) { <BR> $lowval -= 7; <BR> } <BR> if(Ord($tmpBuf[($n - 1) >> 1]) == (($highval << 4) + <BR> $lowval)) { <BR> $match = 1; <BR> } <BR> else { <BR> $match = 0; <BR> break; <BR> } <BR> } <BR> if($match) { <BR> break; <BR> } <BR> } <BR> } <BR> } <BR> if(!$match) { <BR> $ext = ""; <BR> } <BR> fclose($fh); <BR> } <BR> return ($ext); <BR>} <BR>?> <BR>-- <BR> <BR>人生到处知何似? <BR> 应似飞鸿踏雪泥。 <BR> 泥上偶然留指爪, <BR> 鸿飞那复计东西! <BR> <BR> <BR>※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.37.191] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -