From f4aee57e893c7474c8f3fc8a13e36d6d8ffda902 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Sun, 30 Oct 2016 02:49:27 +0200 Subject: [PATCH] Replace the CRC16-CCITT implementation with one generated by pycrc to be MIT compliant. --- Checksums/ccitt.c | 53 +++++++++++++++++++++++++ Checksums/ccitt.cs | 55 ++++++++++++++++++++++++++ Checksums/ccitt.h | 82 +++++++++++++++++++++++++++++++++++++++ Crc16Ccitt.cs | 60 ---------------------------- MysteryGiftConvert.csproj | 2 +- Program.cs | 3 +- 6 files changed, 193 insertions(+), 62 deletions(-) create mode 100644 Checksums/ccitt.c create mode 100644 Checksums/ccitt.cs create mode 100644 Checksums/ccitt.h delete mode 100644 Crc16Ccitt.cs diff --git a/Checksums/ccitt.c b/Checksums/ccitt.c new file mode 100644 index 0000000..1ffffff --- /dev/null +++ b/Checksums/ccitt.c @@ -0,0 +1,53 @@ +/** + * \file ccitt.c + * Functions and types for CRC checks. + * + * Generated on Sun Oct 30 02:18:53 2016, + * by pycrc v0.9, https://pycrc.org + * using the configuration: + * Width = 16 + * Poly = 0x1021 + * Xor_In = 0xffff + * ReflectIn = False + * Xor_Out = 0x0000 + * ReflectOut = False + * Algorithm = bit-by-bit-fast + *****************************************************************************/ +#include "ccitt.h" /* include the header file generated with pycrc */ +#include +#include +#include + +/** + * Update the crc value with new data. + * + * \param crc The current crc value. + * \param data Pointer to a buffer of \a data_len bytes. + * \param data_len Number of bytes in the \a data buffer. + * \return The updated crc value. + *****************************************************************************/ +crc_t crc_update(crc_t crc, const void *data, size_t data_len) +{ + const unsigned char *d = (const unsigned char *)data; + unsigned int i; + bool bit; + unsigned char c; + + while (data_len--) { + c = *d++; + for (i = 0x80; i > 0; i >>= 1) { + bit = crc & 0x8000; + if (c & i) { + bit = !bit; + } + crc <<= 1; + if (bit) { + crc ^= 0x1021; + } + } + crc &= 0xffff; + } + return crc & 0xffff; +} + + diff --git a/Checksums/ccitt.cs b/Checksums/ccitt.cs new file mode 100644 index 0000000..227882a --- /dev/null +++ b/Checksums/ccitt.cs @@ -0,0 +1,55 @@ +// C# port of the ccitt.c/ccitt.h files in this directory. +// Original files generated by pycrc v0.9, https://pycrc.org + +namespace Checksums { + public static class Ccitt { + /** + * Calculate the initial crc value. + * + * \return The initial crc value. + *****************************************************************************/ + public static ushort Init() { + return 0xffff; + } + + /** + * Update the crc value with new data. + * + * \param crc The current crc value. + * \param data Pointer to a buffer of \a data_len bytes. + * \param data_len Number of bytes in the \a data buffer. + * \return The updated crc value. + *****************************************************************************/ + public static ushort Update( ushort crc, byte[] data, long data_len ) { + uint i; + bool bit; + byte c; + + for ( long idx = 0; idx < data_len; ++idx ) { + c = data[idx]; + for ( i = 0x80; i > 0; i >>= 1 ) { + bit = ( crc & 0x8000 ) != 0; + if ( ( c & i ) != 0 ) { + bit = !bit; + } + crc <<= 1; + if ( bit ) { + crc ^= 0x1021; + } + } + crc &= 0xffff; + } + return (ushort)( crc & 0xffff ); + } + + /** + * Calculate the final crc value. + * + * \param crc The current crc value. + * \return The final crc value. + *****************************************************************************/ + public static ushort Finalize( ushort crc ) { + return (ushort)( crc ^ 0x0000 ); + } + } +} diff --git a/Checksums/ccitt.h b/Checksums/ccitt.h new file mode 100644 index 0000000..a122c7d --- /dev/null +++ b/Checksums/ccitt.h @@ -0,0 +1,82 @@ +/** + * \file ccitt.h + * Functions and types for CRC checks. + * + * Generated on Sun Oct 30 02:18:48 2016, + * by pycrc v0.9, https://pycrc.org + * using the configuration: + * Width = 16 + * Poly = 0x1021 + * Xor_In = 0xffff + * ReflectIn = False + * Xor_Out = 0x0000 + * ReflectOut = False + * Algorithm = bit-by-bit-fast + *****************************************************************************/ +#ifndef __CCITT_H__ +#define __CCITT_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * The definition of the used algorithm. + * + * This is not used anywhere in the generated code, but it may be used by the + * application code to call algoritm-specific code, is desired. + *****************************************************************************/ +#define CRC_ALGO_BIT_BY_BIT_FAST 1 + + +/** + * The type of the CRC values. + * + * This type must be big enough to contain at least 16 bits. + *****************************************************************************/ +typedef uint_fast16_t crc_t; + + +/** + * Calculate the initial crc value. + * + * \return The initial crc value. + *****************************************************************************/ +static inline crc_t crc_init(void) +{ + return 0xffff; +} + + +/** + * Update the crc value with new data. + * + * \param crc The current crc value. + * \param data Pointer to a buffer of \a data_len bytes. + * \param data_len Number of bytes in the \a data buffer. + * \return The updated crc value. + *****************************************************************************/ +crc_t crc_update(crc_t crc, const void *data, size_t data_len); + + +/** + * Calculate the final crc value. + * + * \param crc The current crc value. + * \return The final crc value. + *****************************************************************************/ +static inline crc_t crc_finalize(crc_t crc) +{ + return crc ^ 0x0000; +} + + +#ifdef __cplusplus +} /* closing brace for extern "C" */ +#endif + +#endif /* __CCITT_H__ */ diff --git a/Crc16Ccitt.cs b/Crc16Ccitt.cs deleted file mode 100644 index 9c8e2f5..0000000 --- a/Crc16Ccitt.cs +++ /dev/null @@ -1,60 +0,0 @@ -// from http://www.sanity-free.com/133/crc_16_ccitt_in_csharp.html - -using System; - -namespace MysteryGiftConvert { - public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F } - - public class Crc16Ccitt { - private static Crc16Ccitt _StandardAlgorithm = null; - public static Crc16Ccitt StandardAlgorithm { - get { - if ( _StandardAlgorithm == null ) { - _StandardAlgorithm = new Crc16Ccitt( InitialCrcValue.NonZero1 ); - } - return _StandardAlgorithm; - } - } - - const ushort poly = 4129; - ushort[] table = new ushort[256]; - ushort initialValue = 0; - - public ushort ComputeChecksum( byte[] bytes ) { - return ComputeChecksum( bytes, 0, bytes.Length ); - } - public ushort ComputeChecksum( byte[] bytes, int offset, int length ) { - ushort crc = this.initialValue; - for ( int i = 0; i < length; ++i ) { - crc = (ushort)( ( crc << 8 ) ^ table[( ( crc >> 8 ) ^ ( 0xff & bytes[offset + i] ) )] ); - } - return crc; - } - - public byte[] ComputeChecksumBytes( byte[] bytes ) { - return ComputeChecksumBytes( bytes, 0, bytes.Length ); - } - public byte[] ComputeChecksumBytes( byte[] bytes, int offset, int length ) { - ushort crc = ComputeChecksum( bytes, offset, length ); - return BitConverter.GetBytes( crc ); - } - - public Crc16Ccitt( InitialCrcValue initialValue ) { - this.initialValue = (ushort)initialValue; - ushort temp, a; - for ( int i = 0; i < table.Length; ++i ) { - temp = 0; - a = (ushort)( i << 8 ); - for ( int j = 0; j < 8; ++j ) { - if ( ( ( temp ^ a ) & 0x8000 ) != 0 ) { - temp = (ushort)( ( temp << 1 ) ^ poly ); - } else { - temp <<= 1; - } - a <<= 1; - } - table[i] = temp; - } - } - } -} diff --git a/MysteryGiftConvert.csproj b/MysteryGiftConvert.csproj index 62dc641..51a63f9 100644 --- a/MysteryGiftConvert.csproj +++ b/MysteryGiftConvert.csproj @@ -43,7 +43,7 @@ - + diff --git a/Program.cs b/Program.cs index e7467dd..bfb3346 100644 --- a/Program.cs +++ b/Program.cs @@ -115,7 +115,8 @@ namespace MysteryGiftConvert { byte[] outfile = new byte[outStream.Length]; outStream.Read( outfile, 0, outfile.Length ); outStream.Position = outStream.Length; - outStream.Write( Crc16Ccitt.StandardAlgorithm.ComputeChecksumBytes( outfile ), 0, 2 ); + ushort checksum = Checksums.Ccitt.Update( Checksums.Ccitt.Init(), outfile, outfile.LongLength ); + outStream.Write( BitConverter.GetBytes( checksum ), 0, 2 ); } } else { Console.WriteLine( "Input is not a Generation 4 or 5 mystery gift file!" );