ReactOS 0.4.17-dev-218-g5635d24
rw.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/client/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 <k32.h>
14#define NDEBUG
15#include <debug.h>
17
18/* FUNCTIONS ****************************************************************/
19
20/*
21 * @implemented
22 */
23BOOL
29 _Out_opt_ LPDWORD lpNumberOfBytesWritten,
31{
33
34 TRACE("WriteFile(hFile %p)\n", hFile);
35
36 if (lpNumberOfBytesWritten != NULL) *lpNumberOfBytesWritten = 0;
37
39
41 {
42 return WriteConsoleA(hFile,
45 lpNumberOfBytesWritten,
47 }
48
49 if (lpOverlapped != NULL)
50 {
53
54 Offset.u.LowPart = lpOverlapped->Offset;
55 Offset.u.HighPart = lpOverlapped->OffsetHigh;
56 lpOverlapped->Internal = STATUS_PENDING;
57 ApcContext = (((ULONG_PTR)lpOverlapped->hEvent & 0x1) ? NULL : lpOverlapped);
58
60 lpOverlapped->hEvent,
61 NULL,
66 &Offset,
67 NULL);
68
69 /* return FALSE in case of failure and pending operations! */
71 {
73 return FALSE;
74 }
75
76 if (lpNumberOfBytesWritten != NULL)
77 *lpNumberOfBytesWritten = lpOverlapped->InternalHigh;
78 }
79 else
80 {
81 IO_STATUS_BLOCK Iosb = { 0 };
82
84 NULL,
85 NULL,
86 NULL,
87 &Iosb,
90 NULL,
91 NULL);
92
93 /* Wait in case operation is pending */
95 {
97 if (NT_SUCCESS(Status)) Status = Iosb.Status;
98 }
99
100 /*
101 * Windows 2003 and Vista do not check for lpNumberOfBytesWritten == NULL,
102 * but Windows 8+ does and Wine code (crypt32) relies on this.
103 * We ignore Hyrum's Law and assume that crashing here is not a
104 * behavior that software relies on.
105 */
106 if (lpNumberOfBytesWritten != NULL)
107 *lpNumberOfBytesWritten = Iosb.Information;
108
109 if (!NT_SUCCESS(Status))
110 {
112 return FALSE;
113 }
114 }
115
116 TRACE("WriteFile() succeeded\n");
117 return TRUE;
118}
119
120
121/*
122 * @implemented
123 */
124BOOL
125WINAPI
128 _Out_writes_bytes_to_opt_(nNumberOfBytesToRead, *lpNumberOfBytesRead) __out_data_source(FILE) LPVOID lpBuffer,
129 _In_ DWORD nNumberOfBytesToRead,
130 _Out_opt_ LPDWORD lpNumberOfBytesRead,
132{
134
135 TRACE("ReadFile(hFile %p)\n", hFile);
136
137 if (lpNumberOfBytesRead != NULL) *lpNumberOfBytesRead = 0;
138
140
142 {
144 lpBuffer,
145 nNumberOfBytesToRead,
146 lpNumberOfBytesRead,
147 NULL))
148 {
149 DWORD dwMode;
150 GetConsoleMode(hFile, &dwMode);
151 if ((dwMode & ENABLE_PROCESSED_INPUT) && *(PCHAR)lpBuffer == 0x1a)
152 {
153 /* EOF character entered; simulate end-of-file */
154 *lpNumberOfBytesRead = 0;
155 }
156 return TRUE;
157 }
158 return FALSE;
159 }
160
161 if (lpOverlapped != NULL)
162 {
165
166 Offset.u.LowPart = lpOverlapped->Offset;
167 Offset.u.HighPart = lpOverlapped->OffsetHigh;
168 lpOverlapped->Internal = STATUS_PENDING;
169 ApcContext = (((ULONG_PTR)lpOverlapped->hEvent & 0x1) ? NULL : lpOverlapped);
170
172 lpOverlapped->hEvent,
173 NULL,
176 lpBuffer,
177 nNumberOfBytesToRead,
178 &Offset,
179 NULL);
180
181 /* return FALSE in case of failure and pending operations! */
183 {
184 if (Status == STATUS_END_OF_FILE && lpNumberOfBytesRead != NULL)
185 *lpNumberOfBytesRead = 0;
186
188 return FALSE;
189 }
190
191 if (lpNumberOfBytesRead != NULL)
192 *lpNumberOfBytesRead = lpOverlapped->InternalHigh;
193 }
194 else
195 {
196 IO_STATUS_BLOCK Iosb = { 0 };
197
199 NULL,
200 NULL,
201 NULL,
202 &Iosb,
203 lpBuffer,
204 nNumberOfBytesToRead,
205 NULL,
206 NULL);
207
208 /* Wait in case operation is pending */
209 if (Status == STATUS_PENDING)
210 {
212 if (NT_SUCCESS(Status)) Status = Iosb.Status;
213 }
214
216 {
217 /*
218 * Windows 2003 and Vista do not check for lpNumberOfBytesRead == NULL,
219 * but Windows 8+ does. Avoid crashing.
220 */
221 if (lpNumberOfBytesRead != NULL)
222 *lpNumberOfBytesRead = 0;
223 return TRUE;
224 }
225
226 /*
227 * Windows 2003 and Vista do not check for lpNumberOfBytesRead == NULL,
228 * but Windows 8+ does. Avoid crashing.
229 */
230 if (lpNumberOfBytesRead != NULL)
231 *lpNumberOfBytesRead = Iosb.Information;
232
233 if (!NT_SUCCESS(Status))
234 {
236 return FALSE;
237 }
238 }
239
240 TRACE("ReadFile() succeeded\n");
241 return TRUE;
242}
243
248{
249 DWORD dwErrorCode;
250 LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine =
252
254 lpCompletionRoutine(dwErrorCode,
257}
258
259
260/*
261 * @implemented
262 */
268 IN LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
269{
272
273 Offset.u.LowPart = lpOverlapped->Offset;
274 Offset.u.HighPart = lpOverlapped->OffsetHigh;
275 lpOverlapped->Internal = STATUS_PENDING;
276
278 NULL,
280 lpCompletionRoutine,
284 &Offset,
285 NULL);
286
287 if (!NT_SUCCESS(Status))
288 {
290 return FALSE;
291 }
292
293 return TRUE;
294}
295
296
297/*
298 * @implemented
299 */
303 IN DWORD nNumberOfBytesToRead OPTIONAL,
305 IN LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
306{
309
310 Offset.u.LowPart = lpOverlapped->Offset;
311 Offset.u.HighPart = lpOverlapped->OffsetHigh;
312 lpOverlapped->Internal = STATUS_PENDING;
313
315 NULL,
317 lpCompletionRoutine,
319 lpBuffer,
320 nNumberOfBytesToRead,
321 &Offset,
322 NULL);
323
324 if (!NT_SUCCESS(Status))
325 {
327 return FALSE;
328 }
329
330 return TRUE;
331}
332
333
334/*
335 * @implemented
336 */
337BOOL
338WINAPI
340 FILE_SEGMENT_ELEMENT aSegmentArray[],
341 DWORD nNumberOfBytesToRead,
344{
345 PIO_STATUS_BLOCK pIOStatus;
348
349 DPRINT("(%p %p %u %p)\n", hFile, aSegmentArray, nNumberOfBytesToRead, lpOverlapped);
350
351 Offset.LowPart = lpOverlapped->Offset;
352 Offset.HighPart = lpOverlapped->OffsetHigh;
353 pIOStatus = (PIO_STATUS_BLOCK) lpOverlapped;
354 pIOStatus->Status = STATUS_PENDING;
355 pIOStatus->Information = 0;
356
358 NULL,
359 NULL,
360 NULL,
361 pIOStatus,
362 aSegmentArray,
363 nNumberOfBytesToRead,
364 &Offset,
365 NULL);
366
367 if (!NT_SUCCESS(Status))
368 {
370 return FALSE;
371 }
372
373 return TRUE;
374}
375
376/*
377 * @implemented
378 */
379BOOL
380WINAPI
382 FILE_SEGMENT_ELEMENT aSegmentArray[],
386{
387 PIO_STATUS_BLOCK IOStatus;
390
391 DPRINT("%p %p %u %p\n", hFile, aSegmentArray, nNumberOfBytesToWrite, lpOverlapped);
392
393 Offset.LowPart = lpOverlapped->Offset;
394 Offset.HighPart = lpOverlapped->OffsetHigh;
395 IOStatus = (PIO_STATUS_BLOCK) lpOverlapped;
396 IOStatus->Status = STATUS_PENDING;
397 IOStatus->Information = 0;
398
400 NULL,
401 NULL,
402 NULL,
403 IOStatus,
404 aSegmentArray,
406 &Offset,
407 NULL);
408
409 if (!NT_SUCCESS(Status))
410 {
412 return FALSE;
413 }
414
415 return TRUE;
416}
417
418/* EOF */
LONG NTSTATUS
Definition: precomp.h:26
#define DEBUG_CHANNEL(args)
Definition: rdesktop.h:159
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define SetLastError(x)
Definition: compat.h:752
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1571
BOOL WINAPI DECLSPEC_HOTPATCH WriteConsoleA(IN HANDLE hConsoleOutput, IN CONST VOID *lpBuffer, IN DWORD nNumberOfCharsToWrite, OUT LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved)
Definition: readwrite.c:1468
BOOL WINAPI DECLSPEC_HOTPATCH ReadConsoleA(IN HANDLE hConsoleInput, OUT LPVOID lpBuffer, IN DWORD nNumberOfCharsToRead, OUT LPDWORD lpNumberOfCharsRead, IN PCONSOLE_READCONSOLE_CONTROL pInputControl OPTIONAL)
Definition: readwrite.c:1195
BOOL WINAPI WriteFileGather(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[], DWORD nNumberOfBytesToWrite, LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
Definition: rw.c:381
BOOL WINAPI WriteFile(_In_ HANDLE hFile, _In_reads_bytes_opt_(nNumberOfBytesToWrite) LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Out_opt_ LPDWORD lpNumberOfBytesWritten, _Inout_opt_ LPOVERLAPPED lpOverlapped)
Definition: rw.c:25
BOOL WINAPI ReadFileScatter(HANDLE hFile, FILE_SEGMENT_ELEMENT aSegmentArray[], DWORD nNumberOfBytesToRead, LPDWORD lpReserved, LPOVERLAPPED lpOverlapped)
Definition: rw.c:339
BOOL WINAPI ReadFileEx(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead OPTIONAL, IN LPOVERLAPPED lpOverlapped, IN LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
Definition: rw.c:301
BOOL WINAPI WriteFileEx(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, IN LPOVERLAPPED lpOverlapped, IN LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
Definition: rw.c:264
HANDLE TranslateStdHandle(IN HANDLE hHandle)
Definition: handle.c:19
return Iosb
Definition: create.c:4403
#define ULONG_PTR
Definition: config.h:101
struct _IO_STATUS_BLOCK * PIO_STATUS_BLOCK
Definition: change.c:34
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:24
NTSTATUS NTAPI NtReadFileScatter(IN HANDLE FileHandle, IN HANDLE Event OPTIONAL, IN PIO_APC_ROUTINE UserApcRoutine OPTIONAL, IN PVOID UserApcContext OPTIONAL, OUT PIO_STATUS_BLOCK UserIoStatusBlock, IN FILE_SEGMENT_ELEMENT BufferDescription[], IN ULONG BufferLength, IN PLARGE_INTEGER ByteOffset, IN PULONG Key OPTIONAL)
Definition: iofunc.c:3063
NTSTATUS NTAPI NtWriteFileGather(IN HANDLE FileHandle, IN HANDLE Event OPTIONAL, IN PIO_APC_ROUTINE UserApcRoutine OPTIONAL, IN PVOID UserApcContext OPTIONAL, OUT PIO_STATUS_BLOCK UserIoStatusBlock, IN FILE_SEGMENT_ELEMENT BufferDescription[], IN ULONG BufferLength, IN PLARGE_INTEGER ByteOffset, IN PULONG Key OPTIONAL)
Definition: iofunc.c:4132
#define kernel32file
Definition: kernel32.h:6
VOID(WINAPI * LPOVERLAPPED_COMPLETION_ROUTINE)(_In_ DWORD dwErrorCode, _In_ DWORD dwNumberOfBytesTransfered, _Inout_ LPOVERLAPPED lpOverlapped)
Definition: minwinbase.h:386
CONST void * LPCVOID
Definition: minwindef.h:164
_In_ HANDLE hFile
Definition: mswsock.h:90
_In_ HANDLE _In_ DWORD nNumberOfBytesToWrite
Definition: mswsock.h:91
_In_ HANDLE _In_ DWORD _In_ DWORD _Inout_opt_ LPOVERLAPPED lpOverlapped
Definition: mswsock.h:93
_In_opt_ HANDLE _In_opt_ PIO_APC_ROUTINE _In_opt_ PVOID ApcContext
Definition: iofuncs.h:727
_In_opt_ HANDLE _In_opt_ PIO_APC_ROUTINE ApcRoutine
Definition: iofuncs.h:726
_Out_ LPWSTR lpBuffer
Definition: netsh.h:68
#define _Out_opt_
Definition: no_sal2.h:214
#define _Inout_opt_
Definition: no_sal2.h:216
#define _In_
Definition: no_sal2.h:158
#define _Out_writes_bytes_to_opt_(s, c)
Definition: no_sal2.h:240
#define _In_reads_bytes_opt_(s)
Definition: no_sal2.h:224
NTSYSAPI NTSTATUS NTAPI NtWriteFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID WriteBuffer, IN ULONG WriteBufferLength, IN PLARGE_INTEGER FileOffset OPTIONAL, IN PULONG LockOperationKey OPTIONAL)
NTSYSAPI NTSTATUS NTAPI NtWaitForSingleObject(IN HANDLE hObject, IN BOOLEAN bAlertable, IN PLARGE_INTEGER Timeout)
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:100
DWORD BaseSetLastNTError(IN NTSTATUS Status)
Definition: reactos.cpp:167
#define IsConsoleHandle(h)
Definition: console.h:14
#define STATUS_END_OF_FILE
Definition: shellext.h:67
NTSTATUS NTAPI NtReadFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
#define DPRINT
Definition: sndvol32.h:73
#define TRACE(s)
Definition: solgame.cpp:4
#define __out_data_source(src_sym)
Definition: specstrings.h:349
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68
#define STATUS_PENDING
Definition: telnetd.h:14
uint32_t * LPDWORD
Definition: typedefs.h:59
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
char * PCHAR
Definition: typedefs.h:51
#define ENABLE_PROCESSED_INPUT
Definition: wincon.h:107
_Reserved_ PVOID Reserved
Definition: winddi.h:3974
#define WINAPI
Definition: msvc.h:6
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
_In_ DWORD _In_ int _In_ int _In_opt_ LPNLSVERSIONINFO _In_opt_ LPVOID lpReserved
Definition: winnls.h:1268