Difference between revisions of "Zerocode"
Jump to navigation
Jump to search
(Added a modern alternative.) |
m (Fix for >127 zeroes) |
||
Line 113: | Line 113: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
def zero_encode(input: bytes) -> bytes: | def zero_encode(input: bytes) -> bytes: | ||
out = | out = bytearray() | ||
i, n = 0, len(input) | i, n = 0, len(input) | ||
while i < n: # While-loop for index skipping. | while i < n: # While-loop for index skipping. | ||
Line 123: | Line 123: | ||
v = memoryview(input[i:]); | v = memoryview(input[i:]); | ||
zeroes = next((x for x, byte in enumerate(v) if byte != 0x00), len(v)) | zeroes = next((x for x, byte in enumerate(v) if byte != 0x00), len(v)) | ||
out | out.extend([0x00, zeroes]) | ||
i += zeroes | i += zeroes | ||
case | case byte: | ||
out | out.append(byte) | ||
i += 1 | i += 1 | ||
return bytes(out | return bytes(out) | ||
def zero_decode(input: bytes) -> bytes: | def zero_decode(input: bytes) -> bytes: | ||
out = | out = bytearray() | ||
i, n = 0, len(input) | i, n = 0, len(input) | ||
while i < n: # While-loop for index skipping. | while i < n: # While-loop for index skipping. | ||
match _ := input[i]: | match _ := input[i]: | ||
case 0x00: | case 0x00: | ||
out | out.extend(b'\0' * input[i + 1]) | ||
i += 2 | i += 2 | ||
case byte: | case byte: | ||
out | out.append(byte) | ||
i += 1 | i += 1 | ||
return bytes(out | return bytes(out) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category: AW Groupies]] | [[Category: AW Groupies]] |
Revision as of 02:29, 9 September 2024
zero encoding and decoding functions and a couple of useful functions from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/510399
# From http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/510399
# From http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/510399
def ByteToHex( byteStr ):
"""
Convert a byte string to it's hex string representation e.g. for output.
"""
# Uses list comprehension which is a fractionally faster implementation than
# the alternative, more readable, implementation below
#
# hex = []
# for aChar in byteStr:
# hex.append( "%02X " % ord( aChar ) )
#
# return ''.join( hex ).strip()
return ''.join( [ "%02X " % ord( x ) for x in byteStr ] ).strip()
def HexToByte( hexStr ):
"""
Convert a string hex byte values into a byte string. The Hex Byte values may
or may not be space separated.
"""
# The list comprehension implementation is fractionally slower in this case
#
# hexStr = ''.join( hexStr.split(" ") )
# return ''.join( ["%c" % chr( int ( hexStr[i:i+2],16 ) ) \
# for i in range(0, len( hexStr ), 2) ] )
bytes = []
hexStr = ''.join( hexStr.split(" ") )
for i in range(0, len(hexStr), 2):
bytes.append( chr( int (hexStr[i:i+2], 16 ) ) )
return ''.join( bytes )
def zero_encode(inputbuf):
newstring =""
zero = False
zero_count = 0
for c in inputbuf:
if c != '\0':
if zero_count != 0:
newstring = newstring + chr(zero_count)
zero_count = 0
zero = False
newstring = newstring + c
else:
if zero == False:
newstring = newstring + c
zero = True
zero_count = zero_count + 1
if zero_count != 0:
newstring = newstring + chr(zero_count)
return newstring
def zero_decode(inputbuf):
newstring =""
in_zero = False
for c in inputbuf:
if c != '\0':
if in_zero == True:
zero_count = ord(c)
zero_count = zero_count -1
while zero_count>0:
newstring = newstring + '\0'
zero_count = zero_count -1
in_zero = False
else:
newstring = newstring + c
else:
newstring = newstring + c
in_zero = True
return newstring
def zero_decode_ID(inputbuf):
newstring =""
in_zero = False
#print "in encode, input is", ByteToHex(inputbuf)
for c in inputbuf:
if c != '\0':
if in_zero == True:
zero_count = ord(c)
zero_count = zero_count -1
while zero_count>0:
newstring = newstring + '\0'
zero_count = zero_count -1
in_zero = False
else:
newstring = newstring + c
else:
newstring = newstring + c
in_zero = True
return newstring[:4]
Alternate implementations for zero_encode
and zero_decode
in Python 3:
def zero_encode(input: bytes) -> bytes:
out = bytearray()
i, n = 0, len(input)
while i < n: # While-loop for index skipping.
match _ := input[i]:
case 0x00:
# Look ahead from current index.
# Find next nonzero byte.
# Use that index as length.
v = memoryview(input[i:]);
zeroes = next((x for x, byte in enumerate(v) if byte != 0x00), len(v))
out.extend([0x00, zeroes])
i += zeroes
case byte:
out.append(byte)
i += 1
return bytes(out)
def zero_decode(input: bytes) -> bytes:
out = bytearray()
i, n = 0, len(input)
while i < n: # While-loop for index skipping.
match _ := input[i]:
case 0x00:
out.extend(b'\0' * input[i + 1])
i += 2
case byte:
out.append(byte)
i += 1
return bytes(out)