Created
June 2, 2016 12:22
-
-
Save swordfeng/4e1df045ff8b5e06e8bc24fb5522d6c1 to your computer and use it in GitHub Desktop.
A 'Chinese Specific Socialism Core Value' (or somehow a name like that) -based binary encoder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var zlib = require('zlib'); | |
var values = ['富强', '民主', '文明', '和谐', '自由', '平等', '公正', '法治', '爱国', '敬业', '诚信', '友善']; | |
var nums = {}; | |
for (var i = 0; i < 12; i++) nums[values[i]] = i; | |
var primes = [2, 3, 5, 7, 11, 13, 17, 19]; | |
function encode(str) { | |
var buf = Buffer(str, 'utf8'); | |
if (buf.length === 0) return ''; | |
var results = []; | |
var dice = 7; | |
for (var i = 0; i < buf.length; i++) { | |
var v0 = buf[i]; | |
var v = v0; | |
results.push(values[(v + dice) % 12]); | |
dice = (dice + 7) % 12; | |
v = parseInt(v / 12); | |
results.push(values[(v + dice) % 12]); | |
dice = (dice + 7) % 12; | |
v = parseInt(v / 12); | |
var t = 0; | |
for (var j = 0; j < 8; j++) { | |
if (v0 & 1) t += primes[j]; | |
v0 >>>= 1; | |
} | |
t %= 6; | |
v += t * 2; | |
results.push(values[(v + dice) % 12]); | |
dice = (dice + 7) % 12; | |
} | |
var r = results[0]; | |
for (var i = 1; i < results.length; i++) { | |
if (i % 4 === 0) r += '\n'; | |
else r += ' '; | |
r += results[i]; | |
} | |
return r; | |
} | |
function decode(str) { | |
var ts = ''; | |
var results = []; | |
for (var i = 0; i < str.length; i++) { | |
if (str.charCodeAt(i) < 256) continue; | |
ts += str[i]; | |
if (ts.length === 2) { | |
if (nums[ts] === undefined) throw new TypeError('Invalid encoded data!'); | |
results.push(nums[ts]); | |
ts = ''; | |
} | |
} | |
if (ts !== '') throw new TypeError('Invalid encoded data!'); | |
if (results.length % 3 !== 0) throw new TypeError('Invalid encoded data!'); | |
var buf = Buffer(results.length / 3); | |
var dice = 5; | |
var v = 0; | |
for (var i = 0; i < buf.length; i++) { | |
var n1 = (results[i * 3] + dice) % 12; | |
dice = (dice + 5) % 12; | |
var n2 = (results[i * 3 + 1] + dice) % 12; | |
dice = (dice + 5) % 12; | |
var n3 = (results[i * 3 + 2] + dice) % 12; | |
dice = (dice + 5) % 12; | |
var v = n1 + n2 * 12 + (n3 & 1) * 144; | |
var v0 = v; | |
var t = 0; | |
for (var j = 0; j < 8; j++) { | |
if (v0 & 1) t += primes[j]; | |
v0 >>>= 1; | |
} | |
t %= 6; | |
if (t !== n3 >>> 1) throw new TypeError('Invalid encoded data!'); | |
buf[i] = v; | |
} | |
return buf.toString('utf8'); | |
} | |
var decoding = false; | |
for (var p of process.argv) if (p === '-d') decoding = true; | |
var s = Buffer(0); | |
process.stdin.on('data', function (buf) { | |
s = Buffer.concat([s, buf]); | |
}); | |
process.stdin.on('end', function () { | |
if (decoding) { | |
console.log(decode(s.toString('utf8'))); | |
} else { | |
console.log(encode(s.toString('utf8'))); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment