Miscellaneous implementation of kernel32 stubs.
[reactos.git] / reactos / lib / kernel32 / file / rw.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/kernel32/file/rw.c
5 * PURPOSE: Read/write functions
6 * PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
7 * UPDATE HISTORY:
8 * Created 01/11/98
9 */
10
11 /* INCLUDES ****************************************************************/
12
13 #include <windows.h>
14 #include <ddk/ntddk.h>
15 #include <wchar.h>
16 #include <string.h>
17
18 #define NDEBUG
19 #include <kernel32/kernel32.h>
20
21 /* FUNCTIONS ****************************************************************/
22
23
24
25
26 WINBOOL STDCALL WriteFile(HANDLE hFile,
27 LPCVOID lpBuffer,
28 DWORD nNumberOfBytesToWrite,
29 LPDWORD lpNumberOfBytesWritten,
30 LPOVERLAPPED lpOverLapped)
31 {
32 HANDLE hEvent = NULL;
33 LARGE_INTEGER Offset;
34 NTSTATUS errCode;
35 IO_STATUS_BLOCK IIosb;
36 PIO_STATUS_BLOCK IoStatusBlock;
37 PLARGE_INTEGER ptrOffset;
38
39 DPRINT("WriteFile(hFile %x)\n",hFile);
40
41 if (lpOverLapped != NULL)
42 {
43 Offset.u.LowPart = lpOverLapped->Offset;
44 Offset.u.HighPart = lpOverLapped->OffsetHigh;
45 lpOverLapped->Internal = STATUS_PENDING;
46 hEvent = lpOverLapped->hEvent;
47 IoStatusBlock = (PIO_STATUS_BLOCK)lpOverLapped;
48 ptrOffset = &Offset;
49 }
50 else
51 {
52 ptrOffset = NULL;
53 IoStatusBlock = &IIosb;
54 Offset.QuadPart = 0;
55 }
56
57 errCode = NtWriteFile(hFile,
58 hEvent,
59 NULL,
60 NULL,
61 IoStatusBlock,
62 (PVOID)lpBuffer,
63 nNumberOfBytesToWrite,
64 ptrOffset,
65 NULL);
66 if (!NT_SUCCESS(errCode))
67 {
68 SetLastError(RtlNtStatusToDosError(errCode));
69 DPRINT("WriteFile() failed\n");
70 return FALSE;
71 }
72 if (lpNumberOfBytesWritten != NULL )
73 {
74 *lpNumberOfBytesWritten = IoStatusBlock->Information;
75 }
76 DPRINT("WriteFile() succeeded\n");
77 return(TRUE);
78 }
79
80
81
82 WINBOOL STDCALL ReadFile(HANDLE hFile,
83 LPVOID lpBuffer,
84 DWORD nNumberOfBytesToRead,
85 LPDWORD lpNumberOfBytesRead,
86 LPOVERLAPPED lpOverLapped)
87 {
88 HANDLE hEvent = NULL;
89 LARGE_INTEGER Offset;
90 NTSTATUS errCode;
91 IO_STATUS_BLOCK IIosb;
92 PIO_STATUS_BLOCK IoStatusBlock;
93 PLARGE_INTEGER ptrOffset;
94
95 if (lpOverLapped != NULL)
96 {
97 Offset.u.LowPart = lpOverLapped->Offset;
98 Offset.u.HighPart = lpOverLapped->OffsetHigh;
99 lpOverLapped->Internal = STATUS_PENDING;
100 hEvent = lpOverLapped->hEvent;
101 IoStatusBlock = (PIO_STATUS_BLOCK)lpOverLapped;
102 ptrOffset = &Offset;
103 }
104 else
105 {
106 ptrOffset = NULL;
107 IoStatusBlock = &IIosb;
108 }
109
110 errCode = NtReadFile(hFile,
111 hEvent,
112 NULL,
113 NULL,
114 IoStatusBlock,
115 lpBuffer,
116 nNumberOfBytesToRead,
117 ptrOffset,
118 NULL);
119
120 if (errCode != STATUS_PENDING && lpNumberOfBytesRead != NULL)
121 {
122 *lpNumberOfBytesRead = IoStatusBlock->Information;
123 }
124
125 if (!NT_SUCCESS(errCode))
126 {
127 SetLastError(RtlNtStatusToDosError(errCode));
128 return(FALSE);
129 }
130 return(TRUE);
131 }
132
133 VOID ApcRoutine(PVOID ApcContext, struct _IO_STATUS_BLOCK* IoStatusBlock, ULONG NumberOfBytesTransfered)
134 {
135 DWORD dwErrorCode;
136 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine = (LPOVERLAPPED_COMPLETION_ROUTINE)ApcContext;
137
138 dwErrorCode = RtlNtStatusToDosError( IoStatusBlock->Status);
139 lpCompletionRoutine( dwErrorCode, NumberOfBytesTransfered, (LPOVERLAPPED)IoStatusBlock );
140 }
141
142
143 WINBOOL WriteFileEx(
144 HANDLE hFile,
145 LPCVOID lpBuffer,
146 DWORD nNumberOfBytesToWrite,
147 LPOVERLAPPED lpOverLapped,
148 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
149 )
150 {
151
152 LARGE_INTEGER Offset;
153 NTSTATUS errCode;
154 PIO_STATUS_BLOCK IoStatusBlock;
155 PLARGE_INTEGER ptrOffset;
156
157 DPRINT("WriteFileEx(hFile %x)\n",hFile);
158
159 if (lpOverLapped == NULL)
160 return FALSE;
161
162 Offset.u.LowPart = lpOverLapped->Offset;
163 Offset.u.HighPart = lpOverLapped->OffsetHigh;
164 lpOverLapped->Internal = STATUS_PENDING;
165 IoStatusBlock = (PIO_STATUS_BLOCK)lpOverLapped;
166 ptrOffset = &Offset;
167
168
169
170 errCode = NtWriteFile(hFile,
171 NULL,
172 ApcRoutine,
173 lpCompletionRoutine,
174 IoStatusBlock,
175 (PVOID)lpBuffer,
176 nNumberOfBytesToWrite,
177 ptrOffset,
178 NULL);
179 if (!NT_SUCCESS(errCode))
180 {
181 SetLastError(RtlNtStatusToDosError(errCode));
182 DPRINT("WriteFileEx() failed\n");
183 return FALSE;
184 }
185
186 DPRINT("WriteFileEx() succeeded\n");
187 return(TRUE);
188 }
189
190 WINBOOL STDCALL ReadFileEx(HANDLE hFile,
191 LPVOID lpBuffer,
192 DWORD nNumberOfBytesToRead,
193 LPOVERLAPPED lpOverLapped,
194 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
195 {
196 LARGE_INTEGER Offset;
197 NTSTATUS errCode;
198 PIO_STATUS_BLOCK IoStatusBlock;
199 PLARGE_INTEGER ptrOffset;
200
201 if (lpOverLapped == NULL)
202 return FALSE;
203
204 Offset.u.LowPart = lpOverLapped->Offset;
205 Offset.u.HighPart = lpOverLapped->OffsetHigh;
206 lpOverLapped->Internal = STATUS_PENDING;
207 IoStatusBlock = (PIO_STATUS_BLOCK)lpOverLapped;
208 ptrOffset = &Offset;
209
210
211 errCode = NtReadFile(hFile,
212 NULL,
213 ApcRoutine,
214 lpCompletionRoutine,
215 IoStatusBlock,
216 lpBuffer,
217 nNumberOfBytesToRead,
218 ptrOffset,
219 NULL);
220
221
222 if (!NT_SUCCESS(errCode))
223 {
224 SetLastError(RtlNtStatusToDosError(errCode));
225 return(FALSE);
226 }
227 return(TRUE);
228 }