BinSpec (short for Binary Specification) is a binary parsing library built with Python 3.
Project description
BinSpec
BinSpec (short for Binary Specification) is a binary parsing library built with Python 3.
Installation
Install BinSpec from PyPi:
pip install binspec
Usage
BinSpec is based around the Specification class which is used in tandem with SpecTypes to parse and validate binary data.
from binspec import Specification
from binspec.types import String, Int, Array
data = bytearray()
data.append("MAGIC".encode("utf8"))
data.append(0b01110110)
data.append(0b11001100)
spec = Specification(data)
magic = spec.expect(String(length=5, encoding="utf8"))
if magic != "MAGIC":
spec.fail("Expected magic string: MAGIC") # Manually fail and raise a SpecError.
n = spec.expect(Int(bytes=1))
print(n)
# 0b01110110 -> 118
flags = spec.expect(Array(Int(bits=2), length=4))
print(flags)
# [ 0b11, 0b00, 0b11, 0b00 ] -> [ 3, 0, 3, 0 ]
Currently, the builtin
SpecTypes consist ofString,Int,Int8,Int16,Int32,Int64,Packed,ArrayBytes, andBits. Refer to their docstrings for more inforomation.
In practice, data should come from an outside source, such as FileIO.read(). In the best case, a conversion to BytesIO is not necessary and stream-like object can be used directly to supply Specification.
from binspec import Specification
from binspec.types import String, Int, Array
with open("my_imaginary_file.binary", "rb") as f:
spec = Specification(f)
# ...
UI
Additonally, a simple webpage is provided to visualize a specification via a webpage.
from binspec.ui import show
data = bytearray()
# ...
spec = Specification(data)
show(spec, data) # A bytes-like copy of the data must be provided to show.
Creating Custom SpecTypes
To create a custom SpecType, create a class which derives from SpecType:
from binspec.types import SpecType
class ByteAsAString(SpecType):
pass
For this example, the custom SpecType will parse a byte as a string of 1s and 0s.
Continuing, SpecType requires two abstract methods, get_bit_length and parse to be implemented.
from binspec.types import SpecType
class BytesAsAString(SpecType):
def __init__(self, *, byte_count: int):
self.byte_count = byte_count
def get_bit_length(self):
return self.byte_count * 8
def parse(self, bits: bytes) -> str:
s = ""
for b in bits:
if b == 0:
s += "0"
else:
s += "1"
return s
And voila, now use ByteAsAString in Specification.expect (or another SpecType, like Array) to parse it.
from binspec import Specification
from binspec.types import Array
data = bytearray()
data.append(0b11110000)
data.append(0b10101010)
data.append(0b10101010)
data.append(0b11110000)
spec = Specification(data)
s = spec.expect(BytesAsAString(byte_count=2))
print(s)
# "1111000010101010"
arr = spec.expect(Array(BytesAsAString(byte_count=1), length=2))
print(arr)
# [ "10101010", "11110000" ]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file binspec-1.0.0.tar.gz.
File metadata
- Download URL: binspec-1.0.0.tar.gz
- Upload date:
- Size: 22.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a7b711d4ddd95cfbf1b182f35e1b910db393d891bb89f6e4b583b6a986f858c
|
|
| MD5 |
832acf306d4ee7b19ef5b73341263cef
|
|
| BLAKE2b-256 |
ea33d445eb1f25cf77a526ee95af5883b13cb2e774d401862f355ac912d712fd
|
File details
Details for the file binspec-1.0.0-py3-none-any.whl.
File metadata
- Download URL: binspec-1.0.0-py3-none-any.whl
- Upload date:
- Size: 22.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04e75922ef3c5a2ae2565abeaeb6bcb78de0bd19408afd37f92286f72afccd74
|
|
| MD5 |
df7c5b608903b340ccdb0c29feff4fa7
|
|
| BLAKE2b-256 |
1a4a9159342e80470eab2b6240b5f92d6a2ae77fa6740b6751647ae5e5ed0338
|