home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / Binary.php < prev    next >
Encoding:
Text File  |  2001-07-02  |  2.1 KB  |  81 lines

  1.     //**************************************
  2.     //     
  3.     // Name: Binary pattern match
  4.     // Description:This is a function that w
  5.     //     ill return the filetype of a file. It wi
  6.     //     ll perform a binary pattern match of the
  7.     //     contents of the file. An array defines t
  8.     //     he filetype and associated pattern
  9.     // By: PHP Code Exchange
  10.     //**************************************
  11.     //     
  12.     
  13.     <?
  14.     function findfiletype($filename) {
  15.     /*
  16.     The array with the filetype and pattern, separate with a semi-colon. 
  17.     Each pair in the pattern represents a single byte in the input file.
  18.     The question mark is used to match a single digit but should be used on
  19.     both digits in the byte pair. Originally made to find the filetype of
  20.     an input file uploaded using the POST method, which explains the "none"
  21.     comparison.
  22.     - Petter Nilsen <pettern@thule.no>,
  23.     */
  24.     $types = array(
  25.     "zip;$504B",
  26.     "lha;$????2D6C68",
  27.     "gif;$47494638??",
  28.     "jpg;$????????????4A464946",
  29.     "exe;$4D5A",
  30.     "bmp;$424D"
  31.     );
  32.     $len = 0;
  33.     $match = 0;
  34.     $ext = "";
  35.     if($filename == "none") {
  36.     return($ext);
  37.     }
  38.     $fh = fopen($filename, "r");
  39.     if($fh) {
  40.     $tmpBuf = fread($fh, 250);
  41.     if(strlen($tmpBuf) == 250) {
  42.     for($iOffset = 0; $types[$iOffset]; $iOffset += 1) {
  43.     list($ext,$pattern,$junk) = explode( ";",$types[$iOffset]);
  44.     $len = strlen($pattern);
  45.     if($pattern[0] == '$') {
  46.     for($n = 1; $n < $len; $n += 2) {
  47.     $lowval = 0; $highval = 0;
  48.     if($pattern[$n] == '?' || $pattern[$n + 1] == '?')
  49.     continue;
  50.     $highval = Ord($pattern[$n]) - 48;
  51.     if($highval > 9) {
  52.     $highval -= 7;
  53.     }
  54.     $lowval = Ord($pattern[$n + 1]) - 48;
  55.     if($lowval > 9) {
  56.     $lowval -= 7;
  57.     }
  58.     if(Ord($tmpBuf[($n - 1) >> 1]) == (($highval << 4) + $lowval)) {
  59.     $match = 1;
  60.     }
  61.     else {
  62.     $match = 0;
  63.     break;
  64.     }
  65.     }
  66.     if($match) {
  67.     break;
  68.     }
  69.     }
  70.     }
  71.     }
  72.     if(!$match) {
  73.     $ext = "";
  74.     }
  75.     fclose($fh);
  76.     }
  77.     return ($ext);
  78.     }
  79.     ?>
  80.  
  81.