Skip to content

Commit 7007ec6

Browse files
author
Kiddinglife
committed
Improve WriteCompressed() and ReadCompressed()
1 parent beb10e2 commit 7007ec6

2 files changed

Lines changed: 92 additions & 72 deletions

File tree

Source/BitStream.cpp

Lines changed: 91 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,10 @@ void BitStream::SetData( unsigned char *inByteArray )
489489
void BitStream::WriteCompressed( const unsigned char* inByteArray,
490490
const unsigned int size, const bool unsignedData )
491491
{
492-
BitSize_t currentByte = ( size >> 3 ) - 1; // PCs
492+
static bool truee = true;
493+
static bool falsee = false;
494+
495+
BitSize_t currentByte;
493496

494497
unsigned char byteMatch;
495498

@@ -505,27 +508,61 @@ void BitStream::WriteCompressed( const unsigned char* inByteArray,
505508

506509
// Write upper bytes with a single 1
507510
// From high byte to low byte, if high byte is a byteMatch then write a 1 bit. Otherwise write a 0 bit and then write the remaining bytes
508-
while ( currentByte > 0 )
511+
if (!IsBigEndian())
509512
{
510-
if ( inByteArray[ currentByte ] == byteMatch ) // If high byte is byteMatch (0 of 0xff) then it would have the same value shifted
513+
/// get the highest byte with highest index PCs
514+
currentByte = (size >> 3) - 1;
515+
516+
/// From high byte to low byte,
517+
/// if high byte is a byteMatch then write a 1 bit.
518+
/// Otherwise write a 0 bit and then write the remaining bytes
519+
while (currentByte > 0)
511520
{
512-
bool b = true;
513-
Write( b );
521+
/// If high byte is byteMatch (0 or 0xff)
522+
/// then it would have the same value shifted
523+
if (inByteArray[currentByte] == byteMatch)
524+
{
525+
Write(truee);
526+
currentByte--;
527+
}
528+
else /// the first byte is not matched
529+
{
530+
Write(falsee);
531+
// Write the remainder of the data after writing bit false
532+
WriteBits(inByteArray, (currentByte + 1) << 3, true);
533+
return;
534+
}
514535
}
515-
else
516-
{
517-
// Write the remainder of the data after writing 0
518-
bool b = false;
519-
Write( b );
520-
521-
WriteBits( inByteArray, ( currentByte + 1 ) << 3, true );
522-
// currentByte--;
523-
536+
/// make sure we are now on the lowest byte (index 0)
537+
RakAssert(currentByte == 0);
538+
}
539+
else
540+
{
541+
/// get the highest byte with highest index PCs
542+
currentByte = 0;
524543

525-
return ;
544+
/// From high byte to low byte,
545+
/// if high byte is a byteMatch then write a 1 bit.
546+
/// Otherwise write a 0 bit and then write the remaining bytes
547+
while (currentByte < ((size >> 3) - 1))
548+
{
549+
/// If high byte is byteMatch (0 or 0xff)
550+
/// then it would have the same value shifted
551+
if (inByteArray[currentByte] == byteMatch)
552+
{
553+
Write(truee);
554+
currentByte++;
555+
}
556+
else /// the first byte is not matched
557+
{
558+
Write(falsee);
559+
// Write the remainder of the data after writing bit false
560+
WriteBits(inByteArray + currentByte, size - (currentByte << 3), true);
561+
return;
562+
}
526563
}
527-
528-
currentByte--;
564+
/// make sure we are now on the lowest byte (index highest)
565+
RakAssert(currentByte == ((size >> 3) - 1));
529566
}
530567

531568
// If the upper half of the last byte is a 0 (positive) or 16 (negative) then write a 1 and the remaining 4 bits. Otherwise write a 0 and the 8 bites.
@@ -617,7 +654,7 @@ bool BitStream::ReadBits( unsigned char *inOutByteArray, BitSize_t numberOfBitsT
617654
bool BitStream::ReadCompressed( unsigned char* inOutByteArray,
618655
const unsigned int size, const bool unsignedData )
619656
{
620-
unsigned int currentByte = ( size >> 3 ) - 1;
657+
unsigned int currentByte;
621658

622659

623660
unsigned char byteMatch, halfByteMatch;
@@ -636,28 +673,46 @@ bool BitStream::ReadCompressed( unsigned char* inOutByteArray,
636673

637674
// Upper bytes are specified with a single 1 if they match byteMatch
638675
// From high byte to low byte, if high byte is a byteMatch then write a 1 bit. Otherwise write a 0 bit and then write the remaining bytes
639-
while ( currentByte > 0 )
676+
if (!IsBigEndian())
640677
{
641-
// If we read a 1 then the data is byteMatch.
642-
643-
bool b;
644-
645-
if ( Read( b ) == false )
646-
return false;
647-
648-
if ( b ) // Check that bit
678+
currentByte = (size >> 3) - 1;
679+
while (currentByte > 0)
649680
{
650-
inOutByteArray[ currentByte ] = byteMatch;
651-
currentByte--;
681+
// If we read a 1 then the data is byteMatch.
682+
bool b;
683+
Read(b);
684+
if (b) // Check that bit
685+
{
686+
data[currentByte] = byteMatch;
687+
currentByte--;
688+
}
689+
else /// the first byte is not matched
690+
{
691+
// Read the rest of the bytes
692+
ReadBits(data, (currentByte + 1) << 3);
693+
return;
694+
}
652695
}
653-
else
696+
}
697+
else
698+
{
699+
currentByte = 0;
700+
while (currentByte < ((size >> 3) - 1))
654701
{
655-
// Read the rest of the bytes
656-
657-
if ( ReadBits( inOutByteArray, ( currentByte + 1 ) << 3 ) == false )
658-
return false;
659-
660-
return true;
702+
// If we read a 1 then the data is byteMatch.
703+
bool b;
704+
Read(b);
705+
if (b) // Check that bit
706+
{
707+
data[currentByte] = byteMatch;
708+
currentByte++;
709+
}
710+
else /// the first byte is not matched
711+
{
712+
// Read the rest of the bytes
713+
ReadBits(data, size - (currentByte << 3));
714+
return;
715+
}
661716
}
662717
}
663718

Source/BitStream.h

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,25 +1278,8 @@ namespace RakNet
12781278
#ifdef _MSC_VER
12791279
#pragma warning(disable:4127) // conditional expression is constant
12801280
#endif
1281-
if (sizeof(inTemplateVar)==1)
12821281
WriteCompressed( ( unsigned char* ) & inTemplateVar, sizeof( templateType ) * 8, true );
1283-
else
1284-
{
1285-
#ifndef __BITSTREAM_NATIVE_END
1286-
#ifdef _MSC_VER
1287-
#pragma warning(disable:4244) // '=' : conversion from 'unsigned long' to 'unsigned short', possible loss of data
1288-
#endif
12891282

1290-
if (DoEndianSwap())
1291-
{
1292-
unsigned char output[sizeof(templateType)];
1293-
ReverseBytes((unsigned char*)&inTemplateVar, output, sizeof(templateType));
1294-
WriteCompressed( ( unsigned char* ) output, sizeof(templateType) * 8, true );
1295-
}
1296-
else
1297-
#endif
1298-
WriteCompressed( ( unsigned char* ) & inTemplateVar, sizeof(templateType) * 8, true );
1299-
}
13001283
}
13011284

13021285
template <>
@@ -1616,25 +1599,7 @@ namespace RakNet
16161599
#ifdef _MSC_VER
16171600
#pragma warning(disable:4127) // conditional expression is constant
16181601
#endif
1619-
if (sizeof(outTemplateVar)==1)
1620-
return ReadCompressed( ( unsigned char* ) &outTemplateVar, sizeof(templateType) * 8, true );
1621-
else
1622-
{
1623-
#ifndef __BITSTREAM_NATIVE_END
1624-
if (DoEndianSwap())
1625-
{
1626-
unsigned char output[sizeof(templateType)];
1627-
if (ReadCompressed( ( unsigned char* ) output, sizeof(templateType) * 8, true ))
1628-
{
1629-
ReverseBytes(output, (unsigned char*)&outTemplateVar, sizeof(templateType));
1630-
return true;
1631-
}
1632-
return false;
1633-
}
1634-
else
1635-
#endif
1636-
return ReadCompressed( ( unsigned char* ) & outTemplateVar, sizeof(templateType) * 8, true );
1637-
}
1602+
return ReadCompressed((unsigned char*)&outTemplateVar, sizeof(templateType) * 8, true);
16381603
}
16391604

16401605
template <>

0 commit comments

Comments
 (0)