From ac2fcb35f97caff259035b0acb390c6767bd5c2f Mon Sep 17 00:00:00 2001 From: haoxiupeng Date: Mon, 2 Apr 2018 13:14:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=EF=BC=8C=E5=87=8F=E5=B0=91=E5=86=85=E5=AD=98=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=EF=BC=8C=E6=8F=90=E5=8D=87=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ipip/datx/City.php | 51 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/src/ipip/datx/City.php b/src/ipip/datx/City.php index a588b39..e1ef343 100644 --- a/src/ipip/datx/City.php +++ b/src/ipip/datx/City.php @@ -36,7 +36,7 @@ public function __construct($path) $this->offset = unpack('Nlen', fread($this->file, 4)); - $this->index = fread($this->file, $this->offset['len'] - 4); + //$this->index = fread($this->file, $this->offset['len'] - 4); } /** @@ -57,7 +57,16 @@ public function find($ip) $idx = (256 * $ips[0] + $ips[1]) * 4; - $start = unpack('Vlen', substr($this->index, $idx, 4)); + + ///////////////////////////////////////////// + //$start = unpack('Vlen', substr($this->index, $idx, 4)); + + fseek($this->file, 4 + $idx); // offset 时 已经移动了4 字节,所以加4 + + $start = unpack('Vlen', fread($this->file, 4)); + + //print_r($start);exit; + //////////////////////////////////////// $off = NULL; $len = NULL; @@ -66,11 +75,43 @@ public function find($ip) for ($start = $start['len'] * 9 + 262144; $start < $max; $start += 9) { - $tmp = $this->index[$start] . $this->index[$start + 1] . $this->index[$start + 2] . $this->index[$start + 3]; + + /////////////////////////////// + //$tmp = $this->index[$start] . $this->index[$start + 1] . $this->index[$start + 2] . $this->index[$start + 3]; + //$tmp = substr($this->index, $start, 4); + + fseek($this->file, 4 + $start); // offset 时 已经移动了4 字节,所以加4 + + $tmp = fread($this->file, 4); + + //print_r(strncmp($tmp, $tmp1, 10));exit; + //////////////////////////// + + if ($tmp >= $nip2) { - $off = unpack('Vlen', substr($this->index, $start + 4, 3) . "\x0"); - $len = unpack('nlen', $this->index[$start + 7] . $this->index[$start + 8]); + /////////////////////// + //$off = unpack('Vlen', substr($this->index, $start + 4, 3) . "\x0"); + + fseek($this->file, 4 + $start + 4); // offset 时 已经移动了4 字节,所以加4 + + $off = unpack('Vlen', fread($this->file, 3) . "\x0"); + + //print_r($off);exit; + //////////////////////// + + + + ///////////////////////// + //$len = unpack('nlen', $this->index[$start + 7] . $this->index[$start + 8]); + //$len = unpack('nlen', substr($this->index, $start + 7, 2)); + + fseek($this->file, 4 + $start + 7); // offset 时 已经移动了4 字节,所以加4 + + $len = unpack('nlen', fread($this->file, 2)); + + //print_r($len);exit; + ///////////////////////// break; } }