From 3ba39f1a3d1fb673c59595915d7cb8290c4aa060 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 24 Dec 2016 08:26:43 -0800 Subject: [PATCH] Fix more potential Python 3 issues in construct Use b"" strings for paddings/align and anything else dealing with binary data --- .travis.yml | 1 - elftools/construct/adapters.py | 76 +++++++++++++++++----------------- elftools/construct/macros.py | 2 +- 3 files changed, 39 insertions(+), 40 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4b14c03..37b4252 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,4 +6,3 @@ python: - "3.4" - "3.5" script: python test/all_tests.py - diff --git a/elftools/construct/adapters.py b/elftools/construct/adapters.py index c6251d4..54fcc08 100644 --- a/elftools/construct/adapters.py +++ b/elftools/construct/adapters.py @@ -25,11 +25,11 @@ class BitIntegerAdapter(Adapter): """ Adapter for bit-integers (converts bitstrings to integers, and vice versa). See BitField. - + Parameters: * subcon - the subcon to adapt * width - the size of the subcon, in bits - * swapped - whether to swap byte order (little endian/big endian). + * swapped - whether to swap byte order (little endian/big endian). default is False (big endian) * signed - whether the value is signed (two's complement). the default is False (unsigned) @@ -37,7 +37,7 @@ class BitIntegerAdapter(Adapter): default is 8. """ __slots__ = ["width", "swapped", "signed", "bytesize"] - def __init__(self, subcon, width, swapped = False, signed = False, + def __init__(self, subcon, width, swapped = False, signed = False, bytesize = 8): Adapter.__init__(self, subcon) self.width = width @@ -61,7 +61,7 @@ class MappingAdapter(Adapter): """ Adapter that maps objects to other objects. See SymmetricMapping and Enum. - + Parameters: * subcon - the subcon to map * decoding - the decoding (parsing) mapping (a dict) @@ -74,7 +74,7 @@ class MappingAdapter(Adapter): if `Pass` is used, the unmapped object will be passed as-is """ __slots__ = ["encoding", "decoding", "encdefault", "decdefault"] - def __init__(self, subcon, decoding, encoding, + def __init__(self, subcon, decoding, encoding, decdefault = NotImplemented, encdefault = NotImplemented): Adapter.__init__(self, subcon) self.decoding = decoding @@ -107,7 +107,7 @@ class FlagsAdapter(Adapter): Adapter for flag fields. Each flag is extracted from the number, resulting in a FlagsContainer object. Not intended for direct usage. See FlagsEnum. - + Parameters * subcon - the subcon to extract * flags - a dictionary mapping flag-names to their value @@ -130,13 +130,13 @@ class FlagsAdapter(Adapter): class StringAdapter(Adapter): """ - Adapter for strings. Converts a sequence of characters into a python + Adapter for strings. Converts a sequence of characters into a python string, and optionally handles character encoding. See String. - + Parameters: * subcon - the subcon to convert - * encoding - the character encoding name (e.g., "utf8"), or None to + * encoding - the character encoding name (e.g., "utf8"), or None to return raw bytes (usually 8-bit ASCII). """ __slots__ = ["encoding"] @@ -156,21 +156,21 @@ class PaddedStringAdapter(Adapter): r""" Adapter for padded strings. See String. - + Parameters: * subcon - the subcon to adapt - * padchar - the padding character. default is "\x00". - * paddir - the direction where padding is placed ("right", "left", or - "center"). the default is "right". - * trimdir - the direction where trimming will take place ("right" or + * padchar - the padding character. default is b"\x00". + * paddir - the direction where padding is placed ("right", "left", or + "center"). the default is "right". + * trimdir - the direction where trimming will take place ("right" or "left"). the default is "right". trimming is only meaningful for - building, when the given string is too long. + building, when the given string is too long. """ __slots__ = ["padchar", "paddir", "trimdir"] - def __init__(self, subcon, padchar = "\x00", paddir = "right", + def __init__(self, subcon, padchar = b"\x00", paddir = "right", trimdir = "right"): if paddir not in ("right", "left", "center"): - raise ValueError("paddir must be 'right', 'left' or 'center'", + raise ValueError("paddir must be 'right', 'left' or 'center'", paddir) if trimdir not in ("right", "left"): raise ValueError("trimdir must be 'right' or 'left'", trimdir) @@ -203,10 +203,10 @@ class PaddedStringAdapter(Adapter): class LengthValueAdapter(Adapter): """ - Adapter for length-value pairs. It extracts only the value from the + Adapter for length-value pairs. It extracts only the value from the pair, and calculates the length based on the value. See PrefixedArray and PascalString. - + Parameters: * subcon - the subcon returning a length-value pair """ @@ -219,12 +219,12 @@ class LengthValueAdapter(Adapter): class CStringAdapter(StringAdapter): r""" Adapter for C-style strings (strings terminated by a terminator char). - + Parameters: * subcon - the subcon to convert - * terminators - a sequence of terminator chars. default is "\x00". + * terminators - a sequence of terminator chars. default is b"\x00". * encoding - the character encoding to use (e.g., "utf8"), or None to - return raw-bytes. the terminator characters are not affected by the + return raw-bytes. the terminator characters are not affected by the encoding. """ __slots__ = ["terminators"] @@ -244,11 +244,11 @@ class TunnelAdapter(Adapter): to parse that data (bottom-up). For building it works in a top-down manner; first the upper layer builds the data, then the lower layer takes it and writes it to the stream. - + Parameters: * subcon - the lower layer subcon * inner_subcon - the upper layer (tunneled/nested) subcon - + Example: # a pascal string containing compressed data (zlib encoding), so first # the string is read, decompressed, and finally re-parsed as an array @@ -272,18 +272,18 @@ class TunnelAdapter(Adapter): class ExprAdapter(Adapter): """ A generic adapter that accepts 'encoder' and 'decoder' as parameters. You - can use ExprAdapter instead of writing a full-blown class when only a + can use ExprAdapter instead of writing a full-blown class when only a simple expression is needed. - + Parameters: * subcon - the subcon to adapt - * encoder - a function that takes (obj, context) and returns an encoded + * encoder - a function that takes (obj, context) and returns an encoded version of obj - * decoder - a function that takes (obj, context) and returns an decoded + * decoder - a function that takes (obj, context) and returns an decoded version of obj - + Example: - ExprAdapter(UBInt8("foo"), + ExprAdapter(UBInt8("foo"), encoder = lambda obj, ctx: obj / 4, decoder = lambda obj, ctx: obj * 4, ) @@ -311,11 +311,11 @@ class ConstAdapter(Adapter): """ Adapter for enforcing a constant value ("magic numbers"). When decoding, the return value is checked; when building, the value is substituted in. - + Parameters: * subcon - the subcon to validate * value - the expected value - + Example: Const(Field("signature", 2), "MZ") """ @@ -336,7 +336,7 @@ class ConstAdapter(Adapter): class SlicingAdapter(Adapter): """ Adapter for slicing a list (getting a slice from that list) - + Parameters: * subcon - the subcon to slice * start - start index @@ -358,7 +358,7 @@ class SlicingAdapter(Adapter): class IndexingAdapter(Adapter): """ Adapter for indexing a list (getting a single item from that list) - + Parameters: * subcon - the subcon to index * index - the index of the list to get @@ -377,11 +377,11 @@ class IndexingAdapter(Adapter): class PaddingAdapter(Adapter): r""" Adapter for padding. - + Parameters: * subcon - the subcon to pad * pattern - the padding pattern (character as byte). default is b"\x00" - * strict - whether or not to verify, during parsing, that the given + * strict - whether or not to verify, during parsing, that the given padding matches the padding pattern. default is False (unstrict) """ __slots__ = ["pattern", "strict"] @@ -404,9 +404,9 @@ class PaddingAdapter(Adapter): #=============================================================================== class Validator(Adapter): """ - Abstract class: validates a condition on the encoded/decoded object. + Abstract class: validates a condition on the encoded/decoded object. Override _validate(obj, context) in deriving classes. - + Parameters: * subcon - the subcon to validate """ diff --git a/elftools/construct/macros.py b/elftools/construct/macros.py index b62a18c..d768099 100644 --- a/elftools/construct/macros.py +++ b/elftools/construct/macros.py @@ -349,7 +349,7 @@ def Bitwise(subcon): resizer = resizer) return con -def Aligned(subcon, modulus = 4, pattern = "\x00"): +def Aligned(subcon, modulus = 4, pattern = b"\x00"): r"""aligns subcon to modulus boundary using padding pattern * subcon - the subcon to align * modulus - the modulus boundary (default is 4) -- 2.30.2