ARC SDK
Main Page
Related Pages
Modules
Namespaces
Data Structures
Files
File List
Globals
svn-area
arc1-clean
src
hed
libs
common
CheckSum.h
1
// -*- indent-tabs-mode: nil -*-
2
3
#ifndef __ARC_CHECKSUM_H__
4
#define __ARC_CHECKSUM_H__
5
6
#include <cstring>
7
#include <cstdio>
8
#include <string>
9
10
#include <inttypes.h>
11
#include <sys/types.h>
12
#include <zlib.h>
13
14
namespace
Arc {
15
17
30
class
CheckSum
{
31
public
:
33
CheckSum
(
void
) {}
34
virtual
~
CheckSum
(
void
) {}
35
37
41
virtual
void
start
(
void
) = 0;
42
44
51
virtual
void
add
(
void
*buf,
unsigned
long
long
int
len) = 0;
52
54
58
virtual
void
end
(
void
) = 0;
59
61
virtual
void
result
(
unsigned
char
*& res,
unsigned
int
& len)
const
= 0;
62
64
76
virtual
int
print
(
char
*buf,
int
len)
const
{
77
if
(len > 0)
78
buf[0] = 0;
79
return
0;
80
}
81
83
91
virtual
void
scan
(
const
char
*buf) = 0;
92
94
virtual
operator
bool(
void
)
const
{
95
return
false
;
96
}
97
99
virtual
bool
operator!
(
void
)
const
{
100
return
true
;
101
}
102
};
103
105
111
class
CRC32Sum
112
:
public
CheckSum
{
113
private
:
114
uint32_t r;
115
unsigned
long
long
count;
116
bool
computed;
117
public
:
118
CRC32Sum
(
void
);
119
virtual
~
CRC32Sum
(
void
) {}
120
virtual
void
start
(
void
);
121
virtual
void
add
(
void
*buf,
unsigned
long
long
int
len);
122
virtual
void
end
(
void
);
123
virtual
void
result
(
unsigned
char
*& res,
unsigned
int
& len)
const
{
124
res = (
unsigned
char
*)&r;
125
len = 4;
126
}
127
virtual
int
print
(
char
*buf,
int
len)
const
;
128
virtual
void
scan
(
const
char
*buf);
129
virtual
operator
bool(
void
)
const
{
130
return
computed;
131
}
132
virtual
bool
operator!
(
void
)
const
{
133
return
!computed;
134
}
135
uint32_t crc(
void
)
const
{
136
return
r;
137
}
138
};
139
141
148
class
MD5Sum
149
:
public
CheckSum
{
150
private
:
151
bool
computed;
152
uint32_t A;
153
uint32_t B;
154
uint32_t C;
155
uint32_t D;
156
uint64_t count;
157
uint32_t X[16];
158
unsigned
int
Xlen;
159
// uint32_t T[64];
160
public
:
161
MD5Sum
(
void
);
162
virtual
void
start
(
void
);
163
virtual
void
add
(
void
*buf,
unsigned
long
long
int
len);
164
virtual
void
end
(
void
);
165
virtual
void
result
(
unsigned
char
*& res,
unsigned
int
& len)
const
{
166
res = (
unsigned
char
*)&A;
167
len = 16;
168
}
169
virtual
int
print
(
char
*buf,
int
len)
const
;
170
virtual
void
scan
(
const
char
*buf);
171
virtual
operator
bool(
void
)
const
{
172
return
computed;
173
}
174
virtual
bool
operator!
(
void
)
const
{
175
return
!computed;
176
}
177
};
178
180
186
class
Adler32Sum
187
:
public
CheckSum
{
188
private
:
189
uLong adler;
190
bool
computed;
191
public
:
192
Adler32Sum
(
void
) : computed(
false
) {
193
start
();
194
}
195
virtual
void
start
(
void
) {
196
adler = adler32(0L, Z_NULL, 0);
197
}
198
virtual
void
add
(
void
* buf,
unsigned
long
long
int
len) {
199
adler = adler32(adler, (
const
Bytef *)buf, len);
200
}
201
virtual
void
end
(
void
) {
202
computed =
true
;
203
}
204
virtual
void
result
(
unsigned
char
*& res,
unsigned
int
& len)
const
{
205
res=(
unsigned
char
*)&adler;
206
len=4;
207
}
208
virtual
int
print
(
char
* buf,
int
len)
const
{
209
if
(!computed) {
210
if
(len>0) {
211
buf[0]=0;
212
return
0;
213
}
214
}
215
return
snprintf(buf,len,
"adler32:%08lx"
,adler);
216
};
217
virtual
void
scan
(
const
char
*
/* buf */
) { };
218
virtual
operator
bool(
void
)
const
{
219
return
computed;
220
}
221
virtual
bool
operator!
(
void
)
const
{
222
return
!computed;
223
}
224
};
225
227
233
class
CheckSumAny
234
:
public
CheckSum
{
235
public
:
237
typedef
enum
{
238
none
,
239
unknown
,
240
undefined
,
241
cksum
,
242
md5
,
243
adler32
244
}
type
;
245
private
:
246
CheckSum
*cs;
247
type
tp;
248
public
:
250
CheckSumAny
(
CheckSum
*c = NULL)
251
: cs(c),
252
tp(
none
) {}
254
CheckSumAny
(
type
type
);
256
CheckSumAny
(
const
char
*
type
);
257
virtual
~
CheckSumAny
(
void
) {
258
if
(cs)
259
delete
cs;
260
}
261
virtual
void
start
(
void
) {
262
if
(cs)
263
cs->
start
();
264
}
265
virtual
void
add
(
void
*buf,
unsigned
long
long
int
len) {
266
if
(cs)
267
cs->
add
(buf, len);
268
}
269
virtual
void
end
(
void
) {
270
if
(cs)
271
cs->
end
();
272
}
273
virtual
void
result
(
unsigned
char
*& res,
unsigned
int
& len)
const
{
274
if
(cs) {
275
cs->
result
(res, len);
276
return
;
277
}
278
len = 0;
279
}
280
virtual
int
print
(
char
*buf,
int
len)
const
{
281
if
(cs)
282
return
cs->
print
(buf, len);
283
if
(len > 0)
284
buf[0] = 0;
285
return
0;
286
}
287
virtual
void
scan
(
const
char
*buf) {
288
if
(cs)
289
cs->
scan
(buf);
290
}
291
virtual
operator
bool(
void
)
const
{
292
if
(!cs)
293
return
false
;
294
return
*cs;
295
}
296
virtual
bool
operator!
(
void
)
const
{
297
if
(!cs)
298
return
true
;
299
return
!(*cs);
300
}
301
bool
active(
void
) {
302
return
(cs != NULL);
303
}
304
static
type
Type(
const
char
*crc);
305
type
Type(
void
)
const
{
306
return
tp;
307
}
308
void
operator=(
const
char
*
type
);
309
bool
operator==(
const
char
*s);
310
bool
operator==(
const
CheckSumAny
& ck);
311
313
324
static
std::string
FileChecksum
(
const
std::string& filepath,
type
tp =
md5
,
bool
decimalbase =
false
);
325
};
326
327
}
// namespace Arc
328
329
#endif // __ARC_CHECKSUM_H__
Generated on Fri Jul 5 2013 14:37:45 for ARC SDK by
1.8.3.1-20130209