From 953483a14b4d1bbd19a77b560e9fbbe2aef0565f Mon Sep 17 00:00:00 2001 From: wapmorgan Date: Tue, 7 Feb 2017 01:04:58 +0300 Subject: [PATCH] Update compare() method --- src/BinaryStream.php | 6 ++++++ tests/CompareTest.php | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/BinaryStream.php b/src/BinaryStream.php index dbb2c16..2029de3 100644 --- a/src/BinaryStream.php +++ b/src/BinaryStream.php @@ -374,6 +374,12 @@ public function saveGroup($name, $fields) { public function compare($sizeInBytes, $bytes) { $data = fread($this->fp, $sizeInBytes); fseek($this->fp, -$sizeInBytes, SEEK_CUR); + if (is_array($bytes)) { + $source = $bytes; + $bytes = null; + foreach ($source as $byte) + $bytes .= is_int($byte) ? chr($byte) : $byte; + } return ($data === $bytes); } diff --git a/tests/CompareTest.php b/tests/CompareTest.php index a4dc189..da27dbf 100644 --- a/tests/CompareTest.php +++ b/tests/CompareTest.php @@ -23,4 +23,15 @@ public function testCompare() { $this->assertFalse($s->compare(7, 'Called')); $this->assertTrue($s->compare(6, 'Called')); } + + public function testCompareBinary() { + $s = new BinaryStream($this->createStream(pack('H*', '3026B27511'))); + $this->assertFalse($s->compare(4, array(0x00, 0x00, 0x00, 0x00))); + $this->assertFalse($s->compare(3, array(0x30, 0x26, 0xB2, 0x75))); + $this->assertFalse($s->compare(5, array(0x30, 0x26, 0xB2, 0x75))); + $this->assertTrue($s->compare(4, array(0x30, 0x26, 0xB2, 0x75))); + + $s = new BinaryStream($this->createStream(pack('H*', '202020'))); + $this->assertTrue($s->compare(3, array(0x20, 32, ' '))); + } }