03 decode and encoded returns message and FCS

This commit is contained in:
Kacper Kruczek 2018-07-01 15:14:54 +02:00
parent e8f67f6e9e
commit 68c43bfeb3
2 changed files with 65 additions and 19 deletions

View File

@ -3,10 +3,16 @@ let message = process.argv[3];
let flag = process.argv[2]; let flag = process.argv[2];
switch (flag) { switch (flag) {
case '-e': case '-e':
console.log(crc.encode(message)); console.log(crc.encode(message).encoded);
break; break;
case '-d': case '-d':
console.log(crc.decode(message)); let fcs = JSON.parse(process.argv[4].replace(/'/g, '"'));
console.log(crc.decode(message, fcs));
break;
case '-t':
break; break;
default: default:
throw "incorect flag" throw "incorect flag"

72
crc.js
View File

@ -1,7 +1,10 @@
let Polynomial = require('./polynomial.js'); let Polynomial = require('./polynomial.js');
const to_bytes_ascii = a => a.charCodeAt(0); let L = new Polynomial.Class(2, new Array(16).fill(1));
const from_bytes_ascii = a => String.fromCharCode(a); let X = new Polynomial.Class(2, [0, 1]);
let 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ć
let 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ć
const to_bin = a => { const to_bin = a => {
var result = ""; var result = "";
for (var i = 0; i < a.length; i++) { for (var i = 0; i < a.length; i++) {
@ -13,28 +16,23 @@ const to_bin = a => {
const to_ascii = a => { const to_ascii = a => {
a = a.join(''); a = a.join('');
a = parseInt(a, 2); a = parseInt(a, 2);
console.log(a)
//nie znalazłem innego sposobu w js na osiągniecie tego efektu.. //nie znalazłem innego sposobu w js na osiągniecie tego efektu..
if (a < 126) //ponieważ wieksze liczby nie należą do typowego Ascii // if (a < 126) //ponieważ wieksze liczby nie należą do typowego Ascii
return String.fromCharCode(a); return String.fromCharCode(a);
else { // else {
return "0x" + a.toString(16); // return "0x" + a.toString(16);
// return "\\x" + a.toString(16); //escape \ nie wiem czemu dobrze nie działą i i tak wypisuje \\ // // return "\\x" + a.toString(16); //escape \ nie wiem czemu dobrze nie działą i i tak wypisuje \\
} // }
} }
const fcs = m => { const fcs = m => {
bits = m.map(to_bin); //message in binary let bits = m.map(to_bin); //message in binary
bits = bits.join('').split('').reverse(); //reverse binary decoded message bits = bits.join('').split('').reverse(); //reverse binary decoded message
let M = new Polynomial.Class(2, bits); let M = new Polynomial.Class(2, bits);
let L = new Polynomial.Class(2, new Array(16).fill(1));
let X = new Polynomial.Class(2, [0, 1]);
let X16 = Polynomial.power(X, 16);
let 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ć
let fcs = Polynomial.multiply(X16, M); let fcs = Polynomial.multiply(X16, M);
fcs = Polynomial.add(fcs, fcs = Polynomial.add(fcs,
Polynomial.multiply( Polynomial.multiply(
Polynomial.power(X, m.length * 8), Polynomial.power(X, m.length * 8),
@ -47,6 +45,38 @@ const fcs = m => {
return fcs.coefficients; return fcs.coefficients;
} }
function mod8format(lst) {
while (lst.length % 8 != 0) {
lst.push(0);
}
return lst;
}
function check(m) {
let bits = m.map(to_bin); //message in binary
bits = bits.join('').split('').reverse(); //reverse binary decoded message
let fcs = Polynomial.power(X, bits.length);
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;
}
function encode(m) { function encode(m) {
m = m.split(''); m = m.split('');
@ -65,13 +95,23 @@ function encode(m) {
f2 = to_ascii(f2); f2 = to_ascii(f2);
m.push(f1); m.push(f1);
m.push(f2); m.push(f2);
return m; return {
encoded: m,
FCS: [f1, f2]
};
} }
exports.encode = encode; exports.encode = encode;
function decode(m) { function decode(m, fcs) {
m = m.split('');
to_check = fcs;
for (let char in fcs) {
m.push(fcs[char]);
}
return check(m)
} }
exports.decode = decode; exports.decode = decode;