Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
source actions-ci/install.sh
- name: Pip install pylint, black, & Sphinx
run: |
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme
- name: Library version
run: git describe --dirty --always --tags
- name: PyLint
Expand Down
312 changes: 280 additions & 32 deletions adafruit_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,274 @@


TABLE_A2B_B64 = (
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
62,
-1,
-1,
-1,
63,
52,
53,
54,
55,
56,
57,
58,
59,
60,
61,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
-1,
-1,
-1,
-1,
-1,
-1,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case we should return this table to its original formatting and disable Black around this block of code.


TABLE_B2A_B64 = (
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
TABLE_B2A_B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"


class Error(Exception):
"""Exception raised on errors. These are usually programming errors."""

# pylint: disable=unnecessary-pass
pass


if not "unhexlify" in globals():
# pylint: disable=function-redefined
def unhexlify(hexstr):
Expand Down Expand Up @@ -104,18 +346,23 @@ def hexlify(data):
if not data:
raise TypeError("Data provided is zero-length")
data = "".join("%02x" % i for i in data)
return bytes(data, 'utf-8')
return bytes(data, "utf-8")


B2A_HEX = hexlify
A2B_HEX = unhexlify


def _transform(n):
if n == -1:
return '\xff'
return "\xff"
return chr(n)
TABLE_A2B_B64 = ''.join(map(_transform, TABLE_A2B_B64))


TABLE_A2B_B64 = "".join(map(_transform, TABLE_A2B_B64))
assert len(TABLE_A2B_B64) == 256


def a2b_base64(b64_data):
"""Convert a block of base64 data back to binary and return the binary data.

Expand All @@ -132,12 +379,12 @@ def a2b_base64(b64_data):
char = chr(char)
if char == "=":
if quad_pos > 2 or (quad_pos == 2 and last_char_was_a_pad):
break # stop on 'xxx=' or on 'xx=='
break # stop on 'xxx=' or on 'xx=='
last_char_was_a_pad = True
else:
n = ord(TABLE_A2B_B64[ord(char)])
if n == 0xff:
continue # ignore strange characters
if n == 0xFF:
continue # ignore strange characters
#
# Shift it in on the low end, and see if there's
# a byte ready for output.
Expand All @@ -147,15 +394,16 @@ def a2b_base64(b64_data):
#
if leftbits >= 8:
leftbits -= 8
res.append((leftchar >> leftbits).to_bytes(1, 'big'))
leftchar &= ((1 << leftbits) - 1)
res.append((leftchar >> leftbits).to_bytes(1, "big"))
leftchar &= (1 << leftbits) - 1
#
last_char_was_a_pad = False
else:
if leftbits != 0:
raise Exception("Incorrect padding")

return b''.join(res)
return b"".join(res)


def b2a_base64(bin_data):
"""Convert binary data to a line of ASCII characters in base64 coding.
Expand All @@ -173,18 +421,18 @@ def b2a_base64(bin_data):
# Shift into our buffer, and output any 6bits ready
leftchar = (leftchar << 8) | char
leftbits += 8
res.append(TABLE_B2A_B64[(leftchar >> (leftbits-6)) & 0x3f])
res.append(TABLE_B2A_B64[(leftchar >> (leftbits - 6)) & 0x3F])
leftbits -= 6
if leftbits >= 6:
res.append(TABLE_B2A_B64[(leftchar >> (leftbits-6)) & 0x3f])
res.append(TABLE_B2A_B64[(leftchar >> (leftbits - 6)) & 0x3F])
leftbits -= 6
#
if leftbits == 2:
res.append(TABLE_B2A_B64[(leftchar & 3) << 4])
res.append("=")
res.append("=")
elif leftbits == 4:
res.append(TABLE_B2A_B64[(leftchar & 0xf) << 2])
res.append(TABLE_B2A_B64[(leftchar & 0xF) << 2])
res.append("=")
res.append('\n')
return bytes(''.join(res), 'ascii')
res.append("\n")
return bytes("".join(res), "ascii")
Loading