ReactOS 0.4.16-dev-822-gbcedb53
corecrt_internal_lowio.h
Go to the documentation of this file.
1//
2// corecrt_internal_lowio.h
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// This internal header defines internal utilities for working with the lowio
7// library. This header may only be included in C++ translation units.
8//
9#pragma once
10
11#include <fcntl.h>
12#include <io.h>
14#include <share.h>
15#include <stdint.h>
16#include <stdlib.h>
17
19
20
21
22#define LF 10 /* line feed */
23#define CR 13 /* carriage return */
24#define CTRLZ 26 /* ctrl-z means eof for text */
25
26// Real default size for stdio buffers
27#define _INTERNAL_BUFSIZ 4096
28#define _SMALL_BUFSIZ 512
29
30/* Most significant Bit */
31#define _msbit(c) ((c) & 0x80)
32
33/* Independent byte has most significant bit set to 0 */
34#define _utf8_is_independent(c) (_msbit(c) == 0)
35
36/* Get no of trailing bytes from the lookup table */
37// 1 for pattern 110xxxxx - 1 trailbyte
38// 2 for pattern 1110xxxx - 2 trailbytes
39// 3 for pattern 11110xxx - 3 trailbytes
40// 0 for everything else, including invalid patterns.
41// We return 0 for invalid patterns because we rely on MultiByteToWideChar to
42// do the validations.
43
44extern char _lookuptrailbytes[256];
45__inline char _utf8_no_of_trailbytes(const unsigned char c)
46{
47 return _lookuptrailbytes[c];
48}
49// It may be faster to just look up the bytes than to use the lookup table.
50//__inline char _utf8_no_of_trailbytes(const unsigned char c)
51//{
52// // ASCII range is a single character
53// if ((c & 0x80) == 0) return 0;
54// // Trail bytes 10xxxxxx aren't lead bytes
55// if ((c & 0x40) == 0) return 0;
56// // 110xxxxx is a 2 byte sequence (1 trail byte)
57// if ((c & 0x20) == 0) return 1;
58// // 1110xxxx is a 3 byte sequence (2 trail bytes)
59// if ((c & 0x10) == 0) return 2;
60// // 11110xxx is a 4 byte sequence (3 trail bytes)
61// if ((c & 0x08) == 0) return 3;
62// // Anything with 5 or more lead bits is illegal
63// return 0;
64//}
65
66/* Any leadbyte will have the patterns 11000xxx 11100xxx or 11110xxx */
67#define _utf8_is_leadbyte(c) (_utf8_no_of_trailbytes(static_cast<const unsigned char>(c)) != 0)
68
69enum class __crt_lowio_text_mode : char
70{
71 ansi = 0, // Regular text
72 utf8 = 1, // UTF-8 encoded
73 utf16le = 2, // UTF-16LE encoded
74};
75
76// osfile flag values
77enum : unsigned char
78{
79 FOPEN = 0x01, // file handle open
80 FEOFLAG = 0x02, // end of file has been encountered
81 FCRLF = 0x04, // CR-LF across read buffer (in text mode)
82 FPIPE = 0x08, // file handle refers to a pipe
83 FNOINHERIT = 0x10, // file handle opened _O_NOINHERIT
84 FAPPEND = 0x20, // file handle opened O_APPEND
85 FDEV = 0x40, // file handle refers to device
86 FTEXT = 0x80, // file handle is in text mode
87};
88
90
91/*
92 * Control structure for lowio file handles
93 */
95{
97 intptr_t osfhnd; // underlying OS file HANDLE
98 __int64 startpos; // File position that matches buffer start
99 unsigned char osfile; // Attributes of file (e.g., open in text mode?)
102
103 uint8_t unicode : 1; // Was the file opened as unicode?
104 uint8_t utf8translations : 1; // Buffer contains translations other than CRLF
105 uint8_t dbcsBufferUsed : 1; // Is the dbcsBuffer in use?
106 char mbBuffer[MB_LEN_MAX]; // Buffer for the lead byte of DBCS when converting from DBCS to Unicode
107 // Or for the first up to 3 bytes of a UTF-8 character
108};
109
110// The log-base-2 of the number of elements in each array of lowio file objects
111#define IOINFO_L2E 6
112
113// The number of elements in each array of lowio file objects
114#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
115
116// The hard maximum number of arrays of lowio file objects that may be allocated
117#define IOINFO_ARRAYS 128
118
119// The maximum number of lowio file objects that may be allocated at any one time
120#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
121
122
123
124#define STDIO_HANDLES_COUNT 3
125/*
126 * Access macros for getting at an __crt_lowio_handle_data struct and its fields from a
127 * file handle
128 */
129#define _pioinfo(i) (__pioinfo[(i) >> IOINFO_L2E] + ((i) & (IOINFO_ARRAY_ELTS - 1)))
130#define _osfhnd(i) (_pioinfo(i)->osfhnd)
131#define _osfile(i) (_pioinfo(i)->osfile)
132#define _pipe_lookahead(i) (_pioinfo(i)->_pipe_lookahead)
133#define _textmode(i) (_pioinfo(i)->textmode)
134#define _tm_unicode(i) (_pioinfo(i)->unicode)
135#define _startpos(i) (_pioinfo(i)->startpos)
136#define _utf8translations(i) (_pioinfo(i)->utf8translations)
137#define _mbBuffer(i) (_pioinfo(i)->mbBuffer)
138#define _dbcsBuffer(i) (_pioinfo(i)->mbBuffer[0])
139#define _dbcsBufferUsed(i) (_pioinfo(i)->dbcsBufferUsed)
140
141/*
142 * Safer versions of the above macros. Currently, only _osfile_safe is
143 * used.
144 */
145#define _pioinfo_safe(i) ((((i) != -1) && ((i) != -2)) ? _pioinfo(i) : &__badioinfo)
146#define _osfile_safe(i) (_pioinfo_safe(i)->osfile)
147#define _textmode_safe(i) (_pioinfo_safe(i)->textmode)
148#define _tm_unicode_safe(i) (_pioinfo_safe(i)->unicode)
149
151
152// Special, static lowio file object used only for more graceful handling
153// of a C file handle value of -1 (results from common errors at the stdio
154// level).
156
157// The umask value
158extern int _umaskval;
159
160// Global array of pointers to the arrays of lowio file objects.
162
163// The number of handles for which file objects have been allocated. This
164// number is such that for any fh in [0, _nhandle), _pioinfo(fh) is well-
165// formed.
166extern int _nhandle;
167
168
169
170int __cdecl _alloc_osfhnd(void);
171int __cdecl _free_osfhnd(int);
173
174//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
175//
176// Internal lowio functions
177//
178//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
179
180_Success_(return == 0)
182 _Out_ int* _UnlockFlag,
189 );
190
191_Success_(return == 0)
193 _Out_ int* _UnlockFlag,
194 _Out_ int* _FileHandle,
195 _In_z_ wchar_t const* _FileName,
196 _In_ int _OpenFlag,
197 _In_ int _ShareFlag,
199 _In_ int _SecureFlag
200 );
201
202
205
208 );
209
212 _In_ int _FileHandle
213 );
214
217
218extern "C++"
219{
220 template <typename Action>
221 auto __acrt_lowio_lock_fh_and_call(int const fh, Action&& action) throw()
222 -> decltype(action())
223 {
224 return __crt_seh_guarded_call<decltype(action())>()(
225 [fh]() { __acrt_lowio_lock_fh(fh); },
226 action,
227 [fh]() { __acrt_lowio_unlock_fh(fh); });
228 }
229}
230
231// console_invalid_handle indicates that CONOUT$ or CONIN$ could not be created
232// console_uninitialized_handle indicates that the handle has not yet been initialized
233const HANDLE _console_invalid_handle = reinterpret_cast<HANDLE>(-1);
234const HANDLE _console_uninitialized_handle = reinterpret_cast<HANDLE>(-2);
235
237
242 );
243
247 _Out_ LPDWORD lpNumberOfCharsRead
248 );
249
251 _Out_ LPDWORD lpcNumberOfEvents
252 );
253
258 );
259
261 _Out_ LPDWORD lpMode
262 );
263
265 _In_ DWORD dwMode
266 );
267
269
271 _In_ void const * lpBuffer,
272 _In_ DWORD nNumberOfCharsToWrite,
273 _Out_ LPDWORD lpNumberOfCharsWritten
274 );
275
281_Check_return_ int __cdecl _write_nolock(_In_ int _FileHandle, _In_reads_bytes_(_MaxCharCount) const void * _Buf, _In_ unsigned int _MaxCharCount, __crt_cached_ptd_host& _Ptd);
283
284// Temporary until non-PTD propagating versions can be replaced:
288
289//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
290//
291// Internal stdio functions with PTD propagation
292//
293//-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
294
297 _In_ int _FileHandle,
298 _Inout_ __crt_cached_ptd_host& _Ptd
299 );
300
303 _In_ int _FileHandle,
304 _In_ long _Offset,
305 _In_ int _Origin,
306 _Inout_ __crt_cached_ptd_host& _Ptd
307 );
308
311 _In_ int _FileHandle,
313 _In_ int _Origin,
314 _Inout_ __crt_cached_ptd_host& _Ptd
315 );
316
318 _In_ int _FileHandle,
319 _In_reads_bytes_(_MaxCharCount) void const* _Buf,
320 _In_ unsigned int _MaxCharCount,
321 _Inout_ __crt_cached_ptd_host& _Ptd
322 );
323
324// fileno for stdout, stdin & stderr when there is no console
325#define _NO_CONSOLE_FILENO ((intptr_t)-2)
326
327
#define __inline
Definition: _wctype.cpp:15
#define __cdecl
Definition: accygwin.h:79
#define __int64
Definition: basetyps.h:16
return
Definition: dirsup.c:529
Definition: terminate.cpp:24
_Out_ int _In_z_ char const _In_ int _In_ int _In_ int _PermissionFlag
_Check_return_ int __cdecl _chsize_nolock_internal(_In_ int _FileHandle, _In_ __int64 _Size, _Inout_ __crt_cached_ptd_host &_Ptd)
_Check_return_opt_ __int64 __cdecl _lseeki64_nolock_internal(_In_ int _FileHandle, _In_ __int64 _Offset, _In_ int _Origin, _Inout_ __crt_cached_ptd_host &_Ptd)
_Out_ int _In_z_ char const _In_ int _In_ int _In_ int _In_ int _SecureFlag
const HANDLE _console_invalid_handle
int _nhandle
Definition: ioinit.cpp:34
void __cdecl __acrt_lowio_destroy_handle_array(_Pre_maybenull_ _Post_invalid_ _In_reads_opt_(IOINFO_ARRAY_ELTS) __crt_lowio_handle_data *_Array)
_Check_return_opt_ long __cdecl _lseek_nolock(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin)
_Out_ int * _FileHandle
_Check_return_opt_ int __cdecl _close_nolock_internal(_In_ int _FileHandle, _Inout_ __crt_cached_ptd_host &_Ptd)
_Check_return_ int __cdecl _write_nolock(_In_ int _FileHandle, _In_reads_bytes_(_MaxCharCount) const void *_Buf, _In_ unsigned int _MaxCharCount, __crt_cached_ptd_host &_Ptd)
BOOL __cdecl __dcrt_read_console(_Out_ LPVOID lpBuffer, _In_ DWORD nNumberOfCharsToRead, _Out_ LPDWORD lpNumberOfCharsRead)
Definition: initconin.cpp:85
BOOL __cdecl __dcrt_read_console_input(_Out_ PINPUT_RECORD lpBuffer, _In_ DWORD nLength, _Out_ LPDWORD lpNumberOfEventsRead)
Definition: initconin.cpp:67
int __cdecl _alloc_osfhnd(void)
Definition: osfinfo.cpp:116
BOOL __cdecl __dcrt_peek_console_input_a(_Out_ PINPUT_RECORD lpBuffer, _In_ DWORD nLength, _Out_ LPDWORD lpNumberOfEventsRead)
Definition: initconin.cpp:118
BOOL __cdecl __dcrt_get_input_console_mode(_Out_ LPDWORD lpMode)
Definition: initconin.cpp:136
_Check_return_ int __cdecl _chsize_nolock(_In_ int _FileHandle, _In_ __int64 _Size)
__crt_lowio_text_mode
#define IOINFO_ARRAYS
_Check_return_opt_ int __cdecl _close_internal(_In_ int _FileHandle, _Inout_ __crt_cached_ptd_host &_Ptd)
_Check_return_opt_ __int64 __cdecl _lseeki64_internal(_In_ int _FileHandle, _In_ __int64 _Offset, _In_ int _Origin, _Inout_ __crt_cached_ptd_host &_Ptd)
int __cdecl __acrt_lowio_set_os_handle(int, intptr_t)
Definition: osfinfo.cpp:195
BOOL __cdecl __dcrt_get_number_of_console_input_events(_Out_ LPDWORD lpcNumberOfEvents)
Definition: initconin.cpp:104
_Check_return_opt_ errno_t __cdecl __acrt_lowio_ensure_fh_exists(_In_ int _FileHandle)
char __crt_lowio_pipe_lookahead[3]
_Check_return_ int __cdecl _setmode_nolock(_In_ int _FileHandle, _In_ int _Mode)
__crt_lowio_handle_data __badioinfo
Definition: file.c:126
int _umaskval
Definition: umask.cpp:15
__crt_lowio_handle_data * __crt_lowio_handle_data_array[IOINFO_ARRAYS]
_Check_return_opt_ int __cdecl _close_nolock(_In_ int _FileHandle)
BOOL __cdecl __dcrt_lowio_ensure_console_output_initialized(void)
Definition: initcon.cpp:31
BOOL __cdecl __dcrt_lowio_ensure_console_input_initialized(void)
Definition: initconin.cpp:31
#define IOINFO_ARRAY_ELTS
char _lookuptrailbytes[256]
Definition: read.cpp:20
__inline char _utf8_no_of_trailbytes(const unsigned char c)
BOOL __cdecl __dcrt_write_console(_In_ void const *lpBuffer, _In_ DWORD nNumberOfCharsToWrite, _Out_ LPDWORD lpNumberOfCharsWritten)
Definition: initcon.cpp:67
BOOL __cdecl __dcrt_set_input_console_mode(_In_ DWORD dwMode)
Definition: initconin.cpp:150
auto __acrt_lowio_lock_fh_and_call(int const fh, Action &&action) -> decltype(action())
_Check_return_ __crt_lowio_handle_data *__cdecl __acrt_lowio_create_handle_array()
Definition: osfinfo.cpp:13
_Check_return_opt_ long __cdecl _lseek_internal(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin, _Inout_ __crt_cached_ptd_host &_Ptd)
int __cdecl _write_internal(_In_ int _FileHandle, _In_reads_bytes_(_MaxCharCount) void const *_Buf, _In_ unsigned int _MaxCharCount, _Inout_ __crt_cached_ptd_host &_Ptd)
_Check_return_ _In_ unsigned int _MaxCharCount
__crt_lowio_handle_data_array __pioinfo
Definition: file.c:121
_Check_return_opt_ __int64 __cdecl _lseeki64_nolock(_In_ int _FileHandle, _In_ __int64 _Offset, _In_ int _Origin)
_Out_ int _In_z_ char const _In_ int _OpenFlag
const HANDLE _console_uninitialized_handle
int __cdecl _free_osfhnd(int)
Definition: osfinfo.cpp:227
void __cdecl __acrt_lowio_lock_fh(_In_ int _FileHandle)
_Out_ int _In_z_ char const _In_ int _In_ int _ShareFlag
void __cdecl __acrt_lowio_unlock_fh(_In_ int _FileHandle)
_In_ size_t _Inout_ __crt_cached_ptd_host & _Ptd
_Check_return_ _Ret_maybenull_ _In_ size_t _In_ size_t _Size
Definition: malloc.h:109
_Check_return_ _Ret_maybenull_ _In_ size_t _In_ size_t _Offset
Definition: malloc.h:140
static TAGREF LPCWSTR LPDWORD LPVOID lpBuffer
Definition: db.cpp:175
const WCHAR * action
Definition: action.c:7509
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
const GLubyte * c
Definition: glext.h:8905
#define MB_LEN_MAX
Definition: stdlib.h:19
#define c
Definition: ke_i.h:80
BYTE uint8_t
Definition: msvideo1.c:66
#define _In_reads_bytes_(s)
Definition: no_sal2.h:170
#define _Inout_
Definition: no_sal2.h:162
#define _Success_(c)
Definition: no_sal2.h:84
#define _In_z_
Definition: no_sal2.h:164
#define _Pre_maybenull_
Definition: no_sal2.h:514
#define _Check_return_
Definition: no_sal2.h:60
#define _Post_invalid_
Definition: no_sal2.h:524
#define _Out_
Definition: no_sal2.h:160
#define _In_reads_opt_(s)
Definition: no_sal2.h:222
#define _In_
Definition: no_sal2.h:158
#define _Out_writes_bytes_(s)
Definition: no_sal2.h:178
errno_t __cdecl _wsopen_nolock(int *const punlock_flag, int *const pfh, wchar_t const *const path, int const oflag, int const shflag, int const pmode, int const secure)
Definition: open.cpp:655
errno_t __cdecl _sopen_nolock(int *const punlock_flag, int *const pfh, char const *const path, int const oflag, int const shflag, int const pmode, int const secure)
Definition: open.cpp:835
int __cdecl _read_nolock(int const fh, void *const result_buffer, unsigned const result_buffer_size)
Definition: read.cpp:408
__crt_lowio_pipe_lookahead _pipe_lookahead
__crt_lowio_text_mode textmode
uint32_t * LPDWORD
Definition: typedefs.h:59
#define _Check_return_opt_
Definition: corecrt.h:224
int errno_t
Definition: corecrt.h:615
int intptr_t
Definition: vcruntime.h:134
#define _CRT_END_C_HEADER
Definition: vcruntime.h:42
#define _CRT_BEGIN_C_HEADER
Definition: vcruntime.h:40
_In_ WDFIOTARGET _In_ _Strict_type_match_ WDF_IO_TARGET_SENT_IO_ACTION Action
Definition: wdfiotarget.h:510
_In_ DWORD _Out_ LPDWORD lpNumberOfEventsRead
Definition: wincon.h:474
_In_ DWORD nNumberOfCharsToRead
Definition: wincon.h:491
_In_ DWORD nLength
Definition: wincon.h:473
#define const
Definition: zconf.h:233