Skip to content

Commit 67de549

Browse files
committed
Add $allowTrailingData to decoder
1 parent e935e8c commit 67de549

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/Decoder/Decoder.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ class Decoder
7171
*/
7272
private $decodingContextFactory;
7373

74+
/**
75+
* @var bool
76+
*/
77+
private $allowTrailingData;
78+
7479
/**
7580
* Constructor
7681
*
@@ -80,21 +85,24 @@ class Decoder
8085
* @param \LibDNS\Records\ResourceBuilder $resourceBuilder
8186
* @param \LibDNS\Records\Types\TypeBuilder $typeBuilder
8287
* @param \LibDNS\Decoder\DecodingContextFactory $decodingContextFactory
88+
* @param bool $allowTrailingData
8389
*/
8490
public function __construct(
8591
PacketFactory $packetFactory,
8692
MessageFactory $messageFactory,
8793
QuestionFactory $questionFactory,
8894
ResourceBuilder $resourceBuilder,
8995
TypeBuilder $typeBuilder,
90-
DecodingContextFactory $decodingContextFactory
96+
DecodingContextFactory $decodingContextFactory,
97+
$allowTrailingData = false
9198
) {
9299
$this->packetFactory = $packetFactory;
93100
$this->messageFactory = $messageFactory;
94101
$this->questionFactory = $questionFactory;
95102
$this->resourceBuilder = $resourceBuilder;
96103
$this->typeBuilder = $typeBuilder;
97104
$this->decodingContextFactory = $decodingContextFactory;
105+
$this->allowTrailingData = $allowTrailingData;
98106
}
99107

100108
/**
@@ -476,7 +484,7 @@ public function decode($data)
476484
$additionalRecords->add($this->decodeResourceRecord($decodingContext));
477485
}
478486

479-
if ($packet->getBytesRemaining() !== 0) {
487+
if (!$this->allowTrailingData && $packet->getBytesRemaining() !== 0) {
480488
throw new \UnexpectedValueException('Decode error: Unexpected data at end of packet');
481489
}
482490

src/Decoder/DecoderFactory.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ class DecoderFactory
4040
* Create a new Decoder object
4141
*
4242
* @param \LibDNS\Records\TypeDefinitions\TypeDefinitionManager $typeDefinitionManager
43-
* @return \LibDNS\Decoder\Decoder
43+
* @param bool $allowTrailingData
44+
* @return Decoder
4445
*/
45-
public function create(TypeDefinitionManager $typeDefinitionManager = null)
46+
public function create(TypeDefinitionManager $typeDefinitionManager = null, $allowTrailingData = false)
4647
{
4748
$typeBuilder = new TypeBuilder(new TypeFactory);
4849

@@ -62,7 +63,8 @@ public function create(TypeDefinitionManager $typeDefinitionManager = null)
6263
)
6364
),
6465
$typeBuilder,
65-
new DecodingContextFactory
66+
new DecodingContextFactory,
67+
$allowTrailingData
6668
);
6769
}
6870
}

0 commit comments

Comments
 (0)