datasafe.h 150 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449
  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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Commando/datasafe.h $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 1/31/02 12:13p $*
  29. * *
  30. * $Revision:: 20 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #ifndef _DATASAFE_H
  36. #define _DATASAFE_H
  37. /*
  38. ** Adapted from original code by Jeff Brown
  39. **
  40. ** //=============================================================================
  41. ** // DataSafe.h
  42. ** // Public header file for the datasafe
  43. ** // Author: Jeff Brown
  44. ** // Date: Apr 16, 1999
  45. ** //=============================================================================
  46. **
  47. **
  48. **
  49. **
  50. ** Class Overview. ST - 7/11/2001 8:19PM
  51. **
  52. ** DataSafeHandleClass.
  53. ** Used to manage handles to items stored in the data safe.
  54. **
  55. ** DataSafeEntryClass and DataSafeEntryDataClass<T>.
  56. ** Represent a single item as stored in the data safe. DataSafeEntryClass has the header
  57. ** information used to retrieve the item from the safe and the derived DataSafeEntryDataClass
  58. ** also includes the actual type specific data payload.
  59. **
  60. ** DataSafeEntryListClass
  61. ** This is a manager class for a linked list of DataSafeEntryDataClass<T>'s. Only the same
  62. ** type of DataSafeEntryDataClass<T> classes can currently be linked together and exist in
  63. ** the same list.
  64. **
  65. ** DataSafeEntryTypeClass
  66. ** Every object put into the data safe is allocated a type ID so that like types (with the
  67. ** same ID) can be identified for linking and swapping purposes. DataSafeEntryTypeClass is
  68. ** a covenient holder for type information.
  69. **
  70. ** GenericDataSafeClass and DataSafeClass<T>
  71. ** The safe itself consists of one static GenericDataSafeClass and many DataSafeClass<T>'s -
  72. ** one DataSafeClass<T> for every type of data we want to store in the data safe.
  73. ** Also included is an array of DataSafeEntryListClass's that act as pointers to the data safe
  74. ** entries.
  75. **
  76. **
  77. ** SafeDataClass
  78. ** This is a convenience class used to automate insertion and removal of items and provide
  79. ** a set of operators for common types. Using this class, adding existing data to the data safe
  80. ** can be as simple as just changing how it's declared. e.g.
  81. **
  82. ** int SomeData;
  83. **
  84. ** becomes
  85. **
  86. ** SafeDataClass<int> SomeData;
  87. **
  88. ** There are also some macros provided to make declaration easier so you could also just say
  89. **
  90. ** safe_int SomeData;
  91. **
  92. ** Currently, only a subset of operators are defined but more can easily be added as needed.
  93. ** Some changes to code will be required where operators are not available. For example
  94. **
  95. ** SomeData += 10;
  96. **
  97. ** would be a problem because the += operator is not defined. However the 'int' operator, the
  98. ** = operator and the conversion operator () are defined so you could change the code to read
  99. **
  100. ** SomeData = (int)SomeData + 10;
  101. **
  102. ** or
  103. **
  104. ** SomeData = SomeData() + 10;
  105. **
  106. ** You can pretty much throw anything into the data safe using this class to automate isertion
  107. ** and retrieval. Some examples.
  108. **
  109. ** safe_float HideyFloat;
  110. ** safe_double CantFindMe[256];
  111. ** SafeDataClass<SomeNamedClass> ASafeClass;
  112. ** SafeDataClass<SomeOtherType> ASafeType;
  113. **
  114. ** For types without existing macros, you will have to declare a new DataSafeClass<T> once for
  115. ** each type in DataSafe.cpp using the DECLARE_DATA_SAFE macro. So in the above example, you
  116. ** would have to add the following lines in DataSafe.cpp to allow storage of the new types
  117. **
  118. ** DECLARE_DATA_SAFE(SomeNamedClass);
  119. ** DECLARE_DATA_SAFE(SomeOtherType);
  120. **
  121. **
  122. **
  123. **
  124. ** Known external dependencies.
  125. ** DynamicVectorClass
  126. ** TICKS_PER_SECOND = 1000
  127. ** FreeRandom - Renegade random number generator.
  128. ** WWDEBUG_SAY - Equivalent to DebugString in C&C. Just a formatted debug output.
  129. **
  130. */
  131. /*
  132. ** Includes.
  133. */
  134. #ifndef _ALWAYS_H
  135. #include "always.h"
  136. #include <assert.h>
  137. #include <malloc.h>
  138. #include <memory.h>
  139. #include <windows.h>
  140. #endif //_ALWAYS_H
  141. #ifndef _TIMEMGR_H
  142. #include "timemgr.h"
  143. #endif //_TIMEMGR_H
  144. #ifndef _WWDEBUG_H
  145. #include "wwdebug.h"
  146. #endif //_WWDEBUG_H
  147. //#ifndef MMSYS_H
  148. //#include "mmsys.h"
  149. //#endif //MMSYS_H
  150. #include "systimer.h"
  151. /*
  152. ** Use Renegade assert.
  153. */
  154. #ifdef WWASSERT
  155. #define ds_assert WWASSERT
  156. #else //WWASSERT
  157. #define ds_assert assert
  158. #endif //WWASSERT
  159. /*
  160. ** TEMP. Profile builds don't have asserts.
  161. */
  162. #ifndef DEBUG
  163. #undef ds_assert
  164. #define ds_assert(x)
  165. #pragma warning(disable : 4189)
  166. #endif //DEBUG
  167. #ifndef WWDEBUG
  168. #pragma warning(disable : 4101)
  169. #endif
  170. /*
  171. ** Enable this define to make the datasafe thread safe (there is a performance penalty).
  172. */
  173. //#define THREAD_SAFE_DATA_SAFE
  174. /*
  175. ** Number of linked lists we can create for data safe items. Only like sized items can be stored in the same list.
  176. */
  177. #define MAX_DATASAFE_LISTS 8192
  178. /*
  179. ** The maximum number of different data types that can be stored in the data safe. This should normally fit into a byte for
  180. ** handle encoding reasons.
  181. */
  182. #define MAX_DATASAFE_TYPES 128
  183. /*
  184. ** Max number of entries in a single list before we have to create a second list for items of the same size. The smaller this
  185. ** value, the quicker we will be able to retrieve data from the safe. The larger we make it, the more secure the data safe will
  186. ** be.
  187. */
  188. #define MAX_ENTRIES_PER_LIST 16
  189. /*
  190. ** How often the data safe entries get shuffled around and re-keyed. This is the delay between each shuffle.
  191. */
  192. #define SHUFFLE_TIME (TICKS_PER_SECOND * 7)
  193. /*
  194. ** The delay between each integrity check on the data safe. This is a slow operation.
  195. */
  196. #define SECURITY_CHECK_TIME (TICKS_PER_SECOND * 15)
  197. /*
  198. ** The number of copies of objects of any one type that can exist in an unencrypted state. These copies are only used as return
  199. ** values and changes to the copies can never be propagated back into the safe.
  200. */
  201. #define MAX_OBJECT_COPIES 8
  202. /*
  203. ** Sentinel values. Used to see if someone just zeroes the whole safe or tampered with the get function.
  204. */
  205. #define SENTINEL_ONE 0x29874362
  206. #define SENTINEL_TWO 0x0bad0bad
  207. #define DATASAFE_TIME_CHECK_CALLS 100000
  208. /*
  209. ** Defines to allow convenient use of built-in types inside the data safe.
  210. **
  211. */
  212. #ifdef PARAM_EDITING_ON
  213. #define safe_int int
  214. #define safe_unsigned_int unsigned int
  215. #define safe_long long
  216. #define safe_unsigned_long unsigned long
  217. #define safe_float float
  218. #define safe_double double
  219. #else //PARAM_EDITING_ON
  220. #define safe_int SafeDataClass<int>
  221. #define safe_unsigned_int SafeDataClass<unsigned int>
  222. #define safe_long SafeDataClass<int>
  223. #define safe_unsigned_long SafeDataClass<unsigned int>
  224. #define safe_float SafeDataClass<float>
  225. #define safe_double SafeDataClass<double>
  226. #endif //PARAM_EDITING_ON
  227. /*
  228. ** Make sure we pulled in the right timer defines.
  229. */
  230. #if TICKS_PER_SECOND != 1000
  231. #error Code assumes TICKS_PER_SECOND == 1000
  232. #endif
  233. /*
  234. ** We want to inline functions within inline functions to make the code harder to hack by having multiple copies of it.
  235. */
  236. #pragma inline_recursion(on)
  237. #pragma inline_depth(4)
  238. #pragma warning(disable : 4714)
  239. /*
  240. ** Data safe handle.
  241. **
  242. ** This consists of a linked list number, an entry identifier and a type.
  243. **
  244. ** The whole shooting match can just be treated like an int.
  245. **
  246. */
  247. class DataSafeHandleClass
  248. {
  249. public:
  250. /*
  251. ** Consturctor.
  252. */
  253. DataSafeHandleClass(int val = 0) {
  254. Handle.Whole.WholeHandle = val;
  255. };
  256. /*
  257. ** Validity check.
  258. */
  259. bool Is_Valid(void) const {
  260. return(Handle.Whole.WholeHandle != -1);
  261. };
  262. /*
  263. ** Convenience functions and operators to handle setting or getting a handle using an int.
  264. */
  265. inline int operator = (const int &val) {
  266. Handle.Whole.WholeHandle = val;
  267. return(val);
  268. };
  269. inline operator int (void) {
  270. return(Handle.Whole.WholeHandle);
  271. }
  272. inline operator int (void) const {
  273. return(Handle.Whole.WholeHandle);
  274. }
  275. inline int &operator () (void) {
  276. return(Handle.Whole.WholeHandle);
  277. }
  278. /*
  279. ** Handle data.
  280. */
  281. union tHandle {
  282. struct {
  283. /*
  284. ** All the handle components conveniently together in an int.
  285. */
  286. int WholeHandle;
  287. } Whole;
  288. struct {
  289. /*
  290. ** ID is basically the index of the item in the linked list. It's a little more complex
  291. ** than that since an ID can be re-used after the original user is removed from the
  292. ** list but the whole list is searched for a matching ID when retrieving anyway.
  293. */
  294. unsigned char ID;
  295. /*
  296. ** The type of object this handle represents.
  297. */
  298. unsigned char Type;
  299. /*
  300. ** The list number where we can find this object.
  301. */
  302. unsigned short List;
  303. } Part;
  304. } Handle;
  305. };
  306. /*
  307. ** Known packing is required here otherwise the default alignment is the same as the largest member
  308. ** of the structure. When combined with an expansion of DataSafeEntryDataClass<T> for doubles, the
  309. ** packing alignment becomes 8 (the size of a double) thus the offset of Data is not always as expected.
  310. ** i.e. you can no longer use sizeof(DataSafeEntryClass) to get the offset of the Data member of
  311. ** the derived class. This also prevents bloating (due to the bool member) when using large expansions
  312. ** for 'T'
  313. */
  314. #pragma pack(push)
  315. #pragma pack(4)
  316. /*
  317. **
  318. ** This class represents an internal data safe entry minus the actual data. In practice, this class will never be used
  319. ** except to refer to a DataSafeEntryDataClass<T> which actually contains the type specific data.
  320. **
  321. */
  322. class DataSafeEntryClass
  323. {
  324. public:
  325. /*
  326. ** Identifying handle.
  327. */
  328. DataSafeHandleClass Handle;
  329. /*
  330. ** Linked list next and prev.
  331. **
  332. ** Note: These can point to a DataSafeEntryDataClass expanded for any type.
  333. */
  334. DataSafeEntryClass *Next;
  335. DataSafeEntryClass *Prev;
  336. /*
  337. ** Size of data.
  338. */
  339. unsigned long Size;
  340. /*
  341. ** Is this a slop (fake, to allow swapping with only 1 real entry) entry?
  342. */
  343. bool IsSlop;
  344. };
  345. /*
  346. ** This is a complete internal data safe entry including the header and the encrypted data.
  347. **
  348. */
  349. template<class T>
  350. class DataSafeEntryDataClass : public DataSafeEntryClass
  351. {
  352. public:
  353. T Data;
  354. };
  355. #pragma pack(pop)
  356. /*
  357. ** This class represents a single data safe entry list. There can be mutiple lists in a data safe but all entries in a single
  358. ** list must be of the same type.
  359. */
  360. class DataSafeEntryListClass
  361. {
  362. public:
  363. /*
  364. ** Constructor.
  365. */
  366. DataSafeEntryListClass(void) {
  367. SafeList = NULL;
  368. EntryCount = 0;
  369. EntryType = -1;
  370. SlopCount = 0;
  371. memset(HandleIDUsage, 0, sizeof(HandleIDUsage));
  372. };
  373. /*
  374. ** Pointer to the head of the list.
  375. */
  376. DataSafeEntryClass *SafeList;
  377. /*
  378. ** Number of entries in the list.
  379. */
  380. int EntryCount;
  381. /*
  382. ** Type of entry in this list.
  383. */
  384. int EntryType;
  385. /*
  386. ** Number of slop entries in this list.
  387. */
  388. int SlopCount;
  389. /*
  390. ** Handle IDs used in this list.
  391. */
  392. char HandleIDUsage[MAX_ENTRIES_PER_LIST];
  393. };
  394. /*
  395. **
  396. ** This class describes a type of entry in the data safe. It's important to keep track of the types of objects we add since
  397. ** only similar types can be swapped in memory. This class is just a convenient container to help us match type codes with
  398. ** unique IDs.
  399. **
  400. **
  401. */
  402. class DataSafeEntryTypeClass
  403. {
  404. public:
  405. DataSafeEntryTypeClass(void) {
  406. TypeCode = 0;
  407. ID = -1;
  408. Size = 0;
  409. };
  410. /*
  411. ** A unique number used to match and assign type IDs. This can come from anywhere as long as it's different for every type.
  412. */
  413. unsigned long TypeCode;
  414. /*
  415. ** This is the user friendly ID that is stored along with entries in the data safe and returned in the handle that's
  416. ** given out when something is added to the safe.
  417. ** It's also the index into the type list.
  418. */
  419. int ID;
  420. /*
  421. ** The size of this type of object. Must be a multiple of 4 bytes for the data safe. (This is redundant now that we
  422. ** have a type/size lookup table).
  423. */
  424. int Size;
  425. };
  426. /*
  427. ** GenericDataSafeClass
  428. **
  429. ** This class represents a single data safe. It can only function as the base class to one or more instances of
  430. ** DataSafeClass<T>.
  431. **
  432. **
  433. ** This class functions as a base class for all the templated 'DataSafeClass'es so that they can all share a single set of
  434. ** static data. Normally, static data in a templated class is duplicated for each expansion of the class.
  435. **
  436. ** Even though there may be many instances and expansions of the derived DataSafeClass<T> there is only ever one global
  437. ** data safe and that is represented by this class.
  438. **
  439. */
  440. class GenericDataSafeClass
  441. {
  442. public:
  443. /*
  444. ** Constructor.
  445. */
  446. GenericDataSafeClass(void);
  447. /*
  448. ** Query functions.
  449. */
  450. static DataSafeEntryClass * Get_Entry(DataSafeHandleClass handle);
  451. static int Get_Entry_Type(DataSafeHandleClass handle);
  452. /*
  453. ** System timer reset.
  454. */
  455. static void Reset_Timers(void);
  456. /*
  457. ** Thread safety. This is normally handled automatically but if you need to manually lock the safe for a period of time
  458. ** to do a read/hold/write then this is the way to do it.
  459. */
  460. #ifdef THREAD_SAFE_DATA_SAFE
  461. static void Lock(void);
  462. static void Unlock(void);
  463. #endif //THREAD_SAFE_DATA_SAFE
  464. /*
  465. ** Stats, debug helpers.
  466. */
  467. static void Dump_Safe_Stats(char *dump_buffer, int buffer_size);
  468. static void Print_Safe_Stats_To_Debug_Output(void);
  469. static void Reset(void);
  470. #ifdef THREAD_SAFE_DATA_SAFE
  471. static void Set_Preferred_Thread(unsigned int){};
  472. #else //THREAD_SAFE_DATA_SAFE
  473. static void Set_Preferred_Thread(unsigned int thread_id) {PreferredThread = thread_id;};
  474. #endif //THREAD_SAFE_DATA_SAFE
  475. protected:
  476. /*
  477. ** Cleanup.
  478. */
  479. void Shutdown(void);
  480. /*
  481. ** Query functions.
  482. */
  483. static DataSafeEntryClass * Get_Entry_By_Index(int list, int index);
  484. /*
  485. ** Security
  486. */
  487. static void Shuffle(bool forced = false);
  488. static void Swap_Entries(DataSafeEntryClass *first, DataSafeEntryClass *second, int type);
  489. static void Encrypt(void *data, int size, unsigned long key = SimpleKey, bool do_checksum = true);
  490. static void Decrypt(void *data, int size, unsigned long key = SimpleKey, bool do_checksum = true);
  491. static void Mem_Copy_Encrypt(void *dest, void *src, int size, bool do_checksum);
  492. static void Mem_Copy_Decrypt(void *dest, void *src, int size, bool do_checksum);
  493. static __forceinline void Security_Check(void);
  494. static __forceinline void Security_Fault(void);
  495. static void Say_Security_Fault(void);
  496. /*
  497. ** Insertion support.
  498. */
  499. static int Get_Random_List_For_Insertion(int type);
  500. static void Random_Insertion(DataSafeEntryClass *entry, int list, int type, bool is_slop = false);
  501. static int Create_Safe_List(int type);
  502. static int Get_Handle_ID(int list);
  503. /*
  504. ** Deletion support. Just used to make sure we are doing type safe deletion.
  505. */
  506. static void Remove_From_List(int list, DataSafeEntryClass *entry_ptr);
  507. static void Free_Handle_ID(int list, int id);
  508. /*
  509. ** Type identification.
  510. */
  511. static inline int Get_Type_Size(int type);
  512. #ifdef THREAD_SAFE_DATA_SAFE
  513. /*
  514. ** Thread safety.
  515. */
  516. class ThreadLockClass
  517. {
  518. public:
  519. /*
  520. ** Constructor. Grabs the mutex.
  521. */
  522. inline ThreadLockClass(void) {
  523. int deadlock = WaitForSingleObject(GenericDataSafeClass::SafeMutex, 10 * 1000);
  524. if (deadlock == WAIT_TIMEOUT) {
  525. WWDEBUG_SAY(("Data Safe: Timeout waiting for data safe mutex\n"));
  526. ds_assert(deadlock != WAIT_TIMEOUT);
  527. }
  528. };
  529. /*
  530. ** Destructor, releases the mutex.
  531. */
  532. inline ~ThreadLockClass(void) {
  533. ReleaseMutex(GenericDataSafeClass::SafeMutex);
  534. };
  535. };
  536. #else //THREAD_SAFE_DATA_SAFE
  537. /*
  538. ** If thread safety is not required, this class compiles out to nothing when used in release mode.
  539. */
  540. class ThreadLockClass
  541. {
  542. public:
  543. #ifdef WWDEBUG
  544. __forceinline ThreadLockClass(void) {
  545. if (GenericDataSafeClass::PreferredThread != GetCurrentThreadId()) {
  546. WWDEBUG_SAY(("DATASAFE.H - PreferredThread = %08X, GetCurrentThreadId() == %08X\n", GenericDataSafeClass::PreferredThread, GetCurrentThreadId()));
  547. }
  548. ds_assert(GenericDataSafeClass::PreferredThread == GetCurrentThreadId());
  549. };
  550. #endif //WWDEBUG
  551. };
  552. static unsigned int PreferredThread;
  553. #endif //THREAD_SAFE_DATA_SAFE
  554. friend ThreadLockClass;
  555. /*
  556. ** Static data. We only need one each of these, regardless of how many DataSafeClass 'types' there are.
  557. **
  558. **
  559. **
  560. */
  561. /*
  562. ** Simple key value used for xoring.
  563. */
  564. static unsigned long SimpleKey;
  565. /*
  566. ** Key used for encrypting handles.
  567. */
  568. static unsigned long HandleKey;
  569. /*
  570. ** Number of valid entries in the Safe list.
  571. */
  572. static int NumLists;
  573. /*
  574. ** Array of pointers to data lists. We can't use a dynamic vector class here because the handles we give out include
  575. ** an index into this array. Dynamic vector class moves entries around to remove gaps in the array which would leave
  576. ** the handles pointing to the wrong element.
  577. */
  578. static DataSafeEntryListClass *Safe[MAX_DATASAFE_LISTS];
  579. /*
  580. ** Integrity check.
  581. */
  582. static unsigned long Checksum;
  583. /*
  584. ** Shuffle delay.
  585. */
  586. static unsigned long ShuffleDelay;
  587. /*
  588. ** Security check delay.
  589. */
  590. static unsigned long SecurityCheckDelay;
  591. /*
  592. ** List of types that are stored in the data safe.
  593. */
  594. static DataSafeEntryTypeClass TypeList[MAX_DATASAFE_TYPES];
  595. static int TypeListCount;
  596. /*
  597. ** Mutex to ensure thread safety.
  598. */
  599. #ifdef THREAD_SAFE_DATA_SAFE
  600. static HANDLE SafeMutex;
  601. #endif //THREAD_SAFE_DATA_SAFE
  602. /*
  603. ** Sentinels. Can't use 'SafeDataClass' as it's constructor adds data to the safe - i.e. us.
  604. */
  605. static DataSafeHandleClass SentinelOne;
  606. static DataSafeHandleClass SentinelTwo;
  607. /*
  608. ** Number of CRC errors so we can report it in the game results.
  609. */
  610. static int CRCErrors;
  611. /*
  612. ** Statistics - debug only.
  613. */
  614. #ifdef WWDEBUG
  615. static unsigned long LastDump;
  616. static int NumSwaps;
  617. static int NumFetches;
  618. static int SlopCount;
  619. static int NumSecurityChecks;
  620. static int NumShuffles;
  621. #endif //WWDEBUG
  622. };
  623. /*
  624. ** DataSafeClass<T>
  625. **
  626. ** This templated class is used to provide type specific functionality in the data safe. There must be at least one instance
  627. ** of this class for each type of data we want to store in the safe. This, along with 'GenericDataSafeClass' represents
  628. ** a single global data safe.
  629. **
  630. */
  631. template<class T>
  632. class DataSafeClass : public GenericDataSafeClass
  633. {
  634. public:
  635. DataSafeClass(T *bogus_ptr = NULL, int slopcount = 3);
  636. ~DataSafeClass(void);
  637. /*
  638. ** Query functions.
  639. */
  640. static bool Get(DataSafeHandleClass handle, T* &item);
  641. static inline bool In_Return_List(T *ptr) {
  642. if (ptr) {
  643. void *temp = (void*)ptr;
  644. if (temp >= &ReturnList[0][0] && temp < &ReturnList[MAX_OBJECT_COPIES][0]) {
  645. if (((unsigned long) temp - (unsigned long)(&ReturnList[0][0])) % sizeof(T) == 0) {
  646. return(true);
  647. }
  648. }
  649. }
  650. return(false);
  651. };
  652. /*
  653. ** Set functions.
  654. */
  655. static bool Set(DataSafeHandleClass handle, T *data);
  656. /*
  657. ** Add functions.
  658. */
  659. static DataSafeHandleClass Add_Entry(T &value, bool is_slop = false);
  660. /*
  661. ** Removal functions.
  662. */
  663. static void Delete_Entry(DataSafeHandleClass handle);
  664. private:
  665. /*
  666. ** Type identification.
  667. */
  668. static int Get_Type_ID(unsigned long type_code, int size);
  669. static unsigned long Get_Type_Code(void);
  670. /*
  671. ** Type of this DataSafe.
  672. */
  673. static int Type;
  674. /*
  675. ** Min slop entries for this type of safe.
  676. */
  677. static int MinSlop;
  678. /*
  679. ** Copies of items in the data safe. Only used to return copies of items in the safe.
  680. **
  681. ** The number of elements in this list is the number of times you can call the 'Get' function before you start to
  682. ** invalidate the data you got 'n' calls ago. For this reason, you should always make a copy of data retrieved from
  683. ** the safe if you plan to use it more than once.
  684. */
  685. static char ReturnList[MAX_OBJECT_COPIES][sizeof(T)];
  686. static int ReturnIndex;
  687. };
  688. /*
  689. **
  690. ** Macros to help in declaring data safe types.
  691. **
  692. **
  693. **
  694. */
  695. #define DECLARE_DATA_SAFE(type) \
  696. DataSafeClass<type> DataSafe##type; \
  697. int DataSafeClass<type>::Type; \
  698. char DataSafeClass<type>::ReturnList[MAX_OBJECT_COPIES][sizeof(type)]; \
  699. int DataSafeClass<type>::ReturnIndex; \
  700. int DataSafeClass<type>::MinSlop;
  701. /*
  702. **
  703. ** SafeDataClass.
  704. **
  705. ** This class is for convenience when using the data safe. Just expand this class for the type you want to add to the data safe
  706. ** and it will keep track of the handle and automate the adding in, and removal of, data in the safe.
  707. **
  708. */
  709. extern char ErrorVal[1024];
  710. template<class T>
  711. class SafeDataClass
  712. {
  713. public:
  714. /*
  715. ** Constructor, destructor.
  716. */
  717. SafeDataClass(void);
  718. SafeDataClass(T data);
  719. ~SafeDataClass(void);
  720. /*
  721. ** Copy constructor. This is a bad thing to call.
  722. */
  723. SafeDataClass(SafeDataClass &) {
  724. ds_assert(false);
  725. };
  726. /*
  727. ** Helper functions for data safe class storage.
  728. */
  729. inline T* Get_Ptr(void) const;
  730. inline bool Commit(T* data_ptr) const;
  731. /*
  732. ** Operators.
  733. */
  734. inline T &operator () (void) const;
  735. inline T &operator = (T const &data);
  736. inline T &operator = (SafeDataClass<T> &safedata);
  737. inline operator int(void) const;
  738. inline operator unsigned int(void) const;
  739. inline operator long(void) const;
  740. inline operator unsigned long(void) const;
  741. inline operator float(void) const;
  742. inline operator double(void) const;
  743. inline bool operator == (T const &data);
  744. inline bool operator == (SafeDataClass<T> &safedata);
  745. inline bool operator != (T const &data);
  746. inline bool operator != (SafeDataClass<T> &safedata);
  747. inline bool operator > (T const &data);
  748. inline bool operator > (SafeDataClass<T> &safedata);
  749. inline bool operator >= (T const &data);
  750. inline bool operator >= (SafeDataClass<T> &safedata);
  751. inline bool operator < (T const &data);
  752. inline bool operator < (SafeDataClass<T> &safedata);
  753. inline bool operator <= (T const &data);
  754. inline bool operator <= (SafeDataClass<T> &safedata);
  755. inline T &operator + (T const &data);
  756. inline T &operator + (SafeDataClass<T> &safedata);
  757. inline T &operator += (T const &data);
  758. inline T &operator += (SafeDataClass<T> &safedata);
  759. inline T &operator - (T const &data);
  760. inline T &operator - (SafeDataClass<T> &safedata);
  761. inline T &operator -= (T const &data);
  762. inline T &operator -= (SafeDataClass<T> &safedata);
  763. inline T &operator * (T const &data);
  764. inline T &operator * (SafeDataClass<T> &safedata);
  765. inline T &operator *= (T const &data);
  766. inline T &operator *= (SafeDataClass<T> &safedata);
  767. inline T &operator / (T const &data);
  768. inline T &operator / (SafeDataClass<T> &safedata);
  769. inline T &operator /= (T const &data);
  770. inline T &operator /= (SafeDataClass<T> &safedata);
  771. inline T &operator ++ (void);
  772. inline T &operator -- (void);
  773. inline T &operator ++ (int);
  774. inline T &operator -- (int);
  775. /*
  776. ** Query and set functions.
  777. */
  778. DataSafeHandleClass Get_Handle(void) {return(Handle);};
  779. T Get(void);
  780. void Set(T);
  781. private:
  782. /*
  783. ** This is the handle that represents this classes data in the datasafe.
  784. */
  785. DataSafeHandleClass Handle;
  786. #ifdef WWDEBUG
  787. T DebugData;
  788. #endif //WWDEBUG
  789. };
  790. /***********************************************************************************************
  791. **
  792. **
  793. **
  794. ** File separation.
  795. **
  796. **
  797. */
  798. /*
  799. ** Simple functions, suitable for inline expansion.
  800. **
  801. **
  802. **
  803. **
  804. */
  805. /*
  806. **
  807. **
  808. ** GenericDataSafeClass functions.
  809. **
  810. **
  811. */
  812. /***********************************************************************************************
  813. * GenericDataSafeClass::Get_Type_Size -- Get the size of this type *
  814. * *
  815. * *
  816. * *
  817. * INPUT: Type ID of object *
  818. * *
  819. * OUTPUT: Size of object *
  820. * *
  821. * WARNINGS: None *
  822. * *
  823. * HISTORY: *
  824. * 6/27/2001 12:47PM ST : Created *
  825. *=============================================================================================*/
  826. inline int GenericDataSafeClass::Get_Type_Size(int type)
  827. {
  828. ds_assert(type >= 0);
  829. ds_assert(type < MAX_DATASAFE_TYPES);
  830. ds_assert(type < TypeListCount);
  831. return(TypeList[type].Size);
  832. }
  833. /***********************************************************************************************
  834. * GenericDataSafeClass::Security_Fault -- Security fault handler *
  835. * *
  836. * __forceinline used to make sure there are multiple copies of this code. It's bloaty *
  837. * but it makes it harder to hack out (but only a little bit) *
  838. * *
  839. * *
  840. * INPUT: Nothing *
  841. * *
  842. * OUTPUT: Nothing *
  843. * *
  844. * WARNINGS: None *
  845. * *
  846. * HISTORY: *
  847. * 7/9/2001 2:20PM ST : Created *
  848. *=============================================================================================*/
  849. __forceinline void GenericDataSafeClass::Security_Fault(void)
  850. {
  851. WWDEBUG_SAY(("Data Safe:Security fault\n"));
  852. CRCErrors++;
  853. Say_Security_Fault();
  854. ds_assert(false);
  855. }
  856. /***********************************************************************************************
  857. * GenericDataSafeClass::Security_Check -- Look for security problems. *
  858. * *
  859. * __forceinline used to make sure there are multiple copies of this code. It's bloaty *
  860. * but it makes it harder to hack out (but only a little bit) *
  861. * *
  862. * *
  863. * INPUT: Nothing *
  864. * *
  865. * OUTPUT: Nothing *
  866. * *
  867. * WARNINGS: *
  868. * *
  869. * HISTORY: *
  870. * 7/9/2001 1:08PM ST : Created *
  871. *=============================================================================================*/
  872. __forceinline void GenericDataSafeClass::Security_Check(void)
  873. {
  874. ThreadLockClass locker;
  875. /*
  876. ** Flag to prevent recursion.
  877. */
  878. static bool _checking = false;
  879. /*
  880. ** Only check the time every n calls.
  881. */
  882. static unsigned long _calls = 0;
  883. _calls++;
  884. if (_calls < DATASAFE_TIME_CHECK_CALLS) {
  885. return;
  886. }
  887. _calls = 0;
  888. /*
  889. ** See if SimpleKey has been zeroed out.
  890. */
  891. if (HandleKey - SimpleKey == HandleKey) {
  892. WWDEBUG_SAY(("Data Safe: Simple Key compromised!\n"));
  893. Security_Fault();
  894. }
  895. /*
  896. ** See if HandleKey has been zeroed out.
  897. */
  898. if (SimpleKey - HandleKey == SimpleKey) {
  899. WWDEBUG_SAY(("Data Safe: Handle Key compromised!\n"));
  900. Security_Fault();
  901. }
  902. /*
  903. ** Use the checksum to verify the whole data safe.
  904. ** Yes, the whole safe. A teeny bit slow maybe.
  905. **
  906. ** Since we are going through the whole safe here, we might as well make a note of where slop
  907. ** needs to be added or removed and count how many total slop entries we have.
  908. */
  909. unsigned long time = TIMEGETTIME();
  910. if (time < SecurityCheckDelay || (time | SecurityCheckDelay) == 0 || (time - SecurityCheckDelay) > SECURITY_CHECK_TIME) {
  911. #ifdef WWDEBUG
  912. SlopCount = 0;
  913. NumSecurityChecks++;
  914. #endif //WWDEBUG
  915. /*
  916. ** If toooo much time has gone by then do it anyway.
  917. */
  918. if (!_checking) { // || (time - SecurityCheckDelay) > SECURITY_CHECK_TIME * 5) {
  919. _checking = true;
  920. //WWDEBUG_SAY(("Data Safe: Performing security check\n"));
  921. SecurityCheckDelay = time;
  922. unsigned long checkey = ~SimpleKey;
  923. /*
  924. ** Loop through every list.
  925. */
  926. for (int i=0 ; i<NumLists ; i++) {
  927. ds_assert(Safe[i] != NULL);
  928. if (Safe[i] != NULL && Safe[i]->EntryCount > 0) {
  929. /*
  930. ** Dereference stuff - make sure the list makes sense.
  931. */
  932. DataSafeEntryClass *entry_ptr = Safe[i]->SafeList;
  933. unsigned long *data = NULL;
  934. ds_assert(entry_ptr != NULL);
  935. int data_size = entry_ptr->Size;
  936. ds_assert((data_size & 3) == 0);
  937. data_size = data_size >> 2;
  938. if (entry_ptr) {
  939. /*
  940. ** Go through each entry in the list.
  941. */
  942. for (int j=0 ; j<Safe[i]->EntryCount ; j++) {
  943. #ifdef WWDEBUG
  944. /*
  945. ** Count slop entries for debug purposes.
  946. */
  947. if (entry_ptr->IsSlop) {
  948. SlopCount++;
  949. }
  950. #endif //WWDEBUG
  951. /*
  952. ** Add in the handle.
  953. */
  954. checkey ^= entry_ptr->Handle;
  955. /*
  956. ** Add in the data.
  957. */
  958. data = (unsigned long *) (((char*)entry_ptr) + sizeof(*entry_ptr));
  959. for (int z=0 ; z<data_size ; z++) {
  960. checkey ^= *data++;
  961. }
  962. /*
  963. ** Next entry.
  964. */
  965. entry_ptr = entry_ptr->Next;
  966. }
  967. }
  968. }
  969. }
  970. if (checkey != Checksum) {
  971. WWDEBUG_SAY(("Data Safe: Incorrect checksum!\n"));
  972. Security_Fault();
  973. }
  974. /*
  975. ** Check the values of the sentinels.
  976. */
  977. if (NumLists) {
  978. int *val = NULL;
  979. if ((int)SentinelOne != 0) {
  980. bool got = DataSafeClass<int>::Get(SentinelOne, val);
  981. if (!got || val == NULL || *val != SENTINEL_ONE) {
  982. WWDEBUG_SAY(("Data Safe: Incorrect sentinel value!\n"));
  983. Security_Fault();
  984. }
  985. }
  986. val = NULL;
  987. if ((int)SentinelTwo != 0) {
  988. bool got = DataSafeClass<int>::Get(SentinelTwo, val);
  989. if (!got || val == NULL || *val != SENTINEL_TWO) {
  990. WWDEBUG_SAY(("Data Safe: Incorrect sentinel value!\n"));
  991. Security_Fault();
  992. }
  993. }
  994. }
  995. #ifdef WWDEBUG
  996. Print_Safe_Stats_To_Debug_Output();
  997. #endif //WWDEBUG
  998. _checking = false;
  999. }
  1000. }
  1001. }
  1002. /*
  1003. **
  1004. **
  1005. ** DataSafeClass functions.
  1006. **
  1007. **
  1008. */
  1009. /***********************************************************************************************
  1010. * DataSafeClass::DataSafeClass -- Class constructor *
  1011. * *
  1012. * *
  1013. * *
  1014. * INPUT: Bogus pointer. Pointer that isn't referenced. It's only used for type info *
  1015. * Number of 'slop' entries to create *
  1016. * *
  1017. * OUTPUT: Nothing *
  1018. * *
  1019. * WARNINGS: None *
  1020. * *
  1021. * HISTORY: *
  1022. * 6/25/2001 1:51PM ST : Created *
  1023. *=============================================================================================*/
  1024. template <class T>
  1025. DataSafeClass<T>::DataSafeClass(T*, int slopcount)
  1026. {
  1027. ThreadLockClass locker;
  1028. /*
  1029. ** Clear out the return list.
  1030. */
  1031. memset(ReturnList, 0, sizeof(ReturnList));
  1032. ReturnIndex = 0;
  1033. /*
  1034. ** Get the type info for 'T'. Different types get stored in different places in the safe. Only similar types can be in the
  1035. ** same safe list.
  1036. */
  1037. int data_size = sizeof(T);
  1038. int type_code = Get_Type_Code();
  1039. /*
  1040. ** Get_Type_ID will return the Type ID for this type code (instruction pointer). It will create a new type if this one
  1041. ** doesn't already exist.
  1042. */
  1043. int id = Get_Type_ID(type_code, data_size);
  1044. Type = id;
  1045. MinSlop = slopcount;
  1046. /*
  1047. ** Add some slop entries here. These are just bogues entries than can be swapped in memory with real entries. The less
  1048. ** commonly added data types should be give more slop entries.
  1049. */
  1050. for (int i=0 ; i<slopcount ; i++) {
  1051. /*
  1052. ** We need to make sure the constructor gets called for this object type before it's added to the safe. But we must
  1053. ** also make sure that the destructor *isn't* called when we go out of scope here - it will be called explicitly
  1054. ** when the datasafe is destructed. By using _alloca, and doing an in-place new, the memory that the object sits in
  1055. ** will be freed when it goes out of scope but the destructor for 'T' won't be called.
  1056. */
  1057. void *stackmem = _alloca(sizeof(T));
  1058. T *slop_ptr = new (stackmem) T;
  1059. Add_Entry(*slop_ptr, true);
  1060. }
  1061. }
  1062. /***********************************************************************************************
  1063. * DataSafeClass::~DataSafeClass -- Class destructor. Removes remaining slop entries. *
  1064. * *
  1065. * *
  1066. * *
  1067. * INPUT: Nothing *
  1068. * *
  1069. * OUTPUT: Nothing *
  1070. * *
  1071. * WARNINGS: None *
  1072. * *
  1073. * HISTORY: *
  1074. * 7/5/2001 11:38AM ST : Created *
  1075. *=============================================================================================*/
  1076. template <class T>
  1077. DataSafeClass<T>::~DataSafeClass(void)
  1078. {
  1079. ThreadLockClass locker;
  1080. /*
  1081. ** Remove all slop enries of our type from all lists. Note that this will delete non-slop entries too if any remain at this
  1082. ** time.
  1083. */
  1084. for (int i=0 ; i<NumLists ; i++) {
  1085. ds_assert(Safe[i] != NULL);
  1086. if (Safe[i] != NULL && Safe[i]->EntryType == Type && Safe[i]->EntryCount > 0) {
  1087. while (Safe[i]->EntryCount > 0) {
  1088. /*
  1089. ** Delete the first entry in the list.
  1090. */
  1091. int entries = Safe[i]->EntryCount;
  1092. DataSafeHandleClass handle = Safe[i]->SafeList->Handle ^ SimpleKey;
  1093. handle = handle ^ HandleKey;
  1094. Delete_Entry(handle);
  1095. /*
  1096. ** Safety check. If something goes wrong and we didn't delete the entry then we will never get out of this loop
  1097. ** so check for that case.
  1098. */
  1099. ds_assert(entries == Safe[i]->EntryCount+1);
  1100. if (entries != Safe[i]->EntryCount+1) {
  1101. break;
  1102. }
  1103. }
  1104. }
  1105. }
  1106. Shutdown();
  1107. }
  1108. /***********************************************************************************************
  1109. * DataSafeClass::Get_Type_Code -- Get a unique code per type without using RTTI *
  1110. * *
  1111. * *
  1112. * *
  1113. * INPUT: Nothing *
  1114. * *
  1115. * OUTPUT: Type code *
  1116. * *
  1117. * WARNINGS: None *
  1118. * *
  1119. * HISTORY: *
  1120. * 7/2/2001 11:17AM ST : Created *
  1121. *=============================================================================================*/
  1122. template <class T>
  1123. unsigned long DataSafeClass<T>::Get_Type_Code(void)
  1124. {
  1125. /*
  1126. ** Make sure this function gets expanded multiple times for different types by referencing the type.
  1127. */
  1128. volatile int data_size = sizeof(T);
  1129. data_size = data_size;
  1130. /*
  1131. ** Since we aren't using RTTI I need some other way of distinguishing types in the safe. Because it's templatised, this
  1132. ** code will get expanded once for each type it's used with. I will use the location in memory of the function to
  1133. ** uniquely identify each type. What a cunning plan.
  1134. */
  1135. static unsigned long instruction_pointer;
  1136. instruction_pointer = 0;
  1137. __asm {
  1138. here:
  1139. lea eax,here
  1140. mov [instruction_pointer],eax
  1141. };
  1142. ds_assert(instruction_pointer != 0);
  1143. return(instruction_pointer);
  1144. }
  1145. /***********************************************************************************************
  1146. * DataSafeClass::Get_Type_ID -- Get the id for the given type and size *
  1147. * *
  1148. * *
  1149. * *
  1150. * INPUT: Type identifier code *
  1151. * Size of type *
  1152. * *
  1153. * OUTPUT: Type ID *
  1154. * *
  1155. * WARNINGS: None *
  1156. * *
  1157. * HISTORY: *
  1158. * 6/27/2001 12:44PM ST : Created *
  1159. *=============================================================================================*/
  1160. template <class T>
  1161. int DataSafeClass<T>::Get_Type_ID(unsigned long type_code, int size)
  1162. {
  1163. int id = 0;
  1164. /*
  1165. ** See if we already have an ID for this type code.
  1166. */
  1167. for (int i=0 ; i<TypeListCount ; i++) {
  1168. if (TypeList[i].TypeCode == type_code) {
  1169. ds_assert(TypeList[i].Size == size);
  1170. return(TypeList[i].ID);
  1171. }
  1172. }
  1173. /*
  1174. ** Create a new type entry.
  1175. */
  1176. ds_assert(TypeListCount < MAX_DATASAFE_TYPES);
  1177. if (TypeListCount < MAX_DATASAFE_TYPES) {
  1178. ds_assert(TypeList[TypeListCount].TypeCode == 0);
  1179. ds_assert(TypeList[TypeListCount].Size == 0);
  1180. TypeList[TypeListCount].TypeCode = type_code;
  1181. TypeList[TypeListCount].ID = TypeListCount;
  1182. TypeList[TypeListCount].Size = size;
  1183. id = TypeListCount++;
  1184. }
  1185. return(id);
  1186. }
  1187. /***********************************************************************************************
  1188. * DataSafeClass::Add_Entry -- Create a new data safe entry and init it to the value provided *
  1189. * *
  1190. * *
  1191. * *
  1192. * INPUT: Value *
  1193. * *
  1194. * OUTPUT: Handle *
  1195. * *
  1196. * WARNINGS: None *
  1197. * *
  1198. * HISTORY: *
  1199. * 6/19/2001 8:34PM ST : Created *
  1200. *=============================================================================================*/
  1201. template <class T>
  1202. DataSafeHandleClass DataSafeClass<T>::Add_Entry(T &value, bool is_slop)
  1203. {
  1204. ThreadLockClass locker;
  1205. Security_Check();
  1206. /*
  1207. ** Allocate memory for the new entry.
  1208. */
  1209. DataSafeEntryClass *entry_ptr = (DataSafeEntryClass*) new char [sizeof(DataSafeEntryDataClass<T>)];
  1210. ds_assert(entry_ptr);
  1211. if (entry_ptr) {
  1212. /*
  1213. ** Init the data safe entry to reasonable values.
  1214. */
  1215. entry_ptr->Next = NULL;
  1216. entry_ptr->Prev = NULL;
  1217. entry_ptr->Size = sizeof(value);
  1218. entry_ptr->IsSlop = is_slop;
  1219. /*
  1220. ** Copy the data to the safe entry.
  1221. */
  1222. DataSafeEntryDataClass<T>* data_ptr = (DataSafeEntryDataClass<T>*) entry_ptr;
  1223. Mem_Copy_Encrypt(& data_ptr->Data, &value, sizeof(value), true);
  1224. /*
  1225. ** Find out which list we will be adding the entry to.
  1226. */
  1227. int list = Get_Random_List_For_Insertion(Type);
  1228. ds_assert(list >= 0 && list < MAX_DATASAFE_LISTS);
  1229. ds_assert(Safe[list] != NULL);
  1230. if (list != -1 && Safe[list] != NULL) {
  1231. /*
  1232. ** Now we know the list, we can get the next index id for that list and build a handle for the entry.
  1233. */
  1234. int index = Get_Handle_ID(list);
  1235. ds_assert(index >= 0 && index < MAX_ENTRIES_PER_LIST);
  1236. DataSafeHandleClass handle;
  1237. handle.Handle.Part.List = list;
  1238. handle.Handle.Part.ID = index;
  1239. handle.Handle.Part.Type = Type;
  1240. /*
  1241. ** We have a handle. Better encrypt it.
  1242. */
  1243. DataSafeHandleClass encrypted_handle = handle ^ SimpleKey;
  1244. Checksum = Checksum ^ encrypted_handle;
  1245. /*
  1246. ** Store it in the entry so we can do a search and match to get this entry out again.
  1247. */
  1248. entry_ptr->Handle = encrypted_handle;
  1249. /*
  1250. ** Insert the entry into the chosen list.
  1251. */
  1252. Random_Insertion(entry_ptr, list, Type, is_slop);
  1253. /*
  1254. ** Maybe add slop here if there is only one entry in the list.
  1255. ** It will get removed later as more entries are added.
  1256. */
  1257. if (Safe[list]->EntryCount == 1 && !is_slop) {
  1258. for (int i=0 ; i<MinSlop ; i++) {
  1259. entry_ptr = (DataSafeEntryClass*) new char [sizeof(DataSafeEntryDataClass<T>)];
  1260. ds_assert(entry_ptr);
  1261. if (entry_ptr) {
  1262. entry_ptr->Next = NULL;
  1263. entry_ptr->Prev = NULL;
  1264. entry_ptr->Size = sizeof(T);
  1265. entry_ptr->IsSlop = true;
  1266. /*
  1267. ** Encrypt the garbage data in the uninitialized memory we just allocated for the
  1268. ** object. It has to be encrypted to keep the CRC right.
  1269. */
  1270. Encrypt(&((DataSafeEntryDataClass<T>*)entry_ptr)->Data, sizeof(T), SimpleKey, true);
  1271. /*
  1272. ** Get a handle ID. Not really needed since we won't be returning a handle but it
  1273. ** keeps the ID usage list neat. Fill in the rest of the handle to make it valid.
  1274. */
  1275. DataSafeHandleClass slop_handle;
  1276. slop_handle.Handle.Part.ID = Get_Handle_ID(list);
  1277. slop_handle.Handle.Part.List = list;
  1278. slop_handle.Handle.Part.Type = Type;
  1279. DataSafeHandleClass encrypted_slop_handle = slop_handle ^ SimpleKey;
  1280. Checksum = Checksum ^ encrypted_slop_handle;
  1281. entry_ptr->Handle = encrypted_slop_handle;
  1282. /*
  1283. ** Insert the slop entry.
  1284. */
  1285. Random_Insertion(entry_ptr, list, Type, true);
  1286. }
  1287. }
  1288. ds_assert(Safe[list]->SlopCount == MinSlop);
  1289. ds_assert(Safe[list]->EntryCount == MinSlop + 1);
  1290. }
  1291. /*
  1292. ** Maybe remove some slop if the list is filling up.
  1293. */
  1294. if (Safe[list]->SlopCount != 0 && Safe[list]->EntryCount > Safe[list]->SlopCount * 2 && !is_slop) {
  1295. /*
  1296. ** Lots of entries here. Remove all slop by building a list of slop entries then
  1297. ** removing them one by one.
  1298. */
  1299. entry_ptr = Safe[list]->SafeList;
  1300. ds_assert(entry_ptr != NULL);
  1301. DataSafeHandleClass removal_list[MAX_ENTRIES_PER_LIST];
  1302. int removal_count = 0;
  1303. if (entry_ptr) {
  1304. /*
  1305. ** Go through each entry in the list.
  1306. */
  1307. for (int j=0 ; j<Safe[list]->EntryCount ; j++) {
  1308. /*
  1309. ** If this is a slop entry then mark it for removal.
  1310. */
  1311. if (entry_ptr->IsSlop) {
  1312. DataSafeHandleClass decode_handle = entry_ptr->Handle ^ SimpleKey;
  1313. decode_handle = decode_handle ^ HandleKey;
  1314. removal_list[removal_count++] = decode_handle;
  1315. }
  1316. /*
  1317. ** Next entry.
  1318. */
  1319. entry_ptr = entry_ptr->Next;
  1320. }
  1321. }
  1322. /*
  1323. ** Remove all marked entries. Delete_Entry will fix up the lists slop count.
  1324. */
  1325. for (int j=0 ; j<removal_count ; j++) {
  1326. Delete_Entry(removal_list[j]);
  1327. }
  1328. ds_assert(Safe[list]->SlopCount == 0);
  1329. }
  1330. /*
  1331. ** Return the handle, encrypted using the handle key.
  1332. */
  1333. handle.Handle.Whole.WholeHandle ^= HandleKey;
  1334. return(handle);
  1335. }
  1336. }
  1337. /*
  1338. ** Allocation error.
  1339. */
  1340. return((DataSafeHandleClass)-1);
  1341. }
  1342. /***********************************************************************************************
  1343. * DataSafeClass::Delete_Entry -- Delete an entry from the data safe *
  1344. * *
  1345. * *
  1346. * *
  1347. * INPUT: Handle to entry *
  1348. * *
  1349. * OUTPUT: Nothing *
  1350. * *
  1351. * WARNINGS: None *
  1352. * *
  1353. * HISTORY: *
  1354. * 6/25/2001 1:32PM ST : Created *
  1355. *=============================================================================================*/
  1356. template <class T>
  1357. void DataSafeClass<T>::Delete_Entry(DataSafeHandleClass handle)
  1358. {
  1359. /*
  1360. ** Locals.
  1361. */
  1362. ThreadLockClass locker;
  1363. int list;
  1364. int id;
  1365. /*
  1366. ** Asserts.
  1367. */
  1368. ds_assert(handle.Is_Valid());
  1369. /*
  1370. ** Check safe integrity.
  1371. */
  1372. Security_Check();
  1373. /*
  1374. ** The handles we give out are encrypted so we need to decrypt temporarily to extract the list number.
  1375. */
  1376. DataSafeHandleClass new_handle = handle ^ HandleKey;
  1377. list = new_handle.Handle.Part.List;
  1378. id = new_handle.Handle.Part.ID;
  1379. /*
  1380. ** Check that list number.
  1381. */
  1382. ds_assert(list >= 0);
  1383. ds_assert(list < NumLists);
  1384. ds_assert(Safe[list] != NULL);
  1385. /*
  1386. ** Get a pointer to the actual entry in the safe list.
  1387. */
  1388. DataSafeEntryClass *entry_ptr = Get_Entry(handle);
  1389. ds_assert(entry_ptr != NULL);
  1390. if (entry_ptr != NULL) {
  1391. /*
  1392. ** Apply the current data key to the handle to fix up the checksum.
  1393. */
  1394. DataSafeHandleClass match_handle = new_handle ^ SimpleKey;
  1395. Checksum ^= match_handle;
  1396. /*
  1397. ** We found the entry for this handle. We need to fix up the list pointers and delete the memory.
  1398. ** Since we originally memcpy'ed this object into place we will have to call the destructor explicitly. The explicit
  1399. ** destructor call will have no effect if the object doesn't have a destructor defined.
  1400. **
  1401. ** We need to decrypt the object in place before calling the desturctor otherwise it will have a hard time acting on
  1402. ** it's own data. This has the bonus of fixing up the checksum too.
  1403. */
  1404. DataSafeEntryDataClass<T> *whole_entry_ptr = (DataSafeEntryDataClass<T>*) entry_ptr;
  1405. Decrypt(&whole_entry_ptr->Data, Get_Type_Size(Safe[list]->EntryType), SimpleKey);
  1406. if (!whole_entry_ptr->IsSlop) {
  1407. whole_entry_ptr->Data.T::~T();
  1408. }
  1409. Remove_From_List(list, entry_ptr);
  1410. Free_Handle_ID(list, id);
  1411. delete [] (void*) entry_ptr;
  1412. return;
  1413. }
  1414. /*
  1415. ** No match found.
  1416. */
  1417. WWDEBUG_SAY(("Datasafe: No item found for handle %08x\n", (int)handle));
  1418. }
  1419. /***********************************************************************************************
  1420. * DataSafeClass::Get -- Get a copy of an item in the data safe. *
  1421. * *
  1422. * *
  1423. * *
  1424. * INPUT: Handle to data safe entry *
  1425. * (out) Pointer to copy of object *
  1426. * *
  1427. * OUTPUT: True if object retrieved OK *
  1428. * *
  1429. * WARNINGS: None *
  1430. * *
  1431. * HISTORY: *
  1432. * 7/2/2001 11:07AM ST : Created *
  1433. *=============================================================================================*/
  1434. template <class T>
  1435. bool DataSafeClass<T>::Get(DataSafeHandleClass handle, T* &item)
  1436. {
  1437. ThreadLockClass locker;
  1438. /*
  1439. ** Get a pointer to the entry.
  1440. */
  1441. DataSafeEntryClass *entry_ptr = Get_Entry(handle);
  1442. if (entry_ptr == NULL) {
  1443. return(false);
  1444. }
  1445. DataSafeEntryDataClass<T> *data_entry_ptr = (DataSafeEntryDataClass<T>*) entry_ptr;
  1446. /*
  1447. ** Copy the object from the data safe and decrypt it in one pass. Don't adjust the checksum as the object is staying
  1448. ** the same in the safe.
  1449. */
  1450. Mem_Copy_Decrypt(&ReturnList[ReturnIndex][0], &data_entry_ptr->Data, sizeof(T), false);
  1451. /*
  1452. ** Return the object.
  1453. */
  1454. item = (T*)(&ReturnList[ReturnIndex][0]);
  1455. /*
  1456. ** Fix up the return list index.
  1457. */
  1458. ReturnIndex++;
  1459. if (ReturnIndex >= MAX_OBJECT_COPIES) {
  1460. ReturnIndex = 0;
  1461. }
  1462. /*
  1463. ** Move stuff around.
  1464. */
  1465. Shuffle();
  1466. /*
  1467. ** Check safe integrity.
  1468. */
  1469. Security_Check();
  1470. return(true);
  1471. }
  1472. /***********************************************************************************************
  1473. * DataSafeClass::Set -- Set the value of an entry in the data safe *
  1474. * *
  1475. * *
  1476. * *
  1477. * INPUT: Handle to data in the safe *
  1478. * New value to set *
  1479. * *
  1480. * OUTPUT: True if handle was valid *
  1481. * *
  1482. * WARNINGS: None *
  1483. * *
  1484. * HISTORY: *
  1485. * 7/3/2001 12:11PM ST : Created *
  1486. *=============================================================================================*/
  1487. template <class T>
  1488. bool DataSafeClass<T>::Set(DataSafeHandleClass handle, T *data)
  1489. {
  1490. ThreadLockClass locker;
  1491. /*
  1492. ** Get a pointer to the entry.
  1493. */
  1494. DataSafeEntryClass *entry_ptr = Get_Entry(handle);
  1495. if (entry_ptr == NULL) {
  1496. return(false);
  1497. }
  1498. DataSafeEntryDataClass<T> *data_entry_ptr = (DataSafeEntryDataClass<T>*) entry_ptr;
  1499. /*
  1500. ** Decrypt the old entry just to fix up the checksum.
  1501. */
  1502. Decrypt(&data_entry_ptr->Data, sizeof(T), SimpleKey, true);
  1503. /*
  1504. ** Copy the object into the data safe and encrypt it in one pass.
  1505. */
  1506. Mem_Copy_Encrypt(&data_entry_ptr->Data, data, sizeof(T), true);
  1507. /*
  1508. ** Move stuff around.
  1509. */
  1510. Shuffle();
  1511. /*
  1512. ** Check safe integrity.
  1513. */
  1514. Security_Check();
  1515. return(true);
  1516. }
  1517. /*
  1518. **
  1519. **
  1520. ** SafeDataClass functions.
  1521. **
  1522. **
  1523. */
  1524. /***********************************************************************************************
  1525. * SafeDataClass::SafeDataClass -- Class constructor. Adds the data to the data safe *
  1526. * *
  1527. * *
  1528. * *
  1529. * INPUT: Initial value to store in the safe *
  1530. * *
  1531. * OUTPUT: Nothing *
  1532. * *
  1533. * WARNINGS: None *
  1534. * *
  1535. * HISTORY: *
  1536. * 7/3/2001 11:50AM ST : Created *
  1537. *=============================================================================================*/
  1538. template <class T>
  1539. inline SafeDataClass<T>::SafeDataClass(T data)
  1540. {
  1541. Handle = DataSafeClass<T>::Add_Entry(data);
  1542. #ifdef WWDEBUG
  1543. DebugData = data;
  1544. #endif //WWDEBUG
  1545. }
  1546. /***********************************************************************************************
  1547. * SafeDataClass::SafeDataClass -- Class constructor. Adds the fetches a safe handle *
  1548. * *
  1549. * *
  1550. * *
  1551. * INPUT: Nothing *
  1552. * *
  1553. * OUTPUT: Nothing *
  1554. * *
  1555. * WARNINGS: None *
  1556. * *
  1557. * HISTORY: *
  1558. * 7/3/2001 11:50AM ST : Created *
  1559. *=============================================================================================*/
  1560. //warning C4700: local variable 'data' used without having been initialized
  1561. //#pragma warning(push, 0)
  1562. //#pragma warning(disable : 4700)
  1563. template <class T>
  1564. SafeDataClass<T>::SafeDataClass(void)
  1565. {
  1566. /*
  1567. ** The data here isn't initialised since no default value was provided. I have to provide *some* sort of
  1568. ** init though as the bloody compiler won't let me turn off the 'local variable 'data' used without having been initialized'
  1569. ** message. Grrrr.
  1570. **
  1571. ** Allocating the memory on the stack then doing an in place new seems to mollify the compiler so we don't get the warning
  1572. ** and it does much the same thing as just declaring 'T data'. It also has the benefit that the destructor won't be called
  1573. ** for the slop object when we exit this function but the memory will be freed.
  1574. */
  1575. void *stackmem = _alloca(sizeof(T));
  1576. T *data = new (stackmem) T;
  1577. /*
  1578. ** Add the entry to the data safe and note the handle.
  1579. */
  1580. Handle = DataSafeClass<T>::Add_Entry(*data);
  1581. #ifdef WWDEBUG
  1582. DebugData = *data;
  1583. #endif //WWDEBUG
  1584. }
  1585. //#pragma warning(pop)
  1586. /***********************************************************************************************
  1587. * SafeDataClass::~SafeDataClass -- Class desturctor. Removes the data from the data safe *
  1588. * *
  1589. * *
  1590. * *
  1591. * INPUT: Nothing *
  1592. * *
  1593. * OUTPUT: Nothing *
  1594. * *
  1595. * WARNINGS: None *
  1596. * *
  1597. * HISTORY: *
  1598. * 7/3/2001 11:51AM ST : Created *
  1599. *=============================================================================================*/
  1600. template <class T>
  1601. inline SafeDataClass<T>::~SafeDataClass(void)
  1602. {
  1603. if (Handle.Is_Valid()) {
  1604. DataSafeClass<T>::Delete_Entry(Handle);
  1605. }
  1606. }
  1607. /***********************************************************************************************
  1608. * SafeDataClass::operator = - assignment operator. *
  1609. * *
  1610. * *
  1611. * *
  1612. * INPUT: Data to assign *
  1613. * *
  1614. * OUTPUT: Data assigned *
  1615. * *
  1616. * WARNINGS: None *
  1617. * *
  1618. * HISTORY: *
  1619. * 7/3/2001 12:05PM ST : Created *
  1620. *=============================================================================================*/
  1621. template <class T>
  1622. inline T &SafeDataClass<T>::operator = (T const &data)
  1623. {
  1624. /*
  1625. ** If we have a valid handle, then set the data into the data safe.
  1626. */
  1627. if (Handle.Is_Valid()) {
  1628. #ifdef WWDEBUG
  1629. bool ok =
  1630. #endif //WWDEBUG
  1631. DataSafeClass<T>::Set(Handle, (T*) &data);
  1632. ds_assert(ok);
  1633. #ifdef WWDEBUG
  1634. DebugData = data;
  1635. #endif //WWDEBUG
  1636. }
  1637. return((T&)data);
  1638. }
  1639. /***********************************************************************************************
  1640. * SafeDataClass::operator = - assignment operator for assigning from other SafeDataClass's *
  1641. * *
  1642. * *
  1643. * *
  1644. * INPUT: Data to assign *
  1645. * *
  1646. * OUTPUT: Data assigned *
  1647. * *
  1648. * WARNINGS: None *
  1649. * *
  1650. * HISTORY: *
  1651. * 7/3/2001 12:05PM ST : Created *
  1652. *=============================================================================================*/
  1653. template <class T>
  1654. inline T &SafeDataClass<T>::operator = (SafeDataClass<T> &safedata)
  1655. {
  1656. /*
  1657. ** Locals.
  1658. */
  1659. T *other_value = NULL;
  1660. /*
  1661. ** Asserts.
  1662. */
  1663. ds_assert(Handle.Is_Valid());
  1664. ds_assert(safedata.Get_Handle().Is_Valid());
  1665. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safedata.Get_Handle()));
  1666. /*
  1667. ** If we have a valid handle, then get the data for this handle from the data safe.
  1668. */
  1669. if (Handle.Is_Valid()) {
  1670. /*
  1671. ** Get the value for the other safe data class
  1672. ** and set it into the safe entry with our handle.
  1673. */
  1674. other_value = safedata.Get_Ptr();
  1675. ds_assert(other_value != NULL);
  1676. if (other_value) {
  1677. #ifdef WWDEBUG
  1678. bool ok =
  1679. #endif //WWDEBUG
  1680. DataSafeClass<T>::Set(Handle, (T*) other_value);
  1681. ds_assert(ok);
  1682. #ifdef WWDEBUG
  1683. DebugData = *other_value;
  1684. #endif //WWDEBUG
  1685. } else {
  1686. /*
  1687. ** Bad error case, have to return something so use the value we already have.
  1688. */
  1689. return((T&)*other_value);
  1690. }
  1691. }
  1692. return((T&)*other_value);
  1693. }
  1694. /***********************************************************************************************
  1695. * SafeDataClass::operator == - Equality check operator. *
  1696. * *
  1697. * *
  1698. * *
  1699. * INPUT: Data to compare against *
  1700. * *
  1701. * OUTPUT: true if equal *
  1702. * *
  1703. * WARNINGS: None *
  1704. * *
  1705. * HISTORY: *
  1706. * 7/3/2001 12:05PM ST : Created *
  1707. *=============================================================================================*/
  1708. template <class T>
  1709. inline bool SafeDataClass<T>::operator == (T const &data)
  1710. {
  1711. T *data_ptr = NULL;
  1712. /*
  1713. ** If we have a valid handle, then check the value against the supplied data.
  1714. */
  1715. if (Handle.Is_Valid()) {
  1716. #ifdef WWDEBUG
  1717. bool ok =
  1718. #endif //WWDEBUG
  1719. DataSafeClass<T>::Get(Handle, data_ptr);
  1720. ds_assert(ok);
  1721. ds_assert(data_ptr);
  1722. if (data_ptr) {
  1723. return(*data_ptr == data);
  1724. }
  1725. }
  1726. return(false);
  1727. }
  1728. /***********************************************************************************************
  1729. * SafeDataClass::operator == - Equality check operator. *
  1730. * *
  1731. * *
  1732. * *
  1733. * INPUT: Safe Data to compare against *
  1734. * *
  1735. * OUTPUT: true if equal *
  1736. * *
  1737. * WARNINGS: None *
  1738. * *
  1739. * HISTORY: *
  1740. * 7/3/2001 12:05PM ST : Created *
  1741. *=============================================================================================*/
  1742. template <class T>
  1743. inline bool SafeDataClass<T>::operator == (SafeDataClass<T> &safedata)
  1744. {
  1745. /*
  1746. ** Locals.
  1747. */
  1748. T *data_ptr = NULL;
  1749. T *other_value = NULL;
  1750. /*
  1751. ** Asserts.
  1752. */
  1753. ds_assert(Handle.Is_Valid());
  1754. ds_assert(safedata.Get_Handle().Is_Valid());
  1755. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safedata.Get_Handle()));
  1756. /*
  1757. ** If we have a valid handle, then check the value against the supplied data.
  1758. */
  1759. if (Handle.Is_Valid()) {
  1760. #ifdef WWDEBUG
  1761. bool ok =
  1762. #endif //WWDEBUG
  1763. DataSafeClass<T>::Get(Handle, data_ptr);
  1764. ds_assert(ok);
  1765. ds_assert(data_ptr);
  1766. if (data_ptr) {
  1767. /*
  1768. ** Looks like we have the value for this safe data class. Get the value for the other one.
  1769. */
  1770. other_value = safedata.Get_Ptr();
  1771. ds_assert(other_value != NULL);
  1772. if (other_value) {
  1773. /*
  1774. ** Check them against each other,
  1775. */
  1776. return(*data_ptr == *other_value);
  1777. }
  1778. }
  1779. }
  1780. return(false);
  1781. }
  1782. /***********************************************************************************************
  1783. * SafeDataClass::operator != - Inequality check operator. *
  1784. * *
  1785. * *
  1786. * *
  1787. * INPUT: Data to compare against *
  1788. * *
  1789. * OUTPUT: true if not equal *
  1790. * *
  1791. * WARNINGS: None *
  1792. * *
  1793. * HISTORY: *
  1794. * 7/3/2001 12:05PM ST : Created *
  1795. *=============================================================================================*/
  1796. template <class T>
  1797. inline bool SafeDataClass<T>::operator != (T const &data)
  1798. {
  1799. T *data_ptr = NULL;
  1800. /*
  1801. ** If we have a valid handle, then check the value against the supplied data.
  1802. */
  1803. if (Handle.Is_Valid()) {
  1804. #ifdef WWDEBUG
  1805. bool ok =
  1806. #endif //WWDEBUG
  1807. DataSafeClass<T>::Get(Handle, data_ptr);
  1808. ds_assert(ok);
  1809. ds_assert(data_ptr);
  1810. if (data_ptr) {
  1811. return(*data_ptr != data);
  1812. }
  1813. }
  1814. return(true);
  1815. }
  1816. /***********************************************************************************************
  1817. * SafeDataClass::operator != - Inequality check operator. *
  1818. * *
  1819. * *
  1820. * *
  1821. * INPUT: Data to compare against *
  1822. * *
  1823. * OUTPUT: true if not equal *
  1824. * *
  1825. * WARNINGS: None *
  1826. * *
  1827. * HISTORY: *
  1828. * 7/3/2001 12:05PM ST : Created *
  1829. *=============================================================================================*/
  1830. template <class T>
  1831. inline bool SafeDataClass<T>::operator != (SafeDataClass<T> &safedata)
  1832. {
  1833. /*
  1834. ** Locals.
  1835. */
  1836. T *data_ptr = NULL;
  1837. T *other_value = NULL;
  1838. /*
  1839. ** Asserts.
  1840. */
  1841. ds_assert(Handle.Is_Valid());
  1842. ds_assert(safedata.Get_Handle().Is_Valid());
  1843. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safedata.Get_Handle()));
  1844. /*
  1845. ** If we have a valid handle, then check the value against the supplied data.
  1846. */
  1847. if (Handle.Is_Valid()) {
  1848. #ifdef WWDEBUG
  1849. bool ok =
  1850. #endif //WWDEBUG
  1851. DataSafeClass<T>::Get(Handle, data_ptr);
  1852. ds_assert(ok);
  1853. ds_assert(data_ptr);
  1854. if (data_ptr) {
  1855. /*
  1856. ** Looks like we have the value for this safe data class. Get the value for the other one.
  1857. */
  1858. other_value = safedata.Get_Ptr();
  1859. ds_assert(other_value != NULL);
  1860. if (other_value) {
  1861. /*
  1862. ** Check them against each other,
  1863. */
  1864. return(*data_ptr != *other_value);
  1865. }
  1866. }
  1867. }
  1868. return(false);
  1869. }
  1870. /***********************************************************************************************
  1871. * SafeDataClass::operator > - > operator. *
  1872. * *
  1873. * *
  1874. * *
  1875. * INPUT: Data to compare against *
  1876. * *
  1877. * OUTPUT: true if greater than *
  1878. * *
  1879. * WARNINGS: None *
  1880. * *
  1881. * HISTORY: *
  1882. * 7/3/2001 12:05PM ST : Created *
  1883. *=============================================================================================*/
  1884. template <class T>
  1885. inline bool SafeDataClass<T>::operator > (T const &data)
  1886. {
  1887. T *data_ptr = NULL;
  1888. /*
  1889. ** If we have a valid handle, then check the value against the supplied data.
  1890. */
  1891. if (Handle.Is_Valid()) {
  1892. #ifdef WWDEBUG
  1893. bool ok =
  1894. #endif //WWDEBUG
  1895. DataSafeClass<T>::Get(Handle, data_ptr);
  1896. ds_assert(ok);
  1897. ds_assert(data_ptr);
  1898. if (data_ptr) {
  1899. return(*data_ptr > data);
  1900. }
  1901. }
  1902. return(false);
  1903. }
  1904. /***********************************************************************************************
  1905. * SafeDataClass::operator > - > operator. *
  1906. * *
  1907. * *
  1908. * *
  1909. * INPUT: Safe Data to compare against *
  1910. * *
  1911. * OUTPUT: true if greater than *
  1912. * *
  1913. * WARNINGS: None *
  1914. * *
  1915. * HISTORY: *
  1916. * 7/3/2001 12:05PM ST : Created *
  1917. *=============================================================================================*/
  1918. template <class T>
  1919. inline bool SafeDataClass<T>::operator > (SafeDataClass<T> &safedata)
  1920. {
  1921. /*
  1922. ** Locals.
  1923. */
  1924. T *data_ptr = NULL;
  1925. T *other_value = NULL;
  1926. /*
  1927. ** Asserts.
  1928. */
  1929. ds_assert(Handle.Is_Valid());
  1930. ds_assert(safedata.Get_Handle().Is_Valid());
  1931. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safedata.Get_Handle()));
  1932. /*
  1933. ** If we have a valid handle, then check the value against the supplied data.
  1934. */
  1935. if (Handle.Is_Valid()) {
  1936. #ifdef WWDEBUG
  1937. bool ok =
  1938. #endif //WWDEBUG
  1939. DataSafeClass<T>::Get(Handle, data_ptr);
  1940. ds_assert(ok);
  1941. ds_assert(data_ptr);
  1942. if (data_ptr) {
  1943. /*
  1944. ** Looks like we have the value for this safe data class. Get the value for the other one.
  1945. */
  1946. other_value = safedata.Get_Ptr();
  1947. ds_assert(other_value != NULL);
  1948. if (other_value) {
  1949. /*
  1950. ** Check them against each other,
  1951. */
  1952. return(*data_ptr > *other_value);
  1953. }
  1954. }
  1955. }
  1956. return(false);
  1957. }
  1958. /***********************************************************************************************
  1959. * SafeDataClass::operator >= - >= operator. *
  1960. * *
  1961. * *
  1962. * *
  1963. * INPUT: Data to compare against *
  1964. * *
  1965. * OUTPUT: true if greater than or equal *
  1966. * *
  1967. * WARNINGS: None *
  1968. * *
  1969. * HISTORY: *
  1970. * 7/3/2001 12:05PM ST : Created *
  1971. *=============================================================================================*/
  1972. template <class T>
  1973. inline bool SafeDataClass<T>::operator >= (T const &data)
  1974. {
  1975. T *data_ptr = NULL;
  1976. /*
  1977. ** If we have a valid handle, then check the value against the supplied data.
  1978. */
  1979. if (Handle.Is_Valid()) {
  1980. #ifdef WWDEBUG
  1981. bool ok =
  1982. #endif //WWDEBUG
  1983. DataSafeClass<T>::Get(Handle, data_ptr);
  1984. ds_assert(ok);
  1985. ds_assert(data_ptr);
  1986. if (data_ptr) {
  1987. return(*data_ptr >= data);
  1988. }
  1989. }
  1990. return(false);
  1991. }
  1992. /***********************************************************************************************
  1993. * SafeDataClass::operator >= - >= operator. *
  1994. * *
  1995. * *
  1996. * *
  1997. * INPUT: Safe Data to compare against *
  1998. * *
  1999. * OUTPUT: true if greater than or equal *
  2000. * *
  2001. * WARNINGS: None *
  2002. * *
  2003. * HISTORY: *
  2004. * 7/3/2001 12:05PM ST : Created *
  2005. *=============================================================================================*/
  2006. template <class T>
  2007. inline bool SafeDataClass<T>::operator >= (SafeDataClass<T> &safedata)
  2008. {
  2009. /*
  2010. ** Locals.
  2011. */
  2012. T *data_ptr = NULL;
  2013. T *other_value = NULL;
  2014. /*
  2015. ** Asserts.
  2016. */
  2017. ds_assert(Handle.Is_Valid());
  2018. ds_assert(safedata.Get_Handle().Is_Valid());
  2019. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safedata.Get_Handle()));
  2020. /*
  2021. ** If we have a valid handle, then check the value against the supplied data.
  2022. */
  2023. if (Handle.Is_Valid()) {
  2024. #ifdef WWDEBUG
  2025. bool ok =
  2026. #endif //WWDEBUG
  2027. DataSafeClass<T>::Get(Handle, data_ptr);
  2028. ds_assert(ok);
  2029. ds_assert(data_ptr);
  2030. if (data_ptr) {
  2031. /*
  2032. ** Looks like we have the value for this safe data class. Get the value for the other one.
  2033. */
  2034. other_value = safedata.Get_Ptr();
  2035. ds_assert(other_value != NULL);
  2036. if (other_value) {
  2037. /*
  2038. ** Check them against each other,
  2039. */
  2040. return(*data_ptr >= *other_value);
  2041. }
  2042. }
  2043. }
  2044. return(false);
  2045. }
  2046. /***********************************************************************************************
  2047. * SafeDataClass::operator < - < operator. *
  2048. * *
  2049. * *
  2050. * *
  2051. * INPUT: Data to compare against *
  2052. * *
  2053. * OUTPUT: true if less than *
  2054. * *
  2055. * WARNINGS: None *
  2056. * *
  2057. * HISTORY: *
  2058. * 7/3/2001 12:05PM ST : Created *
  2059. *=============================================================================================*/
  2060. template <class T>
  2061. inline bool SafeDataClass<T>::operator < (T const &data)
  2062. {
  2063. T *data_ptr = NULL;
  2064. /*
  2065. ** If we have a valid handle, then check the value against the supplied data.
  2066. */
  2067. if (Handle.Is_Valid()) {
  2068. #ifdef WWDEBUG
  2069. bool ok =
  2070. #endif //WWDEBUG
  2071. DataSafeClass<T>::Get(Handle, data_ptr);
  2072. ds_assert(ok);
  2073. ds_assert(data_ptr);
  2074. if (data_ptr) {
  2075. return(*data_ptr < data);
  2076. }
  2077. }
  2078. return(false);
  2079. }
  2080. /***********************************************************************************************
  2081. * SafeDataClass::operator < - < operator. *
  2082. * *
  2083. * *
  2084. * *
  2085. * INPUT: Safe Data to compare against *
  2086. * *
  2087. * OUTPUT: true if less than *
  2088. * *
  2089. * WARNINGS: None *
  2090. * *
  2091. * HISTORY: *
  2092. * 7/3/2001 12:05PM ST : Created *
  2093. *=============================================================================================*/
  2094. template <class T>
  2095. inline bool SafeDataClass<T>::operator < (SafeDataClass<T> &safedata)
  2096. {
  2097. /*
  2098. ** Locals.
  2099. */
  2100. T *data_ptr = NULL;
  2101. T *other_value = NULL;
  2102. /*
  2103. ** Asserts.
  2104. */
  2105. ds_assert(Handle.Is_Valid());
  2106. ds_assert(safedata.Get_Handle().Is_Valid());
  2107. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safedata.Get_Handle()));
  2108. /*
  2109. ** If we have a valid handle, then check the value against the supplied data.
  2110. */
  2111. if (Handle.Is_Valid()) {
  2112. #ifdef WWDEBUG
  2113. bool ok =
  2114. #endif //WWDEBUG
  2115. DataSafeClass<T>::Get(Handle, data_ptr);
  2116. ds_assert(ok);
  2117. ds_assert(data_ptr);
  2118. if (data_ptr) {
  2119. /*
  2120. ** Looks like we have the value for this safe data class. Get the value for the other one.
  2121. */
  2122. other_value = safedata.Get_Ptr();
  2123. ds_assert(other_value != NULL);
  2124. if (other_value) {
  2125. /*
  2126. ** Check them against each other,
  2127. */
  2128. return(*data_ptr < *other_value);
  2129. }
  2130. }
  2131. }
  2132. return(false);
  2133. }
  2134. /***********************************************************************************************
  2135. * SafeDataClass::operator <= - <= operator. *
  2136. * *
  2137. * *
  2138. * *
  2139. * INPUT: Data to compare against *
  2140. * *
  2141. * OUTPUT: true if less than or equal *
  2142. * *
  2143. * WARNINGS: None *
  2144. * *
  2145. * HISTORY: *
  2146. * 7/3/2001 12:05PM ST : Created *
  2147. *=============================================================================================*/
  2148. template <class T>
  2149. inline bool SafeDataClass<T>::operator <= (T const &data)
  2150. {
  2151. T *data_ptr = NULL;
  2152. /*
  2153. ** If we have a valid handle, then check the value against the supplied data.
  2154. */
  2155. if (Handle.Is_Valid()) {
  2156. #ifdef WWDEBUG
  2157. bool ok =
  2158. #endif //WWDEBUG
  2159. DataSafeClass<T>::Get(Handle, data_ptr);
  2160. ds_assert(ok);
  2161. ds_assert(data_ptr);
  2162. if (data_ptr) {
  2163. return(*data_ptr <= data);
  2164. }
  2165. }
  2166. return(false);
  2167. }
  2168. /***********************************************************************************************
  2169. * SafeDataClass::operator <= - <= operator. *
  2170. * *
  2171. * *
  2172. * *
  2173. * INPUT: Safe Data to compare against *
  2174. * *
  2175. * OUTPUT: true if less than or equal *
  2176. * *
  2177. * WARNINGS: None *
  2178. * *
  2179. * HISTORY: *
  2180. * 7/3/2001 12:05PM ST : Created *
  2181. *=============================================================================================*/
  2182. template <class T>
  2183. inline bool SafeDataClass<T>::operator <= (SafeDataClass<T> &safedata)
  2184. {
  2185. /*
  2186. ** Locals.
  2187. */
  2188. T *data_ptr = NULL;
  2189. T *other_value = NULL;
  2190. /*
  2191. ** Asserts.
  2192. */
  2193. ds_assert(Handle.Is_Valid());
  2194. ds_assert(safedata.Get_Handle().Is_Valid());
  2195. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safedata.Get_Handle()));
  2196. /*
  2197. ** If we have a valid handle, then check the value against the supplied data.
  2198. */
  2199. if (Handle.Is_Valid()) {
  2200. #ifdef WWDEBUG
  2201. bool ok =
  2202. #endif //WWDEBUG
  2203. DataSafeClass<T>::Get(Handle, data_ptr);
  2204. ds_assert(ok);
  2205. ds_assert(data_ptr);
  2206. if (data_ptr) {
  2207. /*
  2208. ** Looks like we have the value for this safe data class. Get the value for the other one.
  2209. */
  2210. other_value = safedata.Get_Ptr();
  2211. ds_assert(other_value != NULL);
  2212. if (other_value) {
  2213. /*
  2214. ** Check them against each other,
  2215. */
  2216. return(*data_ptr <= *other_value);
  2217. }
  2218. }
  2219. }
  2220. return(false);
  2221. }
  2222. /***********************************************************************************************
  2223. * SafeDataClass::operator + - + operator. *
  2224. * *
  2225. * *
  2226. * *
  2227. * INPUT: Data to add *
  2228. * *
  2229. * OUTPUT: Result *
  2230. * *
  2231. * WARNINGS: None *
  2232. * *
  2233. * HISTORY: *
  2234. * 7/3/2001 12:05PM ST : Created *
  2235. *=============================================================================================*/
  2236. template <class T>
  2237. inline T &SafeDataClass<T>::operator + (T const &value)
  2238. {
  2239. /*
  2240. ** Locals.
  2241. */
  2242. T *data_ptr = NULL;
  2243. /*
  2244. ** Asserts.
  2245. */
  2246. ds_assert(Handle.Is_Valid());
  2247. /*
  2248. ** If we have a valid handle, then get the current value and apply the change
  2249. */
  2250. if (Handle.Is_Valid()) {
  2251. #ifdef WWDEBUG
  2252. bool ok =
  2253. #endif //WWDEBUG
  2254. DataSafeClass<T>::Get(Handle, data_ptr);
  2255. ds_assert(ok);
  2256. ds_assert(data_ptr);
  2257. if (data_ptr) {
  2258. *data_ptr = *data_ptr + value;
  2259. return((T&)*data_ptr);
  2260. }
  2261. }
  2262. /*
  2263. ** Error case, have to return something.
  2264. */
  2265. return((T&)ErrorVal[0]);
  2266. }
  2267. /***********************************************************************************************
  2268. * SafeDataClass::operator + - + operator *
  2269. * *
  2270. * *
  2271. * *
  2272. * INPUT: Data to add *
  2273. * *
  2274. * OUTPUT: Result *
  2275. * *
  2276. * WARNINGS: None *
  2277. * *
  2278. * HISTORY: *
  2279. * 7/3/2001 12:05PM ST : Created *
  2280. *=============================================================================================*/
  2281. template <class T>
  2282. inline T &SafeDataClass<T>::operator + (SafeDataClass<T> &safevalue)
  2283. {
  2284. /*
  2285. ** Locals.
  2286. */
  2287. T *data_ptr = NULL;
  2288. T *other_value = NULL;
  2289. /*
  2290. ** Asserts.
  2291. */
  2292. ds_assert(Handle.Is_Valid());
  2293. ds_assert(safevalue.Get_Handle().Is_Valid());
  2294. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safevalue.Get_Handle()));
  2295. /*
  2296. ** If we have a valid handle, then get the data for this handle from the data safe.
  2297. */
  2298. if (Handle.Is_Valid()) {
  2299. #ifdef WWDEBUG
  2300. bool ok =
  2301. #endif //WWDEBUG
  2302. DataSafeClass<T>::Get(Handle, data_ptr);
  2303. ds_assert(ok);
  2304. ds_assert(data_ptr);
  2305. if (data_ptr) {
  2306. /*
  2307. ** Looks like we have the value for this safe data class. Get the value for the other one.
  2308. ** and do the math.
  2309. */
  2310. other_value = safevalue.Get_Ptr();
  2311. ds_assert(other_value != NULL);
  2312. if (other_value) {
  2313. *data_ptr = *data_ptr + *other_value;
  2314. return((T&)*data_ptr);
  2315. } else {
  2316. /*
  2317. ** Bad error case, have to return something so use the value we already have.
  2318. */
  2319. return((T&)*data_ptr);
  2320. }
  2321. }
  2322. }
  2323. /*
  2324. ** Error case, have to return something.
  2325. */
  2326. return((T&)ErrorVal[0]);
  2327. }
  2328. /***********************************************************************************************
  2329. * SafeDataClass::operator += - += operator. *
  2330. * *
  2331. * *
  2332. * *
  2333. * INPUT: Data to add *
  2334. * *
  2335. * OUTPUT: Result *
  2336. * *
  2337. * WARNINGS: None *
  2338. * *
  2339. * HISTORY: *
  2340. * 7/3/2001 12:05PM ST : Created *
  2341. *=============================================================================================*/
  2342. template <class T>
  2343. inline T &SafeDataClass<T>::operator += (T const &value)
  2344. {
  2345. /*
  2346. ** Locals.
  2347. */
  2348. T *data_ptr = NULL;
  2349. /*
  2350. ** Asserts.
  2351. */
  2352. ds_assert(Handle.Is_Valid());
  2353. /*
  2354. ** If we have a valid handle, then get the current value and apply the change
  2355. */
  2356. if (Handle.Is_Valid()) {
  2357. #ifdef WWDEBUG
  2358. bool ok =
  2359. #endif //WWDEBUG
  2360. DataSafeClass<T>::Get(Handle, data_ptr);
  2361. ds_assert(ok);
  2362. ds_assert(data_ptr);
  2363. if (data_ptr) {
  2364. *data_ptr = *data_ptr + value;
  2365. #ifdef WWDEBUG
  2366. bool ok =
  2367. #endif //WWDEBUG
  2368. DataSafeClass<T>::Set(Handle, data_ptr);
  2369. ds_assert(ok);
  2370. #ifdef WWDEBUG
  2371. DebugData = *data_ptr;
  2372. #endif //WWDEBUG
  2373. return((T&)*data_ptr);
  2374. }
  2375. }
  2376. /*
  2377. ** Error case, have to return something.
  2378. */
  2379. return((T&)ErrorVal[0]);
  2380. }
  2381. /***********************************************************************************************
  2382. * SafeDataClass::operator += - += operator *
  2383. * *
  2384. * *
  2385. * *
  2386. * INPUT: Data to add *
  2387. * *
  2388. * OUTPUT: Result *
  2389. * *
  2390. * WARNINGS: None *
  2391. * *
  2392. * HISTORY: *
  2393. * 7/3/2001 12:05PM ST : Created *
  2394. *=============================================================================================*/
  2395. template <class T>
  2396. inline T &SafeDataClass<T>::operator += (SafeDataClass<T> &safevalue)
  2397. {
  2398. /*
  2399. ** Locals.
  2400. */
  2401. T *data_ptr = NULL;
  2402. T *other_value = NULL;
  2403. /*
  2404. ** Asserts.
  2405. */
  2406. ds_assert(Handle.Is_Valid());
  2407. ds_assert(safevalue.Get_Handle().Is_Valid());
  2408. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safevalue.Get_Handle()));
  2409. /*
  2410. ** If we have a valid handle, then get the data for this handle from the data safe.
  2411. */
  2412. if (Handle.Is_Valid()) {
  2413. #ifdef WWDEBUG
  2414. bool ok =
  2415. #endif //WWDEBUG
  2416. DataSafeClass<T>::Get(Handle, data_ptr);
  2417. ds_assert(ok);
  2418. ds_assert(data_ptr);
  2419. if (data_ptr) {
  2420. /*
  2421. ** Looks like we have the value for this safe data class. Get the value for the other one.
  2422. ** and do the math.
  2423. */
  2424. other_value = safevalue.Get_Ptr();
  2425. ds_assert(other_value != NULL);
  2426. if (other_value) {
  2427. *data_ptr = *data_ptr + *other_value;
  2428. #ifdef WWDEBUG
  2429. ok =
  2430. #endif //WWDEBUG
  2431. DataSafeClass<T>::Set(Handle, data_ptr);
  2432. ds_assert(ok);
  2433. #ifdef WWDEBUG
  2434. DebugData = *data_ptr;
  2435. #endif //WWDEBUG
  2436. return((T&)*data_ptr);
  2437. } else {
  2438. /*
  2439. ** Bad error case, have to return something so use the value we already have.
  2440. */
  2441. return((T&)*data_ptr);
  2442. }
  2443. }
  2444. }
  2445. /*
  2446. ** Error case, have to return something.
  2447. */
  2448. return((T&)ErrorVal[0]);
  2449. }
  2450. /***********************************************************************************************
  2451. * SafeDataClass::operator - - - operator. *
  2452. * *
  2453. * *
  2454. * *
  2455. * INPUT: Data to subtract *
  2456. * *
  2457. * OUTPUT: Result *
  2458. * *
  2459. * WARNINGS: None *
  2460. * *
  2461. * HISTORY: *
  2462. * 7/3/2001 12:05PM ST : Created *
  2463. *=============================================================================================*/
  2464. template <class T>
  2465. inline T &SafeDataClass<T>::operator - (T const &value)
  2466. {
  2467. /*
  2468. ** Locals.
  2469. */
  2470. T *data_ptr = NULL;
  2471. /*
  2472. ** Asserts.
  2473. */
  2474. ds_assert(Handle.Is_Valid());
  2475. /*
  2476. ** If we have a valid handle, then get the current value and apply the change
  2477. */
  2478. if (Handle.Is_Valid()) {
  2479. #ifdef WWDEBUG
  2480. bool ok =
  2481. #endif //WWDEBUG
  2482. DataSafeClass<T>::Get(Handle, data_ptr);
  2483. ds_assert(ok);
  2484. ds_assert(data_ptr);
  2485. if (data_ptr) {
  2486. *data_ptr = *data_ptr - value;
  2487. return((T&)*data_ptr);
  2488. }
  2489. }
  2490. /*
  2491. ** Error case, have to return something.
  2492. */
  2493. return((T&)ErrorVal[0]);
  2494. }
  2495. /***********************************************************************************************
  2496. * SafeDataClass::operator - - - operator *
  2497. * *
  2498. * *
  2499. * *
  2500. * INPUT: Data to subtract *
  2501. * *
  2502. * OUTPUT: Result *
  2503. * *
  2504. * WARNINGS: None *
  2505. * *
  2506. * HISTORY: *
  2507. * 7/3/2001 12:05PM ST : Created *
  2508. *=============================================================================================*/
  2509. template <class T>
  2510. inline T &SafeDataClass<T>::operator - (SafeDataClass<T> &safevalue)
  2511. {
  2512. /*
  2513. ** Locals.
  2514. */
  2515. T *data_ptr = NULL;
  2516. T *other_value = NULL;
  2517. /*
  2518. ** Asserts.
  2519. */
  2520. ds_assert(Handle.Is_Valid());
  2521. ds_assert(safevalue.Get_Handle().Is_Valid());
  2522. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safevalue.Get_Handle()));
  2523. /*
  2524. ** If we have a valid handle, then get the data for this handle from the data safe.
  2525. */
  2526. if (Handle.Is_Valid()) {
  2527. #ifdef WWDEBUG
  2528. bool ok =
  2529. #endif //WWDEBUG
  2530. DataSafeClass<T>::Get(Handle, data_ptr);
  2531. ds_assert(ok);
  2532. ds_assert(data_ptr);
  2533. if (data_ptr) {
  2534. /*
  2535. ** Looks like we have the value for this safe data class. Get the value for the other one.
  2536. ** and do the math.
  2537. */
  2538. other_value = safevalue.Get_Ptr();
  2539. ds_assert(other_value != NULL);
  2540. if (other_value) {
  2541. *data_ptr = *data_ptr - *other_value;
  2542. return((T&)*data_ptr);
  2543. } else {
  2544. /*
  2545. ** Bad error case, have to return something so use the value we already have.
  2546. */
  2547. return((T&)*data_ptr);
  2548. }
  2549. }
  2550. }
  2551. /*
  2552. ** Error case, have to return something.
  2553. */
  2554. return((T&)ErrorVal[0]);
  2555. }
  2556. /***********************************************************************************************
  2557. * SafeDataClass::operator -= - -= operator. *
  2558. * *
  2559. * *
  2560. * *
  2561. * INPUT: Data to subtract *
  2562. * *
  2563. * OUTPUT: Result *
  2564. * *
  2565. * WARNINGS: None *
  2566. * *
  2567. * HISTORY: *
  2568. * 7/3/2001 12:05PM ST : Created *
  2569. *=============================================================================================*/
  2570. template <class T>
  2571. inline T &SafeDataClass<T>::operator -= (T const &value)
  2572. {
  2573. /*
  2574. ** Locals.
  2575. */
  2576. T *data_ptr = NULL;
  2577. /*
  2578. ** Asserts.
  2579. */
  2580. ds_assert(Handle.Is_Valid());
  2581. /*
  2582. ** If we have a valid handle, then get the current value and apply the change
  2583. */
  2584. if (Handle.Is_Valid()) {
  2585. #ifdef WWDEBUG
  2586. bool ok =
  2587. #endif //WWDEBUG
  2588. DataSafeClass<T>::Get(Handle, data_ptr);
  2589. ds_assert(ok);
  2590. ds_assert(data_ptr);
  2591. if (data_ptr) {
  2592. *data_ptr = *data_ptr - value;
  2593. #ifdef WWDEBUG
  2594. bool ok =
  2595. #endif //WWDEBUG
  2596. DataSafeClass<T>::Set(Handle, data_ptr);
  2597. ds_assert(ok);
  2598. #ifdef WWDEBUG
  2599. DebugData = *data_ptr;
  2600. #endif //WWDEBUG
  2601. return((T&)*data_ptr);
  2602. }
  2603. }
  2604. /*
  2605. ** Error case, have to return something.
  2606. */
  2607. return((T&)ErrorVal[0]);
  2608. }
  2609. /***********************************************************************************************
  2610. * SafeDataClass::operator -= - -= operator *
  2611. * *
  2612. * *
  2613. * *
  2614. * INPUT: Data to subtract *
  2615. * *
  2616. * OUTPUT: Result *
  2617. * *
  2618. * WARNINGS: None *
  2619. * *
  2620. * HISTORY: *
  2621. * 7/3/2001 12:05PM ST : Created *
  2622. *=============================================================================================*/
  2623. template <class T>
  2624. inline T &SafeDataClass<T>::operator -= (SafeDataClass<T> &safevalue)
  2625. {
  2626. /*
  2627. ** Locals.
  2628. */
  2629. T *data_ptr = NULL;
  2630. T *other_value = NULL;
  2631. /*
  2632. ** Asserts.
  2633. */
  2634. ds_assert(Handle.Is_Valid());
  2635. ds_assert(safevalue.Get_Handle().Is_Valid());
  2636. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safevalue.Get_Handle()));
  2637. /*
  2638. ** If we have a valid handle, then get the data for this handle from the data safe.
  2639. */
  2640. if (Handle.Is_Valid()) {
  2641. #ifdef WWDEBUG
  2642. bool ok =
  2643. #endif //WWDEBUG
  2644. DataSafeClass<T>::Get(Handle, data_ptr);
  2645. ds_assert(ok);
  2646. ds_assert(data_ptr);
  2647. if (data_ptr) {
  2648. /*
  2649. ** Looks like we have the value for this safe data class. Get the value for the other one.
  2650. ** and do the math.
  2651. */
  2652. other_value = safevalue.Get_Ptr();
  2653. ds_assert(other_value != NULL);
  2654. if (other_value) {
  2655. *data_ptr = *data_ptr - *other_value;
  2656. #ifdef WWDEBUG
  2657. ok =
  2658. #endif //WWDEBUG
  2659. DataSafeClass<T>::Set(Handle, data_ptr);
  2660. ds_assert(ok);
  2661. #ifdef WWDEBUG
  2662. DebugData = *data_ptr;
  2663. #endif //WWDEBUG
  2664. return((T&)*data_ptr);
  2665. } else {
  2666. /*
  2667. ** Bad error case, have to return something so use the value we already have.
  2668. */
  2669. return((T&)*data_ptr);
  2670. }
  2671. }
  2672. }
  2673. /*
  2674. ** Error case, have to return something.
  2675. */
  2676. return((T&)ErrorVal[0]);
  2677. }
  2678. /***********************************************************************************************
  2679. * SafeDataClass::operator * - * operator. *
  2680. * *
  2681. * *
  2682. * *
  2683. * INPUT: Data to multiply by *
  2684. * *
  2685. * OUTPUT: Result *
  2686. * *
  2687. * WARNINGS: None *
  2688. * *
  2689. * HISTORY: *
  2690. * 7/3/2001 12:05PM ST : Created *
  2691. *=============================================================================================*/
  2692. template <class T>
  2693. inline T &SafeDataClass<T>::operator * (T const &value)
  2694. {
  2695. /*
  2696. ** Locals.
  2697. */
  2698. T *data_ptr = NULL;
  2699. /*
  2700. ** Asserts.
  2701. */
  2702. ds_assert(Handle.Is_Valid());
  2703. /*
  2704. ** If we have a valid handle, then get the current value and apply the change
  2705. */
  2706. if (Handle.Is_Valid()) {
  2707. #ifdef WWDEBUG
  2708. bool ok =
  2709. #endif //WWDEBUG
  2710. DataSafeClass<T>::Get(Handle, data_ptr);
  2711. ds_assert(ok);
  2712. ds_assert(data_ptr);
  2713. if (data_ptr) {
  2714. *data_ptr = *data_ptr * value;
  2715. return((T&)*data_ptr);
  2716. }
  2717. }
  2718. /*
  2719. ** Error case, have to return something.
  2720. */
  2721. return((T&)ErrorVal[0]);
  2722. }
  2723. /***********************************************************************************************
  2724. * SafeDataClass::operator * - * operator *
  2725. * *
  2726. * *
  2727. * *
  2728. * INPUT: Data to multiply by *
  2729. * *
  2730. * OUTPUT: Result *
  2731. * *
  2732. * WARNINGS: None *
  2733. * *
  2734. * HISTORY: *
  2735. * 7/3/2001 12:05PM ST : Created *
  2736. *=============================================================================================*/
  2737. template <class T>
  2738. inline T &SafeDataClass<T>::operator * (SafeDataClass<T> &safevalue)
  2739. {
  2740. /*
  2741. ** Locals.
  2742. */
  2743. T *data_ptr = NULL;
  2744. T *other_value = NULL;
  2745. /*
  2746. ** Asserts.
  2747. */
  2748. ds_assert(Handle.Is_Valid());
  2749. ds_assert(safevalue.Get_Handle().Is_Valid());
  2750. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safevalue.Get_Handle()));
  2751. /*
  2752. ** If we have a valid handle, then get the data for this handle from the data safe.
  2753. */
  2754. if (Handle.Is_Valid()) {
  2755. #ifdef WWDEBUG
  2756. bool ok =
  2757. #endif //WWDEBUG
  2758. DataSafeClass<T>::Get(Handle, data_ptr);
  2759. ds_assert(ok);
  2760. ds_assert(data_ptr);
  2761. if (data_ptr) {
  2762. /*
  2763. ** Looks like we have the value for this safe data class. Get the value for the other one.
  2764. ** and do the math.
  2765. */
  2766. other_value = safevalue.Get_Ptr();
  2767. ds_assert(other_value != NULL);
  2768. if (other_value) {
  2769. *data_ptr = *data_ptr * *other_value;
  2770. return((T&)*data_ptr);
  2771. } else {
  2772. /*
  2773. ** Bad error case, have to return something so use the value we already have.
  2774. */
  2775. return((T&)*data_ptr);
  2776. }
  2777. }
  2778. }
  2779. /*
  2780. ** Error case, have to return something.
  2781. */
  2782. return((T&)ErrorVal[0]);
  2783. }
  2784. /***********************************************************************************************
  2785. * SafeDataClass::operator *= - *= operator. *
  2786. * *
  2787. * *
  2788. * *
  2789. * INPUT: Data to multiply by *
  2790. * *
  2791. * OUTPUT: Result *
  2792. * *
  2793. * WARNINGS: None *
  2794. * *
  2795. * HISTORY: *
  2796. * 7/3/2001 12:05PM ST : Created *
  2797. *=============================================================================================*/
  2798. template <class T>
  2799. inline T &SafeDataClass<T>::operator *= (T const &value)
  2800. {
  2801. /*
  2802. ** Locals.
  2803. */
  2804. T *data_ptr = NULL;
  2805. /*
  2806. ** Asserts.
  2807. */
  2808. ds_assert(Handle.Is_Valid());
  2809. /*
  2810. ** If we have a valid handle, then get the current value and apply the change
  2811. */
  2812. if (Handle.Is_Valid()) {
  2813. #ifdef WWDEBUG
  2814. bool ok =
  2815. #endif //WWDEBUG
  2816. DataSafeClass<T>::Get(Handle, data_ptr);
  2817. ds_assert(ok);
  2818. ds_assert(data_ptr);
  2819. if (data_ptr) {
  2820. *data_ptr = *data_ptr * value;
  2821. #ifdef WWDEBUG
  2822. bool ok =
  2823. #endif //WWDEBUG
  2824. DataSafeClass<T>::Set(Handle, data_ptr);
  2825. ds_assert(ok);
  2826. #ifdef WWDEBUG
  2827. DebugData = *data_ptr;
  2828. #endif //WWDEBUG
  2829. return((T&)*data_ptr);
  2830. }
  2831. }
  2832. /*
  2833. ** Error case, have to return something.
  2834. */
  2835. return((T&)ErrorVal[0]);
  2836. }
  2837. /***********************************************************************************************
  2838. * SafeDataClass::operator *= - *= operator *
  2839. * *
  2840. * *
  2841. * *
  2842. * INPUT: Data to multiply by *
  2843. * *
  2844. * OUTPUT: Result *
  2845. * *
  2846. * WARNINGS: None *
  2847. * *
  2848. * HISTORY: *
  2849. * 7/3/2001 12:05PM ST : Created *
  2850. *=============================================================================================*/
  2851. template <class T>
  2852. inline T &SafeDataClass<T>::operator *= (SafeDataClass<T> &safevalue)
  2853. {
  2854. /*
  2855. ** Locals.
  2856. */
  2857. T *data_ptr = NULL;
  2858. T *other_value = NULL;
  2859. /*
  2860. ** Asserts.
  2861. */
  2862. ds_assert(Handle.Is_Valid());
  2863. ds_assert(safevalue.Get_Handle().Is_Valid());
  2864. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safevalue.Get_Handle()));
  2865. /*
  2866. ** If we have a valid handle, then get the data for this handle from the data safe.
  2867. */
  2868. if (Handle.Is_Valid()) {
  2869. #ifdef WWDEBUG
  2870. bool ok =
  2871. #endif //WWDEBUG
  2872. DataSafeClass<T>::Get(Handle, data_ptr);
  2873. ds_assert(ok);
  2874. ds_assert(data_ptr);
  2875. if (data_ptr) {
  2876. /*
  2877. ** Looks like we have the value for this safe data class. Get the value for the other one.
  2878. ** and do the math.
  2879. */
  2880. other_value = safevalue.Get_Ptr();
  2881. ds_assert(other_value != NULL);
  2882. if (other_value) {
  2883. *data_ptr = *data_ptr * *other_value;
  2884. #ifdef WWDEBUG
  2885. ok =
  2886. #endif //WWDEBUG
  2887. DataSafeClass<T>::Set(Handle, data_ptr);
  2888. ds_assert(ok);
  2889. #ifdef WWDEBUG
  2890. DebugData = *data_ptr;
  2891. #endif //WWDEBUG
  2892. return((T&)*data_ptr);
  2893. } else {
  2894. /*
  2895. ** Bad error case, have to return something so use the value we already have.
  2896. */
  2897. return((T&)*data_ptr);
  2898. }
  2899. }
  2900. }
  2901. /*
  2902. ** Error case, have to return something.
  2903. */
  2904. return((T&)ErrorVal[0]);
  2905. }
  2906. /***********************************************************************************************
  2907. * SafeDataClass::operator / - / operator. *
  2908. * *
  2909. * *
  2910. * *
  2911. * INPUT: Data to divide by *
  2912. * *
  2913. * OUTPUT: Result *
  2914. * *
  2915. * WARNINGS: None *
  2916. * *
  2917. * HISTORY: *
  2918. * 7/3/2001 12:05PM ST : Created *
  2919. *=============================================================================================*/
  2920. template <class T>
  2921. inline T &SafeDataClass<T>::operator / (T const &value)
  2922. {
  2923. /*
  2924. ** Locals.
  2925. */
  2926. T *data_ptr = NULL;
  2927. /*
  2928. ** Asserts.
  2929. */
  2930. ds_assert(Handle.Is_Valid());
  2931. ds_assert(value != 0);
  2932. /*
  2933. ** If we have a valid handle, then get the current value and apply the change
  2934. */
  2935. if (Handle.Is_Valid()) {
  2936. #ifdef WWDEBUG
  2937. bool ok =
  2938. #endif //WWDEBUG
  2939. DataSafeClass<T>::Get(Handle, data_ptr);
  2940. ds_assert(ok);
  2941. ds_assert(data_ptr);
  2942. if (data_ptr) {
  2943. *data_ptr = *data_ptr / value;
  2944. return((T&)*data_ptr);
  2945. }
  2946. }
  2947. /*
  2948. ** Error case, have to return something.
  2949. */
  2950. return((T&)ErrorVal[0]);
  2951. }
  2952. /***********************************************************************************************
  2953. * SafeDataClass::operator / - / operator *
  2954. * *
  2955. * *
  2956. * *
  2957. * INPUT: Data to divide by *
  2958. * *
  2959. * OUTPUT: Result *
  2960. * *
  2961. * WARNINGS: None *
  2962. * *
  2963. * HISTORY: *
  2964. * 7/3/2001 12:05PM ST : Created *
  2965. *=============================================================================================*/
  2966. template <class T>
  2967. inline T &SafeDataClass<T>::operator / (SafeDataClass<T> &safevalue)
  2968. {
  2969. /*
  2970. ** Locals.
  2971. */
  2972. T *data_ptr = NULL;
  2973. T *other_value = NULL;
  2974. /*
  2975. ** Asserts.
  2976. */
  2977. ds_assert(Handle.Is_Valid());
  2978. ds_assert(safevalue.Get_Handle().Is_Valid());
  2979. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safevalue.Get_Handle()));
  2980. /*
  2981. ** If we have a valid handle, then get the data for this handle from the data safe.
  2982. */
  2983. if (Handle.Is_Valid()) {
  2984. #ifdef WWDEBUG
  2985. bool ok =
  2986. #endif //WWDEBUG
  2987. DataSafeClass<T>::Get(Handle, data_ptr);
  2988. ds_assert(ok);
  2989. ds_assert(data_ptr);
  2990. if (data_ptr) {
  2991. /*
  2992. ** Looks like we have the value for this safe data class. Get the value for the other one.
  2993. ** and do the math.
  2994. */
  2995. other_value = safevalue.Get_Ptr();
  2996. ds_assert(other_value != NULL);
  2997. if (other_value) {
  2998. ds_assert(*other_value != 0);
  2999. *data_ptr = *data_ptr / *other_value;
  3000. return((T&)*data_ptr);
  3001. } else {
  3002. /*
  3003. ** Bad error case, have to return something so use the value we already have.
  3004. */
  3005. return((T&)*data_ptr);
  3006. }
  3007. }
  3008. }
  3009. /*
  3010. ** Error case, have to return something.
  3011. */
  3012. return((T&)ErrorVal[0]);
  3013. }
  3014. /***********************************************************************************************
  3015. * SafeDataClass::operator /= - /= operator. *
  3016. * *
  3017. * *
  3018. * *
  3019. * INPUT: Data to divide by *
  3020. * *
  3021. * OUTPUT: Result *
  3022. * *
  3023. * WARNINGS: None *
  3024. * *
  3025. * HISTORY: *
  3026. * 7/3/2001 12:05PM ST : Created *
  3027. *=============================================================================================*/
  3028. template <class T>
  3029. inline T &SafeDataClass<T>::operator /= (T const &value)
  3030. {
  3031. /*
  3032. ** Locals.
  3033. */
  3034. T *data_ptr = NULL;
  3035. /*
  3036. ** Asserts.
  3037. */
  3038. ds_assert(Handle.Is_Valid());
  3039. ds_assert(value != 0);
  3040. /*
  3041. ** If we have a valid handle, then get the current value and apply the change
  3042. */
  3043. if (Handle.Is_Valid()) {
  3044. #ifdef WWDEBUG
  3045. bool ok =
  3046. #endif //WWDEBUG
  3047. DataSafeClass<T>::Get(Handle, data_ptr);
  3048. ds_assert(ok);
  3049. ds_assert(data_ptr);
  3050. if (data_ptr) {
  3051. *data_ptr = *data_ptr / value;
  3052. #ifdef WWDEBUG
  3053. bool ok =
  3054. #endif //WWDEBUG
  3055. DataSafeClass<T>::Set(Handle, data_ptr);
  3056. ds_assert(ok);
  3057. #ifdef WWDEBUG
  3058. DebugData = *data_ptr;
  3059. #endif //WWDEBUG
  3060. return((T&)*data_ptr);
  3061. }
  3062. }
  3063. /*
  3064. ** Error case, have to return something.
  3065. */
  3066. return((T&)ErrorVal[0]);
  3067. }
  3068. /***********************************************************************************************
  3069. * SafeDataClass::operator /= - /= operator *
  3070. * *
  3071. * *
  3072. * *
  3073. * INPUT: Data to divide by *
  3074. * *
  3075. * OUTPUT: Result *
  3076. * *
  3077. * WARNINGS: None *
  3078. * *
  3079. * HISTORY: *
  3080. * 7/3/2001 12:05PM ST : Created *
  3081. *=============================================================================================*/
  3082. template <class T>
  3083. inline T &SafeDataClass<T>::operator /= (SafeDataClass<T> &safevalue)
  3084. {
  3085. /*
  3086. ** Locals.
  3087. */
  3088. T *data_ptr = NULL;
  3089. T *other_value = NULL;
  3090. /*
  3091. ** Asserts.
  3092. */
  3093. ds_assert(Handle.Is_Valid());
  3094. ds_assert(safevalue.Get_Handle().Is_Valid());
  3095. ds_assert(GenericDataSafeClass::Get_Entry_Type(Handle) == GenericDataSafeClass::Get_Entry_Type(safevalue.Get_Handle()));
  3096. /*
  3097. ** If we have a valid handle, then get the data for this handle from the data safe.
  3098. */
  3099. if (Handle.Is_Valid()) {
  3100. #ifdef WWDEBUG
  3101. bool ok =
  3102. #endif //WWDEBUG
  3103. DataSafeClass<T>::Get(Handle, data_ptr);
  3104. ds_assert(ok);
  3105. ds_assert(data_ptr);
  3106. if (data_ptr) {
  3107. /*
  3108. ** Looks like we have the value for this safe data class. Get the value for the other one.
  3109. ** and do the math.
  3110. */
  3111. other_value = safevalue.Get_Ptr();
  3112. ds_assert(other_value != NULL);
  3113. if (other_value) {
  3114. ds_assert(*other_value != 0);
  3115. *data_ptr = *data_ptr / *other_value;
  3116. #ifdef WWDEBUG
  3117. ok =
  3118. #endif //WWDEBUG
  3119. DataSafeClass<T>::Set(Handle, data_ptr);
  3120. ds_assert(ok);
  3121. #ifdef WWDEBUG
  3122. DebugData = *data_ptr;
  3123. #endif //WWDEBUG
  3124. return((T&)*data_ptr);
  3125. } else {
  3126. /*
  3127. ** Bad error case, have to return something so use the value we already have.
  3128. */
  3129. return((T&)*data_ptr);
  3130. }
  3131. }
  3132. }
  3133. /*
  3134. ** Error case, have to return something.
  3135. */
  3136. return((T&)ErrorVal[0]);
  3137. }
  3138. /***********************************************************************************************
  3139. * SafeDataClass::operator ++ - prefix increment operator. *
  3140. * *
  3141. * *
  3142. * *
  3143. * INPUT: Nothing *
  3144. * *
  3145. * OUTPUT: Result *
  3146. * *
  3147. * WARNINGS: None *
  3148. * *
  3149. * HISTORY: *
  3150. * 7/3/2001 12:05PM ST : Created *
  3151. *=============================================================================================*/
  3152. template <class T>
  3153. inline T &SafeDataClass<T>::operator ++ (void)
  3154. {
  3155. /*
  3156. ** Locals.
  3157. */
  3158. T *data_ptr = NULL;
  3159. /*
  3160. ** Asserts.
  3161. */
  3162. ds_assert(Handle.Is_Valid());
  3163. /*
  3164. ** If we have a valid handle, then get the current value and apply the change
  3165. */
  3166. if (Handle.Is_Valid()) {
  3167. #ifdef WWDEBUG
  3168. bool ok =
  3169. #endif //WWDEBUG
  3170. DataSafeClass<T>::Get(Handle, data_ptr);
  3171. ds_assert(ok);
  3172. ds_assert(data_ptr);
  3173. if (data_ptr) {
  3174. (*data_ptr)++;
  3175. #ifdef WWDEBUG
  3176. ok =
  3177. #endif //WWDEBUG
  3178. DataSafeClass<T>::Set(Handle, data_ptr);
  3179. ds_assert(ok);
  3180. #ifdef WWDEBUG
  3181. DebugData = *data_ptr;
  3182. #endif //WWDEBUG
  3183. return((T&)*data_ptr);
  3184. }
  3185. }
  3186. /*
  3187. ** Error case, have to return something.
  3188. */
  3189. return((T&)ErrorVal[0]);
  3190. }
  3191. /***********************************************************************************************
  3192. * SafeDataClass::operator -- - prefix decrement operator. *
  3193. * *
  3194. * *
  3195. * *
  3196. * INPUT: Nothing *
  3197. * *
  3198. * OUTPUT: Result *
  3199. * *
  3200. * WARNINGS: None *
  3201. * *
  3202. * HISTORY: *
  3203. * 7/3/2001 12:05PM ST : Created *
  3204. *=============================================================================================*/
  3205. template <class T>
  3206. inline T &SafeDataClass<T>::operator -- (void)
  3207. {
  3208. /*
  3209. ** Locals.
  3210. */
  3211. T *data_ptr = NULL;
  3212. /*
  3213. ** Asserts.
  3214. */
  3215. ds_assert(Handle.Is_Valid());
  3216. /*
  3217. ** If we have a valid handle, then get the current value and apply the change
  3218. */
  3219. if (Handle.Is_Valid()) {
  3220. #ifdef WWDEBUG
  3221. bool ok =
  3222. #endif //WWDEBUG
  3223. DataSafeClass<T>::Get(Handle, data_ptr);
  3224. ds_assert(ok);
  3225. ds_assert(data_ptr);
  3226. if (data_ptr) {
  3227. (*data_ptr)--;
  3228. #ifdef WWDEBUG
  3229. ok =
  3230. #endif //WWDEBUG
  3231. DataSafeClass<T>::Set(Handle, data_ptr);
  3232. ds_assert(ok);
  3233. #ifdef WWDEBUG
  3234. DebugData = *data_ptr;
  3235. #endif //WWDEBUG
  3236. return((T&)*data_ptr);
  3237. }
  3238. }
  3239. /*
  3240. ** Error case, have to return something.
  3241. */
  3242. return((T&)ErrorVal[0]);
  3243. }
  3244. /***********************************************************************************************
  3245. * SafeDataClass::operator ++ - postfix ++ operator *
  3246. * *
  3247. * *
  3248. * *
  3249. * INPUT: int (not used, denotes postfix operator) *
  3250. * *
  3251. * OUTPUT: Nothing *
  3252. * *
  3253. * WARNINGS: None *
  3254. * *
  3255. * HISTORY: *
  3256. * 7/23/2001 3:54PM ST : Created *
  3257. *=============================================================================================*/
  3258. template <class T>
  3259. inline T &SafeDataClass<T>::operator ++ (int)
  3260. {
  3261. /*
  3262. ** Just call the prefix version of the operator.
  3263. */
  3264. return(this->operator ++ ());
  3265. }
  3266. /***********************************************************************************************
  3267. * SafeDataClass::operator -- - postfix -- operator *
  3268. * *
  3269. * *
  3270. * *
  3271. * INPUT: int (not used, denotes postfix operator) *
  3272. * *
  3273. * OUTPUT: Nothing *
  3274. * *
  3275. * WARNINGS: None *
  3276. * *
  3277. * HISTORY: *
  3278. * 7/23/2001 3:54PM ST : Created *
  3279. *=============================================================================================*/
  3280. template <class T>
  3281. inline T &SafeDataClass<T>::operator -- (int)
  3282. {
  3283. /*
  3284. ** Just call the prefix version of the operator.
  3285. */
  3286. return(this->operator -- ());
  3287. }
  3288. /***********************************************************************************************
  3289. * SafeDataClass::operator () - Conversion operator used for data retrieval *
  3290. * *
  3291. * *
  3292. * *
  3293. * INPUT: nothing *
  3294. * *
  3295. * OUTPUT: Copy of data in safe *
  3296. * *
  3297. * WARNINGS: None *
  3298. * *
  3299. * HISTORY: *
  3300. * 7/3/2001 12:05PM ST : Created *
  3301. *=============================================================================================*/
  3302. template <class T>
  3303. inline T &SafeDataClass<T>::operator () (void) const
  3304. {
  3305. T *data_ptr = NULL;
  3306. /*
  3307. ** If the handle we have is valid then use it to get a pointer to a temporary copy of the data safe contents for this
  3308. ** handle.
  3309. */
  3310. if (Handle.Is_Valid()) {
  3311. #ifdef WWDEBUG
  3312. bool ok =
  3313. #endif //WWDEBUG
  3314. DataSafeClass<T>::Get(Handle, data_ptr);
  3315. ds_assert(ok);
  3316. if (data_ptr) {
  3317. return(*data_ptr);
  3318. }
  3319. }
  3320. /*
  3321. ** Error case. Need to return some valid value.
  3322. */
  3323. static T oh_dear;
  3324. return(oh_dear);
  3325. }
  3326. /***********************************************************************************************
  3327. * SafeDataClass::operator int -- Return the data for this class as an int *
  3328. * *
  3329. * *
  3330. * *
  3331. * INPUT: Nothing *
  3332. * *
  3333. * OUTPUT: Data cast to int *
  3334. * *
  3335. * WARNINGS: None *
  3336. * *
  3337. * HISTORY: *
  3338. * 7/6/2001 11:47AM ST : Created *
  3339. *=============================================================================================*/
  3340. template <class T>
  3341. inline SafeDataClass<T>::operator int (void) const
  3342. {
  3343. ds_assert(sizeof(T) == sizeof(int));
  3344. T *data_ptr = NULL;
  3345. #ifdef WWDEBUG
  3346. /*
  3347. ** Check that T is safe to return as an int
  3348. */
  3349. T x = 0;
  3350. int y = (T)x;
  3351. ds_assert(x == y);
  3352. #endif //WWDEBUG
  3353. /*
  3354. ** If the handle we have is valid then use it to get a pointer to a temporary copy of the data safe contents for this
  3355. ** handle.
  3356. */
  3357. if (Handle.Is_Valid()) {
  3358. #ifdef WWDEBUG
  3359. bool ok =
  3360. #endif //WWDEBUG
  3361. DataSafeClass<T>::Get(Handle, data_ptr);
  3362. ds_assert(ok);
  3363. if (data_ptr) {
  3364. return(*((int*)data_ptr));
  3365. }
  3366. }
  3367. /*
  3368. ** Error case. Need to return some valid value.
  3369. */
  3370. static int oh_dear;
  3371. return(oh_dear);
  3372. }
  3373. /***********************************************************************************************
  3374. * SafeDataClass::operator unsigned int -- Return the data for this class as an unsigned int *
  3375. * *
  3376. * *
  3377. * *
  3378. * INPUT: Nothing *
  3379. * *
  3380. * OUTPUT: Data cast to unsigned int *
  3381. * *
  3382. * WARNINGS: None *
  3383. * *
  3384. * HISTORY: *
  3385. * 7/6/2001 11:47AM ST : Created *
  3386. *=============================================================================================*/
  3387. template <class T>
  3388. inline SafeDataClass<T>::operator unsigned int (void) const
  3389. {
  3390. ds_assert(sizeof(T) == sizeof(unsigned int));
  3391. T *data_ptr = NULL;
  3392. #ifdef WWDEBUG
  3393. /*
  3394. ** Check that T is safe to return as an unsigned int
  3395. */
  3396. T x = 0;
  3397. unsigned int y = (T)x;
  3398. ds_assert(x == y);
  3399. #endif //WWDEBUG
  3400. /*
  3401. ** If the handle we have is valid then use it to get a pointer to a temporary copy of the data safe contents for this
  3402. ** handle.
  3403. */
  3404. if (Handle.Is_Valid()) {
  3405. #ifdef WWDEBUG
  3406. bool ok =
  3407. #endif //WWDEBUG
  3408. DataSafeClass<T>::Get(Handle, data_ptr);
  3409. ds_assert(ok);
  3410. if (data_ptr) {
  3411. return(*((unsigned int*)data_ptr));
  3412. }
  3413. }
  3414. /*
  3415. ** Error case. Need to return some valid value.
  3416. */
  3417. static unsigned int oh_dear;
  3418. return(oh_dear);
  3419. }
  3420. /***********************************************************************************************
  3421. * SafeDataClass::operator long -- Return the data for this class as a long *
  3422. * *
  3423. * *
  3424. * *
  3425. * INPUT: Nothing *
  3426. * *
  3427. * OUTPUT: Data cast to long *
  3428. * *
  3429. * WARNINGS: None *
  3430. * *
  3431. * HISTORY: *
  3432. * 7/6/2001 11:47AM ST : Created *
  3433. *=============================================================================================*/
  3434. template <class T>
  3435. inline SafeDataClass<T>::operator long (void) const
  3436. {
  3437. ds_assert(sizeof(T) == sizeof(long));
  3438. T *data_ptr = NULL;
  3439. #ifdef WWDEBUG
  3440. /*
  3441. ** Check that T is safe to return as a long
  3442. */
  3443. T x = 0;
  3444. long y = (T)x;
  3445. ds_assert(x == y);
  3446. #endif //WWDEBUG
  3447. /*
  3448. ** If the handle we have is valid then use it to get a pointer to a temporary copy of the data safe contents for this
  3449. ** handle.
  3450. */
  3451. if (Handle.Is_Valid()) {
  3452. #ifdef WWDEBUG
  3453. bool ok =
  3454. #endif //WWDEBUG
  3455. DataSafeClass<T>::Get(Handle, data_ptr);
  3456. ds_assert(ok);
  3457. if (data_ptr) {
  3458. return(*((long*)data_ptr));
  3459. }
  3460. }
  3461. /*
  3462. ** Error case. Need to return some valid value.
  3463. */
  3464. static long oh_dear;
  3465. return(oh_dear);
  3466. }
  3467. /***********************************************************************************************
  3468. * SafeDataClass::operator int -- Return the data for this class as an unsigned long *
  3469. * *
  3470. * *
  3471. * *
  3472. * INPUT: Nothing *
  3473. * *
  3474. * OUTPUT: Data cast to unsigned long *
  3475. * *
  3476. * WARNINGS: None *
  3477. * *
  3478. * HISTORY: *
  3479. * 7/6/2001 11:47AM ST : Created *
  3480. *=============================================================================================*/
  3481. template <class T>
  3482. inline SafeDataClass<T>::operator unsigned long (void) const
  3483. {
  3484. ds_assert(sizeof(T) == sizeof(unsigned long));
  3485. T *data_ptr = NULL;
  3486. #ifdef WWDEBUG
  3487. /*
  3488. ** Check that T is safe to return as an unsigned long
  3489. */
  3490. T x = 0;
  3491. unsigned long y = (T)x;
  3492. ds_assert(x == y);
  3493. #endif //WWDEBUG
  3494. /*
  3495. ** If the handle we have is valid then use it to get a pointer to a temporary copy of the data safe contents for this
  3496. ** handle.
  3497. */
  3498. if (Handle.Is_Valid()) {
  3499. #ifdef WWDEBUG
  3500. bool ok =
  3501. #endif //WWDEBUG
  3502. DataSafeClass<T>::Get(Handle, data_ptr);
  3503. ds_assert(ok);
  3504. if (data_ptr) {
  3505. return(*((unsigned long*)data_ptr));
  3506. }
  3507. }
  3508. /*
  3509. ** Error case. Need to return some valid value.
  3510. */
  3511. static unsigned long oh_dear;
  3512. return(oh_dear);
  3513. }
  3514. /***********************************************************************************************
  3515. * SafeDataClass::operator int -- Return the data for this class as a float *
  3516. * *
  3517. * *
  3518. * *
  3519. * INPUT: Nothing *
  3520. * *
  3521. * OUTPUT: Data cast to float *
  3522. * *
  3523. * WARNINGS: None *
  3524. * *
  3525. * HISTORY: *
  3526. * 7/6/2001 11:47AM ST : Created *
  3527. *=============================================================================================*/
  3528. template <class T>
  3529. inline SafeDataClass<T>::operator float (void) const
  3530. {
  3531. ds_assert(sizeof(T) == sizeof(float));
  3532. T *data_ptr = NULL;
  3533. #ifdef WWDEBUG
  3534. /*
  3535. ** Check that T is safe to return as a float
  3536. */
  3537. T x = 0;
  3538. float y = (T)x;
  3539. ds_assert(x == y);
  3540. #endif //WWDEBUG
  3541. /*
  3542. ** If the handle we have is valid then use it to get a pointer to a temporary copy of the data safe contents for this
  3543. ** handle.
  3544. */
  3545. if (Handle.Is_Valid()) {
  3546. #ifdef WWDEBUG
  3547. bool ok =
  3548. #endif //WWDEBUG
  3549. DataSafeClass<T>::Get(Handle, data_ptr);
  3550. ds_assert(ok);
  3551. if (data_ptr) {
  3552. return(*((float*)data_ptr));
  3553. }
  3554. }
  3555. /*
  3556. ** Error case. Need to return some valid value.
  3557. */
  3558. static float oh_dear;
  3559. return(oh_dear);
  3560. }
  3561. /***********************************************************************************************
  3562. * SafeDataClass::operator int -- Return the data for this class as a double *
  3563. * *
  3564. * *
  3565. * *
  3566. * INPUT: Nothing *
  3567. * *
  3568. * OUTPUT: Data cast to double *
  3569. * *
  3570. * WARNINGS: None *
  3571. * *
  3572. * HISTORY: *
  3573. * 7/6/2001 11:47AM ST : Created *
  3574. *=============================================================================================*/
  3575. template <class T>
  3576. inline SafeDataClass<T>::operator double (void) const
  3577. {
  3578. ds_assert(sizeof(T) == sizeof(double));
  3579. T *data_ptr = NULL;
  3580. #ifdef WWDEBUG
  3581. /*
  3582. ** Check that T is safe to return as a double
  3583. */
  3584. T x = 0;
  3585. double y = (T)x;
  3586. ds_assert(x == y);
  3587. #endif //WWDEBUG
  3588. /*
  3589. ** If the handle we have is valid then use it to get a pointer to a temporary copy of the data safe contents for this
  3590. ** handle.
  3591. */
  3592. if (Handle.Is_Valid()) {
  3593. #ifdef WWDEBUG
  3594. bool ok =
  3595. #endif //WWDEBUG
  3596. DataSafeClass<T>::Get(Handle, data_ptr);
  3597. ds_assert(ok);
  3598. if (data_ptr) {
  3599. return(*((double*)data_ptr));
  3600. }
  3601. }
  3602. /*
  3603. ** Error case. Need to return some valid value.
  3604. */
  3605. static double oh_dear;
  3606. return(oh_dear);
  3607. }
  3608. /***********************************************************************************************
  3609. * SafeDataClass::Get_Ptr - Get a pointer to a decrypted version of a class in the data safe *
  3610. * *
  3611. * *
  3612. * *
  3613. * INPUT: nothing *
  3614. * *
  3615. * OUTPUT: Ptr to copy of data in safe *
  3616. * *
  3617. * WARNINGS: None *
  3618. * *
  3619. * HISTORY: *
  3620. * 7/3/2001 12:05PM ST : Created *
  3621. *=============================================================================================*/
  3622. template <class T>
  3623. inline T *SafeDataClass<T>::Get_Ptr(void) const
  3624. {
  3625. T *data_ptr = NULL;
  3626. /*
  3627. ** If the handle we have is valid then use it to get a pointer to a temporary copy of the data safe contents for this
  3628. ** handle.
  3629. */
  3630. if (Handle.Is_Valid()) {
  3631. #ifdef WWDEBUG
  3632. bool ok =
  3633. #endif //WWDEBUG
  3634. DataSafeClass<T>::Get(Handle, data_ptr);
  3635. ds_assert(ok);
  3636. if (data_ptr) {
  3637. return(data_ptr);
  3638. }
  3639. }
  3640. /*
  3641. ** Error case.
  3642. */
  3643. ds_assert(false);
  3644. return(NULL);
  3645. }
  3646. /***********************************************************************************************
  3647. * SafeDataClass::Commit -- Write decrypted data safe data back into the safe *
  3648. * *
  3649. * *
  3650. * *
  3651. * INPUT: Ptr to data (must be from prior return list) *
  3652. * *
  3653. * OUTPUT: True if commited back into the safe *
  3654. * *
  3655. * WARNINGS: None *
  3656. * *
  3657. * HISTORY: *
  3658. * 7/13/2001 2:28PM ST : Created *
  3659. *=============================================================================================*/
  3660. template <class T>
  3661. inline bool SafeDataClass<T>::Commit(T *data_ptr) const
  3662. {
  3663. ds_assert(Handle.Is_Valid());
  3664. ds_assert(DataSafeClass<T>::In_Return_List(data_ptr));
  3665. /*
  3666. ** If the handle is valid, and the data pointer is one of our temporary return buffers
  3667. ** then copy the data back into the safe.
  3668. */
  3669. if (Handle.Is_Valid()) {
  3670. /*
  3671. ** Verify it's one of our temp data pointers.
  3672. */
  3673. if (DataSafeClass<T>::In_Return_List(data_ptr)) {
  3674. #ifdef WWDEBUG
  3675. DebugData = *data_ptr;
  3676. #endif //WWDEBUG
  3677. return(DataSafeClass<T>::Set(Handle, data_ptr));
  3678. }
  3679. }
  3680. return(false);
  3681. }
  3682. #pragma warning(default : 4189)
  3683. #pragma warning(default : 4101)
  3684. #endif //_DATASAFE_H