PageRenderTime 36ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/CryEngine/CryCommon/crc32.h

http://project-o.googlecode.com/
C Header | 165 lines | 98 code | 33 blank | 34 comment | 6 complexity | 659037cb8e751c1b83f8993d8bf6975a MD5 | raw file
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Crytek Engine Source File.
  4. // Copyright (C), Crytek Studios, 2002.
  5. // -------------------------------------------------------------------------
  6. // File name: crc32.h
  7. // Version: v1.00
  8. // Created: 31/10/2002 by Timur.
  9. // Compilers: Visual Studio.NET
  10. // Description:
  11. // -------------------------------------------------------------------------
  12. // History:
  13. //
  14. ////////////////////////////////////////////////////////////////////////////
  15. #ifndef __crc32_h__
  16. #define __crc32_h__
  17. #if _MSC_VER > 1000
  18. #pragma once
  19. #endif
  20. class Crc32Gen
  21. {
  22. public:
  23. Crc32Gen();
  24. void Init() {
  25. if (!m_bInited)
  26. {
  27. m_bInited = true;
  28. init_CRC32_Table();
  29. }
  30. }
  31. //! Creates a CRC from a text string
  32. unsigned int GetCRC32( const char *text ) const;
  33. unsigned int GetCRC32( const char *data,int size,unsigned int ulCRC ) const;
  34. unsigned int GetCRC32Lowercase( const char *text ) const;
  35. unsigned int GetCRC32Lowercase( const char *data,int size,unsigned int ulCRC ) const;
  36. protected:
  37. unsigned int crc32_table[256]; //!< Lookup table array
  38. bool m_bInited;
  39. void init_CRC32_Table(); //!< Builds lookup table array
  40. unsigned int reflect( unsigned int ref, char ch); //!< Reflects CRC bits in the lookup table
  41. unsigned int get_CRC32( const char *data,int size,unsigned int ulCRC ) const;
  42. unsigned int get_CRC32Lowercase( const char *data,int size,unsigned int ulCRC ) const;
  43. } _ALIGN(128);
  44. inline unsigned int Crc32Gen::GetCRC32( const char *text ) const
  45. {
  46. int len = strlen(text);
  47. return GetCRC32( text,len,0xffffffff );
  48. }
  49. inline unsigned int Crc32Gen::GetCRC32( const char *data, int size, unsigned int ulCRC ) const
  50. {
  51. return get_CRC32( data,size,ulCRC );
  52. }
  53. inline unsigned int Crc32Gen::GetCRC32Lowercase( const char *text ) const
  54. {
  55. int len = strlen(text);
  56. return GetCRC32Lowercase( text,len,0xffffffff );
  57. }
  58. inline unsigned int Crc32Gen::GetCRC32Lowercase( const char *data, int size, unsigned int ulCRC ) const
  59. {
  60. return get_CRC32Lowercase( data,size,ulCRC );
  61. }
  62. #define _ascii_tolower(c) ( (((c) >= L'A') && ((c) <= L'Z')) ? ((c) - L'A' + L'a') : (c) )
  63. inline unsigned int Crc32Gen::get_CRC32Lowercase( const char *data,int size,unsigned int ulCRC ) const
  64. {
  65. int len;
  66. unsigned char* buffer;
  67. // Get the length.
  68. len = size;
  69. // Save the text in the buffer.
  70. buffer = (unsigned char*)data;
  71. // Perform the algorithm on each character in the string, using the lookup table values.
  72. while(len--)
  73. {
  74. unsigned char c = *buffer++;
  75. ulCRC = (ulCRC >> 8) ^ crc32_table[(ulCRC & 0xFF) ^ _ascii_tolower(c)];
  76. }
  77. // Exclusive OR the result with the beginning value.
  78. return ulCRC ^ 0xffffffff;
  79. }
  80. inline unsigned int Crc32Gen::get_CRC32( const char *data, int size, unsigned int ulCRC ) const
  81. {
  82. // Pass a text string to this function and it will return the CRC.
  83. // Once the lookup table has been filled in by the two functions above,
  84. // this function creates all CRCs using only the lookup table.
  85. // Be sure to use unsigned variables, because negative values introduce high bits where zero bits are required.
  86. // Start out with all bits set high.
  87. int len;
  88. unsigned char* buffer;
  89. // Get the length.
  90. len = size;
  91. // Save the text in the buffer.
  92. buffer = (unsigned char*)data;
  93. // Perform the algorithm on each character in the string, using the lookup table values.
  94. while(len--)
  95. ulCRC = (ulCRC >> 8) ^ crc32_table[(ulCRC & 0xFF) ^ *buffer++];
  96. // Exclusive OR the result with the beginning value.
  97. return ulCRC ^ 0xffffffff;
  98. }
  99. inline Crc32Gen::Crc32Gen()
  100. {
  101. m_bInited = false;
  102. Init();
  103. }
  104. inline unsigned int Crc32Gen::reflect(unsigned int ref, char ch)
  105. {// Used only by Init_CRC32_Table().
  106. unsigned int value = 0;
  107. // Swap bit 0 for bit 7
  108. // bit 1 for bit 6, etc.
  109. for(int i = 1; i < (ch + 1); i++)
  110. {
  111. if(ref & 1)
  112. value |= 1 << (ch - i);
  113. ref >>= 1;
  114. }
  115. return value;
  116. }
  117. // Call this function only once to initialize the CRC table.
  118. inline void Crc32Gen::init_CRC32_Table()
  119. {
  120. // This is the official polynomial used by CRC-32
  121. // in PKZip, WinZip and Ethernet.
  122. unsigned int ulPolynomial = 0x04c11db7;
  123. // 256 values representing ASCII character codes.
  124. for(int i = 0; i <= 0xFF; i++)
  125. {
  126. crc32_table[i] = reflect(i, 8) << 24;
  127. for (int j = 0; j < 8; j++)
  128. crc32_table[i] = (crc32_table[i] << 1) ^ (crc32_table[i] & (1U << 31) ? ulPolynomial : 0);
  129. crc32_table[i] = reflect(crc32_table[i], 32);
  130. }
  131. }
  132. #endif // __crc32_h__