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