-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbubble_babble.js
131 lines (104 loc) · 3.08 KB
/
bubble_babble.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var vowels = 'aeiouy';
var consonants = 'bcdfghklmnprstvzx';
var encode = function(input, encoding) {
if (!Buffer.isBuffer(input)) {
input = new Buffer(input, encoding);
}
var result = 'x',
checksum = 1,
len = input.length,
byte1, byte2,
d, e, i;
// create full tuples
for (i = 0; i + 1 < len; i += 2) {
byte1 = input.readUInt8(i);
result += odd_partial(byte1, checksum);
byte2 = input.readUInt8(i + 1);
d = (byte2 >> 4) & 15;
e = byte2 & 15;
result += consonants.charAt(d) + '-' + consonants.charAt(e);
checksum = next_checksum(checksum, byte1, byte2);
}
// handle partial tuple
if (i < len) {
byte1 = input.readUInt8(i);
result += odd_partial(byte1, checksum);
} else {
result += even_partial(checksum);
}
result += 'x';
return result;
};
var odd_partial = function(raw_byte, checksum) {
var a = (((raw_byte >> 6) & 3) + checksum) % 6,
b = (raw_byte >> 2) & 15,
c = ((raw_byte & 3) + Math.floor(checksum / 6)) % 6;
return vowels.charAt(a) + consonants.charAt(b) + vowels.charAt(c);
}
var even_partial = function(checksum) {
var a = checksum % 6,
b = 16,
c = Math.floor(checksum / 6);
return vowels.charAt(a) + consonants.charAt(b) + vowels.charAt(c);
};
var decode = function(input) {
if (input.substr(0, 1) !== 'x' ||
input.substr(-1, 1) !== 'x') {
throw new Error('Corrupt string');
}
var ascii_tuples = input.substring(1, input.length - 1).match(/.{3,6}/g),
len = ascii_tuples ? ascii_tuples.length : 0,
char_codes = [],
checksum = 1,
byte1, byte2, i,
tuple;
// handle full tuples
for (i = 0; i < len - 1; ++i) {
tuple = decode_tuple(ascii_tuples[i]);
byte1 = decode_3part_byte(tuple[0], tuple[1], tuple[2], checksum);
byte2 = decode_2part_byte(tuple[3], tuple[4]);
checksum = next_checksum(checksum, byte1, byte2);
char_codes.push(byte1);
char_codes.push(byte2);
}
// handle partial tuple
tuple = decode_tuple(ascii_tuples[len - 1]);
if (tuple[1] === 16) {
if (tuple[0] !== checksum % 6 ||
tuple[2] !== Math.floor(checksum / 6)) {
throw new Error('Corrupt string');
}
} else {
byte1 = decode_3part_byte(tuple[0], tuple[1], tuple[2], checksum);
char_codes.push(byte1);
}
return new Buffer(char_codes);
};
var decode_tuple = function(ascii_tuple) {
return [
vowels.indexOf(ascii_tuple[0]),
consonants.indexOf(ascii_tuple[1]),
vowels.indexOf(ascii_tuple[2]),
consonants.indexOf(ascii_tuple[3]),
consonants.indexOf(ascii_tuple[5]),
];
};
var decode_3part_byte = function(a, b, c, checksum) {
var high = (a - (checksum % 6) + 6) % 6,
mid = b,
low = (c - (Math.floor(checksum / 6) % 6) + 6) % 6;
if (high >= 4 || low >= 4) {
throw new Error('Corrupt string');
}
return (high << 6) | (mid << 2) | low;
};
var decode_2part_byte = function(d, e) {
return (d << 4) | e;
}
var next_checksum = function(checksum, byte1, byte2) {
return ((checksum * 5) + (byte1 * 7) + byte2) % 36;
}
module.exports = {
encode: encode,
decode: decode
};