Add LDM_DCtx

This commit is contained in:
Stella Lau
2017-07-10 07:38:09 -07:00
parent 5432214ee3
commit ae9cf235d6
2 changed files with 47 additions and 31 deletions
+45 -29
View File
@@ -351,33 +351,49 @@ _last_literals:
} }
typedef struct LDM_DCtx { typedef struct LDM_DCtx {
const BYTE * const ibase; /* Pointer to base of input */ const BYTE *ibase; /* Pointer to base of input */
const BYTE *ip; /* Pointer to current input position */ const BYTE *ip; /* Pointer to current input position */
const BYTE *iend; /* End of source */ const BYTE *iend; /* End of source */
BYTE *op; /* Pointer to output */
const BYTE * const oend; /* Pointer to end of output */
const BYTE *obase; /* Pointer to base of output */
BYTE *op; /* Pointer to output */
const BYTE *oend; /* Pointer to end of output */
size_t compressSize;
size_t maxDecompressSize;
} LDM_DCtx; } LDM_DCtx;
size_t LDM_decompress(const void *src, size_t compressed_size, static void LDM_initializeDCtx(LDM_DCtx *dctx,
void *dst, size_t max_decompressed_size) { const void *src, size_t compressSize,
const BYTE *ip = (const BYTE *)src; void *dst, size_t maxDecompressSize) {
const BYTE * const iend = ip + compressed_size; dctx->ibase = src;
BYTE *op = (BYTE *)dst; dctx->ip = (const BYTE *)src;
BYTE * const oend = op + max_decompressed_size; dctx->iend = dctx->ip + compressSize;
dctx->op = dst;
dctx->oend = dctx->op + maxDecompressSize;
dctx->compressSize = compressSize;
dctx->maxDecompressSize = 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);
BYTE *cpy; BYTE *cpy;
while (ip < iend) { while (dctx.ip < dctx.iend) {
size_t length; size_t length;
const BYTE *match; const BYTE *match;
size_t offset; size_t offset;
/* get literal length */ /* get literal length */
unsigned const token = *ip++; unsigned const token = *(dctx.ip)++;
if ((length=(token >> ML_BITS)) == RUN_MASK) { if ((length=(token >> ML_BITS)) == RUN_MASK) {
unsigned s; unsigned s;
do { do {
s = *ip++; s = *(dctx.ip)++;
length += s; length += s;
} while (s == 255); } while (s == 255);
} }
@@ -386,27 +402,27 @@ size_t LDM_decompress(const void *src, size_t compressed_size,
#endif #endif
/* copy literals */ /* copy literals */
cpy = op + length; cpy = dctx.op + length;
#ifdef LDM_DEBUG #ifdef LDM_DEBUG
printf("Literals "); printf("Literals ");
fwrite(ip, length, 1, stdout); fwrite(dctx.ip, length, 1, stdout);
printf("\n"); printf("\n");
#endif #endif
memcpy(op, ip, length); memcpy(dctx.op, dctx.ip, length);
ip += length; dctx.ip += length;
op = cpy; dctx.op = cpy;
/* get offset */ /* get offset */
/* /*
offset = LDM_readLE16(ip); offset = LDM_readLE16(dctx.ip);
ip += 2; dctx.ip += 2;
*/ */
offset = LDM_read32(ip); offset = LDM_read32(dctx.ip);
ip += 4; dctx.ip += 4;
#ifdef LDM_DEBUG #ifdef LDM_DEBUG
printf("Offset: %zu\n", offset); printf("Offset: %zu\n", offset);
#endif #endif
match = op - offset; match = dctx.op - offset;
// LDM_write32(op, (U32)offset); // LDM_write32(op, (U32)offset);
/* get matchlength */ /* get matchlength */
@@ -414,7 +430,7 @@ size_t LDM_decompress(const void *src, size_t compressed_size,
if (length == ML_MASK) { if (length == ML_MASK) {
unsigned s; unsigned s;
do { do {
s = *ip++; s = *(dctx.ip)++;
length += s; length += s;
} while (s == 255); } while (s == 255);
} }
@@ -423,14 +439,14 @@ size_t LDM_decompress(const void *src, size_t compressed_size,
printf("Match length: %zu\n", length); printf("Match length: %zu\n", length);
#endif #endif
/* copy match */ /* copy match */
cpy = op + length; cpy = dctx.op + length;
// Inefficient for now // Inefficient for now
while (match < cpy - offset && op < oend) { while (match < cpy - offset && dctx.op < dctx.oend) {
*op++ = *match++; *(dctx.op)++ = *match++;
} }
} }
return op - (BYTE *)dst; return dctx.op - (BYTE *)dst;
} }
+2 -2
View File
@@ -88,7 +88,7 @@ static int compress(const char *fname, const char *oname) {
#ifdef DEBUG #ifdef DEBUG
printf("Compressed size: %zu\n", compressSize); printf("Compressed size: %zu\n", compressSize);
printf("Decompressed size: %zu\n", statbuf.st_size); printf("Decompressed size: %zu\n", (size_t)statbuf.st_size);
#endif #endif
#endif #endif
@@ -145,7 +145,7 @@ static int decompress(const char *fname, const char *oname) {
#ifdef DEBUG #ifdef DEBUG
printf("Size, compressSize, decompressSize: %zu %zu %zu\n", printf("Size, compressSize, decompressSize: %zu %zu %zu\n",
statbuf.st_size, compressSize, decompressSize); (size_t)statbuf.st_size, compressSize, decompressSize);
#endif #endif
/* Go to the location corresponding to the last byte. */ /* Go to the location corresponding to the last byte. */