diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-06 16:55:31 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-06 16:55:31 +1300 |
commit | 2b9187ac736feeb661e2814af8fa534a335998d4 (patch) | |
tree | 18e89d33116194bc5f2fa90ea48b169608b81a76 /src/org/traccar/helper | |
parent | 6652e4c3e23da63d4277caaca7f91e651793f503 (diff) | |
download | trackermap-server-2b9187ac736feeb661e2814af8fa534a335998d4.tar.gz trackermap-server-2b9187ac736feeb661e2814af8fa534a335998d4.tar.bz2 trackermap-server-2b9187ac736feeb661e2814af8fa534a335998d4.zip |
Clean up CRC-16 implementations
Diffstat (limited to 'src/org/traccar/helper')
-rw-r--r-- | src/org/traccar/helper/Checksum.java | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/src/org/traccar/helper/Checksum.java b/src/org/traccar/helper/Checksum.java index cdf1bbf50..69c9dd499 100644 --- a/src/org/traccar/helper/Checksum.java +++ b/src/org/traccar/helper/Checksum.java @@ -91,8 +91,14 @@ public class Checksum { 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 }; - public static final int CRC16_CCITT = 0xFFFF; - public static final int CRC16_CCITT_XOROUT = 0xFFFF; + // More info: http://reveng.sourceforge.net/crc-catalogue/16.htm + public static final String CRC16_X25 = "X-25"; + public static final String CRC16_CCITT_FALSE = "CCITT-FALSE"; + public static final String CRC16_KERMIT = "KERMIT"; + public static final String CRC16_XMODEM = "XMODEM"; + public static final String CRC16_AUG_CCITT = "AUG-CCITT"; + public static final String CRC16_GENIBUS = "GENIBUS"; + public static final String CRC16_MCRF4XX = "MCRF4XX"; private static int crc16Unreflected(ByteBuffer buf, int crc_in, int[] table) { int crc16 = crc_in; @@ -110,16 +116,24 @@ public class Checksum { return crc16 & 0xFFFF; } - public static int crc16Ccitt(ByteBuffer buf) { - return crc16Reflected(buf, 0xFFFF, CRC16_CCITT_TABLE_REVERSE) ^ 0xFFFF; - } - - public static int crc16X25Ccitt(ByteBuffer buf) { - return crc16Unreflected(buf, 0xFFFF, CRC16_CCITT_TABLE); - } - - public static int crc16Ccitt(ByteBuffer buf, int seed, int xorout) { - return crc16Reflected(buf, seed, CRC16_CCITT_TABLE_REVERSE) ^ xorout; + public static int crc16(String type, ByteBuffer buf) { + switch (type) { + case CRC16_X25: + return crc16Reflected(buf, 0xFFFF, CRC16_CCITT_TABLE_REVERSE) ^ 0xFFFF; + case CRC16_CCITT_FALSE: + return crc16Unreflected(buf, 0xFFFF, CRC16_CCITT_TABLE); + case CRC16_KERMIT: + return crc16Reflected(buf, 0, CRC16_CCITT_TABLE_REVERSE); + case CRC16_XMODEM: + return crc16Unreflected(buf, 0, CRC16_CCITT_TABLE); + case CRC16_AUG_CCITT: + return crc16Unreflected(buf, 0x1d0f, CRC16_CCITT_TABLE); + case CRC16_GENIBUS: + return crc16Unreflected(buf, 0xFFFF, CRC16_CCITT_TABLE) ^ 0xFFFF; + case CRC16_MCRF4XX: + return crc16Reflected(buf, 0xFFFF, CRC16_CCITT_TABLE_REVERSE); + } + throw new UnsupportedOperationException(); } public static int crc32(ByteBuffer buf) { |