Utilities

leb128

Little Endian Base 128 (LEB128) variable length encoding

https://en.wikipedia.org/wiki/LEB128#Unsigned_LEB128

ppci.utils.leb128.signed_leb128_decode(data) → int

Read variable length encoded 128 bits signed integer.

>>> from ppci.utils.leb128 import signed_leb128_decode
>>> signed_leb128_decode(iter(bytes([0x9b, 0xf1, 0x59])))
-624485
ppci.utils.leb128.signed_leb128_encode(value: int) → bytes

Encode the given number as signed leb128

>>> from ppci.utils.leb128 import signed_leb128_encode
>>> signed_leb128_encode(-1337)
b'Çu'
ppci.utils.leb128.unsigned_leb128_decode(data) → int

Read variable length encoded 128 bits unsigned integer

>>> from ppci.utils.leb128 import unsigned_leb128_decode
>>> signed_leb128_decode(iter(bytes([0xe5, 0x8e, 0x26])))
624485
ppci.utils.leb128.unsigned_leb128_encode(value: int) → bytes

Encode number as into unsigned leb128 encoding

>>> from ppci.utils.leb128 import unsigned_leb128_encode
>>> unsigned_leb128_encode(42)
b'*'

Codepage

Cool idea to load actual object code into memory and execute it from python using ctypes

Credits for idea: Luke Campagnola

class ppci.utils.codepage.Mod(obj, callbacks=None)

Container for machine code

class ppci.utils.codepage.WinPage(size)

Nice windows hack to emulate mmap.

Copied from: https://github.com/campagnola/pycca/blob/master/pycca/asm/codepage.py

ppci.utils.codepage.load_code_as_module(source_file, reporter=None)

Load c3 code as a module

Reporting

To create a nice report of what happened during compilation, this file implements several reporting types.

Reports can be written to plain text, or html.

class ppci.utils.reporting.DummyReportGenerator

Report generator which reports into the void

class ppci.utils.reporting.ReportGenerator

Implement all these function to create a custom reporting generator

dump_exception(einfo)

List the given exception in report

dump_instructions(instructions, arch)

Print instructions

Exe files

The following module can help when working with exe files.

Implements the exe file format used by windows.

See also:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms680547(v=vs.85).aspx

Another excellent effort: https://github.com/erocarrera/pefile

ppci.utils.exefile.align(f, alignment, padding_value=0)

Pad with 0 bytes until certain alignment is met

ppci.utils.exefile.read_exe(f)

Process an exe file

ppci.utils.exefile.write_dos_stub(f)

Write dos stub to file. This stub prints to stdout: ‘this program cannot be run in dos mode’

uboot image files

Uboot image file format

class ppci.utils.uboot_image.ApplicationType

Application type

class ppci.utils.uboot_image.Architecture

Computer architecture

class ppci.utils.uboot_image.Compression

Compression types

class ppci.utils.uboot_image.OperatingSystem

Operating system

ppci.utils.uboot_image.write_uboot_image(f, data: bytes, image_name='foobar', load_address=256, entry_point=256, os=<OperatingSystem.INVALID: 0>, arch=<Architecture.OPENRISC: 21>)

Write uboot image to file