|
| 1 | +# -*- coding: UTF-8 -*- |
| 2 | +"""Polybius Square Codec - polybius-square content encoding. |
| 3 | +
|
| 4 | +This codec: |
| 5 | +- en/decodes strings from str to str |
| 6 | +- en/decodes strings from bytes to bytes |
| 7 | +- decodes file content to str (read) |
| 8 | +- encodes file content from str to bytes (write) |
| 9 | +""" |
| 10 | +from ..__common__ import * |
| 11 | + |
| 12 | + |
| 13 | +__examples__ = { |
| 14 | + 'enc(polybius|polybius-square|polybius_square)': {'this is a test': "44232443 2443 11 44154344"}, |
| 15 | + 'enc(polybius-ABCDEFGHIKLMNOPQRSTUVWXYZ)': {'this is a test': "44232443 2443 11 44154344"}, |
| 16 | + 'dec(polybius)': {'44232443 2443 11 44154344': "THIS IS A TEST"}, |
| 17 | +} |
| 18 | +__guess__ = ["polybius"] |
| 19 | + |
| 20 | + |
| 21 | +# Standard 5×5 Polybius square (I and J share the same cell): |
| 22 | +# 1 2 3 4 5 |
| 23 | +# 1 A B C D E |
| 24 | +# 2 F G H I K |
| 25 | +# 3 L M N O P |
| 26 | +# 4 Q R S T U |
| 27 | +# 5 V W X Y Z |
| 28 | +_DEFAULT_ALPHABET = "ABCDEFGHIKLMNOPQRSTUVWXYZ" |
| 29 | + |
| 30 | + |
| 31 | +def __make_maps(alphabet): |
| 32 | + """ Build the encoding and decoding maps for the given 25-character alphabet. """ |
| 33 | + alph = alphabet.upper() if alphabet else _DEFAULT_ALPHABET |
| 34 | + if len(alph) != 25 or len(set(alph)) != 25: |
| 35 | + raise LookupError("Polybius square requires exactly 25 distinct characters; " |
| 36 | + f"got {len(alph)} character(s) with {len(set(alph))} unique: {alph}") |
| 37 | + encmap = {alph[i]: str(i // 5 + 1) + str(i % 5 + 1) for i in range(25)} |
| 38 | + decmap = {v: k for k, v in encmap.items()} |
| 39 | + if 'J' not in encmap and 'I' in encmap: |
| 40 | + encmap['J'] = encmap['I'] |
| 41 | + encmap[' '] = ' ' |
| 42 | + return encmap, decmap |
| 43 | + |
| 44 | + |
| 45 | +def polybius_encode(alphabet=_DEFAULT_ALPHABET): |
| 46 | + encmap, _ = __make_maps(alphabet) |
| 47 | + def encode(text, errors="strict"): |
| 48 | + _h = handle_error("polybius", errors) |
| 49 | + r = "" |
| 50 | + for pos, c in enumerate(ensure_str(text).upper()): |
| 51 | + r += encmap[c] if c in encmap else _h(c, pos, r) |
| 52 | + return r, len(text) |
| 53 | + return encode |
| 54 | + |
| 55 | + |
| 56 | +def polybius_decode(alphabet=_DEFAULT_ALPHABET): |
| 57 | + _, decmap = __make_maps(alphabet) |
| 58 | + def decode(text, errors="strict"): |
| 59 | + _h = handle_error("polybius", errors, decode=True) |
| 60 | + r, t, i = "", ensure_str(text), 0 |
| 61 | + while i < len(t): |
| 62 | + if t[i] == " ": |
| 63 | + r += " " |
| 64 | + i += 1 |
| 65 | + elif i + 1 < len(t): |
| 66 | + r += decmap.get(t[i:i+2]) or _h(t[i:i+2], i, r) |
| 67 | + i += 2 |
| 68 | + else: |
| 69 | + r += _h(t[i], i, r) |
| 70 | + i += 1 |
| 71 | + return r, len(t) |
| 72 | + return decode |
| 73 | + |
| 74 | + |
| 75 | +add("polybius", polybius_encode, polybius_decode, r"^polybius(?:[-_]square)?(?:[-_]([A-Za-z]{25}))?$", |
| 76 | + printables_rate=1., expansion_factor=(1.7, .3)) |
| 77 | + |
0 commit comments