-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Alejandro Perez <alejandro.perez@scanoss.com>
- Loading branch information
1 parent
db510fb
commit 1dc6855
Showing
9 changed files
with
163 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,91 @@ | ||
package com.scanoss.utils; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Hpsm { | ||
private static final int CRC8_MAXIM_DOW_TABLE_SIZE = 0x100; | ||
private static final int CRC8_MAXIM_DOW_POLYNOMIAL = 0x8C; // 0x31 reflected | ||
private static final int CRC8_MAXIM_DOW_INITIAL = 0x00; // 0x00 reflected | ||
private static final int CRC8_MAXIM_DOW_FINAL = 0x00; // 0x00 reflected | ||
private static int[] crc8MaximDowTable = new int[CRC8_MAXIM_DOW_TABLE_SIZE]; | ||
|
||
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); | ||
|
||
public static String calcHpsm(byte[] content) { | ||
List<Integer> listNormalized = new ArrayList<>(); | ||
List<Integer> crcLines = new ArrayList<>(); | ||
|
||
int lastLine = 0; | ||
crc8MaximDowGenerateTable(); | ||
|
||
for (int i = 0; i < content.length ; i++) { | ||
char c = (char) content[i]; | ||
if (c == '\n') { // When there is a new line | ||
if (!listNormalized.isEmpty()) { | ||
crcLines.add(crc8MaximDowBuffer(convertListToByteArray(listNormalized))); | ||
listNormalized.clear(); | ||
} else if (lastLine + 1 == i) { | ||
crcLines.add(0xFF); | ||
} else if (i - lastLine > 1) { | ||
crcLines.add(0x00); | ||
} | ||
lastLine = i; | ||
} else { | ||
int cNormalized = WinnowingUtils.normalize(c); | ||
if (cNormalized != 0) listNormalized.add(cNormalized); | ||
} | ||
} | ||
|
||
return convertToHex(convertListToByteArray(crcLines)); | ||
} | ||
|
||
private static int crc8MaximDowByteNoTable(int crc, int b) { | ||
crc ^= b; | ||
for (int count = 0; count < 8; count++) { | ||
boolean isSet = (crc & 0x01) != 0; | ||
crc >>= 1; | ||
if (isSet) crc ^= CRC8_MAXIM_DOW_POLYNOMIAL; | ||
} | ||
return crc; | ||
} | ||
|
||
private static void crc8MaximDowGenerateTable() { | ||
for (int i = 0; i < CRC8_MAXIM_DOW_TABLE_SIZE; i++) { | ||
crc8MaximDowTable[i] = crc8MaximDowByteNoTable(0, i); | ||
} | ||
} | ||
|
||
private static int crc8MaximDowByte(int crc, int b) { | ||
int index = b ^ crc; | ||
return crc8MaximDowTable[index] ^ (crc >> 8); | ||
} | ||
|
||
private static int crc8MaximDowBuffer(byte[] buffer) { | ||
int crc = CRC8_MAXIM_DOW_INITIAL; | ||
for (byte b : buffer) { | ||
crc = crc8MaximDowByte(crc, b & 0xFF); // Convert byte to unsigned integer | ||
} | ||
crc ^= CRC8_MAXIM_DOW_FINAL; | ||
return crc; | ||
} | ||
|
||
private static byte[] convertListToByteArray(List<Integer> integerList) { | ||
byte[] byteArray = new byte[integerList.size()]; | ||
for (int i = 0; i < integerList.size(); i++) { | ||
byteArray[i] = integerList.get(i).byteValue(); | ||
} | ||
return byteArray; | ||
} | ||
|
||
private static String convertToHex(byte [] bytes) { | ||
byte[] hexChars = new byte[bytes.length * 2]; | ||
for (int j = 0; j < bytes.length; j++) { | ||
int v = bytes[j] & 0xFF; | ||
hexChars[j * 2] = HEX_ARRAY[v >>> 4]; | ||
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; | ||
} | ||
return new String(hexChars, StandardCharsets.UTF_8).toLowerCase(); | ||
} | ||
} |
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,22 @@ | ||
package com.scanoss.utils; | ||
|
||
public class WinnowingUtils { | ||
|
||
/** | ||
* Normalise the given character | ||
* | ||
* @param c character to normalise | ||
* @return normalised character | ||
*/ | ||
public static char normalize(char c) { | ||
if (c < '0' || c > 'z') { | ||
return 0; | ||
} else if (c <= '9' || c >= 'a') { | ||
return c; | ||
} else if (c >= 'A' && c <= 'Z') { | ||
return (char) (c + 32); | ||
} else { | ||
return 0; | ||
} | ||
} | ||
} |
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