|
| 1 | +package com.scanoss.utils; |
| 2 | + |
| 3 | +import java.nio.charset.StandardCharsets; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +/** |
| 8 | + * SCANOSS Hpsm Class |
| 9 | + * <p> |
| 10 | + * The Hpsm class provides all the necessary implementations to generate HPSM fingerprint for a given file or contents. |
| 11 | + * </p> |
| 12 | + */ |
| 13 | +public class Hpsm { |
| 14 | + |
| 15 | + // CRC8 table, Polynomial, initial CRC and post CRC XOR value. |
| 16 | + private static final int CRC8_MAXIM_DOW_TABLE_SIZE = 0x100; |
| 17 | + private static final int CRC8_MAXIM_DOW_POLYNOMIAL = 0x8C; // 0x31 reflected |
| 18 | + private static final int CRC8_MAXIM_DOW_INITIAL = 0x00; // 0x00 reflected |
| 19 | + private static final int CRC8_MAXIM_DOW_FINAL = 0x00; // 0x00 reflected |
| 20 | + private static int[] crc8MaximDowTable = new int[CRC8_MAXIM_DOW_TABLE_SIZE]; |
| 21 | + |
| 22 | + private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII); |
| 23 | + |
| 24 | + /** |
| 25 | + * Calculates the HPSM value for the given content, represented as an array of bytes. |
| 26 | + * This method performs normalization on the content, calculates CRC8 for each line, |
| 27 | + * and returns the hexadecimal representation of the CRC8 values. |
| 28 | + * |
| 29 | + * @param content the content as an array of bytes |
| 30 | + * @return the HPSM value in hexadecimal format |
| 31 | + */ |
| 32 | + public static String calcHpsm(byte[] content) { |
| 33 | + List<Integer> listNormalized = new ArrayList<>(); |
| 34 | + List<Integer> crcLines = new ArrayList<>(); |
| 35 | + |
| 36 | + int lastLine = 0; |
| 37 | + crc8MaximDowGenerateTable(); |
| 38 | + |
| 39 | + for (int i = 0; i < content.length ; i++) { |
| 40 | + char c = (char) content[i]; |
| 41 | + if (c == '\n') { // When there is a new line |
| 42 | + if (!listNormalized.isEmpty()) { |
| 43 | + crcLines.add(crc8MaximDowBuffer(convertListToByteArray(listNormalized))); |
| 44 | + listNormalized.clear(); |
| 45 | + } else if (lastLine + 1 == i) { |
| 46 | + crcLines.add(0xFF); |
| 47 | + } else if (i - lastLine > 1) { |
| 48 | + crcLines.add(0x00); |
| 49 | + } |
| 50 | + lastLine = i; |
| 51 | + } else { |
| 52 | + int cNormalized = WinnowingUtils.normalize(c); |
| 53 | + if (cNormalized != 0) listNormalized.add(cNormalized); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return convertToHex(convertListToByteArray(crcLines)); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Calculates CRC-8 using the Maxim/Dallas polynomial without using a lookup table. |
| 62 | + * This method is suitable for applications where memory constraints are critical |
| 63 | + * and a lookup table cannot be afforded. |
| 64 | + * |
| 65 | + * @param crc The current CRC value. |
| 66 | + * @param b The byte to be processed. |
| 67 | + * @return The updated CRC value after processing the byte. |
| 68 | + */ |
| 69 | + private static int crc8MaximDowByteNoTable(int crc, int b) { |
| 70 | + crc ^= b; |
| 71 | + for (int count = 0; count < 8; count++) { |
| 72 | + boolean isSet = (crc & 0x01) != 0; |
| 73 | + crc >>= 1; |
| 74 | + if (isSet) crc ^= CRC8_MAXIM_DOW_POLYNOMIAL; |
| 75 | + } |
| 76 | + return crc; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Generates a lookup table for CRC-8 using the Maxim/Dallas polynomial. |
| 81 | + * The generated table is used for faster CRC calculations. |
| 82 | + */ |
| 83 | + private static void crc8MaximDowGenerateTable() { |
| 84 | + for (int i = 0; i < CRC8_MAXIM_DOW_TABLE_SIZE; i++) { |
| 85 | + crc8MaximDowTable[i] = crc8MaximDowByteNoTable(0, i); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Calculates CRC-8 using the Maxim/Dow polynomial with a lookup table. |
| 91 | + * This method utilizes a pre-generated lookup table for faster CRC calculations. |
| 92 | + * |
| 93 | + * @param crc The current CRC value. |
| 94 | + * @param b The byte to be processed. |
| 95 | + * @return The updated CRC value after processing the byte. |
| 96 | + */ |
| 97 | + private static int crc8MaximDowByte(int crc, int b) { |
| 98 | + int index = b ^ crc; |
| 99 | + return crc8MaximDowTable[index] ^ (crc >> 8); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Calculates CRC-8 for a buffer of bytes using the Maxim/Dallas polynomial. |
| 104 | + * |
| 105 | + * @param buffer The buffer containing bytes for CRC calculation. |
| 106 | + * @return The CRC-8 value for the given buffer. |
| 107 | + */ |
| 108 | + private static int crc8MaximDowBuffer(byte[] buffer) { |
| 109 | + int crc = CRC8_MAXIM_DOW_INITIAL; |
| 110 | + for (byte b : buffer) { |
| 111 | + crc = crc8MaximDowByte(crc, b & 0xFF); // Convert byte to unsigned integer |
| 112 | + } |
| 113 | + crc ^= CRC8_MAXIM_DOW_FINAL; |
| 114 | + return crc; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Converts a list of integers to a byte array. |
| 119 | + * |
| 120 | + * @param integerList The list of integers to be converted. |
| 121 | + * @return The byte array representing the converted integers. |
| 122 | + */ |
| 123 | + private static byte[] convertListToByteArray(List<Integer> integerList) { |
| 124 | + byte[] byteArray = new byte[integerList.size()]; |
| 125 | + for (int i = 0; i < integerList.size(); i++) { |
| 126 | + byteArray[i] = integerList.get(i).byteValue(); |
| 127 | + } |
| 128 | + return byteArray; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Converts an array of bytes to its hexadecimal representation. |
| 133 | + * |
| 134 | + * @param bytes the array of bytes to be converted |
| 135 | + * @return the hexadecimal representation of the input byte array |
| 136 | + */ |
| 137 | + private static String convertToHex(byte [] bytes) { |
| 138 | + byte[] hexChars = new byte[bytes.length * 2]; |
| 139 | + for (int j = 0; j < bytes.length; j++) { |
| 140 | + int v = bytes[j] & 0xFF; |
| 141 | + hexChars[j * 2] = HEX_ARRAY[v >>> 4]; |
| 142 | + hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; |
| 143 | + } |
| 144 | + return new String(hexChars, StandardCharsets.UTF_8).toLowerCase(); |
| 145 | + } |
| 146 | +} |
0 commit comments