-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
24 bit integer support, forms strx3, addrx3 (#553)
* 24 bit integer support, forms strx3, addrx3 * More DWARFv1 constants * More DWARFv1 constants * LLVM lineprog header content type codes * No global declarations
- Loading branch information
Showing
5 changed files
with
71 additions
and
4 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 |
---|---|---|
|
@@ -6,9 +6,10 @@ | |
# Eli Bendersky ([email protected]) | ||
# This code is in the public domain | ||
#------------------------------------------------------------------------------- | ||
from struct import Struct | ||
from ..construct import ( | ||
Subconstruct, ConstructError, ArrayError, Adapter, Field, RepeatUntil, | ||
Rename, SizeofError, Construct | ||
Rename, SizeofError, Construct, StaticField | ||
) | ||
|
||
|
||
|
@@ -110,3 +111,30 @@ def _build(self, obj, stream, context): | |
context[self.name] = stream.tell() | ||
def _sizeof(self, context): | ||
return 0 | ||
|
||
_UBInt24_packer = Struct(">BH") | ||
_ULInt24_packer = Struct("<HB") | ||
|
||
class UBInt24(StaticField): | ||
"""unsigned, big endian 24-bit integer""" | ||
def __init__(self, name): | ||
StaticField.__init__(self, name, 3) | ||
|
||
def _parse(self, stream, context): | ||
(h, l) = _UBInt24_packer.unpack(StaticField._parse(self, stream, context)) | ||
return l | (h << 16) | ||
|
||
def _build(self, obj, stream, context): | ||
StaticField._build(self, _UBInt24_packer.pack(obj >> 16, obj & 0xFFFF), stream, context) | ||
|
||
class ULInt24(StaticField): | ||
"""unsigned, little endian 24-bit integer""" | ||
def __init__(self, name): | ||
StaticField.__init__(self, name, 3) | ||
|
||
def _parse(self, stream, context): | ||
(l, h) = _ULInt24_packer.unpack(StaticField._parse(self, stream, context)) | ||
return l | (h << 16) | ||
|
||
def _build(self, obj, stream, context): | ||
StaticField._build(self, _ULInt24_packer.pack(obj & 0xFFFF, obj >> 16), stream, context) |
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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#------------------------------------------------------------------------------ | ||
# elftools tests | ||
# | ||
# Seva Alekseyev ([email protected]) | ||
# This code is in the public domain | ||
#------------------------------------------------------------------------------ | ||
import unittest | ||
import random | ||
from io import BytesIO | ||
|
||
from elftools.common.construct_utils import ULInt24, UBInt24 | ||
from elftools.common.utils import struct_parse | ||
|
||
class TestInt24(unittest.TestCase): | ||
def test_main(self): | ||
# Testing parsing and building, both LE and BE | ||
b = random.randbytes(3) | ||
|
||
n = struct_parse(UBInt24(''), BytesIO(b)) | ||
self.assertEqual(n, (b[0] << 16) | (b[1] << 8) | b[2]) | ||
s = UBInt24('').build(n) | ||
self.assertEqual(s, b) | ||
|
||
n = struct_parse(ULInt24(''), BytesIO(b)) | ||
self.assertEqual(n, b[0] | (b[1] << 8) | (b[2] << 16)) | ||
s = ULInt24('').build(n) | ||
self.assertEqual(s, b) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |