BitMsg.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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. #pragma hdrstop
  21. #include "precompiled.h"
  22. /*
  23. ================================================================================================
  24. idBitMsg
  25. ================================================================================================
  26. */
  27. /*
  28. ========================
  29. idBitMsg::CheckOverflow
  30. ========================
  31. */
  32. bool idBitMsg::CheckOverflow( int numBits ) {
  33. if ( numBits > GetRemainingWriteBits() ) {
  34. if ( !allowOverflow ) {
  35. idLib::FatalError( "idBitMsg: overflow without allowOverflow set; maxsize=%i size=%i numBits=%i numRemainingWriteBits=%i",
  36. GetMaxSize(), GetSize(), numBits, GetRemainingWriteBits() );
  37. }
  38. if ( numBits > ( maxSize << 3 ) ) {
  39. idLib::FatalError( "idBitMsg: %i bits is > full message size", numBits );
  40. }
  41. idLib::Printf( "idBitMsg: overflow\n" );
  42. BeginWriting();
  43. overflowed = true;
  44. return true;
  45. }
  46. return false;
  47. }
  48. /*
  49. ========================
  50. idBitMsg::GetByteSpace
  51. ========================
  52. */
  53. byte *idBitMsg::GetByteSpace( int length ) {
  54. byte *ptr;
  55. if ( !writeData ) {
  56. idLib::FatalError( "idBitMsg::GetByteSpace: cannot write to message" );
  57. }
  58. // round up to the next byte
  59. WriteByteAlign();
  60. // check for overflow
  61. CheckOverflow( length << 3 );
  62. ptr = writeData + curSize;
  63. curSize += length;
  64. return ptr;
  65. }
  66. #define NBM( x ) (uint64)( ( 1LL << x ) - 1 )
  67. static uint64 maskForNumBits64[33] = { NBM( 0x00 ), NBM( 0x01 ), NBM( 0x02 ), NBM( 0x03 ),
  68. NBM( 0x04 ), NBM( 0x05 ), NBM( 0x06 ), NBM( 0x07 ),
  69. NBM( 0x08 ), NBM( 0x09 ), NBM( 0x0A ), NBM( 0x0B ),
  70. NBM( 0x0C ), NBM( 0x0D ), NBM( 0x0E ), NBM( 0x0F ),
  71. NBM( 0x10 ), NBM( 0x11 ), NBM( 0x12 ), NBM( 0x13 ),
  72. NBM( 0x14 ), NBM( 0x15 ), NBM( 0x16 ), NBM( 0x17 ),
  73. NBM( 0x18 ), NBM( 0x19 ), NBM( 0x1A ), NBM( 0x1B ),
  74. NBM( 0x1C ), NBM( 0x1D ), NBM( 0x1E ), NBM( 0x1F ), 0xFFFFFFFF };
  75. /*
  76. ========================
  77. idBitMsg::WriteBits
  78. If the number of bits is negative a sign is included.
  79. ========================
  80. */
  81. void idBitMsg::WriteBits( int value, int numBits ) {
  82. if ( !writeData ) {
  83. idLib::FatalError( "idBitMsg::WriteBits: cannot write to message" );
  84. }
  85. // check if the number of bits is valid
  86. if ( numBits == 0 || numBits < -31 || numBits > 32 ) {
  87. idLib::FatalError( "idBitMsg::WriteBits: bad numBits %i", numBits );
  88. }
  89. // check for value overflows
  90. if ( numBits != 32 ) {
  91. if ( numBits > 0 ) {
  92. if ( value > ( 1 << numBits ) - 1 ) {
  93. idLib::FatalError( "idBitMsg::WriteBits: value overflow %d %d",
  94. value, numBits );
  95. } else if ( value < 0 ) {
  96. idLib::FatalError( "idBitMsg::WriteBits: value overflow %d %d",
  97. value, numBits );
  98. }
  99. } else {
  100. const unsigned shift = ( -1 - numBits );
  101. int r = 1 << shift;
  102. if ( value > r - 1 ) {
  103. idLib::FatalError( "idBitMsg::WriteBits: value overflow %d %d",
  104. value, numBits );
  105. } else if ( value < -r ) {
  106. idLib::FatalError( "idBitMsg::WriteBits: value overflow %d %d",
  107. value, numBits );
  108. }
  109. }
  110. }
  111. if ( numBits < 0 ) {
  112. numBits = -numBits;
  113. }
  114. // check for msg overflow
  115. if ( CheckOverflow( numBits ) ) {
  116. return;
  117. }
  118. // Merge value with possible previous leftover
  119. tempValue |= (((int64)value) & maskForNumBits64[numBits] ) << writeBit;
  120. writeBit += numBits;
  121. // Flush 8 bits (1 byte) at a time
  122. while ( writeBit >= 8 ) {
  123. writeData[curSize++] = tempValue & 255;
  124. tempValue >>= 8;
  125. writeBit -= 8;
  126. }
  127. // Write leftover now, in case this is the last WriteBits call
  128. if ( writeBit > 0 ) {
  129. writeData[curSize] = tempValue & 255;
  130. }
  131. }
  132. /*
  133. ========================
  134. idBitMsg::WriteString
  135. ========================
  136. */
  137. void idBitMsg::WriteString( const char * s, int maxLength, bool make7Bit ) {
  138. if ( !s ) {
  139. WriteData( "", 1 );
  140. } else {
  141. int i, l;
  142. byte *dataPtr;
  143. const byte *bytePtr;
  144. l = idStr::Length( s );
  145. if ( maxLength >= 0 && l >= maxLength ) {
  146. l = maxLength - 1;
  147. }
  148. dataPtr = GetByteSpace( l + 1 );
  149. bytePtr = reinterpret_cast< const byte * >( s );
  150. if ( make7Bit ) {
  151. for ( i = 0; i < l; i++ ) {
  152. if ( bytePtr[i] > 127 ) {
  153. dataPtr[i] = '.';
  154. } else {
  155. dataPtr[i] = bytePtr[i];
  156. }
  157. }
  158. } else {
  159. for ( i = 0; i < l; i++ ) {
  160. dataPtr[i] = bytePtr[i];
  161. }
  162. }
  163. dataPtr[i] = '\0';
  164. }
  165. }
  166. /*
  167. ========================
  168. idBitMsg::WriteData
  169. ========================
  170. */
  171. void idBitMsg::WriteData( const void *data, int length ) {
  172. memcpy( GetByteSpace( length ), data, length );
  173. }
  174. /*
  175. ========================
  176. idBitMsg::WriteNetadr
  177. ========================
  178. */
  179. void idBitMsg::WriteNetadr( const netadr_t adr ) {
  180. WriteData( adr.ip, 4 );
  181. WriteUShort( adr.port );
  182. WriteByte( adr.type );
  183. }
  184. /*
  185. ========================
  186. idBitMsg::WriteDeltaDict
  187. ========================
  188. */
  189. bool idBitMsg::WriteDeltaDict( const idDict &dict, const idDict *base ) {
  190. int i;
  191. const idKeyValue *kv, *basekv;
  192. bool changed = false;
  193. if ( base != NULL ) {
  194. for ( i = 0; i < dict.GetNumKeyVals(); i++ ) {
  195. kv = dict.GetKeyVal( i );
  196. basekv = base->FindKey( kv->GetKey() );
  197. if ( basekv == NULL || basekv->GetValue().Icmp( kv->GetValue() ) != 0 ) {
  198. WriteString( kv->GetKey() );
  199. WriteString( kv->GetValue() );
  200. changed = true;
  201. }
  202. }
  203. WriteString( "" );
  204. for ( i = 0; i < base->GetNumKeyVals(); i++ ) {
  205. basekv = base->GetKeyVal( i );
  206. kv = dict.FindKey( basekv->GetKey() );
  207. if ( kv == NULL ) {
  208. WriteString( basekv->GetKey() );
  209. changed = true;
  210. }
  211. }
  212. WriteString( "" );
  213. } else {
  214. for ( i = 0; i < dict.GetNumKeyVals(); i++ ) {
  215. kv = dict.GetKeyVal( i );
  216. WriteString( kv->GetKey() );
  217. WriteString( kv->GetValue() );
  218. changed = true;
  219. }
  220. WriteString( "" );
  221. WriteString( "" );
  222. }
  223. return changed;
  224. }
  225. /*
  226. ========================
  227. idBitMsg::ReadBits
  228. If the number of bits is negative a sign is included.
  229. ========================
  230. */
  231. int idBitMsg::ReadBits( int numBits ) const {
  232. int value;
  233. int valueBits;
  234. int get;
  235. int fraction;
  236. bool sgn;
  237. if ( !readData ) {
  238. idLib::FatalError( "idBitMsg::ReadBits: cannot read from message" );
  239. }
  240. // check if the number of bits is valid
  241. if ( numBits == 0 || numBits < -31 || numBits > 32 ) {
  242. idLib::FatalError( "idBitMsg::ReadBits: bad numBits %i", numBits );
  243. }
  244. value = 0;
  245. valueBits = 0;
  246. if ( numBits < 0 ) {
  247. numBits = -numBits;
  248. sgn = true;
  249. } else {
  250. sgn = false;
  251. }
  252. // check for overflow
  253. if ( numBits > GetRemainingReadBits() ) {
  254. return -1;
  255. }
  256. while ( valueBits < numBits ) {
  257. if ( readBit == 0 ) {
  258. readCount++;
  259. }
  260. get = 8 - readBit;
  261. if ( get > (numBits - valueBits) ) {
  262. get = (numBits - valueBits);
  263. }
  264. fraction = readData[readCount - 1];
  265. fraction >>= readBit;
  266. fraction &= ( 1 << get ) - 1;
  267. value |= fraction << valueBits;
  268. valueBits += get;
  269. readBit = ( readBit + get ) & 7;
  270. }
  271. if ( sgn ) {
  272. if ( value & ( 1 << ( numBits - 1 ) ) ) {
  273. value |= -1 ^ ( ( 1 << numBits ) - 1 );
  274. }
  275. }
  276. return value;
  277. }
  278. /*
  279. ========================
  280. idBitMsg::ReadString
  281. ========================
  282. */
  283. int idBitMsg::ReadString( char * buffer, int bufferSize ) const {
  284. int l, c;
  285. ReadByteAlign();
  286. l = 0;
  287. while( 1 ) {
  288. c = ReadByte();
  289. if ( c <= 0 || c >= 255 ) {
  290. break;
  291. }
  292. // translate all fmt spec to avoid crash bugs in string routines
  293. if ( c == '%' ) {
  294. c = '.';
  295. }
  296. // we will read past any excessively long string, so
  297. // the following data can be read, but the string will
  298. // be truncated
  299. if ( l < bufferSize - 1 ) {
  300. buffer[l] = c;
  301. l++;
  302. }
  303. }
  304. buffer[l] = 0;
  305. return l;
  306. }
  307. /*
  308. ========================
  309. idBitMsg::ReadString
  310. ========================
  311. */
  312. int idBitMsg::ReadString( idStr & str ) const {
  313. ReadByteAlign();
  314. int cnt = 0;
  315. for ( int i = readCount; i < curSize; i++ ) {
  316. if ( readData[i] == 0 ) {
  317. break;
  318. }
  319. cnt++;
  320. }
  321. str.Clear();
  322. str.Append( (const char *)readData + readCount, cnt );
  323. readCount += cnt + 1;
  324. return str.Length();
  325. }
  326. /*
  327. ========================
  328. idBitMsg::ReadData
  329. ========================
  330. */
  331. int idBitMsg::ReadData( void *data, int length ) const {
  332. int cnt;
  333. ReadByteAlign();
  334. cnt = readCount;
  335. if ( readCount + length > curSize ) {
  336. if ( data ) {
  337. memcpy( data, readData + readCount, GetRemainingData() );
  338. }
  339. readCount = curSize;
  340. } else {
  341. if ( data ) {
  342. memcpy( data, readData + readCount, length );
  343. }
  344. readCount += length;
  345. }
  346. return ( readCount - cnt );
  347. }
  348. /*
  349. ========================
  350. idBitMsg::ReadNetadr
  351. ========================
  352. */
  353. void idBitMsg::ReadNetadr( netadr_t *adr ) const {
  354. ReadData( adr->ip, 4 );
  355. adr->port = ReadUShort();
  356. adr->type = ( netadrtype_t ) ReadByte();
  357. }
  358. /*
  359. ========================
  360. idBitMsg::ReadDeltaDict
  361. ========================
  362. */
  363. bool idBitMsg::ReadDeltaDict( idDict &dict, const idDict *base ) const {
  364. char key[MAX_STRING_CHARS];
  365. char value[MAX_STRING_CHARS];
  366. bool changed = false;
  367. if ( base != NULL ) {
  368. dict = *base;
  369. } else {
  370. dict.Clear();
  371. }
  372. while( ReadString( key, sizeof( key ) ) != 0 ) {
  373. ReadString( value, sizeof( value ) );
  374. dict.Set( key, value );
  375. changed = true;
  376. }
  377. while( ReadString( key, sizeof( key ) ) != 0 ) {
  378. dict.Delete( key );
  379. changed = true;
  380. }
  381. return changed;
  382. }
  383. /*
  384. ========================
  385. idBitMsg::DirToBits
  386. ========================
  387. */
  388. int idBitMsg::DirToBits( const idVec3 &dir, int numBits ) {
  389. int max, bits;
  390. float bias;
  391. assert( numBits >= 6 && numBits <= 32 );
  392. assert( dir.LengthSqr() - 1.0f < 0.01f );
  393. numBits /= 3;
  394. max = ( 1 << ( numBits - 1 ) ) - 1;
  395. bias = 0.5f / max;
  396. bits = IEEE_FLT_SIGNBITSET( dir.x ) << ( numBits * 3 - 1 );
  397. bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.x ) + bias ) * max ) ) << ( numBits * 2 );
  398. bits |= IEEE_FLT_SIGNBITSET( dir.y ) << ( numBits * 2 - 1 );
  399. bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.y ) + bias ) * max ) ) << ( numBits * 1 );
  400. bits |= IEEE_FLT_SIGNBITSET( dir.z ) << ( numBits * 1 - 1 );
  401. bits |= ( idMath::Ftoi( ( idMath::Fabs( dir.z ) + bias ) * max ) ) << ( numBits * 0 );
  402. return bits;
  403. }
  404. /*
  405. ========================
  406. idBitMsg::BitsToDir
  407. ========================
  408. */
  409. idVec3 idBitMsg::BitsToDir( int bits, int numBits ) {
  410. static float sign[2] = { 1.0f, -1.0f };
  411. int max;
  412. float invMax;
  413. idVec3 dir;
  414. assert( numBits >= 6 && numBits <= 32 );
  415. numBits /= 3;
  416. max = ( 1 << ( numBits - 1 ) ) - 1;
  417. invMax = 1.0f / max;
  418. dir.x = sign[( bits >> ( numBits * 3 - 1 ) ) & 1] * ( ( bits >> ( numBits * 2 ) ) & max )
  419. * invMax;
  420. dir.y = sign[( bits >> ( numBits * 2 - 1 ) ) & 1] * ( ( bits >> ( numBits * 1 ) ) & max )
  421. * invMax;
  422. dir.z = sign[( bits >> ( numBits * 1 - 1 ) ) & 1] * ( ( bits >> ( numBits * 0 ) ) & max )
  423. * invMax;
  424. dir.NormalizeFast();
  425. return dir;
  426. }