Binary-reader

Latest version: v1.4.3

Safety actively analyzes 625010 Python packages for vulnerabilities to keep your Python projects secure.

Scan your dependencies

Page 1 of 2

1.4.3

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!)

1.4.2

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

1.4.0

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`:
py
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:
py
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)

1.3.2

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:
py
Whence.BEGIN
Whence.CUR
Whence.END


Methods that accept an endianness parameter can now be used with these:
py
Endian.LITTLE
Endian.BIG

1.3.1

Fixed some issues with align and pad.

1.3

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.
py
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.
py
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.
py
Works the same way as in the previous example
with br.seek_to(offset, whence) as section:
section.read_uint32()

Page 1 of 2

© 2024 Safety CLI Cybersecurity Inc. All Rights Reserved.