2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
26 ===========================================================================
29 #include "precompiled.h"
34 ==============================================================================
38 ==============================================================================
46 idBitMsg::idBitMsg() {
54 allowOverflow = false;
60 idBitMsg::CheckOverflow
63 bool idBitMsg::CheckOverflow( int numBits ) {
64 assert( numBits >= 0 );
65 if ( numBits > GetRemainingWriteBits() ) {
66 if ( !allowOverflow ) {
67 idLib::common->FatalError( "idBitMsg: overflow without allowOverflow set" );
69 if ( numBits > ( maxSize << 3 ) ) {
70 idLib::common->FatalError( "idBitMsg: %i bits is > full message size", numBits );
72 idLib::common->Printf( "idBitMsg: overflow\n" );
82 idBitMsg::GetByteSpace
85 byte *idBitMsg::GetByteSpace( int length ) {
89 idLib::common->FatalError( "idBitMsg::GetByteSpace: cannot write to message" );
92 // round up to the next byte
96 CheckOverflow( length << 3 );
98 ptr = writeData + curSize;
107 If the number of bits is negative a sign is included.
110 void idBitMsg::WriteBits( int value, int numBits ) {
115 idLib::common->Error( "idBitMsg::WriteBits: cannot write to message" );
118 // check if the number of bits is valid
119 if ( numBits == 0 || numBits < -31 || numBits > 32 ) {
120 idLib::common->Error( "idBitMsg::WriteBits: bad numBits %i", numBits );
123 // check for value overflows
124 // this should be an error really, as it can go unnoticed and cause either bandwidth or corrupted data transmitted
125 if ( numBits != 32 ) {
127 if ( value > ( 1 << numBits ) - 1 ) {
128 idLib::common->Warning( "idBitMsg::WriteBits: value overflow %d %d", value, numBits );
129 } else if ( value < 0 ) {
130 idLib::common->Warning( "idBitMsg::WriteBits: value overflow %d %d", value, numBits );
133 int r = 1 << ( - 1 - numBits );
134 if ( value > r - 1 ) {
135 idLib::common->Warning( "idBitMsg::WriteBits: value overflow %d %d", value, numBits );
136 } else if ( value < -r ) {
137 idLib::common->Warning( "idBitMsg::WriteBits: value overflow %d %d", value, numBits );
146 // check for msg overflow
147 if ( CheckOverflow( numBits ) ) {
153 if ( writeBit == 0 ) {
154 writeData[curSize] = 0;
158 if ( put > numBits ) {
161 fraction = value & ( ( 1 << put ) - 1 );
162 writeData[curSize - 1] |= fraction << writeBit;
165 writeBit = ( writeBit + put ) & 7;
171 idBitMsg::WriteString
174 void idBitMsg::WriteString( const char *s, int maxLength, bool make7Bit ) {
182 l = idStr::Length( s );
183 if ( maxLength >= 0 && l >= maxLength ) {
186 dataPtr = GetByteSpace( l + 1 );
187 bytePtr = reinterpret_cast<const byte *>(s);
189 for ( i = 0; i < l; i++ ) {
190 if ( bytePtr[i] > 127 ) {
193 dataPtr[i] = bytePtr[i];
197 for ( i = 0; i < l; i++ ) {
198 dataPtr[i] = bytePtr[i];
210 void idBitMsg::WriteData( const void *data, int length ) {
211 memcpy( GetByteSpace( length ), data, length );
216 idBitMsg::WriteNetadr
219 void idBitMsg::WriteNetadr( const netadr_t adr ) {
221 dataPtr = GetByteSpace( 4 );
222 memcpy( dataPtr, adr.ip, 4 );
223 WriteUShort( adr.port );
231 void idBitMsg::WriteDelta( int oldValue, int newValue, int numBits ) {
232 if ( oldValue == newValue ) {
237 WriteBits( newValue, numBits );
242 idBitMsg::WriteDeltaByteCounter
245 void idBitMsg::WriteDeltaByteCounter( int oldValue, int newValue ) {
248 x = oldValue ^ newValue;
249 for ( i = 7; i > 0; i-- ) {
250 if ( x & ( 1 << i ) ) {
257 WriteBits( ( ( 1 << i ) - 1 ) & newValue, i );
263 idBitMsg::WriteDeltaShortCounter
266 void idBitMsg::WriteDeltaShortCounter( int oldValue, int newValue ) {
269 x = oldValue ^ newValue;
270 for ( i = 15; i > 0; i-- ) {
271 if ( x & ( 1 << i ) ) {
278 WriteBits( ( ( 1 << i ) - 1 ) & newValue, i );
284 idBitMsg::WriteDeltaLongCounter
287 void idBitMsg::WriteDeltaLongCounter( int oldValue, int newValue ) {
290 x = oldValue ^ newValue;
291 for ( i = 31; i > 0; i-- ) {
292 if ( x & ( 1 << i ) ) {
299 WriteBits( ( ( 1 << i ) - 1 ) & newValue, i );
305 idBitMsg::WriteDeltaDict
308 bool idBitMsg::WriteDeltaDict( const idDict &dict, const idDict *base ) {
310 const idKeyValue *kv, *basekv;
311 bool changed = false;
313 if ( base != NULL ) {
315 for ( i = 0; i < dict.GetNumKeyVals(); i++ ) {
316 kv = dict.GetKeyVal( i );
317 basekv = base->FindKey( kv->GetKey() );
318 if ( basekv == NULL || basekv->GetValue().Icmp( kv->GetValue() ) != 0 ) {
319 WriteString( kv->GetKey() );
320 WriteString( kv->GetValue() );
327 for ( i = 0; i < base->GetNumKeyVals(); i++ ) {
328 basekv = base->GetKeyVal( i );
329 kv = dict.FindKey( basekv->GetKey() );
331 WriteString( basekv->GetKey() );
340 for ( i = 0; i < dict.GetNumKeyVals(); i++ ) {
341 kv = dict.GetKeyVal( i );
342 WriteString( kv->GetKey() );
343 WriteString( kv->GetValue() );
359 If the number of bits is negative a sign is included.
362 int idBitMsg::ReadBits( int numBits ) const {
370 idLib::common->FatalError( "idBitMsg::ReadBits: cannot read from message" );
373 // check if the number of bits is valid
374 if ( numBits == 0 || numBits < -31 || numBits > 32 ) {
375 idLib::common->FatalError( "idBitMsg::ReadBits: bad numBits %i", numBits );
388 // check for overflow
389 if ( numBits > GetRemainingReadBits() ) {
393 while ( valueBits < numBits ) {
394 if ( readBit == 0 ) {
398 if ( get > (numBits - valueBits) ) {
399 get = numBits - valueBits;
401 fraction = readData[readCount - 1];
402 fraction >>= readBit;
403 fraction &= ( 1 << get ) - 1;
404 value |= fraction << valueBits;
407 readBit = ( readBit + get ) & 7;
411 if ( value & ( 1 << ( numBits - 1 ) ) ) {
412 value |= -1 ^ ( ( 1 << numBits ) - 1 );
424 int idBitMsg::ReadString( char *buffer, int bufferSize ) const {
431 if ( c <= 0 || c >= 255 ) {
434 // translate all fmt spec to avoid crash bugs in string routines
439 // we will read past any excessively long string, so
440 // the following data can be read, but the string will
442 if ( l < bufferSize - 1 ) {
457 int idBitMsg::ReadData( void *data, int length ) const {
463 if ( readCount + length > curSize ) {
465 memcpy( data, readData + readCount, GetRemaingData() );
470 memcpy( data, readData + readCount, length );
475 return ( readCount - cnt );
483 void idBitMsg::ReadNetadr( netadr_t *adr ) const {
487 for ( i = 0; i < 4; i++ ) {
488 adr->ip[ i ] = ReadByte();
490 adr->port = ReadUShort();
498 int idBitMsg::ReadDelta( int oldValue, int numBits ) const {
499 if ( ReadBits( 1 ) ) {
500 return ReadBits( numBits );
507 idBitMsg::ReadDeltaByteCounter
510 int idBitMsg::ReadDeltaByteCounter( int oldValue ) const {
517 newValue = ReadBits( i );
518 return ( oldValue & ~( ( 1 << i ) - 1 ) | newValue );
523 idBitMsg::ReadDeltaShortCounter
526 int idBitMsg::ReadDeltaShortCounter( int oldValue ) const {
533 newValue = ReadBits( i );
534 return ( oldValue & ~( ( 1 << i ) - 1 ) | newValue );
539 idBitMsg::ReadDeltaLongCounter
542 int idBitMsg::ReadDeltaLongCounter( int oldValue ) const {
549 newValue = ReadBits( i );
550 return ( oldValue & ~( ( 1 << i ) - 1 ) | newValue );
555 idBitMsg::ReadDeltaDict
558 bool idBitMsg::ReadDeltaDict( idDict &dict, const idDict *base ) const {
559 char key[MAX_STRING_CHARS];
560 char value[MAX_STRING_CHARS];
561 bool changed = false;
563 if ( base != NULL ) {
569 while( ReadString( key, sizeof( key ) ) != 0 ) {
570 ReadString( value, sizeof( value ) );
571 dict.Set( key, value );
575 while( ReadString( key, sizeof( key ) ) != 0 ) {
588 int idBitMsg::DirToBits( const idVec3 &dir, int numBits ) {
592 assert( numBits >= 6 && numBits <= 32 );
593 assert( dir.LengthSqr() - 1.0f < 0.01f );
596 max = ( 1 << ( numBits - 1 ) ) - 1;
599 bits = FLOATSIGNBITSET( dir.x ) << ( numBits * 3 - 1 );
600 bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.x ) + bias ) * max ) ) << ( numBits * 2 );
601 bits |= FLOATSIGNBITSET( dir.y ) << ( numBits * 2 - 1 );
602 bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.y ) + bias ) * max ) ) << ( numBits * 1 );
603 bits |= FLOATSIGNBITSET( dir.z ) << ( numBits * 1 - 1 );
604 bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.z ) + bias ) * max ) ) << ( numBits * 0 );
613 idVec3 idBitMsg::BitsToDir( int bits, int numBits ) {
614 static float sign[2] = { 1.0f, -1.0f };
619 assert( numBits >= 6 && numBits <= 32 );
622 max = ( 1 << ( numBits - 1 ) ) - 1;
625 dir.x = sign[( bits >> ( numBits * 3 - 1 ) ) & 1] * ( ( bits >> ( numBits * 2 ) ) & max ) * invMax;
626 dir.y = sign[( bits >> ( numBits * 2 - 1 ) ) & 1] * ( ( bits >> ( numBits * 1 ) ) & max ) * invMax;
627 dir.z = sign[( bits >> ( numBits * 1 - 1 ) ) & 1] * ( ( bits >> ( numBits * 0 ) ) & max ) * invMax;
634 ==============================================================================
638 ==============================================================================
641 const int MAX_DATA_BUFFER = 1024;
645 idBitMsgDelta::WriteBits
648 void idBitMsgDelta::WriteBits( int value, int numBits ) {
650 newBase->WriteBits( value, numBits );
654 writeDelta->WriteBits( value, numBits );
657 int baseValue = base->ReadBits( numBits );
658 if ( baseValue == value ) {
659 writeDelta->WriteBits( 0, 1 );
661 writeDelta->WriteBits( 1, 1 );
662 writeDelta->WriteBits( value, numBits );
670 idBitMsgDelta::WriteDelta
673 void idBitMsgDelta::WriteDelta( int oldValue, int newValue, int numBits ) {
675 newBase->WriteBits( newValue, numBits );
679 if ( oldValue == newValue ) {
680 writeDelta->WriteBits( 0, 1 );
682 writeDelta->WriteBits( 1, 1 );
683 writeDelta->WriteBits( newValue, numBits );
687 int baseValue = base->ReadBits( numBits );
688 if ( baseValue == newValue ) {
689 writeDelta->WriteBits( 0, 1 );
691 writeDelta->WriteBits( 1, 1 );
692 if ( oldValue == newValue ) {
693 writeDelta->WriteBits( 0, 1 );
696 writeDelta->WriteBits( 1, 1 );
697 writeDelta->WriteBits( newValue, numBits );
706 idBitMsgDelta::ReadBits
709 int idBitMsgDelta::ReadBits( int numBits ) const {
713 value = readDelta->ReadBits( numBits );
716 int baseValue = base->ReadBits( numBits );
717 if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
720 value = readDelta->ReadBits( numBits );
726 newBase->WriteBits( value, numBits );
733 idBitMsgDelta::ReadDelta
736 int idBitMsgDelta::ReadDelta( int oldValue, int numBits ) const {
740 if ( readDelta->ReadBits( 1 ) == 0 ) {
743 value = readDelta->ReadBits( numBits );
747 int baseValue = base->ReadBits( numBits );
748 if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
750 } else if ( readDelta->ReadBits( 1 ) == 0 ) {
754 value = readDelta->ReadBits( numBits );
760 newBase->WriteBits( value, numBits );
767 idBitMsgDelta::WriteString
770 void idBitMsgDelta::WriteString( const char *s, int maxLength ) {
772 newBase->WriteString( s, maxLength );
776 writeDelta->WriteString( s, maxLength );
779 char baseString[MAX_DATA_BUFFER];
780 base->ReadString( baseString, sizeof( baseString ) );
781 if ( idStr::Cmp( s, baseString ) == 0 ) {
782 writeDelta->WriteBits( 0, 1 );
784 writeDelta->WriteBits( 1, 1 );
785 writeDelta->WriteString( s, maxLength );
793 idBitMsgDelta::WriteData
796 void idBitMsgDelta::WriteData( const void *data, int length ) {
798 newBase->WriteData( data, length );
802 writeDelta->WriteData( data, length );
805 byte baseData[MAX_DATA_BUFFER];
806 assert( length < sizeof( baseData ) );
807 base->ReadData( baseData, length );
808 if ( memcmp( data, baseData, length ) == 0 ) {
809 writeDelta->WriteBits( 0, 1 );
811 writeDelta->WriteBits( 1, 1 );
812 writeDelta->WriteData( data, length );
820 idBitMsgDelta::WriteDict
823 void idBitMsgDelta::WriteDict( const idDict &dict ) {
825 newBase->WriteDeltaDict( dict, NULL );
829 writeDelta->WriteDeltaDict( dict, NULL );
833 base->ReadDeltaDict( baseDict, NULL );
834 changed = writeDelta->WriteDeltaDict( dict, &baseDict );
840 idBitMsgDelta::WriteDeltaByteCounter
843 void idBitMsgDelta::WriteDeltaByteCounter( int oldValue, int newValue ) {
845 newBase->WriteBits( newValue, 8 );
849 writeDelta->WriteDeltaByteCounter( oldValue, newValue );
852 int baseValue = base->ReadBits( 8 );
853 if ( baseValue == newValue ) {
854 writeDelta->WriteBits( 0, 1 );
856 writeDelta->WriteBits( 1, 1 );
857 writeDelta->WriteDeltaByteCounter( oldValue, newValue );
865 idBitMsgDelta::WriteDeltaShortCounter
868 void idBitMsgDelta::WriteDeltaShortCounter( int oldValue, int newValue ) {
870 newBase->WriteBits( newValue, 16 );
874 writeDelta->WriteDeltaShortCounter( oldValue, newValue );
877 int baseValue = base->ReadBits( 16 );
878 if ( baseValue == newValue ) {
879 writeDelta->WriteBits( 0, 1 );
881 writeDelta->WriteBits( 1, 1 );
882 writeDelta->WriteDeltaShortCounter( oldValue, newValue );
890 idBitMsgDelta::WriteDeltaLongCounter
893 void idBitMsgDelta::WriteDeltaLongCounter( int oldValue, int newValue ) {
895 newBase->WriteBits( newValue, 32 );
899 writeDelta->WriteDeltaLongCounter( oldValue, newValue );
902 int baseValue = base->ReadBits( 32 );
903 if ( baseValue == newValue ) {
904 writeDelta->WriteBits( 0, 1 );
906 writeDelta->WriteBits( 1, 1 );
907 writeDelta->WriteDeltaLongCounter( oldValue, newValue );
915 idBitMsgDelta::ReadString
918 void idBitMsgDelta::ReadString( char *buffer, int bufferSize ) const {
920 readDelta->ReadString( buffer, bufferSize );
923 char baseString[MAX_DATA_BUFFER];
924 base->ReadString( baseString, sizeof( baseString ) );
925 if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
926 idStr::Copynz( buffer, baseString, bufferSize );
928 readDelta->ReadString( buffer, bufferSize );
934 newBase->WriteString( buffer );
940 idBitMsgDelta::ReadData
943 void idBitMsgDelta::ReadData( void *data, int length ) const {
945 readDelta->ReadData( data, length );
948 char baseData[MAX_DATA_BUFFER];
949 assert( length < sizeof( baseData ) );
950 base->ReadData( baseData, length );
951 if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
952 memcpy( data, baseData, length );
954 readDelta->ReadData( data, length );
960 newBase->WriteData( data, length );
966 idBitMsgDelta::ReadDict
969 void idBitMsgDelta::ReadDict( idDict &dict ) {
971 readDelta->ReadDeltaDict( dict, NULL );
975 base->ReadDeltaDict( baseDict, NULL );
979 changed = readDelta->ReadDeltaDict( dict, &baseDict );
984 newBase->WriteDeltaDict( dict, NULL );
990 idBitMsgDelta::ReadDeltaByteCounter
993 int idBitMsgDelta::ReadDeltaByteCounter( int oldValue ) const {
997 value = readDelta->ReadDeltaByteCounter( oldValue );
1000 int baseValue = base->ReadBits( 8 );
1001 if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
1004 value = readDelta->ReadDeltaByteCounter( oldValue );
1010 newBase->WriteBits( value, 8 );
1017 idBitMsgDelta::ReadDeltaShortCounter
1020 int idBitMsgDelta::ReadDeltaShortCounter( int oldValue ) const {
1024 value = readDelta->ReadDeltaShortCounter( oldValue );
1027 int baseValue = base->ReadBits( 16 );
1028 if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
1031 value = readDelta->ReadDeltaShortCounter( oldValue );
1037 newBase->WriteBits( value, 16 );
1044 idBitMsgDelta::ReadDeltaLongCounter
1047 int idBitMsgDelta::ReadDeltaLongCounter( int oldValue ) const {
1051 value = readDelta->ReadDeltaLongCounter( oldValue );
1054 int baseValue = base->ReadBits( 32 );
1055 if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
1058 value = readDelta->ReadDeltaLongCounter( oldValue );
1064 newBase->WriteBits( value, 32 );