1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     util_Crc.cpp
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 31762 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_UTIL_UTIL_CRC_H_
17 #define NN_UTIL_UTIL_CRC_H_
18 
19 #include <nn/types.h>
20 
21 #ifdef __cplusplus
22 
23 namespace nn { namespace util {
24 
25 const size_t CRC_TABLE_SIZE = 256;
26 
27 //----------------------------------------------------------------------------
28 // CRC-8
29 //----------------------------------------------------------------------------
30 
31 //
32 class Crc8 {
33 
34 public:
35 
36     //
37     Crc8();
38 
39     /* Please see man pages for details
40 
41 
42      */
43     void InitializeContext(u8 context = CRC8_STANDARD_INIT);
44 
45     //
46     //
47     //
48     void Update(const void* input, size_t length);
49 
50     //
51     //
52     u8 GetHash();
53 
54     /* Please see man pages for details
55 
56 
57 
58 
59 
60 
61 
62 
63 
64      */
65     static u8 Calculate(const void* input, size_t length, u8 context = CRC8_STANDARD_INIT);
66 
67 private:
68     //
69     static const u8 CRC8_STANDARD_POLY = 0x07;
70 
71     //
72     static const u8 CRC8_STANDARD_INIT = 0;
73 
74     //
75     typedef u8 Context;
76 
77     //
78     struct Table
79     {
80         u8      table[CRC_TABLE_SIZE];
81     };
82 
83     //
84     Context m_Context;
85 
86     //
87     Table   m_Table;
88 
89     //
90     //
91     void InitializeTable(u8 poly);
92 
93 };
94 
95 //----------------------------------------------------------------------------
96 // CRC-16
97 //----------------------------------------------------------------------------
98 
99 //
100 class Crc16 {
101 
102 public:
103     //
104     Crc16();
105 
106     //
107     //
108     void InitializeContext(u16 context = CRC16_STANDARD_INIT);
109 
110     //
111     //
112     //
113     void Update(const void* input, size_t length);
114 
115     //
116     //
117     u16 GetHash();
118 
119     /* Please see man pages for details
120 
121 
122 
123 
124 
125 
126 
127 
128 
129      */
130     static u16 Calculate(const void* input, size_t length, u16 context = CRC16_STANDARD_INIT);
131 
132 private:
133     //
134     static const u16 CRC16_STANDARD_POLY = 0xa001;      // Items that execute bit inversion also invert generated polynomials.
135 
136     //
137     static const u16 CRC16_STANDARD_INIT = 0;
138 
139     //
140     typedef u16 Context;
141 
142     //
143     struct Table
144     {
145         u16      table[CRC_TABLE_SIZE];
146     };
147 
148     //
149     Context m_Context;
150 
151     //
152     Table   m_Table;
153 
154     //
155     //
156     void InitializeTable(u16 poly);
157 
158 };
159 
160 //----------------------------------------------------------------------------
161 // CRC-16/CCITT
162 //----------------------------------------------------------------------------
163 
164 //
165 class Crc16Ccitt {
166 
167 public:
168 
169     //
170     Crc16Ccitt();
171 
172     //
173     //
174     void InitializeContext(u16 context = CRC16_CCITT_INIT);
175 
176     //
177     //
178     //
179     void Update(const void* input, size_t length);
180 
181     //
182     //
183     u16 GetHash();
184 
185     /* Please see man pages for details
186 
187 
188 
189 
190 
191 
192 
193 
194 
195      */
196     static u16 Calculate(const void* input, size_t length, u16 context = CRC16_CCITT_INIT);
197 
198 private:
199     //
200     static const u16 CRC16_CCITT_POLY = 0x1021;
201 
202     //
203     static const u16 CRC16_CCITT_INIT = 0xffff;
204 
205     //
206     typedef u16 Context;
207 
208     //
209     struct Table
210     {
211         u16      table[CRC_TABLE_SIZE];
212     };
213 
214     //
215     Context m_Context;
216 
217     //
218     Table   m_Table;
219 
220 
221     //
222     //
223     void InitializeTable(u16 poly);
224 
225 };
226 
227 //----------------------------------------------------------------------------
228 // CRC-32
229 //----------------------------------------------------------------------------
230 
231 //
232 class Crc32 {
233 
234 public:
235 
236     //
237     Crc32();
238 
239     //
240     //
241     void InitializeContext(u32 context = CRC32_STANDARD_INIT);
242 
243     //
244     //
245     //
246     void Update(const void* input, size_t length);
247 
248     //
249     //
250     u32 GetHash();
251 
252     /* Please see man pages for details
253 
254 
255 
256 
257 
258 
259 
260 
261 
262      */
263     static u32 Calculate(const void* input, size_t length, u32 context = CRC32_STANDARD_INIT);
264 
265 private:
266     //
267     static const u32 CRC32_STANDARD_POLY = 0xedb88320;  // Items that execute bit inversion also invert generated polynomials.
268 
269     //
270     static const u32 CRC32_STANDARD_INIT = 0xffffffff;
271 
272     //
273     typedef u32 Context;
274 
275     //
276     struct Table
277     {
278         u32      table[CRC_TABLE_SIZE];
279     };
280 
281     //
282     Context m_Context;
283 
284     //
285     Table   m_Table;
286 
287 
288     //
289     //
290     void InitializeTable(u32 poly);
291 
292 };
293 
294 //----------------------------------------------------------------------------
295 // CRC-32/POSIX
296 //----------------------------------------------------------------------------
297 
298 //
299 class Crc32Posix {
300 
301 public:
302 
303     //
304     Crc32Posix();
305 
306     //
307     //
308     void InitializeContext(u32 context = CRC32_POSIX_INIT);
309 
310     //
311     //
312     //
313     void Update(const void* input, size_t length);
314 
315     //
316     //
317     u32 GetHash();
318 
319     /* Please see man pages for details
320 
321 
322 
323 
324 
325 
326 
327 
328 
329      */
330     static u32 Calculate(const void* input, size_t length, u32 context = CRC32_POSIX_INIT);
331 
332 private:
333     //
334     static const u32 CRC32_POSIX_POLY = 0x04c11db7;
335 
336     //
337     static const u32 CRC32_POSIX_INIT = 0;
338 
339     //
340     typedef u32 Context;
341 
342     //
343     struct Table
344     {
345         u32      table[CRC_TABLE_SIZE];
346     };
347 
348     //
349     Context m_Context;
350 
351     //
352     Table   m_Table;
353 
354 
355     //
356     //
357     void InitializeTable(u32 poly);
358 
359 };
360 
361 }}
362 
363 #endif // __cplusplus
364 
365 #endif // ifndef NN_UTIL_UTIL_CRC_H_
366