2 * winmisc.c: miscellaneous Windows-specific things
11 OSVERSIONINFO osVersion;
13 char *platform_get_x_display(void) {
14 /* We may as well check for DISPLAY in case it's useful. */
15 return dupstr(getenv("DISPLAY"));
18 Filename *filename_from_str(const char *str)
20 Filename *ret = snew(Filename);
21 ret->path = dupstr(str);
25 Filename *filename_copy(const Filename *fn)
27 return filename_from_str(fn->path);
30 const char *filename_to_str(const Filename *fn)
35 int filename_equal(const Filename *f1, const Filename *f2)
37 return !strcmp(f1->path, f2->path);
40 int filename_is_null(const Filename *fn)
45 void filename_free(Filename *fn)
51 int filename_serialise(const Filename *f, void *vdata)
53 char *data = (char *)vdata;
54 int len = strlen(f->path) + 1; /* include trailing NUL */
56 strcpy(data, f->path);
60 Filename *filename_deserialise(void *vdata, int maxsize, int *used)
62 char *data = (char *)vdata;
64 end = memchr(data, '\0', maxsize);
69 return filename_from_str(data);
72 char filename_char_sanitise(char c)
74 if (strchr("<>:\"/\\|?*", c))
79 #ifndef NO_SECUREZEROMEMORY
81 * Windows implementation of smemclr (see misc.c) using SecureZeroMemory.
83 void smemclr(void *b, size_t n) {
85 SecureZeroMemory(b, n);
89 char *get_username(void)
93 int got_username = FALSE;
94 DECL_WINDOWS_FUNCTION(static, BOOLEAN, GetUserNameExA,
95 (EXTENDED_NAME_FORMAT, LPSTR, PULONG));
98 static int tried_usernameex = FALSE;
99 if (!tried_usernameex) {
100 /* Not available on Win9x, so load dynamically */
101 HMODULE secur32 = load_system32_dll("secur32.dll");
102 GET_WINDOWS_FUNCTION(secur32, GetUserNameExA);
103 tried_usernameex = TRUE;
107 if (p_GetUserNameExA) {
109 * If available, use the principal -- this avoids the problem
110 * that the local username is case-insensitive but Kerberos
111 * usernames are case-sensitive.
116 (void) p_GetUserNameExA(NameUserPrincipal, NULL, &namelen);
118 user = snewn(namelen, char);
119 got_username = p_GetUserNameExA(NameUserPrincipal, user, &namelen);
121 char *p = strchr(user, '@');
129 /* Fall back to local user name */
131 if (GetUserName(NULL, &namelen) == FALSE) {
133 * Apparently this doesn't work at least on Windows XP SP2.
134 * Thus assume a maximum of 256. It will fail again if it
140 user = snewn(namelen, char);
141 got_username = GetUserName(user, &namelen);
147 return got_username ? user : NULL;
150 BOOL init_winver(void)
152 ZeroMemory(&osVersion, sizeof(osVersion));
153 osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
154 return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
157 HMODULE load_system32_dll(const char *libname)
160 * Wrapper function to load a DLL out of c:\windows\system32
161 * without going through the full DLL search path. (Hence no
162 * attack is possible by placing a substitute DLL earlier on that
165 static char *sysdir = NULL;
172 size = 3*size/2 + 512;
173 sysdir = sresize(sysdir, size, char);
174 len = GetSystemDirectory(sysdir, size);
175 } while (len >= size);
178 fullpath = dupcat(sysdir, "\\", libname, NULL);
179 ret = LoadLibrary(fullpath);
185 * A tree234 containing mappings from system error codes to strings.
193 static int errstring_find(void *av, void *bv)
196 struct errstring *b = (struct errstring *)bv;
203 static int errstring_compare(void *av, void *bv)
205 struct errstring *a = (struct errstring *)av;
206 return errstring_find(&a->error, bv);
209 static tree234 *errstrings = NULL;
211 const char *win_strerror(int error)
213 struct errstring *es;
216 errstrings = newtree234(errstring_compare);
218 es = find234(errstrings, &error, errstring_find);
221 char msgtext[65536]; /* maximum size for FormatMessage is 64K */
223 es = snew(struct errstring);
225 if (!FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM |
226 FORMAT_MESSAGE_IGNORE_INSERTS), NULL, error,
227 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
228 msgtext, lenof(msgtext)-1, NULL)) {
230 "(unable to format: FormatMessage returned %d)",
233 int len = strlen(msgtext);
234 if (len > 0 && msgtext[len-1] == '\n')
235 msgtext[len-1] = '\0';
237 es->text = dupprintf("Error %d: %s", error, msgtext);
238 add234(errstrings, es);
245 static FILE *debug_fp = NULL;
246 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
247 static int debug_got_console = 0;
249 void dputs(char *buf)
253 if (!debug_got_console) {
254 if (AllocConsole()) {
255 debug_got_console = 1;
256 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
260 debug_fp = fopen("debug.log", "w");
263 if (debug_hdl != INVALID_HANDLE_VALUE) {
264 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
266 fputs(buf, debug_fp);
273 * Minefield - a Windows equivalent for Electric Fence
276 #define PAGESIZE 4096
281 * We start by reserving as much virtual address space as Windows
282 * will sensibly (or not sensibly) let us have. We flag it all as
285 * Any allocation attempt is satisfied by committing one or more
286 * pages, with an uncommitted page on either side. The returned
287 * memory region is jammed up against the _end_ of the pages.
289 * Freeing anything causes instantaneous decommitment of the pages
290 * involved, so stale pointers are caught as soon as possible.
293 static int minefield_initialised = 0;
294 static void *minefield_region = NULL;
295 static long minefield_size = 0;
296 static long minefield_npages = 0;
297 static long minefield_curpos = 0;
298 static unsigned short *minefield_admin = NULL;
299 static void *minefield_pages = NULL;
301 static void minefield_admin_hide(int hide)
303 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
304 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
307 static void minefield_init(void)
313 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
314 minefield_region = VirtualAlloc(NULL, size,
315 MEM_RESERVE, PAGE_NOACCESS);
316 if (minefield_region)
319 minefield_size = size;
322 * Firstly, allocate a section of that to be the admin block.
323 * We'll need a two-byte field for each page.
325 minefield_admin = minefield_region;
326 minefield_npages = minefield_size / PAGESIZE;
327 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
328 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
329 minefield_pages = (char *) minefield_region + admin_size;
332 * Commit the admin region.
334 VirtualAlloc(minefield_admin, minefield_npages * 2,
335 MEM_COMMIT, PAGE_READWRITE);
338 * Mark all pages as unused (0xFFFF).
340 for (i = 0; i < minefield_npages; i++)
341 minefield_admin[i] = 0xFFFF;
344 * Hide the admin region.
346 minefield_admin_hide(1);
348 minefield_initialised = 1;
351 static void minefield_bomb(void)
353 div(1, *(int *) minefield_pages);
356 static void *minefield_alloc(int size)
359 int pos, lim, region_end, region_start;
363 npages = (size + PAGESIZE - 1) / PAGESIZE;
365 minefield_admin_hide(0);
368 * Search from current position until we find a contiguous
369 * bunch of npages+2 unused pages.
371 pos = minefield_curpos;
372 lim = minefield_npages;
374 /* Skip over used pages. */
375 while (pos < lim && minefield_admin[pos] != 0xFFFF)
377 /* Count unused pages. */
379 while (pos < lim && pos - start < npages + 2 &&
380 minefield_admin[pos] == 0xFFFF)
382 if (pos - start == npages + 2)
384 /* If we've reached the limit, reset the limit or stop. */
386 if (lim == minefield_npages) {
387 /* go round and start again at zero */
388 lim = minefield_curpos;
391 minefield_admin_hide(1);
397 minefield_curpos = pos - 1;
400 * We have npages+2 unused pages starting at start. We leave
401 * the first and last of these alone and use the rest.
403 region_end = (start + npages + 1) * PAGESIZE;
404 region_start = region_end - size;
405 /* FIXME: could align here if we wanted */
408 * Update the admin region.
410 for (i = start + 2; i < start + npages + 1; i++)
411 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
412 minefield_admin[start + 1] = region_start % PAGESIZE;
414 minefield_admin_hide(1);
416 VirtualAlloc((char *) minefield_pages + region_start, size,
417 MEM_COMMIT, PAGE_READWRITE);
418 return (char *) minefield_pages + region_start;
421 static void minefield_free(void *ptr)
423 int region_start, i, j;
425 minefield_admin_hide(0);
427 region_start = (char *) ptr - (char *) minefield_pages;
428 i = region_start / PAGESIZE;
429 if (i < 0 || i >= minefield_npages ||
430 minefield_admin[i] != region_start % PAGESIZE)
432 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
433 minefield_admin[j] = 0xFFFF;
436 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
438 minefield_admin_hide(1);
441 static int minefield_get_size(void *ptr)
443 int region_start, i, j;
445 minefield_admin_hide(0);
447 region_start = (char *) ptr - (char *) minefield_pages;
448 i = region_start / PAGESIZE;
449 if (i < 0 || i >= minefield_npages ||
450 minefield_admin[i] != region_start % PAGESIZE)
452 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
454 minefield_admin_hide(1);
456 return j * PAGESIZE - region_start;
459 void *minefield_c_malloc(size_t size)
461 if (!minefield_initialised)
463 return minefield_alloc(size);
466 void minefield_c_free(void *p)
468 if (!minefield_initialised)
474 * realloc _always_ moves the chunk, for rapid detection of code
475 * that assumes it won't.
477 void *minefield_c_realloc(void *p, size_t size)
481 if (!minefield_initialised)
483 q = minefield_alloc(size);
484 oldsize = minefield_get_size(p);
485 memcpy(q, p, (oldsize < size ? oldsize : size));
490 #endif /* MINEFIELD */
492 FontSpec *fontspec_new(const char *name,
493 int bold, int height, int charset)
495 FontSpec *f = snew(FontSpec);
496 f->name = dupstr(name);
499 f->charset = charset;
502 FontSpec *fontspec_copy(const FontSpec *f)
504 return fontspec_new(f->name, f->isbold, f->height, f->charset);
506 void fontspec_free(FontSpec *f)
511 int fontspec_serialise(FontSpec *f, void *vdata)
513 char *data = (char *)vdata;
514 int len = strlen(f->name) + 1; /* include trailing NUL */
516 strcpy(data, f->name);
517 PUT_32BIT_MSB_FIRST(data + len, f->isbold);
518 PUT_32BIT_MSB_FIRST(data + len + 4, f->height);
519 PUT_32BIT_MSB_FIRST(data + len + 8, f->charset);
521 return len + 12; /* also include three 4-byte ints */
523 FontSpec *fontspec_deserialise(void *vdata, int maxsize, int *used)
525 char *data = (char *)vdata;
529 end = memchr(data, '\0', maxsize-12);
533 *used = end - data + 12;
534 return fontspec_new(data,
535 GET_32BIT_MSB_FIRST(end),
536 GET_32BIT_MSB_FIRST(end + 4),
537 GET_32BIT_MSB_FIRST(end + 8));