[ldm] Clean up code
This commit is contained in:
@@ -3,34 +3,30 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
#include "ldm.h"
|
#include "ldm.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#define HASH_EVERY 7
|
// Insert every (HASH_ONLY_EVERY + 1) into the hash table.
|
||||||
|
#define HASH_ONLY_EVERY 0
|
||||||
|
|
||||||
#define LDM_MEMORY_USAGE 18
|
#define LDM_MEMORY_USAGE 20
|
||||||
#define LDM_HASHLOG (LDM_MEMORY_USAGE-2)
|
#define LDM_HASHLOG (LDM_MEMORY_USAGE-2)
|
||||||
#define LDM_HASHTABLESIZE (1 << (LDM_MEMORY_USAGE))
|
#define LDM_HASHTABLESIZE (1 << (LDM_MEMORY_USAGE))
|
||||||
#define LDM_HASHTABLESIZE_U32 ((LDM_HASHTABLESIZE) >> 2)
|
#define LDM_HASHTABLESIZE_U32 ((LDM_HASHTABLESIZE) >> 2)
|
||||||
#define LDM_HASH_SIZE_U32 (1 << (LDM_HASHLOG))
|
|
||||||
|
|
||||||
#define LDM_OFFSET_SIZE 4
|
#define LDM_OFFSET_SIZE 4
|
||||||
|
|
||||||
#define WINDOW_SIZE (1 << 23)
|
#define WINDOW_SIZE (1 << 20)
|
||||||
#define MAX_WINDOW_SIZE 31
|
|
||||||
#define HASH_SIZE 4
|
|
||||||
#define LDM_HASH_LENGTH 4
|
|
||||||
|
|
||||||
// Should be multiple of four
|
//These should be multiples of four.
|
||||||
#define MINMATCH 4
|
#define LDM_HASH_LENGTH 100
|
||||||
|
#define MINMATCH 100
|
||||||
|
|
||||||
#define ML_BITS 4
|
#define ML_BITS 4
|
||||||
#define ML_MASK ((1U<<ML_BITS)-1)
|
#define ML_MASK ((1U<<ML_BITS)-1)
|
||||||
#define RUN_BITS (8-ML_BITS)
|
#define RUN_BITS (8-ML_BITS)
|
||||||
#define RUN_MASK ((1U<<RUN_BITS)-1)
|
#define RUN_MASK ((1U<<RUN_BITS)-1)
|
||||||
|
|
||||||
#define LDM_ROLLING_HASH
|
|
||||||
#define COMPUTE_STATS
|
#define COMPUTE_STATS
|
||||||
//#define RUN_CHECKS
|
//#define RUN_CHECKS
|
||||||
//#define LDM_DEBUG
|
//#define LDM_DEBUG
|
||||||
@@ -45,10 +41,9 @@ typedef uint32_t offset_t;
|
|||||||
typedef uint32_t hash_t;
|
typedef uint32_t hash_t;
|
||||||
typedef signed char schar;
|
typedef signed char schar;
|
||||||
|
|
||||||
|
typedef struct hashEntry {
|
||||||
typedef struct LDM_hashEntry {
|
|
||||||
offset_t offset;
|
offset_t offset;
|
||||||
} LDM_hashEntry;
|
} hashEntry;
|
||||||
|
|
||||||
typedef struct LDM_compressStats {
|
typedef struct LDM_compressStats {
|
||||||
U32 numMatches;
|
U32 numMatches;
|
||||||
@@ -60,24 +55,6 @@ typedef struct LDM_compressStats {
|
|||||||
U32 numHashInserts;
|
U32 numHashInserts;
|
||||||
} LDM_compressStats;
|
} LDM_compressStats;
|
||||||
|
|
||||||
static void LDM_printCompressStats(const LDM_compressStats *stats) {
|
|
||||||
printf("=====================\n");
|
|
||||||
printf("Compression statistics\n");
|
|
||||||
printf("Total number of matches: %u\n", stats->numMatches);
|
|
||||||
printf("Average match length: %.1f\n", ((double)stats->totalMatchLength) /
|
|
||||||
(double)stats->numMatches);
|
|
||||||
printf("Average literal length: %.1f\n",
|
|
||||||
((double)stats->totalLiteralLength) / (double)stats->numMatches);
|
|
||||||
printf("Average offset length: %.1f\n",
|
|
||||||
((double)stats->totalOffset) / (double)stats->numMatches);
|
|
||||||
printf("Num collisions, num hash inserts, %% collisions: %u, %u, %.3f\n",
|
|
||||||
stats->numCollisions, stats->numHashInserts,
|
|
||||||
stats->numHashInserts == 0 ?
|
|
||||||
1.0 : (100.0 * (double)stats->numCollisions) /
|
|
||||||
(double)stats->numHashInserts);
|
|
||||||
printf("=====================\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct LDM_CCtx {
|
typedef struct LDM_CCtx {
|
||||||
size_t isize; /* Input size */
|
size_t isize; /* Input size */
|
||||||
size_t maxOSize; /* Maximum output size */
|
size_t maxOSize; /* Maximum output size */
|
||||||
@@ -101,25 +78,79 @@ typedef struct LDM_CCtx {
|
|||||||
|
|
||||||
LDM_compressStats stats; /* Compression statistics */
|
LDM_compressStats stats; /* Compression statistics */
|
||||||
|
|
||||||
LDM_hashEntry hashTable[LDM_HASHTABLESIZE_U32];
|
hashEntry hashTable[LDM_HASHTABLESIZE_U32];
|
||||||
|
|
||||||
const BYTE *lastPosHashed; /* Last position hashed */
|
const BYTE *lastPosHashed; /* Last position hashed */
|
||||||
hash_t lastHash; /* Hash corresponding to lastPosHashed */
|
hash_t lastHash; /* Hash corresponding to lastPosHashed */
|
||||||
const BYTE *nextIp;
|
U32 lastSum;
|
||||||
|
|
||||||
|
const BYTE *nextIp; // TODO: this is redundant (ip + step)
|
||||||
const BYTE *nextPosHashed;
|
const BYTE *nextPosHashed;
|
||||||
hash_t nextHash; /* Hash corresponding to nextPosHashed */
|
hash_t nextHash; /* Hash corresponding to nextPosHashed */
|
||||||
|
|
||||||
// Members for rolling hash.
|
|
||||||
U32 lastSum;
|
|
||||||
U32 nextSum;
|
U32 nextSum;
|
||||||
|
|
||||||
unsigned step;
|
unsigned step; // ip step, should be 1.
|
||||||
|
|
||||||
// DEBUG
|
// DEBUG
|
||||||
const BYTE *DEBUG_setNextHash;
|
const BYTE *DEBUG_setNextHash;
|
||||||
} LDM_CCtx;
|
} LDM_CCtx;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Outputs compression statistics.
|
||||||
|
*/
|
||||||
|
static void printCompressStats(const LDM_CCtx *cctx) {
|
||||||
|
const LDM_compressStats *stats = &(cctx->stats);
|
||||||
|
#ifdef COMPUTE_STATS
|
||||||
|
printf("=====================\n");
|
||||||
|
printf("Compression statistics\n");
|
||||||
|
printf("Total number of matches: %u\n", stats->numMatches);
|
||||||
|
printf("Average match length: %.1f\n", ((double)stats->totalMatchLength) /
|
||||||
|
(double)stats->numMatches);
|
||||||
|
printf("Average literal length: %.1f\n",
|
||||||
|
((double)stats->totalLiteralLength) / (double)stats->numMatches);
|
||||||
|
printf("Average offset length: %.1f\n",
|
||||||
|
((double)stats->totalOffset) / (double)stats->numMatches);
|
||||||
|
printf("Num collisions, num hash inserts, %% collisions: %u, %u, %.3f\n",
|
||||||
|
stats->numCollisions, stats->numHashInserts,
|
||||||
|
stats->numHashInserts == 0 ?
|
||||||
|
1.0 : (100.0 * (double)stats->numCollisions) /
|
||||||
|
(double)stats->numHashInserts);
|
||||||
|
|
||||||
|
// Output occupancy of hash table.
|
||||||
|
{
|
||||||
|
U32 i = 0;
|
||||||
|
U32 ctr = 0;
|
||||||
|
for (; i < LDM_HASHTABLESIZE_U32; i++) {
|
||||||
|
if ((cctx->hashTable)[i].offset == 0) {
|
||||||
|
ctr++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("Hash table size, empty slots, %% empty: %u %u %.3f\n",
|
||||||
|
LDM_HASHTABLESIZE_U32, ctr,
|
||||||
|
100.0 * (double)(ctr) / (double)LDM_HASHTABLESIZE_U32);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("=====================\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the MINMATCH bytes from p are the same as the MINMATCH
|
||||||
|
* bytes from match.
|
||||||
|
*
|
||||||
|
* This assumes MINMATCH is a multiple of four.
|
||||||
|
*
|
||||||
|
* Return 1 if valid, 0 otherwise.
|
||||||
|
*/
|
||||||
static int LDM_isValidMatch(const BYTE *p, const BYTE *match) {
|
static int LDM_isValidMatch(const BYTE *p, const BYTE *match) {
|
||||||
|
/*
|
||||||
|
if (memcmp(p, match, MINMATCH) == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
*/
|
||||||
|
|
||||||
|
//TODO: This seems to be faster for some reason?
|
||||||
U16 lengthLeft = MINMATCH;
|
U16 lengthLeft = MINMATCH;
|
||||||
const BYTE *curP = p;
|
const BYTE *curP = p;
|
||||||
const BYTE *curMatch = match;
|
const BYTE *curMatch = match;
|
||||||
@@ -137,25 +168,22 @@ static int LDM_isValidMatch(const BYTE *p, const BYTE *match) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef LDM_ROLLING_HASH
|
|
||||||
/**
|
/**
|
||||||
* Convert a sum computed from LDM_getRollingHash to a hash value in the range
|
* Convert a sum computed from getChecksum to a hash value in the range
|
||||||
* of the hash table.
|
* of the hash table.
|
||||||
*/
|
*/
|
||||||
#define LDM_SUM2HASH2(s1,s2) (((s1) + (s2)) & 0xFFFF)
|
static hash_t checksumToHash(U32 sum) {
|
||||||
#define LDM_SUM2HASH(sum) (LDM_SUM2HASH2((sum)&0xFFFF,(sum)>>16))
|
return ((sum * 2654435761U) >> ((32)-LDM_HASHLOG));
|
||||||
|
|
||||||
static hash_t LDM_sumToHash(U32 sum) {
|
|
||||||
// return sum & (LDM_HASH_SIZE_U32 - 1);
|
|
||||||
// return sum % (LDM_HASHTABLESIZE_U32 );
|
|
||||||
|
|
||||||
return ((sum* 2654435761U) >> ((32)-LDM_HASHLOG));
|
|
||||||
// return LDM_SUM2HASH2(sum&0xFFFF, sum >> 16);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static U32 LDM_getRollingHash(const char *data, U32 len) {
|
/**
|
||||||
|
* Computes a checksum based on rsync's checksum.
|
||||||
|
*
|
||||||
|
* a(k,l) = \sum_{i = k}^l x_i (mod M)
|
||||||
|
* b(k,l) = \sum_{i = k}^l ((l - i + 1) * x_i) (mod M)
|
||||||
|
* checksum(k,l) = a(k,l) + 2^{16} * b(k,l)
|
||||||
|
*/
|
||||||
|
static U32 getChecksum(const char *data, U32 len) {
|
||||||
U32 i;
|
U32 i;
|
||||||
U32 s1, s2;
|
U32 s1, s2;
|
||||||
const schar *buf = (const schar *)data;
|
const schar *buf = (const schar *)data;
|
||||||
@@ -173,34 +201,31 @@ static U32 LDM_getRollingHash(const char *data, U32 len) {
|
|||||||
return (s1 & 0xffff) + (s2 << 16);
|
return (s1 & 0xffff) + (s2 << 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct LDM_sumStruct {
|
/**
|
||||||
U16 s1, s2;
|
* Update a checksum computed from getChecksum(data, len).
|
||||||
} LDM_sumStruct;
|
*
|
||||||
|
* The checksum can be updated along its ends as follows:
|
||||||
static U32 LDM_updateRollingHash(U32 sum, U32 len,
|
* a(k+1, l+1) = (a(k,l) - x_k + x_{l+1}) (mod M)
|
||||||
schar toRemove, schar toAdd) {
|
* b(k+1, l+1) = (b(k,l) - (l-k+1)*x_k + (a(k+1,l+1)) (mod M)
|
||||||
|
*
|
||||||
|
* Thus toRemove should correspond to data[0].
|
||||||
|
*/
|
||||||
|
static U32 updateChecksum(U32 sum, U32 len,
|
||||||
|
schar toRemove, schar toAdd) {
|
||||||
U32 s1 = (sum & 0xffff) - toRemove + toAdd;
|
U32 s1 = (sum & 0xffff) - toRemove + toAdd;
|
||||||
U32 s2 = (sum >> 16) - (toRemove * len) + s1;
|
U32 s2 = (sum >> 16) - (toRemove * len) + s1;
|
||||||
|
|
||||||
return (s1 & 0xffff) + (s2 << 16);
|
return (s1 & 0xffff) + (s2 << 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
/*
|
* Update cctx->nextSum, cctx->nextHash, and cctx->nextPosHashed
|
||||||
static hash_t LDM_hashPosition(const void * const p) {
|
* based on cctx->lastSum and cctx->lastPosHashed.
|
||||||
return LDM_sumToHash(LDM_getRollingHash((const char *)p, LDM_HASH_LENGTH));
|
*
|
||||||
}
|
* This uses a rolling hash and requires that the last position hashed
|
||||||
*/
|
* corresponds to cctx->nextIp - step.
|
||||||
|
*/
|
||||||
/*
|
static void setNextHash(LDM_CCtx *cctx) {
|
||||||
static void LDM_getRollingHashParts(U32 sum, LDM_sumStruct *sumStruct) {
|
|
||||||
sumStruct->s1 = sum & 0xffff;
|
|
||||||
sumStruct->s2 = sum >> 16;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
static void LDM_setNextHash(LDM_CCtx *cctx) {
|
|
||||||
|
|
||||||
#ifdef RUN_CHECKS
|
#ifdef RUN_CHECKS
|
||||||
U32 check;
|
U32 check;
|
||||||
if ((cctx->nextIp - cctx->ibase != 1) &&
|
if ((cctx->nextIp - cctx->ibase != 1) &&
|
||||||
@@ -212,160 +237,100 @@ static void LDM_setNextHash(LDM_CCtx *cctx) {
|
|||||||
cctx->DEBUG_setNextHash = cctx->nextIp;
|
cctx->DEBUG_setNextHash = cctx->nextIp;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// cctx->nextSum = LDM_getRollingHash((const char *)cctx->nextIp, LDM_HASH_LENGTH);
|
// cctx->nextSum = getChecksum((const char *)cctx->nextIp, LDM_HASH_LENGTH);
|
||||||
cctx->nextSum = LDM_updateRollingHash(
|
cctx->nextSum = updateChecksum(
|
||||||
cctx->lastSum, LDM_HASH_LENGTH,
|
cctx->lastSum, LDM_HASH_LENGTH,
|
||||||
(schar)((cctx->lastPosHashed)[0]),
|
(schar)((cctx->lastPosHashed)[0]),
|
||||||
(schar)((cctx->lastPosHashed)[LDM_HASH_LENGTH]));
|
(schar)((cctx->lastPosHashed)[LDM_HASH_LENGTH]));
|
||||||
|
cctx->nextPosHashed = cctx->nextIp;
|
||||||
|
cctx->nextHash = checksumToHash(cctx->nextSum);
|
||||||
|
|
||||||
#ifdef RUN_CHECKS
|
#ifdef RUN_CHECKS
|
||||||
check = LDM_getRollingHash((const char *)cctx->nextIp, LDM_HASH_LENGTH);
|
check = getChecksum((const char *)cctx->nextIp, LDM_HASH_LENGTH);
|
||||||
|
|
||||||
if (check != cctx->nextSum) {
|
if (check != cctx->nextSum) {
|
||||||
printf("CHECK: setNextHash failed %u %u\n", check, cctx->nextSum);
|
printf("CHECK: setNextHash failed %u %u\n", check, cctx->nextSum);
|
||||||
// printf("INFO: %u %u %u\n", LDM_read32(cctx->nextIp),
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
cctx->nextPosHashed = cctx->nextIp;
|
|
||||||
cctx->nextHash = LDM_sumToHash(cctx->nextSum);
|
|
||||||
|
|
||||||
#ifdef RUN_CHECKS
|
|
||||||
if ((cctx->nextIp - cctx->lastPosHashed) != 1) {
|
if ((cctx->nextIp - cctx->lastPosHashed) != 1) {
|
||||||
printf("setNextHash: nextIp != lastPosHashed + 1. %zu %zu %zu\n",
|
printf("setNextHash: nextIp != lastPosHashed + 1. %zu %zu %zu\n",
|
||||||
cctx->nextIp - cctx->ibase, cctx->lastPosHashed - cctx->ibase,
|
cctx->nextIp - cctx->ibase, cctx->lastPosHashed - cctx->ibase,
|
||||||
cctx->ip - cctx->ibase);
|
cctx->ip - cctx->ibase);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LDM_putHashOfCurrentPositionFromHash(
|
static void putHashOfCurrentPositionFromHash(
|
||||||
LDM_CCtx *cctx, hash_t hash, U32 sum) {
|
LDM_CCtx *cctx, hash_t hash, U32 sum) {
|
||||||
/*
|
|
||||||
if (((cctx->ip - cctx->ibase) & HASH_EVERY) != HASH_EVERY) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
#ifdef COMPUTE_STATS
|
#ifdef COMPUTE_STATS
|
||||||
if (cctx->stats.numHashInserts < LDM_HASHTABLESIZE_U32 ) {
|
if (cctx->stats.numHashInserts < LDM_HASHTABLESIZE_U32) {
|
||||||
offset_t offset = (cctx->hashTable)[hash].offset;
|
offset_t offset = (cctx->hashTable)[hash].offset;
|
||||||
cctx->stats.numHashInserts++;
|
cctx->stats.numHashInserts++;
|
||||||
if (offset != 0 && !LDM_isValidMatch(cctx->ip, offset + cctx->ibase)) {
|
if (offset != 0 && !LDM_isValidMatch(cctx->ip, offset + cctx->ibase)) {
|
||||||
// printf("%u %u %zu\n", hash, offset, cctx->ip - cctx->ibase);
|
|
||||||
// printf("TST: %u %u\n", LDM_read32(cctx->ip), LDM_read32(offset + cctx->ibase));
|
|
||||||
cctx->stats.numCollisions++;
|
cctx->stats.numCollisions++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (((cctx->ip - cctx->ibase) & HASH_EVERY) == HASH_EVERY) {
|
// Hash only every HASH_ONLY_EVERY times, based on cctx->ip.
|
||||||
#ifdef COMPUTE_STATS
|
// Note: this works only when cctx->step is 1.
|
||||||
/*
|
if (((cctx->ip - cctx->ibase) & HASH_ONLY_EVERY) == HASH_ONLY_EVERY) {
|
||||||
offset_t offset = (cctx->hashTable)[hash].offset;
|
(cctx->hashTable)[hash] = (hashEntry){ (offset_t)(cctx->ip - cctx->ibase) };
|
||||||
if (offset == 0) {
|
|
||||||
printf("NEW HASH: %u\n", hash);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
#endif
|
|
||||||
|
|
||||||
(cctx->hashTable)[hash] = (LDM_hashEntry){ (offset_t)(cctx->ip - cctx->ibase) };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Book-keeping
|
|
||||||
cctx->lastPosHashed = cctx->ip;
|
cctx->lastPosHashed = cctx->ip;
|
||||||
cctx->lastHash = hash;
|
cctx->lastHash = hash;
|
||||||
cctx->lastSum = sum;
|
cctx->lastSum = sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy over the cctx->lastHash, cctx->lastSum, and cctx->lastPosHashed
|
||||||
|
* fields from the "next" fields.
|
||||||
|
*
|
||||||
|
* This requires that cctx->ip == cctx->nextPosHashed.
|
||||||
|
*/
|
||||||
static void LDM_updateLastHashFromNextHash(LDM_CCtx *cctx) {
|
static void LDM_updateLastHashFromNextHash(LDM_CCtx *cctx) {
|
||||||
#ifdef RUN_CHECKS
|
#ifdef RUN_CHECKS
|
||||||
if (cctx->ip != cctx->nextPosHashed) {
|
if (cctx->ip != cctx->nextPosHashed) {
|
||||||
printf("CHECK failed: updateLastHashFromNextHash %zu\n", cctx->ip - cctx->ibase);
|
printf("CHECK failed: updateLastHashFromNextHash %zu\n",
|
||||||
|
cctx->ip - cctx->ibase);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
LDM_putHashOfCurrentPositionFromHash(cctx, cctx->nextHash, cctx->nextSum);
|
putHashOfCurrentPositionFromHash(cctx, cctx->nextHash, cctx->nextSum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert hash of the current position into the hash table.
|
||||||
|
*/
|
||||||
static void LDM_putHashOfCurrentPosition(LDM_CCtx *cctx) {
|
static void LDM_putHashOfCurrentPosition(LDM_CCtx *cctx) {
|
||||||
U32 sum = LDM_getRollingHash((const char *)cctx->ip, LDM_HASH_LENGTH);
|
U32 sum = getChecksum((const char *)cctx->ip, LDM_HASH_LENGTH);
|
||||||
hash_t hash = LDM_sumToHash(sum);
|
hash_t hash = checksumToHash(sum);
|
||||||
|
|
||||||
#ifdef RUN_CHECKS
|
#ifdef RUN_CHECKS
|
||||||
if (cctx->nextPosHashed != cctx->ip && (cctx->ip != cctx->ibase)) {
|
if (cctx->nextPosHashed != cctx->ip && (cctx->ip != cctx->ibase)) {
|
||||||
printf("CHECK failed: putHashOfCurrentPosition %zu\n", cctx->ip - cctx->ibase);
|
printf("CHECK failed: putHashOfCurrentPosition %zu\n",
|
||||||
}
|
cctx->ip - cctx->ibase);
|
||||||
#endif
|
|
||||||
// hash_t hash = LDM_hashPosition(cctx->ip);
|
|
||||||
LDM_putHashOfCurrentPositionFromHash(cctx, hash, sum);
|
|
||||||
// printf("Offset %zu\n", cctx->ip - cctx->ibase);
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
static hash_t LDM_hash(U32 sequence) {
|
|
||||||
return ((sequence * 2654435761U) >> ((32)-LDM_HASHLOG));
|
|
||||||
}
|
|
||||||
|
|
||||||
static hash_t LDM_hashPosition(const void * const p) {
|
|
||||||
return LDM_hash(LDM_read32(p));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void LDM_putHashOfCurrentPositionFromHash(
|
|
||||||
LDM_CCtx *cctx, hash_t hash) {
|
|
||||||
/*
|
|
||||||
if (((cctx->ip - cctx->ibase) & HASH_EVERY) != HASH_EVERY) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
#ifdef COMPUTE_STATS
|
|
||||||
if (cctx->stats.numHashInserts < LDM_HASHTABLESIZE_U32) {
|
|
||||||
offset_t offset = (cctx->hashTable)[hash].offset;
|
|
||||||
cctx->stats.numHashInserts++;
|
|
||||||
|
|
||||||
if (offset != 0 && !LDM_isValidMatch(cctx->ip, offset + cctx->ibase)) {
|
|
||||||
cctx->stats.numCollisions++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
(cctx->hashTable)[hash] = (LDM_hashEntry){ (hash_t)(cctx->ip - cctx->ibase) };
|
putHashOfCurrentPositionFromHash(cctx, hash, sum);
|
||||||
#ifdef RUN_CHECKS
|
|
||||||
if (cctx->ip - cctx->lastPosHashed != 1) {
|
|
||||||
printf("putHashError\n");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
cctx->lastPosHashed = cctx->ip;
|
|
||||||
cctx->lastHash = hash;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LDM_putHashOfCurrentPosition(LDM_CCtx *cctx) {
|
/**
|
||||||
hash_t hash = LDM_hashPosition(cctx->ip);
|
* Returns the position of the entry at hashTable[hash].
|
||||||
LDM_putHashOfCurrentPositionFromHash(cctx, hash);
|
*/
|
||||||
|
static const BYTE *getPositionOnHash(LDM_CCtx *cctx, hash_t hash) {
|
||||||
|
return cctx->hashTable[hash].offset + cctx->ibase;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
/**
|
||||||
|
* Counts the number of bytes that match from pIn and pMatch,
|
||||||
/*
|
* up to pInLimit.
|
||||||
static hash_t LDM_hash5(U64 sequence) {
|
*
|
||||||
static const U64 prime5bytes = 889523592379ULL;
|
* TODO: make more efficient.
|
||||||
static const U64 prime8bytes = 11400714785074694791ULL;
|
*/
|
||||||
const U32 hashLog = LDM_HASHLOG;
|
static unsigned countMatchLength(const BYTE *pIn, const BYTE *pMatch,
|
||||||
if (LDM_isLittleEndian())
|
const BYTE *pInLimit) {
|
||||||
return (((sequence << 24) * prime5bytes) >> (64 - hashLog));
|
|
||||||
else
|
|
||||||
return (((sequence >> 24) * prime8bytes) >> (64 - hashLog));
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
static const BYTE *LDM_getPositionOnHash(
|
|
||||||
hash_t h, void *tableBase, const BYTE *srcBase) {
|
|
||||||
const LDM_hashEntry * const hashTable = (LDM_hashEntry *)tableBase;
|
|
||||||
return hashTable[h].offset + srcBase;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static unsigned LDM_count(const BYTE *pIn, const BYTE *pMatch,
|
|
||||||
const BYTE *pInLimit) {
|
|
||||||
const BYTE * const pStart = pIn;
|
const BYTE * const pStart = pIn;
|
||||||
while (pIn < pInLimit - 1) {
|
while (pIn < pInLimit - 1) {
|
||||||
BYTE const diff = LDM_readByte(pMatch) ^ LDM_readByte(pIn);
|
BYTE const diff = LDM_readByte(pMatch) ^ LDM_readByte(pIn);
|
||||||
@@ -386,9 +351,12 @@ void LDM_readHeader(const void *src, size_t *compressSize,
|
|||||||
*decompressSize = *ip;
|
*decompressSize = *ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LDM_initializeCCtx(LDM_CCtx *cctx,
|
/**
|
||||||
const void *src, size_t srcSize,
|
* Initialize a compression context.
|
||||||
void *dst, size_t maxDstSize) {
|
*/
|
||||||
|
static void initializeCCtx(LDM_CCtx *cctx,
|
||||||
|
const void *src, size_t srcSize,
|
||||||
|
void *dst, size_t maxDstSize) {
|
||||||
cctx->isize = srcSize;
|
cctx->isize = srcSize;
|
||||||
cctx->maxOSize = maxDstSize;
|
cctx->maxOSize = maxDstSize;
|
||||||
|
|
||||||
@@ -396,11 +364,7 @@ static void LDM_initializeCCtx(LDM_CCtx *cctx,
|
|||||||
cctx->ip = cctx->ibase;
|
cctx->ip = cctx->ibase;
|
||||||
cctx->iend = cctx->ibase + srcSize;
|
cctx->iend = cctx->ibase + srcSize;
|
||||||
|
|
||||||
#ifdef LDM_ROLLING_HASH
|
|
||||||
cctx->ihashLimit = cctx->iend - LDM_HASH_LENGTH;
|
cctx->ihashLimit = cctx->iend - LDM_HASH_LENGTH;
|
||||||
#else
|
|
||||||
cctx->ihashLimit = cctx->iend - HASH_SIZE;
|
|
||||||
#endif
|
|
||||||
cctx->imatchLimit = cctx->iend - MINMATCH;
|
cctx->imatchLimit = cctx->iend - MINMATCH;
|
||||||
|
|
||||||
cctx->obase = (BYTE *)dst;
|
cctx->obase = (BYTE *)dst;
|
||||||
@@ -413,23 +377,27 @@ static void LDM_initializeCCtx(LDM_CCtx *cctx,
|
|||||||
|
|
||||||
cctx->lastPosHashed = NULL;
|
cctx->lastPosHashed = NULL;
|
||||||
|
|
||||||
cctx->step = 1;
|
cctx->step = 1; // Fixed to be 1 for now. Changing may break things.
|
||||||
cctx->nextIp = cctx->ip + cctx->step;
|
cctx->nextIp = cctx->ip + cctx->step;
|
||||||
cctx->nextPosHashed = 0;
|
cctx->nextPosHashed = 0;
|
||||||
|
|
||||||
cctx->DEBUG_setNextHash = 0;
|
cctx->DEBUG_setNextHash = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef LDM_ROLLING_HASH
|
/**
|
||||||
|
* Finds the "best" match.
|
||||||
|
*
|
||||||
|
* Returns 0 if successful and 1 otherwise (i.e. no match can be found
|
||||||
|
* in the remaining input that is long enough).
|
||||||
|
*
|
||||||
|
*/
|
||||||
static int LDM_findBestMatch(LDM_CCtx *cctx, const BYTE **match) {
|
static int LDM_findBestMatch(LDM_CCtx *cctx, const BYTE **match) {
|
||||||
cctx->nextIp = cctx->ip + cctx->step;
|
cctx->nextIp = cctx->ip + cctx->step;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
hash_t h;
|
hash_t h;
|
||||||
U32 sum;
|
U32 sum;
|
||||||
// printf("Call A\n");
|
setNextHash(cctx);
|
||||||
LDM_setNextHash(cctx);
|
|
||||||
// printf("End call a\n");
|
|
||||||
h = cctx->nextHash;
|
h = cctx->nextHash;
|
||||||
sum = cctx->nextSum;
|
sum = cctx->nextSum;
|
||||||
cctx->ip = cctx->nextIp;
|
cctx->ip = cctx->nextIp;
|
||||||
@@ -439,62 +407,27 @@ static int LDM_findBestMatch(LDM_CCtx *cctx, const BYTE **match) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
*match = LDM_getPositionOnHash(h, cctx->hashTable, cctx->ibase);
|
*match = getPositionOnHash(cctx, h);
|
||||||
|
putHashOfCurrentPositionFromHash(cctx, h, sum);
|
||||||
// // Compute cctx->nextSum and cctx->nextHash from cctx->nextIp.
|
|
||||||
// LDM_setNextHash(cctx);
|
|
||||||
LDM_putHashOfCurrentPositionFromHash(cctx, h, sum);
|
|
||||||
|
|
||||||
// printf("%u %u\n", cctx->lastHash, cctx->nextHash);
|
|
||||||
} while (cctx->ip - *match > WINDOW_SIZE ||
|
|
||||||
!LDM_isValidMatch(cctx->ip, *match));
|
|
||||||
// LDM_read64(*match) != LDM_read64(cctx->ip));
|
|
||||||
LDM_setNextHash(cctx);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
static int LDM_findBestMatch(LDM_CCtx *cctx, const BYTE **match) {
|
|
||||||
cctx->nextIp = cctx->ip;
|
|
||||||
|
|
||||||
do {
|
|
||||||
hash_t const h = cctx->nextHash;
|
|
||||||
cctx->ip = cctx->nextIp;
|
|
||||||
cctx->nextIp += cctx->step;
|
|
||||||
|
|
||||||
if (cctx->ip > cctx->imatchLimit) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
*match = LDM_getPositionOnHash(h, cctx->hashTable, cctx->ibase);
|
|
||||||
|
|
||||||
cctx->nextHash = LDM_hashPosition(cctx->nextIp);
|
|
||||||
LDM_putHashOfCurrentPositionFromHash(cctx, h);
|
|
||||||
|
|
||||||
} while (cctx->ip - *match > WINDOW_SIZE ||
|
} while (cctx->ip - *match > WINDOW_SIZE ||
|
||||||
!LDM_isValidMatch(cctx->ip, *match));
|
!LDM_isValidMatch(cctx->ip, *match));
|
||||||
|
setNextHash(cctx);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write current block (literals, literal length, match offset,
|
* Write current block (literals, literal length, match offset,
|
||||||
* match length).
|
* match length).
|
||||||
*
|
*
|
||||||
* Update input pointer, inserting hashes into hash table along the
|
* Update input pointer, inserting hashes into hash table along the way.
|
||||||
* way.
|
|
||||||
*/
|
*/
|
||||||
static void LDM_outputBlock(LDM_CCtx *cctx, const BYTE *match) {
|
static void outputBlock(LDM_CCtx *cctx,
|
||||||
unsigned const literalLength = (unsigned)(cctx->ip - cctx->anchor);
|
unsigned const literalLength,
|
||||||
unsigned const offset = cctx->ip - match;
|
unsigned const offset,
|
||||||
unsigned const matchLength = LDM_count(
|
unsigned const matchLength) {
|
||||||
cctx->ip + MINMATCH, match + MINMATCH, cctx->ihashLimit);
|
|
||||||
BYTE *token = cctx->op++;
|
BYTE *token = cctx->op++;
|
||||||
|
|
||||||
cctx->stats.totalLiteralLength += literalLength;
|
|
||||||
cctx->stats.totalOffset += offset;
|
|
||||||
cctx->stats.totalMatchLength += matchLength + MINMATCH;
|
|
||||||
|
|
||||||
/* Encode the literal length. */
|
/* Encode the literal length. */
|
||||||
if (literalLength >= RUN_MASK) {
|
if (literalLength >= RUN_MASK) {
|
||||||
int len = (int)literalLength - RUN_MASK;
|
int len = (int)literalLength - RUN_MASK;
|
||||||
@@ -515,7 +448,7 @@ static void LDM_outputBlock(LDM_CCtx *cctx, const BYTE *match) {
|
|||||||
LDM_write32(cctx->op, offset);
|
LDM_write32(cctx->op, offset);
|
||||||
cctx->op += LDM_OFFSET_SIZE;
|
cctx->op += LDM_OFFSET_SIZE;
|
||||||
|
|
||||||
/* Encode match length */
|
/* Encode the match length. */
|
||||||
if (matchLength >= ML_MASK) {
|
if (matchLength >= ML_MASK) {
|
||||||
unsigned matchLengthRemaining = matchLength;
|
unsigned matchLengthRemaining = matchLength;
|
||||||
*token += ML_MASK;
|
*token += ML_MASK;
|
||||||
@@ -531,62 +464,21 @@ static void LDM_outputBlock(LDM_CCtx *cctx, const BYTE *match) {
|
|||||||
} else {
|
} else {
|
||||||
*token += (BYTE)(matchLength);
|
*token += (BYTE)(matchLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
// LDM_setNextHash(cctx);
|
|
||||||
// cctx->ip = cctx->lastPosHashed + 1;
|
|
||||||
// cctx->nextIp = cctx->ip + cctx->step;
|
|
||||||
// printf("HERE: %zu %zu %zu\n", cctx->ip - cctx->ibase,
|
|
||||||
// cctx->lastPosHashed - cctx->ibase, cctx->nextIp - cctx->ibase);
|
|
||||||
|
|
||||||
cctx->nextIp = cctx->ip + cctx->step;
|
|
||||||
|
|
||||||
while (cctx->ip < cctx->anchor + MINMATCH + matchLength + literalLength) {
|
|
||||||
// printf("Loop\n");
|
|
||||||
if (cctx->ip > cctx->lastPosHashed) {
|
|
||||||
#ifdef LDM_ROLLING_HASH
|
|
||||||
LDM_updateLastHashFromNextHash(cctx);
|
|
||||||
LDM_setNextHash(cctx);
|
|
||||||
#else
|
|
||||||
LDM_putHashOfCurrentPosition(cctx);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
printf("Call b %zu %zu %zu\n",
|
|
||||||
cctx->lastPosHashed - cctx->ibase,
|
|
||||||
cctx->nextIp - cctx->ibase,
|
|
||||||
cctx->ip - cctx->ibase);
|
|
||||||
*/
|
|
||||||
// printf("end call b\n");
|
|
||||||
cctx->ip++;
|
|
||||||
cctx->nextIp++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// printf("There: %zu %zu\n", cctx->ip - cctx->ibase, cctx->lastPosHashed - cctx->ibase);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: srcSize and maxDstSize is unused
|
// TODO: srcSize and maxDstSize is unused
|
||||||
|
// This is based upon lz4.
|
||||||
size_t LDM_compress(const void *src, size_t srcSize,
|
size_t LDM_compress(const void *src, size_t srcSize,
|
||||||
void *dst, size_t maxDstSize) {
|
void *dst, size_t maxDstSize) {
|
||||||
LDM_CCtx cctx;
|
LDM_CCtx cctx;
|
||||||
LDM_initializeCCtx(&cctx, src, srcSize, dst, maxDstSize);
|
initializeCCtx(&cctx, src, srcSize, dst, maxDstSize);
|
||||||
|
|
||||||
/* Hash the first position and put it into the hash table. */
|
/* Hash the first position and put it into the hash table. */
|
||||||
LDM_putHashOfCurrentPosition(&cctx);
|
LDM_putHashOfCurrentPosition(&cctx);
|
||||||
#ifdef LDM_ROLLING_HASH
|
|
||||||
// LDM_setNextHash(&cctx);
|
|
||||||
// tmp_hash = LDM_updateRollingHash(cctx.lastSum, LDM_HASH_LENGTH,
|
|
||||||
// cctx.ip[0], cctx.ip[LDM_HASH_LENGTH]);
|
|
||||||
// printf("Update test: %u %u\n", tmp_hash, cctx.nextSum);
|
|
||||||
// cctx.ip++;
|
|
||||||
#else
|
|
||||||
cctx.ip++;
|
|
||||||
cctx.nextHash = LDM_hashPosition(cctx.ip);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// TODO: loop condition is not accurate.
|
// TODO: loop condition is not accurate.
|
||||||
while (1) {
|
while (1) {
|
||||||
const BYTE *match;
|
const BYTE *match;
|
||||||
// printf("Start of loop\n");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a match.
|
* Find a match.
|
||||||
@@ -597,16 +489,15 @@ size_t LDM_compress(const void *src, size_t srcSize,
|
|||||||
if (LDM_findBestMatch(&cctx, &match) != 0) {
|
if (LDM_findBestMatch(&cctx, &match) != 0) {
|
||||||
goto _last_literals;
|
goto _last_literals;
|
||||||
}
|
}
|
||||||
// printf("End of match finding\n");
|
#ifdef COMPUTE_STATS
|
||||||
|
|
||||||
cctx.stats.numMatches++;
|
cctx.stats.numMatches++;
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Catch up: look back to extend the match backwards from the found match.
|
* Catch up: look back to extend the match backwards from the found match.
|
||||||
*/
|
*/
|
||||||
while (cctx.ip > cctx.anchor && match > cctx.ibase &&
|
while (cctx.ip > cctx.anchor && match > cctx.ibase &&
|
||||||
cctx.ip[-1] == match[-1]) {
|
cctx.ip[-1] == match[-1]) {
|
||||||
// printf("Catch up\n");
|
|
||||||
cctx.ip--;
|
cctx.ip--;
|
||||||
match--;
|
match--;
|
||||||
}
|
}
|
||||||
@@ -615,26 +506,35 @@ size_t LDM_compress(const void *src, size_t srcSize,
|
|||||||
* Write current block (literals, literal length, match offset, match
|
* Write current block (literals, literal length, match offset, match
|
||||||
* length) and update pointers and hashes.
|
* length) and update pointers and hashes.
|
||||||
*/
|
*/
|
||||||
LDM_outputBlock(&cctx, match);
|
{
|
||||||
// printf("End of loop\n");
|
unsigned const literalLength = (unsigned)(cctx.ip - cctx.anchor);
|
||||||
|
unsigned const offset = cctx.ip - match;
|
||||||
|
unsigned const matchLength = countMatchLength(
|
||||||
|
cctx.ip + MINMATCH, match + MINMATCH, cctx.ihashLimit);
|
||||||
|
|
||||||
|
#ifdef COMPUTE_STATS
|
||||||
|
cctx.stats.totalLiteralLength += literalLength;
|
||||||
|
cctx.stats.totalOffset += offset;
|
||||||
|
cctx.stats.totalMatchLength += matchLength + MINMATCH;
|
||||||
|
#endif
|
||||||
|
outputBlock(&cctx, literalLength, offset, matchLength);
|
||||||
|
|
||||||
|
// Move ip to end of block, inserting hashes at each position.
|
||||||
|
cctx.nextIp = cctx.ip + cctx.step;
|
||||||
|
while (cctx.ip < cctx.anchor + MINMATCH + matchLength + literalLength) {
|
||||||
|
if (cctx.ip > cctx.lastPosHashed) {
|
||||||
|
// TODO: Simplify.
|
||||||
|
LDM_updateLastHashFromNextHash(&cctx);
|
||||||
|
setNextHash(&cctx);
|
||||||
|
}
|
||||||
|
cctx.ip++;
|
||||||
|
cctx.nextIp++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set start of next block to current input pointer.
|
// Set start of next block to current input pointer.
|
||||||
cctx.anchor = cctx.ip;
|
cctx.anchor = cctx.ip;
|
||||||
#ifdef LDM_ROLLING_HASH
|
|
||||||
LDM_updateLastHashFromNextHash(&cctx);
|
LDM_updateLastHashFromNextHash(&cctx);
|
||||||
#else
|
|
||||||
LDM_putHashOfCurrentPosition(&cctx);
|
|
||||||
cctx.ip++;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
|
||||||
LDM_putHashOfCurrentPosition(&cctx);
|
|
||||||
printf("Call c\n");
|
|
||||||
LDM_setNextHash(&cctx);
|
|
||||||
printf("End call c\n");
|
|
||||||
cctx.ip++;
|
|
||||||
cctx.nextIp++;
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
_last_literals:
|
_last_literals:
|
||||||
/* Encode the last literals (no more matches). */
|
/* Encode the last literals (no more matches). */
|
||||||
@@ -653,18 +553,11 @@ _last_literals:
|
|||||||
memcpy(cctx.op, cctx.anchor, lastRun);
|
memcpy(cctx.op, cctx.anchor, lastRun);
|
||||||
cctx.op += lastRun;
|
cctx.op += lastRun;
|
||||||
}
|
}
|
||||||
LDM_printCompressStats(&cctx.stats);
|
|
||||||
|
|
||||||
{
|
#ifdef COMPUTE_STATS
|
||||||
U32 tmp = 0;
|
printCompressStats(&cctx);
|
||||||
U32 ctr = 0;
|
#endif
|
||||||
for (; tmp < LDM_HASH_SIZE_U32; tmp++) {
|
|
||||||
if ((cctx.hashTable)[tmp].offset == 0) {
|
|
||||||
ctr++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("HASH: %u %u\n", ctr, LDM_HASH_SIZE_U32);
|
|
||||||
}
|
|
||||||
return (cctx.op - (const BYTE *)cctx.obase);
|
return (cctx.op - (const BYTE *)cctx.obase);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -715,7 +608,7 @@ size_t LDM_decompress(const void *src, size_t compressSize,
|
|||||||
} while (s == 255);
|
} while (s == 255);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Copy literals. */
|
/* Copy the literals. */
|
||||||
cpy = dctx.op + length;
|
cpy = dctx.op + length;
|
||||||
memcpy(dctx.op, dctx.ip, length);
|
memcpy(dctx.op, dctx.ip, length);
|
||||||
dctx.ip += length;
|
dctx.ip += length;
|
||||||
@@ -748,20 +641,20 @@ size_t LDM_decompress(const void *src, size_t compressSize,
|
|||||||
return dctx.op - (BYTE *)dst;
|
return dctx.op - (BYTE *)dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
void LDM_test(const void *src, size_t srcSize,
|
void LDM_test(const void *src, size_t srcSize,
|
||||||
void *dst, size_t maxDstSize) {
|
void *dst, size_t maxDstSize) {
|
||||||
#ifdef LDM_ROLLING_HASH
|
|
||||||
const BYTE *ip = (const BYTE *)src + 1125;
|
const BYTE *ip = (const BYTE *)src + 1125;
|
||||||
U32 sum = LDM_getRollingHash((const char *)ip, LDM_HASH_LENGTH);
|
U32 sum = getChecksum((const char *)ip, LDM_HASH_LENGTH);
|
||||||
U32 sum2;
|
U32 sum2;
|
||||||
++ip;
|
++ip;
|
||||||
for (; ip < (const BYTE *)src + 1125 + 100; ip++) {
|
for (; ip < (const BYTE *)src + 1125 + 100; ip++) {
|
||||||
sum2 = LDM_updateRollingHash(sum, LDM_HASH_LENGTH,
|
sum2 = updateChecksum(sum, LDM_HASH_LENGTH,
|
||||||
ip[-1], ip[LDM_HASH_LENGTH - 1]);
|
ip[-1], ip[LDM_HASH_LENGTH - 1]);
|
||||||
sum = LDM_getRollingHash((const char *)ip, LDM_HASH_LENGTH);
|
sum = getChecksum((const char *)ip, LDM_HASH_LENGTH);
|
||||||
printf("TEST HASH: %zu %u %u\n", ip - (const BYTE *)src, sum, sum2);
|
printf("TEST HASH: %zu %u %u\n", ip - (const BYTE *)src, sum, sum2);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,12 +7,45 @@
|
|||||||
#define LDM_DECOMPRESS_SIZE 4
|
#define LDM_DECOMPRESS_SIZE 4
|
||||||
#define LDM_HEADER_SIZE ((LDM_COMPRESS_SIZE)+(LDM_DECOMPRESS_SIZE))
|
#define LDM_HEADER_SIZE ((LDM_COMPRESS_SIZE)+(LDM_DECOMPRESS_SIZE))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compresses src into dst.
|
||||||
|
*
|
||||||
|
* NB: This currently ignores maxDstSize and assumes enough space is available.
|
||||||
|
*
|
||||||
|
* Block format (see lz4 documentation for more information):
|
||||||
|
* github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md
|
||||||
|
*
|
||||||
|
* A block is composed of sequences. Each sequence begins with a token, which
|
||||||
|
* is a one-byte value separated into two 4-bit fields.
|
||||||
|
*
|
||||||
|
* The first field uses the four high bits of the token and encodes the literal
|
||||||
|
* length. If the field value is 0, there is no literal. If it is 15,
|
||||||
|
* additional bytes are added (each ranging from 0 to 255) to the previous
|
||||||
|
* value to produce a total length.
|
||||||
|
*
|
||||||
|
* Following the token and optional length bytes are the literals.
|
||||||
|
*
|
||||||
|
* Next are the 4 bytes representing the offset of the match (2 in lz4),
|
||||||
|
* representing the position to copy the literals.
|
||||||
|
*
|
||||||
|
* The lower four bits of the token encode the match length. With additional
|
||||||
|
* bytes added similarly to the additional literal length bytes after the offset.
|
||||||
|
*
|
||||||
|
* The last sequence is incomplete and stops right after the lieterals.
|
||||||
|
*
|
||||||
|
*/
|
||||||
size_t LDM_compress(const void *src, size_t srcSize,
|
size_t LDM_compress(const void *src, size_t srcSize,
|
||||||
void *dst, size_t maxDstSize);
|
void *dst, size_t maxDstSize);
|
||||||
|
|
||||||
size_t LDM_decompress(const void *src, size_t srcSize,
|
size_t LDM_decompress(const void *src, size_t srcSize,
|
||||||
void *dst, size_t maxDstSize);
|
void *dst, size_t maxDstSize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the header from src and writes the compressed size and
|
||||||
|
* decompressed size into compressSize and decompressSize respectively.
|
||||||
|
*
|
||||||
|
* NB: LDM_compress and LDM_decompress currently do not add/read headers.
|
||||||
|
*/
|
||||||
void LDM_readHeader(const void *src, size_t *compressSize,
|
void LDM_readHeader(const void *src, size_t *compressSize,
|
||||||
size_t *decompressSize);
|
size_t *decompressSize);
|
||||||
|
|
||||||
|
|||||||
@@ -13,12 +13,9 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include "ldm.h"
|
#include "ldm.h"
|
||||||
|
|
||||||
// #define BUF_SIZE 16*1024 // Block size
|
|
||||||
#define DEBUG
|
#define DEBUG
|
||||||
//#define TEST
|
//#define TEST
|
||||||
|
|
||||||
//#define ZSTD
|
|
||||||
|
|
||||||
/* Compress file given by fname and output to oname.
|
/* Compress file given by fname and output to oname.
|
||||||
* Returns 0 if successful, error code otherwise.
|
* Returns 0 if successful, error code otherwise.
|
||||||
*/
|
*/
|
||||||
@@ -75,28 +72,25 @@ static int compress(const char *fname, const char *oname) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
#ifdef TEST
|
#ifdef TEST
|
||||||
LDM_test(src, statbuf.st_size,
|
LDM_test(src, statbuf.st_size,
|
||||||
dst + LDM_HEADER_SIZE, statbuf.st_size);
|
dst + LDM_HEADER_SIZE, statbuf.st_size);
|
||||||
#endif
|
#endif
|
||||||
|
*/
|
||||||
|
|
||||||
#ifdef ZSTD
|
|
||||||
compressSize = ZSTD_compress(dst, statbuf.st_size,
|
|
||||||
src, statbuf.st_size, 1);
|
|
||||||
#else
|
|
||||||
compressSize = LDM_HEADER_SIZE +
|
compressSize = LDM_HEADER_SIZE +
|
||||||
LDM_compress(src, statbuf.st_size,
|
LDM_compress(src, statbuf.st_size,
|
||||||
dst + LDM_HEADER_SIZE, statbuf.st_size);
|
dst + LDM_HEADER_SIZE, statbuf.st_size);
|
||||||
|
|
||||||
// Write compress and decompress size to header
|
// Write compress and decompress size to header
|
||||||
// TODO: should depend on LDM_DECOMPRESS_SIZE write32
|
// TODO: should depend on LDM_DECOMPRESS_SIZE write32
|
||||||
memcpy(dst, &compressSize, 4);
|
memcpy(dst, &compressSize, 4);
|
||||||
memcpy(dst + 4, &(statbuf.st_size), 4);
|
memcpy(dst + 4, &(statbuf.st_size), 4);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf("Compressed size: %zu\n", compressSize);
|
printf("Compressed size: %zu\n", compressSize);
|
||||||
printf("Decompressed size: %zu\n", (size_t)statbuf.st_size);
|
printf("Decompressed size: %zu\n", (size_t)statbuf.st_size);
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Truncate file to compressSize.
|
// Truncate file to compressSize.
|
||||||
@@ -169,17 +163,11 @@ static int decompress(const char *fname, const char *oname) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ZSTD
|
|
||||||
outSize = ZSTD_decompress(dst, decomrpessed_size,
|
|
||||||
src + LDM_HEADER_SIZE,
|
|
||||||
statbuf.st_size - LDM_HEADER_SIZE);
|
|
||||||
#else
|
|
||||||
outSize = LDM_decompress(
|
outSize = LDM_decompress(
|
||||||
src + LDM_HEADER_SIZE, statbuf.st_size - LDM_HEADER_SIZE,
|
src + LDM_HEADER_SIZE, statbuf.st_size - LDM_HEADER_SIZE,
|
||||||
dst, decompressSize);
|
dst, decompressSize);
|
||||||
|
|
||||||
printf("Ret size out: %zu\n", outSize);
|
printf("Ret size out: %zu\n", outSize);
|
||||||
#endif
|
|
||||||
ftruncate(fdout, outSize);
|
ftruncate(fdout, outSize);
|
||||||
|
|
||||||
close(fdin);
|
close(fdin);
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
# ################################################################
|
||||||
|
# Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# This source code is licensed under the BSD-style license found in the
|
||||||
|
# LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
# of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
# ################################################################
|
||||||
|
|
||||||
|
# This Makefile presumes libzstd is installed, using `sudo make install`
|
||||||
|
|
||||||
|
CFLAGS ?= -O3
|
||||||
|
DEBUGFLAGS = -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
|
||||||
|
-Wstrict-aliasing=1 -Wswitch-enum -Wdeclaration-after-statement \
|
||||||
|
-Wstrict-prototypes -Wundef -Wpointer-arith -Wformat-security \
|
||||||
|
-Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \
|
||||||
|
-Wredundant-decls
|
||||||
|
CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS)
|
||||||
|
FLAGS = $(CPPFLAGS) $(CFLAGS)
|
||||||
|
|
||||||
|
LDFLAGS += -lzstd
|
||||||
|
|
||||||
|
.PHONY: default all clean
|
||||||
|
|
||||||
|
default: all
|
||||||
|
|
||||||
|
all: main-ldm
|
||||||
|
|
||||||
|
|
||||||
|
#main : ldm.c main.c
|
||||||
|
# $(CC) $(CPPFLAGS) $(CFLAGS) $^ $(LDFLAGS) -o $@
|
||||||
|
|
||||||
|
main-ldm : util.c ldm.c main-ldm.c
|
||||||
|
$(CC) $(CPPFLAGS) $(CFLAGS) $^ $(LDFLAGS) -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -f core *.o tmp* result* *.ldm *.ldm.dec \
|
||||||
|
main main-ldm
|
||||||
|
@echo Cleaning completed
|
||||||
|
|
||||||
@@ -0,0 +1,659 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "ldm.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
// Insert every (HASH_ONLY_EVERY + 1) into the hash table.
|
||||||
|
#define HASH_ONLY_EVERY 0
|
||||||
|
|
||||||
|
#define LDM_MEMORY_USAGE 20
|
||||||
|
#define LDM_HASHLOG (LDM_MEMORY_USAGE-2)
|
||||||
|
#define LDM_HASHTABLESIZE (1 << (LDM_MEMORY_USAGE))
|
||||||
|
#define LDM_HASHTABLESIZE_U32 ((LDM_HASHTABLESIZE) >> 2)
|
||||||
|
|
||||||
|
#define LDM_OFFSET_SIZE 4
|
||||||
|
|
||||||
|
#define WINDOW_SIZE (1 << 20)
|
||||||
|
|
||||||
|
//These should be multiples of four.
|
||||||
|
#define LDM_HASH_LENGTH 100
|
||||||
|
#define MINMATCH 100
|
||||||
|
|
||||||
|
#define ML_BITS 4
|
||||||
|
#define ML_MASK ((1U<<ML_BITS)-1)
|
||||||
|
#define RUN_BITS (8-ML_BITS)
|
||||||
|
#define RUN_MASK ((1U<<RUN_BITS)-1)
|
||||||
|
|
||||||
|
#define COMPUTE_STATS
|
||||||
|
//#define RUN_CHECKS
|
||||||
|
//#define LDM_DEBUG
|
||||||
|
|
||||||
|
typedef uint8_t BYTE;
|
||||||
|
typedef uint16_t U16;
|
||||||
|
typedef uint32_t U32;
|
||||||
|
typedef int32_t S32;
|
||||||
|
typedef uint64_t U64;
|
||||||
|
|
||||||
|
typedef uint32_t offset_t;
|
||||||
|
typedef uint32_t hash_t;
|
||||||
|
typedef signed char schar;
|
||||||
|
|
||||||
|
typedef struct hashEntry {
|
||||||
|
offset_t offset;
|
||||||
|
} hashEntry;
|
||||||
|
|
||||||
|
typedef struct LDM_compressStats {
|
||||||
|
U32 numMatches;
|
||||||
|
U32 totalMatchLength;
|
||||||
|
U32 totalLiteralLength;
|
||||||
|
U64 totalOffset;
|
||||||
|
|
||||||
|
U32 numCollisions;
|
||||||
|
U32 numHashInserts;
|
||||||
|
} LDM_compressStats;
|
||||||
|
|
||||||
|
typedef struct LDM_CCtx {
|
||||||
|
size_t isize; /* Input size */
|
||||||
|
size_t maxOSize; /* Maximum output size */
|
||||||
|
|
||||||
|
const BYTE *ibase; /* Base of input */
|
||||||
|
const BYTE *ip; /* Current input position */
|
||||||
|
const BYTE *iend; /* End of input */
|
||||||
|
|
||||||
|
// Maximum input position such that hashing at the position does not exceed
|
||||||
|
// end of input.
|
||||||
|
const BYTE *ihashLimit;
|
||||||
|
|
||||||
|
// Maximum input position such that finding a match of at least the minimum
|
||||||
|
// match length does not exceed end of input.
|
||||||
|
const BYTE *imatchLimit;
|
||||||
|
|
||||||
|
const BYTE *obase; /* Base of output */
|
||||||
|
BYTE *op; /* Output */
|
||||||
|
|
||||||
|
const BYTE *anchor; /* Anchor to start of current (match) block */
|
||||||
|
|
||||||
|
LDM_compressStats stats; /* Compression statistics */
|
||||||
|
|
||||||
|
hashEntry hashTable[LDM_HASHTABLESIZE_U32];
|
||||||
|
|
||||||
|
const BYTE *lastPosHashed; /* Last position hashed */
|
||||||
|
hash_t lastHash; /* Hash corresponding to lastPosHashed */
|
||||||
|
U32 lastSum;
|
||||||
|
|
||||||
|
const BYTE *nextIp; // TODO: this is redundant (ip + step)
|
||||||
|
const BYTE *nextPosHashed;
|
||||||
|
hash_t nextHash; /* Hash corresponding to nextPosHashed */
|
||||||
|
U32 nextSum;
|
||||||
|
|
||||||
|
unsigned step; // ip step, should be 1.
|
||||||
|
|
||||||
|
// DEBUG
|
||||||
|
const BYTE *DEBUG_setNextHash;
|
||||||
|
} LDM_CCtx;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Outputs compression statistics.
|
||||||
|
*/
|
||||||
|
static void printCompressStats(const LDM_CCtx *cctx) {
|
||||||
|
const LDM_compressStats *stats = &(cctx->stats);
|
||||||
|
#ifdef COMPUTE_STATS
|
||||||
|
printf("=====================\n");
|
||||||
|
printf("Compression statistics\n");
|
||||||
|
printf("Total number of matches: %u\n", stats->numMatches);
|
||||||
|
printf("Average match length: %.1f\n", ((double)stats->totalMatchLength) /
|
||||||
|
(double)stats->numMatches);
|
||||||
|
printf("Average literal length: %.1f\n",
|
||||||
|
((double)stats->totalLiteralLength) / (double)stats->numMatches);
|
||||||
|
printf("Average offset length: %.1f\n",
|
||||||
|
((double)stats->totalOffset) / (double)stats->numMatches);
|
||||||
|
printf("Num collisions, num hash inserts, %% collisions: %u, %u, %.3f\n",
|
||||||
|
stats->numCollisions, stats->numHashInserts,
|
||||||
|
stats->numHashInserts == 0 ?
|
||||||
|
1.0 : (100.0 * (double)stats->numCollisions) /
|
||||||
|
(double)stats->numHashInserts);
|
||||||
|
|
||||||
|
// Output occupancy of hash table.
|
||||||
|
{
|
||||||
|
U32 i = 0;
|
||||||
|
U32 ctr = 0;
|
||||||
|
for (; i < LDM_HASHTABLESIZE_U32; i++) {
|
||||||
|
if ((cctx->hashTable)[i].offset == 0) {
|
||||||
|
ctr++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("Hash table size, empty slots, %% empty: %u %u %.3f\n",
|
||||||
|
LDM_HASHTABLESIZE_U32, ctr,
|
||||||
|
100.0 * (double)(ctr) / (double)LDM_HASHTABLESIZE_U32);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("=====================\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the MINMATCH bytes from p are the same as the MINMATCH
|
||||||
|
* bytes from match.
|
||||||
|
*
|
||||||
|
* This assumes MINMATCH is a multiple of four.
|
||||||
|
*
|
||||||
|
* Return 1 if valid, 0 otherwise.
|
||||||
|
*/
|
||||||
|
static int LDM_isValidMatch(const BYTE *p, const BYTE *match) {
|
||||||
|
/*
|
||||||
|
if (memcmp(p, match, MINMATCH) == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
*/
|
||||||
|
|
||||||
|
//TODO: This seems to be faster for some reason?
|
||||||
|
U16 lengthLeft = MINMATCH;
|
||||||
|
const BYTE *curP = p;
|
||||||
|
const BYTE *curMatch = match;
|
||||||
|
|
||||||
|
for (; lengthLeft >= 8; lengthLeft -= 8) {
|
||||||
|
if (LDM_read64(curP) != LDM_read64(curMatch)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
curP += 8;
|
||||||
|
curMatch += 8;
|
||||||
|
}
|
||||||
|
if (lengthLeft > 0) {
|
||||||
|
return (LDM_read32(curP) == LDM_read32(curMatch));
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a sum computed from getChecksum to a hash value in the range
|
||||||
|
* of the hash table.
|
||||||
|
*/
|
||||||
|
static hash_t checksumToHash(U32 sum) {
|
||||||
|
return ((sum * 2654435761U) >> ((32)-LDM_HASHLOG));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes a checksum based on rsync's checksum.
|
||||||
|
*
|
||||||
|
* a(k,l) = \sum_{i = k}^l x_i (mod M)
|
||||||
|
* b(k,l) = \sum_{i = k}^l ((l - i + 1) * x_i) (mod M)
|
||||||
|
* checksum(k,l) = a(k,l) + 2^{16} * b(k,l)
|
||||||
|
*/
|
||||||
|
static U32 getChecksum(const char *data, U32 len) {
|
||||||
|
U32 i;
|
||||||
|
U32 s1, s2;
|
||||||
|
const schar *buf = (const schar *)data;
|
||||||
|
|
||||||
|
s1 = s2 = 0;
|
||||||
|
for (i = 0; i < (len - 4); i += 4) {
|
||||||
|
s2 += (4 * (s1 + buf[i])) + (3 * buf[i + 1]) +
|
||||||
|
(2 * buf[i + 2]) + (buf[i + 3]);
|
||||||
|
s1 += buf[i] + buf[i + 1] + buf[i + 2] + buf[i + 3];
|
||||||
|
}
|
||||||
|
for(; i < len; i++) {
|
||||||
|
s1 += buf[i];
|
||||||
|
s2 += s1;
|
||||||
|
}
|
||||||
|
return (s1 & 0xffff) + (s2 << 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a checksum computed from getChecksum(data, len).
|
||||||
|
*
|
||||||
|
* The checksum can be updated along its ends as follows:
|
||||||
|
* a(k+1, l+1) = (a(k,l) - x_k + x_{l+1}) (mod M)
|
||||||
|
* b(k+1, l+1) = (b(k,l) - (l-k+1)*x_k + (a(k+1,l+1)) (mod M)
|
||||||
|
*
|
||||||
|
* Thus toRemove should correspond to data[0].
|
||||||
|
*/
|
||||||
|
static U32 updateChecksum(U32 sum, U32 len,
|
||||||
|
schar toRemove, schar toAdd) {
|
||||||
|
U32 s1 = (sum & 0xffff) - toRemove + toAdd;
|
||||||
|
U32 s2 = (sum >> 16) - (toRemove * len) + s1;
|
||||||
|
|
||||||
|
return (s1 & 0xffff) + (s2 << 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update cctx->nextSum, cctx->nextHash, and cctx->nextPosHashed
|
||||||
|
* based on cctx->lastSum and cctx->lastPosHashed.
|
||||||
|
*
|
||||||
|
* This uses a rolling hash and requires that the last position hashed
|
||||||
|
* corresponds to cctx->nextIp - step.
|
||||||
|
*/
|
||||||
|
static void setNextHash(LDM_CCtx *cctx) {
|
||||||
|
#ifdef RUN_CHECKS
|
||||||
|
U32 check;
|
||||||
|
if ((cctx->nextIp - cctx->ibase != 1) &&
|
||||||
|
(cctx->nextIp - cctx->DEBUG_setNextHash != 1)) {
|
||||||
|
printf("CHECK debug fail: %zu %zu\n", cctx->nextIp - cctx->ibase,
|
||||||
|
cctx->DEBUG_setNextHash - cctx->ibase);
|
||||||
|
}
|
||||||
|
|
||||||
|
cctx->DEBUG_setNextHash = cctx->nextIp;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// cctx->nextSum = getChecksum((const char *)cctx->nextIp, LDM_HASH_LENGTH);
|
||||||
|
cctx->nextSum = updateChecksum(
|
||||||
|
cctx->lastSum, LDM_HASH_LENGTH,
|
||||||
|
(schar)((cctx->lastPosHashed)[0]),
|
||||||
|
(schar)((cctx->lastPosHashed)[LDM_HASH_LENGTH]));
|
||||||
|
cctx->nextPosHashed = cctx->nextIp;
|
||||||
|
cctx->nextHash = checksumToHash(cctx->nextSum);
|
||||||
|
|
||||||
|
#ifdef RUN_CHECKS
|
||||||
|
check = getChecksum((const char *)cctx->nextIp, LDM_HASH_LENGTH);
|
||||||
|
|
||||||
|
if (check != cctx->nextSum) {
|
||||||
|
printf("CHECK: setNextHash failed %u %u\n", check, cctx->nextSum);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((cctx->nextIp - cctx->lastPosHashed) != 1) {
|
||||||
|
printf("setNextHash: nextIp != lastPosHashed + 1. %zu %zu %zu\n",
|
||||||
|
cctx->nextIp - cctx->ibase, cctx->lastPosHashed - cctx->ibase,
|
||||||
|
cctx->ip - cctx->ibase);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void putHashOfCurrentPositionFromHash(
|
||||||
|
LDM_CCtx *cctx, hash_t hash, U32 sum) {
|
||||||
|
#ifdef COMPUTE_STATS
|
||||||
|
if (cctx->stats.numHashInserts < LDM_HASHTABLESIZE_U32) {
|
||||||
|
offset_t offset = (cctx->hashTable)[hash].offset;
|
||||||
|
cctx->stats.numHashInserts++;
|
||||||
|
if (offset != 0 && !LDM_isValidMatch(cctx->ip, offset + cctx->ibase)) {
|
||||||
|
cctx->stats.numCollisions++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Hash only every HASH_ONLY_EVERY times, based on cctx->ip.
|
||||||
|
// Note: this works only when cctx->step is 1.
|
||||||
|
if (((cctx->ip - cctx->ibase) & HASH_ONLY_EVERY) == HASH_ONLY_EVERY) {
|
||||||
|
(cctx->hashTable)[hash] = (hashEntry){ (offset_t)(cctx->ip - cctx->ibase) };
|
||||||
|
}
|
||||||
|
|
||||||
|
cctx->lastPosHashed = cctx->ip;
|
||||||
|
cctx->lastHash = hash;
|
||||||
|
cctx->lastSum = sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy over the cctx->lastHash, cctx->lastSum, and cctx->lastPosHashed
|
||||||
|
* fields from the "next" fields.
|
||||||
|
*
|
||||||
|
* This requires that cctx->ip == cctx->nextPosHashed.
|
||||||
|
*/
|
||||||
|
static void LDM_updateLastHashFromNextHash(LDM_CCtx *cctx) {
|
||||||
|
#ifdef RUN_CHECKS
|
||||||
|
if (cctx->ip != cctx->nextPosHashed) {
|
||||||
|
printf("CHECK failed: updateLastHashFromNextHash %zu\n",
|
||||||
|
cctx->ip - cctx->ibase);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
putHashOfCurrentPositionFromHash(cctx, cctx->nextHash, cctx->nextSum);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert hash of the current position into the hash table.
|
||||||
|
*/
|
||||||
|
static void LDM_putHashOfCurrentPosition(LDM_CCtx *cctx) {
|
||||||
|
U32 sum = getChecksum((const char *)cctx->ip, LDM_HASH_LENGTH);
|
||||||
|
hash_t hash = checksumToHash(sum);
|
||||||
|
|
||||||
|
#ifdef RUN_CHECKS
|
||||||
|
if (cctx->nextPosHashed != cctx->ip && (cctx->ip != cctx->ibase)) {
|
||||||
|
printf("CHECK failed: putHashOfCurrentPosition %zu\n",
|
||||||
|
cctx->ip - cctx->ibase);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
putHashOfCurrentPositionFromHash(cctx, hash, sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the position of the entry at hashTable[hash].
|
||||||
|
*/
|
||||||
|
static const BYTE *getPositionOnHash(LDM_CCtx *cctx, hash_t hash) {
|
||||||
|
return cctx->hashTable[hash].offset + cctx->ibase;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Counts the number of bytes that match from pIn and pMatch,
|
||||||
|
* up to pInLimit.
|
||||||
|
*
|
||||||
|
* TODO: make more efficient.
|
||||||
|
*/
|
||||||
|
static unsigned countMatchLength(const BYTE *pIn, const BYTE *pMatch,
|
||||||
|
const BYTE *pInLimit) {
|
||||||
|
const BYTE * const pStart = pIn;
|
||||||
|
while (pIn < pInLimit - 1) {
|
||||||
|
BYTE const diff = LDM_readByte(pMatch) ^ LDM_readByte(pIn);
|
||||||
|
if (!diff) {
|
||||||
|
pIn++;
|
||||||
|
pMatch++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return (unsigned)(pIn - pStart);
|
||||||
|
}
|
||||||
|
return (unsigned)(pIn - pStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LDM_readHeader(const void *src, size_t *compressSize,
|
||||||
|
size_t *decompressSize) {
|
||||||
|
const U32 *ip = (const U32 *)src;
|
||||||
|
*compressSize = *ip++;
|
||||||
|
*decompressSize = *ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize a compression context.
|
||||||
|
*/
|
||||||
|
static void initializeCCtx(LDM_CCtx *cctx,
|
||||||
|
const void *src, size_t srcSize,
|
||||||
|
void *dst, size_t maxDstSize) {
|
||||||
|
cctx->isize = srcSize;
|
||||||
|
cctx->maxOSize = maxDstSize;
|
||||||
|
|
||||||
|
cctx->ibase = (const BYTE *)src;
|
||||||
|
cctx->ip = cctx->ibase;
|
||||||
|
cctx->iend = cctx->ibase + srcSize;
|
||||||
|
|
||||||
|
cctx->ihashLimit = cctx->iend - LDM_HASH_LENGTH;
|
||||||
|
cctx->imatchLimit = cctx->iend - MINMATCH;
|
||||||
|
|
||||||
|
cctx->obase = (BYTE *)dst;
|
||||||
|
cctx->op = (BYTE *)dst;
|
||||||
|
|
||||||
|
cctx->anchor = cctx->ibase;
|
||||||
|
|
||||||
|
memset(&(cctx->stats), 0, sizeof(cctx->stats));
|
||||||
|
memset(cctx->hashTable, 0, sizeof(cctx->hashTable));
|
||||||
|
|
||||||
|
cctx->lastPosHashed = NULL;
|
||||||
|
|
||||||
|
cctx->step = 1; // Fixed to be 1 for now. Changing may break things.
|
||||||
|
cctx->nextIp = cctx->ip + cctx->step;
|
||||||
|
cctx->nextPosHashed = 0;
|
||||||
|
|
||||||
|
cctx->DEBUG_setNextHash = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds the "best" match.
|
||||||
|
*
|
||||||
|
* Returns 0 if successful and 1 otherwise (i.e. no match can be found
|
||||||
|
* in the remaining input that is long enough).
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static int LDM_findBestMatch(LDM_CCtx *cctx, const BYTE **match) {
|
||||||
|
cctx->nextIp = cctx->ip + cctx->step;
|
||||||
|
|
||||||
|
do {
|
||||||
|
hash_t h;
|
||||||
|
U32 sum;
|
||||||
|
setNextHash(cctx);
|
||||||
|
h = cctx->nextHash;
|
||||||
|
sum = cctx->nextSum;
|
||||||
|
cctx->ip = cctx->nextIp;
|
||||||
|
cctx->nextIp += cctx->step;
|
||||||
|
|
||||||
|
if (cctx->ip > cctx->imatchLimit) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*match = getPositionOnHash(cctx, h);
|
||||||
|
putHashOfCurrentPositionFromHash(cctx, h, sum);
|
||||||
|
|
||||||
|
} while (cctx->ip - *match > WINDOW_SIZE ||
|
||||||
|
!LDM_isValidMatch(cctx->ip, *match));
|
||||||
|
setNextHash(cctx);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write current block (literals, literal length, match offset,
|
||||||
|
* match length).
|
||||||
|
*
|
||||||
|
* Update input pointer, inserting hashes into hash table along the way.
|
||||||
|
*/
|
||||||
|
static void outputBlock(LDM_CCtx *cctx,
|
||||||
|
unsigned const literalLength,
|
||||||
|
unsigned const offset,
|
||||||
|
unsigned const matchLength) {
|
||||||
|
BYTE *token = cctx->op++;
|
||||||
|
|
||||||
|
/* Encode the literal length. */
|
||||||
|
if (literalLength >= RUN_MASK) {
|
||||||
|
int len = (int)literalLength - RUN_MASK;
|
||||||
|
*token = (RUN_MASK << ML_BITS);
|
||||||
|
for (; len >= 255; len -= 255) {
|
||||||
|
*(cctx->op)++ = 255;
|
||||||
|
}
|
||||||
|
*(cctx->op)++ = (BYTE)len;
|
||||||
|
} else {
|
||||||
|
*token = (BYTE)(literalLength << ML_BITS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Encode the literals. */
|
||||||
|
memcpy(cctx->op, cctx->anchor, literalLength);
|
||||||
|
cctx->op += literalLength;
|
||||||
|
|
||||||
|
/* Encode the offset. */
|
||||||
|
LDM_write32(cctx->op, offset);
|
||||||
|
cctx->op += LDM_OFFSET_SIZE;
|
||||||
|
|
||||||
|
/* Encode the match length. */
|
||||||
|
if (matchLength >= ML_MASK) {
|
||||||
|
unsigned matchLengthRemaining = matchLength;
|
||||||
|
*token += ML_MASK;
|
||||||
|
matchLengthRemaining -= ML_MASK;
|
||||||
|
LDM_write32(cctx->op, 0xFFFFFFFF);
|
||||||
|
while (matchLengthRemaining >= 4*0xFF) {
|
||||||
|
cctx->op += 4;
|
||||||
|
LDM_write32(cctx->op, 0xffffffff);
|
||||||
|
matchLengthRemaining -= 4*0xFF;
|
||||||
|
}
|
||||||
|
cctx->op += matchLengthRemaining / 255;
|
||||||
|
*(cctx->op)++ = (BYTE)(matchLengthRemaining % 255);
|
||||||
|
} else {
|
||||||
|
*token += (BYTE)(matchLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: srcSize and maxDstSize is unused
|
||||||
|
size_t LDM_compress(const void *src, size_t srcSize,
|
||||||
|
void *dst, size_t maxDstSize) {
|
||||||
|
LDM_CCtx cctx;
|
||||||
|
initializeCCtx(&cctx, src, srcSize, dst, maxDstSize);
|
||||||
|
|
||||||
|
/* Hash the first position and put it into the hash table. */
|
||||||
|
LDM_putHashOfCurrentPosition(&cctx);
|
||||||
|
|
||||||
|
// TODO: loop condition is not accurate.
|
||||||
|
while (1) {
|
||||||
|
const BYTE *match;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a match.
|
||||||
|
* If no more matches can be found (i.e. the length of the remaining input
|
||||||
|
* is less than the minimum match length), then stop searching for matches
|
||||||
|
* and encode the final literals.
|
||||||
|
*/
|
||||||
|
if (LDM_findBestMatch(&cctx, &match) != 0) {
|
||||||
|
goto _last_literals;
|
||||||
|
}
|
||||||
|
#ifdef COMPUTE_STATS
|
||||||
|
cctx.stats.numMatches++;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Catch up: look back to extend the match backwards from the found match.
|
||||||
|
*/
|
||||||
|
while (cctx.ip > cctx.anchor && match > cctx.ibase &&
|
||||||
|
cctx.ip[-1] == match[-1]) {
|
||||||
|
cctx.ip--;
|
||||||
|
match--;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write current block (literals, literal length, match offset, match
|
||||||
|
* length) and update pointers and hashes.
|
||||||
|
*/
|
||||||
|
{
|
||||||
|
unsigned const literalLength = (unsigned)(cctx.ip - cctx.anchor);
|
||||||
|
unsigned const offset = cctx.ip - match;
|
||||||
|
unsigned const matchLength = countMatchLength(
|
||||||
|
cctx.ip + MINMATCH, match + MINMATCH, cctx.ihashLimit);
|
||||||
|
|
||||||
|
#ifdef COMPUTE_STATS
|
||||||
|
cctx.stats.totalLiteralLength += literalLength;
|
||||||
|
cctx.stats.totalOffset += offset;
|
||||||
|
cctx.stats.totalMatchLength += matchLength + MINMATCH;
|
||||||
|
#endif
|
||||||
|
outputBlock(&cctx, literalLength, offset, matchLength);
|
||||||
|
|
||||||
|
// Move ip to end of block, inserting hashes at each position.
|
||||||
|
cctx.nextIp = cctx.ip + cctx.step;
|
||||||
|
while (cctx.ip < cctx.anchor + MINMATCH + matchLength + literalLength) {
|
||||||
|
if (cctx.ip > cctx.lastPosHashed) {
|
||||||
|
// TODO: Simplify.
|
||||||
|
LDM_updateLastHashFromNextHash(&cctx);
|
||||||
|
setNextHash(&cctx);
|
||||||
|
}
|
||||||
|
cctx.ip++;
|
||||||
|
cctx.nextIp++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set start of next block to current input pointer.
|
||||||
|
cctx.anchor = cctx.ip;
|
||||||
|
LDM_updateLastHashFromNextHash(&cctx);
|
||||||
|
}
|
||||||
|
_last_literals:
|
||||||
|
/* Encode the last literals (no more matches). */
|
||||||
|
{
|
||||||
|
size_t const lastRun = (size_t)(cctx.iend - cctx.anchor);
|
||||||
|
if (lastRun >= RUN_MASK) {
|
||||||
|
size_t accumulator = lastRun - RUN_MASK;
|
||||||
|
*(cctx.op)++ = RUN_MASK << ML_BITS;
|
||||||
|
for(; accumulator >= 255; accumulator -= 255) {
|
||||||
|
*(cctx.op)++ = 255;
|
||||||
|
}
|
||||||
|
*(cctx.op)++ = (BYTE)accumulator;
|
||||||
|
} else {
|
||||||
|
*(cctx.op)++ = (BYTE)(lastRun << ML_BITS);
|
||||||
|
}
|
||||||
|
memcpy(cctx.op, cctx.anchor, lastRun);
|
||||||
|
cctx.op += lastRun;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef COMPUTE_STATS
|
||||||
|
printCompressStats(&cctx);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return (cctx.op - (const BYTE *)cctx.obase);
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct LDM_DCtx {
|
||||||
|
size_t compressSize;
|
||||||
|
size_t maxDecompressSize;
|
||||||
|
|
||||||
|
const BYTE *ibase; /* Base of input */
|
||||||
|
const BYTE *ip; /* Current input position */
|
||||||
|
const BYTE *iend; /* End of source */
|
||||||
|
|
||||||
|
const BYTE *obase; /* Base of output */
|
||||||
|
BYTE *op; /* Current output position */
|
||||||
|
const BYTE *oend; /* End of output */
|
||||||
|
} LDM_DCtx;
|
||||||
|
|
||||||
|
static void LDM_initializeDCtx(LDM_DCtx *dctx,
|
||||||
|
const void *src, size_t compressSize,
|
||||||
|
void *dst, size_t maxDecompressSize) {
|
||||||
|
dctx->compressSize = compressSize;
|
||||||
|
dctx->maxDecompressSize = maxDecompressSize;
|
||||||
|
|
||||||
|
dctx->ibase = src;
|
||||||
|
dctx->ip = (const BYTE *)src;
|
||||||
|
dctx->iend = dctx->ip + dctx->compressSize;
|
||||||
|
dctx->op = dst;
|
||||||
|
dctx->oend = dctx->op + dctx->maxDecompressSize;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t LDM_decompress(const void *src, size_t compressSize,
|
||||||
|
void *dst, size_t maxDecompressSize) {
|
||||||
|
LDM_DCtx dctx;
|
||||||
|
LDM_initializeDCtx(&dctx, src, compressSize, dst, maxDecompressSize);
|
||||||
|
|
||||||
|
while (dctx.ip < dctx.iend) {
|
||||||
|
BYTE *cpy;
|
||||||
|
const BYTE *match;
|
||||||
|
size_t length, offset;
|
||||||
|
|
||||||
|
/* Get the literal length. */
|
||||||
|
unsigned const token = *(dctx.ip)++;
|
||||||
|
if ((length = (token >> ML_BITS)) == RUN_MASK) {
|
||||||
|
unsigned s;
|
||||||
|
do {
|
||||||
|
s = *(dctx.ip)++;
|
||||||
|
length += s;
|
||||||
|
} while (s == 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Copy the literals. */
|
||||||
|
cpy = dctx.op + length;
|
||||||
|
memcpy(dctx.op, dctx.ip, length);
|
||||||
|
dctx.ip += length;
|
||||||
|
dctx.op = cpy;
|
||||||
|
|
||||||
|
//TODO : dynamic offset size
|
||||||
|
offset = LDM_read32(dctx.ip);
|
||||||
|
dctx.ip += LDM_OFFSET_SIZE;
|
||||||
|
match = dctx.op - offset;
|
||||||
|
|
||||||
|
/* Get the match length. */
|
||||||
|
length = token & ML_MASK;
|
||||||
|
if (length == ML_MASK) {
|
||||||
|
unsigned s;
|
||||||
|
do {
|
||||||
|
s = *(dctx.ip)++;
|
||||||
|
length += s;
|
||||||
|
} while (s == 255);
|
||||||
|
}
|
||||||
|
length += MINMATCH;
|
||||||
|
|
||||||
|
/* Copy match. */
|
||||||
|
cpy = dctx.op + length;
|
||||||
|
|
||||||
|
// Inefficient for now.
|
||||||
|
while (match < cpy - offset && dctx.op < dctx.oend) {
|
||||||
|
*(dctx.op)++ = *match++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dctx.op - (BYTE *)dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
void LDM_test(const void *src, size_t srcSize,
|
||||||
|
void *dst, size_t maxDstSize) {
|
||||||
|
const BYTE *ip = (const BYTE *)src + 1125;
|
||||||
|
U32 sum = getChecksum((const char *)ip, LDM_HASH_LENGTH);
|
||||||
|
U32 sum2;
|
||||||
|
++ip;
|
||||||
|
for (; ip < (const BYTE *)src + 1125 + 100; ip++) {
|
||||||
|
sum2 = updateChecksum(sum, LDM_HASH_LENGTH,
|
||||||
|
ip[-1], ip[LDM_HASH_LENGTH - 1]);
|
||||||
|
sum = getChecksum((const char *)ip, LDM_HASH_LENGTH);
|
||||||
|
printf("TEST HASH: %zu %u %u\n", ip - (const BYTE *)src, sum, sum2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef LDM_H
|
||||||
|
#define LDM_H
|
||||||
|
|
||||||
|
#include <stddef.h> /* size_t */
|
||||||
|
|
||||||
|
#define LDM_COMPRESS_SIZE 4
|
||||||
|
#define LDM_DECOMPRESS_SIZE 4
|
||||||
|
#define LDM_HEADER_SIZE ((LDM_COMPRESS_SIZE)+(LDM_DECOMPRESS_SIZE))
|
||||||
|
|
||||||
|
size_t LDM_compress(const void *src, size_t srcSize,
|
||||||
|
void *dst, size_t maxDstSize);
|
||||||
|
|
||||||
|
size_t LDM_decompress(const void *src, size_t srcSize,
|
||||||
|
void *dst, size_t maxDstSize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the header from src and writes the compressed size and
|
||||||
|
* decompressed size into compressSize and decompressSize respectively.
|
||||||
|
*/
|
||||||
|
void LDM_readHeader(const void *src, size_t *compressSize,
|
||||||
|
size_t *decompressSize);
|
||||||
|
|
||||||
|
void LDM_test(const void *src, size_t srcSize,
|
||||||
|
void *dst, size_t maxDstSize);
|
||||||
|
|
||||||
|
#endif /* LDM_H */
|
||||||
@@ -0,0 +1,468 @@
|
|||||||
|
// TODO: file size must fit into a U32
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <zstd.h>
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include "ldm.h"
|
||||||
|
|
||||||
|
#define DEBUG
|
||||||
|
//#define TEST
|
||||||
|
|
||||||
|
/* Compress file given by fname and output to oname.
|
||||||
|
* Returns 0 if successful, error code otherwise.
|
||||||
|
*/
|
||||||
|
static int compress(const char *fname, const char *oname) {
|
||||||
|
int fdin, fdout;
|
||||||
|
struct stat statbuf;
|
||||||
|
char *src, *dst;
|
||||||
|
size_t maxCompressSize, compressSize;
|
||||||
|
|
||||||
|
/* Open the input file. */
|
||||||
|
if ((fdin = open(fname, O_RDONLY)) < 0) {
|
||||||
|
perror("Error in file opening");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Open the output file. */
|
||||||
|
if ((fdout = open(oname, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600)) < 0) {
|
||||||
|
perror("Can't create output file");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Find the size of the input file. */
|
||||||
|
if (fstat (fdin, &statbuf) < 0) {
|
||||||
|
perror("Fstat error");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
maxCompressSize = statbuf.st_size + LDM_HEADER_SIZE;
|
||||||
|
|
||||||
|
/* Go to the location corresponding to the last byte. */
|
||||||
|
/* TODO: fallocate? */
|
||||||
|
if (lseek(fdout, maxCompressSize - 1, SEEK_SET) == -1) {
|
||||||
|
perror("lseek error");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write a dummy byte at the last location. */
|
||||||
|
if (write(fdout, "", 1) != 1) {
|
||||||
|
perror("write error");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* mmap the input file. */
|
||||||
|
if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0))
|
||||||
|
== (caddr_t) - 1) {
|
||||||
|
perror("mmap error for input");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* mmap the output file */
|
||||||
|
if ((dst = mmap(0, maxCompressSize, PROT_READ | PROT_WRITE,
|
||||||
|
MAP_SHARED, fdout, 0)) == (caddr_t) - 1) {
|
||||||
|
perror("mmap error for output");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
#ifdef TEST
|
||||||
|
LDM_test(src, statbuf.st_size,
|
||||||
|
dst + LDM_HEADER_SIZE, statbuf.st_size);
|
||||||
|
#endif
|
||||||
|
*/
|
||||||
|
|
||||||
|
compressSize = LDM_HEADER_SIZE +
|
||||||
|
LDM_compress(src, statbuf.st_size,
|
||||||
|
dst + LDM_HEADER_SIZE, statbuf.st_size);
|
||||||
|
|
||||||
|
// Write compress and decompress size to header
|
||||||
|
// TODO: should depend on LDM_DECOMPRESS_SIZE write32
|
||||||
|
memcpy(dst, &compressSize, 4);
|
||||||
|
memcpy(dst + 4, &(statbuf.st_size), 4);
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
printf("Compressed size: %zu\n", compressSize);
|
||||||
|
printf("Decompressed size: %zu\n", (size_t)statbuf.st_size);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Truncate file to compressSize.
|
||||||
|
ftruncate(fdout, compressSize);
|
||||||
|
|
||||||
|
printf("%25s : %6u -> %7u - %s (%.1f%%)\n", fname,
|
||||||
|
(unsigned)statbuf.st_size, (unsigned)compressSize, oname,
|
||||||
|
(double)compressSize / (statbuf.st_size) * 100);
|
||||||
|
|
||||||
|
// Close files.
|
||||||
|
close(fdin);
|
||||||
|
close(fdout);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Decompress file compressed using LDM_compress.
|
||||||
|
* The input file should have the LDM_HEADER followed by payload.
|
||||||
|
* Returns 0 if succesful, and an error code otherwise.
|
||||||
|
*/
|
||||||
|
static int decompress(const char *fname, const char *oname) {
|
||||||
|
int fdin, fdout;
|
||||||
|
struct stat statbuf;
|
||||||
|
char *src, *dst;
|
||||||
|
size_t compressSize, decompressSize, outSize;
|
||||||
|
|
||||||
|
/* Open the input file. */
|
||||||
|
if ((fdin = open(fname, O_RDONLY)) < 0) {
|
||||||
|
perror("Error in file opening");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Open the output file. */
|
||||||
|
if ((fdout = open(oname, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600)) < 0) {
|
||||||
|
perror("Can't create output file");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Find the size of the input file. */
|
||||||
|
if (fstat (fdin, &statbuf) < 0) {
|
||||||
|
perror("Fstat error");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* mmap the input file. */
|
||||||
|
if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0))
|
||||||
|
== (caddr_t) - 1) {
|
||||||
|
perror("mmap error for input");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read the header. */
|
||||||
|
LDM_readHeader(src, &compressSize, &decompressSize);
|
||||||
|
|
||||||
|
/* Go to the location corresponding to the last byte. */
|
||||||
|
if (lseek(fdout, decompressSize - 1, SEEK_SET) == -1) {
|
||||||
|
perror("lseek error");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* write a dummy byte at the last location */
|
||||||
|
if (write(fdout, "", 1) != 1) {
|
||||||
|
perror("write error");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* mmap the output file */
|
||||||
|
if ((dst = mmap(0, decompressSize, PROT_READ | PROT_WRITE,
|
||||||
|
MAP_SHARED, fdout, 0)) == (caddr_t) - 1) {
|
||||||
|
perror("mmap error for output");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
outSize = LDM_decompress(
|
||||||
|
src + LDM_HEADER_SIZE, statbuf.st_size - LDM_HEADER_SIZE,
|
||||||
|
dst, decompressSize);
|
||||||
|
|
||||||
|
printf("Ret size out: %zu\n", outSize);
|
||||||
|
ftruncate(fdout, outSize);
|
||||||
|
|
||||||
|
close(fdin);
|
||||||
|
close(fdout);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Compare two files.
|
||||||
|
* Returns 0 iff they are the same.
|
||||||
|
*/
|
||||||
|
static int compare(FILE *fp0, FILE *fp1) {
|
||||||
|
int result = 0;
|
||||||
|
while (result == 0) {
|
||||||
|
char b0[1024];
|
||||||
|
char b1[1024];
|
||||||
|
const size_t r0 = fread(b0, 1, sizeof(b0), fp0);
|
||||||
|
const size_t r1 = fread(b1, 1, sizeof(b1), fp1);
|
||||||
|
|
||||||
|
result = (int)r0 - (int)r1;
|
||||||
|
|
||||||
|
if (0 == r0 || 0 == r1) break;
|
||||||
|
|
||||||
|
if (0 == result) result = memcmp(b0, b1, r0);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Verify the input file is the same as the decompressed file. */
|
||||||
|
static void verify(const char *inpFilename, const char *decFilename) {
|
||||||
|
FILE *inpFp = fopen(inpFilename, "rb");
|
||||||
|
FILE *decFp = fopen(decFilename, "rb");
|
||||||
|
|
||||||
|
printf("verify : %s <-> %s\n", inpFilename, decFilename);
|
||||||
|
{
|
||||||
|
const int cmp = compare(inpFp, decFp);
|
||||||
|
if(0 == cmp) {
|
||||||
|
printf("verify : OK\n");
|
||||||
|
} else {
|
||||||
|
printf("verify : NG\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(decFp);
|
||||||
|
fclose(inpFp);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, const char *argv[]) {
|
||||||
|
const char * const exeName = argv[0];
|
||||||
|
char inpFilename[256] = { 0 };
|
||||||
|
char ldmFilename[256] = { 0 };
|
||||||
|
char decFilename[256] = { 0 };
|
||||||
|
|
||||||
|
if (argc < 2) {
|
||||||
|
printf("Wrong arguments\n");
|
||||||
|
printf("Usage:\n");
|
||||||
|
printf("%s FILE\n", exeName);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(inpFilename, 256, "%s", argv[1]);
|
||||||
|
snprintf(ldmFilename, 256, "%s.ldm", argv[1]);
|
||||||
|
snprintf(decFilename, 256, "%s.ldm.dec", argv[1]);
|
||||||
|
|
||||||
|
printf("inp = [%s]\n", inpFilename);
|
||||||
|
printf("ldm = [%s]\n", ldmFilename);
|
||||||
|
printf("dec = [%s]\n", decFilename);
|
||||||
|
|
||||||
|
|
||||||
|
/* Compress */
|
||||||
|
{
|
||||||
|
struct timeval tv1, tv2;
|
||||||
|
gettimeofday(&tv1, NULL);
|
||||||
|
if (compress(inpFilename, ldmFilename)) {
|
||||||
|
printf("Compress error");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
gettimeofday(&tv2, NULL);
|
||||||
|
printf("Total compress time = %f seconds\n",
|
||||||
|
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
|
||||||
|
(double) (tv2.tv_sec - tv1.tv_sec));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Decompress */
|
||||||
|
{
|
||||||
|
struct timeval tv1, tv2;
|
||||||
|
gettimeofday(&tv1, NULL);
|
||||||
|
if (decompress(ldmFilename, decFilename)) {
|
||||||
|
printf("Decompress error");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
gettimeofday(&tv2, NULL);
|
||||||
|
printf("Total decompress time = %f seconds\n",
|
||||||
|
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
|
||||||
|
(double) (tv2.tv_sec - tv1.tv_sec));
|
||||||
|
}
|
||||||
|
/* verify */
|
||||||
|
verify(inpFilename, decFilename);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
static size_t compress_file(FILE *in, FILE *out, size_t *size_in,
|
||||||
|
size_t *size_out) {
|
||||||
|
char *src, *buf = NULL;
|
||||||
|
size_t r = 1;
|
||||||
|
size_t size, n, k, count_in = 0, count_out = 0, offset, frame_size = 0;
|
||||||
|
|
||||||
|
src = malloc(BUF_SIZE);
|
||||||
|
if (!src) {
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
size = BUF_SIZE + LDM_HEADER_SIZE;
|
||||||
|
buf = malloc(size);
|
||||||
|
if (!buf) {
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
k = fread(src, 1, BUF_SIZE, in);
|
||||||
|
if (k == 0)
|
||||||
|
break;
|
||||||
|
count_in += k;
|
||||||
|
|
||||||
|
n = LDM_compress(src, buf, k, BUF_SIZE);
|
||||||
|
|
||||||
|
// n = k;
|
||||||
|
// offset += n;
|
||||||
|
offset = k;
|
||||||
|
count_out += k;
|
||||||
|
|
||||||
|
// k = fwrite(src, 1, offset, out);
|
||||||
|
|
||||||
|
k = fwrite(buf, 1, offset, out);
|
||||||
|
if (k < offset) {
|
||||||
|
if (ferror(out))
|
||||||
|
printf("Write failed\n");
|
||||||
|
else
|
||||||
|
printf("Short write\n");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
*size_in = count_in;
|
||||||
|
*size_out = count_out;
|
||||||
|
r = 0;
|
||||||
|
cleanup:
|
||||||
|
free(src);
|
||||||
|
free(buf);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t decompress_file(FILE *in, FILE *out) {
|
||||||
|
void *src = malloc(BUF_SIZE);
|
||||||
|
void *dst = NULL;
|
||||||
|
size_t dst_capacity = BUF_SIZE;
|
||||||
|
size_t ret = 1;
|
||||||
|
size_t bytes_written = 0;
|
||||||
|
|
||||||
|
if (!src) {
|
||||||
|
perror("decompress_file(src)");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (ret != 0) {
|
||||||
|
/* Load more input */
|
||||||
|
size_t src_size = fread(src, 1, BUF_SIZE, in);
|
||||||
|
void *src_ptr = src;
|
||||||
|
void *src_end = src_ptr + src_size;
|
||||||
|
if (src_size == 0 || ferror(in)) {
|
||||||
|
printf("(TODO): Decompress: not enough input or error reading file\n");
|
||||||
|
//TODO
|
||||||
|
ret = 0;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Allocate destination buffer if it hasn't been allocated already */
|
||||||
|
if (!dst) {
|
||||||
|
dst = malloc(dst_capacity);
|
||||||
|
if (!dst) {
|
||||||
|
perror("decompress_file(dst)");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
|
||||||
|
/* Decompress:
|
||||||
|
* Continue while there is more input to read.
|
||||||
|
*/
|
||||||
|
while (src_ptr != src_end && ret != 0) {
|
||||||
|
// size_t dst_size = src_size;
|
||||||
|
size_t dst_size = LDM_decompress(src, dst, src_size, dst_capacity);
|
||||||
|
size_t written = fwrite(dst, 1, dst_size, out);
|
||||||
|
// printf("Writing %zu bytes\n", dst_size);
|
||||||
|
bytes_written += dst_size;
|
||||||
|
if (written != dst_size) {
|
||||||
|
printf("Decompress: Failed to write to file\n");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
src_ptr += src_size;
|
||||||
|
src_size = src_end - src_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Update input */
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Wrote %zu bytes\n", bytes_written);
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
free(src);
|
||||||
|
free(dst);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main2(int argc, char *argv[]) {
|
||||||
|
char inpFilename[256] = { 0 };
|
||||||
|
char ldmFilename[256] = { 0 };
|
||||||
|
char decFilename[256] = { 0 };
|
||||||
|
|
||||||
|
if (argc < 2) {
|
||||||
|
printf("Please specify input filename\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
snprintf(inpFilename, 256, "%s", argv[1]);
|
||||||
|
snprintf(ldmFilename, 256, "%s.ldm", argv[1]);
|
||||||
|
snprintf(decFilename, 256, "%s.ldm.dec", argv[1]);
|
||||||
|
|
||||||
|
printf("inp = [%s]\n", inpFilename);
|
||||||
|
printf("ldm = [%s]\n", ldmFilename);
|
||||||
|
printf("dec = [%s]\n", decFilename);
|
||||||
|
|
||||||
|
/* compress */
|
||||||
|
{
|
||||||
|
FILE *inpFp = fopen(inpFilename, "rb");
|
||||||
|
FILE *outFp = fopen(ldmFilename, "wb");
|
||||||
|
size_t sizeIn = 0;
|
||||||
|
size_t sizeOut = 0;
|
||||||
|
size_t ret;
|
||||||
|
printf("compress : %s -> %s\n", inpFilename, ldmFilename);
|
||||||
|
ret = compress_file(inpFp, outFp, &sizeIn, &sizeOut);
|
||||||
|
if (ret) {
|
||||||
|
printf("compress : failed with code %zu\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
printf("%s: %zu → %zu bytes, %.1f%%\n",
|
||||||
|
inpFilename, sizeIn, sizeOut,
|
||||||
|
(double)sizeOut / sizeIn * 100);
|
||||||
|
printf("compress : done\n");
|
||||||
|
|
||||||
|
fclose(outFp);
|
||||||
|
fclose(inpFp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* decompress */
|
||||||
|
{
|
||||||
|
FILE *inpFp = fopen(ldmFilename, "rb");
|
||||||
|
FILE *outFp = fopen(decFilename, "wb");
|
||||||
|
size_t ret;
|
||||||
|
|
||||||
|
printf("decompress : %s -> %s\n", ldmFilename, decFilename);
|
||||||
|
ret = decompress_file(inpFp, outFp);
|
||||||
|
if (ret) {
|
||||||
|
printf("decompress : failed with code %zu\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
printf("decompress : done\n");
|
||||||
|
|
||||||
|
fclose(outFp);
|
||||||
|
fclose(inpFp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* verify */
|
||||||
|
{
|
||||||
|
FILE *inpFp = fopen(inpFilename, "rb");
|
||||||
|
FILE *decFp = fopen(decFilename, "rb");
|
||||||
|
|
||||||
|
printf("verify : %s <-> %s\n", inpFilename, decFilename);
|
||||||
|
const int cmp = compare(inpFp, decFp);
|
||||||
|
if(0 == cmp) {
|
||||||
|
printf("verify : OK\n");
|
||||||
|
} else {
|
||||||
|
printf("verify : NG\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(decFp);
|
||||||
|
fclose(inpFp);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
typedef uint8_t BYTE;
|
||||||
|
typedef uint16_t U16;
|
||||||
|
typedef uint32_t U32;
|
||||||
|
typedef int32_t S32;
|
||||||
|
typedef uint64_t U64;
|
||||||
|
|
||||||
|
unsigned LDM_isLittleEndian(void) {
|
||||||
|
const union { U32 u; BYTE c[4]; } one = { 1 };
|
||||||
|
return one.c[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
U16 LDM_read16(const void *memPtr) {
|
||||||
|
U16 val;
|
||||||
|
memcpy(&val, memPtr, sizeof(val));
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
U16 LDM_readLE16(const void *memPtr) {
|
||||||
|
if (LDM_isLittleEndian()) {
|
||||||
|
return LDM_read16(memPtr);
|
||||||
|
} else {
|
||||||
|
const BYTE *p = (const BYTE *)memPtr;
|
||||||
|
return (U16)((U16)p[0] + (p[1] << 8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LDM_write16(void *memPtr, U16 value){
|
||||||
|
memcpy(memPtr, &value, sizeof(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void LDM_write32(void *memPtr, U32 value) {
|
||||||
|
memcpy(memPtr, &value, sizeof(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void LDM_writeLE16(void *memPtr, U16 value) {
|
||||||
|
if (LDM_isLittleEndian()) {
|
||||||
|
LDM_write16(memPtr, value);
|
||||||
|
} else {
|
||||||
|
BYTE* p = (BYTE *)memPtr;
|
||||||
|
p[0] = (BYTE) value;
|
||||||
|
p[1] = (BYTE)(value>>8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
U32 LDM_read32(const void *ptr) {
|
||||||
|
return *(const U32 *)ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
U64 LDM_read64(const void *ptr) {
|
||||||
|
return *(const U64 *)ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LDM_copy8(void *dst, const void *src) {
|
||||||
|
memcpy(dst, src, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
BYTE LDM_readByte(const void *memPtr) {
|
||||||
|
BYTE val;
|
||||||
|
memcpy(&val, memPtr, 1);
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef LDM_UTIL_H
|
||||||
|
#define LDM_UTIL_H
|
||||||
|
|
||||||
|
unsigned LDM_isLittleEndian(void);
|
||||||
|
|
||||||
|
uint16_t LDM_read16(const void *memPtr);
|
||||||
|
|
||||||
|
uint16_t LDM_readLE16(const void *memPtr);
|
||||||
|
|
||||||
|
void LDM_write16(void *memPtr, uint16_t value);
|
||||||
|
|
||||||
|
void LDM_write32(void *memPtr, uint32_t value);
|
||||||
|
|
||||||
|
void LDM_writeLE16(void *memPtr, uint16_t value);
|
||||||
|
|
||||||
|
uint32_t LDM_read32(const void *ptr);
|
||||||
|
|
||||||
|
uint64_t LDM_read64(const void *ptr);
|
||||||
|
|
||||||
|
void LDM_copy8(void *dst, const void *src);
|
||||||
|
|
||||||
|
uint8_t LDM_readByte(const void *ptr);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* LDM_UTIL_H */
|
||||||
Reference in New Issue
Block a user