Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
olafkryus committed Jun 20, 2019
0 parents commit dca8af5
Show file tree
Hide file tree
Showing 12 changed files with 678 additions and 0 deletions.
95 changes: 95 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

# Created by https://www.gitignore.io/api/phpstorm,composer
# Edit at https://www.gitignore.io/?templates=phpstorm,composer

### Composer ###
composer.phar
/vendor/

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock

### PhpStorm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

# JetBrains templates
**___jb_tmp___

### PhpStorm Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# Sonarlint plugin
.idea/sonarlint

# End of https://www.gitignore.io/api/phpstorm,composer
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Olaf Kryus

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.
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "kryus/binary",
"license": "MIT",
"description": "Binary file manipulation library.",
"type": "library",
"authors": [
{
"name": "Olaf Kryus"
}
],
"require": {
"php": "^7.1"
},
"autoload": {
"psr-4": {
"Kryus\\Binary\\": "src/"
}
}
}
19 changes: 19 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

201 changes: 201 additions & 0 deletions src/ByteStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php
declare(strict_types=1);

namespace Kryus\Binary;

use Kryus\Binary\DataType\BinaryValue;
use Kryus\Binary\DataType\Byte;
use Kryus\Binary\DataType\Dword;
use Kryus\Binary\DataType\SignedByte;
use Kryus\Binary\DataType\SignedDword;
use Kryus\Binary\DataType\SignedWord;
use Kryus\Binary\DataType\Word;

class ByteStream
{
/** @var string */
private $contents;

/** @var int */
private $cursor = 0;

/** @var int */
private $endianness;

/**
* @param string $contents
* @param int $endianness
* @throws \Exception
*/
public function __construct(string $contents, int $endianness = BinaryValue::ENDIANNESS_LITTLE_ENDIAN)
{
if (!in_array($endianness, [BinaryValue::ENDIANNESS_LITTLE_ENDIAN, BinaryValue::ENDIANNESS_BIG_ENDIAN], true)) {
throw new \Exception('Invalid endianness type.');
}

$this->contents = $contents;
$this->endianness = $endianness;
}

/**
* @param string $filename
* @param int $endianness
* @return ByteStream
* @throws \Exception
*/
public static function createFromFile(string $filename, int $endianness = BinaryValue::ENDIANNESS_LITTLE_ENDIAN): ByteStream
{
$contents = file_get_contents($filename);

return new self($contents, $endianness);
}

/**
* @return bool
*/
public function isEof(): bool
{
return $this->cursor === strlen($this->contents);
}

/**
* @param int $count
* @return string
* @throws \Exception
*/
public function readBytes(int $count): string
{
if ($this->isEof()) {
throw new \Exception("Cannot read value: End of stream reached at position {$this->cursor}.");
}

$value = substr($this->contents, $this->cursor, $count);
$this->cursor += $count;

if (strlen($value) !== $count) {
throw new \Exception("Cannot read value: Unexpected end of stream at position {$this->cursor}.");
}

return $value;
}

/**
* @param int $maxLength
* @param string $encoding
* @return string
* @throws \Exception
*/
public function readString(int $maxLength, string $encoding = 'UTF-8'): string
{
$string = $this->readBytes($maxLength);

$stringTerminatorPosition = strpos($string, chr(0));
if ($stringTerminatorPosition !== false) {
$trimmedString = substr($string, 0, $stringTerminatorPosition);
} else {
$trimmedString = substr($string, 0);
}

if ($encoding === 'UTF-8') {
return $trimmedString;
}

return mb_convert_encoding($trimmedString, 'UTF-8', $encoding);
}

/**
* @param int|null $endianness
* @return Byte
* @throws \Exception
*/
public function readByte(?int $endianness = null): Byte
{
if (!in_array($endianness, [null, BinaryValue::ENDIANNESS_LITTLE_ENDIAN, BinaryValue::ENDIANNESS_BIG_ENDIAN], true)) {
throw new \Exception('Invalid endianness type.');
}

$binaryValue = $this->readBytes(1);

return new Byte($binaryValue, $endianness ?? $this->endianness);
}

/**
* @param int|null $endianness
* @return SignedByte
* @throws \Exception
*/
public function readSignedByte(?int $endianness = null): SignedByte
{
if (!in_array($endianness, [null, BinaryValue::ENDIANNESS_LITTLE_ENDIAN, BinaryValue::ENDIANNESS_BIG_ENDIAN], true)) {
throw new \Exception('Invalid endianness type.');
}

$binaryValue = $this->readBytes(1);

return new SignedByte($binaryValue, $endianness ?? $this->endianness);
}

/**
* @param int|null $endianness
* @return Word
* @throws \Exception
*/
public function readWord(?int $endianness = null): Word
{
if (!in_array($endianness, [null, BinaryValue::ENDIANNESS_LITTLE_ENDIAN, BinaryValue::ENDIANNESS_BIG_ENDIAN], true)) {
throw new \Exception('Invalid endianness type.');
}

$binaryValue = $this->readBytes(2);

return new Word($binaryValue, $endianness ?? $this->endianness);
}

/**
* @param int|null $endianness
* @return SignedWord
* @throws \Exception
*/
public function readSignedWord(?int $endianness = null): SignedWord
{
if (!in_array($endianness, [null, BinaryValue::ENDIANNESS_LITTLE_ENDIAN, BinaryValue::ENDIANNESS_BIG_ENDIAN], true)) {
throw new \Exception('Invalid endianness type.');
}

$binaryValue = $this->readBytes(2);

return new SignedWord($binaryValue, $endianness ?? $this->endianness);
}

/**
* @param int|null $endianness
* @return Dword
* @throws \Exception
*/
public function readDword(?int $endianness = null): Dword
{
if (!in_array($endianness, [null, BinaryValue::ENDIANNESS_LITTLE_ENDIAN, BinaryValue::ENDIANNESS_BIG_ENDIAN], true)) {
throw new \Exception('Invalid endianness type.');
}

$binaryValue = $this->readBytes(4);

return new Dword($binaryValue, $endianness ?? $this->endianness);
}

/**
* @param int|null $endianness
* @return SignedDword
* @throws \Exception
*/
public function readSignedDword(?int $endianness = null): SignedDword
{
if (!in_array($endianness, [null, BinaryValue::ENDIANNESS_LITTLE_ENDIAN, BinaryValue::ENDIANNESS_BIG_ENDIAN], true)) {
throw new \Exception('Invalid endianness type.');
}

$binaryValue = $this->readBytes(4);

return new SignedDword($binaryValue, $endianness ?? $this->endianness);
}
}
Loading

0 comments on commit dca8af5

Please sign in to comment.