Menu

[r1967]: / trunk / src / sdk / crc32.cpp  Maximize  Restore  History

Download this file

128 lines (111 with data), 3.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
This code was taken from:
http://wikisource.org/wiki/CRC32_Checksum_function
by an unknown author...
I just changed the function names to match the conventions used
in the rest of the project.
Yiannis Mandravellos <mandrav@codeblocks.org>
*/
#include "crc32.h"
#include <stdio.h>
static wxUint32 *GetCRC32Table( wxUint32 *crc_table )
{
// First call to this function? Or after freeing the memory?
if ( !crc_table )
{
// Allocate memory
crc_table = new wxUint32[256];
// Was the allocation succesfull?
if ( crc_table )
{
// Generate the crc table
wxUint32 crc ;
unsigned int i, j ;
for(i = 0; i < 256; i++)
{
crc = i;
for (j = 8; j > 0; j--)
{
if (crc & 1) crc = (crc >> 1) ^ 0xEDB88320UL;
else crc >>= 1;
}
crc_table[i] = crc;
}
}
}
// Return the new pointer
return ( crc_table ) ;
}
wxUint32 wxCrc32::FromFile(const wxString& file){return 0uL;};
/* TODO (thomas#1#): Reimplement this by reading in the complete file and calling FromString (function is currently unused anyway) */
//unsigned long wxCrc32::FromFile(const wxString& file)
//{
// static unsigned long *crc_table = NULL;
// unsigned long crc = 0;
//
// if (file)
// {
// // Get the crc table, on first call, generate, otherwise do nothing
// crc_table = GetCRC32Table( crc_table ) ;
//
// // Do we have a crc table?
// if ( crc_table )
// {
// // Open the file for reading
// FILE *fp = fopen(file.mb_str(wxConvUTF8), "r");
//
// // Was the file open succesfull?
// if (fp)
// {
// // Calculate the checksum
// int ch;
//
// crc = 0xFFFFFFFFUL;
// while ((ch = getc(fp)) != EOF)
// { crc = (crc>>8) ^ crc_table[ (crc^ch) & 0xFF ]; }
//
// crc ^= 0xFFFFFFFFUL ;
//
// // Close the file
// fclose(fp);
// }
// }
// }
//
// // If we have a crc table, delete it from memory
// if ( crc_table ) { delete[] crc_table; }
//
// // Set it to a null pointer, the have it (re)created on next calls to this
// // function
// crc_table = NULL;
//
// // Return the checksum result
// return( crc ) ;
//}
wxUint32 wxCrc32::FromString(const wxString& text)
{
static wxUint32 *crc_table = NULL;
wxUint32 crc = 0;
unsigned int i = 0;
if (text)
{
// Get the crc table, on first call, generate, otherwise do nothing
crc_table = GetCRC32Table( crc_table ) ;
// Do we have a crc table?
if ( crc_table )
{
// Calculate the checksum
crc = 0xFFFFFFFFUL;
while (text[i])
{ crc = (crc>>8) ^ crc_table[ (crc^(text[i++])) & 0xFF ]; }
crc ^= 0xFFFFFFFFUL ;
}
}
// If we have a crc table, delete it from memory
if ( crc_table ) { delete[] crc_table; }
// Set it to a null pointer, the have it (re)created on next calls to this
// function
crc_table = NULL;
// Return the checksum result
return( crc ) ;
}