Skip to content

Commit

Permalink
implement uleb128 parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jbremer committed Aug 22, 2018
1 parent 5471d3d commit 9890d99
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Currently Cockroach exports the following methods:
- bigint serialization
- ipv4 parsing
- null/pkcs7 (un)padding
- uleb128

* Bitwise

Expand Down
2 changes: 1 addition & 1 deletion roach/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from roach.hash.crc import crc32
from roach.hash.sha import md5, sha1, sha224, sha384, sha256, sha512
from roach.string.inet import ipv4
from roach.string.ops import asciiz, hex, unhex
from roach.string.ops import asciiz, hex, unhex, uleb128
from roach.structure import Structure

from roach.procmem import (
Expand Down
8 changes: 8 additions & 0 deletions roach/string/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ def hex(s):
def unhex(s):
return binascii.unhexlify(s)

def uleb128(s):
ret = 0
for idx in xrange(len(s)):
ret += (ord(s[idx]) & 0x7f) << (idx*7)
if ord(s[idx]) < 0x80:
break
return idx+1, ret

class Base64(object):
def encode(self, s):
return base64.b64encode(s)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from roach import (
int8, uint8, int16, uint16, int32, uint32, int64, uint64, bigint,
asciiz, pad, unpad, ipv4, pack, unpack, hex, unhex, base64
asciiz, pad, unpad, ipv4, pack, unpack, hex, unhex, base64, uleb128
)

def test_asciiz():
Expand All @@ -14,6 +14,10 @@ def test_hex():
assert hex("hello") == "68656c6c6f"
assert unhex("68656c6c6f") == "hello"

def test_uleb128():
assert uleb128("\x00") == (1, 0)
assert uleb128("\xe5\x8e\x26") == (3, 624485)

def test_pad():
assert pad("hello!!1", 8) == "hello!!1"
assert pad("hello", 8) == "hello\x03\x03\x03"
Expand Down

0 comments on commit 9890d99

Please sign in to comment.