@@ -489,7 +489,10 @@ void BitStream::SetData( unsigned char *inByteArray )
489489void 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
617654bool 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
0 commit comments