Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

/ray/src/lib/crypto/crc32hash.cc

Go to the documentation of this file.
00001 /*
00002  * lib/crypto/crc32hash.cc
00003  * 
00004  * Implementation of class CRC32Hash, a class for computation of 
00005  * the CRC32 checksum (POSIX.2) which is not a cryptographically secure 
00006  * hash algorithm. 
00007  * 
00008  * Copyright (c) 2004 by Wolfgang Wieser ] wwieser (a) gmx <*> de [ 
00009  * 
00010  * This code is based on the algorithm found in the cksum(1) program 
00011  * bundeled with the GNU coreutils, written by 
00012  * Q. Frank Xia, qx@math.columbia.edu
00013  * 
00014  * This file may be distributed and/or modified under the terms of the 
00015  * GNU General Public License version 2 as published by the Free Software 
00016  * Foundation. (See COPYING.GPL for details.)
00017  * 
00018  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00019  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00020  * 
00021  */
00022 
00023 #include "crc32hash.h"
00024 
00025 
00026 // Static data initialisation: 
00027 const SecureHashBase::Parameters CRC32Hash::par=
00028 {
00029     INIT_FIELD(hash_size) 4,
00030     INIT_FIELD(block_size) 4,
00031     INIT_FIELD(hash_ID) 0x3000,
00032     INIT_FIELD(name) "CRC32"
00033 };
00034 
00035 const uint32 CRC32Hash::crc32table[256]=
00036 {
00037     0x00000000U,
00038     0x04c11db7U, 0x09823b6eU, 0x0d4326d9U, 0x130476dcU, 0x17c56b6bU,
00039     0x1a864db2U, 0x1e475005U, 0x2608edb8U, 0x22c9f00fU, 0x2f8ad6d6U,
00040     0x2b4bcb61U, 0x350c9b64U, 0x31cd86d3U, 0x3c8ea00aU, 0x384fbdbdU,
00041     0x4c11db70U, 0x48d0c6c7U, 0x4593e01eU, 0x4152fda9U, 0x5f15adacU,
00042     0x5bd4b01bU, 0x569796c2U, 0x52568b75U, 0x6a1936c8U, 0x6ed82b7fU,
00043     0x639b0da6U, 0x675a1011U, 0x791d4014U, 0x7ddc5da3U, 0x709f7b7aU,
00044     0x745e66cdU, 0x9823b6e0U, 0x9ce2ab57U, 0x91a18d8eU, 0x95609039U,
00045     0x8b27c03cU, 0x8fe6dd8bU, 0x82a5fb52U, 0x8664e6e5U, 0xbe2b5b58U,
00046     0xbaea46efU, 0xb7a96036U, 0xb3687d81U, 0xad2f2d84U, 0xa9ee3033U,
00047     0xa4ad16eaU, 0xa06c0b5dU, 0xd4326d90U, 0xd0f37027U, 0xddb056feU,
00048     0xd9714b49U, 0xc7361b4cU, 0xc3f706fbU, 0xceb42022U, 0xca753d95U,
00049     0xf23a8028U, 0xf6fb9d9fU, 0xfbb8bb46U, 0xff79a6f1U, 0xe13ef6f4U,
00050     0xe5ffeb43U, 0xe8bccd9aU, 0xec7dd02dU, 0x34867077U, 0x30476dc0U,
00051     0x3d044b19U, 0x39c556aeU, 0x278206abU, 0x23431b1cU, 0x2e003dc5U,
00052     0x2ac12072U, 0x128e9dcfU, 0x164f8078U, 0x1b0ca6a1U, 0x1fcdbb16U,
00053     0x018aeb13U, 0x054bf6a4U, 0x0808d07dU, 0x0cc9cdcaU, 0x7897ab07U,
00054     0x7c56b6b0U, 0x71159069U, 0x75d48ddeU, 0x6b93dddbU, 0x6f52c06cU,
00055     0x6211e6b5U, 0x66d0fb02U, 0x5e9f46bfU, 0x5a5e5b08U, 0x571d7dd1U,
00056     0x53dc6066U, 0x4d9b3063U, 0x495a2dd4U, 0x44190b0dU, 0x40d816baU,
00057     0xaca5c697U, 0xa864db20U, 0xa527fdf9U, 0xa1e6e04eU, 0xbfa1b04bU,
00058     0xbb60adfcU, 0xb6238b25U, 0xb2e29692U, 0x8aad2b2fU, 0x8e6c3698U,
00059     0x832f1041U, 0x87ee0df6U, 0x99a95df3U, 0x9d684044U, 0x902b669dU,
00060     0x94ea7b2aU, 0xe0b41de7U, 0xe4750050U, 0xe9362689U, 0xedf73b3eU,
00061     0xf3b06b3bU, 0xf771768cU, 0xfa325055U, 0xfef34de2U, 0xc6bcf05fU,
00062     0xc27dede8U, 0xcf3ecb31U, 0xcbffd686U, 0xd5b88683U, 0xd1799b34U,
00063     0xdc3abdedU, 0xd8fba05aU, 0x690ce0eeU, 0x6dcdfd59U, 0x608edb80U,
00064     0x644fc637U, 0x7a089632U, 0x7ec98b85U, 0x738aad5cU, 0x774bb0ebU,
00065     0x4f040d56U, 0x4bc510e1U, 0x46863638U, 0x42472b8fU, 0x5c007b8aU,
00066     0x58c1663dU, 0x558240e4U, 0x51435d53U, 0x251d3b9eU, 0x21dc2629U,
00067     0x2c9f00f0U, 0x285e1d47U, 0x36194d42U, 0x32d850f5U, 0x3f9b762cU,
00068     0x3b5a6b9bU, 0x0315d626U, 0x07d4cb91U, 0x0a97ed48U, 0x0e56f0ffU,
00069     0x1011a0faU, 0x14d0bd4dU, 0x19939b94U, 0x1d528623U, 0xf12f560eU,
00070     0xf5ee4bb9U, 0xf8ad6d60U, 0xfc6c70d7U, 0xe22b20d2U, 0xe6ea3d65U,
00071     0xeba91bbcU, 0xef68060bU, 0xd727bbb6U, 0xd3e6a601U, 0xdea580d8U,
00072     0xda649d6fU, 0xc423cd6aU, 0xc0e2d0ddU, 0xcda1f604U, 0xc960ebb3U,
00073     0xbd3e8d7eU, 0xb9ff90c9U, 0xb4bcb610U, 0xb07daba7U, 0xae3afba2U,
00074     0xaafbe615U, 0xa7b8c0ccU, 0xa379dd7bU, 0x9b3660c6U, 0x9ff77d71U,
00075     0x92b45ba8U, 0x9675461fU, 0x8832161aU, 0x8cf30badU, 0x81b02d74U,
00076     0x857130c3U, 0x5d8a9099U, 0x594b8d2eU, 0x5408abf7U, 0x50c9b640U,
00077     0x4e8ee645U, 0x4a4ffbf2U, 0x470cdd2bU, 0x43cdc09cU, 0x7b827d21U,
00078     0x7f436096U, 0x7200464fU, 0x76c15bf8U, 0x68860bfdU, 0x6c47164aU,
00079     0x61043093U, 0x65c52d24U, 0x119b4be9U, 0x155a565eU, 0x18197087U,
00080     0x1cd86d30U, 0x029f3d35U, 0x065e2082U, 0x0b1d065bU, 0x0fdc1becU,
00081     0x3793a651U, 0x3352bbe6U, 0x3e119d3fU, 0x3ad08088U, 0x2497d08dU,
00082     0x2056cd3aU, 0x2d15ebe3U, 0x29d4f654U, 0xc5a92679U, 0xc1683bceU,
00083     0xcc2b1d17U, 0xc8ea00a0U, 0xd6ad50a5U, 0xd26c4d12U, 0xdf2f6bcbU,
00084     0xdbee767cU, 0xe3a1cbc1U, 0xe760d676U, 0xea23f0afU, 0xeee2ed18U,
00085     0xf0a5bd1dU, 0xf464a0aaU, 0xf9278673U, 0xfde69bc4U, 0x89b8fd09U,
00086     0x8d79e0beU, 0x803ac667U, 0x84fbdbd0U, 0x9abc8bd5U, 0x9e7d9662U,
00087     0x933eb0bbU, 0x97ffad0cU, 0xafb010b1U, 0xab710d06U, 0xa6322bdfU,
00088     0xa2f33668U, 0xbcb4666dU, 0xb8757bdaU, 0xb5365d03U, 0xb1f740b4U
00089 };
00090 
00091 
00092 #if 0
00093 // This could be used to generate the table above. (Untested!) 
00094 // See coreutils / cksum.c for more information. 
00095 char CRC32Hash::_GenTable()
00096 {
00097     for(uint32 i=0; i<256; i++)
00098     {
00099         uint32 crc=i;
00100         
00101         for(int j=0; j<8; j++)
00102         {
00103             crc >>= 1;
00104             if(crc & 1U)  crc ^= 0xedb88320U;
00105         }
00106         
00107         crc32table[i] = crc;
00108     }
00109     
00110     return(1);
00111 }
00112 #endif
00113 
00114 
00115 void CRC32Hash::GetHash(char *buf) const
00116 {
00117     register unsigned char *hsh=(unsigned char*)buf+3;
00118     register uint32 j=state;
00119     *(hsh--)=(unsigned char)(j & 0xffU);  j>>=8;
00120     *(hsh--)=(unsigned char)(j & 0xffU);  j>>=8;
00121     *(hsh--)=(unsigned char)(j & 0xffU);  j>>=8;
00122     *(hsh  )=(unsigned char)(j & 0xffU);
00123 }
00124 
00125 
00126 void CRC32Hash::reset()
00127 {
00128     length=0LLU;
00129     state=0U;
00130     
00131     // Make sure we initialized. 
00132     Assert(crc32table[1]!=0);
00133 }
00134 
00135 
00136 void CRC32Hash::feed(const char *_buf,size_t len)
00137 {
00138     const unsigned char *buf=(const unsigned char*)_buf;
00139     register uint32 s=state;
00140     for(const unsigned char *bend=buf+len; buf<bend; buf++)
00141     {  s = (s<<8) ^ crc32table[ ((unsigned char)(s>>24))^*buf ];  }
00142     state=s;
00143     
00144     length+=len;
00145 }
00146 
00147 
00148 void CRC32Hash::final()
00149 {
00150     uint64 bytes_read=length;
00151     
00152     while(bytes_read)
00153     {
00154         unsigned char val = (unsigned char)(bytes_read & 0xffU);
00155         state = (state<<8) ^ crc32table[ ((unsigned char)(state>>24)) ^ val ];
00156         bytes_read >>= 8;
00157     }
00158     
00159     state = ~state & 0xffffffffU;  // 0xffffffff is unneeded (optimized away)
00160 }

Generated on Sat Feb 19 22:33:45 2005 for Ray by doxygen 1.3.5