Skip to content

Commit

Permalink
Endian fixes for the LCW compressed straw/pipe classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
isojalka authored and OmniBlade committed Feb 24, 2023
1 parent 8eb5185 commit 40a0b31
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
20 changes: 11 additions & 9 deletions common/lcwpipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ int LCWPipe::Put(void const* source, int slen)
*/
if (Counter == sizeof(BlockHeader)) {
memmove(&BlockHeader, Buffer, sizeof(BlockHeader));
BlockHeader.UncompCount = le16toh(BlockHeader.UncompCount);
BlockHeader.CompCount = le16toh(BlockHeader.CompCount);
Counter = 0;
}
}
Expand Down Expand Up @@ -190,10 +192,10 @@ int LCWPipe::Put(void const* source, int slen)
Counter += tocopy;

if (Counter == BlockSize) {
int len = LCW_Comp(Buffer, Buffer2, BlockSize);
unsigned short len = LCW_Comp(Buffer, Buffer2, BlockSize);

BlockHeader.CompCount = (unsigned short)len;
BlockHeader.UncompCount = (unsigned short)BlockSize;
BlockHeader.CompCount = htole16(len);
BlockHeader.UncompCount = htole16(BlockSize);
total += Pipe::Put(&BlockHeader, sizeof(BlockHeader));
total += Pipe::Put(Buffer2, len);
Counter = 0;
Expand All @@ -205,13 +207,13 @@ int LCWPipe::Put(void const* source, int slen)
** source data left for a whole data block.
*/
while (slen >= BlockSize) {
int len = LCW_Comp(source, Buffer2, BlockSize);
unsigned short len = LCW_Comp(source, Buffer2, BlockSize);

source = ((char*)source) + BlockSize;
slen -= BlockSize;

BlockHeader.CompCount = (unsigned short)len;
BlockHeader.UncompCount = (unsigned short)BlockSize;
BlockHeader.CompCount = htole16(len);
BlockHeader.UncompCount = htole16(BlockSize);
total += Pipe::Put(&BlockHeader, sizeof(BlockHeader));
total += Pipe::Put(Buffer2, len);
}
Expand Down Expand Up @@ -289,10 +291,10 @@ int LCWPipe::Flush(void)
** A partial block in the compression process is a normal occurrence. Just
** compress the partial block and output normally.
*/
int len = LCW_Comp(Buffer, Buffer2, Counter);
unsigned short len = LCW_Comp(Buffer, Buffer2, Counter);

BlockHeader.CompCount = (unsigned short)len;
BlockHeader.UncompCount = (unsigned short)Counter;
BlockHeader.CompCount = htole16(len);
BlockHeader.UncompCount = htole16(Counter);
total += Pipe::Put(&BlockHeader, sizeof(BlockHeader));
total += Pipe::Put(Buffer2, len);
Counter = 0;
Expand Down
9 changes: 7 additions & 2 deletions common/lcwstraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ int LCWStraw::Get(void* destbuf, int slen)
if (incount != sizeof(BlockHeader))
break;

BlockHeader.CompCount = le16toh(BlockHeader.CompCount);
BlockHeader.UncompCount = le16toh(BlockHeader.UncompCount);

void* ptr = &Buffer[(BlockSize + SafetyMargin) - BlockHeader.CompCount];
incount = Straw::Get(ptr, BlockHeader.CompCount);
if (incount != BlockHeader.CompCount)
Expand All @@ -167,10 +170,12 @@ int LCWStraw::Get(void* destbuf, int slen)
BlockHeader.UncompCount = (unsigned short)Straw::Get(Buffer, BlockSize);
if (BlockHeader.UncompCount == 0)
break;
BlockHeader.CompCount =
unsigned short compcount =
(unsigned short)LCW_Comp(Buffer, &Buffer2[sizeof(BlockHeader)], BlockHeader.UncompCount);
BlockHeader.UncompCount = htole16(BlockHeader.UncompCount);
BlockHeader.CompCount = htole16(compcount);
memmove(Buffer2, &BlockHeader, sizeof(BlockHeader));
Counter = BlockHeader.CompCount + sizeof(BlockHeader);
Counter = compcount + sizeof(BlockHeader);
}
}

Expand Down

0 comments on commit 40a0b31

Please sign in to comment.