Skip to content

Instantly share code, notes, and snippets.

@mnixry
Created August 4, 2021 06:35
Show Gist options
  • Save mnixry/5071efae349dd616ec0a468268e8378b to your computer and use it in GitHub Desktop.
Save mnixry/5071efae349dd616ec0a468268e8378b to your computer and use it in GitHub Desktop.
Not-See encoder (encode any character to blank string)
/*
Blank character encoder/decoder
A tool to anti-censorship at Bilibili
Original author: https://not-see.pages.dev/
*/
const ALLOWED_BLANKS = [
"\u0020",
"\u3000",
"\u205F",
"\u2000",
"\u2001",
"\u2002",
"\u2003",
"\u2004",
"\u2005",
"\u2006",
"\u2007",
"\u2008",
"\u2009",
"\u200A",
"\u202F",
"\n",
];
const BLANKS_MAP = Object.fromEntries(
ALLOWED_BLANKS.map((v, i) => [v.charCodeAt(0), i])
);
/**
* @param {ArrayLike<number>} target
* @returns {String}
*/
function encode(target) {
return Array.from(target)
.map((v) => ALLOWED_BLANKS[v >> 4] + ALLOWED_BLANKS[v & 0xf])
.join("");
}
/**
* @param {String} target
* @returns {Uint8Array}
*/
function decode(target) {
return new Uint8Array(
Array.from(
{ length: target.length >> 1 },
(_, k) =>
(BLANKS_MAP[target.charCodeAt(k * 2)] << 4) +
BLANKS_MAP[target.charCodeAt(k * 2 + 1)]
)
);
}
"""
Blank character encoder/decoder
A tool to anti-censorship at Bilibili
Credits:
Original author: https://not-see.pages.dev/
Transplanted to Python by: mnixry@GitHub
"""
class NotSeeUtils:
ALLOWED_BLANKS = [
"\u0020",
"\u3000",
"\u205F",
"\u2000",
"\u2001",
"\u2002",
"\u2003",
"\u2004",
"\u2005",
"\u2006",
"\u2007",
"\u2008",
"\u2009",
"\u200A",
"\u202F",
"\n",
]
_BLANKS_MAP = {ord(s): i for i, s in enumerate(ALLOWED_BLANKS)}
@classmethod
def encode(cls, target: bytes) -> str:
"""
Original code:
```javascript
const encoded = Array.from(new TextEncoder().encode(src))
.map((v) => `${spaces[v >> 4]}${spaces[v & 0xf]}`)
.join("");
```
"""
return "".join(
map(
lambda v: (cls.ALLOWED_BLANKS[v >> 4] + cls.ALLOWED_BLANKS[v & 0xF]),
target,
)
)
@classmethod
def decode(cls, target: str) -> bytes:
"""
Original code:
```javascript
const decoded = new TextDecoder().decode(
new Uint8Array(
Array.from({ length: src.length >> 1 }, (_, k) => {
return (
(reverted[src.charCodeAt(k * 2)] << 4) +
reverted[src.charCodeAt(k * 2 + 1)]
);
})
)
);
```
"""
return bytes(
(
(cls._BLANKS_MAP[ord(target[i * 2])] << 4)
+ cls._BLANKS_MAP[ord(target[i * 2 + 1])]
)
for i in range(len(target) >> 1)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment