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 #ifndef NO_SECUREZEROMEMORY
73 * Windows implementation of smemclr (see misc.c) using SecureZeroMemory.
75 void smemclr(void *b, size_t n) {
77 SecureZeroMemory(b, n);
81 char *get_username(void)
85 int got_username = FALSE;
86 DECL_WINDOWS_FUNCTION(static, BOOLEAN, GetUserNameExA,
87 (EXTENDED_NAME_FORMAT, LPSTR, PULONG));
90 static int tried_usernameex = FALSE;
91 if (!tried_usernameex) {
92 /* Not available on Win9x, so load dynamically */
93 HMODULE secur32 = load_system32_dll("secur32.dll");
94 GET_WINDOWS_FUNCTION(secur32, GetUserNameExA);
95 tried_usernameex = TRUE;
99 if (p_GetUserNameExA) {
101 * If available, use the principal -- this avoids the problem
102 * that the local username is case-insensitive but Kerberos
103 * usernames are case-sensitive.
108 (void) p_GetUserNameExA(NameUserPrincipal, NULL, &namelen);
110 user = snewn(namelen, char);
111 got_username = p_GetUserNameExA(NameUserPrincipal, user, &namelen);
113 char *p = strchr(user, '@');
121 /* Fall back to local user name */
123 if (GetUserName(NULL, &namelen) == FALSE) {
125 * Apparently this doesn't work at least on Windows XP SP2.
126 * Thus assume a maximum of 256. It will fail again if it
132 user = snewn(namelen, char);
133 got_username = GetUserName(user, &namelen);
139 return got_username ? user : NULL;
142 BOOL init_winver(void)
144 ZeroMemory(&osVersion, sizeof(osVersion));
145 osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
146 return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
149 HMODULE load_system32_dll(const char *libname)
152 * Wrapper function to load a DLL out of c:\windows\system32
153 * without going through the full DLL search path. (Hence no
154 * attack is possible by placing a substitute DLL earlier on that
157 static char *sysdir = NULL;
164 size = 3*size/2 + 512;
165 sysdir = sresize(sysdir, size, char);
166 len = GetSystemDirectory(sysdir, size);
167 } while (len >= size);
170 fullpath = dupcat(sysdir, "\\", libname, NULL);
171 ret = LoadLibrary(fullpath);
177 * A tree234 containing mappings from system error codes to strings.
185 static int errstring_find(void *av, void *bv)
188 struct errstring *b = (struct errstring *)bv;
195 static int errstring_compare(void *av, void *bv)
197 struct errstring *a = (struct errstring *)av;
198 return errstring_find(&a->error, bv);
201 static tree234 *errstrings = NULL;
203 const char *win_strerror(int error)
205 struct errstring *es;
208 errstrings = newtree234(errstring_compare);
210 es = find234(errstrings, &error, errstring_find);
213 char msgtext[65536]; /* maximum size for FormatMessage is 64K */
215 es = snew(struct errstring);
217 if (!FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM |
218 FORMAT_MESSAGE_IGNORE_INSERTS), NULL, error,
219 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
220 msgtext, lenof(msgtext)-1, NULL)) {
222 "(unable to format: FormatMessage returned %d)",
223 error, GetLastError());
225 int len = strlen(msgtext);
226 if (len > 0 && msgtext[len-1] == '\n')
227 msgtext[len-1] = '\0';
229 es->text = dupprintf("Error %d: %s", error, msgtext);
230 add234(errstrings, es);
237 static FILE *debug_fp = NULL;
238 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
239 static int debug_got_console = 0;
241 void dputs(char *buf)
245 if (!debug_got_console) {
246 if (AllocConsole()) {
247 debug_got_console = 1;
248 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
252 debug_fp = fopen("debug.log", "w");
255 if (debug_hdl != INVALID_HANDLE_VALUE) {
256 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
258 fputs(buf, debug_fp);
265 * Minefield - a Windows equivalent for Electric Fence
268 #define PAGESIZE 4096
273 * We start by reserving as much virtual address space as Windows
274 * will sensibly (or not sensibly) let us have. We flag it all as
277 * Any allocation attempt is satisfied by committing one or more
278 * pages, with an uncommitted page on either side. The returned
279 * memory region is jammed up against the _end_ of the pages.
281 * Freeing anything causes instantaneous decommitment of the pages
282 * involved, so stale pointers are caught as soon as possible.
285 static int minefield_initialised = 0;
286 static void *minefield_region = NULL;
287 static long minefield_size = 0;
288 static long minefield_npages = 0;
289 static long minefield_curpos = 0;
290 static unsigned short *minefield_admin = NULL;
291 static void *minefield_pages = NULL;
293 static void minefield_admin_hide(int hide)
295 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
296 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
299 static void minefield_init(void)
305 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
306 minefield_region = VirtualAlloc(NULL, size,
307 MEM_RESERVE, PAGE_NOACCESS);
308 if (minefield_region)
311 minefield_size = size;
314 * Firstly, allocate a section of that to be the admin block.
315 * We'll need a two-byte field for each page.
317 minefield_admin = minefield_region;
318 minefield_npages = minefield_size / PAGESIZE;
319 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
320 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
321 minefield_pages = (char *) minefield_region + admin_size;
324 * Commit the admin region.
326 VirtualAlloc(minefield_admin, minefield_npages * 2,
327 MEM_COMMIT, PAGE_READWRITE);
330 * Mark all pages as unused (0xFFFF).
332 for (i = 0; i < minefield_npages; i++)
333 minefield_admin[i] = 0xFFFF;
336 * Hide the admin region.
338 minefield_admin_hide(1);
340 minefield_initialised = 1;
343 static void minefield_bomb(void)
345 div(1, *(int *) minefield_pages);
348 static void *minefield_alloc(int size)
351 int pos, lim, region_end, region_start;
355 npages = (size + PAGESIZE - 1) / PAGESIZE;
357 minefield_admin_hide(0);
360 * Search from current position until we find a contiguous
361 * bunch of npages+2 unused pages.
363 pos = minefield_curpos;
364 lim = minefield_npages;
366 /* Skip over used pages. */
367 while (pos < lim && minefield_admin[pos] != 0xFFFF)
369 /* Count unused pages. */
371 while (pos < lim && pos - start < npages + 2 &&
372 minefield_admin[pos] == 0xFFFF)
374 if (pos - start == npages + 2)
376 /* If we've reached the limit, reset the limit or stop. */
378 if (lim == minefield_npages) {
379 /* go round and start again at zero */
380 lim = minefield_curpos;
383 minefield_admin_hide(1);
389 minefield_curpos = pos - 1;
392 * We have npages+2 unused pages starting at start. We leave
393 * the first and last of these alone and use the rest.
395 region_end = (start + npages + 1) * PAGESIZE;
396 region_start = region_end - size;
397 /* FIXME: could align here if we wanted */
400 * Update the admin region.
402 for (i = start + 2; i < start + npages + 1; i++)
403 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
404 minefield_admin[start + 1] = region_start % PAGESIZE;
406 minefield_admin_hide(1);
408 VirtualAlloc((char *) minefield_pages + region_start, size,
409 MEM_COMMIT, PAGE_READWRITE);
410 return (char *) minefield_pages + region_start;
413 static void minefield_free(void *ptr)
415 int region_start, i, j;
417 minefield_admin_hide(0);
419 region_start = (char *) ptr - (char *) minefield_pages;
420 i = region_start / PAGESIZE;
421 if (i < 0 || i >= minefield_npages ||
422 minefield_admin[i] != region_start % PAGESIZE)
424 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
425 minefield_admin[j] = 0xFFFF;
428 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
430 minefield_admin_hide(1);
433 static int minefield_get_size(void *ptr)
435 int region_start, i, j;
437 minefield_admin_hide(0);
439 region_start = (char *) ptr - (char *) minefield_pages;
440 i = region_start / PAGESIZE;
441 if (i < 0 || i >= minefield_npages ||
442 minefield_admin[i] != region_start % PAGESIZE)
444 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
446 minefield_admin_hide(1);
448 return j * PAGESIZE - region_start;
451 void *minefield_c_malloc(size_t size)
453 if (!minefield_initialised)
455 return minefield_alloc(size);
458 void minefield_c_free(void *p)
460 if (!minefield_initialised)
466 * realloc _always_ moves the chunk, for rapid detection of code
467 * that assumes it won't.
469 void *minefield_c_realloc(void *p, size_t size)
473 if (!minefield_initialised)
475 q = minefield_alloc(size);
476 oldsize = minefield_get_size(p);
477 memcpy(q, p, (oldsize < size ? oldsize : size));
482 #endif /* MINEFIELD */
484 FontSpec *fontspec_new(const char *name,
485 int bold, int height, int charset)
487 FontSpec *f = snew(FontSpec);
488 f->name = dupstr(name);
491 f->charset = charset;
494 FontSpec *fontspec_copy(const FontSpec *f)
496 return fontspec_new(f->name, f->isbold, f->height, f->charset);
498 void fontspec_free(FontSpec *f)
503 int fontspec_serialise(FontSpec *f, void *vdata)
505 char *data = (char *)vdata;
506 int len = strlen(f->name) + 1; /* include trailing NUL */
508 strcpy(data, f->name);
509 PUT_32BIT_MSB_FIRST(data + len, f->isbold);
510 PUT_32BIT_MSB_FIRST(data + len + 4, f->height);
511 PUT_32BIT_MSB_FIRST(data + len + 8, f->charset);
513 return len + 12; /* also include three 4-byte ints */
515 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
517 char *data = (char *)vdata;
521 end = memchr(data, '\0', maxsize-12);
525 *used = end - data + 12;
526 return fontspec_new(data,
527 GET_32BIT_MSB_FIRST(end),
528 GET_32BIT_MSB_FIRST(end + 4),
529 GET_32BIT_MSB_FIRST(end + 8));