//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============// // // Purpose: Generic CRC functions // //=============================================================================// #include "tier0/platform.h" #include "commonmacros.h" #include "tier1/checksum_crc.h" // memdbgon must be the last include file in a .cpp file!!! #include "tier0/memdbgon.h" #define CRC32_INIT_VALUE 0xFFFFFFFFUL #define CRC32_XOR_VALUE 0xFFFFFFFFUL #define NUM_BYTES 256 static const CRC32_t pulCRCTable[NUM_BYTES] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; void CRC32_Init(CRC32_t *pulCRC) { *pulCRC = CRC32_INIT_VALUE; } void CRC32_Final(CRC32_t *pulCRC) { *pulCRC ^= CRC32_XOR_VALUE; } CRC32_t CRC32_GetTableEntry( unsigned int slot ) { return pulCRCTable[(unsigned char)slot]; } void CRC32_ProcessBuffer(CRC32_t *pulCRC, const void *pBuffer, int nBuffer) { CRC32_t ulCrc = *pulCRC; unsigned char *pb = (unsigned char *)pBuffer; unsigned int nFront; int nMain; JustAfew: switch (nBuffer) { case 7: ulCrc = pulCRCTable[*pb++ ^ (unsigned char)ulCrc] ^ (ulCrc >> 8); case 6: ulCrc = pulCRCTable[*pb++ ^ (unsigned char)ulCrc] ^ (ulCrc >> 8); case 5: ulCrc = pulCRCTable[*pb++ ^ (unsigned char)ulCrc] ^ (ulCrc >> 8); case 4: ulCrc ^= LittleLong( *(CRC32_t *)pb ); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); *pulCRC = ulCrc; return; case 3: ulCrc = pulCRCTable[*pb++ ^ (unsigned char)ulCrc] ^ (ulCrc >> 8); case 2: ulCrc = pulCRCTable[*pb++ ^ (unsigned char)ulCrc] ^ (ulCrc >> 8); case 1: ulCrc = pulCRCTable[*pb++ ^ (unsigned char)ulCrc] ^ (ulCrc >> 8); case 0: *pulCRC = ulCrc; return; } // We may need to do some alignment work up front, and at the end, so that // the main loop is aligned and only has to worry about 8 byte at a time. // // The low-order two bits of pb and nBuffer in total control the // upfront work. // nFront = ((uintp)pb) & 3; nBuffer -= nFront; switch (nFront) { case 3: ulCrc = pulCRCTable[*pb++ ^ (unsigned char)ulCrc] ^ (ulCrc >> 8); case 2: ulCrc = pulCRCTable[*pb++ ^ (unsigned char)ulCrc] ^ (ulCrc >> 8); case 1: ulCrc = pulCRCTable[*pb++ ^ (unsigned char)ulCrc] ^ (ulCrc >> 8); } nMain = nBuffer >> 3; while (nMain--) { ulCrc ^= LittleLong( *(CRC32_t *)pb ); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); ulCrc ^= LittleLong( *(CRC32_t *)(pb + 4) ); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); ulCrc = pulCRCTable[(unsigned char)ulCrc] ^ (ulCrc >> 8); pb += 8; } nBuffer &= 7; goto JustAfew; } //----------------------------------------------------------------------------- // CRC64 //----------------------------------------------------------------------------- #define CRC64_INIT_VALUE 0xFFFFFFFFFFFFFFFFULL #define CRC64_XOR_VALUE 0xFFFFFFFFFFFFFFFFULL #define NUM_CRC64_TABLE_ENTRIES 256 static const CRC64_t pCRC64Table[ NUM_CRC64_TABLE_ENTRIES ] = { /* // Uncomment this to generate and print this table. // The value 0x95AC9329AC4BC9B5ULL comes from "An Improved 64-bit Cyclic Redundancy Check for Protein Sequences" David T. Jones for ( int i = 0; i < 256; i++ ) { CRC64_t part = i; for (int b = 0; b < 8; b++) { if (part & 1) part = (part >> 1) ^ 0x95AC9329AC4BC9B5ULL; else part >>= 1; } if ( i % 4 == 0 ) Msg( "\n" ); Msg( "0x%016llXULL, ", part ); } */ 0x0000000000000000ULL, 0x7AD870C830358979ULL, 0xF5B0E190606B12F2ULL, 0x8F689158505E9B8BULL, 0xC038E5739841B68FULL, 0xBAE095BBA8743FF6ULL, 0x358804E3F82AA47DULL, 0x4F50742BC81F2D04ULL, 0xAB28ECB46814FE75ULL, 0xD1F09C7C5821770CULL, 0x5E980D24087FEC87ULL, 0x24407DEC384A65FEULL, 0x6B1009C7F05548FAULL, 0x11C8790FC060C183ULL, 0x9EA0E857903E5A08ULL, 0xE478989FA00BD371ULL, 0x7D08FF3B88BE6F81ULL, 0x07D08FF3B88BE6F8ULL, 0x88B81EABE8D57D73ULL, 0xF2606E63D8E0F40AULL, 0xBD301A4810FFD90EULL, 0xC7E86A8020CA5077ULL, 0x4880FBD87094CBFCULL, 0x32588B1040A14285ULL, 0xD620138FE0AA91F4ULL, 0xACF86347D09F188DULL, 0x2390F21F80C18306ULL, 0x594882D7B0F40A7FULL, 0x1618F6FC78EB277BULL, 0x6CC0863448DEAE02ULL, 0xE3A8176C18803589ULL, 0x997067A428B5BCF0ULL, 0xFA11FE77117CDF02ULL, 0x80C98EBF2149567BULL, 0x0FA11FE77117CDF0ULL, 0x75796F2F41224489ULL, 0x3A291B04893D698DULL, 0x40F16BCCB908E0F4ULL, 0xCF99FA94E9567B7FULL, 0xB5418A5CD963F206ULL, 0x513912C379682177ULL, 0x2BE1620B495DA80EULL, 0xA489F35319033385ULL, 0xDE51839B2936BAFCULL, 0x9101F7B0E12997F8ULL, 0xEBD98778D11C1E81ULL, 0x64B116208142850AULL, 0x1E6966E8B1770C73ULL, 0x8719014C99C2B083ULL, 0xFDC17184A9F739FAULL, 0x72A9E0DCF9A9A271ULL, 0x08719014C99C2B08ULL, 0x4721E43F0183060CULL, 0x3DF994F731B68F75ULL, 0xB29105AF61E814FEULL, 0xC849756751DD9D87ULL, 0x2C31EDF8F1D64EF6ULL, 0x56E99D30C1E3C78FULL, 0xD9810C6891BD5C04ULL, 0xA3597CA0A188D57DULL, 0xEC09088B6997F879ULL, 0x96D1784359A27100ULL, 0x19B9E91B09FCEA8BULL, 0x636199D339C963F2ULL, 0xDF7ADABD7A6E2D6FULL, 0xA5A2AA754A5BA416ULL, 0x2ACA3B2D1A053F9DULL, 0x50124BE52A30B6E4ULL, 0x1F423FCEE22F9BE0ULL, 0x659A4F06D21A1299ULL, 0xEAF2DE5E82448912ULL, 0x902AAE96B271006BULL, 0x74523609127AD31AULL, 0x0E8A46C1224F5A63ULL, 0x81E2D7997211C1E8ULL, 0xFB3AA75142244891ULL, 0xB46AD37A8A3B6595ULL, 0xCEB2A3B2BA0EECECULL, 0x41DA32EAEA507767ULL, 0x3B024222DA65FE1EULL, 0xA2722586F2D042EEULL, 0xD8AA554EC2E5CB97ULL, 0x57C2C41692BB501CULL, 0x2D1AB4DEA28ED965ULL, 0x624AC0F56A91F461ULL, 0x1892B03D5AA47D18ULL, 0x97FA21650AFAE693ULL, 0xED2251AD3ACF6FEAULL, 0x095AC9329AC4BC9BULL, 0x7382B9FAAAF135E2ULL, 0xFCEA28A2FAAFAE69ULL, 0x8632586ACA9A2710ULL, 0xC9622C4102850A14ULL, 0xB3BA5C8932B0836DULL, 0x3CD2CDD162EE18E6ULL, 0x460ABD1952DB919FULL, 0x256B24CA6B12F26DULL, 0x5FB354025B277B14ULL, 0xD0DBC55A0B79E09FULL, 0xAA03B5923B4C69E6ULL, 0xE553C1B9F35344E2ULL, 0x9F8BB171C366CD9BULL, 0x10E3202993385610ULL, 0x6A3B50E1A30DDF69ULL, 0x8E43C87E03060C18ULL, 0xF49BB8B633338561ULL, 0x7BF329EE636D1EEAULL, 0x012B592653589793ULL, 0x4E7B2D0D9B47BA97ULL, 0x34A35DC5AB7233EEULL, 0xBBCBCC9DFB2CA865ULL, 0xC113BC55CB19211CULL, 0x5863DBF1E3AC9DECULL, 0x22BBAB39D3991495ULL, 0xADD33A6183C78F1EULL, 0xD70B4AA9B3F20667ULL, 0x985B3E827BED2B63ULL, 0xE2834E4A4BD8A21AULL, 0x6DEBDF121B863991ULL, 0x1733AFDA2BB3B0E8ULL, 0xF34B37458BB86399ULL, 0x8993478DBB8DEAE0ULL, 0x06FBD6D5EBD3716BULL, 0x7C23A61DDBE6F812ULL, 0x3373D23613F9D516ULL, 0x49ABA2FE23CC5C6FULL, 0xC6C333A67392C7E4ULL, 0xBC1B436E43A74E9DULL, 0x95AC9329AC4BC9B5ULL, 0xEF74E3E19C7E40CCULL, 0x601C72B9CC20DB47ULL, 0x1AC40271FC15523EULL, 0x5594765A340A7F3AULL, 0x2F4C0692043FF643ULL, 0xA02497CA54616DC8ULL, 0xDAFCE7026454E4B1ULL, 0x3E847F9DC45F37C0ULL, 0x445C0F55F46ABEB9ULL, 0xCB349E0DA4342532ULL, 0xB1ECEEC59401AC4BULL, 0xFEBC9AEE5C1E814FULL, 0x8464EA266C2B0836ULL, 0x0B0C7B7E3C7593BDULL, 0x71D40BB60C401AC4ULL, 0xE8A46C1224F5A634ULL, 0x927C1CDA14C02F4DULL, 0x1D148D82449EB4C6ULL, 0x67CCFD4A74AB3DBFULL, 0x289C8961BCB410BBULL, 0x5244F9A98C8199C2ULL, 0xDD2C68F1DCDF0249ULL, 0xA7F41839ECEA8B30ULL, 0x438C80A64CE15841ULL, 0x3954F06E7CD4D138ULL, 0xB63C61362C8A4AB3ULL, 0xCCE411FE1CBFC3CAULL, 0x83B465D5D4A0EECEULL, 0xF96C151DE49567B7ULL, 0x76048445B4CBFC3CULL, 0x0CDCF48D84FE7545ULL, 0x6FBD6D5EBD3716B7ULL, 0x15651D968D029FCEULL, 0x9A0D8CCEDD5C0445ULL, 0xE0D5FC06ED698D3CULL, 0xAF85882D2576A038ULL, 0xD55DF8E515432941ULL, 0x5A3569BD451DB2CAULL, 0x20ED197575283BB3ULL, 0xC49581EAD523E8C2ULL, 0xBE4DF122E51661BBULL, 0x3125607AB548FA30ULL, 0x4BFD10B2857D7349ULL, 0x04AD64994D625E4DULL, 0x7E7514517D57D734ULL, 0xF11D85092D094CBFULL, 0x8BC5F5C11D3CC5C6ULL, 0x12B5926535897936ULL, 0x686DE2AD05BCF04FULL, 0xE70573F555E26BC4ULL, 0x9DDD033D65D7E2BDULL, 0xD28D7716ADC8CFB9ULL, 0xA85507DE9DFD46C0ULL, 0x273D9686CDA3DD4BULL, 0x5DE5E64EFD965432ULL, 0xB99D7ED15D9D8743ULL, 0xC3450E196DA80E3AULL, 0x4C2D9F413DF695B1ULL, 0x36F5EF890DC31CC8ULL, 0x79A59BA2C5DC31CCULL, 0x037DEB6AF5E9B8B5ULL, 0x8C157A32A5B7233EULL, 0xF6CD0AFA9582AA47ULL, 0x4AD64994D625E4DAULL, 0x300E395CE6106DA3ULL, 0xBF66A804B64EF628ULL, 0xC5BED8CC867B7F51ULL, 0x8AEEACE74E645255ULL, 0xF036DC2F7E51DB2CULL, 0x7F5E4D772E0F40A7ULL, 0x05863DBF1E3AC9DEULL, 0xE1FEA520BE311AAFULL, 0x9B26D5E88E0493D6ULL, 0x144E44B0DE5A085DULL, 0x6E963478EE6F8124ULL, 0x21C640532670AC20ULL, 0x5B1E309B16452559ULL, 0xD476A1C3461BBED2ULL, 0xAEAED10B762E37ABULL, 0x37DEB6AF5E9B8B5BULL, 0x4D06C6676EAE0222ULL, 0xC26E573F3EF099A9ULL, 0xB8B627F70EC510D0ULL, 0xF7E653DCC6DA3DD4ULL, 0x8D3E2314F6EFB4ADULL, 0x0256B24CA6B12F26ULL, 0x788EC2849684A65FULL, 0x9CF65A1B368F752EULL, 0xE62E2AD306BAFC57ULL, 0x6946BB8B56E467DCULL, 0x139ECB4366D1EEA5ULL, 0x5CCEBF68AECEC3A1ULL, 0x2616CFA09EFB4AD8ULL, 0xA97E5EF8CEA5D153ULL, 0xD3A62E30FE90582AULL, 0xB0C7B7E3C7593BD8ULL, 0xCA1FC72BF76CB2A1ULL, 0x45775673A732292AULL, 0x3FAF26BB9707A053ULL, 0x70FF52905F188D57ULL, 0x0A2722586F2D042EULL, 0x854FB3003F739FA5ULL, 0xFF97C3C80F4616DCULL, 0x1BEF5B57AF4DC5ADULL, 0x61372B9F9F784CD4ULL, 0xEE5FBAC7CF26D75FULL, 0x9487CA0FFF135E26ULL, 0xDBD7BE24370C7322ULL, 0xA10FCEEC0739FA5BULL, 0x2E675FB4576761D0ULL, 0x54BF2F7C6752E8A9ULL, 0xCDCF48D84FE75459ULL, 0xB71738107FD2DD20ULL, 0x387FA9482F8C46ABULL, 0x42A7D9801FB9CFD2ULL, 0x0DF7ADABD7A6E2D6ULL, 0x772FDD63E7936BAFULL, 0xF8474C3BB7CDF024ULL, 0x829F3CF387F8795DULL, 0x66E7A46C27F3AA2CULL, 0x1C3FD4A417C62355ULL, 0x935745FC4798B8DEULL, 0xE98F353477AD31A7ULL, 0xA6DF411FBFB21CA3ULL, 0xDC0731D78F8795DAULL, 0x536FA08FDFD90E51ULL, 0x29B7D047EFEC8728ULL }; void CRC64_Init( CRC64_t *pCRC ) { *pCRC = CRC64_INIT_VALUE; } void CRC64_ProcessBuffer( CRC64_t *pCRC, const void *p, int len ) { uint8 *pBytes = (uint8*)p; CRC64_t nCRC = *pCRC; while ( len > 0 ) { nCRC = pCRC64Table[ ( nCRC ^ (*pBytes) ) & 0xff ] ^ ( nCRC >> 8 ); pBytes++; len--; } *pCRC = nCRC; } void CRC64_Final( CRC64_t *pCRC ) { *pCRC ^= CRC64_XOR_VALUE; }