mdurrant / php-binary-reader Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Hi,
I'm not sure but I think the method bitReader in the class Int32 is wrong.
My fix is on the masks. I think it should be :
$hiMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_LO) ^ 0xFF;
$loMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_HI) ^ 0xFF;
Case :
Buffer :
FF 07 00 00 00
Position : 1 (2nd byte)
Current bit : 2
Nest Byte : FF
Try to read int32.
The masks are :
hiMask : 00111111
loMask : 00000011
But should be :
hiMask : 11111100
loMask : 11000000
The result will be 1058799616 (111111000111000000000000000000) and should be 4229693440 (11111100000111000000000000000000).
Great project ๐
Can the latest commits from june be tagged into a release? Need the floating point reader but don't want to depend on dev-master
Thanks
Most of the functions specify return type int
. While I believe the ones reading from $this->byteReader
are returning string
. $this->byteReader->read
uses fread
which returns a string
(as mentioned in the comments for $this->byteReader->read
).
php-binary-reader/src/BinaryReader.php
Lines 166 to 173 in 8901cbf
Conversely, getUInt16
has return type string
in the comments. This should probably be int
.
php-binary-reader/src/BinaryReader.php
Lines 191 to 205 in 8901cbf
I think some other functions have similar problems.
I've been using the library to get contiguous bits from a binary file where the bits are not byte-aligned. I have not been getting the values I expect.
After much head-scratching, tweaking and testing I came to realise that the Bit reader wasn't working the way I expected.
It seems to be a little naive in that it assumes byte grouping and order is always based on standard approaches and so it doesn't return the anticipated values when I read bits in sequence from a file that doesn't follow convention.
For example, I have a data file with a series of 40-bit frames. Byte order becomes insignificant because the bits are sequential and variable-length, e.g. a two 14-bit integers, followed by a 5-bit integer, two individual bits, a 3-bit integer and finally 2 bits for padding to a full byte.
The problem appears to be that, for non-standard length frames like this (that potentially cross traditional byte boundaries), this library doesn't handle the bits in the strict order I'm asking for because it breaks the bits down into multi-byte chunks.
Starting at Type/Bit.php#37
for example, it explicitly tries to break down the length of bits requested into a number of whole bytes to read using the 'standard' methods (Int32, Int16 etc).
I've adapted this for my use case and I'm conscious I might not be the only person experiencing this issue, so I'm keen to integrate my changes.
While I'd like to merge my changes in, I first wanted to open up discussion for the best way to handle this as I'm sure the current Bit reader is still useful the way it is and I don't want to disrupt that.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.