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