@@ -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
0 commit comments