-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f6a34b2
Showing
10 changed files
with
617 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
/vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Andrei Komarov. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## PHP BCDataStream | ||
|
||
A simple stream implementation in PHP for reading/writing binary data in Bitcoin protocol. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "andkom/bcdatastream", | ||
"description": "A simple stream implementation in PHP for reading/writing binary data in Bitcoin protocol.", | ||
"type": "library", | ||
"keywords": [ | ||
"stream", | ||
"bitcoin", | ||
"binary" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Andrei Komarov" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.1" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": ">=5.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"AndKom\\BCDataStream\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"AndKom\\BCDataStream\\Tests\\": "tests/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
colors="true" | ||
verbose="true"> | ||
<testsuites> | ||
<testsuite name="Unit"> | ||
<directory suffix="Test.php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AndKom\BCDataStream; | ||
|
||
// determine machine byte-order | ||
if (!defined('BIG_ENDIAN')) { | ||
define('BIG_ENDIAN', pack('L', 1) === pack('N', 1)); | ||
} | ||
|
||
/** | ||
* Class ByteOrder | ||
* @package AndKom\BCDataStream | ||
*/ | ||
class ByteOrder | ||
{ | ||
const BO_MACHINE = 0; | ||
const BO_LE = 1; | ||
const BO_BE = 2; | ||
|
||
/** | ||
* @param string $bytes | ||
* @param int $order | ||
* @return string | ||
*/ | ||
static public function convert(string $bytes, int $order): string | ||
{ | ||
if ($order != static::BO_MACHINE && ($order == static::BO_BE) != BIG_ENDIAN) { | ||
$bytes = strrev($bytes); | ||
} | ||
|
||
return $bytes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AndKom\BCDataStream; | ||
|
||
/** | ||
* Class Reader | ||
* @package AndKom\Reader | ||
*/ | ||
class Reader | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $buffer; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $position; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $byteOrder; | ||
|
||
/** | ||
* Reader constructor. | ||
* @param string $buffer | ||
* @param int $position | ||
* @param int $byteOrder | ||
*/ | ||
public function __construct(string $buffer = '', int $position = 0, int $byteOrder = ByteOrder::BO_LE) | ||
{ | ||
$this->setBuffer($buffer); | ||
$this->setPosition($position); | ||
$this->setByteOrder($byteOrder); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->getBuffer(); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getBuffer(): string | ||
{ | ||
return $this->buffer; | ||
} | ||
|
||
/** | ||
* @param string $buffer | ||
* @return Reader | ||
*/ | ||
public function setBuffer(string $buffer): self | ||
{ | ||
$this->buffer = $buffer; | ||
$this->position = 0; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getPosition(): int | ||
{ | ||
return $this->position; | ||
} | ||
|
||
/** | ||
* @param int $position | ||
* @return Reader | ||
*/ | ||
public function setPosition(int $position): self | ||
{ | ||
if ($this->position < 0) { | ||
throw new \InvalidArgumentException('Negative position.'); | ||
} | ||
|
||
if ($this->position > strlen($this->buffer)) { | ||
throw new \InvalidArgumentException('Position is greater then buffer length.'); | ||
} | ||
|
||
$this->position = $position; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getByteOrder(): int | ||
{ | ||
return $this->byteOrder; | ||
} | ||
|
||
/** | ||
* @param int $byteOrder | ||
* @return Reader | ||
*/ | ||
public function setByteOrder(int $byteOrder): self | ||
{ | ||
$this->byteOrder = $byteOrder; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getSize(): int | ||
{ | ||
return strlen($this->buffer); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isEOF(): bool | ||
{ | ||
return $this->position >= strlen($this->buffer); | ||
} | ||
|
||
/** | ||
* @param int $size | ||
* @return string | ||
*/ | ||
public function read(int $size): string | ||
{ | ||
if ($this->position + $size > strlen($this->buffer)) { | ||
throw new \InvalidArgumentException("Not enough buffer to read $size byte(s)."); | ||
} | ||
|
||
$bytes = substr($this->buffer, $this->position, $size); | ||
|
||
$this->position += strlen($bytes); | ||
|
||
return $bytes; | ||
} | ||
|
||
/** | ||
* @param string $format | ||
* @return int | ||
*/ | ||
public function readInt(string $format): int | ||
{ | ||
$size = strlen(pack($format, 1)); | ||
$bytes = $this->read($size); | ||
$bytes = ByteOrder::convert($bytes, $this->byteOrder); | ||
|
||
list(, $num) = unpack($format, $bytes); | ||
|
||
return $num; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function readVarInt(): int | ||
{ | ||
$size = ord($this->read(1)); | ||
|
||
if ($size == 253) { | ||
$size = $this->readInt('S'); | ||
} elseif ($size == 254) { | ||
$size = $this->readInt('L'); | ||
} elseif ($size == 255) { | ||
$size = $this->readInt('Q'); | ||
} | ||
|
||
return $size; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function readString(): string | ||
{ | ||
return $this->read($this->readVarInt()); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function readInt16(): int | ||
{ | ||
return $this->readInt('s'); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function readUInt16(): int | ||
{ | ||
return $this->readInt('S'); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function readInt32(): int | ||
{ | ||
return $this->readInt('l'); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function readUInt32(): int | ||
{ | ||
return $this->readInt('L'); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function readInt64(): int | ||
{ | ||
return $this->readInt('q'); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function readUInt64(): int | ||
{ | ||
return $this->readInt('Q'); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function readBoolean(): bool | ||
{ | ||
return $this->read(1) != chr(0); | ||
} | ||
} |
Oops, something went wrong.