TNB Library
TnbCrcComputer.h
[詳解]
1#pragma once
11#include "TnbStr.h"
12
13
14
15//T-TestCaseコードカバレッジDisable
16#pragma comment(user,"T-Coverage Disable")
17
18
19
20//TNB Library
21namespace TNB
22{
23
24
25
37{
38public:
40 enum
41 {
42 CRCPOLY1 = 0x1021,
43 CRCPOLY2 = 0x8408,
45 TABLE_SIZE = 256,
46 };
47
55 WORD CalcTb1(size_t size, const void* P, WORD r = 0xFFFF)
56 {
57 m_InitTable1();
58 const BYTE* B = static_cast<const BYTE*>(P);
59 loop ( i, size )
60 {
61 r = static_cast<WORD>((r << BYTE_BIT) ^ m_table1[BYTE((r >> (16 - BYTE_BIT)) ^ *B++)]);
62 }
63 return static_cast<WORD>(~r);
64 }
65
73 static WORD Calc1(size_t size, const void* P, WORD r = 0xFFFF)
74 {
75 const BYTE* B = static_cast<const BYTE*>(P);
76 loop ( i, size )
77 {
78 r ^= B[i] << (16 - BYTE_BIT);
79 loop ( j, BYTE_BIT )
80 {
81 r = static_cast<WORD>(((r & 0x8000) != 0) ? ((r << 1) ^ CRCPOLY1) : (r << 1));
82 }
83 }
84 return static_cast<WORD>(~r);
85 }
86
94 WORD CalcTb2(size_t size, const void* P, WORD r = 0xFFFF)
95 {
96 m_InitTable2();
97 const BYTE* B = static_cast<const BYTE*>(P);
98 loop ( i, size )
99 {
100 r = static_cast<WORD>((r >> BYTE_BIT) ^ m_table2[BYTE(r ^ *B++)]);
101 }
102 return static_cast<WORD>(r ^ 0xFFFF);
103 }
104
112 static WORD Calc2(size_t size, const void* P, WORD r = 0xFFFF)
113 {
114 const BYTE* B = static_cast<const BYTE*>(P);
115 loop ( i, size )
116 {
117 r ^= B[i];
118 loop ( j, BYTE_BIT )
119 {
120 r = static_cast<WORD>(((r & 1) != 0) ? ((r >> 1) ^ CRCPOLY2) : (r >> 1));
121 }
122 }
123 return static_cast<WORD>(r ^ 0xFFFF);
124 }
125
126private:
127 // テーブル作成(MSBファースト)
128 void m_InitTable1(void)
129 {
130 if ( m_table1.GetSize() == 0 )
131 {
132 m_table1.Resize(TABLE_SIZE);
133 loop ( i, TABLE_SIZE )
134 {
135 WORD r = static_cast<WORD>(i << (16 - BYTE_BIT));
136 loop ( j, BYTE_BIT )
137 {
138 r = static_cast<WORD>(((r & 0x8000) != 0) ? ((r << 1) ^ CRCPOLY1) : (r << 1));
139 }
140 m_table1[i] = r;
141 }
142 }
143 }
144 // テーブル作成(LSBファースト)
145 void m_InitTable2(void)
146 {
147 if ( m_table2.GetSize() == 0 )
148 {
149 m_table2.Resize(TABLE_SIZE);
150 loop ( i, TABLE_SIZE )
151 {
152 WORD r = static_cast<WORD>(i);
153 loop ( j, BYTE_BIT )
154 {
155 r = static_cast<WORD>(((r & 1) != 0) ? ((r >> 1) ^ CRCPOLY2) : (r >> 1));
156 }
157 m_table2[i] = r;
158 }
159 }
160 }
161 CWorkMemT<WORD> m_table1;
162 CWorkMemT<WORD> m_table2;
163};
164
165
166
178{
179public:
180
187 static WORD Calc1(size_t size, const void* P)
188 {
189 return ms_Calc(size, P, 0xA001);
190 }
191
198 static WORD Calc2(size_t size, const void* P)
199 {
200 return ms_Calc(size, P, 0x8005);
201 }
202
203private:
204 // [計算] 計算
205 static WORD ms_Calc(size_t size, const void* P, WORD r)
206 {
207 WORD crc = 0;
208 const BYTE* B = static_cast<const BYTE*>(P);
209 loop ( i, size )
210 {
211 BYTE b = B[i];
212 loop ( j, 8 )
213 {
214 crc = static_cast<WORD>(((b ^ static_cast<BYTE>(crc)) & 1) ? ((crc >> 1) ^ r) : (crc >> 1));
215 b >>= 1;
216 }
217 }
218 return crc;
219 }
220};
221
222
223
235{
236public:
237
244 DWORD CalcTb(size_t size, const void* p)
245 {
246 return CalcTb(0xffffffffL, size, p) ^ 0xffffffffL;
247 }
248
256 DWORD CalcTb(DWORD crc, size_t size, const void* p)
257 {
258 const BYTE *b = static_cast<const BYTE*>(p);
259 DWORD c = crc;
260 m_InitTable();
261 loop ( n, size )
262 {
263 c = m_table[(c ^ b[n]) & 0xff] ^ (c >> 8);
264 }
265 return c;
266 }
267
268private:
269 // テーブル作成
270 void m_InitTable(void)
271 {
272 if ( m_table.GetSize() == 0 )
273 {
274 m_table.Resize(256);
275 loop ( n, 256 )
276 {
277 DWORD c = static_cast<DWORD>(n);
278 loop ( k, 8 )
279 {
280 if ( (c & 1) != 0 )
281 {
282 c = 0xedb88320L ^ (c >> 1);
283 }
284 else
285 {
286 c >>= 1;
287 }
288 }
289 m_table[n] = c;
290 }
291 }
292 }
293 CWorkMemT<DWORD> m_table;
294};
295
296
297
298}; // TNB
299
300
301
302//T-TestCaseコードカバレッジEnable
303#pragma comment(user,"T-Coverage Enable")
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
文字列管理関係のヘッダ
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
CRC-16-IBM(Cyclic Redundancy Check 16bit)計算クラス.
static WORD Calc1(size_t size, const void *P)
[計算] 計算(0xA001)
static WORD Calc2(size_t size, const void *P)
[計算] 計算(0x8005)
CRC-16-CCITT(Cyclic Redundancy Check 16bit)計算クラス.
static WORD Calc2(size_t size, const void *P, WORD r=0xFFFF)
[計算] 計算(LSBファースト)
static WORD Calc1(size_t size, const void *P, WORD r=0xFFFF)
[計算] 計算(MSBファースト)
WORD CalcTb2(size_t size, const void *P, WORD r=0xFFFF)
[計算] テーブル計算(LSBファースト)
@ CRCPOLY1
MSB ファースト定数
@ TABLE_SIZE
テーブルサイズ
@ BYTE_BIT
BYTE の bit数
@ CRCPOLY2
LSB ファースト 定数
WORD CalcTb1(size_t size, const void *P, WORD r=0xFFFF)
[計算] テーブル計算(MSBファースト)
CRC32(Cyclic Redundancy Check 32bit)計算クラス.
DWORD CalcTb(size_t size, const void *p)
[計算] テーブル計算
DWORD CalcTb(DWORD crc, size_t size, const void *p)
[計算] テーブル計算
size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbDef.h:665
void Resize(size_t l)
[設定] サイズ再設定
Definition: TnbDef.h:672
TNB Library
Definition: TnbDoxyTitle.txt:2