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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
//start of CRC16.java
//TEXT_STYLE:CODE=Shift_JIS(Japanese):RET_CODE=CRLF
/**
* CRC16.java
*
* Copyright (C) 2001-2002 Michel Ishizuka All rights reserved.
*
* ȉ̏ɓӂȂ\[XƃoCi`̍ĔzzƎgp
* ύX̗Lɂ炸B
*
* PD\[XR[h̍ĔzzɂĒ쌠\ ̏̃Xg
* щL̐ێȂĂ͂ȂȂB
*
* QDoCi`̍ĔzzɂĒ쌠\ ̏̃Xg
* щL̐gp ̑̔zz
* ܂ގɋLqȂȂȂB
*
* ̃\tgEFA͐Β˔ڂɂĖۏŒA̖
* IBłƂۏAilLƂۏɂƂǂ܂炸A
* Ȃ閾IшÎIȕۏȂB
* Β˔ڂ ̃\tgEFA̎gpɂ钼ړIAԐړIA
* IAȁAT^IȁA邢͕KRIȑQ(gpɂf[^
* AƖ̒f〈܂Ăv̈⎸Ai
* T[rX̓l邪AĂꂾɌ肳Ȃ
* Q)ɑāAȂ鎖Ԃ̌ƂȂƂĂA_̐
* C△ߎӔC܂ ȂӔC낤ƂAƂꂪs
* ŝׂ߂łƂĂA܂͂̂悤ȑQ̉\
* ĂƂĂ̐ӔCȂ̂ƂB
*/
package jp.gr.java_conf.dangan.util.lha;
//import classes and interfaces
import java.util.zip.Checksum;
//import exceptions
/**
* CRC16lZo邽߂̃NXB
*
* NX̒萔AA
* <pre>
* bɂASYT
* F Zp]_
* ISBN4-87408-414-1 C3055 2400~(w)
* </pre>
* ɂB
*
* <pre>
* -- revision history --
* $Log: CRC16.java,v $
* Revision 1.0 2002/07/24 00:00:00 dangan
* add to version control
* [maintanance]
* \[X
* ^up~
* CZX̕ύX
*
* </pre>
*
* @author $Author: dangan $
* @version $Revision: 1.0 $
*/
public class CRC16 implements Checksum{
//------------------------------------------------------------------
// class field
//------------------------------------------------------------------
// public static final int CRC_ANSY_POLY
// public static final int CRC_ANSY_INIT
// public static final int CCITT_POLY
// public static final int CCITT_INIT
// public static final int DefaultPOLY
// public static final int DefaultINIT
//------------------------------------------------------------------
/**
* CRC-ANSY ܂ CRC-16 ƂėL
* x^16 + x^15 + x^2 + 1 rbg\ɂ́B
*/
public static final int CRC_ANSY_POLY = 0xA001;
/**
* LHAŎgp crc ̏lB
* ҂ɐݒ肵lłA
* CRC-ANSY ł̒llƂ
* ߂Ă邩͒mȂB
*/
public static final int CRC_ANSY_INIT = 0x0000;
/**
* CCITT X.25ƂKi
* x^16 + x^12 + x^5 + 1 rbg\ɂ́B
*/
public static final int CCITT_POLY = 0x8408;
/**
* CCITT X.25ƂKi crc ̏lB
*/
public static final int CCITT_INIT = 0xFFFF;
/**
* LHAŒʏgpAƂӖŃftHgCRCB
* CRC16.CRC_ANSY_POLY ƓłB
*/
public static final int DefaultPOLY = CRC16.CRC_ANSY_POLY;
/**
* LHAŒʏgpAƂӖŃftHgcrc̏lB
* CRC16.CRC_ANSY_INIT ƓłB
*/
public static final int DefaultINIT = CRC16.CRC_ANSY_INIT;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// private int crc
// private int init
// private int[] crcTable
//------------------------------------------------------------------
/**
* CRC16l
*/
private int crc;
/**
* crc ̏l
*/
private int init;
/**
* CRC16l̍XVpe[u
*/
private int[] crcTable;
//------------------------------------------------------------------
// constructor
//------------------------------------------------------------------
// public CRC16()
// public CRC16( int poly )
// public CRC16( int poly, int init )
// public CRC16( int[] crcTable, int init )
//------------------------------------------------------------------
/**
* LHAŎgp Əl CRC16B
*/
public CRC16(){
this( DefaultPOLY, DefaultINIT );
}
/**
* poly Ŏw肳 CRC16B
* l poly CRC16.CCITT_POLY ł
* CRC16.CCITT_INIT łȂ
* CRC16.DefaultINIT gpB
*
* @param poly CRC16ZoɎgp鑽̃rbg\
*/
public CRC16( int poly ){
this( poly,
( poly == CRC16.CCITT_POLY ?
CRC16.CCITT_INIT :
CRC16.DefaultINIT ) );
}
/**
* poly Ŏw肳 initŎw肳鏉l
* CRC16B
*
* @param poly CRC16ZoɎgp鑽̃rbg\
* @param init crc ̏l
*/
public CRC16( int poly, int init ){
this( CRC16.makeCrcTable( poly ), init );
}
/**
* crcTable Ŏw肳 CRCZop\
* initŎw肳鏉l CRC16쐬B
*
* @param crcTable CRC16ZoɎgp\
* @param init crc ̏l
*/
public CRC16( int[] crcTable, int init ){
final int BYTE_PATTERNS= 256;
if( crcTable.length == BYTE_PATTERNS ){
this.crcTable = crcTable;
this.init = init;
this.reset();
}else{
throw new IllegalArgumentException( "crcTable.length must equals 256" );
}
}
//------------------------------------------------------------------
// method of java.util.zip.Checksum
//------------------------------------------------------------------
// update
//------------------------------------------------------------------
// public void update( int byte8 )
// public void update( byte[] buffer )
// public void update( byte[] buffer, int index, int length )
//------------------------------------------------------------------
/**
* byte8 Ŏw肳 1oCg̃f[^ crc̒lXVB
*
* @param byte8 crcXV 1oCg̃f[^
*/
public void update( int byte8 ){
final int BYTE_BITS = 8;
this.crc = ( this.crc >> BYTE_BITS )
^ this.crcTable[ ( this.crc ^ byte8 ) & 0xFF ];
}
/**
* buffer Ŏw肵oCgz crc ̒lXVB
*
* @param buffer crcXV f[^oCgz
*/
public void update( byte[] buffer ){
this.update( buffer, 0, buffer.length );
}
/**
* buffer Ŏw肵oCgz crc ̒lXVB
*
* @param buffer crcXV f[^oCgz
* @param index f[^̊Jnʒu
* @param length `FbNT̍XVɎgoCg
*/
public void update( byte[] buffer, int index, int length ){
final int BYTE_BITS = 8;
while( 0 < ( index & 0x03 ) && 0 < length-- ){
this.crc = ( this.crc >> BYTE_BITS )
^ this.crcTable[ ( this.crc ^ buffer[index++] ) & 0xFF ];
}
while( 4 <= length ){
int data = ( buffer[index++] & 0xFF )
| ( ( buffer[index++] & 0xFF ) << 8 )
| ( ( buffer[index++] & 0xFF ) << 16 )
| ( buffer[index++] << 24 );
this.crc = ( this.crc >> BYTE_BITS )
^ this.crcTable[ ( this.crc ^ data ) & 0xFF ];
data >>>= BYTE_BITS;
this.crc = ( this.crc >> BYTE_BITS )
^ this.crcTable[ ( this.crc ^ data ) & 0xFF ];
data >>>= BYTE_BITS;
this.crc = ( this.crc >> BYTE_BITS )
^ this.crcTable[ ( this.crc ^ data ) & 0xFF ];
data >>>= BYTE_BITS;
this.crc = ( this.crc >> BYTE_BITS )
^ this.crcTable[ ( this.crc ^ data ) & 0xFF ];
length -= 4;
}
while( 0 < length-- ){
this.crc = ( this.crc >> BYTE_BITS )
^ this.crcTable[ ( this.crc ^ buffer[index++] ) & 0xFF ];
}
}
//------------------------------------------------------------------
// method of java.util.zip.Checksum
//------------------------------------------------------------------
// other
//------------------------------------------------------------------
// public void reset()
// public long getValue()
//------------------------------------------------------------------
/**
* crc llɐݒ肵ȂB
*/
public void reset(){
this.crc = this.init;
}
/**
* crc lB
* crc l 2oCg̒lłA
* 0x0000`0xFFFFɃ}bvB
*
* @return crc l
*/
public long getValue(){
return this.crc & 0xFFFF;
}
//------------------------------------------------------------------
// shared method
//------------------------------------------------------------------
// public static int[] makeCrcTable( int init )
//------------------------------------------------------------------
/**
* CRClZop \쐬B
*
* @param poly CRCZop̑
*/
public static int[] makeCrcTable( int poly ){
final int BYTE_PATTERNS = 256;
final int BYTE_BITS = 8;
int[] crcTable = new int[BYTE_PATTERNS];
for( int i = 0 ; i < BYTE_PATTERNS ; i++ ){
crcTable[i] = i;
for( int j = 0 ; j < BYTE_BITS ; j++ ){
if( ( crcTable[i] & 1 ) != 0 ){
crcTable[i] = ( crcTable[i] >> 1 ) ^ poly;
}else{
crcTable[i] >>= 1;
}
}
}
return crcTable;
}
}
//end of CRC16.java
|