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)
20 strncpy(ret.path, str, sizeof(ret.path));
21 ret.path[sizeof(ret.path)-1] = '\0';
25 const char *filename_to_str(const Filename *fn)
30 int filename_equal(Filename f1, Filename f2)
32 return !strcmp(f1.path, f2.path);
35 int filename_is_null(Filename fn)
40 char *get_username(void)
44 int got_username = FALSE;
45 DECL_WINDOWS_FUNCTION(static, BOOLEAN, GetUserNameExA,
46 (EXTENDED_NAME_FORMAT, LPSTR, PULONG));
49 static int tried_usernameex = FALSE;
50 if (!tried_usernameex) {
51 /* Not available on Win9x, so load dynamically */
52 HMODULE secur32 = LoadLibrary("SECUR32.DLL");
53 GET_WINDOWS_FUNCTION(secur32, GetUserNameExA);
54 tried_usernameex = TRUE;
58 if (p_GetUserNameExA) {
60 * If available, use the principal -- this avoids the problem
61 * that the local username is case-insensitive but Kerberos
62 * usernames are case-sensitive.
67 (void) p_GetUserNameExA(NameUserPrincipal, NULL, &namelen);
69 user = snewn(namelen, char);
70 got_username = p_GetUserNameExA(NameUserPrincipal, user, &namelen);
72 char *p = strchr(user, '@');
80 /* Fall back to local user name */
82 if (GetUserName(NULL, &namelen) == FALSE) {
84 * Apparently this doesn't work at least on Windows XP SP2.
85 * Thus assume a maximum of 256. It will fail again if it
91 user = snewn(namelen, char);
92 got_username = GetUserName(user, &namelen);
98 return got_username ? user : NULL;
101 BOOL init_winver(void)
103 ZeroMemory(&osVersion, sizeof(osVersion));
104 osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
105 return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
109 static FILE *debug_fp = NULL;
110 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
111 static int debug_got_console = 0;
113 void dputs(char *buf)
117 if (!debug_got_console) {
118 if (AllocConsole()) {
119 debug_got_console = 1;
120 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
124 debug_fp = fopen("debug.log", "w");
127 if (debug_hdl != INVALID_HANDLE_VALUE) {
128 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
130 fputs(buf, debug_fp);
137 * Minefield - a Windows equivalent for Electric Fence
140 #define PAGESIZE 4096
145 * We start by reserving as much virtual address space as Windows
146 * will sensibly (or not sensibly) let us have. We flag it all as
149 * Any allocation attempt is satisfied by committing one or more
150 * pages, with an uncommitted page on either side. The returned
151 * memory region is jammed up against the _end_ of the pages.
153 * Freeing anything causes instantaneous decommitment of the pages
154 * involved, so stale pointers are caught as soon as possible.
157 static int minefield_initialised = 0;
158 static void *minefield_region = NULL;
159 static long minefield_size = 0;
160 static long minefield_npages = 0;
161 static long minefield_curpos = 0;
162 static unsigned short *minefield_admin = NULL;
163 static void *minefield_pages = NULL;
165 static void minefield_admin_hide(int hide)
167 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
168 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
171 static void minefield_init(void)
177 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
178 minefield_region = VirtualAlloc(NULL, size,
179 MEM_RESERVE, PAGE_NOACCESS);
180 if (minefield_region)
183 minefield_size = size;
186 * Firstly, allocate a section of that to be the admin block.
187 * We'll need a two-byte field for each page.
189 minefield_admin = minefield_region;
190 minefield_npages = minefield_size / PAGESIZE;
191 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
192 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
193 minefield_pages = (char *) minefield_region + admin_size;
196 * Commit the admin region.
198 VirtualAlloc(minefield_admin, minefield_npages * 2,
199 MEM_COMMIT, PAGE_READWRITE);
202 * Mark all pages as unused (0xFFFF).
204 for (i = 0; i < minefield_npages; i++)
205 minefield_admin[i] = 0xFFFF;
208 * Hide the admin region.
210 minefield_admin_hide(1);
212 minefield_initialised = 1;
215 static void minefield_bomb(void)
217 div(1, *(int *) minefield_pages);
220 static void *minefield_alloc(int size)
223 int pos, lim, region_end, region_start;
227 npages = (size + PAGESIZE - 1) / PAGESIZE;
229 minefield_admin_hide(0);
232 * Search from current position until we find a contiguous
233 * bunch of npages+2 unused pages.
235 pos = minefield_curpos;
236 lim = minefield_npages;
238 /* Skip over used pages. */
239 while (pos < lim && minefield_admin[pos] != 0xFFFF)
241 /* Count unused pages. */
243 while (pos < lim && pos - start < npages + 2 &&
244 minefield_admin[pos] == 0xFFFF)
246 if (pos - start == npages + 2)
248 /* If we've reached the limit, reset the limit or stop. */
250 if (lim == minefield_npages) {
251 /* go round and start again at zero */
252 lim = minefield_curpos;
255 minefield_admin_hide(1);
261 minefield_curpos = pos - 1;
264 * We have npages+2 unused pages starting at start. We leave
265 * the first and last of these alone and use the rest.
267 region_end = (start + npages + 1) * PAGESIZE;
268 region_start = region_end - size;
269 /* FIXME: could align here if we wanted */
272 * Update the admin region.
274 for (i = start + 2; i < start + npages + 1; i++)
275 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
276 minefield_admin[start + 1] = region_start % PAGESIZE;
278 minefield_admin_hide(1);
280 VirtualAlloc((char *) minefield_pages + region_start, size,
281 MEM_COMMIT, PAGE_READWRITE);
282 return (char *) minefield_pages + region_start;
285 static void minefield_free(void *ptr)
287 int region_start, i, j;
289 minefield_admin_hide(0);
291 region_start = (char *) ptr - (char *) minefield_pages;
292 i = region_start / PAGESIZE;
293 if (i < 0 || i >= minefield_npages ||
294 minefield_admin[i] != region_start % PAGESIZE)
296 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
297 minefield_admin[j] = 0xFFFF;
300 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
302 minefield_admin_hide(1);
305 static int minefield_get_size(void *ptr)
307 int region_start, i, j;
309 minefield_admin_hide(0);
311 region_start = (char *) ptr - (char *) minefield_pages;
312 i = region_start / PAGESIZE;
313 if (i < 0 || i >= minefield_npages ||
314 minefield_admin[i] != region_start % PAGESIZE)
316 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
318 minefield_admin_hide(1);
320 return j * PAGESIZE - region_start;
323 void *minefield_c_malloc(size_t size)
325 if (!minefield_initialised)
327 return minefield_alloc(size);
330 void minefield_c_free(void *p)
332 if (!minefield_initialised)
338 * realloc _always_ moves the chunk, for rapid detection of code
339 * that assumes it won't.
341 void *minefield_c_realloc(void *p, size_t size)
345 if (!minefield_initialised)
347 q = minefield_alloc(size);
348 oldsize = minefield_get_size(p);
349 memcpy(q, p, (oldsize < size ? oldsize : size));
354 #endif /* MINEFIELD */