Skip to content

Commit 5182fcc

Browse files
committed
Added console scripts for multiple base encodings (2)
1 parent e5055d4 commit 5182fcc

6 files changed

Lines changed: 57 additions & 26 deletions

File tree

codext/base/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def main():
5252
c = _input(args.file)
5353
c = c.rstrip("\r\n") if isinstance(c, str) else c.rstrip(b"\r\n")
5454
r = codecs.guess(c, sfunc, args.min_depth, args.max_depth, exclude=excl, codec_categories="base",
55-
stop=not args.do_not_stop, show=True, scoring_heuristic=False)
55+
stop=not args.do_not_stop, show=True, scoring_heuristic=False, debug=args.verbose)
5656
if not args.do_not_stop:
57-
print(ensure_str(list(r.items())[0][1]))
57+
print("Could not decode :-(" if len(r) == 0 else ensure_str(list(r.items())[0][1]))
5858

codext/base/base100.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
# no __examples__ ; handled manually in tests/test_base.py
1717

1818

19+
def base100_encode(input, errors='strict'):
20+
raise NotImplementedError
21+
22+
23+
def base100_decode(input, errors='strict'):
24+
raise NotImplementedError
25+
26+
1927
if PY3:
2028
class Base100DecodeError(ValueError):
2129
pass
@@ -41,12 +49,6 @@ def base100_decode(input, errors="strict"):
4149
elif i % 4 == 3:
4250
r[i//4] = (c - 128 + tmp - 55) & 0xff
4351
return bytes(r), len(input)
44-
else:
45-
def base100_encode(input, errors='strict'):
46-
raise NotImplementedError
47-
48-
def base100_decode(input, errors='strict'):
49-
raise NotImplementedError
5052

5153

5254
add("base100", base100_encode, base100_decode, r"^(?:base[-_]?100|emoji)$")

codext/base/base122.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
_i = lambda c: c if isinstance(c, int) else ord(c)
2727

2828

29+
def base122_encode(input, errors='strict'):
30+
raise NotImplementedError
31+
32+
33+
def base122_decode(input, errors='strict'):
34+
raise NotImplementedError
35+
36+
2937
if PY3:
3038
# inspired from: https://github.com/kevinAlbs/Base122/blob/master/base122.js
3139
def base122_encode(input, errors="strict"):
@@ -91,12 +99,6 @@ def _get_7bits(currB, bob, B, decoded):
9199
else:
92100
currB, bob = _get_7bits(currB, bob, input[i], r)
93101
return "".join(map(chr, r)), len(input)
94-
else:
95-
def base122_encode(input, errors='strict'):
96-
raise NotImplementedError
97-
98-
def base122_decode(input, errors='strict'):
99-
raise NotImplementedError
100102

101103

102104
add("base122", base122_encode, base122_decode, r"^base[-_]?122$")

codext/base/base85.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@
2828
#base(B85, r"^base[-_]?85(|[-_](?:z(?:eromq)?|rfc1924))$")
2929

3030

31+
def base85_encode(input, errors='strict'):
32+
raise NotImplementedError
33+
34+
35+
def base85_decode(input, errors='strict'):
36+
raise NotImplementedError
37+
38+
3139
if PY3:
3240
def base85_encode(input, errors='strict'):
3341
return base64.b85encode(b(input)), len(input)
3442

3543
def base85_decode(input, errors='strict'):
3644
return base64.b85decode(b(input)), len(input)
37-
else:
38-
def base85_encode(input, errors='strict'):
39-
raise NotImplementedError
40-
41-
def base85_decode(input, errors='strict'):
42-
raise NotImplementedError
4345

4446

4547
add("base85", base85_encode, base85_decode, r"^base[-_]?85$", entropy=7.05)

codext/base/baseN.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,22 @@
1414

1515
B2 = {r'': "01", r'[-_]inv(erted)?': "10"}
1616
base2n(B2, r"^(?:base[-_]?2|bin)(|[-_]inv(?:erted)?|[-_][a-zA-Z0-9]{2})$")
17+
main2 = main(2)
1718

1819

1920
B3 = {r'': "123", r'[-_]inv(erted)?': "321"}
2021
base(B3, r"^base[-_]?3(|[-_]inv(?:erted)?|[-_][a-zA-Z0-9]{3})$")
22+
main3 = main(3)
2123

2224

2325
B4 = {r'': "1234", r'[-_]inv(erted)?': "4321"}
2426
base2n(B4, r"^base[-_]?4(|[-_]inv(?:erted)?|[-_][a-zA-Z0-9]{4})$")
27+
main4 = main(4)
2528

2629

2730
B8 = {r'': "abcdefgh", r'[-_]inv(erted)?': "hgfedcba"}
2831
base2n(B8, r"^base[-_]?8(|[-_]inv(?:erted)?|[-_][a-zA-Z0-9]{8})$")
32+
main8 = main(8)
2933

3034

3135
B16 = {'': digits + "ABCDEF", 'inv': "ABCDEF" + digits}
@@ -39,9 +43,16 @@
3943
r'(?:[-_]ext(?:ended)?)?[-_]hex$': digits + upper[:22],
4044
r'[-_]geohash': digits + "bcdefghjkmnpqrstuvwxyz",
4145
}
42-
base2n(B32, r"^base[-_]?32(|[-_]inv(?:erted)?|(?:[-_]ext(?:ended)?)?[-_]hex|[-_]geohash)$", padding_char="=")
46+
base2n(B32, r"^base[-_]?32(|[-_]inv(?:erted)?|(?:[-_]ext(?:ended)?)?[-_]hex|[-_]geohash)$", padding_char="=",
47+
guess=["base32", "base32-inv", "base32-ext", "base32-geohash"])
48+
main32 = main(32, "RFC 4648")
49+
main32ext = main(32, "RFC 4648", "extended", False)
50+
main32geo = main(32, "RFC 4648", "geohash", False)
51+
52+
4353
ZB32 = {'': "ybndrfg8ejkmcpqxot1uwisza345h769"}
4454
base2n(ZB32, r"^z[-_]?base[-_]?32$", name="zbase32", padding_char="=")
55+
mainz32 = main(32, "<https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt>", "z", False)
4556

