BitMsg.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  17. 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.
  18. ===========================================================================
  19. */
  20. #include "precompiled.h"
  21. #pragma hdrstop
  22. /*
  23. ==============================================================================
  24. idBitMsg
  25. ==============================================================================
  26. */
  27. /*
  28. ================
  29. idBitMsg::idBitMsg
  30. ================
  31. */
  32. idBitMsg::idBitMsg() {
  33. writeData = NULL;
  34. readData = NULL;
  35. maxSize = 0;
  36. curSize = 0;
  37. writeBit = 0;
  38. readCount = 0;
  39. readBit = 0;
  40. allowOverflow = false;
  41. overflowed = false;
  42. }
  43. /*
  44. ================
  45. idBitMsg::CheckOverflow
  46. ================
  47. */
  48. bool idBitMsg::CheckOverflow( int numBits ) {
  49. assert( numBits >= 0 );
  50. if ( numBits > GetRemainingWriteBits() ) {
  51. if ( !allowOverflow ) {
  52. idLib::common->FatalError( "idBitMsg: overflow without allowOverflow set" );
  53. }
  54. if ( numBits > ( maxSize << 3 ) ) {
  55. idLib::common->FatalError( "idBitMsg: %i bits is > full message size", numBits );
  56. }
  57. idLib::common->Printf( "idBitMsg: overflow\n" );
  58. BeginWriting();
  59. overflowed = true;
  60. return true;
  61. }
  62. return false;
  63. }
  64. /*
  65. ================
  66. idBitMsg::GetByteSpace
  67. ================
  68. */
  69. byte *idBitMsg::GetByteSpace( int length ) {
  70. byte *ptr;
  71. if ( !writeData ) {
  72. idLib::common->FatalError( "idBitMsg::GetByteSpace: cannot write to message" );
  73. }
  74. // round up to the next byte
  75. WriteByteAlign();
  76. // check for overflow
  77. CheckOverflow( length << 3 );
  78. ptr = writeData + curSize;
  79. curSize += length;
  80. return ptr;
  81. }
  82. /*
  83. ================
  84. idBitMsg::WriteBits
  85. If the number of bits is negative a sign is included.
  86. ================
  87. */
  88. void idBitMsg::WriteBits( int value, int numBits ) {
  89. int put;
  90. int fraction;
  91. if ( !writeData ) {
  92. idLib::common->Error( "idBitMsg::WriteBits: cannot write to message" );
  93. }
  94. // check if the number of bits is valid
  95. if ( numBits == 0 || numBits < -31 || numBits > 32 ) {
  96. idLib::common->Error( "idBitMsg::WriteBits: bad numBits %i", numBits );
  97. }
  98. // check for value overflows
  99. // this should be an error really, as it can go unnoticed and cause either bandwidth or corrupted data transmitted
  100. if ( numBits != 32 ) {
  101. if ( numBits > 0 ) {
  102. if ( value > ( 1 << numBits ) - 1 ) {
  103. idLib::common->Warning( "idBitMsg::WriteBits: value overflow %d %d", value, numBits );
  104. } else if ( value < 0 ) {
  105. idLib::common->Warning( "idBitMsg::WriteBits: value overflow %d %d", value, numBits );
  106. }
  107. } else {
  108. int r = 1 << ( - 1 - numBits );
  109. if ( value > r - 1 ) {
  110. idLib::common->Warning( "idBitMsg::WriteBits: value overflow %d %d", value, numBits );
  111. } else if ( value < -r ) {
  112. idLib::common->Warning( "idBitMsg::WriteBits: value overflow %d %d", value, numBits );
  113. }
  114. }
  115. }
  116. if ( numBits < 0 ) {
  117. numBits = -numBits;
  118. }
  119. // check for msg overflow
  120. if ( CheckOverflow( numBits ) ) {
  121. return;
  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. idBitMsg::WriteString
  143. ================
  144. */
  145. void idBitMsg::WriteString( const char *s, int maxLength, bool make7Bit ) {
  146. if ( !s ) {
  147. WriteData( "", 1 );
  148. } else {
  149. int i, l;
  150. byte *dataPtr;
  151. const byte *bytePtr;
  152. l = idStr::Length( s );
  153. if ( maxLength >= 0 && l >= maxLength ) {
  154. l = maxLength - 1;
  155. }
  156. dataPtr = GetByteSpace( l + 1 );
  157. bytePtr = reinterpret_cast<const byte *>(s);
  158. if ( make7Bit ) {
  159. for ( i = 0; i < l; i++ ) {
  160. if ( bytePtr[i] > 127 ) {
  161. dataPtr[i] = '.';
  162. } else {
  163. dataPtr[i] = bytePtr[i];
  164. }
  165. }
  166. } else {
  167. for ( i = 0; i < l; i++ ) {
  168. dataPtr[i] = bytePtr[i];
  169. }
  170. }
  171. dataPtr[i] = '\0';
  172. }
  173. }
  174. /*
  175. ================
  176. idBitMsg::WriteData
  177. ================
  178. */
  179. void idBitMsg::WriteData( const void *data, int length ) {
  180. memcpy( GetByteSpace( length ), data, length );
  181. }
  182. /*
  183. ================
  184. idBitMsg::WriteNetadr
  185. ================
  186. */
  187. void idBitMsg::WriteNetadr( const netadr_t adr ) {
  188. byte *dataPtr;
  189. dataPtr = GetByteSpace( 4 );
  190. memcpy( dataPtr, adr.ip, 4 );
  191. WriteUShort( adr.port );
  192. }
  193. /*
  194. ================
  195. idBitMsg::WriteDelta
  196. ================
  197. */
  198. void idBitMsg::WriteDelta( int oldValue, int newValue, int numBits ) {
  199. if ( oldValue == newValue ) {
  200. WriteBits( 0, 1 );
  201. return;
  202. }
  203. WriteBits( 1, 1 );
  204. WriteBits( newValue, numBits );
  205. }
  206. /*
  207. ================
  208. idBitMsg::WriteDeltaByteCounter
  209. ================
  210. */
  211. void idBitMsg::WriteDeltaByteCounter( int oldValue, int newValue ) {
  212. int i, x;
  213. x = oldValue ^ newValue;
  214. for ( i = 7; i > 0; i-- ) {
  215. if ( x & ( 1 << i ) ) {
  216. i++;
  217. break;
  218. }
  219. }
  220. WriteBits( i, 3 );
  221. if ( i ) {
  222. WriteBits( ( ( 1 << i ) - 1 ) & newValue, i );
  223. }
  224. }
  225. /*
  226. ================
  227. idBitMsg::WriteDeltaShortCounter
  228. ================
  229. */
  230. void idBitMsg::WriteDeltaShortCounter( int oldValue, int newValue ) {
  231. int i, x;
  232. x = oldValue ^ newValue;
  233. for ( i = 15; i > 0; i-- ) {
  234. if ( x & ( 1 << i ) ) {
  235. i++;
  236. break;
  237. }
  238. }
  239. WriteBits( i, 4 );
  240. if ( i ) {
  241. WriteBits( ( ( 1 << i ) - 1 ) & newValue, i );
  242. }
  243. }
  244. /*
  245. ================
  246. idBitMsg::WriteDeltaLongCounter
  247. ================
  248. */
  249. void idBitMsg::WriteDeltaLongCounter( int oldValue, int newValue ) {
  250. int i, x;
  251. x = oldValue ^ newValue;
  252. for ( i = 31; i > 0; i-- ) {
  253. if ( x & ( 1 << i ) ) {
  254. i++;
  255. break;
  256. }
  257. }
  258. WriteBits( i, 5 );
  259. if ( i ) {
  260. WriteBits( ( ( 1 << i ) - 1 ) & newValue, i );
  261. }
  262. }
  263. /*
  264. ==================
  265. idBitMsg::WriteDeltaDict
  266. ==================
  267. */
  268. bool idBitMsg::WriteDeltaDict( const idDict &dict, const idDict *base ) {
  269. int i;
  270. const idKeyValue *kv, *basekv;
  271. bool changed = false;
  272. if ( base != NULL ) {
  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. WriteString( "" );
  283. for ( i = 0; i < base->GetNumKeyVals(); i++ ) {
  284. basekv = base->GetKeyVal( i );
  285. kv = dict.FindKey( basekv->GetKey() );
  286. if ( kv == NULL ) {
  287. WriteString( basekv->GetKey() );
  288. changed = true;
  289. }
  290. }
  291. WriteString( "" );
  292. } else {
  293. for ( i = 0; i < dict.GetNumKeyVals(); i++ ) {
  294. kv = dict.GetKeyVal( i );
  295. WriteString( kv->GetKey() );
  296. WriteString( kv->GetValue() );
  297. changed = true;
  298. }
  299. WriteString( "" );
  300. WriteString( "" );
  301. }
  302. return changed;
  303. }
  304. /*
  305. ================
  306. idBitMsg::ReadBits
  307. If the number of bits is negative a sign is included.
  308. ================
  309. */
  310. int idBitMsg::ReadBits( int numBits ) const {
  311. int value;
  312. int valueBits;
  313. int get;
  314. int fraction;
  315. bool sgn;
  316. if ( !readData ) {
  317. idLib::common->FatalError( "idBitMsg::ReadBits: cannot read from message" );
  318. }
  319. // check if the number of bits is valid
  320. if ( numBits == 0 || numBits < -31 || numBits > 32 ) {
  321. idLib::common->FatalError( "idBitMsg::ReadBits: bad numBits %i", numBits );
  322. }
  323. value = 0;
  324. valueBits = 0;
  325. if ( numBits < 0 ) {
  326. numBits = -numBits;
  327. sgn = true;
  328. } else {
  329. sgn = false;
  330. }
  331. // check for overflow
  332. if ( numBits > GetRemainingReadBits() ) {
  333. return -1;
  334. }
  335. while ( valueBits < numBits ) {
  336. if ( readBit == 0 ) {
  337. readCount++;
  338. }
  339. get = 8 - readBit;
  340. if ( get > (numBits - valueBits) ) {
  341. get = numBits - valueBits;
  342. }
  343. fraction = readData[readCount - 1];
  344. fraction >>= readBit;
  345. fraction &= ( 1 << get ) - 1;
  346. value |= fraction << valueBits;
  347. valueBits += get;
  348. readBit = ( readBit + get ) & 7;
  349. }
  350. if ( sgn ) {
  351. if ( value & ( 1 << ( numBits - 1 ) ) ) {
  352. value |= -1 ^ ( ( 1 << numBits ) - 1 );
  353. }
  354. }
  355. return value;
  356. }
  357. /*
  358. ================
  359. idBitMsg::ReadString
  360. ================
  361. */
  362. int idBitMsg::ReadString( char *buffer, int bufferSize ) const {
  363. int l, c;
  364. ReadByteAlign();
  365. l = 0;
  366. while( 1 ) {
  367. c = ReadByte();
  368. if ( c <= 0 || c >= 255 ) {
  369. break;
  370. }
  371. // translate all fmt spec to avoid crash bugs in string routines
  372. if ( c == '%' ) {
  373. c = '.';
  374. }
  375. // we will read past any excessively long string, so
  376. // the following data can be read, but the string will
  377. // be truncated
  378. if ( l < bufferSize - 1 ) {
  379. buffer[l] = c;
  380. l++;
  381. }
  382. }
  383. buffer[l] = 0;
  384. return l;
  385. }
  386. /*
  387. ================
  388. idBitMsg::ReadData
  389. ================
  390. */
  391. int idBitMsg::ReadData( void *data, int length ) const {
  392. int cnt;
  393. ReadByteAlign();
  394. cnt = readCount;
  395. if ( readCount + length > curSize ) {
  396. if ( data ) {
  397. memcpy( data, readData + readCount, GetRemaingData() );
  398. }
  399. readCount = curSize;
  400. } else {
  401. if ( data ) {
  402. memcpy( data, readData + readCount, length );
  403. }
  404. readCount += length;
  405. }
  406. return ( readCount - cnt );
  407. }
  408. /*
  409. ================
  410. idBitMsg::ReadNetadr
  411. ================
  412. */
  413. void idBitMsg::ReadNetadr( netadr_t *adr ) const {
  414. int i;
  415. adr->type = NA_IP;
  416. for ( i = 0; i < 4; i++ ) {
  417. adr->ip[ i ] = ReadByte();
  418. }
  419. adr->port = ReadUShort();
  420. }
  421. /*
  422. ================
  423. idBitMsg::ReadDelta
  424. ================
  425. */
  426. int idBitMsg::ReadDelta( int oldValue, int numBits ) const {
  427. if ( ReadBits( 1 ) ) {
  428. return ReadBits( numBits );
  429. }
  430. return oldValue;
  431. }
  432. /*
  433. ================
  434. idBitMsg::ReadDeltaByteCounter
  435. ================
  436. */
  437. int idBitMsg::ReadDeltaByteCounter( int oldValue ) const {
  438. int i, newValue;
  439. i = ReadBits( 3 );
  440. if ( !i ) {
  441. return oldValue;
  442. }
  443. newValue = ReadBits( i );
  444. return ( oldValue & ~( ( 1 << i ) - 1 ) | newValue );
  445. }
  446. /*
  447. ================
  448. idBitMsg::ReadDeltaShortCounter
  449. ================
  450. */
  451. int idBitMsg::ReadDeltaShortCounter( int oldValue ) const {
  452. int i, newValue;
  453. i = ReadBits( 4 );
  454. if ( !i ) {
  455. return oldValue;
  456. }
  457. newValue = ReadBits( i );
  458. return ( oldValue & ~( ( 1 << i ) - 1 ) | newValue );
  459. }
  460. /*
  461. ================
  462. idBitMsg::ReadDeltaLongCounter
  463. ================
  464. */
  465. int idBitMsg::ReadDeltaLongCounter( int oldValue ) const {
  466. int i, newValue;
  467. i = ReadBits( 5 );
  468. if ( !i ) {
  469. return oldValue;
  470. }
  471. newValue = ReadBits( i );
  472. return ( oldValue & ~( ( 1 << i ) - 1 ) | newValue );
  473. }
  474. /*
  475. ==================
  476. idBitMsg::ReadDeltaDict
  477. ==================
  478. */
  479. bool idBitMsg::ReadDeltaDict( idDict &dict, const idDict *base ) const {
  480. char key[MAX_STRING_CHARS];
  481. char value[MAX_STRING_CHARS];
  482. bool changed = false;
  483. if ( base != NULL ) {
  484. dict = *base;
  485. } else {
  486. dict.Clear();
  487. }
  488. while( ReadString( key, sizeof( key ) ) != 0 ) {
  489. ReadString( value, sizeof( value ) );
  490. dict.Set( key, value );
  491. changed = true;
  492. }
  493. while( ReadString( key, sizeof( key ) ) != 0 ) {
  494. dict.Delete( key );
  495. changed = true;
  496. }
  497. return changed;
  498. }
  499. /*
  500. ================
  501. idBitMsg::DirToBits
  502. ================
  503. */
  504. int idBitMsg::DirToBits( const idVec3 &dir, int numBits ) {
  505. int max, bits;
  506. float bias;
  507. assert( numBits >= 6 && numBits <= 32 );
  508. assert( dir.LengthSqr() - 1.0f < 0.01f );
  509. numBits /= 3;
  510. max = ( 1 << ( numBits - 1 ) ) - 1;
  511. bias = 0.5f / max;
  512. bits = FLOATSIGNBITSET( dir.x ) << ( numBits * 3 - 1 );
  513. bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.x ) + bias ) * max ) ) << ( numBits * 2 );
  514. bits |= FLOATSIGNBITSET( dir.y ) << ( numBits * 2 - 1 );
  515. bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.y ) + bias ) * max ) ) << ( numBits * 1 );
  516. bits |= FLOATSIGNBITSET( dir.z ) << ( numBits * 1 - 1 );
  517. bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.z ) + bias ) * max ) ) << ( numBits * 0 );
  518. return bits;
  519. }
  520. /*
  521. ================
  522. idBitMsg::BitsToDir
  523. ================
  524. */
  525. idVec3 idBitMsg::BitsToDir( int bits, int numBits ) {
  526. static float sign[2] = { 1.0f, -1.0f };
  527. int max;
  528. float invMax;
  529. idVec3 dir;
  530. assert( numBits >= 6 && numBits <= 32 );
  531. numBits /= 3;
  532. max = ( 1 << ( numBits - 1 ) ) - 1;
  533. invMax = 1.0f / max;
  534. dir.x = sign[( bits >> ( numBits * 3 - 1 ) ) & 1] * ( ( bits >> ( numBits * 2 ) ) & max ) * invMax;
  535. dir.y = sign[( bits >> ( numBits * 2 - 1 ) ) & 1] * ( ( bits >> ( numBits * 1 ) ) & max ) * invMax;
  536. dir.z = sign[( bits >> ( numBits * 1 - 1 ) ) & 1] * ( ( bits >> ( numBits * 0 ) ) & max ) * invMax;
  537. dir.NormalizeFast();
  538. return dir;
  539. }
  540. /*
  541. ==============================================================================
  542. idBitMsgDelta
  543. ==============================================================================
  544. */
  545. const int MAX_DATA_BUFFER = 1024;
  546. /*
  547. ================
  548. idBitMsgDelta::WriteBits
  549. ================
  550. */
  551. void idBitMsgDelta::WriteBits( int value, int numBits ) {
  552. if ( newBase ) {
  553. newBase->WriteBits( value, numBits );
  554. }
  555. if ( !base ) {
  556. writeDelta->WriteBits( value, numBits );
  557. changed = true;
  558. } else {
  559. int baseValue = base->ReadBits( numBits );
  560. if ( baseValue == value ) {
  561. writeDelta->WriteBits( 0, 1 );
  562. } else {
  563. writeDelta->WriteBits( 1, 1 );
  564. writeDelta->WriteBits( value, numBits );
  565. changed = true;
  566. }
  567. }
  568. }
  569. /*
  570. ================
  571. idBitMsgDelta::WriteDelta
  572. ================
  573. */
  574. void idBitMsgDelta::WriteDelta( int oldValue, int newValue, int numBits ) {
  575. if ( newBase ) {
  576. newBase->WriteBits( newValue, numBits );
  577. }
  578. if ( !base ) {
  579. if ( oldValue == newValue ) {
  580. writeDelta->WriteBits( 0, 1 );
  581. } else {
  582. writeDelta->WriteBits( 1, 1 );
  583. writeDelta->WriteBits( newValue, numBits );
  584. }
  585. changed = true;
  586. } else {
  587. int baseValue = base->ReadBits( numBits );
  588. if ( baseValue == newValue ) {
  589. writeDelta->WriteBits( 0, 1 );
  590. } else {
  591. writeDelta->WriteBits( 1, 1 );
  592. if ( oldValue == newValue ) {
  593. writeDelta->WriteBits( 0, 1 );
  594. changed = true;
  595. } else {
  596. writeDelta->WriteBits( 1, 1 );
  597. writeDelta->WriteBits( newValue, numBits );
  598. changed = true;
  599. }
  600. }
  601. }
  602. }
  603. /*
  604. ================
  605. idBitMsgDelta::ReadBits
  606. ================
  607. */
  608. int idBitMsgDelta::ReadBits( int numBits ) const {
  609. int value;
  610. if ( !base ) {
  611. value = readDelta->ReadBits( numBits );
  612. changed = true;
  613. } else {
  614. int baseValue = base->ReadBits( numBits );
  615. if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  616. value = baseValue;
  617. } else {
  618. value = readDelta->ReadBits( numBits );
  619. changed = true;
  620. }
  621. }
  622. if ( newBase ) {
  623. newBase->WriteBits( value, numBits );
  624. }
  625. return value;
  626. }
  627. /*
  628. ================
  629. idBitMsgDelta::ReadDelta
  630. ================
  631. */
  632. int idBitMsgDelta::ReadDelta( int oldValue, int numBits ) const {
  633. int value;
  634. if ( !base ) {
  635. if ( readDelta->ReadBits( 1 ) == 0 ) {
  636. value = oldValue;
  637. } else {
  638. value = readDelta->ReadBits( numBits );
  639. }
  640. changed = true;
  641. } else {
  642. int baseValue = base->ReadBits( numBits );
  643. if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  644. value = baseValue;
  645. } else if ( readDelta->ReadBits( 1 ) == 0 ) {
  646. value = oldValue;
  647. changed = true;
  648. } else {
  649. value = readDelta->ReadBits( numBits );
  650. changed = true;
  651. }
  652. }
  653. if ( newBase ) {
  654. newBase->WriteBits( value, numBits );
  655. }
  656. return value;
  657. }
  658. /*
  659. ================
  660. idBitMsgDelta::WriteString
  661. ================
  662. */
  663. void idBitMsgDelta::WriteString( const char *s, int maxLength ) {
  664. if ( newBase ) {
  665. newBase->WriteString( s, maxLength );
  666. }
  667. if ( !base ) {
  668. writeDelta->WriteString( s, maxLength );
  669. changed = true;
  670. } else {
  671. char baseString[MAX_DATA_BUFFER];
  672. base->ReadString( baseString, sizeof( baseString ) );
  673. if ( idStr::Cmp( s, baseString ) == 0 ) {
  674. writeDelta->WriteBits( 0, 1 );
  675. } else {
  676. writeDelta->WriteBits( 1, 1 );
  677. writeDelta->WriteString( s, maxLength );
  678. changed = true;
  679. }
  680. }
  681. }
  682. /*
  683. ================
  684. idBitMsgDelta::WriteData
  685. ================
  686. */
  687. void idBitMsgDelta::WriteData( const void *data, int length ) {
  688. if ( newBase ) {
  689. newBase->WriteData( data, length );
  690. }
  691. if ( !base ) {
  692. writeDelta->WriteData( data, length );
  693. changed = true;
  694. } else {
  695. byte baseData[MAX_DATA_BUFFER];
  696. assert( length < sizeof( baseData ) );
  697. base->ReadData( baseData, length );
  698. if ( memcmp( data, baseData, length ) == 0 ) {
  699. writeDelta->WriteBits( 0, 1 );
  700. } else {
  701. writeDelta->WriteBits( 1, 1 );
  702. writeDelta->WriteData( data, length );
  703. changed = true;
  704. }
  705. }
  706. }
  707. /*
  708. ================
  709. idBitMsgDelta::WriteDict
  710. ================
  711. */
  712. void idBitMsgDelta::WriteDict( const idDict &dict ) {
  713. if ( newBase ) {
  714. newBase->WriteDeltaDict( dict, NULL );
  715. }
  716. if ( !base ) {
  717. writeDelta->WriteDeltaDict( dict, NULL );
  718. changed = true;
  719. } else {
  720. idDict baseDict;
  721. base->ReadDeltaDict( baseDict, NULL );
  722. changed = writeDelta->WriteDeltaDict( dict, &baseDict );
  723. }
  724. }
  725. /*
  726. ================
  727. idBitMsgDelta::WriteDeltaByteCounter
  728. ================
  729. */
  730. void idBitMsgDelta::WriteDeltaByteCounter( int oldValue, int newValue ) {
  731. if ( newBase ) {
  732. newBase->WriteBits( newValue, 8 );
  733. }
  734. if ( !base ) {
  735. writeDelta->WriteDeltaByteCounter( oldValue, newValue );
  736. changed = true;
  737. } else {
  738. int baseValue = base->ReadBits( 8 );
  739. if ( baseValue == newValue ) {
  740. writeDelta->WriteBits( 0, 1 );
  741. } else {
  742. writeDelta->WriteBits( 1, 1 );
  743. writeDelta->WriteDeltaByteCounter( oldValue, newValue );
  744. changed = true;
  745. }
  746. }
  747. }
  748. /*
  749. ================
  750. idBitMsgDelta::WriteDeltaShortCounter
  751. ================
  752. */
  753. void idBitMsgDelta::WriteDeltaShortCounter( int oldValue, int newValue ) {
  754. if ( newBase ) {
  755. newBase->WriteBits( newValue, 16 );
  756. }
  757. if ( !base ) {
  758. writeDelta->WriteDeltaShortCounter( oldValue, newValue );
  759. changed = true;
  760. } else {
  761. int baseValue = base->ReadBits( 16 );
  762. if ( baseValue == newValue ) {
  763. writeDelta->WriteBits( 0, 1 );
  764. } else {
  765. writeDelta->WriteBits( 1, 1 );
  766. writeDelta->WriteDeltaShortCounter( oldValue, newValue );
  767. changed = true;
  768. }
  769. }
  770. }
  771. /*
  772. ================
  773. idBitMsgDelta::WriteDeltaLongCounter
  774. ================
  775. */
  776. void idBitMsgDelta::WriteDeltaLongCounter( int oldValue, int newValue ) {
  777. if ( newBase ) {
  778. newBase->WriteBits( newValue, 32 );
  779. }
  780. if ( !base ) {
  781. writeDelta->WriteDeltaLongCounter( oldValue, newValue );
  782. changed = true;
  783. } else {
  784. int baseValue = base->ReadBits( 32 );
  785. if ( baseValue == newValue ) {
  786. writeDelta->WriteBits( 0, 1 );
  787. } else {
  788. writeDelta->WriteBits( 1, 1 );
  789. writeDelta->WriteDeltaLongCounter( oldValue, newValue );
  790. changed = true;
  791. }
  792. }
  793. }
  794. /*
  795. ================
  796. idBitMsgDelta::ReadString
  797. ================
  798. */
  799. void idBitMsgDelta::ReadString( char *buffer, int bufferSize ) const {
  800. if ( !base ) {
  801. readDelta->ReadString( buffer, bufferSize );
  802. changed = true;
  803. } else {
  804. char baseString[MAX_DATA_BUFFER];
  805. base->ReadString( baseString, sizeof( baseString ) );
  806. if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  807. idStr::Copynz( buffer, baseString, bufferSize );
  808. } else {
  809. readDelta->ReadString( buffer, bufferSize );
  810. changed = true;
  811. }
  812. }
  813. if ( newBase ) {
  814. newBase->WriteString( buffer );
  815. }
  816. }
  817. /*
  818. ================
  819. idBitMsgDelta::ReadData
  820. ================
  821. */
  822. void idBitMsgDelta::ReadData( void *data, int length ) const {
  823. if ( !base ) {
  824. readDelta->ReadData( data, length );
  825. changed = true;
  826. } else {
  827. char baseData[MAX_DATA_BUFFER];
  828. assert( length < sizeof( baseData ) );
  829. base->ReadData( baseData, length );
  830. if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  831. memcpy( data, baseData, length );
  832. } else {
  833. readDelta->ReadData( data, length );
  834. changed = true;
  835. }
  836. }
  837. if ( newBase ) {
  838. newBase->WriteData( data, length );
  839. }
  840. }
  841. /*
  842. ================
  843. idBitMsgDelta::ReadDict
  844. ================
  845. */
  846. void idBitMsgDelta::ReadDict( idDict &dict ) {
  847. if ( !base ) {
  848. readDelta->ReadDeltaDict( dict, NULL );
  849. changed = true;
  850. } else {
  851. idDict baseDict;
  852. base->ReadDeltaDict( baseDict, NULL );
  853. if ( !readDelta ) {
  854. dict = baseDict;
  855. } else {
  856. changed = readDelta->ReadDeltaDict( dict, &baseDict );
  857. }
  858. }
  859. if ( newBase ) {
  860. newBase->WriteDeltaDict( dict, NULL );
  861. }
  862. }
  863. /*
  864. ================
  865. idBitMsgDelta::ReadDeltaByteCounter
  866. ================
  867. */
  868. int idBitMsgDelta::ReadDeltaByteCounter( int oldValue ) const {
  869. int value;
  870. if ( !base ) {
  871. value = readDelta->ReadDeltaByteCounter( oldValue );
  872. changed = true;
  873. } else {
  874. int baseValue = base->ReadBits( 8 );
  875. if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  876. value = baseValue;
  877. } else {
  878. value = readDelta->ReadDeltaByteCounter( oldValue );
  879. changed = true;
  880. }
  881. }
  882. if ( newBase ) {
  883. newBase->WriteBits( value, 8 );
  884. }
  885. return value;
  886. }
  887. /*
  888. ================
  889. idBitMsgDelta::ReadDeltaShortCounter
  890. ================
  891. */
  892. int idBitMsgDelta::ReadDeltaShortCounter( int oldValue ) const {
  893. int value;
  894. if ( !base ) {
  895. value = readDelta->ReadDeltaShortCounter( oldValue );
  896. changed = true;
  897. } else {
  898. int baseValue = base->ReadBits( 16 );
  899. if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  900. value = baseValue;
  901. } else {
  902. value = readDelta->ReadDeltaShortCounter( oldValue );
  903. changed = true;
  904. }
  905. }
  906. if ( newBase ) {
  907. newBase->WriteBits( value, 16 );
  908. }
  909. return value;
  910. }
  911. /*
  912. ================
  913. idBitMsgDelta::ReadDeltaLongCounter
  914. ================
  915. */
  916. int idBitMsgDelta::ReadDeltaLongCounter( int oldValue ) const {
  917. int value;
  918. if ( !base ) {
  919. value = readDelta->ReadDeltaLongCounter( oldValue );
  920. changed = true;
  921. } else {
  922. int baseValue = base->ReadBits( 32 );
  923. if ( !readDelta || readDelta->ReadBits( 1 ) == 0 ) {
  924. value = baseValue;
  925. } else {
  926. value = readDelta->ReadDeltaLongCounter( oldValue );
  927. changed = true;
  928. }
  929. }
  930. if ( newBase ) {
  931. newBase->WriteBits( value, 32 );
  932. }
  933. return value;
  934. }