2 * winmisc.c: miscellaneous Windows-specific things
10 OSVERSIONINFO osVersion
;
12 char *platform_get_x_display(void) {
13 /* We may as well check for DISPLAY in case it's useful. */
14 return dupstr(getenv("DISPLAY"));
17 Filename
filename_from_str(const char *str
)
20 strncpy(ret
.path
, str
, sizeof(ret
.path
));
21 ret
.path
[sizeof(ret
.path
)-1] = '\0';
25 const char *filename_to_str(const Filename
*fn
)
30 int filename_equal(Filename f1
, Filename f2
)
32 return !strcmp(f1
.path
, f2
.path
);
35 int filename_is_null(Filename fn
)
40 char *get_username(void)
44 int got_username
= FALSE
;
45 DECL_WINDOWS_FUNCTION(static, BOOLEAN
, GetUserNameExA
,
46 (EXTENDED_NAME_FORMAT
, LPSTR
, PULONG
));
49 static int tried_usernameex
= FALSE
;
50 if (!tried_usernameex
) {
51 /* Not available on Win9x, so load dynamically */
52 HMODULE secur32
= load_system32_dll("secur32.dll");
53 GET_WINDOWS_FUNCTION(secur32
, GetUserNameExA
);
54 tried_usernameex
= TRUE
;
58 if (p_GetUserNameExA
) {
60 * If available, use the principal -- this avoids the problem
61 * that the local username is case-insensitive but Kerberos
62 * usernames are case-sensitive.
67 (void) p_GetUserNameExA(NameUserPrincipal
, NULL
, &namelen
);
69 user
= snewn(namelen
, char);
70 got_username
= p_GetUserNameExA(NameUserPrincipal
, user
, &namelen
);
72 char *p
= strchr(user
, '@');
80 /* Fall back to local user name */
82 if (GetUserName(NULL
, &namelen
) == FALSE
) {
84 * Apparently this doesn't work at least on Windows XP SP2.
85 * Thus assume a maximum of 256. It will fail again if it
91 user
= snewn(namelen
, char);
92 got_username
= GetUserName(user
, &namelen
);
98 return got_username ? user
: NULL
;
101 BOOL
init_winver(void)
103 ZeroMemory(&osVersion
, sizeof(osVersion
));
104 osVersion
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFO
);
105 return GetVersionEx ( (OSVERSIONINFO
*) &osVersion
);
108 HMODULE
load_system32_dll(const char *libname
)
111 * Wrapper function to load a DLL out of c:\windows\system32
112 * without going through the full DLL search path. (Hence no
113 * attack is possible by placing a substitute DLL earlier on that
116 static char *sysdir
= NULL
;
123 size
= 3*size
/2 + 512;
124 sysdir
= sresize(sysdir
, size
, char);
125 len
= GetSystemDirectory(sysdir
, size
);
126 } while (len
>= size
);
129 fullpath
= dupcat(sysdir
, "\\", libname
, NULL
);
130 ret
= LoadLibrary(fullpath
);
136 static FILE *debug_fp
= NULL
;
137 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
138 static int debug_got_console
= 0;
140 void dputs(char *buf
)
144 if (!debug_got_console
) {
145 if (AllocConsole()) {
146 debug_got_console
= 1;
147 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
151 debug_fp
= fopen("debug.log", "w");
154 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
155 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
157 fputs(buf
, debug_fp
);
164 * Minefield - a Windows equivalent for Electric Fence
167 #define PAGESIZE 4096
172 * We start by reserving as much virtual address space as Windows
173 * will sensibly (or not sensibly) let us have. We flag it all as
176 * Any allocation attempt is satisfied by committing one or more
177 * pages, with an uncommitted page on either side. The returned
178 * memory region is jammed up against the _end_ of the pages.
180 * Freeing anything causes instantaneous decommitment of the pages
181 * involved, so stale pointers are caught as soon as possible.
184 static int minefield_initialised
= 0;
185 static void *minefield_region
= NULL
;
186 static long minefield_size
= 0;
187 static long minefield_npages
= 0;
188 static long minefield_curpos
= 0;
189 static unsigned short *minefield_admin
= NULL
;
190 static void *minefield_pages
= NULL
;
192 static void minefield_admin_hide(int hide
)
194 int access
= hide ? PAGE_NOACCESS
: PAGE_READWRITE
;
195 VirtualProtect(minefield_admin
, minefield_npages
* 2, access
, NULL
);
198 static void minefield_init(void)
204 for (size
= 0x40000000; size
> 0; size
= ((size
>> 3) * 7) & ~0xFFF) {
205 minefield_region
= VirtualAlloc(NULL
, size
,
206 MEM_RESERVE
, PAGE_NOACCESS
);
207 if (minefield_region
)
210 minefield_size
= size
;
213 * Firstly, allocate a section of that to be the admin block.
214 * We'll need a two-byte field for each page.
216 minefield_admin
= minefield_region
;
217 minefield_npages
= minefield_size
/ PAGESIZE
;
218 admin_size
= (minefield_npages
* 2 + PAGESIZE
- 1) & ~(PAGESIZE
- 1);
219 minefield_npages
= (minefield_size
- admin_size
) / PAGESIZE
;
220 minefield_pages
= (char *) minefield_region
+ admin_size
;
223 * Commit the admin region.
225 VirtualAlloc(minefield_admin
, minefield_npages
* 2,
226 MEM_COMMIT
, PAGE_READWRITE
);
229 * Mark all pages as unused (0xFFFF).
231 for (i
= 0; i
< minefield_npages
; i
++)
232 minefield_admin
[i
] = 0xFFFF;
235 * Hide the admin region.
237 minefield_admin_hide(1);
239 minefield_initialised
= 1;
242 static void minefield_bomb(void)
244 div(1, *(int *) minefield_pages
);
247 static void *minefield_alloc(int size
)
250 int pos
, lim
, region_end
, region_start
;
254 npages
= (size
+ PAGESIZE
- 1) / PAGESIZE
;
256 minefield_admin_hide(0);
259 * Search from current position until we find a contiguous
260 * bunch of npages+2 unused pages.
262 pos
= minefield_curpos
;
263 lim
= minefield_npages
;
265 /* Skip over used pages. */
266 while (pos
< lim
&& minefield_admin
[pos
] != 0xFFFF)
268 /* Count unused pages. */
270 while (pos
< lim
&& pos
- start
< npages
+ 2 &&
271 minefield_admin
[pos
] == 0xFFFF)
273 if (pos
- start
== npages
+ 2)
275 /* If we've reached the limit, reset the limit or stop. */
277 if (lim
== minefield_npages
) {
278 /* go round and start again at zero */
279 lim
= minefield_curpos
;
282 minefield_admin_hide(1);
288 minefield_curpos
= pos
- 1;
291 * We have npages+2 unused pages starting at start. We leave
292 * the first and last of these alone and use the rest.
294 region_end
= (start
+ npages
+ 1) * PAGESIZE
;
295 region_start
= region_end
- size
;
296 /* FIXME: could align here if we wanted */
299 * Update the admin region.
301 for (i
= start
+ 2; i
< start
+ npages
+ 1; i
++)
302 minefield_admin
[i
] = 0xFFFE; /* used but no region starts here */
303 minefield_admin
[start
+ 1] = region_start
% PAGESIZE
;
305 minefield_admin_hide(1);
307 VirtualAlloc((char *) minefield_pages
+ region_start
, size
,
308 MEM_COMMIT
, PAGE_READWRITE
);
309 return (char *) minefield_pages
+ region_start
;
312 static void minefield_free(void *ptr
)
314 int region_start
, i
, j
;
316 minefield_admin_hide(0);
318 region_start
= (char *) ptr
- (char *) minefield_pages
;
319 i
= region_start
/ PAGESIZE
;
320 if (i
< 0 || i
>= minefield_npages
||
321 minefield_admin
[i
] != region_start
% PAGESIZE
)
323 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++) {
324 minefield_admin
[j
] = 0xFFFF;
327 VirtualFree(ptr
, j
* PAGESIZE
- region_start
, MEM_DECOMMIT
);
329 minefield_admin_hide(1);
332 static int minefield_get_size(void *ptr
)
334 int region_start
, i
, j
;
336 minefield_admin_hide(0);
338 region_start
= (char *) ptr
- (char *) minefield_pages
;
339 i
= region_start
/ PAGESIZE
;
340 if (i
< 0 || i
>= minefield_npages
||
341 minefield_admin
[i
] != region_start
% PAGESIZE
)
343 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++);
345 minefield_admin_hide(1);
347 return j
* PAGESIZE
- region_start
;
350 void *minefield_c_malloc(size_t size
)
352 if (!minefield_initialised
)
354 return minefield_alloc(size
);
357 void minefield_c_free(void *p
)
359 if (!minefield_initialised
)
365 * realloc _always_ moves the chunk, for rapid detection of code
366 * that assumes it won't.
368 void *minefield_c_realloc(void *p
, size_t size
)
372 if (!minefield_initialised
)
374 q
= minefield_alloc(size
);
375 oldsize
= minefield_get_size(p
);
376 memcpy(q
, p
, (oldsize
< size ? oldsize
: size
));
381 #endif /* MINEFIELD */