From 68c43bfeb390b7f32082b0f32c9d61adae3c538e Mon Sep 17 00:00:00 2001 From: Kacper Kruczek Date: Sun, 1 Jul 2018 15:14:54 +0200 Subject: [PATCH] 03 decode and encoded returns message and FCS --- 03-rozwiazanie.js | 10 +++++-- crc.js | 74 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 65 insertions(+), 19 deletions(-) diff --git a/03-rozwiazanie.js b/03-rozwiazanie.js index 7532bb6..be5bc10 100644 --- a/03-rozwiazanie.js +++ b/03-rozwiazanie.js @@ -3,10 +3,16 @@ let message = process.argv[3]; let flag = process.argv[2]; switch (flag) { case '-e': - console.log(crc.encode(message)); + console.log(crc.encode(message).encoded); break; 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; default: throw "incorect flag" diff --git a/crc.js b/crc.js index d9ade17..d5f2180 100644 --- a/crc.js +++ b/crc.js @@ -1,7 +1,10 @@ let Polynomial = require('./polynomial.js'); -const to_bytes_ascii = a => a.charCodeAt(0); -const from_bytes_ascii = a => String.fromCharCode(a); +let L = new Polynomial.Class(2, new Array(16).fill(1)); +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 => { var result = ""; for (var i = 0; i < a.length; i++) { @@ -13,28 +16,23 @@ const to_bin = a => { const to_ascii = a => { a = a.join(''); a = parseInt(a, 2); - console.log(a) //nie znalazłem innego sposobu w js na osiągniecie tego efektu.. - if (a < 126) //ponieważ wieksze liczby nie należą do typowego Ascii - 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 \\ - } + // if (a < 126) //ponieważ wieksze liczby nie należą do typowego Ascii + 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 \\ + // } } 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 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); + fcs = Polynomial.add(fcs, Polynomial.multiply( Polynomial.power(X, m.length * 8), @@ -47,6 +45,38 @@ const fcs = m => { 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) { m = m.split(''); @@ -65,13 +95,23 @@ function encode(m) { f2 = to_ascii(f2); m.push(f1); m.push(f2); - return m; + return { + encoded: m, + FCS: [f1, f2] + + }; } 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; \ No newline at end of file