mirror of
https://github.com/AdmiralCurtiss/MysteryGiftConvert.git
synced 2026-03-21 17:48:29 -05:00
Replace the CRC16-CCITT implementation with one generated by pycrc to be MIT compliant.
This commit is contained in:
parent
db6d6b64b6
commit
f4aee57e89
53
Checksums/ccitt.c
Normal file
53
Checksums/ccitt.c
Normal file
|
|
@ -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 <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
||||
55
Checksums/ccitt.cs
Normal file
55
Checksums/ccitt.cs
Normal file
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
82
Checksums/ccitt.h
Normal file
82
Checksums/ccitt.h
Normal file
|
|
@ -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 <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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__ */
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Crc16Ccitt.cs" />
|
||||
<Compile Include="Checksums\ccitt.cs" />
|
||||
<Compile Include="MysteryGiftGen4.cs" />
|
||||
<Compile Include="MysteryGiftGen5.cs" />
|
||||
<Compile Include="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!" );
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user