|
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""" |
2 | 2 |
|
3 | 3 | # Modified 04-Oct-1995 by Jack Jansen to use binascii module |
4 | 4 | # Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support |
|
17 | 17 | 'b32hexencode', 'b32hexdecode', 'b16encode', 'b16decode', |
18 | 18 | # Base85 and Ascii85 encodings |
19 | 19 | 'b85encode', 'b85decode', 'a85encode', 'a85decode', 'z85encode', 'z85decode', |
| 20 | + # Base2MSBF and Base2LSBF encodings |
| 21 | + 'b2msbfencode', 'b2msbfdecode', 'b2lsbfencode', 'b2lsbfdecode', |
20 | 22 | # Standard Base64 encoding |
21 | 23 | 'standard_b64encode', 'standard_b64decode', |
22 | 24 | # Some common Base64 alternatives. As referenced by RFC 3458, see thread |
@@ -522,6 +524,54 @@ def z85decode(s): |
522 | 524 | except ValueError as e: |
523 | 525 | raise ValueError(e.args[0].replace('base85', 'z85')) from None |
524 | 526 |
|
| 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 | + |
525 | 575 | # Legacy interface. This code could be cleaned up since I don't believe |
526 | 576 | # binascii has any line length limitations. It just doesn't seem worth it |
527 | 577 | # though. The files should be opened in binary mode. |
|
0 commit comments