1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/**
Calcul de CRC32
*/
public class ClassCRC32 {
private long pcrc;
private long[] ptabConst = new long[256];
private long DefaultPoly = StaticDefaultPoly;
final private static long StaticDefaultPoly = 0x04c11db7L; // AUTODIN II, Ethernet, FDDI
//*******************************************
public ClassCRC32(){
pcrc = initCRC(ptabConst, DefaultPoly);
}
public ClassCRC32(long crc32_poly){
pcrc = initCRC(ptabConst, crc32_poly);
}
/**
* @param tab : tableau de byte a tester
*/
public ClassCRC32(int[] tab){
pcrc = initCRC(ptabConst, DefaultPoly);
add(tab);
}
/**
*
* @param tab : tableau de byte a tester
* @param crc_poly : valeur définissant la complexité de la focntion CRC polynominal
*/
public ClassCRC32(int[] tab, long crc32_poly){
pcrc = initCRC(ptabConst, crc32_poly);
add(tab);
}
//*******************************************
/**
* @param tab : tableau de byte a tester
*/
public void add(int[] tab){
pcrc = addData(pcrc, tab, ptabConst);
}
//*******************************************
public long getCRC(){
return getVal(pcrc);
}
// **************************************************************
// **************************************************************
private static long initCRC(long[] tabCRC, long poly){
long i, j;
long c;
for (i = 0; i < 256; ++i){
for (c = i << 24, j = 8; j > 0; --j) {
if((c & 0x80000000L) != 0){
c = (c << 1) ^ poly;
} else {
c = (c << 1);
}
}
tabCRC[(int)i] = c & 0xFFFFFFFFL;
}
return 0xffffffffL;
}
private static long addData(long crc, int[] tab, long[] tabCRC){
long crcTmp = crc;
for(int valInt : tab){
long val = (long)(valInt & 0xFF); // on ne traite que 8 bits (utilisation de int pour éviter les problème de signes)
System.out.println("1- crcTmp: " + String.format("%08X", crcTmp)); // debug
long val1 = (crcTmp << 8) & 0xFFFFFFFFL;
System.out.println("2a- val1: " + String.format("%08X", val1)); // debug
long val2 = ((crcTmp & 0xFFFFFFFFL) >>> 24);
System.out.println("3- val2: " + String.format("%08X", val2)); // debug
val2 ^= val;
System.out.println("4- val2: " + String.format("%08X", val2) + " / val: " + String.format("%08X", val)); // debug
val2 = tabCRC[(int)(val2 & 0xFF)];
System.out.println("5- val2: " + String.format("%08X", val2)); // debug
crcTmp = val1 ^ val2;
System.out.println("6- crcTmp: " + String.format("%08X", crcTmp)); // debug
//crcTmp = (crcTmp << 8) ^ tabCRC[(crcTmp >> 24) ^ val];
}
return crcTmp;
}
private static long getVal(long crc){
return (~crc) & 0xFFFFFFFFL;
}
// *******************************************************
// *******************************************************
/**
* Récupération du CRC avec paramêtrage de la formule polynomiale
*/
static public long getCRC(int[] tab, long poly){
long[] tabConst = new long[256];
long crc;
crc = initCRC(tabConst, poly);
crc = addData(crc, tab, tabConst);
crc = getVal(crc);
return crc;
}
/**
* Récupération du CRC
*/
static public long getCRC(int[] tab){
long[] tabConst = new long[256];
long crc;
crc = initCRC(tabConst, StaticDefaultPoly);
crc = addData(crc, tab, tabConst);
return getVal(crc);
}
// **************************************************************
// **************************************************************
/**
* Récupération du CRC en chaine de caractères
*/
public String toString(){
return String.format("%08X", getVal(pcrc));
}
} |
Partager