package org.ts.ddcs.common.util;

public class CRC166 {

	private final static short[] CRC16_TABLE = { 0x0000, 0x1021, 0x2042,
			0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, (short) 0x8108,
			(short) 0x9129, (short) 0xa14a, (short) 0xb16b, (short) 0xc18c,
			(short) 0xd1ad, (short) 0xe1ce, (short) 0xf1ef, 0x1231, 0x0210,
			0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, (short) 0x9339,
			(short) 0x8318, (short) 0xb37b, (short) 0xa35a, (short) 0xd3bd,
			(short) 0xc39c, (short) 0xf3ff, (short) 0xe3de, 0x2462, 0x3443,
			0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, (short) 0xa56a,
			(short) 0xb54b, (short) 0x8528, (short) 0x9509, (short) 0xe5ee,
			(short) 0xf5cf, (short) 0xc5ac, (short) 0xd58d, 0x3653, 0x2672,
			0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, (short) 0xb75b,
			(short) 0xa77a, (short) 0x9719, (short) 0x8738, (short) 0xf7df,
			(short) 0xe7fe, (short) 0xd79d, (short) 0xc7bc, 0x48c4, 0x58e5,
			0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, (short) 0xc9cc,
			(short) 0xd9ed, (short) 0xe98e, (short) 0xf9af, (short) 0x8948,
			(short) 0x9969, (short) 0xa90a, (short) 0xb92b, 0x5af5, 0x4ad4,
			0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, (short) 0xdbfd,
			(short) 0xcbdc, (short) 0xfbbf, (short) 0xeb9e, (short) 0x9b79,
			(short) 0x8b58, (short) 0xbb3b, (short) 0xab1a, 0x6ca6, 0x7c87,
			0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, (short) 0xedae,
			(short) 0xfd8f, (short) 0xcdec, (short) 0xddcd, (short) 0xad2a,
			(short) 0xbd0b, (short) 0x8d68, (short) 0x9d49, 0x7e97, 0x6eb6,
			0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, (short) 0xff9f,
			(short) 0xefbe, (short) 0xdfdd, (short) 0xcffc, (short) 0xbf1b,
			(short) 0xaf3a, (short) 0x9f59, (short) 0x8f78, (short) 0x9188,
			(short) 0x81a9, (short) 0xb1ca, (short) 0xa1eb, (short) 0xd10c,
			(short) 0xc12d, (short) 0xf14e, (short) 0xe16f, 0x1080, 0x00a1,
			0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, (short) 0x83b9,
			(short) 0x9398, (short) 0xa3fb, (short) 0xb3da, (short) 0xc33d,
			(short) 0xd31c, (short) 0xe37f, (short) 0xf35e, 0x02b1, 0x1290,
			0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, (short) 0xb5ea,
			(short) 0xa5cb, (short) 0x95a8, (short) 0x8589, (short) 0xf56e,
			(short) 0xe54f, (short) 0xd52c, (short) 0xc50d, 0x34e2, 0x24c3,
			0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, (short) 0xa7db,
			(short) 0xb7fa, (short) 0x8799, (short) 0x97b8, (short) 0xe75f,
			(short) 0xf77e, (short) 0xc71d, (short) 0xd73c, 0x26d3, 0x36f2,
			0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, (short) 0xd94c,
			(short) 0xc96d, (short) 0xf90e, (short) 0xe92f, (short) 0x99c8,
			(short) 0x89e9, (short) 0xb98a, (short) 0xa9ab, 0x5844, 0x4865,
			0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, (short) 0xcb7d,
			(short) 0xdb5c, (short) 0xeb3f, (short) 0xfb1e, (short) 0x8bf9,
			(short) 0x9bd8, (short) 0xabbb, (short) 0xbb9a, 0x4a75, 0x5a54,
			0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, (short) 0xfd2e,
			(short) 0xed0f, (short) 0xdd6c, (short) 0xcd4d, (short) 0xbdaa,
			(short) 0xad8b, (short) 0x9de8, (short) 0x8dc9, 0x7c26, 0x6c07,
			0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, (short) 0xef1f,
			(short) 0xff3e, (short) 0xcf5d, (short) 0xdf7c, (short) 0xaf9b,
			(short) 0xbfba, (short) 0x8fd9, (short) 0x9ff8, 0x6e17, 0x7e36,
			0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 };

	/**
	 * 获取crc校验的short形式
	 * 
	 * @param data
	 * @return
	 */
	private static short getCrc16(byte[] data) {
		int crc = 0;
		int length = data.length;
		for (int i = 0; i < length; i++) {
			crc = ((crc & 0xFF) << 8)
					^ CRC16_TABLE[(((crc & 0xFF00) >> 8) ^ data[i]) & 0xFF];
		}
		crc = crc & 0xFFFF;
		return (short) crc;
	}

	/**
	 * 获取crc校验的byte形式
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] crc16Bytes(byte[] data) {
		return BytesUtil.short2bytes(getCrc16(data));
	}

}
