Skip to content

Instantly share code, notes, and snippets.

@v3l0c1r4pt0r
Created January 30, 2019 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save v3l0c1r4pt0r/f32faacdc979a198e0e78846b5db1035 to your computer and use it in GitHub Desktop.
Save v3l0c1r4pt0r/f32faacdc979a198e0e78846b5db1035 to your computer and use it in GitHub Desktop.
Quick and dirty bitmap format support using makeelf classes
from makeelf.type.uint16 import uint16
from makeelf.type.uint32 import uint32
## \class BITMAPFILEHEADER
# \brief Bitmap main header
# \see https://docs.microsoft.com/en-us/windows/desktop/api/wingdi/ns-wingdi-bitmapfileheader
class BITMAPFILEHEADER:
def __init__(self, bfType=0x4d42, bfSize=0, bfReserved1=0, bfReserved2=0,
bfOffBits=0, little=False):
## Signature
# \details Magic 'BM'
self.bfType = bfType
## File size
self.bfSize = bfSize
self.bfReserved1 = bfReserved1
self.bfReserved2 = bfReserved2
## Offset to bitmap
self.bfOffBits = bfOffBits
## Header endianness indicator
# \details Is true, if header values are meant to be stored as
# little-endian or false otherwise
self.little = little
def __str__(self):
return '{bfType=%s, bfSize=%s, bfOffBits=%s}' % (self.bfType,
self.bfSize, self.bfOffBits)
def __repr__(self):
# Bad idea: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
return '%s(%s, %s, %s)' % (type(self).__name__, bytes(uint16(self.bfType, True)), self.bfSize,
self.bfOffBits)
def __eq__(self, rhs):
return type(self) == type(rhs) and \
self.bfType == rhs.bfType and \
self.bfType == rhs.bfType and \
self.bfType == rhs.bfType and \
self.bfType == rhs.bfType and \
self.bfOffBits == rhs.bfOffBits
def __bytes__(self):
Type = uint16(self.bfType, little=self.little)
Size = uint32(self.bfSize, little=self.little)
Reserved1 = uint16(self.bfReserved1, little=self.little)
Reserved2 = uint16(self.bfReserved2, little=self.little)
OffBits = uint32(self.bfOffBits, little=self.little)
return bytes(Type) + bytes(Size) + bytes(Reserved1) + \
bytes(Reserved2) + bytes(OffBits)
def from_bytes(b, little=False):
bfType, b = uint16.from_bytes(b, little=little)
bfSize, b = uint32.from_bytes(b, little=little)
bfReserved1, b = uint16.from_bytes(b, little=little)
bfReserved2, b = uint16.from_bytes(b, little=little)
bfOffBits, b = uint32.from_bytes(b, little=little)
return BITMAPFILEHEADER(bfType.integer, bfSize.integer, bfReserved1.integer,
bfReserved2.integer, bfOffBits.integer, little), b
def __len__(self):
return len(bytes(self))
## \class BITMAPV4HEADER
# \brief Bitmap DIB header version 4
# \see https://docs.microsoft.com/en-us/windows/desktop/api/wingdi/ns-wingdi-bitmapv4header
class BITMAPV4HEADER:
def __init__(self, bv4Size=0, bv4Width=0, bv4Height=0, bv4Planes=0,
bv4BitCount=0, rest=bytes(0x5c), little=False):
self.bv4Size = bv4Size
self.bv4Width = bv4Width
self.bv4Height = bv4Height
self.bv4Planes = bv4Planes
self.bv4BitCount = bv4BitCount
self.rest = rest
## Header endianness indicator
# \details Is true, if header values are meant to be stored as
# little-endian or false otherwise
self.little = little
def __str__(self):
return '{bv4Size=%s, bv4Width=%s, bv4Height=%s, bv4Planes=%s, ' \
'bv4BitCount=%s, ...}' % (self.bv4Size, self.bv4Width,
self.bv4Height, self.bv4Planes, self.bv4BitCount)
def __repr__(self):
return '%s(%s, %s, %s, %s, %s, ...)' % (type(self).__name__, self.bv4Size,
self.bv4Width, self.bv4Height, self.bv4Planes, self.bv4BitCount)
def __eq__(self, rhs):
return type(self) == type(rhs) and \
self.bv4Size == rhs.bv4Size and \
self.bv4Size == rhs.bv4Size and \
self.bv4Size == rhs.bv4Size and \
self.bv4Size == rhs.bv4Size and \
self.bv4BitCount == rhs.bv4BitCount
def __bytes__(self):
bv4Size = uint32(self.bv4Size, little=self.little)
bv4Width = uint32(self.bv4Width, little=self.little)
bv4Height = uint32(self.bv4Height, little=self.little)
bv4Planes = uint16(self.bv4Planes, little=self.little)
bv4BitCount = uint16(self.bv4BitCount, little=self.little)
return bytes(bv4Size) + bytes(bv4Width) + bytes(bv4Height) + \
bytes(bv4Planes) + bytes(bv4BitCount) + self.rest
def from_bytes(b, little=False):
bv4Size, b = uint32.from_bytes(b, little=little)
bv4Width, b = uint32.from_bytes(b, little=little)
bv4Height, b = uint32.from_bytes(b, little=little)
bv4Planes, b = uint16.from_bytes(b, little=little)
bv4BitCount, b = uint16.from_bytes(b, little=little)
rest, b = b[:0x5c], b[0x5c:]
return BITMAPV4HEADER(bv4Size, bv4Width, bv4Height, bv4Planes,
bv4BitCount, rest, little), b
def __len__(self):
return len(bytes(self))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment