Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/wordfenc.../vendor/wordfenc.../mmdb-rea.../src
File: DataFieldParser.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Wordfence\MmdbReader;
[2] Fix | Delete
[3] Fix | Delete
use Wordfence\MmdbReader\Exception\FormatException;
[4] Fix | Delete
use Wordfence\MmdbReader\Exception\InvalidArgumentException;
[5] Fix | Delete
[6] Fix | Delete
class DataFieldParser {
[7] Fix | Delete
[8] Fix | Delete
private $handle;
[9] Fix | Delete
private $sectionOffset;
[10] Fix | Delete
[11] Fix | Delete
public function __construct($handle, $sectionOffset = null) {
[12] Fix | Delete
$this->handle = $handle;
[13] Fix | Delete
$this->sectionOffset = $sectionOffset === null ? $this->handle->getPosition() : $sectionOffset;
[14] Fix | Delete
}
[15] Fix | Delete
[16] Fix | Delete
public function processControlByte() {
[17] Fix | Delete
return ControlByte::consume($this->handle);
[18] Fix | Delete
}
[19] Fix | Delete
[20] Fix | Delete
private function readStandardField($controlByte) {
[21] Fix | Delete
$size = $controlByte->getSize();
[22] Fix | Delete
if ($size === 0)
[23] Fix | Delete
return '';
[24] Fix | Delete
return $this->handle->read($size);
[25] Fix | Delete
}
[26] Fix | Delete
[27] Fix | Delete
private function parseUtf8String($controlByte) {
[28] Fix | Delete
return $this->readStandardField($controlByte);
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
private function parseUnsignedInteger($controlByte) {
[32] Fix | Delete
//TODO: Does this handle large-enough values gracefully?
[33] Fix | Delete
return IntegerParser::parseUnsigned($this->handle, $controlByte->getSize());
[34] Fix | Delete
}
[35] Fix | Delete
[36] Fix | Delete
private function parseMap($controlByte) {
[37] Fix | Delete
$map = array();
[38] Fix | Delete
for ($i = 0; $i < $controlByte->getSize(); $i++) {
[39] Fix | Delete
$keyByte = $this->processControlByte();
[40] Fix | Delete
$key = $this->parseField($keyByte);
[41] Fix | Delete
if (!is_string($key))
[42] Fix | Delete
throw new FormatException('Map keys must be strings, received ' . $keyByte . ' / ' . print_r($key, true) . ', map: ' . print_r($map, true));
[43] Fix | Delete
$value = $this->parseField();
[44] Fix | Delete
$map[$key] = $value;
[45] Fix | Delete
}
[46] Fix | Delete
return $map;
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
private function parseArray($controlByte) {
[50] Fix | Delete
$array = array();
[51] Fix | Delete
for ($i = 0; $i < $controlByte->getSize(); $i++) {
[52] Fix | Delete
$array[$i] = $this->parseField();
[53] Fix | Delete
}
[54] Fix | Delete
return $array;
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
private function parseBoolean($controlByte) {
[58] Fix | Delete
return (bool) $controlByte->getSize();
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
private static function unpackSingleValue($format, $data, $controlByte) {
[62] Fix | Delete
$values = unpack($format, $data);
[63] Fix | Delete
if ($values === false)
[64] Fix | Delete
throw new FormatException("Unpacking field failed for {$controlByte}");
[65] Fix | Delete
return reset($values);
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
private static function getPackedLength($formatCharacter) {
[69] Fix | Delete
switch ($formatCharacter) {
[70] Fix | Delete
case 'E':
[71] Fix | Delete
return 8;
[72] Fix | Delete
case 'G':
[73] Fix | Delete
case 'l':
[74] Fix | Delete
return 4;
[75] Fix | Delete
}
[76] Fix | Delete
throw new InvalidArgumentException("Unsupported format character: {$formatCharacter}");
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
private static function usesSystemByteOrder($formatCharacter) {
[80] Fix | Delete
switch ($formatCharacter) {
[81] Fix | Delete
case 'l':
[82] Fix | Delete
return true;
[83] Fix | Delete
default:
[84] Fix | Delete
return false;
[85] Fix | Delete
}
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
private function parseByUnpacking($controlByte, $format) {
[89] Fix | Delete
//TODO: Is this reliable for float/double types, considering that the size for unpack is platform dependent?
[90] Fix | Delete
$data = $this->readStandardField($controlByte);
[91] Fix | Delete
$data = str_pad($data, self::getPackedLength($format), "\0", STR_PAD_LEFT);
[92] Fix | Delete
if (self::usesSystemByteOrder($format))
[93] Fix | Delete
$data = Endianness::convert($data, Endianness::BIG);
[94] Fix | Delete
return $this->unpackSingleValue($format, $data, $controlByte);
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
private function parsePointer($controlByte) {
[98] Fix | Delete
$data = $controlByte->getSize();
[99] Fix | Delete
$size = $data >> 3;
[100] Fix | Delete
$address = $data & 7;
[101] Fix | Delete
if ($size === 3)
[102] Fix | Delete
$address = 0;
[103] Fix | Delete
for ($i = 0; $i < $size + 1; $i++) {
[104] Fix | Delete
$address = ($address << 8) + $this->handle->readByte();
[105] Fix | Delete
}
[106] Fix | Delete
switch ($size) {
[107] Fix | Delete
case 1:
[108] Fix | Delete
$address += 2048;
[109] Fix | Delete
break;
[110] Fix | Delete
case 2:
[111] Fix | Delete
$address += 526336;
[112] Fix | Delete
break;
[113] Fix | Delete
}
[114] Fix | Delete
$previous = $this->handle->getPosition();
[115] Fix | Delete
$this->handle->seek($this->sectionOffset + $address, SEEK_SET);
[116] Fix | Delete
$referenceControlByte = $this->processControlByte();
[117] Fix | Delete
if ($referenceControlByte->getType() === ControlByte::TYPE_POINTER)
[118] Fix | Delete
throw new FormatException('Per the MMDB specification, pointers may not point to other pointers. This database does not comply with the specification.');
[119] Fix | Delete
$value = $this->parseField($referenceControlByte);
[120] Fix | Delete
$this->handle->seek($previous, SEEK_SET);
[121] Fix | Delete
return $value;
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
private function parseSignedInteger($controlByte, $format) {
[125] Fix | Delete
if ($controlByte->getSize() === 0)
[126] Fix | Delete
return 0;
[127] Fix | Delete
return $this->parseByUnpacking($controlByte, $format);
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
public function parseField(&$controlByte = null) {
[131] Fix | Delete
if ($controlByte === null)
[132] Fix | Delete
$controlByte = $this->processControlByte();
[133] Fix | Delete
switch ($controlByte->getType()) {
[134] Fix | Delete
case ControlByte::TYPE_POINTER:
[135] Fix | Delete
return $this->parsePointer($controlByte);
[136] Fix | Delete
case ControlByte::TYPE_UTF8_STRING:
[137] Fix | Delete
return $this->parseUtf8String($controlByte);
[138] Fix | Delete
case ControlByte::TYPE_DOUBLE:
[139] Fix | Delete
$this->parseByUnpacking($controlByte, 'E');
[140] Fix | Delete
case ControlByte::TYPE_BYTES:
[141] Fix | Delete
case ControlByte::TYPE_CONTAINER:
[142] Fix | Delete
return $this->readStandardField($controlByte);
[143] Fix | Delete
case ControlByte::TYPE_UINT16:
[144] Fix | Delete
case ControlByte::TYPE_UINT32:
[145] Fix | Delete
case ControlByte::TYPE_UINT64:
[146] Fix | Delete
case ControlByte::TYPE_UINT128:
[147] Fix | Delete
return $this->parseUnsignedInteger($controlByte);
[148] Fix | Delete
case ControlByte::TYPE_INT32:
[149] Fix | Delete
return $this->parseSignedInteger($controlByte, 'l');
[150] Fix | Delete
case ControlByte::TYPE_MAP:
[151] Fix | Delete
return $this->parseMap($controlByte);
[152] Fix | Delete
case ControlByte::TYPE_ARRAY:
[153] Fix | Delete
return $this->parseArray($controlByte);
[154] Fix | Delete
case ControlByte::TYPE_END_MARKER:
[155] Fix | Delete
return null;
[156] Fix | Delete
case ControlByte::TYPE_BOOLEAN:
[157] Fix | Delete
return $this->parseBoolean($controlByte);
[158] Fix | Delete
case ControlByte::TYPE_FLOAT:
[159] Fix | Delete
$this->parseByUnpacking($controlByte, 'G');
[160] Fix | Delete
default:
[161] Fix | Delete
throw new FormatException("Unable to parse data field for {$controlByte}");
[162] Fix | Delete
}
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
}
[166] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function