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.

148 lines
4.5 KiB

  1. /*++
  2. Module Name:
  3. fileio.c
  4. Abstract:
  5. Environment:
  6. kernel mode only
  7. Notes:
  8. Revision History:
  9. --*/
  10. #include <ntddk.h>
  11. #include <wdmguid.h>
  12. #include <ntddser.h>
  13. #include <initguid.h>
  14. #include "mxenum.h"
  15. #ifdef _X86_
  16. NTSTATUS Win2KOpenFile(PWCHAR filename, BOOLEAN read, PHANDLE phandle);
  17. NTSTATUS Win2KCloseFile(HANDLE handle);
  18. unsigned __int64 Win2KGetFileSize(HANDLE handle);
  19. NTSTATUS Win2KReadFile(HANDLE handle, PVOID buffer, ULONG nbytes, PULONG pnumread);
  20. NTSTATUS Win2KWriteFile(HANDLE handle, PVOID buffer, ULONG nbytes, PULONG pnumread);
  21. ///////////////////////////////////////////////////////////////////////////////
  22. NTSTATUS MxenumOpenFile(PWCHAR filename, BOOLEAN read, PHANDLE phandle)
  23. { // OpenFile
  24. return Win2KOpenFile(filename, read, phandle);
  25. } // OpenFile
  26. ///////////////////////////////////////////////////////////////////////////////
  27. NTSTATUS MxenumCloseFile(HANDLE handle)
  28. { // CloseFile
  29. return Win2KCloseFile(handle);
  30. } // CloseFile
  31. ///////////////////////////////////////////////////////////////////////////////
  32. unsigned __int64 MxenumGetFileSize(HANDLE handle)
  33. { // GetFileSize
  34. return Win2KGetFileSize(handle);
  35. } // GetFileSize
  36. ///////////////////////////////////////////////////////////////////////////////
  37. NTSTATUS MxenumReadFile(HANDLE handle, PVOID buffer, ULONG nbytes, PULONG pnumread)
  38. { // ReadFile
  39. return Win2KReadFile(handle, buffer, nbytes, pnumread);
  40. } // ReadFile
  41. ///////////////////////////////////////////////////////////////////////////////
  42. NTSTATUS MxenumWriteFile(HANDLE handle, PVOID buffer, ULONG nbytes, PULONG pnumwritten)
  43. { // WriteFile
  44. return Win2KWriteFile(handle, buffer, nbytes, pnumwritten);
  45. } // WriteFile
  46. ///////////////////////////////////////////////////////////////////////////////
  47. #else // not _X86_
  48. #define Win2KOpenFile OpenFile
  49. #define Win2KCloseFile CloseFile
  50. #define Win2KGetFileSize GetFileSize
  51. #define Win2KReadFile ReadFile
  52. #define Win2KWriteFile WriteFile
  53. #endif // not _X86_
  54. ///////////////////////////////////////////////////////////////////////////////
  55. ///////////////////////////////////////////////////////////////////////////////
  56. NTSTATUS Win2KOpenFile(PWCHAR filename, BOOLEAN read, PHANDLE phandle)
  57. { // Win2KOpenFile
  58. NTSTATUS status;
  59. OBJECT_ATTRIBUTES oa;
  60. UNICODE_STRING usname;
  61. HANDLE hfile;
  62. IO_STATUS_BLOCK iostatus;
  63. RtlInitUnicodeString(&usname, filename);
  64. InitializeObjectAttributes(&oa, &usname, OBJ_CASE_INSENSITIVE, NULL, NULL);
  65. if (read)
  66. status = ZwCreateFile(&hfile, GENERIC_READ, &oa, &iostatus, NULL,
  67. 0, FILE_SHARE_READ, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
  68. else
  69. status = ZwCreateFile(&hfile, GENERIC_WRITE, &oa, &iostatus, NULL,
  70. FILE_ATTRIBUTE_NORMAL, 0, FILE_OVERWRITE_IF, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
  71. if (NT_SUCCESS(status))
  72. *phandle = hfile;
  73. return status;
  74. } // Win2KOpenFile
  75. ///////////////////////////////////////////////////////////////////////////////
  76. NTSTATUS Win2KCloseFile(HANDLE handle)
  77. { // Win2KCloseFile
  78. return ZwClose(handle);
  79. } // Win2KCloseFile
  80. ///////////////////////////////////////////////////////////////////////////////
  81. unsigned __int64 Win2KGetFileSize(HANDLE handle)
  82. { // Win2KGetFileSize
  83. NTSTATUS status;
  84. IO_STATUS_BLOCK iostatus;
  85. FILE_STANDARD_INFORMATION fi;
  86. status = ZwQueryInformationFile(handle, &iostatus, (PVOID) &fi, sizeof(fi), FileStandardInformation);
  87. if (!NT_SUCCESS(status))
  88. return 0;
  89. return fi.EndOfFile.QuadPart;
  90. } // Win2KGetFileSize
  91. ///////////////////////////////////////////////////////////////////////////////
  92. NTSTATUS Win2KReadFile(HANDLE handle, PVOID buffer, ULONG nbytes, PULONG pnumread)
  93. { // Win2KReadFile
  94. IO_STATUS_BLOCK iostatus;
  95. ZwReadFile(handle, NULL, NULL, NULL, &iostatus, buffer, nbytes, NULL, NULL);
  96. if (NT_SUCCESS(iostatus.Status))
  97. *pnumread = iostatus.Information;
  98. return iostatus.Status;
  99. } // Win2KReadFile
  100. ///////////////////////////////////////////////////////////////////////////////
  101. NTSTATUS Win2KWriteFile(HANDLE handle, PVOID buffer, ULONG nbytes, PULONG pnumwritten)
  102. { // Win2KWriteFile
  103. IO_STATUS_BLOCK iostatus;
  104. ZwWriteFile(handle, NULL, NULL, NULL, &iostatus, buffer, nbytes, NULL, NULL);
  105. if (NT_SUCCESS(iostatus.Status))
  106. *pnumwritten = iostatus.Information;
  107. return iostatus.Status;
  108. } // Win2KWriteFile