forked from dhondta/python-codext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase100.py
More file actions
47 lines (36 loc) · 1.46 KB
/
Copy pathbase100.py
File metadata and controls
47 lines (36 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: UTF-8 -*-
"""Base100 Codec - base100 content encoding.
Note: only works in Python3 ; strongly inspired from https://github.com/MasterGroosha/pybase100
This codec:
- en/decodes strings from str to str
- en/decodes strings from bytes to bytes
- decodes file content to str (read)
- encodes file content from str to bytes (write)
"""
from ._base import main
from ..__common__ import *
# no __examples__ ; handled manually in tests/test_base.py
class Base100DecodeError(ValueError):
__module__ = "builtins"
def base100_encode(input, errors="strict"):
input = b(input)
r = [240, 159, 0, 0] * len(input)
for i, c in enumerate(input):
r[4*i+2] = (c + 55) // 64 + 143
r[4*i+3] = (c + 55) % 64 + 128
return bytes(r), len(input)
def base100_decode(input, errors="strict"):
input = b(_stripl(input, True, True))
if errors == "ignore":
input = input.replace(b"\n", b"")
if len(input) % 4 != 0:
raise Base100DecodeError("Bad input (length should be multiple of 4)")
r = [None] * (len(input) // 4)
for i, c in enumerate(input):
if i % 4 == 2:
tmp = ((c - 143) * 64) % 256
elif i % 4 == 3:
r[i//4] = (c - 128 + tmp - 55) & 0xff
return bytes(r), len(input)
add("base100", base100_encode, base100_decode, r"^(?:base[-_]?100|emoji)$", expansion_factor=1.)
main100 = main(100, "<https://github.com/AdamNiederer/base100>")