Skip to content

Releases: mosamadeeb/PyBinaryReader

binary_reader v1.4.3

10 Jun 21:55
Compare
Choose a tag to compare

Changelog:

  • Fixed #2. write_str_fixed did not properly trim when using 2-byte based encodings.
  • PR: #3 Added method to read strings until a string token is found. (Thanks, @CapitanRetraso!)

binary_reader v1.4.2

15 Jun 08:56
Compare
Choose a tag to compare

Changelog:

  • Added a method to write strings with fixed size: br.write_str_fixed(string, size, encoding)
  • Changed the behavior of br.read_str(size, encoding) to read 0 bytes if the size is not given

binary_reader v1.4.0

26 May 09:18
Compare
Choose a tag to compare

Changelog:

  • Added methods to check for end of file
    • past_eof() will return True if the current position is after the end of file.
    • eof() will return True if the current position is at/ after the end of file.
  • Allow trimming to 0 size
    • trim(0) will now work.
  • Fixed read_bytes returning a tuple
  • Added basic struct-like implementation to allow classes to be read/written.

BinaryReader now has read_struct and write_struct methods. These methods are only used for calling the __br_read__ and __br_write__ methods of the given class/object, respectively. The class/object must inherit from BrStruct, a new class that contains the signatures of these methods.

Classes inheriting from BrStruct can read/write other BrStruct classes as well.

Example of a class inheriting from BrStruct:

from binary_reader import BinaryReader, BrStruct

class MyStruct(BrStruct):
    def __br_read__(self, br):
        # no extra arguments are needed, but they can be given as *args in the
        # binary reader's read_struct and write_struct methods (check __br_write__ below)
        self.value1 = br.read_int32()
        self.value2 = br.read_float()

    def __br_write__(self, br, some_value):
        # some_value is an additional argument that will be given to the binary reader's write_struct method
        br.write_uint32(self.value1 + some_value)
        br.write_float(self.value2)

Example usage of the new read_struct and write_struct methods:

# will read and return 2 MyStruct objects as a tuple, after calling each one's __br_read__ method
(my_struct1, my_struct2) = br.read_struct(MyStruct, count=2)

# will write my_struct1 by calling its __br_write__ method and passing 42 as the 'some_value' argument
br.write_struct(my_struct1, 42)

binary_reader v1.3.2

18 May 16:39
Compare
Choose a tag to compare

Changelog:

  • Changed whence and endianness to enums.
  • Fixed issue with seeking relative to the end.

Methods that accept a whence parameter can now be used with these:

Whence.BEGIN
Whence.CUR
Whence.END

Methods that accept an endianness parameter can now be used with these:

Endian.LITTLE
Endian.BIG

binary_reader v1.3.1

10 May 23:07
Compare
Choose a tag to compare

Fixed some issues with align and pad.

binary_reader v1.3

10 May 13:39
Compare
Choose a tag to compare

Changelog:

  • Added an encoding parameter in the constructer which defaults to 'utf-8'.
  • Added context managers for with statement. See examples below.

The BinaryReader's buffer will get cleared after returning to the original context.

with BinaryReader() as br:
    br.write_uint32(0)

The BinaryReader will seek_to the given offset, and will return to the original position before the method was called after exiting the context.

# Position is pos
with br.seek_to(offset):
    # Position is offset
    br.read_uint32()
    # Position is offset + 4
# Position is pos

You can also use as to get a reference to the BinaryReader, if you prefer to use a different variable name inside the new context.

# Works the same way as in the previous example
with br.seek_to(offset, whence) as section:
    section.read_uint32()

binary_reader v1.2

10 May 11:31
Compare
Choose a tag to compare
Update internal version

binary_reader v1.1

10 May 11:19
Compare
Choose a tag to compare
Add PyPi dependencies