日記を埋めるためだけのネタつぶし(4)

pngCRCの作成メソッド。
作っておくと何かと便利。


private static int crc_table[] = null;

/*!
* @brief CRCを作成します。
*
* @param buf 作成元のバイト配列
* @param off bufのオフセット
* @param len bufの長さ
*
* @retval CRCを返します。
*/
final protected int createCRC(byte[] buf, int off, int len) {
// テーブルが作成されていない場合、作成する
if (crc_table == null) {
int c,n,k;
crc_table = new int[256];
for (n = 0 ; n < 256 ; n++){
c = n;
for (k = 0 ; k < 8 ; k++){
if ((c & 1) != 0) c = (c >>> 1) ^ 0xedb88320;
else c = (c >>> 1) ;
}
crc_table[ n ] = c;
}
}
int crc = 0xffffffff;
len += off;
for (; off < len; off++){
crc = crc_table[ (crc ^ (int)buf[ off ]) & 0xff ] ^ (crc >>> 8);
}
return (crc ^ 0xffffffff);
}