Use byte literals in examples and unit tests

In Python 2.x an unprefixed string literal produces a byte string.
In Python 3.x an unprefixed string literal produces a textual string.

To produce a byte string in both a b prefix is needed, e.g. b'foo'.
Since I believe Hammer works predominantly with byte strings I have used
b prefixes throughout.
This commit is contained in:
Alex Willmer 2019-05-10 21:54:07 +01:00
parent 8b4b8ddc57
commit 59ba68ef84
4 changed files with 203 additions and 203 deletions

View file

@ -23,13 +23,13 @@ def init_parser():
alpha = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))
# AUX.
plus = h.ch('+')
slash = h.ch('/')
equals = h.ch('=')
plus = h.ch(b'+')
slash = h.ch(b'/')
equals = h.ch(b'=')
bsfdig = h.choice(alpha, digit, plus, slash)
bsfdig_4bit = h.in_('AEIMQUYcgkosw048')
bsfdig_2bit = h.in_('AQgw')
bsfdig_4bit = h.in_(b'AEIMQUYcgkosw048')
bsfdig_2bit = h.in_(b'AQgw')
base64_3 = h.repeat_n(bsfdig, 4)
base64_2 = h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals)
base64_1 = h.sequence(bsfdig, bsfdig_2bit, equals, equals)

View file

@ -34,9 +34,9 @@ def act_bsfdig(p, user_data=None):
return c - 0x61 + 26
elif 0x30 <= c <= 0x39: # 0-9
return c - 0x30 + 52
elif c == '+':
elif c == b'+':
return 62
elif c == '/':
elif c == b'/':
return 63
else:
raise ValueError
@ -118,16 +118,16 @@ def init_parser():
# literals, or integers
digit = h.ch_range(0x30, 0x39)
alpha = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))
space = h.in_(" \t\n\r\f\v")
space = h.in_(b" \t\n\r\f\v")
# AUX.
plus = h.ch('+')
slash = h.ch('/')
equals = h.action(h.ch('='), act_equals)
plus = h.ch(b'+')
slash = h.ch(b'/')
equals = h.action(h.ch(b'='), act_equals)
bsfdig = h.action(h.choice(alpha, digit, plus, slash), act_bsfdig)
bsfdig_4bit = h.action(h.in_("AEIMQUYcgkosw048"), act_bsfdig_4bit)
bsfdig_2bit = h.action(h.in_("AQgw"), act_bsfdig_2bit)
bsfdig_4bit = h.action(h.in_(b"AEIMQUYcgkosw048"), act_bsfdig_4bit)
bsfdig_2bit = h.action(h.in_(b"AQgw"), act_bsfdig_2bit)
base64_3 = h.action(h.repeat_n(bsfdig, 4), act_base64_3)
base64_2 = h.action(h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals),
act_base64_2)

View file

@ -36,9 +36,9 @@ def bsfdig_value(p):
return c - 0x61 + 26
elif 0x30 <= c <= 0x39: # 0-9
return c - 0x30 + 52
elif c == '+':
elif c == b'+':
return 62
elif c == '/':
elif c == b'/':
return 63
return 0
@ -109,16 +109,16 @@ def init_parser():
# CORE
digit = h.ch_range(0x30, 0x39)
alpha = h.choice(h.ch_range(0x41, 0x5a), h.ch_range(0x61, 0x7a))
space = h.in_(" \t\n\r\f\v")
space = h.in_(b" \t\n\r\f\v")
# AUX.
plus = h.ch('+')
slash = h.ch('/')
equals = h.ch('=')
plus = h.ch(b'+')
slash = h.ch(b'/')
equals = h.ch(b'=')
bsfdig = h.choice(alpha, digit, plus, slash)
bsfdig_4bit = h.in_("AEIMQUYcgkosw048")
bsfdig_2bit = h.in_("AQgw")
bsfdig_4bit = h.in_(b"AEIMQUYcgkosw048")
bsfdig_2bit = h.in_(b"AQgw")
base64_3 = h.repeat_n(bsfdig, 4)
base64_2 = h.sequence(bsfdig, bsfdig, bsfdig_4bit, equals)
base64_1 = h.sequence(bsfdig, bsfdig_2bit, equals, equals)