Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/themainframe/php-binary
A PHP library for parsing structured binary streams.
https://github.com/themainframe/php-binary
bytes parsing php schema
Last synced: 20 days ago
JSON representation
A PHP library for parsing structured binary streams.
- Host: GitHub
- URL: https://github.com/themainframe/php-binary
- Owner: themainframe
- License: mit
- Created: 2013-11-19T23:17:17.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2016-05-23T21:31:34.000Z (over 8 years ago)
- Last Synced: 2024-11-17T18:07:08.834Z (about 1 month ago)
- Topics: bytes, parsing, php, schema
- Language: PHP
- Homepage: http://php-binary.damow.net
- Size: 96.7 KB
- Stars: 32
- Watchers: 5
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
php-binary [![Build Status](https://travis-ci.org/themainframe/php-binary.svg?branch=master)](https://travis-ci.org/themainframe/php-binary)
==========A PHP library for parsing structured binary streams.
## Documentation
Documentation can be found in the `documentation` directory, as well as online at [php-binary.damow.net](http://php-binary.damow.net)
## Usage
Here is an example binary format:
1. **4 bytes** of text.
2. **1 byte** unsigned integer.
3. A field of **2 bytes** of text followed by a **1 byte** unsigned integer; repeated *n* times, where *n* is a backreference to the byte described in point **2**.### Writing a Parser Schema
This format can be parsed as follows. In this example, the schema is described using JSON for clarity, though in practise any array may be used.
```php
$builder = new Binary\SchemaBuilder;
$schema = $builder->createFromArray(json_decode('{
"sometext": {
"_type": "Text",
"size": 4
},
"somebyte": {
"_type": "UnsignedInteger",
"size": 1
},
"somefields": {
"_type": "Compound",
"count": "@somebyte",
"_fields": {
"footext": {
"_type": "Text",
"size": 2
},
"foobyte": {
"_type": "UnsignedInteger",
"size": 1
}
}
}
}', true));
```### Parsing a Stream
You can have php-binary parse a generic stream of bytes and output fields as an associative array matching your schema definition.
```php
$stream = new Binary\Stream\StringStream("FOOO\x03LOLLOMLON");
$result = $schema->readStream($stream);
```The resulting associative array in `$result` (shown here as JSON for clarity) would contain:
```json
{
"sometext": "FOOO",
"somebyte": 3,
"somefields": [
{
"footext": "LO",
"foobyte": 76
},
{
"footext": "LO",
"foobyte": 77
},
{
"footext": "LO",
"foobyte": 78
}
]
}
```