2 * winmisc.c: miscellaneous Windows-specific things
13 OSVERSIONINFO osVersion;
15 char *platform_get_x_display(void) {
16 /* We may as well check for DISPLAY in case it's useful. */
17 return dupstr(getenv("DISPLAY"));
20 Filename *filename_from_str(const char *str)
22 Filename *ret = snew(Filename);
23 ret->path = dupstr(str);
27 Filename *filename_copy(const Filename *fn)
29 return filename_from_str(fn->path);
32 const char *filename_to_str(const Filename *fn)
37 int filename_equal(const Filename *f1, const Filename *f2)
39 return !strcmp(f1->path, f2->path);
42 int filename_is_null(const Filename *fn)
47 void filename_free(Filename *fn)
53 int filename_serialise(const Filename *f, void *vdata)
55 char *data = (char *)vdata;
56 int len = strlen(f->path) + 1; /* include trailing NUL */
58 strcpy(data, f->path);
62 Filename *filename_deserialise(void *vdata, int maxsize, int *used)
64 char *data = (char *)vdata;
66 end = memchr(data, '\0', maxsize);
71 return filename_from_str(data);
74 #ifndef NO_SECUREZEROMEMORY
76 * Windows implementation of smemclr (see misc.c) using SecureZeroMemory.
78 void smemclr(void *b, size_t n) {
80 SecureZeroMemory(b, n);
84 char *get_username(void)
88 int got_username = FALSE;
89 DECL_WINDOWS_FUNCTION(static, BOOLEAN, GetUserNameExA,
90 (EXTENDED_NAME_FORMAT, LPSTR, PULONG));
93 static int tried_usernameex = FALSE;
94 if (!tried_usernameex) {
95 /* Not available on Win9x, so load dynamically */
96 HMODULE secur32 = load_system32_dll("secur32.dll");
97 GET_WINDOWS_FUNCTION(secur32, GetUserNameExA);
98 tried_usernameex = TRUE;
102 if (p_GetUserNameExA) {
104 * If available, use the principal -- this avoids the problem
105 * that the local username is case-insensitive but Kerberos
106 * usernames are case-sensitive.
111 (void) p_GetUserNameExA(NameUserPrincipal, NULL, &namelen);
113 user = snewn(namelen, char);
114 got_username = p_GetUserNameExA(NameUserPrincipal, user, &namelen);
116 char *p = strchr(user, '@');
124 /* Fall back to local user name */
126 if (GetUserName(NULL, &namelen) == FALSE) {
128 * Apparently this doesn't work at least on Windows XP SP2.
129 * Thus assume a maximum of 256. It will fail again if it
135 user = snewn(namelen, char);
136 got_username = GetUserName(user, &namelen);
142 return got_username ? user : NULL;
145 BOOL init_winver(void)
147 ZeroMemory(&osVersion, sizeof(osVersion));
148 osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
149 return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
152 HMODULE load_system32_dll(const char *libname)
155 * Wrapper function to load a DLL out of c:\windows\system32
156 * without going through the full DLL search path. (Hence no
157 * attack is possible by placing a substitute DLL earlier on that
160 static char *sysdir = NULL;
167 size = 3*size/2 + 512;
168 sysdir = sresize(sysdir, size, char);
169 len = GetSystemDirectory(sysdir, size);
170 } while (len >= size);
173 fullpath = dupcat(sysdir, "\\", libname, NULL);
174 ret = LoadLibrary(fullpath);
180 * A tree234 containing mappings from system error codes to strings.
188 static int errstring_find(void *av, void *bv)
191 struct errstring *b = (struct errstring *)bv;
198 static int errstring_compare(void *av, void *bv)
200 struct errstring *a = (struct errstring *)av;
201 return errstring_find(&a->error, bv);
204 static tree234 *errstrings = NULL;
206 const char *win_strerror(int error)
208 struct errstring *es;
211 errstrings = newtree234(errstring_compare);
213 es = find234(errstrings, &error, errstring_find);
216 char msgtext[65536]; /* maximum size for FormatMessage is 64K */
218 es = snew(struct errstring);
220 if (!FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM |
221 FORMAT_MESSAGE_IGNORE_INSERTS), NULL, error,
222 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
223 msgtext, lenof(msgtext)-1, NULL)) {
225 "(unable to format: FormatMessage returned %u)",
226 (unsigned int)GetLastError());
228 int len = strlen(msgtext);
229 if (len > 0 && msgtext[len-1] == '\n')
230 msgtext[len-1] = '\0';
232 es->text = dupprintf("Error %d: %s", error, msgtext);
233 add234(errstrings, es);
240 static FILE *debug_fp = NULL;
241 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
242 static int debug_got_console = 0;
244 void dputs(const char *buf)
248 if (!debug_got_console) {
249 if (AllocConsole()) {
250 debug_got_console = 1;
251 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
255 debug_fp = fopen("debug.log", "w");
258 if (debug_hdl != INVALID_HANDLE_VALUE) {
259 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
261 fputs(buf, debug_fp);
268 * Minefield - a Windows equivalent for Electric Fence
271 #define PAGESIZE 4096
276 * We start by reserving as much virtual address space as Windows
277 * will sensibly (or not sensibly) let us have. We flag it all as
280 * Any allocation attempt is satisfied by committing one or more
281 * pages, with an uncommitted page on either side. The returned
282 * memory region is jammed up against the _end_ of the pages.
284 * Freeing anything causes instantaneous decommitment of the pages
285 * involved, so stale pointers are caught as soon as possible.
288 static int minefield_initialised = 0;
289 static void *minefield_region = NULL;
290 static long minefield_size = 0;
291 static long minefield_npages = 0;
292 static long minefield_curpos = 0;
293 static unsigned short *minefield_admin = NULL;
294 static void *minefield_pages = NULL;
296 static void minefield_admin_hide(int hide)
298 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
299 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
302 static void minefield_init(void)
308 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
309 minefield_region = VirtualAlloc(NULL, size,
310 MEM_RESERVE, PAGE_NOACCESS);
311 if (minefield_region)
314 minefield_size = size;
317 * Firstly, allocate a section of that to be the admin block.
318 * We'll need a two-byte field for each page.
320 minefield_admin = minefield_region;
321 minefield_npages = minefield_size / PAGESIZE;
322 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
323 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
324 minefield_pages = (char *) minefield_region + admin_size;
327 * Commit the admin region.
329 VirtualAlloc(minefield_admin, minefield_npages * 2,
330 MEM_COMMIT, PAGE_READWRITE);
333 * Mark all pages as unused (0xFFFF).
335 for (i = 0; i < minefield_npages; i++)
336 minefield_admin[i] = 0xFFFF;
339 * Hide the admin region.
341 minefield_admin_hide(1);
343 minefield_initialised = 1;
346 static void minefield_bomb(void)
348 div(1, *(int *) minefield_pages);
351 static void *minefield_alloc(int size)
354 int pos, lim, region_end, region_start;
358 npages = (size + PAGESIZE - 1) / PAGESIZE;
360 minefield_admin_hide(0);
363 * Search from current position until we find a contiguous
364 * bunch of npages+2 unused pages.
366 pos = minefield_curpos;
367 lim = minefield_npages;
369 /* Skip over used pages. */
370 while (pos < lim && minefield_admin[pos] != 0xFFFF)
372 /* Count unused pages. */
374 while (pos < lim && pos - start < npages + 2 &&
375 minefield_admin[pos] == 0xFFFF)
377 if (pos - start == npages + 2)
379 /* If we've reached the limit, reset the limit or stop. */
381 if (lim == minefield_npages) {
382 /* go round and start again at zero */
383 lim = minefield_curpos;
386 minefield_admin_hide(1);
392 minefield_curpos = pos - 1;
395 * We have npages+2 unused pages starting at start. We leave
396 * the first and last of these alone and use the rest.
398 region_end = (start + npages + 1) * PAGESIZE;
399 region_start = region_end - size;
400 /* FIXME: could align here if we wanted */
403 * Update the admin region.
405 for (i = start + 2; i < start + npages + 1; i++)
406 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
407 minefield_admin[start + 1] = region_start % PAGESIZE;
409 minefield_admin_hide(1);
411 VirtualAlloc((char *) minefield_pages + region_start, size,
412 MEM_COMMIT, PAGE_READWRITE);
413 return (char *) minefield_pages + region_start;
416 static void minefield_free(void *ptr)
418 int region_start, i, j;
420 minefield_admin_hide(0);
422 region_start = (char *) ptr - (char *) minefield_pages;
423 i = region_start / PAGESIZE;
424 if (i < 0 || i >= minefield_npages ||
425 minefield_admin[i] != region_start % PAGESIZE)
427 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
428 minefield_admin[j] = 0xFFFF;
431 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
433 minefield_admin_hide(1);
436 static int minefield_get_size(void *ptr)
438 int region_start, i, j;
440 minefield_admin_hide(0);
442 region_start = (char *) ptr - (char *) minefield_pages;
443 i = region_start / PAGESIZE;
444 if (i < 0 || i >= minefield_npages ||
445 minefield_admin[i] != region_start % PAGESIZE)
447 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
449 minefield_admin_hide(1);
451 return j * PAGESIZE - region_start;
454 void *minefield_c_malloc(size_t size)
456 if (!minefield_initialised)
458 return minefield_alloc(size);
461 void minefield_c_free(void *p)
463 if (!minefield_initialised)
469 * realloc _always_ moves the chunk, for rapid detection of code
470 * that assumes it won't.
472 void *minefield_c_realloc(void *p, size_t size)
476 if (!minefield_initialised)
478 q = minefield_alloc(size);
479 oldsize = minefield_get_size(p);
480 memcpy(q, p, (oldsize < size ? oldsize : size));
485 #endif /* MINEFIELD */
487 FontSpec *fontspec_new(const char *name,
488 int bold, int height, int charset)
490 FontSpec *f = snew(FontSpec);
491 f->name = dupstr(name);
494 f->charset = charset;
497 FontSpec *fontspec_copy(const FontSpec *f)
499 return fontspec_new(f->name, f->isbold, f->height, f->charset);
501 void fontspec_free(FontSpec *f)
506 int fontspec_serialise(FontSpec *f, void *vdata)
508 char *data = (char *)vdata;
509 int len = strlen(f->name) + 1; /* include trailing NUL */
511 strcpy(data, f->name);
512 PUT_32BIT_MSB_FIRST(data + len, f->isbold);
513 PUT_32BIT_MSB_FIRST(data + len + 4, f->height);
514 PUT_32BIT_MSB_FIRST(data + len + 8, f->charset);
516 return len + 12; /* also include three 4-byte ints */
518 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
520 char *data = (char *)vdata;
524 end = memchr(data, '\0', maxsize-12);
528 *used = end - data + 12;
529 return fontspec_new(data,
530 GET_32BIT_MSB_FIRST(end),
531 GET_32BIT_MSB_FIRST(end + 4),
532 GET_32BIT_MSB_FIRST(end + 8));