1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     util_Crc.h
4 
5   Copyright (C) 2009-2011 Nintendo. 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: 27772 $
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 //! @brief    The class for calculating the CRC-8.
32 class Crc8 {
33 
34 public:
35 
36     //! @brief  Instantiates the object with default parameters.
37     Crc8();
38 
39     /*!
40         @brief Initializes the context used for calculating CRC-8 hash values.
41         @param[in] context  Specifies the default context value. Normally use the default value.
42      */
43     void InitializeContext(u8 context = CRC8_STANDARD_INIT);
44 
45     //! @brief Updates the CRC-8 hash value with the input data.
46     //! @param[in] input  Pointer to the input data.
47     //! @param[in] length  Specifies the size of the input data.
48     void Update(const void* input, size_t length);
49 
50     //! @brief Gets the final result of calculating the CRC-8 hash value.
51     //! @return The calculated result.
52     u8 GetHash();
53 
54     /*!
55         @brief
56 
57         Calls all of the associated functions needed to calculate the <tt>CRC-8</tt>, and gets the calculation result.
58 
59         @param[in] input  Pointer to the input data.
60         @param[in] length  Specifies the size of the input data.
61         @param[in] context  Specifies the default context value. Normally use the default value.
62 
63         @return The calculated result.
64      */
65     static u8 Calculate(const void* input, size_t length, u8 context = CRC8_STANDARD_INIT);
66 
67 private:
68     //! @brief The CRC generator polynomial.
69     static const u8 CRC8_STANDARD_POLY = 0x07;
70 
71     //! @brief  The default context value.
72     static const u8 CRC8_STANDARD_INIT = 0;
73 
74     //! @brief  The context structure used to calculate the hash.
75     typedef u8 Context;
76 
77     //! @brief  The table structure used to calculate the hash.
78     struct Table
79     {
80         u8      table[CRC_TABLE_SIZE];
81     };
82 
83     //! @brief  The context used to calculate the hash.
84     Context m_Context;
85 
86     //! @brief  The table used to calculate the hash.
87     Table   m_Table;
88 
89     //! @brief  Initializes the table used to calculated the hash.
90     //! @param[in] poly  Specifies the generator polynomial used to initialize the table.
91     void InitializeTable(u8 poly);
92 
93 };
94 
95 //----------------------------------------------------------------------------
96 // CRC-16
97 //----------------------------------------------------------------------------
98 
99 //! @brief    The class for calculating the CRC-16.
100 class Crc16 {
101 
102 public:
103     //! @brief  Instantiates the object with default parameters.
104     Crc16();
105 
106     //! @brief Initializes the context used for calculating CRC-16 hash values.
107     //! @param[in] context  Specifies the default context value. Normally use the default value.
108     void InitializeContext(u16 context = CRC16_STANDARD_INIT);
109 
110     //! @brief Updates the CRC-16 hash value with the input data.
111     //! @param[in] input  Pointer to the input data.
112     //! @param[in] length  Specifies the size of the input data.
113     void Update(const void* input, size_t length);
114 
115     //! @brief Gets the final result of calculating the CRC-16 hash value.
116     //! @return The calculated result.
117     u16 GetHash();
118 
119     /*!
120         @brief
121 
122         Calls all of the associated functions needed to calculate the <tt>CRC-16</tt>, and gets the calculation result.
123 
124         @param[in] input  Pointer to the input data.
125         @param[in] length  Specifies the size of the input data.
126         @param[in] context  Specifies the default context value. Normally use the default value.
127 
128         @return The calculated result.
129      */
130     static u16 Calculate(const void* input, size_t length, u16 context = CRC16_STANDARD_INIT);
131 
132 private:
133     //! @brief The CRC generator polynomial.
134     static const u16 CRC16_STANDARD_POLY = 0xa001;      // Calculations that perform bit inversion also invert the generated polynomials.
135 
136     //! @brief  The default context value.
137     static const u16 CRC16_STANDARD_INIT = 0;
138 
139     //! @brief  The context structure used to calculate the hash.
140     typedef u16 Context;
141 
142     //! @brief  The table structure used to calculate the hash.
143     struct Table
144     {
145         u16      table[CRC_TABLE_SIZE];
146     };
147 
148     //! @brief  The context used to calculate the hash.
149     Context m_Context;
150 
151     //! @brief  The table used to calculate the hash.
152     Table   m_Table;
153 
154     //! @brief  Initializes the table used to calculated the hash.
155     //! @param[in] poly  Specifies the generator polynomial used to initialize the table.
156     void InitializeTable(u16 poly);
157 
158 };
159 
160 //----------------------------------------------------------------------------
161 // CRC-16/CCITT
162 //----------------------------------------------------------------------------
163 
164 //! @brief    The class for calculating CRC-16/CCITT.
165 class Crc16Ccitt {
166 
167 public:
168 
169     //! @brief  Instantiates the object with default parameters.
170     Crc16Ccitt();
171 
172     //! @brief  Initializes the context used for calculating CRC-16/CCITT hash values.
173     //! @param[in] context  Specifies the default context value. Normally use the default value.
174     void InitializeContext(u16 context = CRC16_CCITT_INIT);
175 
176     //! @brief Updates the CRC-16/CCITT hash value with the input data.
177     //! @param[in] input  Pointer to the input data.
178     //! @param[in] length  Specifies the size of the input data.
179     void Update(const void* input, size_t length);
180 
181     //! @brief Gets the final result of calculating the CRC-16/CCITT hash value.
182     //! @return The calculated result.
183     u16 GetHash();
184 
185     /*!
186         @brief
187 
188         Calls all of the associated functions needed to calculate <tt>CRC-16/CCITT</tt>, and gets the calculation result.
189 
190         @param[in] input  Pointer to the input data.
191         @param[in] length  Specifies the size of the input data.
192         @param[in] context  Specifies the default context value. Normally use the default value.
193 
194         @return The calculated result.
195      */
196     static u16 Calculate(const void* input, size_t length, u16 context = CRC16_CCITT_INIT);
197 
198 private:
199     //! @brief The CRC generator polynomial.
200     static const u16 CRC16_CCITT_POLY = 0x1021;
201 
202     //! @brief  The default context value.
203     static const u16 CRC16_CCITT_INIT = 0xffff;
204 
205     //! @brief  The context structure used to calculate the hash.
206     typedef u16 Context;
207 
208     //! @brief  The table structure used to calculate the hash.
209     struct Table
210     {
211         u16      table[CRC_TABLE_SIZE];
212     };
213 
214     //! @brief  The context used to calculate the hash.
215     Context m_Context;
216 
217     //! @brief  The table used to calculate the hash.
218     Table   m_Table;
219 
220 
221     //! @brief  Initializes the table used to calculated the hash.
222     //! @param[in] poly  Specifies the generator polynomial used to initialize the table.
223     void InitializeTable(u16 poly);
224 
225 };
226 
227 //----------------------------------------------------------------------------
228 // CRC-32
229 //----------------------------------------------------------------------------
230 
231 //! @brief    The class for calculating the CRC-32.
232 class Crc32 {
233 
234 public:
235 
236     //! @brief  Instantiates the object with default parameters.
237     Crc32();
238 
239     //! @brief Initializes the context used for calculating CRC-32 hash values.
240     //! @param[in] context  Specifies the default context value. Normally use the default value.
241     void InitializeContext(u32 context = CRC32_STANDARD_INIT);
242 
243     //! @brief Updates the CRC-32 hash value with the input data.
244     //! @param[in] input  Pointer to the input data.
245     //! @param[in] length  Specifies the size of the input data.
246     void Update(const void* input, size_t length);
247 
248     //! @brief Gets the final result of calculating the CRC-32 hash value.
249     //! @return The calculated result.
250     u32 GetHash();
251 
252     /*!
253         @brief
254 
255         Calls all of the associated functions needed to calculate the <tt>CRC-32</tt>, and gets the calculation result.
256 
257         @param[in] input  Pointer to the input data.
258         @param[in] length  Specifies the size of the input data.
259         @param[in] context  Specifies the default context value. Normally use the default value.
260 
261         @return The calculated result.
262      */
263     static u32 Calculate(const void* input, size_t length, u32 context = CRC32_STANDARD_INIT);
264 
265 private:
266     //! @brief The CRC generator polynomial.
267     static const u32 CRC32_STANDARD_POLY = 0xedb88320;  // Calculations that perform bit inversion also invert the generated polynomials.
268 
269     //! @brief  The default context value.
270     static const u32 CRC32_STANDARD_INIT = 0xffffffff;
271 
272     //! @brief  The context structure used to calculate the hash.
273     typedef u32 Context;
274 
275     //! @brief  The table structure used to calculate the hash.
276     struct Table
277     {
278         u32      table[CRC_TABLE_SIZE];
279     };
280 
281     //! @brief  The context used to calculate the hash.
282     Context m_Context;
283 
284     //! @brief  The table used to calculate the hash.
285     Table   m_Table;
286 
287 
288     //! @brief  Initializes the table used to calculated the hash.
289     //! @param[in] poly  Specifies the generator polynomial used to initialize the table.
290     void InitializeTable(u32 poly);
291 
292 };
293 
294 //----------------------------------------------------------------------------
295 // CRC-32/POSIX
296 //----------------------------------------------------------------------------
297 
298 //! @brief  The class for calculating CRC-32/POSIX.
299 class Crc32Posix {
300 
301 public:
302 
303     //! @brief  Instantiates the object with default parameters.
304     Crc32Posix();
305 
306     //! @brief Initializes the context used for calculating CRC-32/POSIX hash values.
307     //! @param[in] context  Specifies the default context value. Normally use the default value.
308     void InitializeContext(u32 context = CRC32_POSIX_INIT);
309 
310     //! @brief Updates the CRC-32/POSIX hash value with the input data.
311     //! @param[in] input  Pointer to the input data.
312     //! @param[in] length  Specifies the size of the input data.
313     void Update(const void* input, size_t length);
314 
315     //! @brief Gets the final result of calculating the CRC-32/POSIX hash value.
316     //! @return The calculated result.
317     u32 GetHash();
318 
319     /*!
320         @brief
321 
322         Calls all of the associated functions needed to calculate the <tt>CRC-32/POSIX</tt>, and gets the calculation result.
323 
324         @param[in] input  Pointer to the input data.
325         @param[in] length  Specifies the size of the input data.
326         @param[in] context  Specifies the default context value. Normally use the default value.
327 
328         @return The calculated result.
329      */
330     static u32 Calculate(const void* input, size_t length, u32 context = CRC32_POSIX_INIT);
331 
332 private:
333     //! @brief The CRC generator polynomial.
334     static const u32 CRC32_POSIX_POLY = 0x04c11db7;
335 
336     //! @brief  The default context value.
337     static const u32 CRC32_POSIX_INIT = 0;
338 
339     //! @brief  The context structure used to calculate the hash.
340     typedef u32 Context;
341 
342     //! @brief  The table structure used to calculate the hash.
343     struct Table
344     {
345         u32      table[CRC_TABLE_SIZE];
346     };
347 
348     //! @brief  The context used to calculate the hash.
349     Context m_Context;
350 
351     //! @brief  The table used to calculate the hash.
352     Table   m_Table;
353 
354 
355     //! @brief  Initializes the table used to calculated the hash.
356     //! @param[in] poly  Specifies the generator polynomial used to initialize the table.
357     void InitializeTable(u32 poly);
358 
359 };
360 
361 }}
362 
363 #endif // __cplusplus
364 
365 #endif // ifndef NN_UTIL_UTIL_CRC_H_
366