sysbot/bin

Basic binary serialization/de-serialization utilities, providing support for both 32-bit and 64-bit PHP versions.

1.1.2 2022-06-21 22:26 UTC

This package is auto-updated.

Last update: 2024-04-22 02:42:53 UTC


README

License Required PHP Version Latest Stable Version Tests Dependencies Code Quality

Sysbot/bin is a library used by Sysbot. It handles serialization and deserialization of binary data, providing support for both 32-bit and 64-bit systems.

Changelog

You can find the changelog here.

Installation

Install the library with composer:

$ composer require sysbot/bin --prefer-stable

(32-bit systems) You must also install the required dependency brick/math.

$ composer require "brick/math:^0.10"

Usage

Here's an example on how to use the library.

<?php

require_once 'vendor/autoload.php';

use Sysbot\Bin\Serializer;
use Sysbot\Bin\Deserializer;

// Serialization

$serializer = new Serializer();

$serializer->addLong(15); // adds a 32-bit integer (little-endian)

$long = PHP_INT_MAX;

if (PHP_INT_SIZE === 4) { // 32-bit systems
    // since 32-bit systems can't handle 64-bit numbers, we must relay on the BigInteger class
    $long = BigInteger::of('9223372036854775807');
}
$serializer->addLongLong($long, true); // adds a 64-bit integer (big-endian)

$serializer->addString('Hi mom!'); // adds a string

// casting a Serializer instance to a string will return the bytes
echo bin2hex((string)$serializer); // outputs "0f0000007fffffffffffffff074869206d6f6d21"


// Deserialization

$deserializer = new Deserializer((string)$serializer);

echo $deserializer->readLong(); // reads a 32-bit integer (little-endian), outputs "15"

$long = $deserializer->readLongLong(true); // reads a 64-bit integer (big-endian)

if (PHP_INT_SIZE === 4) {
    $long = (string)$long; // on 32-bit systems, an instance of the BigInteger class will be returned: to get the number, we must cast to string
}

echo $long; // outputs "9223372036854775807"

echo $deserializer->readString(); // reads a string, outputs "Hi mom!"