2 * winmisc.c: miscellaneous Windows-specific things
9 OSVERSIONINFO osVersion;
11 void platform_get_x11_auth(char *display, int *proto,
12 unsigned char *data, int *datalen)
14 /* We don't support this at all under Windows. */
17 const char platform_x11_best_transport[] = "localhost";
19 char *platform_get_x_display(void) {
20 /* We may as well check for DISPLAY in case it's useful. */
21 return dupstr(getenv("DISPLAY"));
24 Filename filename_from_str(const char *str)
27 strncpy(ret.path, str, sizeof(ret.path));
28 ret.path[sizeof(ret.path)-1] = '\0';
32 const char *filename_to_str(const Filename *fn)
37 int filename_equal(Filename f1, Filename f2)
39 return !strcmp(f1.path, f2.path);
42 int filename_is_null(Filename fn)
47 char *get_username(void)
53 if (GetUserName(NULL, &namelen) == FALSE)
56 user = snewn(namelen, char);
57 GetUserName(user, &namelen);
62 int SaneDialogBox(HINSTANCE hinst,
74 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
75 wc.lpfnWndProc = DefDlgProc;
77 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
80 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
81 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
82 wc.lpszMenuName = NULL;
83 wc.lpszClassName = "PuTTYConfigBox";
86 hwnd = CreateDialog(hinst, tmpl, hwndparent, lpDialogFunc);
88 SetWindowLong(hwnd, BOXFLAGS, 0); /* flags */
89 SetWindowLong(hwnd, BOXRESULT, 0); /* result from SaneEndDialog */
91 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
92 flags=GetWindowLong(hwnd, BOXFLAGS);
93 if (!(flags & DF_END) && !IsDialogMessage(hwnd, &msg))
94 DispatchMessage(&msg);
100 PostQuitMessage(msg.wParam); /* We got a WM_QUIT, pass it on */
102 ret=GetWindowLong(hwnd, BOXRESULT);
107 void SaneEndDialog(HWND hwnd, int ret)
109 SetWindowLong(hwnd, BOXRESULT, ret);
110 SetWindowLong(hwnd, BOXFLAGS, DF_END);
113 BOOL init_winver(void)
115 ZeroMemory(&osVersion, sizeof(osVersion));
116 osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
117 return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
121 static FILE *debug_fp = NULL;
122 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
123 static int debug_got_console = 0;
125 void dputs(char *buf)
129 if (!debug_got_console) {
130 if (AllocConsole()) {
131 debug_got_console = 1;
132 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
136 debug_fp = fopen("debug.log", "w");
139 if (debug_hdl != INVALID_HANDLE_VALUE) {
140 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
142 fputs(buf, debug_fp);
149 * Minefield - a Windows equivalent for Electric Fence
152 #define PAGESIZE 4096
157 * We start by reserving as much virtual address space as Windows
158 * will sensibly (or not sensibly) let us have. We flag it all as
161 * Any allocation attempt is satisfied by committing one or more
162 * pages, with an uncommitted page on either side. The returned
163 * memory region is jammed up against the _end_ of the pages.
165 * Freeing anything causes instantaneous decommitment of the pages
166 * involved, so stale pointers are caught as soon as possible.
169 static int minefield_initialised = 0;
170 static void *minefield_region = NULL;
171 static long minefield_size = 0;
172 static long minefield_npages = 0;
173 static long minefield_curpos = 0;
174 static unsigned short *minefield_admin = NULL;
175 static void *minefield_pages = NULL;
177 static void minefield_admin_hide(int hide)
179 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
180 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
183 static void minefield_init(void)
189 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
190 minefield_region = VirtualAlloc(NULL, size,
191 MEM_RESERVE, PAGE_NOACCESS);
192 if (minefield_region)
195 minefield_size = size;
198 * Firstly, allocate a section of that to be the admin block.
199 * We'll need a two-byte field for each page.
201 minefield_admin = minefield_region;
202 minefield_npages = minefield_size / PAGESIZE;
203 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
204 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
205 minefield_pages = (char *) minefield_region + admin_size;
208 * Commit the admin region.
210 VirtualAlloc(minefield_admin, minefield_npages * 2,
211 MEM_COMMIT, PAGE_READWRITE);
214 * Mark all pages as unused (0xFFFF).
216 for (i = 0; i < minefield_npages; i++)
217 minefield_admin[i] = 0xFFFF;
220 * Hide the admin region.
222 minefield_admin_hide(1);
224 minefield_initialised = 1;
227 static void minefield_bomb(void)
229 div(1, *(int *) minefield_pages);
232 static void *minefield_alloc(int size)
235 int pos, lim, region_end, region_start;
239 npages = (size + PAGESIZE - 1) / PAGESIZE;
241 minefield_admin_hide(0);
244 * Search from current position until we find a contiguous
245 * bunch of npages+2 unused pages.
247 pos = minefield_curpos;
248 lim = minefield_npages;
250 /* Skip over used pages. */
251 while (pos < lim && minefield_admin[pos] != 0xFFFF)
253 /* Count unused pages. */
255 while (pos < lim && pos - start < npages + 2 &&
256 minefield_admin[pos] == 0xFFFF)
258 if (pos - start == npages + 2)
260 /* If we've reached the limit, reset the limit or stop. */
262 if (lim == minefield_npages) {
263 /* go round and start again at zero */
264 lim = minefield_curpos;
267 minefield_admin_hide(1);
273 minefield_curpos = pos - 1;
276 * We have npages+2 unused pages starting at start. We leave
277 * the first and last of these alone and use the rest.
279 region_end = (start + npages + 1) * PAGESIZE;
280 region_start = region_end - size;
281 /* FIXME: could align here if we wanted */
284 * Update the admin region.
286 for (i = start + 2; i < start + npages + 1; i++)
287 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
288 minefield_admin[start + 1] = region_start % PAGESIZE;
290 minefield_admin_hide(1);
292 VirtualAlloc((char *) minefield_pages + region_start, size,
293 MEM_COMMIT, PAGE_READWRITE);
294 return (char *) minefield_pages + region_start;
297 static void minefield_free(void *ptr)
299 int region_start, i, j;
301 minefield_admin_hide(0);
303 region_start = (char *) ptr - (char *) minefield_pages;
304 i = region_start / PAGESIZE;
305 if (i < 0 || i >= minefield_npages ||
306 minefield_admin[i] != region_start % PAGESIZE)
308 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
309 minefield_admin[j] = 0xFFFF;
312 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
314 minefield_admin_hide(1);
317 static int minefield_get_size(void *ptr)
319 int region_start, i, j;
321 minefield_admin_hide(0);
323 region_start = (char *) ptr - (char *) minefield_pages;
324 i = region_start / PAGESIZE;
325 if (i < 0 || i >= minefield_npages ||
326 minefield_admin[i] != region_start % PAGESIZE)
328 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
330 minefield_admin_hide(1);
332 return j * PAGESIZE - region_start;
335 void *minefield_c_malloc(size_t size)
337 if (!minefield_initialised)
339 return minefield_alloc(size);
342 void minefield_c_free(void *p)
344 if (!minefield_initialised)
350 * realloc _always_ moves the chunk, for rapid detection of code
351 * that assumes it won't.
353 void *minefield_c_realloc(void *p, size_t size)
357 if (!minefield_initialised)
359 q = minefield_alloc(size);
360 oldsize = minefield_get_size(p);
361 memcpy(q, p, (oldsize < size ? oldsize : size));
366 #endif /* MINEFIELD */