Code::Blocks  SVN r11506
crc32.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 10769 $
6  * $Id: crc32.cpp 10769 2016-02-06 14:26:58Z mortenmacfly $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/crc32.cpp $
8  */
9 
10 /*
11  This code was taken from:
12  http://wikisource.org/wiki/CRC32_Checksum_function
13  by an unknown author...
14 
15  I just changed the function names to match the conventions used
16  in the rest of the project.
17 
18  Yiannis Mandravellos <mandrav@codeblocks.org>
19 */
20 
21 #include <wx/string.h>
22 #include <wx/file.h>
23 #include "crc32.h"
24 #include "globals.h"
25 
26 static wxUint32 *GetCRC32Table( wxUint32 *crc_table )
27 {
28  // First call to this function? Or after freeing the memory?
29  if ( !crc_table )
30  {
31  // Allocate memory
32  crc_table = new wxUint32[256];
33 
34  // Was the allocation successful?
35  if ( crc_table )
36  {
37  // Generate the crc table
38  for(unsigned int i = 0; i < 256; ++i)
39  {
40  wxUint32 crc = i;
41  for (unsigned int j = 8; j > 0; --j)
42  {
43  if (crc & 1) crc = (crc >> 1) ^ 0xEDB88320UL;
44  else crc >>= 1;
45  }
46  crc_table[i] = crc;
47  }
48  }
49  }
50 
51  // Return the new pointer
52  return ( crc_table ) ;
53 }
54 
56 {
57  wxFile f(file);
58  wxString contents = cbReadFileContents(f);
59  if (contents.IsEmpty())
60  return 0;
61  return FromString(contents);
62 }
63 
65 {
66  static wxUint32 *crc_table = NULL;
67  wxUint32 crc = 0;
68  unsigned int i = 0;
69 
70  if (!text.IsEmpty())
71  {
72  // Get the crc table, on first call, generate, otherwise do nothing
73  crc_table = GetCRC32Table( crc_table ) ;
74 
75  // Do we have a crc table?
76  if ( crc_table )
77  {
78  // Calculate the checksum
79  crc = 0xFFFFFFFFUL;
80  while (text[i])
81  #if wxCHECK_VERSION(3, 0, 0)
82  { crc = (crc>>8) ^ crc_table[ (crc^(text[i++].GetValue())) & 0xFF ]; }
83  #else
84  { crc = (crc>>8) ^ crc_table[ (crc^(text[i++])) & 0xFF ]; }
85  #endif
86 
87  crc ^= 0xFFFFFFFFUL ;
88  }
89  }
90 
91  // If we have a crc table, delete it from memory
92  if ( crc_table ) { delete[] crc_table; }
93 
94  // Set it to a null pointer, the have it (re)created on next calls to this
95  // function
96  crc_table = NULL;
97 
98  // Return the checksum result
99  return( crc ) ;
100 }
static wxUint32 * GetCRC32Table(wxUint32 *crc_table)
Definition: crc32.cpp:26
DLLIMPORT wxString cbReadFileContents(wxFile &file, wxFontEncoding encoding=wxFONTENCODING_SYSTEM)
Reads a wxString from a non-unicode file. File must be open. File is closed automatically.
Definition: globals.cpp:697
DLLIMPORT wxUint32 FromString(const wxString &text)
Definition: crc32.cpp:64
unsigned int wxUint32
bool IsEmpty() const
DLLIMPORT wxUint32 FromFile(const wxString &filename)
Definition: crc32.cpp:55
#define NULL
Definition: prefix.cpp:59