From 84c7c81153749bd03864cac7947af6447bc770cd Mon Sep 17 00:00:00 2001 From: Hubert Tylkowski Date: Wed, 20 Jun 2018 00:30:21 +0200 Subject: [PATCH] encode added! --- Zadanie-03/src/com/tylkowski/crc/CrcTask.java | 155 ++++++++++++++++-- Zadanie-03/src/com/tylkowski/crc/Main.java | 3 +- 2 files changed, 143 insertions(+), 15 deletions(-) diff --git a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java index cf67b17..57d2e59 100644 --- a/Zadanie-03/src/com/tylkowski/crc/CrcTask.java +++ b/Zadanie-03/src/com/tylkowski/crc/CrcTask.java @@ -1,20 +1,125 @@ package com.tylkowski.crc; -public class CrcTask { +import java.util.Arrays; + +class CrcTask { private String message; private short[] messageAsShortArray; private short[] polyGenerator; + private String rawMessage; - public CrcTask(String message) { - this.message = message + "0000000000000000"; - convertMessageToBinaryShortArray(); - createGeneratingPoly(); - swapPolyValues(); + CrcTask(String message, String rawMessage) { + this.message = formatMessage(message) + "0000000000000000"; + this.rawMessage = rawMessage; + createGeneratingPolynomial(); + } + + private String formatMessage(String message) { + int firstNonZeroVal = 0; + boolean found = false; + StringBuilder validString = new StringBuilder(); + while (!found && firstNonZeroVal < message.length()) { + if (message.charAt(firstNonZeroVal) == 48) { + firstNonZeroVal++; + } else { + found = true; + } + } + + for (int i = firstNonZeroVal; i < message.length(); i++) { + if (message.charAt(i) == 48) { + validString.append(0); + } else { + validString.append(1); + } + } + StringBuilder msg = new StringBuilder(validString.toString()); + while (msg.length() % 8 != 0) { + msg.insert(0, "0"); + } + + return msg.toString(); + } + + private String generateFCS() { + while (true) { + if (messageAsShortArray[0] == 0) { + messageAsShortArray = Arrays.copyOfRange(messageAsShortArray, 1, messageAsShortArray.length); + } else { + short[] piece = Arrays.copyOfRange(messageAsShortArray, 0, Math.min(polyGenerator.length, messageAsShortArray.length)); + if (piece.length < polyGenerator.length) { + fillPolynomial(piece); + return "" + getCharFromShortArray(piece, 0, 8) + getCharFromShortArray(piece, piece.length - 8, piece.length); + } + short[] remainder = calcXOR(piece, polyGenerator); + remainder = removeUnecessaryZeros(remainder); + createMessageFromRemainderAndPartFromOldMessage(remainder, remainder.length); + } + + + } + + } + + private void createMessageFromRemainderAndPartFromOldMessage(short[] remainder, int length) { + short[] tempArr = new short[remainder.length + messageAsShortArray.length - polyGenerator.length]; + System.arraycopy(remainder, 0, tempArr, 0, length); + int diff = polyGenerator.length - remainder.length; + System.arraycopy(messageAsShortArray, length + diff, tempArr, length, messageAsShortArray.length - diff - length); + messageAsShortArray = tempArr; + } + + private short[] removeUnecessaryZeros(short[] remainder) { + int firstNonZeroVal = 0; + boolean found = false; + while (!found && firstNonZeroVal < remainder.length) { + if (remainder[firstNonZeroVal] == 0) { + firstNonZeroVal++; + } else { + found = true; + } + } + return Arrays.copyOfRange(remainder, firstNonZeroVal, remainder.length); + } + + private short[] calcXOR(short[] chunk, short[] polyGenerator) { + int a = Integer.parseInt(shortArrayToBinaryString(chunk), 2); + int b = Integer.parseInt(shortArrayToBinaryString(polyGenerator), 2); + String binaryString = Integer.toBinaryString(a ^ b); + return convertBinaryStringToShortArray(binaryString); + } + + private String shortArrayToBinaryString(short[] value) { + StringBuilder binaryString = new StringBuilder(); + for (Short a : value) { + binaryString.append(a); + } + return binaryString.toString(); + } + + private char getCharFromShortArray(short[] chunk, int from, int to) { + short[] sign = Arrays.copyOfRange(chunk, from, to); + int character = Integer.parseInt(shortArrayToBinaryString(sign), 2); +// System.out.println(character); + return (char) character; + } + + private void fillPolynomial(short[] chunk) { + while (chunk.length % 8 != 0) { + chunk = addZeroAtBeginningOfArray(chunk); + } + } + + private short[] addZeroAtBeginningOfArray(short[] chunk) { + short[] letterTemp = new short[chunk.length + 1]; + letterTemp[0] = 0; + System.arraycopy(chunk, 0, letterTemp, 1, chunk.length); + return letterTemp; } private void convertMessageToBinaryShortArray() { messageAsShortArray = new short[message.length()]; - for (int i = 0; i < message.length(); i++) { + for (int i = 0; i < polyGenerator.length; i++) { if (message.charAt(i) == 48) { messageAsShortArray[i] = 0; } else { @@ -23,16 +128,38 @@ public class CrcTask { } } - private void createGeneratingPoly() { - polyGenerator = new short[16]; - for (int i = 0; i < 16; i++) { + private short[] convertBinaryStringToShortArray(String binaryString) { + short[] shortArray = new short[binaryString.length()]; + for (int i = 0; i < binaryString.length(); i++) { + if (binaryString.charAt(i) == 48) { + shortArray[i] = 0; + } else { + shortArray[i] = 1; + } + } + return shortArray; + } + + private void createGeneratingPolynomial() { + polyGenerator = new short[17]; + for (int i = 0; i < 17; i++) { polyGenerator[i] = 0; } + polyGenerator[0] = 1; + polyGenerator[4] = 1; + polyGenerator[11] = 1; + polyGenerator[16] = 1; + } + + private void swapPolynomialValues() { + for (int i = 0; i < polyGenerator.length - 1; i++) { + messageAsShortArray[i] = (short) ((messageAsShortArray[i] + 1) % 2); + } } - private void swapPolyValues() { - for (int i = 0; i < polyGenerator.length; i++) { - messageAsShortArray[i] = (short) ((messageAsShortArray[i] + 1) % 2); - } + String encode() { + convertMessageToBinaryShortArray(); + swapPolynomialValues(); + return rawMessage + generateFCS(); } } diff --git a/Zadanie-03/src/com/tylkowski/crc/Main.java b/Zadanie-03/src/com/tylkowski/crc/Main.java index 00dad62..3cc07b8 100644 --- a/Zadanie-03/src/com/tylkowski/crc/Main.java +++ b/Zadanie-03/src/com/tylkowski/crc/Main.java @@ -3,7 +3,8 @@ package com.tylkowski.crc; public class Main { public static void main(String[] args) { - CrcTask crcTask = new CrcTask(toBinary(args[0])); + CrcTask crcTask = new CrcTask(toBinary(args[0]), args[0]); + System.out.println(crcTask.encode()); } private static String toBinary(String s) {