cache1d.c: in (de)compr. funcs, add an bound-checking assert that fails w/ AMC TC.

Also, formatting.

git-svn-id: https://svn.eduke32.com/eduke32@4837 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2014-12-26 17:29:54 +00:00
parent 584ed7982a
commit b7805868f7

View file

@ -1463,8 +1463,7 @@ static uint32_t decompress_part(intptr_t f, uint32_t *kgoalptr)
// Read from 'f' into 'buffer'. // Read from 'f' into 'buffer'.
C1D_STATIC int32_t c1d_read_compressed(void *buffer, bsize_t dasizeof, bsize_t count, intptr_t f) C1D_STATIC int32_t c1d_read_compressed(void *buffer, bsize_t dasizeof, bsize_t count, intptr_t f)
{ {
uint32_t i, j, k, kgoal; char *ptr = (char *)buffer;
char *ptr;
if (dasizeof > LZWSIZE) if (dasizeof > LZWSIZE)
{ {
@ -1475,21 +1474,23 @@ C1D_STATIC int32_t c1d_read_compressed(void *buffer, bsize_t dasizeof, bsize_t c
} }
else else
{ {
i = count; uint32_t i = count;
count = dasizeof; count = dasizeof;
dasizeof = i; dasizeof = i;
} }
} }
ptr = (char *)buffer; uint32_t kgoal;
k = decompress_part(f, &kgoal); if (decompress_part(f, &kgoal))
if (k) return -1; return -1;
Bassert(dasizeof < sizeof(lzwrawbuf));
Bmemcpy(ptr, lzwrawbuf, (int32_t)dasizeof); Bmemcpy(ptr, lzwrawbuf, (int32_t)dasizeof);
k += (int32_t)dasizeof;
for (i=1; i<count; i++) uint32_t k = (int32_t)dasizeof;
for (uint32_t i=1; i<count; i++)
{ {
if (k >= kgoal) if (k >= kgoal)
{ {
@ -1497,14 +1498,16 @@ C1D_STATIC int32_t c1d_read_compressed(void *buffer, bsize_t dasizeof, bsize_t c
if (k) return -1; if (k) return -1;
} }
j = 0; uint32_t j = 0;
if (dasizeof >= 4) if (dasizeof >= 4)
{ {
for (; j<dasizeof-4; j+=4) for (; j<dasizeof-4; j+=4)
{ {
ptr[j+dasizeof] = ((ptr[j]+lzwrawbuf[j+k])&255); ptr[j+1+dasizeof] = ((ptr[j+1]+lzwrawbuf[j+1+k])&255); ptr[j+dasizeof] = ((ptr[j]+lzwrawbuf[j+k])&255);
ptr[j+2+dasizeof] = ((ptr[j+2]+lzwrawbuf[j+2+k])&255); ptr[j+3+dasizeof] = ((ptr[j+3]+lzwrawbuf[j+3+k])&255); ptr[j+1+dasizeof] = ((ptr[j+1]+lzwrawbuf[j+1+k])&255);
ptr[j+2+dasizeof] = ((ptr[j+2]+lzwrawbuf[j+2+k])&255);
ptr[j+3+dasizeof] = ((ptr[j+3]+lzwrawbuf[j+3+k])&255);
} }
} }
@ -1540,7 +1543,6 @@ static uint32_t compress_part(uint32_t k, intptr_t f)
// Write from 'buffer' to 'f'. // Write from 'buffer' to 'f'.
C1D_STATIC void c1d_write_compressed(const void *buffer, bsize_t dasizeof, bsize_t count, intptr_t f) C1D_STATIC void c1d_write_compressed(const void *buffer, bsize_t dasizeof, bsize_t count, intptr_t f)
{ {
uint32_t i, j, k;
const char *ptr = (char*)buffer; const char *ptr = (char*)buffer;
if (dasizeof > LZWSIZE && count > LZWSIZE) if (dasizeof > LZWSIZE && count > LZWSIZE)
@ -1552,28 +1554,31 @@ C1D_STATIC void c1d_write_compressed(const void *buffer, bsize_t dasizeof, bsize
} }
else else
{ {
i = count; uint32_t i = count;
count = dasizeof; count = dasizeof;
dasizeof = i; dasizeof = i;
} }
} }
Bassert(dasizeof < sizeof(lzwrawbuf));
Bmemcpy(lzwrawbuf, ptr, (int32_t)dasizeof); Bmemcpy(lzwrawbuf, ptr, (int32_t)dasizeof);
k = dasizeof; uint32_t k = dasizeof;
if (k > LZWSIZE-dasizeof) if (k > LZWSIZE-dasizeof)
k = compress_part(k, f); k = compress_part(k, f);
for (i=1; i<count; i++) for (uint32_t i=1; i<count; i++)
{ {
j = 0; uint32_t j = 0;
if (dasizeof >= 4) if (dasizeof >= 4)
{ {
for (; j<dasizeof-4; j+=4) for (; j<dasizeof-4; j+=4)
{ {
lzwrawbuf[j+k] = ((ptr[j+dasizeof]-ptr[j])&255); lzwrawbuf[j+1+k] = ((ptr[j+1+dasizeof]-ptr[j+1])&255); lzwrawbuf[j+k] = ((ptr[j+dasizeof]-ptr[j])&255);
lzwrawbuf[j+2+k] = ((ptr[j+2+dasizeof]-ptr[j+2])&255); lzwrawbuf[j+3+k] = ((ptr[j+3+dasizeof]-ptr[j+3])&255); lzwrawbuf[j+1+k] = ((ptr[j+1+dasizeof]-ptr[j+1])&255);
lzwrawbuf[j+2+k] = ((ptr[j+2+dasizeof]-ptr[j+2])&255);
lzwrawbuf[j+3+k] = ((ptr[j+3+dasizeof]-ptr[j+3])&255);
} }
} }