views:

24

answers:

1

I have some binary data which has a corresponding "map" file which identifies each data point in the binary data by providing a bit offset and length. So, I might have the following mappings:

$paths = array ( "path1" => array ("offset" => 224, "size" => 2),
                 "path2" => array ("offset" => 226, "size" => 6),
);

In actuality I have tens of thousands of these paths to offset/size mappings. Now, given a path I want to be able to seek to the appropriate offset and read the number of bits as given by size. In this simple case, path1 and path2 represent a single byte where path1 is the first 2 bits and path2 are the last 6 bits.

I have already written this in Python and am now porting over the code to PHP for reasons I won't go into :)

Anyway, for whole bytes sizes at whole byte offsets I can just use unpack with the appropriate format string. What I have a problem with is how to handle these "oddly" sized sets of bits.

The only way I have thought of thus far would be finding the nearest bit offset where the offset modulus 8 gives 0 (whole byte) and is less than the given offset, then find the smallest size (again in whole byte sizes) from this new offset which encompasses all of the required bits, then use a bitwise & to mask the bits I want.

In Python I could just use the bitbuffer module which let me seek to a bit offset, then read any number of bits I wanted. Anything like this in the PHP world?

Thanks.

A: 

Your solution of grabbing the whole bytes then using a bitmask sounds like the way to do it. Then I guess you'd also do a right shift given that the bits you're interested in may not have ended on a byte boundary.

It'll be a slow way of doing it but I don't know of any internal PHP function that can do this sort of thing.

thomasrutter
This is the answer I was **afraid** of.
sberry2A
I'd imagine doing something like (0xffff << (32-$leftoffset)) | (0xffff >> (32-$rightoffset)) to creatre the bitmask - ooh note that on 64-bit PHP your ints will be 64-bit so may have to modify the above. Note that doing any sort of binary processing in PHP is gonna be slow if there's no library function for it, and I'm 99% sure there isn't (there's a small chance there's one and I just don't know about it).
thomasrutter