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