| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace app\extra\ip2region\xdb;
- const IPv4VersionNo = 4;
- class IPv4
- {
- public $id;
- public $name;
- public $bytes;
- public $segmentIndexSize;
- private static $C = null;
- public static function default() {
- if (self::$C == null) {
- // 14 = 4 + 4 + 2 + 4
- self::$C = new self(IPv4VersionNo, 'IPv4', 4, 14);
- }
- return self::$C;
- }
- public function __construct($id, $name, $bytes, $segmentIndexSize) {
- $this->id = $id;
- $this->name = $name;
- $this->bytes = $bytes;
- $this->segmentIndexSize = $segmentIndexSize;
- }
- // compare the two ip bytes with the current version
- public function ipSubCompare($ip1, $buff, $offset) {
- // ip1: Little endian byte order encoded long from searcher.
- // ip2: Little endian byte order read from xdb index.
- $len = strlen($ip1);
- $eIdx = $offset + $len;
- for ($i = 0, $j = $eIdx - 1; $i < $len; $i++, $j--) {
- $i1 = ord($ip1[$i]) & 0xFF;
- $i2 = ord($buff[$j]) & 0xFF;
- // printf("i:%d, j:%d, i1:%d, i2:%d\n", $i, $j, $i1, $i2);
- if ($i1 > $i2) {
- return 1;
- } else if ($i1 < $i2) {
- return -1;
- }
- }
- return 0;
- }
- public function __toString() {
- return sprintf(
- "{id:%d, name:%s, bytes:%d, segmentIndexSize:%d}",
- $this->id, $this->name, $this->bytes, $this->segmentIndexSize
- );
- }
- }
|