-
Notifications
You must be signed in to change notification settings - Fork 5
/
CRC32Ex.cpp.bak
149 lines (120 loc) · 2.79 KB
/
CRC32Ex.cpp.bak
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright 2006 - 2008, All Rights Reserved.
CRC32类之扩展
作者 - 张鲁夺(zhangluduo)
MSN - [email protected]
QQ群 - 34064264
为所有爱我的人和我爱的人努力!
*/
#include "CRC32Ex.h"
CRC32Ex::CRC32Ex()
{
}
CRC32Ex::~CRC32Ex()
{
}
//void CRC32Ex::OnFileProcessing(int nProgress, MemberFxn addr)
//{
// void* pThis = addr.GetThis();
// unsigned long Addr = addr.GetAddr();
// /** the Calling Convention must be __stdcall or
// thiscall (thiscall not a keyword),
// thiscall is the default calling convention used by C++ member functions
// */
// __asm
// {
// push nProgress ;
// mov ecx, pThis ;
// call Addr ;
// }
//}
void CRC32Ex::StopCalcFile()
{
m_bTerminate = true;
}
string CRC32Ex::GetError()
{
return m_strErr;
}
//string CRC32Ex::GetCRC32FromFile(char* FileName, bool bUpper, MemberFxn addr)
//{
// FILE *file = 0;
// unsigned char buffer[BUFFER_SIZE] = { 0 };
// m_bTerminate = false;
// m_strErr = "";
// try
// {
// if ((file = fopen (FileName, "rb")) == NULL)
// {
// m_strErr = "The file can't be opened!";
// return "";
// }
// else
// {
// int len = 0;
// int ReadCount = 0;
// unsigned long nFileSize = 0;
// // 获取文件大小, 以下方式只能获取小于2^32字节的文件大小
// {
// HANDLE hFile = CreateFile(FileName, NULL, FILE_SHARE_READ, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, 0);
// if(hFile== NULL)
// {
// m_strErr = "Get file size failed!";
// return "";
// }
// nFileSize=::GetFileSize(hFile, NULL);
// CloseHandle(hFile);
// }
// if(!addr.IsNull())
// OnFileProcessing(0, addr);
// unsigned long CRCResult;
// CRCInit(CRCResult);
// while (len = fread (buffer, 1, BUFFER_SIZE, file))
// {
// ReadCount++;
// if(m_bTerminate)
// {
// m_strErr = "User terminate calculate!";
// return "";
// }
// CRCUpdate((unsigned char *)buffer, len, CRCResult);
// if(!addr.IsNull())
// {
// int n = (int)(nFileSize / 100);
// if(n <= 0)
// n = 1;
// OnFileProcessing((int)(BUFFER_SIZE * ReadCount / n), addr);
// }
// memset(buffer, 0, BUFFER_SIZE);
// }
// CRCFinal(CRCResult);
// if(!addr.IsNull())
// OnFileProcessing(100, addr);
// fclose (file);
// return ResultToHex(CRCResult, bUpper);
// }
// }
// catch(...)
// {
// m_strErr = "UnKnow error!";
// return "";
// }
//}
string CRC32Ex::ResultToHex(unsigned long Result, bool bUpper)
{
char buf[10] = { 0 };
if(bUpper)
sprintf(buf, "%.8X", Result);
else
sprintf(buf, "%.8x", Result);
return string(buf);
}
string CRC32Ex::GetCRC32FromBuffer (unsigned char* pData, unsigned long DataLen, bool bUpper)
{
m_strErr = "";
unsigned long CRCResult;
CRCInit(CRCResult);
CRCUpdate((char*)pData, DataLen, CRCResult);
CRCFinal(CRCResult);
return ResultToHex(CRCResult, bUpper);
}