Implement CRC64.

This commit is contained in:
2019-03-20 23:41:16 +00:00
parent 3ce5015f9d
commit cbbd54be89
5 changed files with 136 additions and 1 deletions

View File

@@ -5,6 +5,6 @@ set(CMAKE_C_STANDARD 99)
add_compile_definitions(__STDC_FORMAT_MACROS=1) add_compile_definitions(__STDC_FORMAT_MACROS=1)
add_library(libdicformat SHARED include/dicformat/consts.h include/dicformat/enums.h include/dic.h include/dicformat.h add_library(libdicformat SHARED include/dicformat/consts.h include/dicformat/enums.h include/dic.h include/dicformat.h
include/dicformat/decls.h include/dicformat/structs.h src/identify.c src/open.c include/dicformat/context.h src/close.c include/dicformat/errors.h src/read.c) include/dicformat/decls.h include/dicformat/structs.h src/identify.c src/open.c include/dicformat/context.h src/close.c include/dicformat/errors.h src/read.c src/crc64.c)
include_directories(include include/dicformat) include_directories(include include/dicformat)

View File

@@ -56,6 +56,9 @@
/** This mask is to check for position in CompactDisc suffix/prefix deduplicated block */ /** This mask is to check for position in CompactDisc suffix/prefix deduplicated block */
#define CD_DFIX_MASK 0x00FFFFFF #define CD_DFIX_MASK 0x00FFFFFF
#define CRC64_ECMA_POLY 0xC96C5795D7870F42
#define CRC64_ECMA_SEED 0xFFFFFFFFFFFFFFFF
#endif //LIBDICFORMAT_CONSTS_H #endif //LIBDICFORMAT_CONSTS_H
#pragma clang diagnostic pop #pragma clang diagnostic pop

View File

@@ -46,5 +46,16 @@ int close(void *context);
uint8_t *read_media_tag(void *context, int tag); uint8_t *read_media_tag(void *context, int tag);
void *crc64_init(uint64_t polynomial, uint64_t seed);
void *crc64_init_ecma(void);
void crc64_update(void *context, const char *data, size_t len);
uint64_t crc64_final(void *context);
uint64_t crc64_data(const char *data, size_t len, uint64_t polynomial, uint64_t seed);
uint64_t crc64_data_ecma(const char *data, size_t len);
#endif //LIBDICFORMAT_DECLS_H #endif //LIBDICFORMAT_DECLS_H

View File

@@ -302,6 +302,12 @@ typedef struct ChecksumEntry
uint32_t length; uint32_t length;
} ChecksumEntry; } ChecksumEntry;
typedef struct Crc64Context
{
uint64_t finalSeed;
uint64_t table[256];
uint64_t hashInt;
} Crc64Context;
#pragma pack(pop) #pragma pack(pop)

115
src/crc64.c Normal file
View File

@@ -0,0 +1,115 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : crc64.c
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Checksums.
//
// --[ Description ] ----------------------------------------------------------
//
// Implements a CRC64 algorithm.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
#include <stdint.h>
#include <malloc.h>
#include <string.h>
#include <dicformat.h>
void *crc64_init(uint64_t polynomial, uint64_t seed)
{
Crc64Context *ctx;
ctx = malloc(sizeof(Crc64Context));
if(ctx == NULL)
return NULL;
memset(ctx, 1, sizeof(Crc64Context));
ctx->finalSeed = seed;
ctx->hashInt = seed;
for(int i = 0; i < 256; i++)
{
uint64_t entry = (uint64_t)i;
for(int j = 0; j < 8; j++)
if((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial;
else
entry = entry >> 1;
ctx->table[i] = entry;
}
return ctx;
}
void *crc64_init_ecma(void)
{
return crc64_init(CRC64_ECMA_POLY, CRC64_ECMA_SEED);
}
void crc64_update(void *context, const char *data, size_t len)
{
Crc64Context *ctx = context;
for(size_t i = 0; i < len; i++)
ctx->hashInt = (ctx->hashInt >> 8) ^ ctx->table[data[i] ^ (ctx->hashInt & 0xFF)];
}
uint64_t crc64_final(void *context)
{
Crc64Context *ctx = context;
return ctx->hashInt ^ ctx->finalSeed;
}
uint64_t crc64_data(const char *data, size_t len, uint64_t polynomial, uint64_t seed)
{
uint64_t table[256];
uint64_t hashInt = seed;
for(int i = 0; i < 256; i++)
{
uint64_t entry = (uint64_t)i;
for(int j = 0; j < 8; j++)
if((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial;
else
entry = entry >> 1;
table[i] = entry;
}
for(size_t i = 0; i < len; i++)
hashInt = (hashInt >> 8) ^ table[data[i] ^ (hashInt & 0xFF)];
return hashInt ^ seed;
}
uint64_t crc64_data_ecma(const char *data, size_t len)
{
return crc64_data(data, len, CRC64_ECMA_POLY, CRC64_ECMA_SEED);
}