NIB Parser Model

class nibarchive.NIBFormatError

Exception raised for errors related to NIB format.

This exception is raised when there is an error or inconsistency in the parsed NIB archive.

nibarchive.is_nib(fp) bool

Check if the given file or byte array represents a NIB archive.

This function reads the first 10 bytes of the file or byte array and compares it with the expected magic bytes to determine if it is a NIB archive.

Parameters:

fp (Union[io.IOBase, bytes, bytearray]) – File or byte array to check.

Returns:

True if the file or byte array is a NIB archive, False otherwise.

Return type:

bool

Raises:
  • TypeError – If the input is not a file object or byte array.

  • ValueError – If the length of the input is not 10 bytes.

nibarchive.varint(buf: bytes | IOBase, offset: int = 0) tuple[int, int]

Decode a variable-length integer (varint) from a byte buffer.

This function decodes a varint from the given byte buffer and returns the decoded integer value. A variable integer can consume up to two bytes and is compressed into the first 7 bits only.

Parameters:
  • buf (bytes) – Byte buffer containing the varint.

  • offset (int) – Offset in the byte buffer to start decoding (default: 0).

Returns:

The decoded integer value and its byte count

Return type:

int

The bit encoding can be defined as follows:

┌───────────┬──────────────────────────────────┐
│ Value     │ Bitcoding                        │
├───────────┼──────────────────────────────────┤
│ 0-127     │ 1 b b b b b b b                  │ b := number from 0 to 9
├───────────┼──────────────────────────────────┤
│ 128-16383 │ 0 b b b b b b b  1 b b b b b b b │
└───────────┴──────────────────────────────────┘
class nibarchive.NIBArchiveParser(verify: bool = True)

A simple parser for NIB archives.

Parameters:

verify (bool) – Flag indicating whether to perform verification checks during parsing (default: True).

parse(fp: IOBase) NIBArchive

Parses the NIB archive.

This method reads the NIB archive from the given file object (fp) and constructs a NIBArchive object representing the parsed contents.

Parameters:

fp (io.IOBase) – File object containing the NIB archive.

Returns:

The parsed NIBArchive object.

Return type:

NIBArchive

Raises:
  • TypeError – If the input is not a file object.

  • NIBFormatError – If a verification check fails.