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.

149 lines
4.6 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 2001 - 2001
  5. //
  6. // File: fileutil.cpp
  7. //
  8. // Contents: File utility functions used by the minimal cryptographic
  9. // APIs.
  10. //
  11. // Functions: I_MinCryptMapFile
  12. //
  13. // History: 21-Jan-01 philh created
  14. //--------------------------------------------------------------------------
  15. #include "global.hxx"
  16. #define I_CreateFileU CreateFileW
  17. //+-------------------------------------------------------------------------
  18. // Maps the file into memory.
  19. //
  20. // According to dwFileType, pvFile can be a pwszFilename, hFile or pFileBlob.
  21. // Only READ access is required.
  22. //
  23. // dwFileType:
  24. // MINCRYPT_FILE_NAME : pvFile - LPCWSTR pwszFilename
  25. // MINCRYPT_FILE_HANDLE : pvFile - HANDLE hFile
  26. // MINCRYPT_FILE_BLOB : pvFile - PCRYPT_DATA_BLOB pFileBlob
  27. //
  28. // *pFileBlob is updated with pointer to and length of the mapped file. For
  29. // MINCRYPT_FILE_NAME and MINCRYPT_FILE_HANDLE, UnmapViewOfFile() must
  30. // be called to free pFileBlob->pbData.
  31. //
  32. // All accesses to this mapped memory must be within __try / __except's.
  33. //--------------------------------------------------------------------------
  34. LONG
  35. WINAPI
  36. I_MinCryptMapFile(
  37. IN DWORD dwFileType,
  38. IN const VOID *pvFile,
  39. OUT PCRYPT_DATA_BLOB pFileBlob
  40. )
  41. {
  42. LONG lErr = ERROR_SUCCESS;
  43. switch (dwFileType) {
  44. case MINCRYPT_FILE_NAME:
  45. {
  46. LPCWSTR pwszInFilename = (LPCWSTR) pvFile;
  47. HANDLE hFile;
  48. hFile = I_CreateFileU(
  49. pwszInFilename,
  50. GENERIC_READ,
  51. FILE_SHARE_READ,
  52. NULL, // lpsa
  53. OPEN_EXISTING,
  54. FILE_ATTRIBUTE_NORMAL,
  55. NULL // hTemplateFile
  56. );
  57. if (INVALID_HANDLE_VALUE == hFile)
  58. goto CreateFileError;
  59. lErr = I_MinCryptMapFile(
  60. MINCRYPT_FILE_HANDLE,
  61. (const VOID *) hFile,
  62. pFileBlob
  63. );
  64. CloseHandle(hFile);
  65. }
  66. break;
  67. case MINCRYPT_FILE_HANDLE:
  68. {
  69. HANDLE hInFile = (HANDLE) pvFile;
  70. HANDLE hMappedFile;
  71. DWORD cbHighSize = 0;;
  72. DWORD cbLowSize;
  73. cbLowSize = GetFileSize(hInFile, &cbHighSize);
  74. if (INVALID_FILE_SIZE == cbLowSize)
  75. goto GetFileSizeError;
  76. if (0 != cbHighSize)
  77. goto Exceeded32BitFileSize;
  78. hMappedFile = CreateFileMappingA(
  79. hInFile,
  80. NULL, // lpFileMappingAttributes,
  81. PAGE_READONLY,
  82. 0, // dwMaximumSizeHigh
  83. 0, // dwMaximumSizeLow
  84. NULL // lpName
  85. );
  86. if (NULL == hMappedFile)
  87. goto CreateFileMappingError;
  88. pFileBlob->pbData = (BYTE *) MapViewOfFile(
  89. hMappedFile,
  90. FILE_MAP_READ,
  91. 0, // dwFileOffsetHigh
  92. 0, // dwFileOffsetLow
  93. 0 // dwNumberOfBytesToMap, 0 => entire file
  94. );
  95. CloseHandle(hMappedFile);
  96. if (NULL == pFileBlob->pbData)
  97. goto MapViewOfFileError;
  98. pFileBlob->cbData = cbLowSize;
  99. }
  100. break;
  101. case MINCRYPT_FILE_BLOB:
  102. {
  103. PCRYPT_DATA_BLOB pInFileBlob = (PCRYPT_DATA_BLOB) pvFile;
  104. *pFileBlob = *pInFileBlob;
  105. }
  106. break;
  107. default:
  108. goto InvalidParameter;
  109. }
  110. CommonReturn:
  111. return lErr;
  112. ErrorReturn:
  113. assert(ERROR_SUCCESS != lErr);
  114. pFileBlob->pbData = NULL;
  115. pFileBlob->cbData = 0;
  116. goto CommonReturn;
  117. InvalidParameter:
  118. lErr = ERROR_INVALID_PARAMETER;
  119. goto ErrorReturn;
  120. Exceeded32BitFileSize:
  121. lErr = ERROR_FILE_INVALID;
  122. goto ErrorReturn;
  123. CreateFileError:
  124. GetFileSizeError:
  125. CreateFileMappingError:
  126. MapViewOfFileError:
  127. lErr = GetLastError();
  128. if (ERROR_SUCCESS == lErr)
  129. lErr = ERROR_OPEN_FAILED;
  130. goto ErrorReturn;
  131. }