/*---------------------------------------------------------------------------* Project: Horizon File: util_Crc.h Copyright (C) Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 27772 $ *---------------------------------------------------------------------------*/ #ifndef NN_UTIL_UTIL_CRC_H_ #define NN_UTIL_UTIL_CRC_H_ #include #ifdef __cplusplus namespace nn { namespace util { const size_t CRC_TABLE_SIZE = 256; //---------------------------------------------------------------------------- // CRC-8 //---------------------------------------------------------------------------- //! @brief The class for calculating the CRC-8. class Crc8 { public: //! @brief Instantiates the object with default parameters. Crc8(); /*! @brief Initializes the context used for calculating CRC-8 hash values. @param[in] context Specifies the default context value. Normally use the default value. */ void InitializeContext(u8 context = CRC8_STANDARD_INIT); //! @brief Updates the CRC-8 hash value with the input data. //! @param[in] input Pointer to the input data. //! @param[in] length Specifies the size of the input data. void Update(const void* input, size_t length); //! @brief Gets the final result of calculating the CRC-8 hash value. //! @return The calculated result. u8 GetHash(); /*! @brief Calls all of the associated functions needed to calculate the CRC-8, and gets the calculation result. @param[in] input Pointer to the input data. @param[in] length Specifies the size of the input data. @param[in] context Specifies the default context value. Normally use the default value. @return The calculated result. */ static u8 Calculate(const void* input, size_t length, u8 context = CRC8_STANDARD_INIT); private: //! @brief The CRC generator polynomial. static const u8 CRC8_STANDARD_POLY = 0x07; //! @brief The default context value. static const u8 CRC8_STANDARD_INIT = 0; //! @brief The context structure used to calculate the hash. typedef u8 Context; //! @brief The table structure used to calculate the hash. struct Table { u8 table[CRC_TABLE_SIZE]; }; //! @brief The context used to calculate the hash. Context m_Context; //! @brief The table used to calculate the hash. Table m_Table; //! @brief Initializes the table used to calculated the hash. //! @param[in] poly Specifies the generator polynomial used to initialize the table. void InitializeTable(u8 poly); }; //---------------------------------------------------------------------------- // CRC-16 //---------------------------------------------------------------------------- //! @brief The class for calculating the CRC-16. class Crc16 { public: //! @brief Instantiates the object with default parameters. Crc16(); //! @brief Initializes the context used for calculating CRC-16 hash values. //! @param[in] context Specifies the default context value. Normally use the default value. void InitializeContext(u16 context = CRC16_STANDARD_INIT); //! @brief Updates the CRC-16 hash value with the input data. //! @param[in] input Pointer to the input data. //! @param[in] length Specifies the size of the input data. void Update(const void* input, size_t length); //! @brief Gets the final result of calculating the CRC-16 hash value. //! @return The calculated result. u16 GetHash(); /*! @brief Calls all of the associated functions needed to calculate the CRC-16, and gets the calculation result. @param[in] input Pointer to the input data. @param[in] length Specifies the size of the input data. @param[in] context Specifies the default context value. Normally use the default value. @return The calculated result. */ static u16 Calculate(const void* input, size_t length, u16 context = CRC16_STANDARD_INIT); private: //! @brief The CRC generator polynomial. static const u16 CRC16_STANDARD_POLY = 0xa001; // Calculations that perform bit inversion also invert the generated polynomials. //! @brief The default context value. static const u16 CRC16_STANDARD_INIT = 0; //! @brief The context structure used to calculate the hash. typedef u16 Context; //! @brief The table structure used to calculate the hash. struct Table { u16 table[CRC_TABLE_SIZE]; }; //! @brief The context used to calculate the hash. Context m_Context; //! @brief The table used to calculate the hash. Table m_Table; //! @brief Initializes the table used to calculated the hash. //! @param[in] poly Specifies the generator polynomial used to initialize the table. void InitializeTable(u16 poly); }; //---------------------------------------------------------------------------- // CRC-16/CCITT //---------------------------------------------------------------------------- //! @brief The class for calculating CRC-16/CCITT. class Crc16Ccitt { public: //! @brief Instantiates the object with default parameters. Crc16Ccitt(); //! @brief Initializes the context used for calculating CRC-16/CCITT hash values. //! @param[in] context Specifies the default context value. Normally use the default value. void InitializeContext(u16 context = CRC16_CCITT_INIT); //! @brief Updates the CRC-16/CCITT hash value with the input data. //! @param[in] input Pointer to the input data. //! @param[in] length Specifies the size of the input data. void Update(const void* input, size_t length); //! @brief Gets the final result of calculating the CRC-16/CCITT hash value. //! @return The calculated result. u16 GetHash(); /*! @brief Calls all of the associated functions needed to calculate CRC-16/CCITT, and gets the calculation result. @param[in] input Pointer to the input data. @param[in] length Specifies the size of the input data. @param[in] context Specifies the default context value. Normally use the default value. @return The calculated result. */ static u16 Calculate(const void* input, size_t length, u16 context = CRC16_CCITT_INIT); private: //! @brief The CRC generator polynomial. static const u16 CRC16_CCITT_POLY = 0x1021; //! @brief The default context value. static const u16 CRC16_CCITT_INIT = 0xffff; //! @brief The context structure used to calculate the hash. typedef u16 Context; //! @brief The table structure used to calculate the hash. struct Table { u16 table[CRC_TABLE_SIZE]; }; //! @brief The context used to calculate the hash. Context m_Context; //! @brief The table used to calculate the hash. Table m_Table; //! @brief Initializes the table used to calculated the hash. //! @param[in] poly Specifies the generator polynomial used to initialize the table. void InitializeTable(u16 poly); }; //---------------------------------------------------------------------------- // CRC-32 //---------------------------------------------------------------------------- //! @brief The class for calculating the CRC-32. class Crc32 { public: //! @brief Instantiates the object with default parameters. Crc32(); //! @brief Initializes the context used for calculating CRC-32 hash values. //! @param[in] context Specifies the default context value. Normally use the default value. void InitializeContext(u32 context = CRC32_STANDARD_INIT); //! @brief Updates the CRC-32 hash value with the input data. //! @param[in] input Pointer to the input data. //! @param[in] length Specifies the size of the input data. void Update(const void* input, size_t length); //! @brief Gets the final result of calculating the CRC-32 hash value. //! @return The calculated result. u32 GetHash(); /*! @brief Calls all of the associated functions needed to calculate the CRC-32, and gets the calculation result. @param[in] input Pointer to the input data. @param[in] length Specifies the size of the input data. @param[in] context Specifies the default context value. Normally use the default value. @return The calculated result. */ static u32 Calculate(const void* input, size_t length, u32 context = CRC32_STANDARD_INIT); private: //! @brief The CRC generator polynomial. static const u32 CRC32_STANDARD_POLY = 0xedb88320; // Calculations that perform bit inversion also invert the generated polynomials. //! @brief The default context value. static const u32 CRC32_STANDARD_INIT = 0xffffffff; //! @brief The context structure used to calculate the hash. typedef u32 Context; //! @brief The table structure used to calculate the hash. struct Table { u32 table[CRC_TABLE_SIZE]; }; //! @brief The context used to calculate the hash. Context m_Context; //! @brief The table used to calculate the hash. Table m_Table; //! @brief Initializes the table used to calculated the hash. //! @param[in] poly Specifies the generator polynomial used to initialize the table. void InitializeTable(u32 poly); }; //---------------------------------------------------------------------------- // CRC-32/POSIX //---------------------------------------------------------------------------- //! @brief The class for calculating CRC-32/POSIX. class Crc32Posix { public: //! @brief Instantiates the object with default parameters. Crc32Posix(); //! @brief Initializes the context used for calculating CRC-32/POSIX hash values. //! @param[in] context Specifies the default context value. Normally use the default value. void InitializeContext(u32 context = CRC32_POSIX_INIT); //! @brief Updates the CRC-32/POSIX hash value with the input data. //! @param[in] input Pointer to the input data. //! @param[in] length Specifies the size of the input data. void Update(const void* input, size_t length); //! @brief Gets the final result of calculating the CRC-32/POSIX hash value. //! @return The calculated result. u32 GetHash(); /*! @brief Calls all of the associated functions needed to calculate the CRC-32/POSIX, and gets the calculation result. @param[in] input Pointer to the input data. @param[in] length Specifies the size of the input data. @param[in] context Specifies the default context value. Normally use the default value. @return The calculated result. */ static u32 Calculate(const void* input, size_t length, u32 context = CRC32_POSIX_INIT); private: //! @brief The CRC generator polynomial. static const u32 CRC32_POSIX_POLY = 0x04c11db7; //! @brief The default context value. static const u32 CRC32_POSIX_INIT = 0; //! @brief The context structure used to calculate the hash. typedef u32 Context; //! @brief The table structure used to calculate the hash. struct Table { u32 table[CRC_TABLE_SIZE]; }; //! @brief The context used to calculate the hash. Context m_Context; //! @brief The table used to calculate the hash. Table m_Table; //! @brief Initializes the table used to calculated the hash. //! @param[in] poly Specifies the generator polynomial used to initialize the table. void InitializeTable(u32 poly); }; }} #endif // __cplusplus #endif // ifndef NN_UTIL_UTIL_CRC_H_