-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Dennis Sheirer <[email protected]>
- Loading branch information
Showing
6 changed files
with
138 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* ***************************************************************************** | ||
* Copyright (C) 2014-2024 Dennis Sheirer | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* **************************************************************************** | ||
*/ | ||
|
||
package io.github.dsheirer.edac; | ||
|
||
import io.github.dsheirer.bits.BinaryMessage; | ||
import io.github.dsheirer.bits.IntField; | ||
|
||
/** | ||
* Utility for calculating the CRC checksum for CRC-16 using polynomial 0x1021 and Initial Fill/Residual of 0xFFFF | ||
*/ | ||
public class CRC16 | ||
{ | ||
/** | ||
* Calculates the 16-bit CRC checksum for the message using polynomial 0x1021 and residual 0xFFFF | ||
* @param message with transmitted 16-bit checksum at the end. | ||
* @return true if the check is correct or false if it fails the CRC check. | ||
*/ | ||
public static boolean check(BinaryMessage message) | ||
{ | ||
BinaryMessage copy = message.copy(); | ||
BinaryMessage polynomial = BinaryMessage.loadHex("11021"); | ||
polynomial.rotateLeft(3, 0, 20); | ||
polynomial.setSize(message.size()); | ||
|
||
int previousX = 0; | ||
for(int x = copy.nextSetBit(0); x < copy.size() - 16; x = copy.nextSetBit(x + 1)) | ||
{ | ||
polynomial.rotateRight(x - previousX, previousX, 17 + x); | ||
previousX = x; | ||
copy.xor(polynomial); | ||
} | ||
|
||
IntField crc = IntField.length16(copy.size() - 16); | ||
return copy.getInt(crc) == 0xFFFF; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters