]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/c.rs
std: Delete the `alloc_system` crate
[rust.git] / src / libstd / sys / windows / c.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! C definitions used by libnative that don't belong in liblibc
12
13 #![allow(nonstandard_style)]
14 #![cfg_attr(test, allow(dead_code))]
15 #![unstable(issue = "0", feature = "windows_c")]
16
17 use os::raw::{c_int, c_uint, c_ulong, c_long, c_longlong, c_ushort, c_char};
18 #[cfg(target_arch = "x86_64")]
19 use os::raw::c_ulonglong;
20 use libc::{wchar_t, size_t, c_void};
21 use ptr;
22
23 pub use self::FILE_INFO_BY_HANDLE_CLASS::*;
24 pub use self::EXCEPTION_DISPOSITION::*;
25
26 pub type DWORD = c_ulong;
27 pub type HANDLE = LPVOID;
28 pub type HINSTANCE = HANDLE;
29 pub type HMODULE = HINSTANCE;
30 pub type HRESULT = LONG;
31 pub type BOOL = c_int;
32 pub type BYTE = u8;
33 pub type BOOLEAN = BYTE;
34 pub type GROUP = c_uint;
35 pub type LARGE_INTEGER = c_longlong;
36 pub type LONG = c_long;
37 pub type UINT = c_uint;
38 pub type WCHAR = u16;
39 pub type USHORT = c_ushort;
40 pub type SIZE_T = usize;
41 pub type WORD = u16;
42 pub type CHAR = c_char;
43 pub type ULONG_PTR = usize;
44 pub type ULONG = c_ulong;
45 #[cfg(target_arch = "x86_64")]
46 pub type ULONGLONG = u64;
47 #[cfg(target_arch = "x86_64")]
48 pub type DWORDLONG = ULONGLONG;
49
50 pub type LPBOOL = *mut BOOL;
51 pub type LPBYTE = *mut BYTE;
52 pub type LPBY_HANDLE_FILE_INFORMATION = *mut BY_HANDLE_FILE_INFORMATION;
53 pub type LPCSTR = *const CHAR;
54 pub type LPCVOID = *const c_void;
55 pub type LPCWSTR = *const WCHAR;
56 pub type LPDWORD = *mut DWORD;
57 pub type LPHANDLE = *mut HANDLE;
58 pub type LPOVERLAPPED = *mut OVERLAPPED;
59 pub type LPPROCESS_INFORMATION = *mut PROCESS_INFORMATION;
60 pub type LPSECURITY_ATTRIBUTES = *mut SECURITY_ATTRIBUTES;
61 pub type LPSTARTUPINFO = *mut STARTUPINFO;
62 pub type LPVOID = *mut c_void;
63 pub type LPWCH = *mut WCHAR;
64 pub type LPWIN32_FIND_DATAW = *mut WIN32_FIND_DATAW;
65 pub type LPWSADATA = *mut WSADATA;
66 pub type LPWSAPROTOCOL_INFO = *mut WSAPROTOCOL_INFO;
67 pub type LPSTR = *mut CHAR;
68 pub type LPWSTR = *mut WCHAR;
69 pub type LPFILETIME = *mut FILETIME;
70
71 pub type PCONDITION_VARIABLE = *mut CONDITION_VARIABLE;
72 pub type PLARGE_INTEGER = *mut c_longlong;
73 pub type PSRWLOCK = *mut SRWLOCK;
74
75 pub type SOCKET = ::os::windows::raw::SOCKET;
76 pub type socklen_t = c_int;
77 pub type ADDRESS_FAMILY = USHORT;
78
79 pub const TRUE: BOOL = 1;
80 pub const FALSE: BOOL = 0;
81
82 pub const FILE_ATTRIBUTE_READONLY: DWORD = 0x1;
83 pub const FILE_ATTRIBUTE_DIRECTORY: DWORD = 0x10;
84 pub const FILE_ATTRIBUTE_REPARSE_POINT: DWORD = 0x400;
85
86 pub const FILE_SHARE_DELETE: DWORD = 0x4;
87 pub const FILE_SHARE_READ: DWORD = 0x1;
88 pub const FILE_SHARE_WRITE: DWORD = 0x2;
89
90 pub const CREATE_ALWAYS: DWORD = 2;
91 pub const CREATE_NEW: DWORD = 1;
92 pub const OPEN_ALWAYS: DWORD = 4;
93 pub const OPEN_EXISTING: DWORD = 3;
94 pub const TRUNCATE_EXISTING: DWORD = 5;
95
96 pub const FILE_WRITE_DATA: DWORD = 0x00000002;
97 pub const FILE_APPEND_DATA: DWORD = 0x00000004;
98 pub const FILE_WRITE_EA: DWORD = 0x00000010;
99 pub const FILE_WRITE_ATTRIBUTES: DWORD = 0x00000100;
100 pub const READ_CONTROL: DWORD = 0x00020000;
101 pub const SYNCHRONIZE: DWORD = 0x00100000;
102 pub const GENERIC_READ: DWORD = 0x80000000;
103 pub const GENERIC_WRITE: DWORD = 0x40000000;
104 pub const STANDARD_RIGHTS_WRITE: DWORD = READ_CONTROL;
105 pub const FILE_GENERIC_WRITE: DWORD = STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA |
106                                       FILE_WRITE_ATTRIBUTES |
107                                       FILE_WRITE_EA |
108                                       FILE_APPEND_DATA |
109                                       SYNCHRONIZE;
110
111 pub const FILE_FLAG_OPEN_REPARSE_POINT: DWORD = 0x00200000;
112 pub const FILE_FLAG_BACKUP_SEMANTICS: DWORD = 0x02000000;
113 pub const SECURITY_SQOS_PRESENT: DWORD = 0x00100000;
114
115 pub const FIONBIO: c_ulong = 0x8004667e;
116
117 #[cfg(target_arch = "arm")]
118 const ARM_MAX_BREAKPOINTS: usize = 8;
119 #[cfg(target_arch = "arm")]
120 const ARM_MAX_WATCHPOINTS: usize = 1;
121
122 #[repr(C)]
123 #[derive(Copy)]
124 pub struct WIN32_FIND_DATAW {
125     pub dwFileAttributes: DWORD,
126     pub ftCreationTime: FILETIME,
127     pub ftLastAccessTime: FILETIME,
128     pub ftLastWriteTime: FILETIME,
129     pub nFileSizeHigh: DWORD,
130     pub nFileSizeLow: DWORD,
131     pub dwReserved0: DWORD,
132     pub dwReserved1: DWORD,
133     pub cFileName: [wchar_t; 260], // #define MAX_PATH 260
134     pub cAlternateFileName: [wchar_t; 14],
135 }
136 impl Clone for WIN32_FIND_DATAW {
137     fn clone(&self) -> Self { *self }
138 }
139
140 pub const WSA_FLAG_OVERLAPPED: DWORD = 0x01;
141
142 pub const WSADESCRIPTION_LEN: usize = 256;
143 pub const WSASYS_STATUS_LEN: usize = 128;
144 pub const WSAPROTOCOL_LEN: DWORD = 255;
145 pub const INVALID_SOCKET: SOCKET = !0;
146
147 pub const WSAEACCES: c_int = 10013;
148 pub const WSAEINVAL: c_int = 10022;
149 pub const WSAEWOULDBLOCK: c_int = 10035;
150 pub const WSAEADDRINUSE: c_int = 10048;
151 pub const WSAEADDRNOTAVAIL: c_int = 10049;
152 pub const WSAECONNABORTED: c_int = 10053;
153 pub const WSAECONNRESET: c_int = 10054;
154 pub const WSAENOTCONN: c_int = 10057;
155 pub const WSAESHUTDOWN: c_int = 10058;
156 pub const WSAETIMEDOUT: c_int = 10060;
157 pub const WSAECONNREFUSED: c_int = 10061;
158
159 pub const MAX_PROTOCOL_CHAIN: DWORD = 7;
160
161 pub const TOKEN_READ: DWORD = 0x20008;
162 pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;
163 pub const FSCTL_GET_REPARSE_POINT: DWORD = 0x900a8;
164 pub const IO_REPARSE_TAG_SYMLINK: DWORD = 0xa000000c;
165 pub const IO_REPARSE_TAG_MOUNT_POINT: DWORD = 0xa0000003;
166 pub const SYMLINK_FLAG_RELATIVE: DWORD = 0x00000001;
167 pub const FSCTL_SET_REPARSE_POINT: DWORD = 0x900a4;
168
169 pub const SYMBOLIC_LINK_FLAG_DIRECTORY: DWORD = 0x1;
170 pub const SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE: DWORD = 0x2;
171
172 // Note that these are not actually HANDLEs, just values to pass to GetStdHandle
173 pub const STD_INPUT_HANDLE: DWORD = -10i32 as DWORD;
174 pub const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
175 pub const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
176
177 pub const HANDLE_FLAG_INHERIT: DWORD = 0x00000001;
178
179 pub const PROGRESS_CONTINUE: DWORD = 0;
180
181 pub const ERROR_FILE_NOT_FOUND: DWORD = 2;
182 pub const ERROR_PATH_NOT_FOUND: DWORD = 3;
183 pub const ERROR_ACCESS_DENIED: DWORD = 5;
184 pub const ERROR_INVALID_HANDLE: DWORD = 6;
185 pub const ERROR_NO_MORE_FILES: DWORD = 18;
186 pub const ERROR_HANDLE_EOF: DWORD = 38;
187 pub const ERROR_FILE_EXISTS: DWORD = 80;
188 pub const ERROR_INVALID_PARAMETER: DWORD = 87;
189 pub const ERROR_BROKEN_PIPE: DWORD = 109;
190 pub const ERROR_CALL_NOT_IMPLEMENTED: DWORD = 120;
191 pub const ERROR_INSUFFICIENT_BUFFER: DWORD = 122;
192 pub const ERROR_ALREADY_EXISTS: DWORD = 183;
193 pub const ERROR_NO_DATA: DWORD = 232;
194 pub const ERROR_ENVVAR_NOT_FOUND: DWORD = 203;
195 pub const ERROR_OPERATION_ABORTED: DWORD = 995;
196 pub const ERROR_IO_PENDING: DWORD = 997;
197 pub const ERROR_TIMEOUT: DWORD = 0x5B4;
198
199 pub const E_NOTIMPL: HRESULT = 0x80004001u32 as HRESULT;
200
201 pub const INVALID_HANDLE_VALUE: HANDLE = !0 as HANDLE;
202
203 pub const FACILITY_NT_BIT: DWORD = 0x1000_0000;
204
205 pub const FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
206 pub const FORMAT_MESSAGE_FROM_HMODULE: DWORD = 0x00000800;
207 pub const FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
208
209 pub const TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
210
211 pub const DLL_THREAD_DETACH: DWORD = 3;
212 pub const DLL_PROCESS_DETACH: DWORD = 0;
213
214 pub const INFINITE: DWORD = !0;
215
216 pub const DUPLICATE_SAME_ACCESS: DWORD = 0x00000002;
217
218 pub const CONDITION_VARIABLE_INIT: CONDITION_VARIABLE = CONDITION_VARIABLE {
219     ptr: ptr::null_mut(),
220 };
221 pub const SRWLOCK_INIT: SRWLOCK = SRWLOCK { ptr: ptr::null_mut() };
222
223 pub const DETACHED_PROCESS: DWORD = 0x00000008;
224 pub const CREATE_NEW_PROCESS_GROUP: DWORD = 0x00000200;
225 pub const CREATE_UNICODE_ENVIRONMENT: DWORD = 0x00000400;
226 pub const STARTF_USESTDHANDLES: DWORD = 0x00000100;
227
228 pub const AF_INET: c_int = 2;
229 pub const AF_INET6: c_int = 23;
230 pub const SD_BOTH: c_int = 2;
231 pub const SD_RECEIVE: c_int = 0;
232 pub const SD_SEND: c_int = 1;
233 pub const SOCK_DGRAM: c_int = 2;
234 pub const SOCK_STREAM: c_int = 1;
235 pub const SOL_SOCKET: c_int = 0xffff;
236 pub const SO_RCVTIMEO: c_int = 0x1006;
237 pub const SO_SNDTIMEO: c_int = 0x1005;
238 pub const SO_REUSEADDR: c_int = 0x0004;
239 pub const IPPROTO_IP: c_int = 0;
240 pub const IPPROTO_TCP: c_int = 6;
241 pub const IPPROTO_IPV6: c_int = 41;
242 pub const TCP_NODELAY: c_int = 0x0001;
243 pub const IP_TTL: c_int = 4;
244 pub const IPV6_V6ONLY: c_int = 27;
245 pub const SO_ERROR: c_int = 0x1007;
246 pub const SO_BROADCAST: c_int = 0x0020;
247 pub const IP_MULTICAST_LOOP: c_int = 11;
248 pub const IPV6_MULTICAST_LOOP: c_int = 11;
249 pub const IP_MULTICAST_TTL: c_int = 10;
250 pub const IP_ADD_MEMBERSHIP: c_int = 12;
251 pub const IP_DROP_MEMBERSHIP: c_int = 13;
252 pub const IPV6_ADD_MEMBERSHIP: c_int = 12;
253 pub const IPV6_DROP_MEMBERSHIP: c_int = 13;
254 pub const MSG_PEEK: c_int = 0x2;
255
256 #[repr(C)]
257 pub struct ip_mreq {
258     pub imr_multiaddr: in_addr,
259     pub imr_interface: in_addr,
260 }
261
262 #[repr(C)]
263 pub struct ipv6_mreq {
264     pub ipv6mr_multiaddr: in6_addr,
265     pub ipv6mr_interface: c_uint,
266 }
267
268 pub const VOLUME_NAME_DOS: DWORD = 0x0;
269 pub const MOVEFILE_REPLACE_EXISTING: DWORD = 1;
270
271 pub const FILE_BEGIN: DWORD = 0;
272 pub const FILE_CURRENT: DWORD = 1;
273 pub const FILE_END: DWORD = 2;
274
275 pub const WAIT_OBJECT_0: DWORD = 0x00000000;
276 pub const WAIT_TIMEOUT: DWORD = 258;
277 pub const WAIT_FAILED: DWORD = 0xFFFFFFFF;
278
279 #[cfg(target_env = "msvc")]
280 #[cfg(feature = "backtrace")]
281 pub const MAX_SYM_NAME: usize = 2000;
282 #[cfg(target_arch = "x86")]
283 #[cfg(feature = "backtrace")]
284 pub const IMAGE_FILE_MACHINE_I386: DWORD = 0x014c;
285 #[cfg(target_arch = "x86_64")]
286 #[cfg(feature = "backtrace")]
287 pub const IMAGE_FILE_MACHINE_AMD64: DWORD = 0x8664;
288 #[cfg(target_arch = "aarch64")]
289 #[cfg(feature = "backtrace")]
290 pub const IMAGE_FILE_MACHINE_ARM64: DWORD = 0xAA64;
291 #[cfg(target_arch = "arm")]
292 #[cfg(feature = "backtrace")]
293 pub const IMAGE_FILE_MACHINE_ARMNT: DWORD = 0x01c4;
294
295 pub const EXCEPTION_CONTINUE_SEARCH: LONG = 0;
296 pub const EXCEPTION_STACK_OVERFLOW: DWORD = 0xc00000fd;
297 pub const EXCEPTION_MAXIMUM_PARAMETERS: usize = 15;
298
299 pub const PIPE_ACCESS_INBOUND: DWORD = 0x00000001;
300 pub const PIPE_ACCESS_OUTBOUND: DWORD = 0x00000002;
301 pub const FILE_FLAG_FIRST_PIPE_INSTANCE: DWORD = 0x00080000;
302 pub const FILE_FLAG_OVERLAPPED: DWORD = 0x40000000;
303 pub const PIPE_WAIT: DWORD = 0x00000000;
304 pub const PIPE_TYPE_BYTE: DWORD = 0x00000000;
305 pub const PIPE_REJECT_REMOTE_CLIENTS: DWORD = 0x00000008;
306 pub const PIPE_READMODE_BYTE: DWORD = 0x00000000;
307
308 pub const FD_SETSIZE: usize = 64;
309
310 pub const STACK_SIZE_PARAM_IS_A_RESERVATION: DWORD = 0x00010000;
311
312 pub const HEAP_ZERO_MEMORY: DWORD = 0x00000008;
313
314 #[repr(C)]
315 #[cfg(not(target_pointer_width = "64"))]
316 pub struct WSADATA {
317     pub wVersion: WORD,
318     pub wHighVersion: WORD,
319     pub szDescription: [u8; WSADESCRIPTION_LEN + 1],
320     pub szSystemStatus: [u8; WSASYS_STATUS_LEN + 1],
321     pub iMaxSockets: u16,
322     pub iMaxUdpDg: u16,
323     pub lpVendorInfo: *mut u8,
324 }
325 #[repr(C)]
326 #[cfg(target_pointer_width = "64")]
327 pub struct WSADATA {
328     pub wVersion: WORD,
329     pub wHighVersion: WORD,
330     pub iMaxSockets: u16,
331     pub iMaxUdpDg: u16,
332     pub lpVendorInfo: *mut u8,
333     pub szDescription: [u8; WSADESCRIPTION_LEN + 1],
334     pub szSystemStatus: [u8; WSASYS_STATUS_LEN + 1],
335 }
336
337 #[repr(C)]
338 pub struct WSAPROTOCOL_INFO {
339     pub dwServiceFlags1: DWORD,
340     pub dwServiceFlags2: DWORD,
341     pub dwServiceFlags3: DWORD,
342     pub dwServiceFlags4: DWORD,
343     pub dwProviderFlags: DWORD,
344     pub ProviderId: GUID,
345     pub dwCatalogEntryId: DWORD,
346     pub ProtocolChain: WSAPROTOCOLCHAIN,
347     pub iVersion: c_int,
348     pub iAddressFamily: c_int,
349     pub iMaxSockAddr: c_int,
350     pub iMinSockAddr: c_int,
351     pub iSocketType: c_int,
352     pub iProtocol: c_int,
353     pub iProtocolMaxOffset: c_int,
354     pub iNetworkByteOrder: c_int,
355     pub iSecurityScheme: c_int,
356     pub dwMessageSize: DWORD,
357     pub dwProviderReserved: DWORD,
358     pub szProtocol: [u16; (WSAPROTOCOL_LEN as usize) + 1],
359 }
360
361 #[repr(C)]
362 #[derive(Copy, Clone)]
363 pub struct WIN32_FILE_ATTRIBUTE_DATA {
364     pub dwFileAttributes: DWORD,
365     pub ftCreationTime: FILETIME,
366     pub ftLastAccessTime: FILETIME,
367     pub ftLastWriteTime: FILETIME,
368     pub nFileSizeHigh: DWORD,
369     pub nFileSizeLow: DWORD,
370 }
371
372 #[repr(C)]
373 pub struct BY_HANDLE_FILE_INFORMATION {
374     pub dwFileAttributes: DWORD,
375     pub ftCreationTime: FILETIME,
376     pub ftLastAccessTime: FILETIME,
377     pub ftLastWriteTime: FILETIME,
378     pub dwVolumeSerialNumber: DWORD,
379     pub nFileSizeHigh: DWORD,
380     pub nFileSizeLow: DWORD,
381     pub nNumberOfLinks: DWORD,
382     pub nFileIndexHigh: DWORD,
383     pub nFileIndexLow: DWORD,
384 }
385
386 #[repr(C)]
387 #[allow(dead_code)] // we only use some variants
388 pub enum FILE_INFO_BY_HANDLE_CLASS {
389     FileBasicInfo                   = 0,
390     FileStandardInfo                = 1,
391     FileNameInfo                    = 2,
392     FileRenameInfo                  = 3,
393     FileDispositionInfo             = 4,
394     FileAllocationInfo              = 5,
395     FileEndOfFileInfo               = 6,
396     FileStreamInfo                  = 7,
397     FileCompressionInfo             = 8,
398     FileAttributeTagInfo            = 9,
399     FileIdBothDirectoryInfo         = 10, // 0xA
400     FileIdBothDirectoryRestartInfo  = 11, // 0xB
401     FileIoPriorityHintInfo          = 12, // 0xC
402     FileRemoteProtocolInfo          = 13, // 0xD
403     FileFullDirectoryInfo           = 14, // 0xE
404     FileFullDirectoryRestartInfo    = 15, // 0xF
405     FileStorageInfo                 = 16, // 0x10
406     FileAlignmentInfo               = 17, // 0x11
407     FileIdInfo                      = 18, // 0x12
408     FileIdExtdDirectoryInfo         = 19, // 0x13
409     FileIdExtdDirectoryRestartInfo  = 20, // 0x14
410     MaximumFileInfoByHandlesClass
411 }
412
413 #[repr(C)]
414 pub struct FILE_BASIC_INFO {
415     pub CreationTime: LARGE_INTEGER,
416     pub LastAccessTime: LARGE_INTEGER,
417     pub LastWriteTime: LARGE_INTEGER,
418     pub ChangeTime: LARGE_INTEGER,
419     pub FileAttributes: DWORD,
420 }
421
422 #[repr(C)]
423 pub struct FILE_END_OF_FILE_INFO {
424     pub EndOfFile: LARGE_INTEGER,
425 }
426
427 #[repr(C)]
428 pub struct REPARSE_DATA_BUFFER {
429     pub ReparseTag: c_uint,
430     pub ReparseDataLength: c_ushort,
431     pub Reserved: c_ushort,
432     pub rest: (),
433 }
434
435 #[repr(C)]
436 pub struct SYMBOLIC_LINK_REPARSE_BUFFER {
437     pub SubstituteNameOffset: c_ushort,
438     pub SubstituteNameLength: c_ushort,
439     pub PrintNameOffset: c_ushort,
440     pub PrintNameLength: c_ushort,
441     pub Flags: c_ulong,
442     pub PathBuffer: WCHAR,
443 }
444
445 #[repr(C)]
446 pub struct MOUNT_POINT_REPARSE_BUFFER {
447     pub SubstituteNameOffset: c_ushort,
448     pub SubstituteNameLength: c_ushort,
449     pub PrintNameOffset: c_ushort,
450     pub PrintNameLength: c_ushort,
451     pub PathBuffer: WCHAR,
452 }
453
454 pub type LPPROGRESS_ROUTINE = ::option::Option<unsafe extern "system" fn(
455     TotalFileSize: LARGE_INTEGER,
456     TotalBytesTransferred: LARGE_INTEGER,
457     StreamSize: LARGE_INTEGER,
458     StreamBytesTransferred: LARGE_INTEGER,
459     dwStreamNumber: DWORD,
460     dwCallbackReason: DWORD,
461     hSourceFile: HANDLE,
462     hDestinationFile: HANDLE,
463     lpData: LPVOID,
464 ) -> DWORD>;
465
466 #[repr(C)]
467 pub struct CONDITION_VARIABLE { pub ptr: LPVOID }
468 #[repr(C)]
469 pub struct SRWLOCK { pub ptr: LPVOID }
470 #[repr(C)]
471 pub struct CRITICAL_SECTION {
472     CriticalSectionDebug: LPVOID,
473     LockCount: LONG,
474     RecursionCount: LONG,
475     OwningThread: HANDLE,
476     LockSemaphore: HANDLE,
477     SpinCount: ULONG_PTR
478 }
479
480 #[repr(C)]
481 pub struct REPARSE_MOUNTPOINT_DATA_BUFFER {
482     pub ReparseTag: DWORD,
483     pub ReparseDataLength: DWORD,
484     pub Reserved: WORD,
485     pub ReparseTargetLength: WORD,
486     pub ReparseTargetMaximumLength: WORD,
487     pub Reserved1: WORD,
488     pub ReparseTarget: WCHAR,
489 }
490
491 #[repr(C)]
492 pub struct EXCEPTION_RECORD {
493     pub ExceptionCode: DWORD,
494     pub ExceptionFlags: DWORD,
495     pub ExceptionRecord: *mut EXCEPTION_RECORD,
496     pub ExceptionAddress: LPVOID,
497     pub NumberParameters: DWORD,
498     pub ExceptionInformation: [LPVOID; EXCEPTION_MAXIMUM_PARAMETERS]
499 }
500
501 #[repr(C)]
502 pub struct EXCEPTION_POINTERS {
503     pub ExceptionRecord: *mut EXCEPTION_RECORD,
504     pub ContextRecord: *mut CONTEXT,
505 }
506
507 pub type PVECTORED_EXCEPTION_HANDLER = extern "system"
508         fn(ExceptionInfo: *mut EXCEPTION_POINTERS) -> LONG;
509
510 #[repr(C)]
511 pub struct GUID {
512     pub Data1: DWORD,
513     pub Data2: WORD,
514     pub Data3: WORD,
515     pub Data4: [BYTE; 8],
516 }
517
518 #[repr(C)]
519 pub struct WSAPROTOCOLCHAIN {
520     pub ChainLen: c_int,
521     pub ChainEntries: [DWORD; MAX_PROTOCOL_CHAIN as usize],
522 }
523
524 #[repr(C)]
525 pub struct SECURITY_ATTRIBUTES {
526     pub nLength: DWORD,
527     pub lpSecurityDescriptor: LPVOID,
528     pub bInheritHandle: BOOL,
529 }
530
531 #[repr(C)]
532 pub struct PROCESS_INFORMATION {
533     pub hProcess: HANDLE,
534     pub hThread: HANDLE,
535     pub dwProcessId: DWORD,
536     pub dwThreadId: DWORD,
537 }
538
539 #[repr(C)]
540 pub struct STARTUPINFO {
541     pub cb: DWORD,
542     pub lpReserved: LPWSTR,
543     pub lpDesktop: LPWSTR,
544     pub lpTitle: LPWSTR,
545     pub dwX: DWORD,
546     pub dwY: DWORD,
547     pub dwXSize: DWORD,
548     pub dwYSize: DWORD,
549     pub dwXCountChars: DWORD,
550     pub dwYCountCharts: DWORD,
551     pub dwFillAttribute: DWORD,
552     pub dwFlags: DWORD,
553     pub wShowWindow: WORD,
554     pub cbReserved2: WORD,
555     pub lpReserved2: LPBYTE,
556     pub hStdInput: HANDLE,
557     pub hStdOutput: HANDLE,
558     pub hStdError: HANDLE,
559 }
560
561 #[repr(C)]
562 pub struct SOCKADDR {
563     pub sa_family: ADDRESS_FAMILY,
564     pub sa_data: [CHAR; 14],
565 }
566
567 #[repr(C)]
568 #[derive(Copy, Clone)]
569 pub struct FILETIME {
570     pub dwLowDateTime: DWORD,
571     pub dwHighDateTime: DWORD,
572 }
573
574 #[repr(C)]
575 pub struct OVERLAPPED {
576     pub Internal: *mut c_ulong,
577     pub InternalHigh: *mut c_ulong,
578     pub Offset: DWORD,
579     pub OffsetHigh: DWORD,
580     pub hEvent: HANDLE,
581 }
582
583 #[repr(C)]
584 #[cfg(target_env = "msvc")]
585 #[cfg(feature = "backtrace")]
586 pub struct SYMBOL_INFO {
587     pub SizeOfStruct: c_ulong,
588     pub TypeIndex: c_ulong,
589     pub Reserved: [u64; 2],
590     pub Index: c_ulong,
591     pub Size: c_ulong,
592     pub ModBase: u64,
593     pub Flags: c_ulong,
594     pub Value: u64,
595     pub Address: u64,
596     pub Register: c_ulong,
597     pub Scope: c_ulong,
598     pub Tag: c_ulong,
599     pub NameLen: c_ulong,
600     pub MaxNameLen: c_ulong,
601     // note that windows has this as 1, but it basically just means that
602     // the name is inline at the end of the struct. For us, we just bump
603     // the struct size up to MAX_SYM_NAME.
604     pub Name: [c_char; MAX_SYM_NAME],
605 }
606
607 #[repr(C)]
608 #[cfg(target_env = "msvc")]
609 #[cfg(feature = "backtrace")]
610 pub struct IMAGEHLP_LINE64 {
611     pub SizeOfStruct: u32,
612     pub Key: *const c_void,
613     pub LineNumber: u32,
614     pub Filename: *const c_char,
615     pub Address: u64,
616 }
617
618 #[repr(C)]
619 #[allow(dead_code)] // we only use some variants
620 pub enum ADDRESS_MODE {
621     AddrMode1616,
622     AddrMode1632,
623     AddrModeReal,
624     AddrModeFlat,
625 }
626
627 #[repr(C)]
628 #[cfg(feature = "backtrace")]
629 pub struct ADDRESS64 {
630     pub Offset: u64,
631     pub Segment: u16,
632     pub Mode: ADDRESS_MODE,
633 }
634
635 #[repr(C)]
636 #[cfg(feature = "backtrace")]
637 pub struct STACKFRAME_EX {
638     pub AddrPC: ADDRESS64,
639     pub AddrReturn: ADDRESS64,
640     pub AddrFrame: ADDRESS64,
641     pub AddrStack: ADDRESS64,
642     pub AddrBStore: ADDRESS64,
643     pub FuncTableEntry: *mut c_void,
644     pub Params: [u64; 4],
645     pub Far: BOOL,
646     pub Virtual: BOOL,
647     pub Reserved: [u64; 3],
648     pub KdHelp: KDHELP64,
649     pub StackFrameSize: DWORD,
650     pub InlineFrameContext: DWORD,
651 }
652
653 #[repr(C)]
654 #[cfg(feature = "backtrace")]
655 pub struct STACKFRAME64 {
656     pub AddrPC: ADDRESS64,
657     pub AddrReturn: ADDRESS64,
658     pub AddrFrame: ADDRESS64,
659     pub AddrStack: ADDRESS64,
660     pub AddrBStore: ADDRESS64,
661     pub FuncTableEntry: *mut c_void,
662     pub Params: [u64; 4],
663     pub Far: BOOL,
664     pub Virtual: BOOL,
665     pub Reserved: [u64; 3],
666     pub KdHelp: KDHELP64,
667 }
668
669 #[repr(C)]
670 #[cfg(feature = "backtrace")]
671 pub struct KDHELP64 {
672     pub Thread: u64,
673     pub ThCallbackStack: DWORD,
674     pub ThCallbackBStore: DWORD,
675     pub NextCallback: DWORD,
676     pub FramePointer: DWORD,
677     pub KiCallUserMode: u64,
678     pub KeUserCallbackDispatcher: u64,
679     pub SystemRangeStart: u64,
680     pub KiUserExceptionDispatcher: u64,
681     pub StackBase: u64,
682     pub StackLimit: u64,
683     pub Reserved: [u64; 5],
684 }
685
686 #[cfg(target_arch = "x86")]
687 #[repr(C)]
688 pub struct CONTEXT {
689     pub ContextFlags: DWORD,
690     pub Dr0: DWORD,
691     pub Dr1: DWORD,
692     pub Dr2: DWORD,
693     pub Dr3: DWORD,
694     pub Dr6: DWORD,
695     pub Dr7: DWORD,
696     pub FloatSave: FLOATING_SAVE_AREA,
697     pub SegGs: DWORD,
698     pub SegFs: DWORD,
699     pub SegEs: DWORD,
700     pub SegDs: DWORD,
701     pub Edi: DWORD,
702     pub Esi: DWORD,
703     pub Ebx: DWORD,
704     pub Edx: DWORD,
705     pub Ecx: DWORD,
706     pub Eax: DWORD,
707     pub Ebp: DWORD,
708     pub Eip: DWORD,
709     pub SegCs: DWORD,
710     pub EFlags: DWORD,
711     pub Esp: DWORD,
712     pub SegSs: DWORD,
713     pub ExtendedRegisters: [u8; 512],
714 }
715
716 #[cfg(target_arch = "x86")]
717 #[repr(C)]
718 pub struct FLOATING_SAVE_AREA {
719     pub ControlWord: DWORD,
720     pub StatusWord: DWORD,
721     pub TagWord: DWORD,
722     pub ErrorOffset: DWORD,
723     pub ErrorSelector: DWORD,
724     pub DataOffset: DWORD,
725     pub DataSelector: DWORD,
726     pub RegisterArea: [u8; 80],
727     pub Cr0NpxState: DWORD,
728 }
729
730 #[cfg(target_arch = "x86_64")]
731 #[repr(C, align(16))]
732 pub struct CONTEXT {
733     pub P1Home: DWORDLONG,
734     pub P2Home: DWORDLONG,
735     pub P3Home: DWORDLONG,
736     pub P4Home: DWORDLONG,
737     pub P5Home: DWORDLONG,
738     pub P6Home: DWORDLONG,
739
740     pub ContextFlags: DWORD,
741     pub MxCsr: DWORD,
742
743     pub SegCs: WORD,
744     pub SegDs: WORD,
745     pub SegEs: WORD,
746     pub SegFs: WORD,
747     pub SegGs: WORD,
748     pub SegSs: WORD,
749     pub EFlags: DWORD,
750
751     pub Dr0: DWORDLONG,
752     pub Dr1: DWORDLONG,
753     pub Dr2: DWORDLONG,
754     pub Dr3: DWORDLONG,
755     pub Dr6: DWORDLONG,
756     pub Dr7: DWORDLONG,
757
758     pub Rax: DWORDLONG,
759     pub Rcx: DWORDLONG,
760     pub Rdx: DWORDLONG,
761     pub Rbx: DWORDLONG,
762     pub Rsp: DWORDLONG,
763     pub Rbp: DWORDLONG,
764     pub Rsi: DWORDLONG,
765     pub Rdi: DWORDLONG,
766     pub R8:  DWORDLONG,
767     pub R9:  DWORDLONG,
768     pub R10: DWORDLONG,
769     pub R11: DWORDLONG,
770     pub R12: DWORDLONG,
771     pub R13: DWORDLONG,
772     pub R14: DWORDLONG,
773     pub R15: DWORDLONG,
774
775     pub Rip: DWORDLONG,
776
777     pub FltSave: FLOATING_SAVE_AREA,
778
779     pub VectorRegister: [M128A; 26],
780     pub VectorControl: DWORDLONG,
781
782     pub DebugControl: DWORDLONG,
783     pub LastBranchToRip: DWORDLONG,
784     pub LastBranchFromRip: DWORDLONG,
785     pub LastExceptionToRip: DWORDLONG,
786     pub LastExceptionFromRip: DWORDLONG,
787 }
788
789 #[cfg(target_arch = "x86_64")]
790 #[repr(C, align(16))]
791 pub struct M128A {
792     pub Low:  c_ulonglong,
793     pub High: c_longlong
794 }
795
796 #[cfg(target_arch = "x86_64")]
797 #[repr(C, align(16))]
798 pub struct FLOATING_SAVE_AREA {
799     _Dummy: [u8; 512] // FIXME: Fill this out
800 }
801
802 #[cfg(target_arch = "arm")]
803 #[repr(C)]
804 pub struct CONTEXT {
805     pub ContextFlags: ULONG,
806     pub R0: ULONG,
807     pub R1: ULONG,
808     pub R2: ULONG,
809     pub R3: ULONG,
810     pub R4: ULONG,
811     pub R5: ULONG,
812     pub R6: ULONG,
813     pub R7: ULONG,
814     pub R8: ULONG,
815     pub R9: ULONG,
816     pub R10: ULONG,
817     pub R11: ULONG,
818     pub R12: ULONG,
819     pub Sp: ULONG,
820     pub Lr: ULONG,
821     pub Pc: ULONG,
822     pub Cpsr: ULONG,
823     pub Fpscr: ULONG,
824     pub Padding: ULONG,
825     pub D: [u64; 32],
826     pub Bvr: [ULONG; ARM_MAX_BREAKPOINTS],
827     pub Bcr: [ULONG; ARM_MAX_BREAKPOINTS],
828     pub Wvr: [ULONG; ARM_MAX_WATCHPOINTS],
829     pub Wcr: [ULONG; ARM_MAX_WATCHPOINTS],
830     pub Padding2: [ULONG; 2]
831 }
832
833 // FIXME(#43348): This structure is used for backtrace only, and a fake
834 // definition is provided here only to allow rustdoc to pass type-check. This
835 // will not appear in the final documentation. This should be also defined for
836 // other architectures supported by Windows such as ARM, and for historical
837 // interest, maybe MIPS and PowerPC as well.
838 #[cfg(all(rustdoc, not(any(target_arch = "x86_64", target_arch = "x86",
839       target_arch = "aarch64", target_arch = "arm"))))]
840 pub enum CONTEXT {}
841
842 #[cfg(target_arch = "aarch64")]
843 pub const ARM64_MAX_BREAKPOINTS: usize = 8;
844
845 #[cfg(target_arch = "aarch64")]
846 pub const ARM64_MAX_WATCHPOINTS: usize = 2;
847
848 #[cfg(target_arch = "aarch64")]
849 #[repr(C)]
850 pub struct ARM64_NT_NEON128 {
851     pub D: [f64; 2],
852 }
853
854 #[cfg(target_arch = "aarch64")]
855 #[repr(C, align(16))]
856 pub struct CONTEXT {
857     pub ContextFlags: DWORD,
858     pub Cpsr: DWORD,
859     pub X0: u64,
860     pub X1: u64,
861     pub X2: u64,
862     pub X3: u64,
863     pub X4: u64,
864     pub X5: u64,
865     pub X6: u64,
866     pub X7: u64,
867     pub X8: u64,
868     pub X9: u64,
869     pub X10: u64,
870     pub X11: u64,
871     pub X12: u64,
872     pub X13: u64,
873     pub X14: u64,
874     pub X15: u64,
875     pub X16: u64,
876     pub X17: u64,
877     pub X18: u64,
878     pub X19: u64,
879     pub X20: u64,
880     pub X21: u64,
881     pub X22: u64,
882     pub X23: u64,
883     pub X24: u64,
884     pub X25: u64,
885     pub X26: u64,
886     pub X27: u64,
887     pub X28: u64,
888     pub Fp: u64,
889     pub Lr: u64,
890     pub Sp: u64,
891     pub Pc: u64,
892     pub V: [ARM64_NT_NEON128; 32],
893     pub Fpcr: DWORD,
894     pub Fpsr: DWORD,
895     pub Bcr: [DWORD; ARM64_MAX_BREAKPOINTS],
896     pub Bvr: [DWORD; ARM64_MAX_BREAKPOINTS],
897     pub Wcr: [DWORD; ARM64_MAX_WATCHPOINTS],
898     pub Wvr: [DWORD; ARM64_MAX_WATCHPOINTS],
899 }
900
901 #[repr(C)]
902 pub struct SOCKADDR_STORAGE_LH {
903     pub ss_family: ADDRESS_FAMILY,
904     pub __ss_pad1: [CHAR; 6],
905     pub __ss_align: i64,
906     pub __ss_pad2: [CHAR; 112],
907 }
908
909 #[repr(C)]
910 pub struct ADDRINFOA {
911     pub ai_flags: c_int,
912     pub ai_family: c_int,
913     pub ai_socktype: c_int,
914     pub ai_protocol: c_int,
915     pub ai_addrlen: size_t,
916     pub ai_canonname: *mut c_char,
917     pub ai_addr: *mut SOCKADDR,
918     pub ai_next: *mut ADDRINFOA,
919 }
920
921 #[repr(C)]
922 #[derive(Copy, Clone)]
923 pub struct sockaddr_in {
924     pub sin_family: ADDRESS_FAMILY,
925     pub sin_port: USHORT,
926     pub sin_addr: in_addr,
927     pub sin_zero: [CHAR; 8],
928 }
929
930 #[repr(C)]
931 #[derive(Copy, Clone)]
932 pub struct sockaddr_in6 {
933     pub sin6_family: ADDRESS_FAMILY,
934     pub sin6_port: USHORT,
935     pub sin6_flowinfo: c_ulong,
936     pub sin6_addr: in6_addr,
937     pub sin6_scope_id: c_ulong,
938 }
939
940 #[repr(C)]
941 #[derive(Copy, Clone)]
942 pub struct in_addr {
943     pub s_addr: u32,
944 }
945
946 #[repr(C)]
947 #[derive(Copy, Clone)]
948 pub struct in6_addr {
949     pub s6_addr: [u8; 16],
950 }
951
952 #[repr(C)]
953 #[derive(Copy, Clone)]
954 #[allow(dead_code)] // we only use some variants
955 pub enum EXCEPTION_DISPOSITION {
956     ExceptionContinueExecution,
957     ExceptionContinueSearch,
958     ExceptionNestedException,
959     ExceptionCollidedUnwind
960 }
961
962 #[repr(C)]
963 #[derive(Copy, Clone)]
964 pub struct CONSOLE_READCONSOLE_CONTROL {
965     pub nLength: ULONG,
966     pub nInitialChars: ULONG,
967     pub dwCtrlWakeupMask: ULONG,
968     pub dwControlKeyState: ULONG,
969 }
970 pub type PCONSOLE_READCONSOLE_CONTROL = *mut CONSOLE_READCONSOLE_CONTROL;
971
972 #[repr(C)]
973 #[derive(Copy)]
974 pub struct fd_set {
975     pub fd_count: c_uint,
976     pub fd_array: [SOCKET; FD_SETSIZE],
977 }
978
979 impl Clone for fd_set {
980     fn clone(&self) -> fd_set {
981         *self
982     }
983 }
984
985 #[repr(C)]
986 #[derive(Copy, Clone)]
987 pub struct timeval {
988     pub tv_sec: c_long,
989     pub tv_usec: c_long,
990 }
991
992 extern "system" {
993     pub fn WSAStartup(wVersionRequested: WORD,
994                       lpWSAData: LPWSADATA) -> c_int;
995     pub fn WSACleanup() -> c_int;
996     pub fn WSAGetLastError() -> c_int;
997     pub fn WSADuplicateSocketW(s: SOCKET,
998                                dwProcessId: DWORD,
999                                lpProtocolInfo: LPWSAPROTOCOL_INFO)
1000                                -> c_int;
1001     pub fn GetCurrentProcessId() -> DWORD;
1002     pub fn WSASocketW(af: c_int,
1003                       kind: c_int,
1004                       protocol: c_int,
1005                       lpProtocolInfo: LPWSAPROTOCOL_INFO,
1006                       g: GROUP,
1007                       dwFlags: DWORD) -> SOCKET;
1008     pub fn ioctlsocket(s: SOCKET, cmd: c_long, argp: *mut c_ulong) -> c_int;
1009     pub fn InitializeCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
1010     pub fn EnterCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
1011     pub fn TryEnterCriticalSection(CriticalSection: *mut CRITICAL_SECTION) -> BOOLEAN;
1012     pub fn LeaveCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
1013     pub fn DeleteCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
1014
1015     pub fn ReadConsoleW(hConsoleInput: HANDLE,
1016                         lpBuffer: LPVOID,
1017                         nNumberOfCharsToRead: DWORD,
1018                         lpNumberOfCharsRead: LPDWORD,
1019                         pInputControl: PCONSOLE_READCONSOLE_CONTROL) -> BOOL;
1020
1021     pub fn WriteConsoleW(hConsoleOutput: HANDLE,
1022                          lpBuffer: LPCVOID,
1023                          nNumberOfCharsToWrite: DWORD,
1024                          lpNumberOfCharsWritten: LPDWORD,
1025                          lpReserved: LPVOID) -> BOOL;
1026
1027     pub fn GetConsoleMode(hConsoleHandle: HANDLE,
1028                           lpMode: LPDWORD) -> BOOL;
1029     pub fn RemoveDirectoryW(lpPathName: LPCWSTR) -> BOOL;
1030     pub fn SetFileAttributesW(lpFileName: LPCWSTR,
1031                               dwFileAttributes: DWORD) -> BOOL;
1032     pub fn GetFileInformationByHandle(hFile: HANDLE,
1033                             lpFileInformation: LPBY_HANDLE_FILE_INFORMATION)
1034                             -> BOOL;
1035
1036     pub fn SetLastError(dwErrCode: DWORD);
1037     pub fn GetCommandLineW() -> *mut LPCWSTR;
1038     pub fn LocalFree(ptr: *mut c_void);
1039     pub fn CommandLineToArgvW(lpCmdLine: *mut LPCWSTR,
1040                               pNumArgs: *mut c_int) -> *mut *mut u16;
1041     pub fn GetTempPathW(nBufferLength: DWORD,
1042                         lpBuffer: LPCWSTR) -> DWORD;
1043     pub fn OpenProcessToken(ProcessHandle: HANDLE,
1044                             DesiredAccess: DWORD,
1045                             TokenHandle: *mut HANDLE) -> BOOL;
1046     pub fn GetCurrentProcess() -> HANDLE;
1047     pub fn GetCurrentThread() -> HANDLE;
1048     pub fn GetStdHandle(which: DWORD) -> HANDLE;
1049     pub fn ExitProcess(uExitCode: c_uint) -> !;
1050     pub fn DeviceIoControl(hDevice: HANDLE,
1051                            dwIoControlCode: DWORD,
1052                            lpInBuffer: LPVOID,
1053                            nInBufferSize: DWORD,
1054                            lpOutBuffer: LPVOID,
1055                            nOutBufferSize: DWORD,
1056                            lpBytesReturned: LPDWORD,
1057                            lpOverlapped: LPOVERLAPPED) -> BOOL;
1058     pub fn CreateThread(lpThreadAttributes: LPSECURITY_ATTRIBUTES,
1059                         dwStackSize: SIZE_T,
1060                         lpStartAddress: extern "system" fn(*mut c_void)
1061                                                            -> DWORD,
1062                         lpParameter: LPVOID,
1063                         dwCreationFlags: DWORD,
1064                         lpThreadId: LPDWORD) -> HANDLE;
1065     pub fn WaitForSingleObject(hHandle: HANDLE,
1066                                dwMilliseconds: DWORD) -> DWORD;
1067     pub fn SwitchToThread() -> BOOL;
1068     pub fn Sleep(dwMilliseconds: DWORD);
1069     pub fn GetProcessId(handle: HANDLE) -> DWORD;
1070     pub fn GetUserProfileDirectoryW(hToken: HANDLE,
1071                                     lpProfileDir: LPWSTR,
1072                                     lpcchSize: *mut DWORD) -> BOOL;
1073     pub fn SetHandleInformation(hObject: HANDLE,
1074                                 dwMask: DWORD,
1075                                 dwFlags: DWORD) -> BOOL;
1076     pub fn CopyFileExW(lpExistingFileName: LPCWSTR,
1077                        lpNewFileName: LPCWSTR,
1078                        lpProgressRoutine: LPPROGRESS_ROUTINE,
1079                        lpData: LPVOID,
1080                        pbCancel: LPBOOL,
1081                        dwCopyFlags: DWORD) -> BOOL;
1082     pub fn AddVectoredExceptionHandler(FirstHandler: ULONG,
1083                                        VectoredHandler: PVECTORED_EXCEPTION_HANDLER)
1084                                        -> LPVOID;
1085     pub fn FormatMessageW(flags: DWORD,
1086                           lpSrc: LPVOID,
1087                           msgId: DWORD,
1088                           langId: DWORD,
1089                           buf: LPWSTR,
1090                           nsize: DWORD,
1091                           args: *const c_void)
1092                           -> DWORD;
1093     pub fn TlsAlloc() -> DWORD;
1094     pub fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
1095     pub fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
1096     pub fn GetLastError() -> DWORD;
1097     pub fn QueryPerformanceFrequency(lpFrequency: *mut LARGE_INTEGER) -> BOOL;
1098     pub fn QueryPerformanceCounter(lpPerformanceCount: *mut LARGE_INTEGER)
1099                                    -> BOOL;
1100     pub fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: LPDWORD) -> BOOL;
1101     pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) -> BOOL;
1102     pub fn CreateProcessW(lpApplicationName: LPCWSTR,
1103                           lpCommandLine: LPWSTR,
1104                           lpProcessAttributes: LPSECURITY_ATTRIBUTES,
1105                           lpThreadAttributes: LPSECURITY_ATTRIBUTES,
1106                           bInheritHandles: BOOL,
1107                           dwCreationFlags: DWORD,
1108                           lpEnvironment: LPVOID,
1109                           lpCurrentDirectory: LPCWSTR,
1110                           lpStartupInfo: LPSTARTUPINFO,
1111                           lpProcessInformation: LPPROCESS_INFORMATION)
1112                           -> BOOL;
1113     pub fn GetEnvironmentVariableW(n: LPCWSTR, v: LPWSTR, nsize: DWORD) -> DWORD;
1114     pub fn SetEnvironmentVariableW(n: LPCWSTR, v: LPCWSTR) -> BOOL;
1115     pub fn GetEnvironmentStringsW() -> LPWCH;
1116     pub fn FreeEnvironmentStringsW(env_ptr: LPWCH) -> BOOL;
1117     pub fn GetModuleFileNameW(hModule: HMODULE,
1118                               lpFilename: LPWSTR,
1119                               nSize: DWORD)
1120                               -> DWORD;
1121     pub fn CreateDirectoryW(lpPathName: LPCWSTR,
1122                             lpSecurityAttributes: LPSECURITY_ATTRIBUTES)
1123                             -> BOOL;
1124     pub fn DeleteFileW(lpPathName: LPCWSTR) -> BOOL;
1125     pub fn GetCurrentDirectoryW(nBufferLength: DWORD, lpBuffer: LPWSTR) -> DWORD;
1126     pub fn SetCurrentDirectoryW(lpPathName: LPCWSTR) -> BOOL;
1127     pub fn WideCharToMultiByte(CodePage: UINT,
1128                                dwFlags: DWORD,
1129                                lpWideCharStr: LPCWSTR,
1130                                cchWideChar: c_int,
1131                                lpMultiByteStr: LPSTR,
1132                                cbMultiByte: c_int,
1133                                lpDefaultChar: LPCSTR,
1134                                lpUsedDefaultChar: LPBOOL) -> c_int;
1135
1136     pub fn closesocket(socket: SOCKET) -> c_int;
1137     pub fn recv(socket: SOCKET, buf: *mut c_void, len: c_int,
1138                 flags: c_int) -> c_int;
1139     pub fn send(socket: SOCKET, buf: *const c_void, len: c_int,
1140                 flags: c_int) -> c_int;
1141     pub fn recvfrom(socket: SOCKET,
1142                     buf: *mut c_void,
1143                     len: c_int,
1144                     flags: c_int,
1145                     addr: *mut SOCKADDR,
1146                     addrlen: *mut c_int)
1147                     -> c_int;
1148     pub fn sendto(socket: SOCKET,
1149                   buf: *const c_void,
1150                   len: c_int,
1151                   flags: c_int,
1152                   addr: *const SOCKADDR,
1153                   addrlen: c_int)
1154                   -> c_int;
1155     pub fn shutdown(socket: SOCKET, how: c_int) -> c_int;
1156     pub fn accept(socket: SOCKET,
1157                   address: *mut SOCKADDR,
1158                   address_len: *mut c_int)
1159                   -> SOCKET;
1160     pub fn DuplicateHandle(hSourceProcessHandle: HANDLE,
1161                            hSourceHandle: HANDLE,
1162                            hTargetProcessHandle: HANDLE,
1163                            lpTargetHandle: LPHANDLE,
1164                            dwDesiredAccess: DWORD,
1165                            bInheritHandle: BOOL,
1166                            dwOptions: DWORD)
1167                            -> BOOL;
1168     pub fn ReadFile(hFile: HANDLE,
1169                     lpBuffer: LPVOID,
1170                     nNumberOfBytesToRead: DWORD,
1171                     lpNumberOfBytesRead: LPDWORD,
1172                     lpOverlapped: LPOVERLAPPED)
1173                     -> BOOL;
1174     pub fn WriteFile(hFile: HANDLE,
1175                      lpBuffer: LPVOID,
1176                      nNumberOfBytesToWrite: DWORD,
1177                      lpNumberOfBytesWritten: LPDWORD,
1178                      lpOverlapped: LPOVERLAPPED)
1179                      -> BOOL;
1180     pub fn CloseHandle(hObject: HANDLE) -> BOOL;
1181     pub fn CreateHardLinkW(lpSymlinkFileName: LPCWSTR,
1182                            lpTargetFileName: LPCWSTR,
1183                            lpSecurityAttributes: LPSECURITY_ATTRIBUTES)
1184                            -> BOOL;
1185     pub fn MoveFileExW(lpExistingFileName: LPCWSTR,
1186                        lpNewFileName: LPCWSTR,
1187                        dwFlags: DWORD)
1188                        -> BOOL;
1189     pub fn SetFilePointerEx(hFile: HANDLE,
1190                             liDistanceToMove: LARGE_INTEGER,
1191                             lpNewFilePointer: PLARGE_INTEGER,
1192                             dwMoveMethod: DWORD)
1193                             -> BOOL;
1194     pub fn FlushFileBuffers(hFile: HANDLE) -> BOOL;
1195     pub fn CreateFileW(lpFileName: LPCWSTR,
1196                        dwDesiredAccess: DWORD,
1197                        dwShareMode: DWORD,
1198                        lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
1199                        dwCreationDisposition: DWORD,
1200                        dwFlagsAndAttributes: DWORD,
1201                        hTemplateFile: HANDLE)
1202                        -> HANDLE;
1203
1204     pub fn FindFirstFileW(fileName: LPCWSTR,
1205                           findFileData: LPWIN32_FIND_DATAW)
1206                           -> HANDLE;
1207     pub fn FindNextFileW(findFile: HANDLE, findFileData: LPWIN32_FIND_DATAW)
1208                          -> BOOL;
1209     pub fn FindClose(findFile: HANDLE) -> BOOL;
1210     #[cfg(feature = "backtrace")]
1211     pub fn RtlCaptureContext(ctx: *mut CONTEXT);
1212     pub fn getsockopt(s: SOCKET,
1213                       level: c_int,
1214                       optname: c_int,
1215                       optval: *mut c_char,
1216                       optlen: *mut c_int)
1217                       -> c_int;
1218     pub fn setsockopt(s: SOCKET,
1219                       level: c_int,
1220                       optname: c_int,
1221                       optval: *const c_void,
1222                       optlen: c_int)
1223                       -> c_int;
1224     pub fn getsockname(socket: SOCKET,
1225                        address: *mut SOCKADDR,
1226                        address_len: *mut c_int)
1227                        -> c_int;
1228     pub fn getpeername(socket: SOCKET,
1229                        address: *mut SOCKADDR,
1230                        address_len: *mut c_int)
1231                        -> c_int;
1232     pub fn bind(socket: SOCKET, address: *const SOCKADDR,
1233                 address_len: socklen_t) -> c_int;
1234     pub fn listen(socket: SOCKET, backlog: c_int) -> c_int;
1235     pub fn connect(socket: SOCKET, address: *const SOCKADDR, len: c_int)
1236                    -> c_int;
1237     pub fn getaddrinfo(node: *const c_char, service: *const c_char,
1238                        hints: *const ADDRINFOA,
1239                        res: *mut *mut ADDRINFOA) -> c_int;
1240     pub fn freeaddrinfo(res: *mut ADDRINFOA);
1241
1242     #[cfg(feature = "backtrace")]
1243     pub fn LoadLibraryW(name: LPCWSTR) -> HMODULE;
1244     #[cfg(feature = "backtrace")]
1245     pub fn FreeLibrary(handle: HMODULE) -> BOOL;
1246     pub fn GetProcAddress(handle: HMODULE,
1247                           name: LPCSTR) -> *mut c_void;
1248     pub fn GetModuleHandleW(lpModuleName: LPCWSTR) -> HMODULE;
1249
1250     pub fn GetSystemTimeAsFileTime(lpSystemTimeAsFileTime: LPFILETIME);
1251
1252     pub fn CreateEventW(lpEventAttributes: LPSECURITY_ATTRIBUTES,
1253                         bManualReset: BOOL,
1254                         bInitialState: BOOL,
1255                         lpName: LPCWSTR) -> HANDLE;
1256     pub fn WaitForMultipleObjects(nCount: DWORD,
1257                                   lpHandles: *const HANDLE,
1258                                   bWaitAll: BOOL,
1259                                   dwMilliseconds: DWORD) -> DWORD;
1260     pub fn CreateNamedPipeW(lpName: LPCWSTR,
1261                             dwOpenMode: DWORD,
1262                             dwPipeMode: DWORD,
1263                             nMaxInstances: DWORD,
1264                             nOutBufferSize: DWORD,
1265                             nInBufferSize: DWORD,
1266                             nDefaultTimeOut: DWORD,
1267                             lpSecurityAttributes: LPSECURITY_ATTRIBUTES)
1268                             -> HANDLE;
1269     pub fn CancelIo(handle: HANDLE) -> BOOL;
1270     pub fn GetOverlappedResult(hFile: HANDLE,
1271                                lpOverlapped: LPOVERLAPPED,
1272                                lpNumberOfBytesTransferred: LPDWORD,
1273                                bWait: BOOL) -> BOOL;
1274     pub fn select(nfds: c_int,
1275                   readfds: *mut fd_set,
1276                   writefds: *mut fd_set,
1277                   exceptfds: *mut fd_set,
1278                   timeout: *const timeval) -> c_int;
1279
1280     #[link_name = "SystemFunction036"]
1281     pub fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: ULONG) -> BOOLEAN;
1282
1283     pub fn GetProcessHeap() -> HANDLE;
1284     pub fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
1285     pub fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
1286     pub fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
1287 }
1288
1289 // Functions that aren't available on every version of Windows that we support,
1290 // but we still use them and just provide some form of a fallback implementation.
1291 compat_fn! {
1292     kernel32:
1293
1294     pub fn CreateSymbolicLinkW(_lpSymlinkFileName: LPCWSTR,
1295                                _lpTargetFileName: LPCWSTR,
1296                                _dwFlags: DWORD) -> BOOLEAN {
1297         SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1298     }
1299     pub fn GetFinalPathNameByHandleW(_hFile: HANDLE,
1300                                      _lpszFilePath: LPCWSTR,
1301                                      _cchFilePath: DWORD,
1302                                      _dwFlags: DWORD) -> DWORD {
1303         SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1304     }
1305     pub fn SetThreadStackGuarantee(_size: *mut c_ulong) -> BOOL {
1306         SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1307     }
1308     pub fn SetThreadDescription(hThread: HANDLE,
1309                                 lpThreadDescription: LPCWSTR) -> HRESULT {
1310         SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); E_NOTIMPL
1311     }
1312     pub fn SetFileInformationByHandle(_hFile: HANDLE,
1313                     _FileInformationClass: FILE_INFO_BY_HANDLE_CLASS,
1314                     _lpFileInformation: LPVOID,
1315                     _dwBufferSize: DWORD) -> BOOL {
1316         SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1317     }
1318     pub fn SleepConditionVariableSRW(ConditionVariable: PCONDITION_VARIABLE,
1319                                      SRWLock: PSRWLOCK,
1320                                      dwMilliseconds: DWORD,
1321                                      Flags: ULONG) -> BOOL {
1322         panic!("condition variables not available")
1323     }
1324     pub fn WakeConditionVariable(ConditionVariable: PCONDITION_VARIABLE)
1325                                  -> () {
1326         panic!("condition variables not available")
1327     }
1328     pub fn WakeAllConditionVariable(ConditionVariable: PCONDITION_VARIABLE)
1329                                     -> () {
1330         panic!("condition variables not available")
1331     }
1332     pub fn AcquireSRWLockExclusive(SRWLock: PSRWLOCK) -> () {
1333         panic!("rwlocks not available")
1334     }
1335     pub fn AcquireSRWLockShared(SRWLock: PSRWLOCK) -> () {
1336         panic!("rwlocks not available")
1337     }
1338     pub fn ReleaseSRWLockExclusive(SRWLock: PSRWLOCK) -> () {
1339         panic!("rwlocks not available")
1340     }
1341     pub fn ReleaseSRWLockShared(SRWLock: PSRWLOCK) -> () {
1342         panic!("rwlocks not available")
1343     }
1344     pub fn TryAcquireSRWLockExclusive(SRWLock: PSRWLOCK) -> BOOLEAN {
1345         panic!("rwlocks not available")
1346     }
1347     pub fn TryAcquireSRWLockShared(SRWLock: PSRWLOCK) -> BOOLEAN {
1348         panic!("rwlocks not available")
1349     }
1350 }
1351
1352 #[cfg(all(target_env = "gnu", feature = "backtrace"))]
1353 mod gnu {
1354     use super::*;
1355
1356     pub const PROCESS_QUERY_INFORMATION: DWORD = 0x0400;
1357
1358     pub const CP_ACP: UINT = 0;
1359
1360     pub const WC_NO_BEST_FIT_CHARS: DWORD = 0x00000400;
1361
1362     extern "system" {
1363         pub fn OpenProcess(dwDesiredAccess: DWORD,
1364                            bInheritHandle: BOOL,
1365                            dwProcessId: DWORD) -> HANDLE;
1366     }
1367
1368     compat_fn! {
1369         kernel32:
1370
1371         pub fn QueryFullProcessImageNameW(_hProcess: HANDLE,
1372                                           _dwFlags: DWORD,
1373                                           _lpExeName: LPWSTR,
1374                                           _lpdwSize: LPDWORD) -> BOOL {
1375             SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); 0
1376         }
1377     }
1378 }
1379
1380 #[cfg(all(target_env = "gnu", feature = "backtrace"))]
1381 pub use self::gnu::*;