|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Badcow DNS Library. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Samuel Williams <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Badcow\DNS\Rdata; |
|
15
|
|
|
|
|
16
|
|
|
use Badcow\DNS\Parser\StringIterator; |
|
17
|
|
|
use Badcow\DNS\Parser\Tokens; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @see https://tools.ietf.org/html/rfc1035#section-3.3.14 |
|
21
|
|
|
*/ |
|
22
|
|
|
class TXT implements RdataInterface |
|
23
|
|
|
{ |
|
24
|
1 |
|
use RdataTrait; |
|
25
|
|
|
|
|
26
|
|
|
const TYPE = 'TXT'; |
|
27
|
|
|
const TYPE_CODE = 16; |
|
28
|
|
|
|
|
29
|
|
|
const WHITESPACE = [Tokens::SPACE, Tokens::TAB, Tokens::LINE_FEED, Tokens::CARRIAGE_RETURN]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string|null |
|
33
|
|
|
*/ |
|
34
|
31 |
|
private $text; |
|
35
|
|
|
|
|
36
|
31 |
|
public function setText(?string $text): void |
|
37
|
|
|
{ |
|
38
|
|
|
if (null === $text) { |
|
39
|
|
|
$this->text = null; |
|
40
|
|
|
|
|
41
|
|
|
return; |
|
42
|
31 |
|
} |
|
43
|
31 |
|
|
|
44
|
|
|
$this->text = stripslashes($text); |
|
45
|
8 |
|
} |
|
46
|
|
|
|
|
47
|
8 |
|
public function getText(): ?string |
|
48
|
|
|
{ |
|
49
|
|
|
return (string) $this->text ?? ''; |
|
50
|
9 |
|
} |
|
51
|
|
|
|
|
52
|
9 |
|
public function toText(): string |
|
53
|
9 |
|
{ |
|
54
|
9 |
|
$chunks = array_map(function (string $chunk) { |
|
55
|
|
|
return sprintf('"%s"', addcslashes($chunk, Tokens::DOUBLE_QUOTES.Tokens::BACKSLASH)); |
|
56
|
9 |
|
}, str_split($this->text ?? '', 255)); |
|
57
|
|
|
|
|
58
|
|
|
return implode(' ', $chunks); |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
public function toWire(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->text ?? ''; |
|
64
|
2 |
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void |
|
67
|
2 |
|
{ |
|
68
|
2 |
|
$rdLength = $rdLength ?? strlen($rdata); |
|
69
|
2 |
|
$this->setText(substr($rdata, $offset, $rdLength)); |
|
70
|
|
|
$offset += $rdLength; |
|
71
|
13 |
|
} |
|
72
|
|
|
|
|
73
|
13 |
|
public function fromText(string $text): void |
|
74
|
13 |
|
{ |
|
75
|
|
|
$string = new StringIterator($text); |
|
76
|
13 |
|
$txt = new StringIterator(); |
|
77
|
13 |
|
$whitespace = [Tokens::SPACE, Tokens::TAB]; |
|
|
|
|
|
|
78
|
13 |
|
|
|
79
|
|
|
while ($string->valid()) { |
|
80
|
|
|
if ($string->is(static::WHITESPACE)) { |
|
81
|
13 |
|
$string->next(); |
|
82
|
13 |
|
continue; |
|
83
|
|
|
} |
|
84
|
15 |
|
|
|
85
|
|
|
if ($string->is(Tokens::DOUBLE_QUOTES)) { |
|
86
|
15 |
|
self::handleTxt($string, $txt); |
|
87
|
3 |
|
$string->next(); |
|
88
|
|
|
continue; |
|
89
|
|
|
} |
|
90
|
15 |
|
|
|
91
|
|
|
self::handleContiguousString($string, $txt); |
|
92
|
15 |
|
break; |
|
93
|
15 |
|
} |
|
94
|
9 |
|
|
|
95
|
|
|
$this->setText((string) $txt); |
|
96
|
|
|
} |
|
97
|
15 |
|
|
|
98
|
15 |
|
/** |
|
99
|
|
|
* This handles the case where character string is encapsulated inside quotation marks. |
|
100
|
15 |
|
* |
|
101
|
|
|
* @param StringIterator $string The string to parse |
|
102
|
|
|
* @param StringIterator $txt The output string |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function handleTxt(StringIterator $string, StringIterator $txt): void |
|
105
|
|
|
{ |
|
106
|
|
|
if ($string->isNot(Tokens::DOUBLE_QUOTES)) { |
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$string->next(); |
|
111
|
|
|
|
|
112
|
|
|
while ($string->isNot(Tokens::DOUBLE_QUOTES) && $string->valid()) { |
|
113
|
|
|
if ($string->is(Tokens::BACKSLASH)) { |
|
114
|
|
|
$string->next(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$txt->append($string->current()); |
|
118
|
|
|
$string->next(); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* This handles the case where character string is not encapsulated inside quotation marks. |
|
124
|
|
|
* |
|
125
|
|
|
* @param StringIterator $string The string to parse |
|
126
|
|
|
* @param StringIterator $txt The output string |
|
127
|
|
|
*/ |
|
128
|
|
|
private static function handleContiguousString(StringIterator $string, StringIterator $txt): void |
|
129
|
|
|
{ |
|
130
|
|
|
while ($string->valid() && $string->isNot(static::WHITESPACE)) { |
|
131
|
|
|
$txt->append($string->current()); |
|
132
|
|
|
$string->next(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.