note 27995 deleted from function.fgets by sniper

From: Date: Sat, 03 Jan 2004 02:46:29 +0000
Subject: note 27995 deleted from function.fgets by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-62830@lists.php.net to get a copy of this message
Note Submitter: joeldegan AT yahoo.com ---- While working with some binary files for the eMule/edonkey2000 (ed2k) linux clients I ran across some c-code for reading in the server.met files. I ported the code to PHP which uses fgets to pull in binary data from a server.met file piped into the php script. For those of you trying to port code to PHP this might come in handy for how to do it using fgets(). <? # # call like so "php thisfile.php < server.met" # function ireadch ($fp, $number) { do $c.= fgetc($fp); while (--$number); return (int)$c; } function lreadch ($fp, $number) { do $c.= ord(fgetc($fp)); while (--$number); return (int)$c; } function readlong ($fp) { $c = ord(fgetc($fp)); $c |= (ord(fgetc($fp)) << 8); $c |= (ord(fgetc($fp)) << 16); $c |= (ord(fgetc($fp)) << 24); return (int)$c; } #$fp = fopen("/var/www/donk/server.met.1","r"); $fp = fopen('php://stdin', 'r'); fgetc($fp); $serversf = readlong($fp); do { for ($icount=0;$icount<4;$icount++){ $ip[$icount]=ord(fgetc($fp)); } $port = ord(fgetc($fp)); $port |= (ord(fgetc($fp)) << 8); $meta = readlong($fp); for ($lcount=0;$lcount<$meta;$lcount++) { $t_type = intval(ord(fgetc($fp))); $t_length = ord(fgetc($fp)); // Name Length $t_length |= (ord(fgetc($fp)) << 8); if ($t_length===EOF) exit(0); if ($t_length) lreadch($fp,$t_length); if ($t_type==2) { // String Tag $t_length = ord(fgetc($fp)); // Value Length $t_length |= (ord(fgetc($fp)) << 8); if ($t_length===EOF) exit(0); if ($t_length){ lreadch($fp,$t_length); } // else jump over } else if ($t_type==3) { // Number Tag $o = ireadch($fp,4); } }//rof printf ("%u.%u.%u.%u %ld\n",$ip[0],$ip[1],$ip[2],$ip[3],$port); $serversc++; } while($serversc <= $serversf); ?>

« previous php.notes (#62830) next »