[ldm] Add CHAR_OFFSET in hash function and extend header size
This commit is contained in:
parent
8de82b6eb0
commit
92bed4a7e0
@ -28,6 +28,7 @@
|
||||
#define RUN_MASK ((1U<<RUN_BITS)-1)
|
||||
|
||||
#define COMPUTE_STATS
|
||||
#define CHECKSUM_CHAR_OFFSET 2
|
||||
//#define RUN_CHECKS
|
||||
//#define LDM_DEBUG
|
||||
|
||||
@ -191,11 +192,14 @@ static U32 getChecksum(const char *data, U32 len) {
|
||||
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];
|
||||
(2 * buf[i + 2]) + (buf[i + 3]) +
|
||||
(10 * CHECKSUM_CHAR_OFFSET);
|
||||
s1 += buf[i] + buf[i + 1] + buf[i + 2] + buf[i + 3] +
|
||||
+ (4 * CHECKSUM_CHAR_OFFSET);
|
||||
|
||||
}
|
||||
for(; i < len; i++) {
|
||||
s1 += buf[i];
|
||||
s1 += buf[i] + CHECKSUM_CHAR_OFFSET;
|
||||
s2 += s1;
|
||||
}
|
||||
return (s1 & 0xffff) + (s2 << 16);
|
||||
@ -213,7 +217,7 @@ static U32 getChecksum(const char *data, U32 len) {
|
||||
static U32 updateChecksum(U32 sum, U32 len,
|
||||
schar toRemove, schar toAdd) {
|
||||
U32 s1 = (sum & 0xffff) - toRemove + toAdd;
|
||||
U32 s2 = (sum >> 16) - (toRemove * len) + s1;
|
||||
U32 s2 = (sum >> 16) - ((toRemove + CHECKSUM_CHAR_OFFSET) * len) + s1;
|
||||
|
||||
return (s1 & 0xffff) + (s2 << 16);
|
||||
}
|
||||
@ -344,9 +348,9 @@ static unsigned countMatchLength(const BYTE *pIn, const BYTE *pMatch,
|
||||
return (unsigned)(pIn - pStart);
|
||||
}
|
||||
|
||||
void LDM_readHeader(const void *src, size_t *compressSize,
|
||||
size_t *decompressSize) {
|
||||
const U32 *ip = (const U32 *)src;
|
||||
void LDM_readHeader(const void *src, U64 *compressSize,
|
||||
U64 *decompressSize) {
|
||||
const U64 *ip = (const U64 *)src;
|
||||
*compressSize = *ip++;
|
||||
*decompressSize = *ip;
|
||||
}
|
||||
|
@ -3,8 +3,8 @@
|
||||
|
||||
#include <stddef.h> /* size_t */
|
||||
|
||||
#define LDM_COMPRESS_SIZE 4
|
||||
#define LDM_DECOMPRESS_SIZE 4
|
||||
#define LDM_COMPRESS_SIZE 8
|
||||
#define LDM_DECOMPRESS_SIZE 8
|
||||
#define LDM_HEADER_SIZE ((LDM_COMPRESS_SIZE)+(LDM_DECOMPRESS_SIZE))
|
||||
|
||||
/**
|
||||
|
@ -85,8 +85,8 @@ static int compress(const char *fname, const char *oname) {
|
||||
|
||||
// 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);
|
||||
memcpy(dst, &compressSize, 8);
|
||||
memcpy(dst + 8, &(statbuf.st_size), 8);
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("Compressed size: %zu\n", compressSize);
|
||||
@ -267,202 +267,3 @@ int main(int argc, const char *argv[]) {
|
||||
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
|
||||
|
||||
|
@ -53,6 +53,11 @@ U32 LDM_read32(const void *ptr) {
|
||||
return *(const U32 *)ptr;
|
||||
}
|
||||
|
||||
//TODO: endianness?
|
||||
void LDM_write64(void *memPtr, U64 value) {
|
||||
memcpy(memPtr, &value, sizeof(value));
|
||||
}
|
||||
|
||||
U64 LDM_read64(const void *ptr) {
|
||||
return *(const U64 *)ptr;
|
||||
}
|
||||
|
@ -21,5 +21,7 @@ void LDM_copy8(void *dst, const void *src);
|
||||
|
||||
uint8_t LDM_readByte(const void *ptr);
|
||||
|
||||
void LDM_write64(void *memPtr, uint64_t value);
|
||||
|
||||
|
||||
#endif /* LDM_UTIL_H */
|
||||
|
Loading…
Reference in New Issue
Block a user