Skip to content

Commit 22c52d1

Browse files
authored
Add Base2 encoding methods (Base2MSBF and Base2LSBF) to the Base64 module
1 parent b74c8f5 commit 22c52d1

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

Lib/base64.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Base16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data encodings"""
1+
"""Base2 (MSBF and LSBF), Base16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data encodings"""
22

33
# Modified 04-Oct-1995 by Jack Jansen to use binascii module
44
# Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support
@@ -17,6 +17,8 @@
1717
'b32hexencode', 'b32hexdecode', 'b16encode', 'b16decode',
1818
# Base85 and Ascii85 encodings
1919
'b85encode', 'b85decode', 'a85encode', 'a85decode', 'z85encode', 'z85decode',
20+
# Base2MSBF and Base2LSBF encodings
21+
'b2msbfencode', 'b2msbfdecode', 'b2lsbfencode', 'b2lsbfdecode',
2022
# Standard Base64 encoding
2123
'standard_b64encode', 'standard_b64decode',
2224
# Some common Base64 alternatives. As referenced by RFC 3458, see thread
@@ -522,6 +524,54 @@ def z85decode(s):
522524
except ValueError as e:
523525
raise ValueError(e.args[0].replace('base85', 'z85')) from None
524526

527+
528+
529+
# Base2 encodings/decodings
530+
531+
# Base2MSBF (Most Significant Bit First): The binary digits are processed starting with the most significant
532+
# bit (the leftmost bit in standard notation). This is the most common format when writing or interpreting
533+
# binary numbers in human-readable form.
534+
def b2msbfencode(s):
535+
"""Encode bytes-like object s in Base2MSBF format and return a bytes object."""
536+
if not isinstance(s, bytes_types):
537+
s = memoryview(s).tobytes()
538+
return ''.join(format(byte, '08b') for byte in s).encode('ascii')
539+
540+
def b2msbfdecode(s):
541+
"""Decode the Base2MSBF encoded bytes-like object or ASCII string s.
542+
543+
The result is returned as a bytes object.
544+
"""
545+
s = _bytes_from_decode_data(s)
546+
if any(c not in '01' for c in s.decode('ascii')):
547+
raise ValueError("Base2MSBF input must be a string of '0' and '1'.")
548+
if len(s) % 8 != 0:
549+
raise ValueError("Base2MSBF input length must be a multiple of 8.")
550+
return bytes(int(s[i:i + 8], 2) for i in range(0, len(s), 8))
551+
552+
# Base2LSBF (Least Significant Bit First): The binary digits are processed starting with the least significant
553+
# bit (the rightmost bit in standard notation). This order is often used in systems where lower-order bits
554+
# are easier to access or modify.
555+
def b2lsbfencode(s):
556+
"""Encode bytes-like object s in Base2LSBF format and return a bytes object."""
557+
if not isinstance(s, bytes_types):
558+
s = memoryview(s).tobytes()
559+
return ''.join(format(byte, '08b')[::-1] for byte in s).encode('ascii') # Reverse each byte's bits
560+
561+
def b2lsbfdecode(s):
562+
"""Decode the Base2LSBF encoded bytes-like object or ASCII string s.
563+
564+
The result is returned as a bytes object.
565+
"""
566+
s = _bytes_from_decode_data(s)
567+
if any(c not in '01' for c in s.decode('ascii')):
568+
raise ValueError("Base2LSBF input must be a string of '0' and '1'.")
569+
if len(s) % 8 != 0:
570+
raise ValueError("Base2LSBF input length must be a multiple of 8.")
571+
return bytes(int(s[i:i + 8][::-1], 2) for i in range(0, len(s), 8))
572+
573+
574+
525575
# Legacy interface. This code could be cleaned up since I don't believe
526576
# binascii has any line length limitations. It just doesn't seem worth it
527577
# though. The files should be opened in binary mode.

0 commit comments

Comments
 (0)