- メインページ
- モジュール
- ネームスペース
- データ構造
- ファイル

- ファイル一覧
- グローバル

crc.h
説明を見る。
1 //
2 // Copyright (c) 2003-2011, MIST Project, Nagoya University
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the Nagoya University nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28
33 #ifndef __INCLUDE_CRC__
34 #define __INCLUDE_CRC__
35
36 #ifndef __INCLUDE_MIST_CONF_H__
37 #include "config/mist_conf.h"
38 #endif
39
40 // mist名前空間の始まり
41 _MIST_BEGIN
42
43
44 namespace crc_generator_polynominal
45 {
46
47 template < int BIT > struct crc_default
48 {
49 _MIST_CONST( size_t, gen_poly, 0 );
50 };
51
52 template < > struct crc_default< 8 >
53 {
54 _MIST_CONST( size_t, gen_poly, 0x07 );
55 };
56
57 template < > struct crc_default< 16 >
58 {
59 _MIST_CONST( size_t, gen_poly, 0x8005 );
60 };
61
62 template < > struct crc_default< 32 >
63 {
64 _MIST_CONST( size_t, gen_poly, 0x04c11db7 );
65 };
66 }
67
68
70 template < int BIT >
71 struct crc
72 {
73
82 static size_t generate( const unsigned char * pdata, size_t len, size_t gen_poly = crc_generator_polynominal::crc_default< BIT >::gen_poly )
83 {
84 size_t crc = 0;
85 size_t mask;
86 size_t n, m;
87 unsigned char data;
88
89 mask = 1 << ( BIT - 1 );
90
91 for( n = 0 ; n < len ; n++ )
92 {
93 data = pdata[ n ];
94
95 for( m = 0 ; m < 8 ; m++ )
96 {
97 if( ( crc & mask ) != 0 )
98 {
99 crc <<= 1;
100 crc ^= gen_poly;
101 }
102 else
103 {
104 crc <<= 1;
105 }
106
107 if( ( data & 0x80 ) != 0 )
108 {
109 crc ^= 0x01;
110 }
111
112 data <<= 1;
113 }
114 }
115
116 for( n = 0 ; n < ( BIT >> 3 ) ; n++ )
117 {
118 for( m = 0 ; m < 8 ; m++ )
119 {
120 if( ( crc & mask ) != 0 )
121 {
122 crc <<= 1;
123 crc ^= gen_poly;
124 }
125 else
126 {
127 crc <<= 1;
128 }
129 }
130 }
131
132 return ( crc & ( ( mask << 1 ) - 1 ) );
133 }
134
135
145 static bool check( const unsigned char * pdata, size_t len, size_t crc_, size_t gen_poly = crc_generator_polynominal::crc_default< BIT >::gen_poly )
146 {
147 return ( generate( pdata, len, gen_poly ) == crc_ );
148 }
149
150
159 static size_t generate_implant( unsigned char * pdata, size_t len, size_t gen_poly = crc_generator_polynominal::crc_default< BIT >::gen_poly )
160 {
161 size_t crc = 0;
162 size_t mask;
163 size_t n, m;
164 unsigned char data;
165
166 mask = 1 << ( BIT - 1 );
167
168 for( n = 0 ; n < len - BIT / 8 ; n++ )
169 {
170 data = pdata[ n ];
171
172 for( m = 0 ; m < 8 ; m++ )
173 {
174 if( ( crc & mask ) != 0 )
175 {
176 crc <<= 1;
177 crc ^= gen_poly;
178 }
179 else
180 {
181 crc <<= 1;
182 }
183
184 if( ( data & 0x80 ) != 0 )
185 {
186 crc ^= 0x01;
187 }
188
189 data <<= 1;
190 }
191 }
192
193 for( n = 0 ; n < ( BIT >> 3 ) ; n++ )
194 {
195 for( m = 0 ; m < 8 ; m++ )
196 {
197 if( ( crc & mask ) != 0 )
198 {
199 crc <<= 1;
200 crc ^= gen_poly;
201 }
202 else
203 {
204 crc <<= 1;
205 }
206 }
207 }
208
209 // endian 変換．要修正．
210 for( n = 0 ; n < ( BIT >> 3 ) ; n++ )
211 {
212 pdata[ len - ( BIT >> 3 ) + n ] = static_cast< unsigned char >( crc >> ( ( ( BIT >> 3 ) - 1 - n ) << 3 ) ) & 0xff;
213 }
214
215 return ( crc & ( ( mask << 1 ) - 1 ) );
216 }
217
218
228 static bool check_implant( const unsigned char * pdata, size_t len, size_t gen_poly = crc_generator_polynominal::crc_default< BIT >::gen_poly )
229 {
230 return ( generate( pdata, len, gen_poly ) == 0 );
231 }
232 };
233
234
235 // mist名前空間の終わり
236 _MIST_END
237
238
239 #endif // __INCLUDE_CRC__
240