Skip to content

Commit a4ce5b0

Browse files
committed
Improved codec: excess3
1 parent 1744eed commit a4ce5b0

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

codext/binary/excess3.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
'This is a test!': ";t7C\x84H6T8D\x83e<\xa3eD\x944D\x84I6`",
1616
'This is another test ': ";t7C\x84H6T8D\x83e<\xa4CDDICt4DseD\x944D\x84I6P",
1717
},
18+
'dec(excess-3|xs3)': {
19+
'\x00': None,
20+
'\xff': None,
21+
';t7C\x84H6T8D\x83e<\xa3eD\x944D\x84I6`': "This is a test!",
22+
';t7C\x84H6T8D\x83e<\xa4CDDICt4DseD\x944D\x84I6P': "This is another test ",
23+
},
1824
}
1925

2026

@@ -24,6 +30,10 @@
2430
}
2531

2632

33+
class Excess3DecodeError(ValueError):
34+
pass
35+
36+
2737
def excess3_encode(text, errors="strict"):
2838
r, bits = "", ""
2939
for c in text:
@@ -40,13 +50,15 @@ def excess3_encode(text, errors="strict"):
4050
def excess3_decode(text, errors="strict"):
4151
code = {v: k for k, v in CODE.items()}
4252
r, d = "", ""
43-
for c in text:
53+
for i, c in enumerate(text):
4454
bin_c = bin(ord(c))[2:].zfill(8)
45-
for i in range(0, 8, 4):
55+
for k in range(0, 8, 4):
56+
hb = bin_c[k:k+4]
4657
try:
47-
d += code[bin_c[i:i+4]]
58+
d += code[hb]
4859
except KeyError: # (normal case) occurs when 0000 was used for padding
49-
break
60+
if i != len(text) - 1 or k != 4 or hb != "0000":
61+
d += handle_error("excess3", errors, Excess3DecodeError, decode=True)(hb, i)
5062
if len(d) == 3:
5163
r += chr(int(d))
5264
d = ""

0 commit comments

Comments
 (0)