|
| 1 | +# -*- coding: UTF-8 -*- |
| 2 | +"""Base92 Codec - base92 content encoding. |
| 3 | +
|
| 4 | +Base92, designed by Nathan Hwang (thenoviceoof), uses 91 printable ASCII characters ("!", then "#" to "_" and "a" to |
| 5 | + "}", hence all but space, '"', "`" and "~"), the empty string being encoded as "~". The input bits are read by groups |
| 6 | + of 13 bits, each group being written with 2 characters (91 x 91 = 8281 >= 8192), hence an expansion of about 23% ; the |
| 7 | + last bits are padded with zeros and written with a single character if they fit in 6 bits, or with 2 characters |
| 8 | + otherwise. When decoding, a pair of characters whose value exceeds 13 bits is invalid, as well as a single character. |
| 9 | +
|
| 10 | +This codec: |
| 11 | +- en/decodes strings from str to str |
| 12 | +- en/decodes strings from bytes to bytes |
| 13 | +- decodes file content to str (read) |
| 14 | +- encodes file content from str to bytes (write) |
| 15 | +
|
| 16 | +Reference: https://github.com/thenoviceoof/base92 |
| 17 | +""" |
| 18 | +from ._base import main |
| 19 | +from ..__common__ import * |
| 20 | + |
| 21 | + |
| 22 | +__examples__ = { |
| 23 | + 'enc(base92|base-92|base_92)': {'hello world': "Fc_$aOTdKnsM*k", '': "~", 'a': "D,", |
| 24 | + '^\xb6;\xbb\xe0\x1e\xee\xd0\x93\xcb"\xbb\x8fZ\xcd\xc3': "C=i.w6'IvB/viUpRAwco"}, |
| 25 | + 'dec(base92)': {'Fc_$aOTdKnsM*k': "hello world", '~': "", 'Fc_$aOTd\nKnsM*k': "hello world", 'F': None, |
| 26 | + 'Fc"$': None, '}}': None, 'Fc~': None}, |
| 27 | + 'enc-dec(base92)': ["test", "This is a test", "aoeuaoeuaoeu", "@random{512,1024,2048}"], |
| 28 | +} |
| 29 | +__guess__ = ["base92"] |
| 30 | + |
| 31 | + |
| 32 | +_range = lambda c1, c2: "".join(chr(c) for c in range(ord(c1), ord(c2) + 1)) |
| 33 | +B92 = "!" + _range("#", "_") + _range("a", "}") |
| 34 | +B92_VALUES = {c: i for i, c in enumerate(B92)} |
| 35 | + |
| 36 | + |
| 37 | +def base92_encode(text, errors="strict"): |
| 38 | + t = b(text) |
| 39 | + if len(t) == 0: |
| 40 | + return "~", 0 |
| 41 | + r, bits, n = [], 0, 0 |
| 42 | + for byte in t: |
| 43 | + bits, n = (bits << 8) | byte, n + 8 |
| 44 | + while n >= 13: |
| 45 | + n -= 13 |
| 46 | + v, bits = bits >> n, bits & ((1 << n) - 1) |
| 47 | + r.extend((B92[v // 91], B92[v % 91])) |
| 48 | + # the last bits are padded with zeros, to 6 bits (1 character) or to 13 bits (2 characters) |
| 49 | + if n > 0: |
| 50 | + if n < 7: |
| 51 | + r.append(B92[bits << (6 - n)]) |
| 52 | + else: |
| 53 | + v = bits << (13 - n) |
| 54 | + r.extend((B92[v // 91], B92[v % 91])) |
| 55 | + return "".join(r), len(t) |
| 56 | + |
| 57 | + |
| 58 | +def base92_decode(text, errors="strict"): |
| 59 | + t = ensure_str(_stripl(text, True, True)) |
| 60 | + if t == "~": |
| 61 | + return "", len(text) |
| 62 | + _h = handle_error("base92", errors, decode=True) |
| 63 | + # characters out of the alphabet are errors (only skipped if errors are not handled strictly) |
| 64 | + values, r = [], bytearray() |
| 65 | + for i, c in enumerate(t): |
| 66 | + if c in B92_VALUES: |
| 67 | + values.append(B92_VALUES[c]) |
| 68 | + else: |
| 69 | + _h(c, i, bytes(r)) |
| 70 | + if len(values) == 1: |
| 71 | + _h(t, 0, bytes(r)) |
| 72 | + return "", len(text) |
| 73 | + bits, n = 0, 0 |
| 74 | + for i in range(0, len(values) - 1, 2): |
| 75 | + v = values[i] * 91 + values[i+1] |
| 76 | + if v >= 8192: |
| 77 | + _h(t[i:i+2], i, bytes(r)) |
| 78 | + continue |
| 79 | + bits, n = (bits << 13) | v, n + 13 |
| 80 | + while n >= 8: |
| 81 | + n -= 8 |
| 82 | + r.append(bits >> n) |
| 83 | + bits &= (1 << n) - 1 |
| 84 | + # a single last character holds 6 bits ; the padding bits that remain are dropped |
| 85 | + if len(values) % 2 == 1: |
| 86 | + bits, n = (bits << 6) | values[-1], n + 6 |
| 87 | + while n >= 8: |
| 88 | + n -= 8 |
| 89 | + r.append(bits >> n) |
| 90 | + bits &= (1 << n) - 1 |
| 91 | + return bytes(r), len(text) |
| 92 | + |
| 93 | + |
| 94 | +add("base92", base92_encode, base92_decode, r"^base[-_]?92$", entropy=6.5, expansion_factor=1.231, len_charset=91, |
| 95 | + printables_rate=1.) |
| 96 | +main92 = main(92, "<https://github.com/thenoviceoof/base92>", inv=False) |
0 commit comments