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 char filename_char_sanitise(char c)
76 if (strchr("<>:\"/\\|?*", c))
81 #ifndef NO_SECUREZEROMEMORY
83 * Windows implementation of smemclr (see misc.c) using SecureZeroMemory.
85 void smemclr(void *b, size_t n) {
87 SecureZeroMemory(b, n);
91 char *get_username(void)
95 int got_username = FALSE;
96 DECL_WINDOWS_FUNCTION(static, BOOLEAN, GetUserNameExA,
97 (EXTENDED_NAME_FORMAT, LPSTR, PULONG));
100 static int tried_usernameex = FALSE;
101 if (!tried_usernameex) {
102 /* Not available on Win9x, so load dynamically */
103 HMODULE secur32 = load_system32_dll("secur32.dll");
104 GET_WINDOWS_FUNCTION(secur32, GetUserNameExA);
105 tried_usernameex = TRUE;
109 if (p_GetUserNameExA) {
111 * If available, use the principal -- this avoids the problem
112 * that the local username is case-insensitive but Kerberos
113 * usernames are case-sensitive.
118 (void) p_GetUserNameExA(NameUserPrincipal, NULL, &namelen);
120 user = snewn(namelen, char);
121 got_username = p_GetUserNameExA(NameUserPrincipal, user, &namelen);
123 char *p = strchr(user, '@');
131 /* Fall back to local user name */
133 if (GetUserName(NULL, &namelen) == FALSE) {
135 * Apparently this doesn't work at least on Windows XP SP2.
136 * Thus assume a maximum of 256. It will fail again if it
142 user = snewn(namelen, char);
143 got_username = GetUserName(user, &namelen);
149 return got_username ? user : NULL;
152 BOOL init_winver(void)
154 ZeroMemory(&osVersion, sizeof(osVersion));
155 osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
156 return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
159 HMODULE load_system32_dll(const char *libname)
162 * Wrapper function to load a DLL out of c:\windows\system32
163 * without going through the full DLL search path. (Hence no
164 * attack is possible by placing a substitute DLL earlier on that
167 static char *sysdir = NULL;
174 size = 3*size/2 + 512;
175 sysdir = sresize(sysdir, size, char);
176 len = GetSystemDirectory(sysdir, size);
177 } while (len >= size);
180 fullpath = dupcat(sysdir, "\\", libname, NULL);
181 ret = LoadLibrary(fullpath);
187 * A tree234 containing mappings from system error codes to strings.
195 static int errstring_find(void *av, void *bv)
198 struct errstring *b = (struct errstring *)bv;
205 static int errstring_compare(void *av, void *bv)
207 struct errstring *a = (struct errstring *)av;
208 return errstring_find(&a->error, bv);
211 static tree234 *errstrings = NULL;
213 const char *win_strerror(int error)
215 struct errstring *es;
218 errstrings = newtree234(errstring_compare);
220 es = find234(errstrings, &error, errstring_find);
223 char msgtext[65536]; /* maximum size for FormatMessage is 64K */
225 es = snew(struct errstring);
227 if (!FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM |
228 FORMAT_MESSAGE_IGNORE_INSERTS), NULL, error,
229 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
230 msgtext, lenof(msgtext)-1, NULL)) {
232 "(unable to format: FormatMessage returned %u)",
233 (unsigned int)GetLastError());
235 int len = strlen(msgtext);
236 if (len > 0 && msgtext[len-1] == '\n')
237 msgtext[len-1] = '\0';
239 es->text = dupprintf("Error %d: %s", error, msgtext);
240 add234(errstrings, es);
247 static FILE *debug_fp = NULL;
248 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
249 static int debug_got_console = 0;
251 void dputs(const char *buf)
255 if (!debug_got_console) {
256 if (AllocConsole()) {
257 debug_got_console = 1;
258 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
262 debug_fp = fopen("debug.log", "w");
265 if (debug_hdl != INVALID_HANDLE_VALUE) {
266 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
268 fputs(buf, debug_fp);
275 * Minefield - a Windows equivalent for Electric Fence
278 #define PAGESIZE 4096
283 * We start by reserving as much virtual address space as Windows
284 * will sensibly (or not sensibly) let us have. We flag it all as
287 * Any allocation attempt is satisfied by committing one or more
288 * pages, with an uncommitted page on either side. The returned
289 * memory region is jammed up against the _end_ of the pages.
291 * Freeing anything causes instantaneous decommitment of the pages
292 * involved, so stale pointers are caught as soon as possible.
295 static int minefield_initialised = 0;
296 static void *minefield_region = NULL;
297 static long minefield_size = 0;
298 static long minefield_npages = 0;
299 static long minefield_curpos = 0;
300 static unsigned short *minefield_admin = NULL;
301 static void *minefield_pages = NULL;
303 static void minefield_admin_hide(int hide)
305 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
306 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
309 static void minefield_init(void)
315 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
316 minefield_region = VirtualAlloc(NULL, size,
317 MEM_RESERVE, PAGE_NOACCESS);
318 if (minefield_region)
321 minefield_size = size;
324 * Firstly, allocate a section of that to be the admin block.
325 * We'll need a two-byte field for each page.
327 minefield_admin = minefield_region;
328 minefield_npages = minefield_size / PAGESIZE;
329 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
330 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
331 minefield_pages = (char *) minefield_region + admin_size;
334 * Commit the admin region.
336 VirtualAlloc(minefield_admin, minefield_npages * 2,
337 MEM_COMMIT, PAGE_READWRITE);
340 * Mark all pages as unused (0xFFFF).
342 for (i = 0; i < minefield_npages; i++)
343 minefield_admin[i] = 0xFFFF;
346 * Hide the admin region.
348 minefield_admin_hide(1);
350 minefield_initialised = 1;
353 static void minefield_bomb(void)
355 div(1, *(int *) minefield_pages);
358 static void *minefield_alloc(int size)
361 int pos, lim, region_end, region_start;
365 npages = (size + PAGESIZE - 1) / PAGESIZE;
367 minefield_admin_hide(0);
370 * Search from current position until we find a contiguous
371 * bunch of npages+2 unused pages.
373 pos = minefield_curpos;
374 lim = minefield_npages;
376 /* Skip over used pages. */
377 while (pos < lim && minefield_admin[pos] != 0xFFFF)
379 /* Count unused pages. */
381 while (pos < lim && pos - start < npages + 2 &&
382 minefield_admin[pos] == 0xFFFF)
384 if (pos - start == npages + 2)
386 /* If we've reached the limit, reset the limit or stop. */
388 if (lim == minefield_npages) {
389 /* go round and start again at zero */
390 lim = minefield_curpos;
393 minefield_admin_hide(1);
399 minefield_curpos = pos - 1;
402 * We have npages+2 unused pages starting at start. We leave
403 * the first and last of these alone and use the rest.
405 region_end = (start + npages + 1) * PAGESIZE;
406 region_start = region_end - size;
407 /* FIXME: could align here if we wanted */
410 * Update the admin region.
412 for (i = start + 2; i < start + npages + 1; i++)
413 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
414 minefield_admin[start + 1] = region_start % PAGESIZE;
416 minefield_admin_hide(1);
418 VirtualAlloc((char *) minefield_pages + region_start, size,
419 MEM_COMMIT, PAGE_READWRITE);
420 return (char *) minefield_pages + region_start;
423 static void minefield_free(void *ptr)
425 int region_start, i, j;
427 minefield_admin_hide(0);
429 region_start = (char *) ptr - (char *) minefield_pages;
430 i = region_start / PAGESIZE;
431 if (i < 0 || i >= minefield_npages ||
432 minefield_admin[i] != region_start % PAGESIZE)
434 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
435 minefield_admin[j] = 0xFFFF;
438 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
440 minefield_admin_hide(1);
443 static int minefield_get_size(void *ptr)
445 int region_start, i, j;
447 minefield_admin_hide(0);
449 region_start = (char *) ptr - (char *) minefield_pages;
450 i = region_start / PAGESIZE;
451 if (i < 0 || i >= minefield_npages ||
452 minefield_admin[i] != region_start % PAGESIZE)
454 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
456 minefield_admin_hide(1);
458 return j * PAGESIZE - region_start;
461 void *minefield_c_malloc(size_t size)
463 if (!minefield_initialised)
465 return minefield_alloc(size);
468 void minefield_c_free(void *p)
470 if (!minefield_initialised)
476 * realloc _always_ moves the chunk, for rapid detection of code
477 * that assumes it won't.
479 void *minefield_c_realloc(void *p, size_t size)
483 if (!minefield_initialised)
485 q = minefield_alloc(size);
486 oldsize = minefield_get_size(p);
487 memcpy(q, p, (oldsize < size ? oldsize : size));
492 #endif /* MINEFIELD */
494 FontSpec *fontspec_new(const char *name,
495 int bold, int height, int charset)
497 FontSpec *f = snew(FontSpec);
498 f->name = dupstr(name);
501 f->charset = charset;
504 FontSpec *fontspec_copy(const FontSpec *f)
506 return fontspec_new(f->name, f->isbold, f->height, f->charset);
508 void fontspec_free(FontSpec *f)
513 int fontspec_serialise(FontSpec *f, void *vdata)
515 char *data = (char *)vdata;
516 int len = strlen(f->name) + 1; /* include trailing NUL */
518 strcpy(data, f->name);
519 PUT_32BIT_MSB_FIRST(data + len, f->isbold);
520 PUT_32BIT_MSB_FIRST(data + len + 4, f->height);
521 PUT_32BIT_MSB_FIRST(data + len + 8, f->charset);
523 return len + 12; /* also include three 4-byte ints */
525 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
527 char *data = (char *)vdata;
531 end = memchr(data, '\0', maxsize-12);
535 *used = end - data + 12;
536 return fontspec_new(data,
537 GET_32BIT_MSB_FIRST(end),
538 GET_32BIT_MSB_FIRST(end + 4),
539 GET_32BIT_MSB_FIRST(end + 8));