From d02ab6699d1d546a8060139eb73ae53500e5f6e2 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Thu, 13 Jun 2013 17:11:02 +0000 Subject: [PATCH] cache1d.c: prettify compression functions, add one currently failing assertion. See http://forums.duke4.net/topic/6526-eduke32-crashing-when-loading-a-dukeplus-map-save git-svn-id: https://svn.eduke32.com/eduke32@3879 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/build/src/cache1d.c | 44 +++++++++++++++++++---------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/polymer/eduke32/build/src/cache1d.c b/polymer/eduke32/build/src/cache1d.c index 86088c3c8..92f222d7a 100644 --- a/polymer/eduke32/build/src/cache1d.c +++ b/polymer/eduke32/build/src/cache1d.c @@ -1416,15 +1416,19 @@ C1D_STATIC void (*c1d_writefunc)(intptr_t, const void *, int32_t) = dfwrite_func ////////// COMPRESSED READ ////////// -static uint32_t decompress_part(int16_t *lengptr, intptr_t f, uint32_t *kgoalptr) +static uint32_t decompress_part(intptr_t f, uint32_t *kgoalptr) { - int32_t leng; - if (c1d_readfunc(f, lengptr, 2) != 2) + int16_t leng; + + // Read compressed length first. + if (c1d_readfunc(f, &leng, 2) != 2) return 1; - leng = B_LITTLE16(*lengptr); + leng = B_LITTLE16(leng); + if (c1d_readfunc(f,lzwcompbuf, leng) != leng) return 1; - *kgoalptr = lzwuncompress(lzwcompbuf, *lengptr, lzwrawbuf); + + *kgoalptr = lzwuncompress(lzwcompbuf, leng, lzwrawbuf); return 0; } @@ -1432,7 +1436,6 @@ static uint32_t decompress_part(int16_t *lengptr, intptr_t f, uint32_t *kgoalptr C1D_STATIC int32_t c1d_read_compressed(void *buffer, bsize_t dasizeof, bsize_t count, intptr_t f) { uint32_t i, j, k, kgoal; - int16_t leng; char *ptr; if (dasizeof > LZWSIZE) @@ -1443,7 +1446,7 @@ C1D_STATIC int32_t c1d_read_compressed(void *buffer, bsize_t dasizeof, bsize_t c ptr = (char *)buffer; - k = decompress_part(&leng, f, &kgoal); + k = decompress_part(f, &kgoal); if (k) return -1; Bmemcpy(ptr, lzwrawbuf, (int32_t)dasizeof); @@ -1453,7 +1456,7 @@ C1D_STATIC int32_t c1d_read_compressed(void *buffer, bsize_t dasizeof, bsize_t c { if (k >= kgoal) { - k = decompress_part(&leng, f, &kgoal); + k = decompress_part(f, &kgoal); if (k) return -1; } @@ -1477,11 +1480,12 @@ int32_t kdfread(void *buffer, bsize_t dasizeof, bsize_t count, int32_t fil) static uint32_t compress_part(uint32_t k, intptr_t f) { - int16_t leng, swleng; - leng = (int16_t)lzwcompress(lzwrawbuf,k,lzwcompbuf); - swleng = B_LITTLE16(leng); + const int16_t leng = (int16_t)lzwcompress(lzwrawbuf, k, lzwcompbuf); + const int16_t swleng = B_LITTLE16(leng); + c1d_writefunc(f, &swleng, 2); c1d_writefunc(f, lzwcompbuf, leng); + return 0; } @@ -1607,23 +1611,25 @@ static int32_t lzwcompress(const char *lzwinbuf, int32_t uncompleng, char *lzwou shortptr = (int16_t *)lzwoutbuf; shortptr[0] = B_LITTLE16((int16_t)uncompleng); + if (((bitcnt+7)>>3) < uncompleng) { shortptr[1] = B_LITTLE16((int16_t)addrcnt); - return((bitcnt+7)>>3); + return (bitcnt+7)>>3; } + // Failed compressing, mark this in the stream. shortptr[1] = 0; for (i=0; i>3]; - dat = ((B_LITTLE32(intptr[0])>>(bitcnt&7)) & (oneupnumbits-1)); + int32_t dat = ((B_LITTLE32(intptr[0])>>(bitcnt&7)) & (oneupnumbits-1)); + int32_t leng; + bitcnt += numbits; if ((dat&((oneupnumbits>>1)-1)) > ((currstr-1)&((oneupnumbits>>1)-1))) { dat &= ((oneupnumbits>>1)-1); bitcnt--; }