forked from pig4210/xlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc.h
83 lines (66 loc) · 1.84 KB
/
crc.h
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
/*!
\file crc.h
\brief crc.h定义了crc16算法、crc32算法、crc64、crc ccitt算法
\version 5.0.1707.2418
\note For All
\author triones
\date 2013-03-19
*/
#ifndef _XLIB_CRC_H_
#define _XLIB_CRC_H_
#include <string>
#include "xlib_base.h"
/*!
生成指定数据的crc16
\param data 指定需要计算crc16的数据
\return 返回crc16值
\code
std::cout << crc16(std::string("012345"));
\endcode
*/
uint16 crc16(const void* data, const size_t size);
/*!
生成指定数据的crc32
\param data 指定需要计算crc32的数据
\return 返回crc32值
\code
std::cout << crc32(std::string("012345"));
\endcode
*/
uint32 crc32(const void* data, const size_t size);
/*!
生成指定数据的crc64
\param data 指定需要计算crc64的数据
\return 返回crc64值
\code
std::cout << crc64(std::string("012345"));
\endcode
*/
uint64 crc64(const void* data, const size_t size);
/*!
生成指定数据的crc ccitt
\param data 指定需要计算crc ccitt的数据
\return 返回crc ccitt值
\code
std::cout << crcccitt(std::string("012345"));
\endcode
*/
uint16 crcccitt(const void* data, const size_t size);
//////////////////////////////////////////////////////////////////////////
template<typename T> uint16 crc16(const std::basic_string<T>& s)
{
return crc16(s.c_str(), s.size() * sizeof(T));
}
template<typename T> uint32 crc32(const std::basic_string<T>& s)
{
return crc32(s.c_str(), s.size() * sizeof(T));
}
template<typename T> uint64 crc64(const std::basic_string<T>& s)
{
return crc64(s.c_str(), s.size() * sizeof(T));
}
template<typename T> uint16 crcccitt(const std::basic_string<T>& s)
{
return crcccitt(s.c_str(), s.size() * sizeof(T));
}
#endif // _XLIB_CRC_H_