From c10ac6c93f9cdc5a68ec164518c7eb3b490df50d Mon Sep 17 00:00:00 2001 From: Jake Zhang Date: Sat, 25 Nov 2017 20:43:10 +1100 Subject: [PATCH 1/2] fixed up endian issue --- Source/src/BitStream.cpp | 63 ++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/Source/src/BitStream.cpp b/Source/src/BitStream.cpp index 3655e4b9c..11270e8f4 100644 --- a/Source/src/BitStream.cpp +++ b/Source/src/BitStream.cpp @@ -614,7 +614,7 @@ bool BitStream::ReadBits( unsigned char *inOutByteArray, BitSize_t numberOfBitsT bool BitStream::ReadCompressed( unsigned char* inOutByteArray, const unsigned int size, const bool unsignedData ) { - unsigned int currentByte = ( size >> 3 ) - 1; + unsigned int currentByte; unsigned char byteMatch, halfByteMatch; @@ -632,29 +632,62 @@ bool BitStream::ReadCompressed( unsigned char* inOutByteArray, } // Upper bytes are specified with a single 1 if they match byteMatch - // 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 - while ( currentByte > 0 ) + // 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 + if (!IsNetworkOrder()) { - // If we read a 1 then the data is byteMatch. + currentByte = (size >> 3) - 1; + while (currentByte > 0) + { + // If we read a 1 then the data is byteMatch. - bool b; + bool b; - if ( Read( b ) == false ) - return false; + if (Read(b) == false) + return false; - if ( b ) // Check that bit - { - inOutByteArray[ currentByte ] = byteMatch; - currentByte--; + if (b) // Check that bit + { + inOutByteArray[currentByte] = byteMatch; + currentByte--; + } + else + { + // Read the rest of the bytes + + if (ReadBits(inOutByteArray, (currentByte + 1) << 3) == false) + return false; + + return true; + } } - else + } + else + { + currentByte = 0; + while (currentByte <((size >>3 )-1)) { - // Read the rest of the bytes + // If we read a 1 then the data is byteMatch. + + bool b; - if ( ReadBits( inOutByteArray, ( currentByte + 1 ) << 3 ) == false ) + if (Read(b) == false) return false; - return true; + if (b) // Check that bit + { + inOutByteArray[currentByte] = byteMatch; + currentByte++; + } + else + { + // Read the rest of the bytes + + if (ReadBits(inOutByteArray, size - (currentByte << 3) == false) + return false; + + return true; + } } } From 5817f7154c4dad75f66d3e7d87750ec42bab18f3 Mon Sep 17 00:00:00 2001 From: Jake Zhang Date: Sat, 25 Nov 2017 20:59:40 +1100 Subject: [PATCH 2/2] fixed up endian issue --- Source/src/BitStream.cpp | 92 +++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 34 deletions(-) diff --git a/Source/src/BitStream.cpp b/Source/src/BitStream.cpp index 11270e8f4..e39ca9a6f 100644 --- a/Source/src/BitStream.cpp +++ b/Source/src/BitStream.cpp @@ -486,58 +486,74 @@ void BitStream::SetData( unsigned char *inByteArray ) void BitStream::WriteCompressed( const unsigned char* inByteArray, const unsigned int size, const bool unsignedData ) { - BitSize_t currentByte = ( size >> 3 ) - 1; // PCs + BitSize_t currentByte; - unsigned char byteMatch; - - if ( unsignedData ) - { - byteMatch = 0; - } - - else - { - byteMatch = 0xFF; - } + unsigned char byteMatch = unsignedData ? 0 : 0xFF; // Write upper bytes with a single 1 - // 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 - while ( currentByte > 0 ) + // 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 + if (!IsNetworkOrder) { - if ( inByteArray[ currentByte ] == byteMatch ) // If high byte is byteMatch (0 of 0xff) then it would have the same value shifted + // get the highest byte with highest index PCs + currentByte = (size >> 3) - 1; + while (currentByte > 0) { - bool b = true; - Write( b ); + if (inByteArray[currentByte] == byteMatch) // If high byte is byteMatch (0 of 0xff) then it would have the same value shifted + { + Write(true); + currByte--; + } + else + { + // Write the remainder of the data after writing 0 + Write(false); + // Write the remainings + WriteBits(inByteArray, (currentByte + 1) << 3, true); + return; + } } - else - { - // Write the remainder of the data after writing 0 - bool b = false; - Write( b ); - - WriteBits( inByteArray, ( currentByte + 1 ) << 3, true ); - // currentByte--; - - return ; + // make sure we are now on the lowest byte (index 0) + if (currentByte > 0) + return false; + } + else + { + // get the highest byte with highest index PCs + currentByte = 0; + while (currentByte > ((size >> 3) - 1)) + { + if (inByteArray[currentByte] == byteMatch) // If high byte is byteMatch (0 of 0xff) then it would have the same value shifted + { + Write(true); + currByte++; + } + else + { + // Write the remainder of the data after writing 0 + Write(false); + // Write the remainings + WriteBits(src + currentByte, size - (currentByte << 3)); + return; + } } - currentByte--; + // make sure we are now on the lowest byte (index highest) + if (currByte < ((size >> 3) - 1)) + return false; } // 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. - if ( ( unsignedData && ( ( *( inByteArray + currentByte ) ) & 0xF0 ) == 0x00 ) || - ( unsignedData == false && ( ( *( inByteArray + currentByte ) ) & 0xF0 ) == 0xF0 ) ) + if ((inByteArray[currentByte] & 0xF0) == 0x00 || (inByteArray[currentByte] & 0xF0) == 0xF0) { - bool b = true; - Write( b ); + Write( true ); WriteBits( inByteArray + currentByte, 4, true ); } else { - bool b = false; - Write( b ); + Write( false ); WriteBits( inByteArray + currentByte, 8, true ); } } @@ -661,6 +677,10 @@ bool BitStream::ReadCompressed( unsigned char* inOutByteArray, return true; } } + + // make sure we are now on the lowest byte (index of 0) + if (currentByte > 0) + return false; } else { @@ -689,6 +709,10 @@ bool BitStream::ReadCompressed( unsigned char* inOutByteArray, return true; } } + + // make sure we are now on the highest byte (index of (size >>3 )-1)) + if (currentByte < (size >> 3) - 1)) + return false; } // All but the first bytes are byteMatch. If the upper half of the last byte is a 0 (positive) or 16 (negative) then what we read will be a 1 and the remaining 4 bits.