4657

4758
B36 = {'': digits + upper, 'inv': upper + digits}
@@ -54,10 +65,11 @@
5465
r'[-_](rp|ripple)$': "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz",
5566
r'[-_](fl|flickr|short[-]?url|url)$': "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ",
5667
}
57-
base(B58, r"^base[-_]?58(|[-_](bc|bitcoin|rp|ripple|fl|flickr|short[-]?url|url))$")
58-
main58bc = main(58, "<https://en.bitcoinwiki.org/wiki/Base58>", "-bitcoin")
59-
main58rp = main(58, "<https://en.bitcoinwiki.org/wiki/Base58>", "-ripple")
60-
main58fl = main(58, "<https://en.bitcoinwiki.org/wiki/Base58>", "-flickr")
68+
base(B58, r"^base[-_]?58(|[-_](bc|bitcoin|rp|ripple|fl|flickr|short[-]?url|url))$",
69+
guess=["base58-bitcoin", "base58-ripple", "base58-flickr"])
70+
main58bc = main(58, "<https://en.bitcoinwiki.org/wiki/Base58>", "bitcoin")
71+
main58rp = main(58, "<https://en.bitcoinwiki.org/wiki/Base58>", "ripple")
72+
main58fl = main(58, "<https://en.bitcoinwiki.org/wiki/Base58>", "flickr")
6173

6274

6375
B62 = {'': digits + upper + lower, 'inv': digits + lower + upper}
@@ -75,7 +87,10 @@
7587
r'[-_]inv(erted)?$': lower + upper + digits + "+/",
7688
r'[-_]?(file|url)(safe)?$': upper + lower + digits + "-_",
7789
}
78-
base2n(B64, r"^base[-_]?64(|[-_]inv(?:erted)?|[-_]?(?:file|url)(?:safe)?)$", padding_char="=")
90+
base2n(B64, r"^base[-_]?64(|[-_]inv(?:erted)?|[-_]?(?:file|url)(?:safe)?)$", padding_char="=",
91+
guess=["base64", "base64-inv", "base64-url"])
92+
main64 = main(64, "RFC 4648")
93+
main64url = main(64, "RFC 4648 / Base64URL", "url", False)
7994

8095

8196
B67 = {

setup.cfg

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,24 @@ python-requires = >=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,<4
4444
4545
[options.entry_points]
4646
console_scripts =
47+
base2 = codext.base.baseN:main2
48+
base3 = codext.base.baseN:main3
49+
base4 = codext.base.baseN:main4
50+
base8 = codext.base.baseN:main8
4751
base16 = codext.base.baseN:main16
52+
base32 = codext.base.baseN:main32
53+
base32-extended = codext.base.baseN:main32ext
54+
base32-geohash = codext.base.baseN:main32geo
55+
base32-z = codext.base.baseN:mainz32
4856
base36 = codext.base.baseN:main36
4957
base45 = codext.base.base45:main
5058
base58-bitcoin = codext.base.baseN:main58bc
5159
base58-ripple = codext.base.baseN:main58rp
5260
base58-flickr = codext.base.baseN:main58fl
5361
base62 = codext.base.baseN:main62
5462
base63 = codext.base.baseN:main63
63+
base64 = codext.base.baseN:main64
64+
base64-url = codext.base.baseN:main64url
5565
base67 = codext.base.baseN:main67
5666
base85 = codext.base.base85:main
5767
base91 = codext.base.base91:main

0 commit comments

Comments
 (0)