-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release version 2.0 of dissect.cstruct
- Loading branch information
Showing
33 changed files
with
3,379 additions
and
2,089 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
*~ | ||
.*.swp | ||
*.pyc | ||
*.egg-info | ||
.DS_Store | ||
.tox | ||
.pytest_cache | ||
*.egg-info/ | ||
.tox/ | ||
.pytest_cache/ | ||
.eggs/ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
exclude .gitignore | ||
exclude .gitlab-ci.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,64 @@ | ||
from dissect.cstruct.compiler import Compiler | ||
from dissect.cstruct.expression import Expression | ||
from dissect.cstruct.types.base import Array, BaseType, RawType | ||
from dissect.cstruct.types.chartype import CharType | ||
from dissect.cstruct.types.instance import Instance | ||
from dissect.cstruct.types.structure import Structure, Field, Union | ||
from dissect.cstruct.types.voidtype import VoidType | ||
from dissect.cstruct.types.wchartype import WcharType | ||
from dissect.cstruct.types.packedtype import PackedType | ||
from dissect.cstruct.types.flag import Flag, FlagInstance | ||
from dissect.cstruct.types.enum import Enum, EnumInstance | ||
from dissect.cstruct.types.bytesinteger import BytesInteger | ||
from dissect.cstruct.types.pointer import Pointer, PointerInstance | ||
|
||
from dissect.cstruct.exceptions import ( | ||
Error, | ||
ParserError, | ||
ResolveError, | ||
NullPointerDereference, | ||
) | ||
|
||
from dissect.cstruct.cstruct import ( | ||
cstruct, | ||
ctypes, | ||
) | ||
|
||
from dissect.cstruct.utils import ( | ||
dumpstruct, | ||
hexdump, | ||
Instance, | ||
PointerInstance, | ||
Parser, | ||
RawType, | ||
BaseType, | ||
Error, | ||
ParserError, | ||
CompilerError, | ||
ResolveError, | ||
NullPointerDereference, | ||
) | ||
|
||
from dissect.cstruct.bitbuffer import BitBuffer | ||
|
||
__all__ = [ | ||
"cstruct", | ||
"ctypes", | ||
"dumpstruct", | ||
"hexdump", | ||
"Compiler", | ||
"Array", | ||
"Union", | ||
"Field", | ||
"Instance", | ||
"Structure", | ||
"Expression", | ||
"PackedType", | ||
"Pointer", | ||
"PointerInstance", | ||
"Parser", | ||
"VoidType", | ||
"WcharType", | ||
"RawType", | ||
"BaseType", | ||
"CharType", | ||
"Enum", | ||
"EnumInstance", | ||
"Flag", | ||
"FlagInstance", | ||
"BytesInteger", | ||
"BitBuffer", | ||
"cstruct", | ||
"ctypes", | ||
"dumpstruct", | ||
"hexdump", | ||
"Error", | ||
"ParserError", | ||
"CompilerError", | ||
"ResolveError", | ||
"NullPointerDereference", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class BitBuffer(object): | ||
"""Implements a bit buffer that can read and write bit fields.""" | ||
|
||
def __init__(self, stream, endian): | ||
self.stream = stream | ||
self.endian = endian | ||
|
||
self._type = None | ||
self._buffer = 0 | ||
self._remaining = 0 | ||
|
||
def read(self, field_type, bits): | ||
if self._remaining < 1 or self._type.size != field_type.size: | ||
self._type = field_type | ||
self._remaining = field_type.size * 8 | ||
self._buffer = field_type._read(self.stream) | ||
|
||
if self.endian != '>': | ||
v = self._buffer & ((1 << bits) - 1) | ||
self._buffer >>= bits | ||
self._remaining -= bits | ||
else: | ||
v = self._buffer & (((1 << (self._remaining - bits)) - 1) ^ ((1 << self._remaining) - 1)) | ||
v >>= self._remaining - bits | ||
self._remaining -= bits | ||
|
||
return v | ||
|
||
def write(self, field_type, data, bits): | ||
if self._remaining == 0: | ||
self._remaining = field_type.size * 8 | ||
self._type = field_type | ||
|
||
if self.endian != '>': | ||
self._buffer |= data << (self._type.size * 8 - self._remaining) | ||
else: | ||
self._buffer |= data << (self._remaining - bits) | ||
|
||
self._remaining -= bits | ||
|
||
def flush(self): | ||
self._type._write(self.stream, self._buffer) | ||
self._type = None | ||
self._remaining = 0 | ||
self._buffer = 0 | ||
|
||
def reset(self): | ||
self._type = None | ||
self._buffer = 0 | ||
self._remaining = 0 |
Oops, something went wrong.