From 3bbfa1249e8ef2f2b966f6b71a75d8cfbb53089a Mon Sep 17 00:00:00 2001 From: Stella Lau Date: Thu, 6 Jul 2017 16:47:08 -0700 Subject: [PATCH] Update compressor and decompressor --- contrib/long_distance_matching/ldm.c | 40 ++++++++++++++++++----- contrib/long_distance_matching/main-ldm.c | 4 +-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/contrib/long_distance_matching/ldm.c b/contrib/long_distance_matching/ldm.c index c8051ea4..908ac2ac 100644 --- a/contrib/long_distance_matching/ldm.c +++ b/contrib/long_distance_matching/ldm.c @@ -20,6 +20,8 @@ #define RUN_BITS (8-ML_BITS) #define RUN_MASK ((1U<= 255; len -= 255) { - *op++ = (BYTE)len; + *op++ = 255; } + *op++ = (BYTE)len; } else { *token = (BYTE)(litLength << ML_BITS); } - +#ifdef LDM_DEBUG printf("Literals "); fwrite(anchor, litLength, 1, stdout); printf("\n"); - +#endif LDM_wild_copy(op, anchor, op + litLength); op += litLength; } @@ -232,10 +238,11 @@ _next_match: unsigned matchCode; matchCode = LDM_count(ip + MINMATCH, match + MINMATCH, matchlimit); - +#ifdef LDM_DEBUG printf("Match length %zu\n", matchCode + MINMATCH); fwrite(ip, MINMATCH + matchCode, 1, stdout); printf("\n"); +#endif ip += MINMATCH + matchCode; if (matchCode >= ML_MASK) { *token += ML_MASK; @@ -251,7 +258,9 @@ _next_match: } else { *token += (BYTE)(matchCode); } +#ifdef LDM_DEBUG printf("\n"); +#endif } anchor = ip; @@ -308,24 +317,33 @@ size_t LDM_decompress(void const *source, void *dest, size_t compressed_size, length += s; } while (s == 255); } +#ifdef LDM_DEBUG printf("Literal length: %zu\n", length); +#endif /* copy literals */ cpy = op + length; +#ifdef LDM_DEBUG + printf("Literals "); + fwrite(ip, length, 1, stdout); + printf("\n"); +#endif LDM_wild_copy(op, ip, cpy); ip += length; op = cpy; /* get offset */ offset = LDM_readLE16(ip); + +#ifdef LDM_DEBUG printf("Offset: %zu\n", offset); +#endif ip += 2; match = op - offset; // LDM_write32(op, (U32)offset); /* get matchlength */ length = token & ML_MASK; - printf("Match length: %zu\n", length); if (length == ML_MASK) { unsigned s; do { @@ -334,16 +352,20 @@ size_t LDM_decompress(void const *source, void *dest, size_t compressed_size, } while (s == 255); } length += MINMATCH; - +#ifdef LDM_DEBUG + printf("Match length: %zu\n", length); +#endif /* copy match */ cpy = op + length; - - + // Inefficient for now + while (match < cpy - offset) { + *op++ = *match++; + } } // memcpy(dest, source, compressed_size); - return compressed_size; + return op - (BYTE *)dest; } diff --git a/contrib/long_distance_matching/main-ldm.c b/contrib/long_distance_matching/main-ldm.c index 8b97ce92..7f1abdab 100644 --- a/contrib/long_distance_matching/main-ldm.c +++ b/contrib/long_distance_matching/main-ldm.c @@ -229,7 +229,7 @@ static size_t decompress(const char *fname, const char *oname) { } /* go to the location corresponding to the last byte */ - if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1) { + if (lseek(fdout, 2*statbuf.st_size - 1, SEEK_SET) == -1) { perror("lseek error"); return 1; } @@ -264,7 +264,7 @@ static size_t decompress(const char *fname, const char *oname) { size_t size_out = LDM_decompress(src, dst, statbuf.st_size, statbuf.st_size); #endif - ftruncate(fdout, size_out); + //ftruncate(fdout, size_out); close(fdin); close(fdout);