datasafe.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/Commando/datasafe.cpp $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 10/10/02 2:38p $*
  29. * *
  30. * $Revision:: 22 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  34. #include "always.h"
  35. #include "crandom.h"
  36. #include "datasafe.h"
  37. #include "vector.h"
  38. #include <malloc.h>
  39. #include <windows.h>
  40. #include "systimer.h"
  41. #include <stdio.h>
  42. /*
  43. ** Renegade specific includes. For reporting tampering.
  44. */
  45. #include "cstextobj.h"
  46. #include "cnetwork.h"
  47. #include "player.h"
  48. #include "playermanager.h"
  49. #include "devoptions.h"
  50. #include "string_ids.h"
  51. #include "translatedb.h"
  52. /*
  53. ** Unreferenced local variable.
  54. */
  55. #ifndef WWDEBUG
  56. #pragma warning(disable : 4101)
  57. #endif
  58. /*
  59. ** warning C4073: initializers put in library initialization area
  60. */
  61. //#pragma warning(disable : 4073)
  62. /*
  63. ** Put this data in the lib init area so it gets initialised first.
  64. */
  65. //#pragma init_seg(lib)
  66. /*
  67. ** Static data. See class definition for details.
  68. **
  69. ** I need to make this stuff static so that I can templatize the derived class and have all expansions use the same data.
  70. */
  71. unsigned long GenericDataSafeClass::SimpleKey;
  72. unsigned long GenericDataSafeClass::HandleKey;
  73. unsigned long GenericDataSafeClass::Checksum;
  74. unsigned long GenericDataSafeClass::ShuffleDelay;
  75. unsigned long GenericDataSafeClass::SecurityCheckDelay;
  76. DataSafeHandleClass GenericDataSafeClass::SentinelOne = 0;
  77. int GenericDataSafeClass::NumLists = 0;
  78. DataSafeEntryListClass *GenericDataSafeClass::Safe[MAX_DATASAFE_LISTS];
  79. DataSafeEntryTypeClass GenericDataSafeClass::TypeList[MAX_DATASAFE_TYPES];
  80. int GenericDataSafeClass::TypeListCount = 0;
  81. DataSafeHandleClass GenericDataSafeClass::SentinelTwo = 0;
  82. int GenericDataSafeClass::CRCErrors = 0;
  83. #ifdef THREAD_SAFE_DATA_SAFE
  84. HANDLE GenericDataSafeClass::SafeMutex;
  85. #else //THREAD_SAFE_DATA_SAFE
  86. unsigned int GenericDataSafeClass::PreferredThread = GetCurrentThreadId();
  87. #endif //THREAD_SAFE_DATA_SAFE
  88. #ifdef WWDEBUG
  89. unsigned long GenericDataSafeClass::LastDump = TIMEGETTIME();
  90. int GenericDataSafeClass::NumSwaps = 0;
  91. int GenericDataSafeClass::NumFetches = 0;
  92. int GenericDataSafeClass::SlopCount = 0;
  93. int GenericDataSafeClass::NumSecurityChecks = 0;
  94. int GenericDataSafeClass::NumShuffles = 0;
  95. #endif //WWDEBUG
  96. static DynamicVectorClass<int> random_list_contenders;
  97. /*
  98. ** Buffer to return a reference to in error cases.
  99. */
  100. char ErrorVal[1024] = {0,0,0,0};
  101. /*
  102. ** Instances of data safes. One for each type of data we will be storing in the safe.
  103. **
  104. ** For each type, we have to declare more than just the class, we have to declare static data too. This is taken care of by
  105. ** the DECLARE_DATA_SAFE macro.
  106. **
  107. ** All data safe type MUST be declared here so that they are constructed after the ::TypeList and ::TypeListCount statics above.
  108. **
  109. ** A data safe of type 'int' must always be declared first.
  110. **
  111. */
  112. typedef unsigned int DATASAFE_UNSIGNED_INT;
  113. DECLARE_DATA_SAFE(int);
  114. DECLARE_DATA_SAFE(DATASAFE_UNSIGNED_INT);
  115. DECLARE_DATA_SAFE(float);
  116. DECLARE_DATA_SAFE(double);
  117. /*
  118. ** Define this to set the keys to a fixed value to aid debugging.
  119. */
  120. //#define FIXED_KEY
  121. /***********************************************************************************************
  122. * GenericDataSafeClass::GenericDataSafeClass -- Class constructor. *
  123. * *
  124. * *
  125. * *
  126. * INPUT: Nothing *
  127. * *
  128. * OUTPUT: Nothing *
  129. * *
  130. * WARNINGS: None *
  131. * *
  132. * HISTORY: *
  133. * 7/3/2001 2:58PM ST : Created *
  134. *=============================================================================================*/
  135. GenericDataSafeClass::GenericDataSafeClass(void)
  136. {
  137. /*
  138. ** This constructor could be called lots of times as new types are added to the data safe. Make sure we don't get an
  139. ** unexpected reset if we have already added types.
  140. */
  141. if (TypeListCount == 0) {
  142. #ifdef THREAD_SAFE_DATA_SAFE
  143. SafeMutex = CreateMutex(NULL, false, NULL);
  144. #else
  145. PreferredThread = GetCurrentThreadId();
  146. #endif //THREAD_SAFE_DATA_SAFE
  147. #ifdef FIXED_KEY
  148. SimpleKey = 0x80000000;
  149. HandleKey = 0x40000000;
  150. #else //FIXED_KEY
  151. SimpleKey = 0x55555555 ^ TIMEGETTIME() ^ FreeRandom.Get_Int(0, 0xffffffff);
  152. HandleKey = 0xaaaaaaaa ^ (TIMEGETTIME()*2) ^ FreeRandom.Get_Int(0, 0xffffffff);
  153. #endif //FIXED_KEY
  154. NumLists = 0;
  155. Checksum = ~SimpleKey;
  156. ShuffleDelay = TIMEGETTIME();
  157. SecurityCheckDelay = TIMEGETTIME();
  158. memset(&Safe[0], 0, sizeof(Safe));
  159. Reset();
  160. } else {
  161. if (TypeListCount == 1 && SentinelOne == 0 && SentinelTwo == 0) {
  162. /*
  163. ** Add sentinels if we already know about the 'int' type.
  164. */
  165. int s1 = SENTINEL_ONE;
  166. int s2 = SENTINEL_TWO;
  167. SentinelOne = DataSafeClass<int>::Add_Entry(s1);
  168. SentinelTwo = DataSafeClass<int>::Add_Entry(s2);
  169. }
  170. }
  171. }
  172. /***********************************************************************************************
  173. * GenericDataSafeClass::Reset_Timers -- Handle timer reset *
  174. * *
  175. * *
  176. * *
  177. * INPUT: Nothing *
  178. * *
  179. * OUTPUT: Nothing *
  180. * *
  181. * WARNINGS: None *
  182. * *
  183. * HISTORY: *
  184. * 1/31/2002 12:12PM ST : Created *
  185. *=============================================================================================*/
  186. void GenericDataSafeClass::Reset_Timers(void)
  187. {
  188. ShuffleDelay = TIMEGETTIME();
  189. SecurityCheckDelay = TIMEGETTIME();
  190. #ifdef WWDEBUG
  191. LastDump = TIMEGETTIME();
  192. #endif //WWDEBUG
  193. }
  194. /***********************************************************************************************
  195. * GenericDataSafeClass::Reset -- Reset the safe stats. *
  196. * *
  197. * *
  198. * *
  199. * INPUT: Nothing *
  200. * *
  201. * OUTPUT: Nothing *
  202. * *
  203. * WARNINGS: None *
  204. * *
  205. * HISTORY: *
  206. * 7/30/2001 11:38AM ST : Created *
  207. *=============================================================================================*/
  208. void GenericDataSafeClass::Reset(void)
  209. {
  210. CRCErrors = 0;
  211. #ifdef WWDEBUG
  212. LastDump = TIMEGETTIME();
  213. NumSwaps = 0;
  214. NumFetches = 0;
  215. SlopCount = 0;
  216. NumSecurityChecks = 0;
  217. NumShuffles = 0;
  218. #endif //WWDEBUG
  219. if (NumLists) {
  220. Shuffle();
  221. Security_Check();
  222. }
  223. }
  224. /***********************************************************************************************
  225. * GenericDataSafeClass::Shutdown -- Free any remaining memory if we are finished with the safe*
  226. * *
  227. * *
  228. * *
  229. * INPUT: Nothing *
  230. * *
  231. * OUTPUT: Nothing *
  232. * *
  233. * WARNINGS: None *
  234. * *
  235. * HISTORY: *
  236. * 7/6/2001 1:47PM ST : Created *
  237. *=============================================================================================*/
  238. void GenericDataSafeClass::Shutdown(void)
  239. {
  240. /*
  241. ** If there is nothing left in the data safe - not even slop - then free the list memory.
  242. */
  243. bool found = false;
  244. for (int i=0 ; i<NumLists ; i++) {
  245. ds_assert(Safe[i] != NULL);
  246. if (Safe[i] != NULL && Safe[i]->EntryCount != 0) {
  247. found = true;
  248. break;
  249. }
  250. }
  251. if (!found) {
  252. for (int i=0 ; i<NumLists ; i++) {
  253. delete Safe[i];
  254. }
  255. NumLists = 0;
  256. #ifdef THREAD_SAFE_DATA_SAFE
  257. CloseHandle(SafeMutex);
  258. #endif //THREAD_SAFE_DATA_SAFE
  259. }
  260. }
  261. /***********************************************************************************************
  262. * GenericDataSafeClass::Get_Entry -- Find a raw entry in the data safe *
  263. * *
  264. * *
  265. * *
  266. * INPUT: Handle *
  267. * *
  268. * OUTPUT: Pointer to raw entry (including header) or NULL if not found *
  269. * *
  270. * WARNINGS: None *
  271. * *
  272. * HISTORY: *
  273. * 5/13/2001 9:43PM ST : Created *
  274. *=============================================================================================*/
  275. DataSafeEntryClass *GenericDataSafeClass::Get_Entry(DataSafeHandleClass handle)
  276. {
  277. int list;
  278. #ifdef WWDEBUG
  279. NumFetches++;
  280. #endif //WWDEBUG
  281. /*
  282. ** Check safe integrity.
  283. */
  284. Security_Check();
  285. /*
  286. ** The handles we give out are encrypted so we need to decrypt temporarily to extract the list number.
  287. */
  288. DataSafeHandleClass new_handle = handle ^ HandleKey;
  289. list = new_handle.Handle.Part.List;
  290. /*
  291. ** Apply the current data key to the handle for matching purposes.
  292. */
  293. DataSafeHandleClass match_handle = new_handle ^ SimpleKey;
  294. ds_assert(list >= 0);
  295. ds_assert(list < NumLists);
  296. ds_assert(Safe[list] != NULL);
  297. /*
  298. ** Get the head of the list.
  299. */
  300. DataSafeEntryClass *entry_ptr = Safe[list]->SafeList;
  301. /*
  302. ** Search through until we find a matching handle. Check to make sure we don't get stuck in an infinite loop.
  303. */
  304. int times = 0;
  305. while (entry_ptr) {
  306. if (entry_ptr->Handle == match_handle) {
  307. return(entry_ptr);
  308. }
  309. entry_ptr = entry_ptr->Next;
  310. times++;
  311. ds_assert(times <= MAX_ENTRIES_PER_LIST);
  312. if (times > MAX_ENTRIES_PER_LIST) {
  313. entry_ptr = NULL;
  314. break;
  315. }
  316. }
  317. /*
  318. ** Falling through to here means no match found and we return NULL as an error.
  319. */
  320. WWDEBUG_SAY(("WARNING: Data Safe: No item found for handle %08x\n", (int)handle));
  321. ds_assert(entry_ptr != NULL);
  322. return(NULL);
  323. }
  324. /***********************************************************************************************
  325. * GenericDataSafeClass::Get_Entry -- Find a raw entry in the data safe *
  326. * *
  327. * *
  328. * *
  329. * INPUT: Handle *
  330. * *
  331. * OUTPUT: Type of object that handle represents *
  332. * *
  333. * WARNINGS: None *
  334. * *
  335. * HISTORY: *
  336. * 7/17/2001 12:44PM ST : Created *
  337. *=============================================================================================*/
  338. int GenericDataSafeClass::Get_Entry_Type(DataSafeHandleClass handle)
  339. {
  340. int list;
  341. /*
  342. ** Check safe integrity.
  343. */
  344. Security_Check();
  345. /*
  346. ** The handles we give out are encrypted so we need to decrypt temporarily to extract the list number.
  347. */
  348. DataSafeHandleClass new_handle = handle ^ HandleKey;
  349. list = new_handle.Handle.Part.List;
  350. ds_assert(list >= 0);
  351. ds_assert(list < NumLists);
  352. ds_assert(Safe[list] != NULL);
  353. /*
  354. ** Return the type of data stored in this list.
  355. */
  356. return(Safe[list]->EntryType);
  357. }
  358. /***********************************************************************************************
  359. * GenericDataSafeClass::Get_Entry_By_Index -- Find the nth entry in the given data safe list *
  360. * *
  361. * *
  362. * *
  363. * INPUT: List number *
  364. * Entry number *
  365. * *
  366. * OUTPUT: Pointer to entry or NULL if not found *
  367. * *
  368. * WARNINGS: None *
  369. * *
  370. * HISTORY: *
  371. * 5/13/2001 9:43PM ST : Created *
  372. *=============================================================================================*/
  373. DataSafeEntryClass *GenericDataSafeClass::Get_Entry_By_Index(int list, int index)
  374. {
  375. #ifdef WWDEBUG
  376. NumFetches++;
  377. #endif //WWDEBUG
  378. /*
  379. ** Locals.
  380. */
  381. int count = 0;
  382. /*
  383. ** Input ds_asserts.
  384. */
  385. ds_assert(list >= 0);
  386. ds_assert(list < NumLists);
  387. ds_assert(Safe[list] != NULL);
  388. /*
  389. ** Get the head of the list.
  390. */
  391. DataSafeEntryClass *entry_ptr = Safe[list]->SafeList;
  392. /*
  393. ** Iterate through the list, counting entries, until we find the requested entry number.
  394. */
  395. while (entry_ptr) {
  396. if (count == index) {
  397. return(entry_ptr);
  398. }
  399. entry_ptr = entry_ptr->Next;
  400. count++;
  401. ds_assert(count < MAX_ENTRIES_PER_LIST);
  402. }
  403. /*
  404. ** Falling through to here means no match found and we return NULL as an error.
  405. */
  406. WWDEBUG_SAY(("WARNING: Data Safe: No item found for list %d, index %d\n", list, index));
  407. ds_assert(entry_ptr != NULL);
  408. return(NULL);
  409. }
  410. /***********************************************************************************************
  411. * GenericDataSafeClass::Mem_Copy_Encrypt -- Copy and encrypt data in one pass. *
  412. * *
  413. * *
  414. * *
  415. * INPUT: Ptr to data dest *
  416. * Ptr to data source *
  417. * Length of data in bytes *
  418. * Should we update the checksum *
  419. * *
  420. * OUTPUT: Nothing *
  421. * *
  422. * WARNINGS: None *
  423. * *
  424. * HISTORY: *
  425. * 6/19/2001 9:29PM ST : Created *
  426. *=============================================================================================*/
  427. void GenericDataSafeClass::Mem_Copy_Encrypt(void *dest, void *src, int size, bool do_checksum)
  428. {
  429. ds_assert((size % 4) == 0);
  430. unsigned long temp;
  431. unsigned long *s = (unsigned long *) src;
  432. unsigned long *d = (unsigned long *) dest;
  433. if (do_checksum) {
  434. for (int i = 0 ; i < (size / 4) ; i++) {
  435. temp = *s++;
  436. temp = temp ^ SimpleKey;
  437. Checksum = Checksum ^ temp;
  438. *d++ = temp;
  439. }
  440. } else {
  441. for (int i = 0 ; i < (size / 4) ; i++) {
  442. *d++ = *s++ ^ SimpleKey;
  443. }
  444. }
  445. }
  446. /***********************************************************************************************
  447. * GenericDataSafeClass::Mem_Copy_Decrypt -- Copy and decrypt data in one pass. *
  448. * *
  449. * *
  450. * *
  451. * INPUT: Ptr to data dest *
  452. * Ptr to data source *
  453. * Length of data in bytes *
  454. * Should we update the checksum *
  455. * *
  456. * OUTPUT: Nothing *
  457. * *
  458. * WARNINGS: None *
  459. * *
  460. * HISTORY: *
  461. * 6/19/2001 9:29PM ST : Created *
  462. *=============================================================================================*/
  463. void GenericDataSafeClass::Mem_Copy_Decrypt(void *dest, void *src, int size, bool do_checksum)
  464. {
  465. ds_assert((size % 4) == 0);
  466. unsigned long temp;
  467. unsigned long *s = (unsigned long *) src;
  468. unsigned long *d = (unsigned long *) dest;
  469. if (do_checksum) {
  470. for (int i = 0 ; i < (size / 4) ; i++) {
  471. temp = *s++;
  472. Checksum = Checksum ^ temp;
  473. temp = temp ^ SimpleKey;
  474. *d++ = temp;
  475. }
  476. } else {
  477. for (int i = 0 ; i < (size / 4) ; i++) {
  478. *d++ = *s++ ^ SimpleKey;
  479. }
  480. }
  481. }
  482. /***********************************************************************************************
  483. * GenericDataSafeClass::Encrypt -- Encrypt data. *
  484. * *
  485. * *
  486. * *
  487. * INPUT: Ptr to data to encrypt *
  488. * Length of data in bytes *
  489. * Key to use *
  490. * Should we update the checksum *
  491. * *
  492. * OUTPUT: Nothing *
  493. * *
  494. * WARNINGS: None *
  495. * *
  496. * HISTORY: *
  497. * 6/19/2001 9:29PM ST : Created *
  498. *=============================================================================================*/
  499. void GenericDataSafeClass::Encrypt(void *data, int size, unsigned long key, bool do_checksum)
  500. {
  501. ds_assert((size % 4) == 0);
  502. unsigned long *data_ptr = (unsigned long*)data;
  503. if (do_checksum) {
  504. for (int i = 0 ; i < (size / 4) ; i++) {
  505. *data_ptr ^= key;
  506. Checksum ^= *data_ptr++;
  507. }
  508. } else {
  509. for (int i = 0 ; i < (size / 4) ; i++) {
  510. *data_ptr ^= key;
  511. }
  512. }
  513. }
  514. /***********************************************************************************************
  515. * GenericDataSafeClass::Decrypt -- Decrypt data. *
  516. * *
  517. * *
  518. * *
  519. * INPUT: Ptr to data to decrypt *
  520. * Length of data in bytes *
  521. * Key to use *
  522. * Should we update the checksum *
  523. * *
  524. * OUTPUT: Nothing *
  525. * *
  526. * WARNINGS: None *
  527. * *
  528. * HISTORY: *
  529. * 6/19/2001 9:29PM ST : Created *
  530. *=============================================================================================*/
  531. void GenericDataSafeClass::Decrypt(void *data, int size, unsigned long key, bool do_checksum)
  532. {
  533. ds_assert((size % 4) == 0);
  534. unsigned long *data_ptr = (unsigned long*)data;
  535. if (do_checksum) {
  536. for (int i = 0 ; i < (size / 4) ; i++) {
  537. Checksum ^= *data_ptr;
  538. *data_ptr++ ^= key;
  539. }
  540. } else {
  541. for (int i = 0 ; i < (size / 4) ; i++) {
  542. *data_ptr++ ^= key;
  543. }
  544. }
  545. }
  546. /***********************************************************************************************
  547. * GenericDataSafeClass::Get_Random_List_For_Insertion -- Get a random list to insert an item *
  548. * *
  549. * *
  550. * *
  551. * INPUT: Item type *
  552. * *
  553. * OUTPUT: List index *
  554. * *
  555. * WARNINGS: None *
  556. * *
  557. * HISTORY: *
  558. * 6/25/2001 12:07PM ST : Created *
  559. *=============================================================================================*/
  560. int GenericDataSafeClass::Get_Random_List_For_Insertion(int type)
  561. {
  562. ThreadLockClass locker;
  563. random_list_contenders.Reset_Active();
  564. /*
  565. ** Build a list of lists that contain the appropriate size data and have room for extra entries.
  566. */
  567. for (int i=0 ; i<NumLists ; i++) {
  568. ds_assert(Safe[i] != NULL);
  569. if (Safe[i] != NULL && (Safe[i]->EntryType == type || Safe[i]->EntryType == -1) && Safe[i]->EntryCount < MAX_ENTRIES_PER_LIST) {
  570. random_list_contenders.Add(i);
  571. }
  572. }
  573. /*
  574. ** If no lists are available then we need to create one.
  575. */
  576. if (random_list_contenders.Count() == 0) {
  577. int safe_index = Create_Safe_List(type);
  578. ds_assert(safe_index != -1);
  579. ds_assert(Safe[safe_index] != NULL);
  580. return(safe_index);
  581. }
  582. /*
  583. ** Choose a random list from the possible contenders.
  584. */
  585. int pick = 0;
  586. if (random_list_contenders.Count() > 1) {
  587. pick = FreeRandom.Get_Int(0, random_list_contenders.Count()-1);
  588. }
  589. ds_assert(pick >= 0 && pick < random_list_contenders.Count());
  590. /*
  591. ** If the list we picked is currently empty then we might need to do some fixup before we can use it.
  592. */
  593. int list_index = random_list_contenders[pick];
  594. ds_assert(Safe[list_index] != NULL);
  595. if (Safe[list_index]->EntryType == -1 || Safe[list_index]->SafeList == NULL) {
  596. Safe[list_index]->EntryType = type;
  597. Safe[list_index]->SlopCount = 0;
  598. }
  599. return(list_index);
  600. }
  601. /***********************************************************************************************
  602. * GenericDataSafeClass::Create_Safe_List -- Create a single data safe list of specified type *
  603. * *
  604. * *
  605. * *
  606. * INPUT: Type of list to create *
  607. * *
  608. * OUTPUT: Index of list. -1 if unable to create *
  609. * *
  610. * WARNINGS: None *
  611. * *
  612. * HISTORY: *
  613. * 6/25/2001 12:27PM ST : Created *
  614. *=============================================================================================*/
  615. int GenericDataSafeClass::Create_Safe_List(int type)
  616. {
  617. /*
  618. ** Create a new list entry.
  619. */
  620. DataSafeEntryListClass *safe_list = new DataSafeEntryListClass;
  621. ds_assert(safe_list->EntryCount == 0);
  622. ds_assert(safe_list->SafeList == NULL);
  623. safe_list->EntryType = type;
  624. safe_list->SlopCount = 0;
  625. /*
  626. ** Add it to the list of data safes.
  627. */
  628. ds_assert(NumLists < MAX_DATASAFE_LISTS);
  629. if (NumLists < MAX_DATASAFE_LISTS) {
  630. Safe[NumLists++] = safe_list;
  631. return(NumLists-1);
  632. }
  633. return(-1);
  634. }
  635. /***********************************************************************************************
  636. * GenericDataSafeClass::Random_Insertion -- Insert an entry at a random position in the list *
  637. * *
  638. * *
  639. * *
  640. * INPUT: Ptr to data entry *
  641. * Index of list to add to *
  642. * Type of data (size) *
  643. * *
  644. * OUTPUT: Nothing *
  645. * *
  646. * WARNINGS: None *
  647. * *
  648. * HISTORY: *
  649. * 6/25/2001 12:34PM ST : Created *
  650. *=============================================================================================*/
  651. void GenericDataSafeClass::Random_Insertion(DataSafeEntryClass *entry_ptr, int list, int type, bool is_slop)
  652. {
  653. ds_assert(Safe[list] != NULL);
  654. ds_assert(Safe[list]->EntryCount < MAX_ENTRIES_PER_LIST);
  655. ds_assert(Safe[list]->EntryType == type);
  656. /*
  657. ** If this is the first entry to be added to the list then there is no possibility of random insertion. Just stick it
  658. ** at the beginning.
  659. */
  660. if (Safe[list]->EntryCount == 0) {
  661. entry_ptr->Next = entry_ptr;
  662. entry_ptr->Prev = entry_ptr;
  663. Safe[list]->SafeList = entry_ptr;
  664. Safe[list]->EntryCount++;
  665. } else {
  666. /*
  667. ** Pick a random insertion point.
  668. */
  669. int pick = 0;
  670. if (Safe[list]->EntryCount > 1) {
  671. pick = FreeRandom.Get_Int(0, Safe[list]->EntryCount-1);
  672. }
  673. /*
  674. ** Find the item to insert ahead of.
  675. */
  676. DataSafeEntryClass *walker = Safe[list]->SafeList;
  677. int skip = pick;
  678. while (skip) {
  679. walker = walker->Next;
  680. skip--;
  681. }
  682. /*
  683. ** Fix up the list pointers to insert.
  684. */
  685. entry_ptr->Prev = walker->Prev;
  686. walker->Prev->Next = entry_ptr;
  687. entry_ptr->Next = walker;
  688. walker->Prev = entry_ptr;
  689. if (pick == 0) {
  690. Safe[list]->SafeList = entry_ptr;
  691. }
  692. Safe[list]->EntryCount++;
  693. }
  694. Safe[list]->SlopCount += (is_slop ? 1 : 0);
  695. }
  696. /***********************************************************************************************
  697. * GenericDataSafeClass::Swap_Entries -- Swap two entries in memory (must be same type/size). *
  698. * *
  699. * *
  700. * *
  701. * INPUT: Ptr to first entry *
  702. * Ptr to second entry *
  703. * Entry types (must be same type) *
  704. * *
  705. * OUTPUT: Nothing *
  706. * *
  707. * WARNINGS: Entries must be same size and in the same list. If entries are moved to a *
  708. * different list then the owners handle will no longer work *
  709. * *
  710. * *
  711. * HISTORY: *
  712. * 6/25/2001 1:02PM ST : Created *
  713. *=============================================================================================*/
  714. void GenericDataSafeClass::Swap_Entries(DataSafeEntryClass *first, DataSafeEntryClass *second, int type)
  715. {
  716. ds_assert(first != NULL);
  717. ds_assert(second != NULL);
  718. ds_assert(type >= 0);
  719. #ifdef WWDEBUG
  720. NumSwaps++;
  721. #endif //WWDEBUG
  722. /*
  723. ** Get pointers to the actual data rider part of the entry.
  724. */
  725. void *first_data = ((char*)first) + sizeof(*first);
  726. void *second_data = ((char*)second) + sizeof(*second);
  727. /*
  728. ** Swap the objects in memory. Just swap the data and the handle so that the next and prev pointers are still good
  729. */
  730. if (first && second && first != second) {
  731. int size = Get_Type_Size(type); // + sizeof(DataSafeEntryClass);
  732. /*
  733. ** Optimise for small data types.
  734. */
  735. if (size == 4) {
  736. /*
  737. ** Convert to long pointers to make it easy to read.
  738. */
  739. long *p1 = (long*) first_data;
  740. long *p2 = (long*) second_data;
  741. long temp = *p1;
  742. *p1 = *p2;
  743. *p2 = temp;
  744. } else {
  745. /*
  746. ** Use a temp buffer on the stack to swap through.
  747. */
  748. void *temp_buffer = _alloca(size);
  749. memcpy(temp_buffer, first_data, size);
  750. memcpy(first_data, second_data, size);
  751. memcpy(second_data, temp_buffer, size);
  752. }
  753. /*
  754. ** Swap the handles.
  755. */
  756. DataSafeHandleClass temp_handle = first->Handle;
  757. bool is_slop = first->IsSlop;
  758. first->Handle = second->Handle;
  759. first->IsSlop = second->IsSlop;
  760. second->Handle = temp_handle;
  761. second->IsSlop = is_slop;
  762. }
  763. }
  764. /***********************************************************************************************
  765. * GenericDataSafeClass::Remove_From_List -- Remove entry from the linked list and fix pointers*
  766. * *
  767. * *
  768. * *
  769. * INPUT: List number *
  770. * Pointer to entry in list *
  771. * *
  772. * OUTPUT: Nothing *
  773. * *
  774. * WARNINGS: None *
  775. * *
  776. * HISTORY: *
  777. * 6/29/2001 10:54AM ST : Created *
  778. *=============================================================================================*/
  779. void GenericDataSafeClass::Remove_From_List(int list, DataSafeEntryClass *entry_ptr)
  780. {
  781. ds_assert(list >= 0);
  782. ds_assert(list < NumLists);
  783. ds_assert(Safe[list] != NULL);
  784. /*
  785. ** We found the entry for this handle. We need to fix up the list pointers and delete the memory.
  786. */
  787. if (Safe[list] && Safe[list]->EntryCount) {
  788. int data_size = Get_Type_Size(Safe[list]->EntryType);
  789. if (Safe[list]->EntryCount > 1) {
  790. entry_ptr->Prev->Next = entry_ptr->Next;
  791. entry_ptr->Next->Prev = entry_ptr->Prev;
  792. /*
  793. ** If this entry was at the head of the list then we need to adjust the head pointer.
  794. */
  795. if (Safe[list]->SafeList == entry_ptr) {
  796. Safe[list]->SafeList = entry_ptr->Next;
  797. }
  798. } else {
  799. Safe[list]->SafeList = NULL;
  800. Safe[list]->EntryType = -1;
  801. }
  802. Safe[list]->EntryCount--;
  803. if (entry_ptr->IsSlop) {
  804. Safe[list]->SlopCount--;
  805. }
  806. /*
  807. ** Muss up the memory so the data value isn't hanging around.
  808. */
  809. memset(entry_ptr, FreeRandom.Get_Int(0, 255), sizeof(*entry_ptr) + data_size);
  810. }
  811. }
  812. /***********************************************************************************************
  813. * GenericDataSafeClass::Shuffle -- Move stuff around in the safe, maybe change the key *
  814. * *
  815. * *
  816. * *
  817. * INPUT: Forced - set if we should shuffle now - even if it's not time yet *
  818. * *
  819. * OUTPUT: Nothing *
  820. * *
  821. * WARNINGS: Sloooooooow *
  822. * *
  823. * HISTORY: *
  824. * 6/25/2001 9:12PM ST : Created *
  825. *=============================================================================================*/
  826. void GenericDataSafeClass::Shuffle(bool forced)
  827. {
  828. ThreadLockClass locker;
  829. #ifndef FIXED_KEY
  830. /*
  831. ** Only check the time every n calls.
  832. */
  833. static unsigned long _calls = 0;
  834. _calls++;
  835. if (_calls < DATASAFE_TIME_CHECK_CALLS) {
  836. return;
  837. }
  838. _calls = 0;
  839. /*
  840. ** Locals.
  841. */
  842. unsigned long new_key;
  843. unsigned long mod_key;
  844. int i,j;
  845. /*
  846. ** We should only do this once in a while
  847. */
  848. unsigned long time = TIMEGETTIME();
  849. if (forced || time < ShuffleDelay || (time | ShuffleDelay) == 0 || (time - ShuffleDelay) > SHUFFLE_TIME) {
  850. //WWDEBUG_SAY(("Data Safe: Performing data shuffle and re-key\n"));
  851. #ifdef WWDEBUG
  852. NumShuffles++;
  853. #endif //WWDEBUG
  854. /*
  855. ** Calculate a new key. Lifed from old datasafe code.
  856. */
  857. /*
  858. ** Change keys
  859. ** Given: encrypted item a, old key b, new key c
  860. ** To re-key, the normal algorithm is (a ^ b) ^ c
  861. ** To hide what we're doing, we're going to do
  862. ** this: a ^ ( b ^ c )
  863. */
  864. /*
  865. ** Clear keys.
  866. */
  867. mod_key ^= mod_key;
  868. new_key ^= new_key;
  869. if (SimpleKey == 0) {
  870. /*
  871. ** Do some error stuff.
  872. */
  873. }
  874. /*
  875. ** XOR in old key.
  876. */
  877. mod_key ^= SimpleKey;
  878. /*
  879. ** Generate a new key.
  880. */
  881. new_key = TIMEGETTIME();
  882. new_key ^= FreeRandom.Get_Int(0, 0xffffffff);
  883. /*
  884. ** Reset the checksum. Can't keep a running checksum if we re-key
  885. */
  886. Checksum = ~new_key;
  887. /*
  888. ** Xor in the new key
  889. */
  890. mod_key ^= new_key;
  891. /*
  892. ** Now, mod_key contains the "modifier key" used to rekey items. We will use it after we have swapped some stuff around.
  893. */
  894. for (i=0 ; i<NumLists ; i++) {
  895. ds_assert(Safe[i] != NULL);
  896. if (Safe[i] != NULL && Safe[i]->EntryCount > 1) {
  897. for (j=0 ; j < max(Safe[i]->EntryCount / 3, 1) ; j++) {
  898. /*
  899. ** This code has been replaced by the simplified version below to avoid a compiler bug in release mode.
  900. */
  901. //DataSafeEntryClass *first = Get_Entry_By_Index(i, FreeRandom.Get_Int(0, Safe[i]->EntryCount - 1));
  902. //DataSafeEntryClass *second = Get_Entry_By_Index(i, FreeRandom.Get_Int(0, Safe[i]->EntryCount - 1));
  903. int first_index = FreeRandom.Get_Int(0, Safe[i]->EntryCount - 1);
  904. int second_index = FreeRandom.Get_Int(0, Safe[i]->EntryCount - 1);
  905. DataSafeEntryClass *first = Get_Entry_By_Index(i, first_index);
  906. DataSafeEntryClass *second = Get_Entry_By_Index(i, second_index);
  907. if (first && second && first != second) {
  908. Swap_Entries(first, second, Safe[i]->EntryType);
  909. }
  910. }
  911. }
  912. }
  913. /*
  914. ** Clear the old key.
  915. */
  916. SimpleKey ^= SimpleKey;
  917. /*
  918. ** Now, as if that wasn't slow enough, we have to go through and re-key *everything*.
  919. */
  920. /*
  921. ** Loop through all safe lists.
  922. */
  923. for (i=0 ; i<NumLists ; i++) {
  924. ds_assert(Safe[i] != NULL);
  925. if (Safe[i] != NULL && Safe[i]->EntryCount > 0) {
  926. /*
  927. ** Get the pointer to the first entry in the list.
  928. */
  929. DataSafeEntryClass *entry_ptr = Safe[i]->SafeList;
  930. /*
  931. ** Loop through all entries in this list.
  932. */
  933. for (j=0 ; j < Safe[i]->EntryCount ; j++) {
  934. /*
  935. ** Re-key the entry.
  936. */
  937. entry_ptr->Handle = entry_ptr->Handle ^ mod_key;
  938. Checksum ^= entry_ptr->Handle;
  939. void *data_ptr = ((char *)entry_ptr) + sizeof(*entry_ptr);
  940. Encrypt(data_ptr, Get_Type_Size(Safe[i]->EntryType), mod_key, true);
  941. /*
  942. ** Next entry.
  943. */
  944. entry_ptr = entry_ptr->Next;
  945. }
  946. }
  947. }
  948. /*
  949. ** Set the key.
  950. */
  951. SimpleKey ^= new_key;
  952. /*
  953. ** Reset the delay.
  954. */
  955. ShuffleDelay = TIMEGETTIME();
  956. /*
  957. ** Check safe integrity.
  958. */
  959. Security_Check();
  960. }
  961. #endif //FIXED_KEY
  962. }
  963. /***********************************************************************************************
  964. * GenericDataSafeClass::Get_Handle_ID -- Get the next available handle ID in the given list. *
  965. * *
  966. * *
  967. * *
  968. * INPUT: List number *
  969. * *
  970. * OUTPUT: Handle ID *
  971. * *
  972. * WARNINGS: None *
  973. * *
  974. * HISTORY: *
  975. * 7/5/2001 8:56PM ST : Created *
  976. *=============================================================================================*/
  977. int GenericDataSafeClass::Get_Handle_ID(int list)
  978. {
  979. /*
  980. ** Asserts.
  981. */
  982. ds_assert(Safe[list] != NULL);
  983. /*
  984. ** If the list isn't allocated then we are out of luck.
  985. */
  986. if (Safe[list] != NULL) {
  987. /*
  988. ** Go through the handle id usage table looking for a free spot.
  989. */
  990. for (int i=0 ; i<MAX_ENTRIES_PER_LIST ; i++) {
  991. if (Safe[list]->HandleIDUsage[i] == 0) {
  992. Safe[list]->HandleIDUsage[i] = 1;
  993. return(i);
  994. }
  995. }
  996. }
  997. /*
  998. ** Error. No free space or list not allocated.
  999. */
  1000. return(0);
  1001. }
  1002. /***********************************************************************************************
  1003. * GenericDataSafeClass::Free_Handle_ID -- Return a handle ID to the available pool *
  1004. * *
  1005. * *
  1006. * *
  1007. * INPUT: Safe list number *
  1008. * Handle ID *
  1009. * *
  1010. * OUTPUT: Nothing *
  1011. * *
  1012. * WARNINGS: None *
  1013. * *
  1014. * HISTORY: *
  1015. * 7/5/2001 8:59PM ST : Created *
  1016. *=============================================================================================*/
  1017. void GenericDataSafeClass::Free_Handle_ID(int list, int id)
  1018. {
  1019. /*
  1020. ** Asserts.
  1021. */
  1022. ds_assert(Safe[list] != NULL);
  1023. ds_assert(id >= 0 && id < MAX_ENTRIES_PER_LIST);
  1024. /*
  1025. ** If the list isn't allocated then we are out of luck.
  1026. */
  1027. if (Safe[list] != NULL) {
  1028. /*
  1029. ** It's easy, we just zero out the array element of the ID we no longer need.
  1030. */
  1031. Safe[list]->HandleIDUsage[id] = 0;
  1032. }
  1033. }
  1034. /***********************************************************************************************
  1035. * GenericDataSafeClass::Say_Security_Fault -- Security fault display action *
  1036. * *
  1037. * *
  1038. * *
  1039. * INPUT: Nothing *
  1040. * *
  1041. * OUTPUT: Nothing *
  1042. * *
  1043. * WARNINGS: None *
  1044. * *
  1045. * HISTORY: *
  1046. * 8/8/2001 2:25PM ST : Created *
  1047. *=============================================================================================*/
  1048. void GenericDataSafeClass::Say_Security_Fault(void)
  1049. {
  1050. WideStringClass text(TRANSLATION(IDS_DATASAFE_DETECTED_TAMPERING), true);
  1051. if (cNetwork::I_Am_Server()) {
  1052. text += L"Host";
  1053. cScTextObj *event_obj = new cScTextObj;
  1054. event_obj->Init(text, TEXT_MESSAGE_PUBLIC, false, HOST_TEXT_SENDER, -1);
  1055. } else {
  1056. cCsTextObj *message = new cCsTextObj;
  1057. cPlayer *me = cPlayerManager::Find_Player(cNetwork::Get_My_Id());
  1058. if (me) {
  1059. text += me->Get_Name();
  1060. }
  1061. message->Init(text, TEXT_MESSAGE_PUBLIC, cNetwork::Get_My_Id(), -1);
  1062. }
  1063. }
  1064. #ifdef THREAD_SAFE_DATA_SAFE
  1065. /***********************************************************************************************
  1066. * GenericDataSafeClass::Lock -- Get the safe mutex. *
  1067. * *
  1068. * *
  1069. * *
  1070. * INPUT: Nothing *
  1071. * *
  1072. * OUTPUT: Nothing *
  1073. * *
  1074. * WARNINGS: None *
  1075. * *
  1076. * HISTORY: *
  1077. * 7/6/2001 3:02PM ST : Created *
  1078. *=============================================================================================*/
  1079. inline void GenericDataSafeClass::Lock(void)
  1080. {
  1081. int deadlock = WaitForSingleObject(GenericDataSafeClass::SafeMutex, 10 * 1000);
  1082. if (deadlock == WAIT_TIMEOUT) {
  1083. WWDEBUG_SAY(("ERROR: Data Safe: Timeout waiting for data safe mutex\n"));
  1084. ds_assert(deadlock != WAIT_TIMEOUT);
  1085. }
  1086. }
  1087. /***********************************************************************************************
  1088. * GenericDataSafeClass::Unlock -- Release the safe mutex. *
  1089. * *
  1090. * *
  1091. * *
  1092. * INPUT: Nothing *
  1093. * *
  1094. * OUTPUT: Nothing *
  1095. * *
  1096. * WARNINGS: None *
  1097. * *
  1098. * HISTORY: *
  1099. * 7/6/2001 3:03PM ST : Created *
  1100. *=============================================================================================*/
  1101. inline void GenericDataSafeClass::Unlock(void)
  1102. {
  1103. ReleaseMutex(GenericDataSafeClass::SafeMutex);
  1104. }
  1105. #endif //THREAD_SAFE_DATA_SAFE
  1106. /***********************************************************************************************
  1107. * GenericDataSafeClass::Print_Safe_Stats_To_Debug_Output -- Print stats with OutputDebugString*
  1108. * *
  1109. * *
  1110. * *
  1111. * INPUT: Nothing *
  1112. * *
  1113. * OUTPUT: Nothing *
  1114. * *
  1115. * WARNINGS: None *
  1116. * *
  1117. * HISTORY: *
  1118. * 7/16/2001 2:28PM ST : Created *
  1119. *=============================================================================================*/
  1120. void GenericDataSafeClass::Print_Safe_Stats_To_Debug_Output(void)
  1121. {
  1122. #ifdef WWDEBUG
  1123. if (cDevOptions::LogDataSafe.Is_True()) {
  1124. char temp[8192];
  1125. Dump_Safe_Stats(temp, 8192);
  1126. WWDEBUG_SAY((temp));
  1127. }
  1128. #endif //WWDEBUG
  1129. }
  1130. /***********************************************************************************************
  1131. * GenericDataSafeClass::Dump_Safe_Stats - Dump safe stats into a buffer *
  1132. * *
  1133. * *
  1134. * *
  1135. * INPUT: Ptr to buffer to receive stats *
  1136. * Size of buffer *
  1137. * *
  1138. * OUTPUT: Nothing *
  1139. * *
  1140. * WARNINGS: None *
  1141. * *
  1142. * HISTORY: *
  1143. * 7/16/2001 2:12PM ST : Created *
  1144. *=============================================================================================*/
  1145. void GenericDataSafeClass::Dump_Safe_Stats(char *dump_buffer, int buffer_size)
  1146. {
  1147. #ifndef WWDEBUG
  1148. dump_buffer = dump_buffer;
  1149. buffer_size = buffer_size;
  1150. #else //WWDEBUG
  1151. #define UPDATE_PTR \
  1152. chars += strlen(dump_ptr); \
  1153. dump_ptr = dump_buffer + chars; \
  1154. ds_assert(buffer_size > chars); \
  1155. /*
  1156. ** Asserts.
  1157. */
  1158. ds_assert(dump_buffer != NULL);
  1159. ds_assert(buffer_size >= 1024);
  1160. ds_assert(!IsBadWritePtr(dump_buffer, buffer_size));
  1161. /*
  1162. ** Make a copy of the buffer ptr.
  1163. */
  1164. int chars = 0;
  1165. char *dump_ptr = dump_buffer;
  1166. /*
  1167. ** Precalculate some stats.
  1168. */
  1169. unsigned long time = TIMEGETTIME() - LastDump;
  1170. LastDump = TIMEGETTIME();
  1171. time = time / 1000;
  1172. int fetches_per_second = 0;
  1173. int swaps_per_second = 0;
  1174. if (time) {
  1175. fetches_per_second = NumFetches / time;
  1176. swaps_per_second = NumSwaps / time;
  1177. }
  1178. NumFetches = 0;
  1179. NumSwaps = 0;
  1180. /*
  1181. ** Dump basic datasafe stats into the buffer.
  1182. */
  1183. sprintf(dump_ptr, "Data Safe Stats\n\n");
  1184. UPDATE_PTR;
  1185. sprintf(dump_ptr, " Lists : %d\n", NumLists);
  1186. UPDATE_PTR;
  1187. sprintf(dump_ptr, " Slop Entries : %d\n", SlopCount);
  1188. UPDATE_PTR;
  1189. sprintf(dump_ptr, " Safe Key : %08X\n", SimpleKey);
  1190. UPDATE_PTR;
  1191. sprintf(dump_ptr, " Handle Key : %08X\n", HandleKey);
  1192. UPDATE_PTR;
  1193. sprintf(dump_ptr, " Checksum : %08X\n", Checksum);
  1194. UPDATE_PTR;
  1195. sprintf(dump_ptr, " Known Types : %d\n", TypeListCount);
  1196. UPDATE_PTR;
  1197. sprintf(dump_ptr, " Security Checks : %d\n", NumSecurityChecks);
  1198. UPDATE_PTR;
  1199. sprintf(dump_ptr, " Shuffles : %d\n", NumShuffles);
  1200. UPDATE_PTR;
  1201. sprintf(dump_ptr, " Fetches per second : %d\n", fetches_per_second);
  1202. UPDATE_PTR;
  1203. sprintf(dump_ptr, " Swaps per second : %d\n", swaps_per_second);
  1204. UPDATE_PTR;
  1205. sprintf(dump_ptr, " CRC Errors : %d\n", CRCErrors);
  1206. UPDATE_PTR;
  1207. /*
  1208. ** Count the number of items currently stored in the data safe.
  1209. */
  1210. int count = 0;
  1211. unsigned long bytes = 0;
  1212. for (int i=0 ; i<NumLists ; i++) {
  1213. ds_assert(Safe[i] != NULL);
  1214. if (Safe[i] != NULL) {
  1215. if (Safe[i]->EntryCount) {
  1216. count += Safe[i]->EntryCount;
  1217. bytes += (sizeof(DataSafeEntryClass) + Get_Type_Size(Safe[i]->EntryType)) * Safe[i]->EntryCount;
  1218. }
  1219. }
  1220. }
  1221. /*
  1222. ** Print out the usage stats.
  1223. */
  1224. sprintf(dump_ptr, "\n Safe contains %d objects consuming %d bytes\n", count, bytes);
  1225. UPDATE_PTR;
  1226. /*
  1227. ** Print out the percentage of safe capacity in use.
  1228. */
  1229. unsigned long percent = (count * 100) / (MAX_DATASAFE_LISTS * MAX_ENTRIES_PER_LIST);
  1230. sprintf(dump_ptr, "\n Safe is %d percent full\n\n", percent);
  1231. UPDATE_PTR;
  1232. #endif //WWDEBUG
  1233. }