Skip to content

Commit

Permalink
Fixed exepack2 decompression.
Browse files Browse the repository at this point in the history
Now 4os2.exe runs without a weird crash during startup (but still fails on a
missing API call a little later).
  • Loading branch information
icculus committed Jun 30, 2018
1 parent ec49615 commit afc9e08
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions lx_loader.c
Expand Up @@ -239,14 +239,27 @@ static char *lxMakeUnixPath(const char *os2path, uint32 *err)
} // lxMakeUnixPath


// So exepack2 sometimes copies between pieces of the destination buffer,
// but you can't memmove() since you actually want it to copy the data as
// it changes in case of overlap. But to prevent debugging tools from
// complaining about overlapping memcpy()'s, we just do a simple for-loop.
static void linearmove(uint8 *dst, const uint8 *src, uint8 len)
{
while (len--) {
*dst = *src;
dst++;
src++;
}
} // linearmove

/* this algorithm is from lxlite 138u. */
static int decompressExePack2(uint8 *dst, const uint32 dstlen, const uint8 *src, const uint32 srclen)
{
uint32 sOf = 0;
uint32 dOf = 0;
uint32 bOf = 0;
uint32 b1 = 0;
uint32 b2 = 0;
sint32 sOf = 0;
sint32 dOf = 0;
sint32 bOf = 0;
uint8 b1 = 0;
uint8 b2 = 0;

#define SRCAVAIL(n) ((sOf + (n)) <= srclen)
#define DSTAVAIL(n) ((dOf + (n)) <= dstlen)
Expand Down Expand Up @@ -289,11 +302,11 @@ static int decompressExePack2(uint8 *dst, const uint32 dstlen, const uint8 *src,
b2 = ((b1 >> 4) & 7) + 3;
b1 = ((b1 >> 2) & 3);
sOf += 2;
if (SRCAVAIL(b1) && DSTAVAIL(b1 + b2) && (dOf + b1 - bOf >= 0)) {
if (SRCAVAIL(b1) && DSTAVAIL(b1 + b2) && ((dOf + b1 - bOf) >= 0)) {
memcpy(dst + dOf, src + sOf, b1);
dOf += b1;
sOf += b1;
memmove(dst + dOf, dst + (dOf - bOf), b2);
linearmove(dst + dOf, dst + (dOf - bOf), b2);
dOf += b2;
} else {
return 0;
Expand All @@ -306,7 +319,7 @@ static int decompressExePack2(uint8 *dst, const uint32 dstlen, const uint8 *src,
bOf = (*((const uint16 *) (src + sOf))) >> 4;
b1 = ((b1 >> 2) & 3) + 3;
if (DSTAVAIL(b1) && (dOf - bOf >= 0)) {
memmove(dst + dOf, dst + (dOf - bOf), b1);
linearmove(dst + dOf, dst + (dOf - bOf), b1);
dOf += b1;
sOf += 2;
} else {
Expand All @@ -321,12 +334,12 @@ static int decompressExePack2(uint8 *dst, const uint32 dstlen, const uint8 *src,
b1 = (src[sOf] >> 2) & 0x0F;
bOf = (*((const uint16 *) (src + (sOf + 1)))) >> 4;
sOf += 3;
if (SRCAVAIL(b1) && DSTAVAIL(b1 + b2) && (dOf + b1 - bOf >= 0))
if (SRCAVAIL(b1) && DSTAVAIL(b1 + b2) && ((dOf + b1 - bOf) >= 0))
{
memcpy(dst + dOf, src + sOf, b1);
dOf += b1;
sOf += b1;
memmove(dst + dOf, dst + (dOf - bOf), b2);
linearmove(dst + dOf, dst + (dOf - bOf), b2);
dOf += b2;
} else {
return 0;
Expand Down

0 comments on commit afc9e08

Please sign in to comment.