2018-07-01 15:27:15 +02:00
|
|
|
const Polynomial = require('./polynomial.js');
|
2018-07-01 13:37:23 +02:00
|
|
|
|
2018-07-01 15:27:15 +02:00
|
|
|
const L = new Polynomial.Class(2, new Array(16).fill(1));
|
|
|
|
const X16 = new Polynomial.Class(2, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); //jako ze można to od razu wymnożyć
|
|
|
|
const G = new Polynomial.Class(2, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]); //jako ze można to od razu wymnożyć
|
2018-07-01 15:14:54 +02:00
|
|
|
|
2018-07-01 13:37:23 +02:00
|
|
|
const to_bin = a => {
|
|
|
|
var result = "";
|
|
|
|
for (var i = 0; i < a.length; i++) {
|
|
|
|
var bin = a[i].charCodeAt().toString(2);
|
|
|
|
result += Array(8 - bin.length + 1).join("0") + bin;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
const to_ascii = a => {
|
|
|
|
a = a.join('');
|
2018-07-01 14:26:14 +02:00
|
|
|
a = parseInt(a, 2);
|
|
|
|
//nie znalazłem innego sposobu w js na osiągniecie tego efektu..
|
2018-07-01 15:27:15 +02:00
|
|
|
// if (a < 126) //ponieważ wieksze liczby nie należą do typowego Ascii//porzucone ze wzgledu na decode
|
2018-07-01 15:14:54 +02:00
|
|
|
return String.fromCharCode(a);
|
|
|
|
// else {
|
|
|
|
// return "0x" + a.toString(16);
|
|
|
|
// // return "\\x" + a.toString(16); //escape \ nie wiem czemu dobrze nie działą i i tak wypisuje \\
|
|
|
|
// }
|
2018-07-01 13:37:23 +02:00
|
|
|
|
|
|
|
|
2018-07-01 14:26:14 +02:00
|
|
|
}
|
2018-07-01 15:27:15 +02:00
|
|
|
const mod8format = array => {
|
|
|
|
while (array.length % 8 != 0) {
|
|
|
|
array.push(0);
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
2018-07-01 13:37:23 +02:00
|
|
|
|
|
|
|
const fcs = m => {
|
2018-07-01 15:14:54 +02:00
|
|
|
let bits = m.map(to_bin); //message in binary
|
2018-07-01 13:37:23 +02:00
|
|
|
bits = bits.join('').split('').reverse(); //reverse binary decoded message
|
|
|
|
let M = new Polynomial.Class(2, bits);
|
|
|
|
let fcs = Polynomial.multiply(X16, M);
|
2018-07-01 15:14:54 +02:00
|
|
|
|
2018-07-01 13:37:23 +02:00
|
|
|
fcs = Polynomial.add(fcs,
|
|
|
|
Polynomial.multiply(
|
2018-07-01 15:27:15 +02:00
|
|
|
Polynomial.Mono(m.length * 8, 1, 2),
|
2018-07-01 13:37:23 +02:00
|
|
|
L
|
|
|
|
)
|
|
|
|
)
|
|
|
|
fcs = Polynomial.divide(fcs, G);
|
|
|
|
for (let i = 0; i < 16 - fcs.coefficients.length; i++) fcs.coefficients.push(0);
|
|
|
|
fcs.coefficients.reverse();
|
|
|
|
return fcs.coefficients;
|
|
|
|
}
|
|
|
|
|
2018-07-01 15:14:54 +02:00
|
|
|
|
2018-07-01 15:27:15 +02:00
|
|
|
|
|
|
|
const check = m => {
|
2018-07-01 15:14:54 +02:00
|
|
|
|
|
|
|
let bits = m.map(to_bin); //message in binary
|
|
|
|
bits = bits.join('').split('').reverse(); //reverse binary decoded message
|
|
|
|
|
2018-07-01 15:27:15 +02:00
|
|
|
let fcs = Polynomial.Mono(bits.length, 1, 2);
|
2018-07-01 15:14:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
let C = new Polynomial.Class(2, bits);
|
|
|
|
C = Polynomial.multiply(X16, C);
|
|
|
|
C.coefficients = mod8format(C.coefficients);
|
|
|
|
let S = Polynomial.add(
|
|
|
|
C,
|
|
|
|
Polynomial.multiply(
|
|
|
|
fcs,
|
|
|
|
L
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
S = Polynomial.divide(S, G)
|
|
|
|
if (S.coefficients.length === 0) {
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-01 13:37:23 +02:00
|
|
|
|
|
|
|
function encode(m) {
|
|
|
|
m = m.split('');
|
|
|
|
let res = fcs(m);
|
|
|
|
|
|
|
|
let f1 = [];
|
|
|
|
let f2 = [];
|
|
|
|
for (let i = 0; i < res.length; i++) {
|
|
|
|
if (i < 8) {
|
|
|
|
f1.push(res[i]);
|
|
|
|
} else {
|
|
|
|
f2.push(res[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f1 = to_ascii(f1);
|
|
|
|
f2 = to_ascii(f2);
|
|
|
|
m.push(f1);
|
|
|
|
m.push(f2);
|
2018-07-01 15:14:54 +02:00
|
|
|
return {
|
|
|
|
encoded: m,
|
|
|
|
FCS: [f1, f2]
|
|
|
|
|
|
|
|
};
|
2018-07-01 13:37:23 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-01 14:26:14 +02:00
|
|
|
exports.encode = encode;
|
|
|
|
|
2018-07-01 15:14:54 +02:00
|
|
|
function decode(m, fcs) {
|
|
|
|
m = m.split('');
|
|
|
|
to_check = fcs;
|
|
|
|
for (let char in fcs) {
|
|
|
|
m.push(fcs[char]);
|
|
|
|
}
|
|
|
|
return check(m)
|
2018-07-01 14:26:14 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
exports.decode = decode;
|