home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / BitMsg.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  26KB  |  1,298 lines

  1. #include "precompiled.h"
  2. #pragma hdrstop
  3.  
  4.  
  5. /*
  6. ==============================================================================
  7.  
  8.   idBitMsg
  9.  
  10. ==============================================================================
  11. */
  12.  
  13. /*
  14. ================
  15. idBitMsg::idBitMsg
  16. ================
  17. */
  18. idBitMsg::idBitMsg() {
  19.     writeData = NULL;
  20.     readData = NULL;
  21.     maxSize = 0;
  22.     curSize = 0;
  23.     writeBit = 0;
  24.     readCount = 0;
  25.     readBit = 0;
  26.     numValueOverflows = 0;
  27.     allowOverflow = false;
  28.     overflowed = false;
  29. }
  30.  
  31. /*
  32. ================
  33. idBitMsg::CheckOverflow
  34. ================
  35. */
  36. bool idBitMsg::CheckOverflow( int numBits ) {
  37.     assert( numBits >= 0 );
  38.     if ( numBits > GetRemainingWriteBits() ) {
  39.         if ( !allowOverflow ) {
  40.             idLib::common->FatalError( "idBitMsg: overflow without allowOverflow set" );
  41.         }
  42.         if ( numBits > ( maxSize << 3 ) ) {
  43.             idLib::common->FatalError( "idBitMsg: %i bits is > full message size", numBits );
  44.         }
  45.         idLib::common->Printf( "idBitMsg: overflow\n" );
  46.         BeginWriting();
  47.         overflowed = true;
  48.         return true;
  49.     }
  50.     return false;
  51. }
  52.  
  53. /*
  54. ================
  55. idBitMsg::GetByteSpace
  56. ================
  57. */
  58. byte *idBitMsg::GetByteSpace( int length ) {
  59.     byte *ptr;
  60.  
  61.     if ( !writeData ) {
  62.         idLib::common->FatalError( "idBitMsg::GetByteSpace: cannot write to message" );
  63.     }
  64.  
  65.     // round up to the next byte
  66.     WriteByteAlign();
  67.  
  68.     // check for overflow
  69.     CheckOverflow( length << 3 );
  70.  
  71.     ptr = writeData + curSize;
  72.     curSize += length;
  73.     return ptr;
  74. }
  75.  
  76. /*
  77. ================
  78. idBitMsg::WriteBits
  79.  
  80.   If the number of bits is negative a sign is included.
  81. ================
  82. */
  83. void idBitMsg::WriteBits( int value, int numBits ) {
  84.     int        put;
  85.     int        fraction;
  86.  
  87.     if ( !writeData ) {
  88.         idLib::common->FatalError( "idBitMsg::WriteBits: cannot write to message" );
  89.     }
  90.  
  91.     // check if the number of bits is valid
  92.     if ( numBits == 0 || numBits < -31 || numBits > 32 ) {
  93.         idLib::common->FatalError( "idBitMsg::WriteBits: bad numBits %i", numBits );
  94.     }
  95.  
  96.     // check for value overflows
  97.     if ( numBits != 32 ) {
  98.         if ( numBits > 0 ) {
  99.             if ( value > ( 1 << numBits ) - 1 ) {
  100.                 numValueOverflows++;
  101.             } else if ( value < 0 ) {
  102.                 numValueOverflows++;
  103.             }
  104.         } else {
  105.             int r = 1 << ( numBits - 1 );
  106.             if ( value > r - 1 ) {
  107.                 numValueOverflows++;
  108.             } else if ( value < -r ) {
  109.                 numValueOverflows++;
  110.             }
  111.         }
  112.     }
  113.  
  114.     if ( numBits < 0 ) {
  115.         numBits = -numBits;
  116.     }
  117.  
  118.     // check for msg overflow
  119.     if ( CheckOverflow( numBits ) ) {
  120.         return;
  121.     }
  122.  
  123.     // write the bits
  124.     while( numBits ) {
  125.         if ( writeBit == 0 ) {
  126.             writeData[curSize] = 0;
  127.             curSize++;
  128.         }
  129.         put = 8 - writeBit;
  130.         if ( put > numBits ) {
  131.             put = numBits;
  132.         }
  133.         fraction = value & ( ( 1 << put ) - 1 );
  134.         writeData[curSize - 1] |= fraction << writeBit;
  135.         numBits -= put;
  136.         value >>= put;
  137.         writeBit = ( writeBit + put ) & 7;
  138.     }
  139. }
  140.  
  141. /*
  142. ================
  143. idBitMsg::WriteString
  144. ================
  145. */
  146. void idBitMsg::WriteString( const char *s, int maxLength ) {
  147.     if ( !s ) {
  148.         WriteData( "", 1 );
  149.     } else {
  150.         int l;
  151.         byte *dataPtr;
  152.  
  153.         l = idStr::Length( s );
  154.         if ( maxLength >= 0 && l >= maxLength ) {
  155.             l = maxLength - 1;
  156.         }
  157.         dataPtr = GetByteSpace( l + 1 );
  158.         memcpy( dataPtr, s, l );
  159.         dataPtr[l] = '\0';
  160.     }
  161. }
  162.  
  163. /*
  164. ================
  165. idBitMsg::WriteData
  166. ================
  167. */
  168. void idBitMsg::WriteData( const void *data, int length ) {
  169.     memcpy( GetByteSpace( length ), data, length );
  170. }
  171.  
  172. /*
  173. ================
  174. idBitMsg::WriteNetadr
  175. ================
  176. */
  177. void idBitMsg::WriteNetadr( const netadr_t adr ) {
  178.     byte *dataPtr;
  179.     dataPtr = GetByteSpace( 4 );
  180.     memcpy( dataPtr, adr.ip, 4 );
  181.     WriteShort( adr.port );
  182. }
  183.  
  184. /*
  185. ================
  186. idBitMsg::WriteDelta
  187. ================
  188. */
  189. void idBitMsg::WriteDelta( int oldValue, int newValue, int numBits ) {
  190.     if ( oldValue == newValue ) {
  191.         WriteBits( 0, 1 );
  192.         return;
  193.     }
  194.     WriteBits( 1, 1 );
  195.     WriteBits( newValue, numBits );
  196. }
  197.  
  198. /*
  199. ================
  200. idBitMsg::WriteDeltaByteCounter
  201. ================
  202. */
  203. void idBitMsg::WriteDeltaByteCounter( int oldValue, int newValue ) {
  204.     int i, x;
  205.  
  206.     x = oldValue ^ newValue;
  207.     for ( i = 7; i > 0; i-- ) {
  208.         if ( x & ( 1 << i ) ) {
  209.             i++;
  210.             break;
  211.         }
  212.     }
  213.     WriteBits( i, 3 );
  214.     if ( i ) {
  215.         WriteBits( newValue, i );
  216.     }
  217. }
  218.  
  219. /*
  220. ================
  221. idBitMsg::WriteDeltaShortCounter
  222. ================
  223. */
  224. void idBitMsg::WriteDeltaShortCounter( int oldValue, int newValue ) {
  225.     int i, x;
  226.  
  227.     x = oldValue ^ newValue;
  228.     for ( i = 15; i > 0; i-- ) {
  229.         if ( x & ( 1 << i ) ) {
  230.             i++;
  231.             break;
  232.         }
  233.     }
  234.     WriteBits( i, 4 );
  235.     if ( i ) {
  236.         WriteBits( newValue, i );
  237.     }
  238. }
  239.  
  240. /*
  241. ================
  242. idBitMsg::WriteDeltaLongCounter
  243. ================
  244. */
  245. void idBitMsg::WriteDeltaLongCounter( int oldValue, int newValue ) {
  246.     int i, x;
  247.  
  248.     x = oldValue ^ newValue;
  249.     for ( i = 31; i > 0; i-- ) {
  250.         if ( x & ( 1 << i ) ) {
  251.             i++;
  252.             break;
  253.         }
  254.     }
  255.     WriteBits( i, 5 );
  256.     if ( i ) {
  257.         WriteBits( newValue, i );
  258.     }
  259. }
  260.  
  261. /*
  262. ==================
  263. idBitMsg::WriteDeltaDict
  264. ==================
  265. */
  266. bool idBitMsg::WriteDeltaDict( const idDict &dict, const idDict *base ) {
  267.     int i;
  268.     const idKeyValue *kv, *basekv;
  269.     bool changed = false;
  270.  
  271.     if ( base != NULL ) {
  272.  
  273.         for ( i = 0; i < dict.GetNumKeyVals(); i++ ) {
  274.             kv = dict.GetKeyVal( i );
  275.             basekv = base->FindKey( kv->GetKey() );
  276.             if ( basekv == NULL || basekv->GetValue().Icmp( kv->GetValue() ) != 0 ) {
  277.                 WriteString( kv->GetKey() );
  278.                 WriteString( kv->GetValue() );
  279.                 changed = true;
  280.             }
  281.         }
  282.  
  283.         WriteString( "" );
  284.  
  285.         for ( i = 0; i < base->GetNumKeyVals(); i++ ) {
  286.             basekv = base->GetKeyVal( i );
  287.             kv = dict.FindKey( basekv->GetKey() );
  288.             if ( kv == NULL ) {
  289.                 WriteString( basekv->GetKey() );
  290.                 changed = true;
  291.             }
  292.         }
  293.  
  294.         WriteString( "" );
  295.  
  296.     } else {
  297.  
  298.         for ( i = 0; i < dict.GetNumKeyVals(); i++ ) {
  299.             kv = dict.GetKeyVal( i );
  300.             WriteString( kv->GetKey() );
  301.             WriteString( kv->GetValue() );
  302.             changed = true;
  303.         }
  304.         WriteString( "" );
  305.  
  306.         WriteString( "" );
  307.  
  308.     }
  309.  
  310.     return changed;
  311. }
  312.  
  313. /*
  314. ================
  315. idBitMsg::ReadBits
  316.  
  317.   If the number of bits is negative a sign is included.
  318. ================
  319. */
  320. int idBitMsg::ReadBits( int numBits ) const {
  321.     int        value;
  322.     int        valueBits;
  323.     int        get;
  324.     int        fraction;
  325.     bool    sgn;
  326.  
  327.     if ( !readData ) {
  328.         idLib::common->FatalError( "idBitMsg::ReadBits: cannot read from message" );
  329.     }
  330.  
  331.     // check if the number of bits is valid
  332.     if ( numBits == 0 || numBits < -31 || numBits > 32 ) {
  333.         idLib::common->FatalError( "idBitMsg::ReadBits: bad numBits %i", numBits );
  334.     }
  335.  
  336.     value = 0;
  337.     valueBits = 0;
  338.  
  339.     if ( numBits < 0 ) {
  340.         numBits = -numBits;
  341.         sgn = true;
  342.     } else {
  343.         sgn = false;
  344.     }
  345.  
  346.     // check for overflow
  347.     if ( numBits > GetRemainingReadBits() ) {
  348.         return -1;
  349.     }
  350.  
  351.     while ( valueBits < numBits ) {
  352.         if ( readBit == 0 ) {
  353.             readCount++;
  354.         }
  355.         get = 8 - readBit;
  356.         if ( get > (numBits - valueBits) ) {
  357.             get = numBits - valueBits;
  358.         }
  359.         fraction = readData[readCount - 1];
  360.         fraction >>= readBit;
  361.         fraction &= ( 1 << get ) - 1;
  362.         value |= fraction << valueBits;
  363.  
  364.         valueBits += get;
  365.         readBit = ( readBit + get ) & 7;
  366.     }
  367.  
  368.     if ( sgn ) {
  369.         if ( value & ( 1 << ( numBits - 1 ) ) ) {
  370.             value |= -1 ^ ( ( 1 << numBits ) - 1 );
  371.         }
  372.     }
  373.  
  374.     return value;
  375. }
  376.  
  377. /*
  378. ================
  379. idBitMsg::ReadString
  380. ================
  381. */
  382. int idBitMsg::ReadString( char *buffer, int bufferSize ) const {
  383.     int    l, c;
  384.     
  385.     ReadByteAlign();
  386.     l = 0;
  387.     while( 1 ) {
  388.         c = ReadByte();
  389.         if ( c <= 0 || c >= 255 ) {
  390.             break;
  391.         }
  392.         // translate all fmt spec to avoid crash bugs in string routines
  393.         if ( c == '%' ) {
  394.             c = '.';
  395.         }
  396.  
  397.         // we will read past any excessively long string, so
  398.         // the following data can be read, but the string will
  399.         // be truncated
  400.         if ( l < bufferSize - 1 ) {
  401.             buffer[l] = c;
  402.             l++;
  403.         }
  404.     }
  405.     
  406.     buffer[l] = 0;
  407.     return l;
  408. }
  409.  
  410. /*
  411. ================
  412. idBitMsg::ReadData
  413. ================
  414. */
  415. int idBitMsg::ReadData( void *data, int length ) const {
  416.     int cnt;
  417.  
  418.     ReadByteAlign();
  419.     cnt = readCount;
  420.  
  421.     if ( readCount + length > curSize ) {
  422.         if ( data ) {
  423.             memcpy( data, readData + readCount, GetRemainingData() );
  424.         }
  425.         readCount = curSize;
  426.     } else {
  427.         if ( data ) {
  428.             memcpy( data, readData + readCount, length );
  429.         }
  430.         readCount += length;
  431.     }
  432.  
  433.     return ( readCount - cnt );
  434. }
  435.  
  436. /*
  437. ================
  438. idBitMsg::ReadNetadr
  439. ================
  440. */
  441. void idBitMsg::ReadNetadr( netadr_t *adr ) const {
  442.     int i;
  443.  
  444.     adr->type = NA_IP;
  445.     for ( i = 0; i < 4; i++ ) {
  446.         adr->ip[ i ] = ReadByte();
  447.     }
  448.     adr->port = ReadShort();
  449. }
  450.  
  451. /*
  452. ================
  453. idBitMsg::ReadDelta
  454. ================
  455. */
  456. int idBitMsg::ReadDelta( int oldValue, int numBits ) const {
  457.     if ( ReadBits( 1 ) ) {
  458.         return ReadBits( numBits );
  459.     }
  460.     return oldValue;
  461. }
  462.  
  463. /*
  464. ================
  465. idBitMsg::ReadDeltaByteCounter
  466. ================
  467. */
  468. int idBitMsg::ReadDeltaByteCounter( int oldValue ) const {
  469.     int i, newValue;
  470.  
  471.     i = ReadBits( 3 );
  472.     if ( !i ) {
  473.         return oldValue;
  474.     }
  475.     newValue = ReadBits( i );
  476.     return ( oldValue & ~( ( 1 << i ) - 1 ) | newValue );
  477. }
  478.  
  479. /*
  480. ================
  481. idBitMsg::ReadDeltaShortCounter
  482. ================
  483. */
  484. int idBitMsg::ReadDeltaShortCounter( int oldValue ) const {
  485.     int i, newValue;
  486.  
  487.     i = ReadBits( 4 );
  488.     if ( !i ) {
  489.         return oldValue;
  490.     }
  491.     newValue = ReadBits( i );
  492.     return ( oldValue & ~( ( 1 << i ) - 1 ) | newValue );
  493. }
  494.  
  495. /*
  496. ================
  497. idBitMsg::ReadDeltaLongCounter
  498. ================
  499. */
  500. int idBitMsg::ReadDeltaLongCounter( int oldValue ) const {
  501.     int i, newValue;
  502.  
  503.     i = ReadBits( 5 );
  504.     if ( !i ) {
  505.         return oldValue;
  506.     }
  507.     newValue = ReadBits( i );
  508.     return ( oldValue & ~( ( 1 << i ) - 1 ) | newValue );
  509. }
  510.  
  511. /*
  512. ==================
  513. idBitMsg::ReadDeltaDict
  514. ==================
  515. */
  516. bool idBitMsg::ReadDeltaDict( idDict &dict, const idDict *base ) const {
  517.     char        key[MAX_STRING_CHARS];
  518.     char        value[MAX_STRING_CHARS];
  519.     bool        changed = false;
  520.  
  521.     if ( base != NULL ) {
  522.         dict = *base;
  523.     } else {
  524.         dict.Clear();
  525.     }
  526.  
  527.     while( ReadString( key, sizeof( key ) ) != 0 ) {
  528.         ReadString( value, sizeof( value ) );
  529.         dict.Set( key, value );
  530.         changed = true;
  531.     }
  532.  
  533.     while( ReadString( key, sizeof( key ) ) != 0 ) {
  534.         dict.Delete( key );
  535.         changed = true;
  536.     }
  537.  
  538.     return changed;
  539. }
  540.  
  541. /*
  542. ================
  543. idBitMsg::DirToBits
  544. ================
  545. */
  546. int idBitMsg::DirToBits( const idVec3 &dir, int numBits ) {
  547.     int max, bits;
  548.     float bias;
  549.  
  550.     assert( numBits >= 6 && numBits <= 32 );
  551.     assert( dir.LengthSqr() - 1.0f < 0.01f );
  552.  
  553.     numBits /= 3;
  554.     max = ( 1 << ( numBits - 1 ) ) - 1;
  555.     bias = 0.5f / max;
  556.  
  557.     bits = FLOATSIGNBITSET( dir.x ) << ( numBits * 3 - 1 );
  558.     bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.x ) + bias ) * max ) ) << ( numBits * 2 );
  559.     bits |= FLOATSIGNBITSET( dir.y ) << ( numBits * 2 - 1 );
  560.     bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.y ) + bias ) * max ) ) << ( numBits * 1 );
  561.     bits |= FLOATSIGNBITSET( dir.z ) << ( numBits * 1 - 1 );
  562.     bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.z ) + bias ) * max ) ) << ( numBits * 0 );
  563.     return bits;
  564. }
  565.  
  566. /*
  567. ================
  568. idBitMsg::BitsToDir
  569. ================
  570. */
  571. idVec3 idBitMsg::BitsToDir( int bits, int numBits ) {
  572.     static float sign[2] = { 1.0f, -1.0f };
  573.     int max;
  574.     float invMax;
  575.     idVec3 dir;
  576.  
  577.     assert( numBits >= 6 && numBits <= 32 );
  578.  
  579.     numBits /= 3;
  580.     max = ( 1 << ( numBits - 1 ) ) - 1;
  581.     invMax = 1.0f / max;
  582.  
  583.     dir.x = sign[( bits >> ( numBits * 3 - 1 ) ) & 1] * ( ( bits >> ( numBits * 2 ) ) & max ) * invMax;
  584.     dir.y = sign[( bits >> ( numBits * 2 - 1 ) ) & 1] * ( ( bits >> ( numBits * 1 ) ) & max ) * invMax;
  585.     dir.z = sign[( bits >> ( numBits * 1 - 1 ) ) & 1] * ( ( bits >> ( numBits * 0 ) ) & max ) * invMax;
  586.     dir.NormalizeFast();
  587.     return dir;
  588. }
  589.  
  590.  
  591. /*
  592. ==============================================================================
  593.  
  594.   idBitMsgDelta
  595.  
  596. ==============================================================================
  597. */
  598.  
  599. const int MAX_DATA_BUFFER        = 1024;
  600.  
  601. /*
  602. ================
  603. idBitMsgDelta::WriteBits
  604. ================
  605. */
  606. void idBitMsgDelta::WriteBits( int value, int numBits ) {
  607.     if ( newBase ) {
  608.         newBase->WriteBits( value, numBits );
  609.     }
  610.  
  611.     if ( !base ) {
  612.         writeDelta->WriteBits( value, numBits );
  613.         changed = true;
  614.     } else {
  615.         int baseValue = base->ReadBits( numBits );
  616.         if ( baseValue == value ) {
  617.             writeDelta->WriteBits( 0, 1 );
  618.         } else {
  619.             writeDelta->WriteBits( 1, 1 );
  620.             writeDelta->WriteBits( value, numBits );
  621.             changed = true;
  622.         }
  623.     }
  624. }
  625.  
  626. /*
  627. ================
  628. idBitMsgDelta::WriteDelta
  629. ================
  630. */
  631. void idBitMsgDelta::WriteDelta( int oldValue, int newValue, int numBits ) {
  632.     if ( newBase ) {
  633.         newBase->WriteBits( newValue, numBits );
  634.     }
  635.  
  636.     if ( !base ) {
  637.         if ( oldValue == newValue ) {
  638.             writeDelta->WriteBits( 0, 1 );
  639.         } else {
  640.             writeDelta->WriteBits( 1, 1 );
  641.             writeDelta->WriteBits( newValue, numBits );
  642.         }
  643.         changed = true;
  644.     } else {
  645.         int baseValue = base->ReadBits( numBits );
  646.         if ( baseValue == newValue ) {
  647.             writeDelta->WriteBits( 0, 1 );
  648.         } else {
  649.             writeDelta->WriteBits( 1, 1 );
  650.             if ( oldValue == newValue ) {
  651.                 writeDelta->WriteBits( 0, 1 );
  652.                 changed = true;
  653.             } else {
  654.                 writeDelta->WriteBits( 1, 1 );
  655.                 writeDelta->WriteBits( newValue, numBits );
  656.                 changed = true;
  657.             }
  658.         }
  659.     }
  660. }
  661.  
  662. /*
  663. ================
  664. idBitMsgDelta::ReadBits
  665. ================
  666. */
  667. int idBitMsgDelta::ReadBits( int numBits ) const {
  668.     int value;
  669.  
  670.     if ( !base ) {
  671.         value = readDelta->ReadBits( numBits );
  672.         changed = true;
  673.     } else {
  674.         int baseValue = base->ReadBits( numBits );
  675.         if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  676.             value = baseValue;
  677.         } else {
  678.             value = readDelta->ReadBits( numBits );
  679.             changed = true;
  680.         }
  681.     }
  682.  
  683.     if ( newBase ) {
  684.         newBase->WriteBits( value, numBits );
  685.     }
  686.     return value;
  687. }
  688.  
  689. /*
  690. ================
  691. idBitMsgDelta::ReadDelta
  692. ================
  693. */
  694. int idBitMsgDelta::ReadDelta( int oldValue, int numBits ) const {
  695.     int value;
  696.  
  697.     if ( !base ) {
  698.         if ( readDelta->ReadBits( 1 ) == 0 ) {
  699.             value = oldValue;
  700.         } else {
  701.             value = readDelta->ReadBits( numBits );
  702.         }
  703.         changed = true;
  704.     } else {
  705.         int baseValue = base->ReadBits( numBits );
  706.         if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  707.             value = baseValue;
  708.         } else if ( readDelta->ReadBits( 1 ) == 0 ) {
  709.             value = oldValue;
  710.             changed = true;
  711.         } else {
  712.             value = readDelta->ReadBits( numBits );
  713.             changed = true;
  714.         }
  715.     }
  716.  
  717.     if ( newBase ) {
  718.         newBase->WriteBits( value, numBits );
  719.     }
  720.     return value;
  721. }
  722.  
  723. /*
  724. ================
  725. idBitMsgDelta::WriteString
  726. ================
  727. */
  728. void idBitMsgDelta::WriteString( const char *s, int maxLength ) {
  729.     if ( newBase ) {
  730.         newBase->WriteString( s, maxLength );
  731.     }
  732.  
  733.     if ( !base ) {
  734.         writeDelta->WriteString( s, maxLength );
  735.         changed = true;
  736.     } else {
  737.         char baseString[MAX_DATA_BUFFER];
  738.         base->ReadString( baseString, sizeof( baseString ) );
  739.         if ( idStr::Cmp( s, baseString ) == 0 ) {
  740.             writeDelta->WriteBits( 0, 1 );
  741.         } else {
  742.             writeDelta->WriteBits( 1, 1 );
  743.             writeDelta->WriteString( s, maxLength );
  744.             changed = true;
  745.         }
  746.     }
  747. }
  748.  
  749. /*
  750. ================
  751. idBitMsgDelta::WriteData
  752. ================
  753. */
  754. void idBitMsgDelta::WriteData( const void *data, int length ) {
  755.     if ( newBase ) {
  756.         newBase->WriteData( data, length );
  757.     }
  758.  
  759.     if ( !base ) {
  760.         writeDelta->WriteData( data, length );
  761.         changed = true;
  762.     } else {
  763.         byte baseData[MAX_DATA_BUFFER];
  764.         assert( length < sizeof( baseData ) );
  765.         base->ReadData( baseData, length );
  766.         if ( memcmp( data, baseData, length ) == 0 ) {
  767.             writeDelta->WriteBits( 0, 1 );
  768.         } else {
  769.             writeDelta->WriteBits( 1, 1 );
  770.             writeDelta->WriteData( data, length );
  771.             changed = true;
  772.         }
  773.     }
  774. }
  775.  
  776. /*
  777. ================
  778. idBitMsgDelta::WriteDict
  779. ================
  780. */
  781. void idBitMsgDelta::WriteDict( const idDict &dict ) {
  782.     if ( newBase ) {
  783.         newBase->WriteDeltaDict( dict, NULL );
  784.     }
  785.  
  786.     if ( !base ) {
  787.         writeDelta->WriteDeltaDict( dict, NULL );
  788.         changed = true;
  789.     } else {
  790.         idDict baseDict;
  791.         base->ReadDeltaDict( baseDict, NULL );
  792.         changed = writeDelta->WriteDeltaDict( dict, &baseDict );
  793.     }
  794. }
  795.  
  796. /*
  797. ================
  798. idBitMsgDelta::WriteDeltaByteCounter
  799. ================
  800. */
  801. void idBitMsgDelta::WriteDeltaByteCounter( int oldValue, int newValue ) {
  802.     if ( newBase ) {
  803.         newBase->WriteBits( newValue, 8 );
  804.     }
  805.  
  806.     if ( !base ) {
  807.         writeDelta->WriteDeltaByteCounter( oldValue, newValue );
  808.         changed = true;
  809.     } else {
  810.         int baseValue = base->ReadBits( 8 );
  811.         if ( baseValue == newValue ) {
  812.             writeDelta->WriteBits( 0, 1 );
  813.         } else {
  814.             writeDelta->WriteBits( 1, 1 );
  815.             writeDelta->WriteDeltaByteCounter( oldValue, newValue );
  816.             changed = true;
  817.         }
  818.     }
  819. }
  820.  
  821. /*
  822. ================
  823. idBitMsgDelta::WriteDeltaShortCounter
  824. ================
  825. */
  826. void idBitMsgDelta::WriteDeltaShortCounter( int oldValue, int newValue ) {
  827.     if ( newBase ) {
  828.         newBase->WriteBits( newValue, 16 );
  829.     }
  830.  
  831.     if ( !base ) {
  832.         writeDelta->WriteDeltaShortCounter( oldValue, newValue );
  833.         changed = true;
  834.     } else {
  835.         int baseValue = base->ReadBits( 16 );
  836.         if ( baseValue == newValue ) {
  837.             writeDelta->WriteBits( 0, 1 );
  838.         } else {
  839.             writeDelta->WriteBits( 1, 1 );
  840.             writeDelta->WriteDeltaShortCounter( oldValue, newValue );
  841.             changed = true;
  842.         }
  843.     }
  844. }
  845.  
  846. /*
  847. ================
  848. idBitMsgDelta::WriteDeltaLongCounter
  849. ================
  850. */
  851. void idBitMsgDelta::WriteDeltaLongCounter( int oldValue, int newValue ) {
  852.     if ( newBase ) {
  853.         newBase->WriteBits( newValue, 32 );
  854.     }
  855.  
  856.     if ( !base ) {
  857.         writeDelta->WriteDeltaLongCounter( oldValue, newValue );
  858.         changed = true;
  859.     } else {
  860.         int baseValue = base->ReadBits( 32 );
  861.         if ( baseValue == newValue ) {
  862.             writeDelta->WriteBits( 0, 1 );
  863.         } else {
  864.             writeDelta->WriteBits( 1, 1 );
  865.             writeDelta->WriteDeltaLongCounter( oldValue, newValue );
  866.             changed = true;
  867.         }
  868.     }
  869. }
  870.  
  871. /*
  872. ================
  873. idBitMsgDelta::ReadString
  874. ================
  875. */
  876. void idBitMsgDelta::ReadString( char *buffer, int bufferSize ) const {
  877.     if ( !base ) {
  878.         readDelta->ReadString( buffer, bufferSize );
  879.         changed = true;
  880.     } else {
  881.         char baseString[MAX_DATA_BUFFER];
  882.         base->ReadString( baseString, sizeof( baseString ) );
  883.         if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  884.             idStr::Copynz( buffer, baseString, bufferSize );
  885.         } else {
  886.             readDelta->ReadString( buffer, bufferSize );
  887.             changed = true;
  888.         }
  889.     }
  890.  
  891.     if ( newBase ) {
  892.         newBase->WriteString( buffer );
  893.     }
  894. }
  895.  
  896. /*
  897. ================
  898. idBitMsgDelta::ReadData
  899. ================
  900. */
  901. void idBitMsgDelta::ReadData( void *data, int length ) const {
  902.     if ( !base ) {
  903.         readDelta->ReadData( data, length );
  904.         changed = true;
  905.     } else {
  906.         char baseData[MAX_DATA_BUFFER];
  907.         assert( length < sizeof( baseData ) );
  908.         base->ReadData( baseData, length );
  909.         if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  910.             memcpy( data, baseData, length );
  911.         } else {
  912.             readDelta->ReadData( data, length );
  913.             changed = true;
  914.         }
  915.     }
  916.  
  917.     if ( newBase ) {
  918.         newBase->WriteData( data, length );
  919.     }
  920. }
  921.  
  922. /*
  923. ================
  924. idBitMsgDelta::ReadDict
  925. ================
  926. */
  927. void idBitMsgDelta::ReadDict( idDict &dict ) {
  928.     if ( !base ) {
  929.         readDelta->ReadDeltaDict( dict, NULL );
  930.         changed = true;
  931.     } else {
  932.         idDict baseDict;
  933.         base->ReadDeltaDict( baseDict, NULL );
  934.         if ( !readDelta ) {
  935.             dict = baseDict;
  936.         } else {
  937.             changed = readDelta->ReadDeltaDict( dict, &baseDict );
  938.         }
  939.     }
  940.  
  941.     if ( newBase ) {
  942.         newBase->WriteDeltaDict( dict, NULL );
  943.     }
  944. }
  945.  
  946. /*
  947. ================
  948. idBitMsgDelta::ReadDeltaByteCounter
  949. ================
  950. */
  951. int idBitMsgDelta::ReadDeltaByteCounter( int oldValue ) const {
  952.     int value;
  953.  
  954.     if ( !base ) {
  955.         value = readDelta->ReadDeltaByteCounter( oldValue );
  956.         changed = true;
  957.     } else {
  958.         int baseValue = base->ReadBits( 8 );
  959.         if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  960.             value = baseValue;
  961.         } else {
  962.             value = readDelta->ReadDeltaByteCounter( oldValue );
  963.             changed = true;
  964.         }
  965.     }
  966.  
  967.     if ( newBase ) {
  968.         newBase->WriteBits( value, 8 );
  969.     }
  970.     return value;
  971. }
  972.  
  973. /*
  974. ================
  975. idBitMsgDelta::ReadDeltaShortCounter
  976. ================
  977. */
  978. int idBitMsgDelta::ReadDeltaShortCounter( int oldValue ) const {
  979.     int value;
  980.  
  981.     if ( !base ) {
  982.         value = readDelta->ReadDeltaShortCounter( oldValue );
  983.         changed = true;
  984.     } else {
  985.         int baseValue = base->ReadBits( 16 );
  986.         if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  987.             value = baseValue;
  988.         } else {
  989.             value = readDelta->ReadDeltaShortCounter( oldValue );
  990.             changed = true;
  991.         }
  992.     }
  993.  
  994.     if ( newBase ) {
  995.         newBase->WriteBits( value, 16 );
  996.     }
  997.     return value;
  998. }
  999.  
  1000. /*
  1001. ================
  1002. idBitMsgDelta::ReadDeltaLongCounter
  1003. ================
  1004. */
  1005. int idBitMsgDelta::ReadDeltaLongCounter( int oldValue ) const {
  1006.     int value;
  1007.  
  1008.     if ( !base ) {
  1009.         value = readDelta->ReadDeltaLongCounter( oldValue );
  1010.         changed = true;
  1011.     } else {
  1012.         int baseValue = base->ReadBits( 32 );
  1013.         if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  1014.             value = baseValue;
  1015.         } else {
  1016.             value = readDelta->ReadDeltaLongCounter( oldValue );
  1017.             changed = true;
  1018.         }
  1019.     }
  1020.  
  1021.     if ( newBase ) {
  1022.         newBase->WriteBits( value, 32 );
  1023.     }
  1024.     return value;
  1025. }
  1026.  
  1027. /*
  1028. ===========================================================================
  1029. idMsgQueue
  1030. ===========================================================================
  1031. */
  1032.  
  1033. /*
  1034. ===============
  1035. idMsgQueue::idMsgQueue
  1036. ===============
  1037. */
  1038. idMsgQueue::idMsgQueue( void ) {
  1039.     Init( 0 );
  1040. }
  1041.  
  1042. /*
  1043. ===============
  1044. idMsgQueue::Init
  1045. ===============
  1046. */
  1047. void idMsgQueue::Init( int sequence ) {
  1048.     first = last = sequence;
  1049.     startIndex = endIndex = 0;
  1050. }
  1051.  
  1052. /*
  1053. ===============
  1054. idMsgQueue::Add
  1055. ===============
  1056. */
  1057. bool idMsgQueue::Add( const byte *data, const int size, bool sequencing ) {
  1058.     if ( GetSpaceLeft() < size + 8 ) {
  1059.         return false;
  1060.     }
  1061.  
  1062.     assert( size );
  1063.  
  1064.     WriteShort( size );
  1065.     if ( sequencing ) {
  1066.         WriteLong( last );
  1067.     }
  1068.     last++;
  1069.     WriteData( data, size );
  1070.     return true;
  1071. }
  1072.  
  1073. /*
  1074. ===============
  1075. idMsgQueue::Get
  1076. ===============
  1077. */
  1078. // RAVEN BEGIN
  1079. // rjohnson: added check for data overflow
  1080. bool idMsgQueue::Get( byte *data, int dataSize, int &size, bool sequencing ) {
  1081. // RAVEN END
  1082.     if ( sequencing ? ( first == last ) : ( startIndex == endIndex ) ) {
  1083.         size = 0;
  1084.         return false;
  1085.     }
  1086.     int sequence;
  1087.     size = ReadShort();
  1088. // RAVEN BEGIN
  1089. // rjohnson: added check for data overflow
  1090.     if ( data && size > dataSize ) {
  1091.         common->Error( "idMsgQueue::Get  buffer size of %d < get size of %d", dataSize, size );
  1092.     }
  1093. // RAVEN END
  1094.  
  1095.     if ( sequencing ) {
  1096.         sequence = ReadLong();
  1097.         assert( sequence == first );
  1098.     }
  1099.     ReadData( data, size );
  1100.     first++;
  1101.     return true;
  1102. }
  1103.  
  1104. /*
  1105. ===============
  1106. idMsgQueue::GetTotalSize
  1107. ===============
  1108. */
  1109. int idMsgQueue::GetTotalSize( void ) const {
  1110.     if ( startIndex <= endIndex ) {
  1111.         return ( endIndex - startIndex );
  1112.     } else {
  1113.         return ( sizeof( buffer ) - startIndex + endIndex );
  1114.     }
  1115. }
  1116.  
  1117. /*
  1118. ===============
  1119. idMsgQueue::GetSpaceLeft
  1120. ===============
  1121. */
  1122. int idMsgQueue::GetSpaceLeft( void ) const {
  1123.     if ( startIndex <= endIndex ) {
  1124.         return sizeof( buffer ) - ( endIndex - startIndex ) - 1;
  1125.     } else {
  1126.         return ( startIndex - endIndex ) - 1;
  1127.     }
  1128. }
  1129.  
  1130. /*
  1131. ===============
  1132. idMsgQueue::CopyToBuffer
  1133. ===============
  1134. */
  1135. void idMsgQueue::CopyToBuffer( byte *buf ) const {
  1136.     if ( startIndex <= endIndex ) {
  1137. // RAVEN BEGIN
  1138. // JSinger: Changed to call optimized memcpy
  1139.         SIMDProcessor->Memcpy( buf, buffer + startIndex, endIndex - startIndex );
  1140. // RAVEN END
  1141.     } else {
  1142. // RAVEN BEGIN
  1143. // JSinger: Changed to call optimized memcpy
  1144.         SIMDProcessor->Memcpy( buf, buffer + startIndex, sizeof( buffer ) - startIndex );
  1145.         SIMDProcessor->Memcpy( buf + sizeof( buffer ) - startIndex, buffer, endIndex );
  1146. // RAVEN END
  1147.     }
  1148. }
  1149.  
  1150. /*
  1151. ===============
  1152. idMsgQueue::WriteByte
  1153. ===============
  1154. */
  1155. void idMsgQueue::WriteByte( byte b ) {
  1156.     buffer[endIndex] = b;
  1157.     endIndex = ( endIndex + 1 ) & ( MAX_MSG_QUEUE_SIZE - 1 );
  1158. }
  1159.  
  1160. /*
  1161. ===============
  1162. idMsgQueue::ReadByte
  1163. ===============
  1164. */
  1165. byte idMsgQueue::ReadByte( void ) {
  1166.     byte b = buffer[startIndex];
  1167.     startIndex = ( startIndex + 1 ) & ( MAX_MSG_QUEUE_SIZE - 1 );
  1168.     return b;
  1169. }
  1170.  
  1171. /*
  1172. ===============
  1173. idMsgQueue::WriteShort
  1174. ===============
  1175. */
  1176. void idMsgQueue::WriteShort( int s ) {
  1177.     WriteByte( ( s >>  0 ) & 255 );
  1178.     WriteByte( ( s >>  8 ) & 255 );
  1179. }
  1180.  
  1181. /*
  1182. ===============
  1183. idMsgQueue::ReadShort
  1184. ===============
  1185. */
  1186. int idMsgQueue::ReadShort( void ) {
  1187. // RAVEN BEGIN
  1188. // ddynerman: removed side-effecting bitwise or
  1189.     byte l = ReadByte();
  1190.     byte h = ReadByte();
  1191.     return l | ( h << 8 );
  1192. // RAVEN LOW
  1193. }
  1194.  
  1195. /*
  1196. ===============
  1197. idMsgQueue::WriteLong
  1198. ===============
  1199. */
  1200. void idMsgQueue::WriteLong( int l ) {
  1201.     WriteByte( ( l >>  0 ) & 255 );
  1202.     WriteByte( ( l >>  8 ) & 255 );
  1203.     WriteByte( ( l >> 16 ) & 255 );
  1204.     WriteByte( ( l >> 24 ) & 255 );
  1205. }
  1206.  
  1207. /*
  1208. ===============
  1209. idMsgQueue::ReadLong
  1210. ===============
  1211. */
  1212. int idMsgQueue::ReadLong( void ) {
  1213. // RAVEN BEGIN
  1214. // ddynerman: removed side-effecting bitwise or
  1215.     byte ll = ReadByte();
  1216.     byte lh = ReadByte();
  1217.     byte hl = ReadByte();
  1218.     byte hh = ReadByte();
  1219.     return ll | ( lh << 8 ) | ( hl << 16 ) | ( hh << 24 );
  1220. // RAVEN END
  1221. }
  1222.  
  1223. /*
  1224. ===============
  1225. idMsgQueue::WriteData
  1226. ===============
  1227. */
  1228. void idMsgQueue::WriteData( const byte *data, const int size ) {
  1229.     for ( int i = 0; i < size; i++ ) {
  1230.         WriteByte( data[i] );
  1231.     }
  1232. }
  1233.  
  1234. /*
  1235. ===============
  1236. idMsgQueue::ReadData
  1237. ===============
  1238. */
  1239. void idMsgQueue::ReadData( byte *data, const int size ) {
  1240.     if ( data ) {
  1241.         for ( int i = 0; i < size; i++ ) {
  1242.             data[i] = ReadByte();
  1243.         }
  1244.     } else {
  1245.         for ( int i = 0; i < size; i++ ) {
  1246.             ReadByte();
  1247.         }
  1248.     }
  1249. }
  1250.  
  1251. /*
  1252. ===============
  1253. idMsgQueue::FlushTo
  1254. ===============
  1255. */
  1256. void idMsgQueue::FlushTo( idBitMsg &msg ) {
  1257.     msg.WriteShort( GetTotalSize() );
  1258.     assert( startIndex == 0 );
  1259.     msg.WriteData( buffer + startIndex, endIndex - startIndex );
  1260.     Init( 0 );
  1261. }
  1262.  
  1263. /*
  1264. ===============
  1265. idMsgQueue::ReadFrom
  1266. ===============
  1267. */
  1268. void idMsgQueue::ReadFrom( const idBitMsg &msg ) {
  1269.     Init( 0 );
  1270.     endIndex = msg.ReadShort();
  1271.     msg.ReadData( buffer, endIndex );
  1272. }
  1273.  
  1274. /*
  1275. ===============
  1276. idMsgQueue::Save
  1277. ===============
  1278. */
  1279. void idMsgQueue::Save( idFile *file ) const {
  1280.     file->WriteInt( first );
  1281.     file->WriteInt( last );
  1282.     file->WriteInt( startIndex );
  1283.     file->WriteInt( endIndex );
  1284.     file->Write( buffer + startIndex, endIndex - startIndex );
  1285. }
  1286. /*
  1287. ===============
  1288. idMsgQueue::Restore
  1289. ===============
  1290. */
  1291. void idMsgQueue::Restore( idFile *file ) {
  1292.     file->ReadInt( first );
  1293.     file->ReadInt( last );
  1294.     file->ReadInt( startIndex );
  1295.     file->ReadInt( endIndex );
  1296.     file->Read( buffer + startIndex, endIndex - startIndex );
  1297. }
  1298.