Skip to content
markbook2 edited this page Feb 3, 2017 · 9 revisions

The Kaitai Struct: runtime for PHP can be used in two different ways:

  1. As part of Kaitai Struct

  2. Independently as Composer's library

The both approaches are described below in order.

1. Usage as part of Kaitai Struct

@TODO

2. Independent usage as Composer's library

# Create composer.json
cat <<'OUT' > composer.json
{
    "minimum-stability": "dev",
    "require": {
        "kaitai-io/kaitai_struct_php_runtime": "dev-master"
    },
    "repositories": [
        {
            "type": "vcs",
            "url":  "[email protected]:kaitai-io/kaitai_struct_php_runtime.git"
        }
    ]
}
OUT

# Install the library
composer install --prefer-source --no-dev

# Usage example
cat <<'OUT' > bash-ep.php
<?php
require __DIR__ . '/vendor/autoload.php';

$binStream = new \Kaitai\Struct\Stream(fopen('/usr/bin/bash', 'rb'));
$binStream->seek(5);
$endianness = $binStream->readS1();
$binStream->seek(24);
if ($endianness === 1) {
    // Read on little-endian machine
    $entryPoint = $binStream->readU8le();
} else {
    // Read on bin-endian machine
    $entryPoint = $binStream->readU8be();
}
echo "Entry point 0x" . dechex($entryPoint) . " (as int: $entryPoint)";
OUT

# Shows "Entry point 0x41b6e0 (as int: 4306656)"
php bash-ep.php
Clone this wiki locally