performance - Searching for hex string in a file in php? -
performance - Searching for hex string in a file in php? -
i'm using next 2 methods in class job done:
function xseek($h,$pos){ rewind($h); if($pos>0) fread($h,$pos); } function find($str){ homecoming $this->startingindex($this->name,$str); } function startingindex($a,$b){ $lim = 1 + filesize($a) - strlen($b)/2; $h = fopen($a,"rb"); rewind($h); for($i=0;$i<$lim;$i++){ $this->xseek($h,$i); if($b==strtoupper(bin2hex(fread($h,strlen($b)/2)))){ fclose($h); homecoming $i; } } fclose($h); homecoming -1; }
i realize quite inefficient, php, i'm not allowed other language on hosting plan.
i ran couple tests, , when hex string towards origin of file, runs , returns offset. when hex string isn't found, however, page hangs while. kills me within because lastly time tested php , had hanging pages, webhost shut site downwards 24 hours due much cpu time.
is there improve way accomplish (finding hex string's offset in file)? there aspects of improved speed execution?
i read entire contents of file 1 hex string , utilize strrpos, getting errors maximum memory beingness exceeded. improve method if chopped file , searched big pieces strrpos?
edit:
to specify, i'm dealing settings file game. settings , values in block there 32-bit int before setting, setting, 32-bit int before value, , value. both ints represent lengths of next strings. example, if setting "test" , value "0", (in hex): 00000004746573740000000130. mention it, seem bad way go it. recommend?
edit 2:
i tried file below maximum memory i'm allowed , tried strrpos, much slower way i've been trying.
edit 3: in reply charles:
what's unknown length of settings block , starts. know first , lastly settings are. i've been using these searching methods find location of first , lastly setting , determine length of settings block. know parent block starts. settings block no more 50 bytes parent, start search first setting there , limit how far search. problem need find lastly setting. length of settings block variable , length. read file way assume game does, reading size of setting, reading setting, reading size of value, reading value, etc. until reached byte value -1, or ff in hex. combination of limiting search first setting , reading settings create much more efficient?
you have lot of garbage code. example, code doing nothing:
function xseek($h,$pos){ rewind($h); if($pos>0) fread($h,$pos); }
because reads everytime begining of file. furthemore, why need read if not returning it? may looke fseek()
?
if need find hex string in binary file, may improve utilize this: http://pastebin.com/fpdbdsvv (tell me if there bugs/problems).
but, if parsing game's settings file, i'd advise utilize fseek()
, fread()
, unpack()
seek place of setting is, read portion of bytes , unpack php's variable types.
php performance file cpu
Comments
Post a Comment