Leaked source code of windows server 2003
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.

144 lines
3.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: catutil.cpp
  8. //
  9. // Contents: catalog database common functions
  10. //
  11. // History: 01-may-2000 reidk created
  12. //
  13. //--------------------------------------------------------------------------
  14. #include "global.hxx"
  15. #include "catutil.h"
  16. //---------------------------------------------------------------------------------------
  17. //
  18. // CatUtil_CreateCTLContextFromFileName
  19. //
  20. //---------------------------------------------------------------------------------------
  21. BOOL
  22. CatUtil_CreateCTLContextFromFileName(
  23. LPCWSTR pwszFileName,
  24. HANDLE *phMappedFile,
  25. BYTE **ppbMappedFile,
  26. PCCTL_CONTEXT *ppCTLContext,
  27. BOOL fCreateSorted)
  28. {
  29. BOOL fRet = TRUE;
  30. HANDLE hFile = INVALID_HANDLE_VALUE;
  31. DWORD cbFile = 0;
  32. HANDLE hMappedFile = NULL;
  33. BYTE *pbMappedFile = NULL;
  34. //
  35. // Initialize out params
  36. //
  37. *phMappedFile = NULL;
  38. *ppbMappedFile = NULL;
  39. *ppCTLContext = NULL;
  40. //
  41. // Open the existing catalog file
  42. //
  43. hFile = CreateFileW(
  44. pwszFileName,
  45. GENERIC_READ,
  46. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  47. NULL,
  48. OPEN_EXISTING,
  49. FILE_ATTRIBUTE_NORMAL,
  50. NULL);
  51. if (hFile == INVALID_HANDLE_VALUE)
  52. {
  53. goto ErrorCreateFile;
  54. }
  55. cbFile = GetFileSize(hFile, NULL);
  56. if (cbFile == 0xFFFFFFFF)
  57. {
  58. goto ErrorGetFileSize;
  59. }
  60. hMappedFile = CreateFileMapping(
  61. hFile,
  62. NULL,
  63. PAGE_READONLY,
  64. 0,
  65. 0,
  66. NULL);
  67. if (hMappedFile == NULL)
  68. {
  69. goto ErrorCreateFileMapping;
  70. }
  71. pbMappedFile = (BYTE *) MapViewOfFile(
  72. hMappedFile,
  73. FILE_MAP_READ,
  74. 0,
  75. 0,
  76. cbFile);
  77. if (pbMappedFile == NULL)
  78. {
  79. goto ErrorMapViewOfFile;
  80. }
  81. //
  82. // Don't need the file handle since we have a mapped file handle
  83. //
  84. CloseHandle(hFile);
  85. hFile = INVALID_HANDLE_VALUE;
  86. if (NULL == (*ppCTLContext = (PCCTL_CONTEXT)
  87. CertCreateContext(
  88. CERT_STORE_CTL_CONTEXT,
  89. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  90. pbMappedFile,
  91. cbFile,
  92. fCreateSorted ?
  93. CERT_CREATE_CONTEXT_NOCOPY_FLAG | CERT_CREATE_CONTEXT_SORTED_FLAG :
  94. CERT_CREATE_CONTEXT_NOCOPY_FLAG,
  95. NULL)))
  96. {
  97. goto ErrorCertCreateContect;
  98. }
  99. *phMappedFile = hMappedFile;
  100. *ppbMappedFile = pbMappedFile;
  101. CommonReturn:
  102. if (hFile != INVALID_HANDLE_VALUE)
  103. {
  104. CloseHandle(hFile);
  105. }
  106. return fRet;
  107. ErrorReturn:
  108. if (pbMappedFile != NULL)
  109. {
  110. UnmapViewOfFile(pbMappedFile);
  111. }
  112. if (hMappedFile != NULL)
  113. {
  114. CloseHandle(hMappedFile);
  115. }
  116. fRet = FALSE;
  117. goto CommonReturn;
  118. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorCreateFile)
  119. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorGetFileSize)
  120. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorCreateFileMapping)
  121. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMapViewOfFile)
  122. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorCertCreateContect)
  123. }