8ctopus / byte-buffer
A php buffer to work with binary data.
Installs: 2 639
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- nunomaduro/collision: ^7|^8
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.0|^10.0
README
A php buffer to work with binary data.
install
composer require 8ctopus/byte-buffer
demo
use Oct8pus\ByteBuffer\ByteBuffer; use Oct8pus\ByteBuffer\Endian; use Oct8pus\ByteBuffer\Origin; require_once __DIR__ . '/vendor/autoload.php'; echo "Let's create a new little endian buffer and write string Hello World\n"; $buffer = (new ByteBuffer()) ->setEndian(Endian::LittleEndian) ->writeString('Hello World'); echo $buffer . "\n"; // hex (12/12): 48656c6c 6f20576f 726c6400 - Hello World. echo "Add byte 0x07, word 0xFFFF and dword 0xAABBCCDD\n"; $buffer ->writeByte(0x07) ->writeWord(0xFFFF) ->writeDword(0xAABBCCDD); echo $buffer; // hex (19/19): 48656c6c 6f20576f 726c6400 07ffffdd ccbbaa - Hello World........ echo "\nSeek buffer back to origin\n"; $buffer->seek(0, Origin::Start); echo $buffer; // hex (0/19): 48656c6c 6f20576f 726c6400 07ffffdd ccbbaa - Hello World........ echo "\nRead string from buffer\n"; echo $buffer->readString() . "\n"; // Hello World echo "\nRead byte, word and dword\n"; printf("0x%02X\n", $buffer->readByte()); printf("0x%04X\n", $buffer->readword()); printf("0x%08X\n", $buffer->readDword()); // 0x07 // 0xFFFF // 0xAABBCCDD echo "\nDelete World from buffer\n"; $buffer->delete(6, 5); echo $buffer; // hex (19/14): 48656c6c 6f200007 ffffddcc bbaa - Hello ........ echo "\nCalculate buffer crc32b\n"; echo '0x'. strtoupper($buffer->crc32b(true)) . "\n"; // 0xF3B2604E echo "\nInsert Parrot at position 6\n"; $buffer ->seek(6, Origin::Start) ->insertChars('Parrot'); echo $buffer; // hex (12/20): 48656c6c 6f205061 72726f74 0007ffff ddccbbaa - Hello Parrot........ echo "\nCopy Parrot to a new buffer\n"; $parrot = $buffer->copy(6, 6); echo $parrot; // hex (0/6): 50617272 6f74 - Parrot echo "\nInvert Parrot\n"; echo $parrot->invert(); // hex (0/6): 746f7272 6150 - torraP echo "\nCalculate hashes\n"; echo 'md5: ' . $parrot->md5(false) . "\n"; echo 'sha1: ' . $parrot->sha1(false) . "\n"; echo 'sha256: ' . $parrot->sha256(false) . "\n"; // md5: 4264ed2a05f9548fb3b26601c1c904c4 // sha1: d50da4682cdb41c803075168f5c132fd33ffa34d // sha256: e2d6ddb19ca9c3396521297f4a09581c1187acb29931c8d4431941be71d2215c echo "\nTruncate buffer\n"; echo $parrot->truncate(); // hex (0/0):
run tests
composer test
clean code
composer fix
composer fix-risky