Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

399 lines
9.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: catdb.cpp
  8. //
  9. // Contents: Microsoft Internet Security Catalog Utilities
  10. //
  11. // History: 23-Oct-1997 pberkman created
  12. //
  13. //--------------------------------------------------------------------------
  14. #include "global.hxx"
  15. #include "catdb.hxx"
  16. cCatalogDB_::cCatalogDB_(WCHAR *pwszBaseDirIn, WCHAR *pwszSubDirIn)
  17. {
  18. WCHAR wszPath[MAX_PATH];
  19. LPWSTR pwszPath;
  20. BOOL fCreatedOK = FALSE;
  21. pCatMast = NULL;
  22. pwszPath = new WCHAR [ wcslen(pwszBaseDirIn)+wcslen(pwszSubDirIn)+1 ];
  23. if ( pwszPath == NULL )
  24. {
  25. return;
  26. }
  27. wcscpy(pwszPath, pwszBaseDirIn);
  28. CreateDirectoryU(pwszPath, NULL);
  29. //
  30. // sysmast is in the CatRoot directory.
  31. //
  32. if ((pSysMast = new cBFile_(&MSCAT_CriticalSection,
  33. pwszPath,
  34. SYSMAST_NAME,
  35. sizeof(GUID),
  36. sizeof(SysMastRec),
  37. CATDB_VERSION_1,
  38. &fCreatedOK)) &&
  39. fCreatedOK &&
  40. (pSysMast->Initialize()))
  41. {
  42. wcscat(pwszPath, pwszSubDirIn);
  43. // create the directory!
  44. CreateDirectoryU(pwszPath, NULL);
  45. if ((pCatMast = new cBFile_(&MSCAT_CriticalSection,
  46. pwszPath,
  47. CATMAST_NAME,
  48. sizeof(DWORD),
  49. sizeof(CatMastRec),
  50. CATDB_VERSION_1,
  51. &fCreatedOK)) &&
  52. fCreatedOK &&
  53. (pCatMast->Initialize()))
  54. {
  55. pCatMast->UseRecNumAsKey(TRUE);
  56. DELETE_OBJECT(pwszPath);
  57. return;
  58. }
  59. }
  60. DELETE_OBJECT(pSysMast);
  61. DELETE_OBJECT(pCatMast);
  62. DELETE_OBJECT(pwszPath);
  63. }
  64. cCatalogDB_::~cCatalogDB_(void)
  65. {
  66. DELETE_OBJECT(pSysMast);
  67. DELETE_OBJECT(pCatMast);
  68. }
  69. BOOL cCatalogDB_::Initialize(void)
  70. {
  71. if ((pSysMast) &&
  72. (pCatMast))
  73. {
  74. return(TRUE);
  75. }
  76. return(FALSE);
  77. }
  78. ULONG cCatalogDB_::SysMast_GetNewId(void)
  79. {
  80. return(0L); // handled in the add....
  81. }
  82. DWORD cCatalogDB_::SysMast_NumKeys(void)
  83. {
  84. return(pSysMast->GetNumKeys());
  85. }
  86. BOOL cCatalogDB_::SysMast_Get(const GUID *pgSubSys, SysMast *psData)
  87. {
  88. SysMastRec *rec;
  89. memset(psData, 0x00, sizeof(SysMast));
  90. pSysMast->setKey((void *)pgSubSys);
  91. if (pSysMast->Find())
  92. {
  93. rec = (SysMastRec *)pSysMast->getData();
  94. MultiByteToWideChar(0, 0, rec->SubDir, -1, psData->SubDir, REG_MAX_GUID_TEXT);
  95. psData->SubDir[REG_MAX_GUID_TEXT - 1] = NULL;
  96. memcpy(&psData->SysGuid, &rec->SysGuid, sizeof(GUID));
  97. psData->SysId = rec->SysId;
  98. return(TRUE);
  99. }
  100. return(FALSE);
  101. }
  102. BOOL cCatalogDB_::SysMast_GetFirst(SysMast *psData)
  103. {
  104. SysMastRec *rec;
  105. memset(psData, 0x00, sizeof(SysMast));
  106. if (pSysMast->GetFirst())
  107. {
  108. rec = (SysMastRec *)pSysMast->getData();
  109. MultiByteToWideChar(0, 0, rec->SubDir, -1, psData->SubDir, REG_MAX_GUID_TEXT);
  110. psData->SubDir[REG_MAX_GUID_TEXT - 1] = NULL;
  111. memcpy((void *)&psData->SysGuid, &rec->SysGuid, sizeof(GUID));
  112. psData->SysId = rec->SysId;
  113. return(TRUE);
  114. }
  115. return(FALSE);
  116. }
  117. BOOL cCatalogDB_::SysMast_GetNext(SysMast *psData)
  118. {
  119. SysMastRec *rec;
  120. memset(psData, 0x00, sizeof(SysMast));
  121. if (pSysMast->GetNext())
  122. {
  123. rec = (SysMastRec *)pSysMast->getData();
  124. MultiByteToWideChar(0, 0, rec->SubDir, -1, psData->SubDir, REG_MAX_GUID_TEXT);
  125. psData->SubDir[REG_MAX_GUID_TEXT - 1] = NULL;
  126. memcpy((void *)&psData->SysGuid, &rec->SysGuid, sizeof(GUID));
  127. psData->SysId = rec->SysId;
  128. return(TRUE);
  129. }
  130. return(FALSE);
  131. }
  132. BOOL cCatalogDB_::SysMast_Add(SysMast *psData)
  133. {
  134. SysMastRec rec;
  135. memset(&rec, 0x00, sizeof(SysMastRec));
  136. WideCharToMultiByte(0, 0, psData->SubDir, -1, &rec.SubDir[0], REG_MAX_GUID_TEXT, NULL, NULL);
  137. rec.SubDir[REG_MAX_GUID_TEXT - 1] = NULL;
  138. memcpy(&rec.SysGuid, &psData->SysGuid, sizeof(GUID));
  139. rec.SysId = psData->SysId;
  140. pSysMast->setKey(&psData->SysGuid);
  141. pSysMast->setData(&rec);
  142. if (pSysMast->Add())
  143. {
  144. //
  145. // because the id is the record number, we need to add it first, get the rec
  146. // number, force an index update, then update the record.
  147. //
  148. psData->SysId = pSysMast->getRecNum();
  149. memcpy(&rec, pSysMast->getData(), sizeof(SysMastRec));
  150. rec.SysId = psData->SysId;
  151. pSysMast->Sort(); // force update of the index!
  152. pSysMast->setKey(&psData->SysGuid);
  153. pSysMast->setData(&rec);
  154. pSysMast->Update();
  155. return(TRUE);
  156. }
  157. return(FALSE);
  158. }
  159. ULONG cCatalogDB_::CatMast_GetNewId(void)
  160. {
  161. return(0L); // handled in the add....
  162. }
  163. BOOL cCatalogDB_::CatMast_Get(DWORD ulId, CatMast *psData)
  164. {
  165. CatMastRec *rec;
  166. memset(psData, 0x00, sizeof(CatMast));
  167. pCatMast->setKey(&ulId);
  168. if (pCatMast->Find())
  169. {
  170. rec = (CatMastRec *)pCatMast->getData();
  171. MultiByteToWideChar(0, 0, rec->OrigName, -1, psData->OrigName, MAX_PATH);
  172. psData->OrigName[MAX_PATH - 1] = NULL;
  173. MultiByteToWideChar(0, 0, rec->CurName, -1, psData->CurName, MAX_PATH);
  174. psData->CurName[MAX_PATH - 1] = NULL;
  175. psData->CatId = rec->CatId;
  176. psData->SysId = rec->SysId;
  177. memcpy(&psData->InstDate, &rec->InstDate, sizeof(FILETIME));
  178. return(TRUE);
  179. }
  180. return(FALSE);
  181. }
  182. BOOL cCatalogDB_::CatMast_Add(CatMast *psData)
  183. {
  184. CatMastRec rec;
  185. memset(&rec, 0x00, sizeof(CatMastRec));
  186. WideCharToMultiByte(0, 0, psData->OrigName, -1, &rec.OrigName[0], MAX_PATH, NULL, NULL);
  187. rec.OrigName[MAX_PATH - 1] = NULL;
  188. WideCharToMultiByte(0, 0, psData->CurName, -1, &rec.CurName[0], MAX_PATH, NULL, NULL);
  189. rec.CurName[MAX_PATH - 1] = NULL;
  190. rec.SysId = psData->SysId;
  191. pCatMast->setData(&rec);
  192. if (pCatMast->Add())
  193. {
  194. memcpy(&rec, pCatMast->getData(), sizeof(CatMastRec));
  195. psData->CatId = pCatMast->getRecNum();
  196. rec.CatId = psData->CatId;
  197. pCatMast->Sort(); // force index update.
  198. if (!(rec.CurName[0]))
  199. {
  200. sprintf(&rec.CurName[0], "%08.8lX.CAT", rec.CatId);
  201. MultiByteToWideChar(0, 0, &rec.CurName[0], -1, psData->CurName, MAX_PATH);
  202. psData->CurName[MAX_PATH - 1] = NULL;
  203. }
  204. pCatMast->setKey(&psData->CatId);
  205. pCatMast->setData(&rec);
  206. pCatMast->Update();
  207. return(TRUE);
  208. }
  209. return(FALSE);
  210. }
  211. cHashDB_::cHashDB_(WCHAR *pwszBase, WCHAR *pwszSubSysDirIn, BOOL *pfCreatedOK)
  212. {
  213. *pfCreatedOK = TRUE;
  214. pwszSubSysDir = new WCHAR[wcslen(pwszBase) + wcslen(pwszSubSysDirIn) + 2];
  215. if (pwszSubSysDir)
  216. {
  217. BOOL fCreatedOK;
  218. wcscpy(pwszSubSysDir, pwszBase);
  219. wcscat(pwszSubSysDir, pwszSubSysDirIn);
  220. pHashMast = new cBFile_(&MSCAT_CriticalSection, pwszSubSysDir,
  221. HASHMAST_NAME, MAX_HASH_LEN, sizeof(HashMastRec), CATDB_VERSION_1, pfCreatedOK);
  222. if (pHashMast == NULL)
  223. {
  224. *pfCreatedOK = FALSE;
  225. }
  226. }
  227. else
  228. {
  229. *pfCreatedOK = FALSE;
  230. }
  231. }
  232. cHashDB_::~cHashDB_(void)
  233. {
  234. DELETE_OBJECT(pHashMast);
  235. DELETE_OBJECT(pwszSubSysDir);
  236. }
  237. BOOL cHashDB_::Initialize(void)
  238. {
  239. if (pHashMast)
  240. {
  241. return(TRUE);
  242. }
  243. return(FALSE);
  244. }
  245. BOOL cHashDB_::HashMast_Get(DWORD dwLastRec, BYTE *pbHash, DWORD cbHash, HashMast *psData)
  246. {
  247. HashMastRec *rec;
  248. BYTE bHash[MAX_HASH_LEN];
  249. BOOL fRet;
  250. if (cbHash > MAX_HASH_LEN)
  251. {
  252. return(FALSE);
  253. }
  254. memset(&bHash[0], 0x00, MAX_HASH_LEN);
  255. memcpy(&bHash[0], pbHash, cbHash);
  256. memset(psData, 0x00, sizeof(HashMast));
  257. pHashMast->setKey(&bHash[0]);
  258. if (dwLastRec == 0xFFFFFFFF)
  259. {
  260. fRet = pHashMast->Find();
  261. }
  262. else
  263. {
  264. fRet = pHashMast->GetNext(dwLastRec);
  265. }
  266. if (fRet)
  267. {
  268. if (memcmp(&bHash[0], pHashMast->getKey(), MAX_HASH_LEN) != 0)
  269. {
  270. return(FALSE);
  271. }
  272. rec = (HashMastRec *)pHashMast->getData();
  273. MultiByteToWideChar(0, 0, rec->CatName, -1, psData->CatName, MAX_PATH);
  274. psData->CatName[MAX_PATH - 1] = NULL;
  275. return(TRUE);
  276. }
  277. return(FALSE);
  278. }
  279. BOOL cHashDB_::HashMast_Add(HashMast *psData)
  280. {
  281. HashMastRec rec;
  282. memset(&rec, 0x00, sizeof(HashMastRec));
  283. WideCharToMultiByte(0, 0, psData->CatName, -1, &rec.CatName[0], MAX_PATH, NULL, NULL);
  284. rec.CatName[MAX_PATH - 1] = NULL;
  285. pHashMast->setKey(&psData->Hash[0]);
  286. pHashMast->setData(&rec);
  287. if (pHashMast->Add())
  288. {
  289. return(TRUE);
  290. }
  291. return(FALSE);
  292. }