$num) { if ($num === null) { $searchSeq[] = null; } elseif (is_array($num) && count($num) == 2 && is_int($num[0]) && is_int($num[1]) && $num[0] >= 0 && $num[0] <= 255 && $num[1] >= 0 && $num[1] <= 255 && $num[0] < $num[1]) { $searchSeq[] = $num; //Leave them as numeric } elseif (is_int($num) && $num >= 0 && $num <= 255) { $searchSeq[] = chr($num); } else { perr("Found invalid search sequence at index $idx", true); } } //$pos denotes start position but it's also used to mark where start of a potential pattern match was found fseek($fp, $pos); do { //This loop is optimized for speed $buf = fread($fp, $bufLen); if (!isset($buf[$bufLen-1])) { break; //Not enough data = no match } $successfulLoops = 0; foreach ($searchSeq as $byteIdx => $seekByte) { if ($seekByte === null) { //any character ++$successfulLoops; continue; } //element in the array can be a range [(int)from,(int)to] or a literal SINGLE byte //if isset finds a second element it will mean for us that it's an array of 2 elements (as we don't expect //a string longer than a single byte) if (isset($seekByte[1])) { $curByteNum = ord($buf[$byteIdx]); if ($curByteNum < $seekByte[0] || $curByteNum > $seekByte[1]) { break; } } elseif($buf[$byteIdx] !== $seekByte) { //If the byte doesn't match literally we know it's not a match break; } ++$successfulLoops; } if ($successfulLoops === $bufLen) { return $pos; } fseek($fp, ++$pos); $maxToCheck--; } while (!feof($fp) && $maxToCheck != 0); return -1; } /** * @return resource */ function getFileMemMapped(string $path) { $fp = fopen('php://memory', 'r+'); fwrite($fp, file_get_contents($path)); //poor man's mmap :D return $fp; } function saveStreamToFile($fp, string $path) { perr("Saving stream to $path ...\n"); $fp2 = fopen($path, 'w'); fseek($fp, 0); while (!feof($fp)) { fwrite($fp2, fread($fp, 8192)); } fclose($fp2); perr("DONE!\n"); } /** * Do not call this in time-sensitive code... */ function readAt($fp, int $pos, int $len) { fseek($fp, $pos); return fread($fp, $len); }