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)
19 Filename *ret = snew(Filename);
20 ret->path = dupstr(str);
24 Filename *filename_copy(const Filename *fn)
26 return filename_from_str(fn->path);
29 const char *filename_to_str(const Filename *fn)
34 int filename_equal(const Filename *f1, const Filename *f2)
36 return !strcmp(f1->path, f2->path);
39 int filename_is_null(const Filename *fn)
44 void filename_free(Filename *fn)
50 int filename_serialise(const Filename *f, void *vdata)
52 char *data = (char *)vdata;
53 int len = strlen(f->path) + 1; /* include trailing NUL */
55 strcpy(data, f->path);
59 Filename *filename_deserialise(void *vdata, int maxsize, int *used)
61 char *data = (char *)vdata;
63 end = memchr(data, '\0', maxsize);
68 return filename_from_str(data);
71 char *get_username(void)
75 int got_username = FALSE;
76 DECL_WINDOWS_FUNCTION(static, BOOLEAN, GetUserNameExA,
77 (EXTENDED_NAME_FORMAT, LPSTR, PULONG));
80 static int tried_usernameex = FALSE;
81 if (!tried_usernameex) {
82 /* Not available on Win9x, so load dynamically */
83 HMODULE secur32 = load_system32_dll("secur32.dll");
84 GET_WINDOWS_FUNCTION(secur32, GetUserNameExA);
85 tried_usernameex = TRUE;
89 if (p_GetUserNameExA) {
91 * If available, use the principal -- this avoids the problem
92 * that the local username is case-insensitive but Kerberos
93 * usernames are case-sensitive.
98 (void) p_GetUserNameExA(NameUserPrincipal, NULL, &namelen);
100 user = snewn(namelen, char);
101 got_username = p_GetUserNameExA(NameUserPrincipal, user, &namelen);
103 char *p = strchr(user, '@');
111 /* Fall back to local user name */
113 if (GetUserName(NULL, &namelen) == FALSE) {
115 * Apparently this doesn't work at least on Windows XP SP2.
116 * Thus assume a maximum of 256. It will fail again if it
122 user = snewn(namelen, char);
123 got_username = GetUserName(user, &namelen);
129 return got_username ? user : NULL;
132 BOOL init_winver(void)
134 ZeroMemory(&osVersion, sizeof(osVersion));
135 osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
136 return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
139 HMODULE load_system32_dll(const char *libname)
142 * Wrapper function to load a DLL out of c:\windows\system32
143 * without going through the full DLL search path. (Hence no
144 * attack is possible by placing a substitute DLL earlier on that
147 static char *sysdir = NULL;
154 size = 3*size/2 + 512;
155 sysdir = sresize(sysdir, size, char);
156 len = GetSystemDirectory(sysdir, size);
157 } while (len >= size);
160 fullpath = dupcat(sysdir, "\\", libname, NULL);
161 ret = LoadLibrary(fullpath);
167 static FILE *debug_fp = NULL;
168 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
169 static int debug_got_console = 0;
171 void dputs(char *buf)
175 if (!debug_got_console) {
176 if (AllocConsole()) {
177 debug_got_console = 1;
178 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
182 debug_fp = fopen("debug.log", "w");
185 if (debug_hdl != INVALID_HANDLE_VALUE) {
186 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
188 fputs(buf, debug_fp);
195 * Minefield - a Windows equivalent for Electric Fence
198 #define PAGESIZE 4096
203 * We start by reserving as much virtual address space as Windows
204 * will sensibly (or not sensibly) let us have. We flag it all as
207 * Any allocation attempt is satisfied by committing one or more
208 * pages, with an uncommitted page on either side. The returned
209 * memory region is jammed up against the _end_ of the pages.
211 * Freeing anything causes instantaneous decommitment of the pages
212 * involved, so stale pointers are caught as soon as possible.
215 static int minefield_initialised = 0;
216 static void *minefield_region = NULL;
217 static long minefield_size = 0;
218 static long minefield_npages = 0;
219 static long minefield_curpos = 0;
220 static unsigned short *minefield_admin = NULL;
221 static void *minefield_pages = NULL;
223 static void minefield_admin_hide(int hide)
225 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
226 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
229 static void minefield_init(void)
235 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
236 minefield_region = VirtualAlloc(NULL, size,
237 MEM_RESERVE, PAGE_NOACCESS);
238 if (minefield_region)
241 minefield_size = size;
244 * Firstly, allocate a section of that to be the admin block.
245 * We'll need a two-byte field for each page.
247 minefield_admin = minefield_region;
248 minefield_npages = minefield_size / PAGESIZE;
249 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
250 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
251 minefield_pages = (char *) minefield_region + admin_size;
254 * Commit the admin region.
256 VirtualAlloc(minefield_admin, minefield_npages * 2,
257 MEM_COMMIT, PAGE_READWRITE);
260 * Mark all pages as unused (0xFFFF).
262 for (i = 0; i < minefield_npages; i++)
263 minefield_admin[i] = 0xFFFF;
266 * Hide the admin region.
268 minefield_admin_hide(1);
270 minefield_initialised = 1;
273 static void minefield_bomb(void)
275 div(1, *(int *) minefield_pages);
278 static void *minefield_alloc(int size)
281 int pos, lim, region_end, region_start;
285 npages = (size + PAGESIZE - 1) / PAGESIZE;
287 minefield_admin_hide(0);
290 * Search from current position until we find a contiguous
291 * bunch of npages+2 unused pages.
293 pos = minefield_curpos;
294 lim = minefield_npages;
296 /* Skip over used pages. */
297 while (pos < lim && minefield_admin[pos] != 0xFFFF)
299 /* Count unused pages. */
301 while (pos < lim && pos - start < npages + 2 &&
302 minefield_admin[pos] == 0xFFFF)
304 if (pos - start == npages + 2)
306 /* If we've reached the limit, reset the limit or stop. */
308 if (lim == minefield_npages) {
309 /* go round and start again at zero */
310 lim = minefield_curpos;
313 minefield_admin_hide(1);
319 minefield_curpos = pos - 1;
322 * We have npages+2 unused pages starting at start. We leave
323 * the first and last of these alone and use the rest.
325 region_end = (start + npages + 1) * PAGESIZE;
326 region_start = region_end - size;
327 /* FIXME: could align here if we wanted */
330 * Update the admin region.
332 for (i = start + 2; i < start + npages + 1; i++)
333 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
334 minefield_admin[start + 1] = region_start % PAGESIZE;
336 minefield_admin_hide(1);
338 VirtualAlloc((char *) minefield_pages + region_start, size,
339 MEM_COMMIT, PAGE_READWRITE);
340 return (char *) minefield_pages + region_start;
343 static void minefield_free(void *ptr)
345 int region_start, i, j;
347 minefield_admin_hide(0);
349 region_start = (char *) ptr - (char *) minefield_pages;
350 i = region_start / PAGESIZE;
351 if (i < 0 || i >= minefield_npages ||
352 minefield_admin[i] != region_start % PAGESIZE)
354 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
355 minefield_admin[j] = 0xFFFF;
358 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
360 minefield_admin_hide(1);
363 static int minefield_get_size(void *ptr)
365 int region_start, i, j;
367 minefield_admin_hide(0);
369 region_start = (char *) ptr - (char *) minefield_pages;
370 i = region_start / PAGESIZE;
371 if (i < 0 || i >= minefield_npages ||
372 minefield_admin[i] != region_start % PAGESIZE)
374 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
376 minefield_admin_hide(1);
378 return j * PAGESIZE - region_start;
381 void *minefield_c_malloc(size_t size)
383 if (!minefield_initialised)
385 return minefield_alloc(size);
388 void minefield_c_free(void *p)
390 if (!minefield_initialised)
396 * realloc _always_ moves the chunk, for rapid detection of code
397 * that assumes it won't.
399 void *minefield_c_realloc(void *p, size_t size)
403 if (!minefield_initialised)
405 q = minefield_alloc(size);
406 oldsize = minefield_get_size(p);
407 memcpy(q, p, (oldsize < size ? oldsize : size));
412 #endif /* MINEFIELD */
414 FontSpec *fontspec_new(const char *name,
415 int bold, int height, int charset)
417 FontSpec *f = snew(FontSpec);
418 f->name = dupstr(name);
421 f->charset = charset;
424 FontSpec *fontspec_copy(const FontSpec *f)
426 return fontspec_new(f->name, f->isbold, f->height, f->charset);
428 void fontspec_free(FontSpec *f)
433 int fontspec_serialise(FontSpec *f, void *vdata)
435 char *data = (char *)vdata;
436 int len = strlen(f->name) + 1; /* include trailing NUL */
438 strcpy(data, f->name);
439 PUT_32BIT_MSB_FIRST(data + len, f->isbold);
440 PUT_32BIT_MSB_FIRST(data + len + 4, f->height);
441 PUT_32BIT_MSB_FIRST(data + len + 8, f->charset);
443 return len + 12; /* also include three 4-byte ints */
445 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
447 char *data = (char *)vdata;
451 end = memchr(data, '\0', maxsize-12);
455 *used = end - data + 12;
456 return fontspec_new(data,
457 GET_32BIT_MSB_FIRST(end),
458 GET_32BIT_MSB_FIRST(end + 4),
459 GET_32BIT_MSB_FIRST(end + 8));