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