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 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)
46 if (GetUserName(NULL, &namelen) == FALSE)
49 user = snewn(namelen, char);
50 GetUserName(user, &namelen);
55 int SaneDialogBox(HINSTANCE hinst,
67 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
68 wc.lpfnWndProc = DefDlgProc;
70 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
73 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
74 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
75 wc.lpszMenuName = NULL;
76 wc.lpszClassName = "PuTTYConfigBox";
79 hwnd = CreateDialog(hinst, tmpl, hwndparent, lpDialogFunc);
81 SetWindowLong(hwnd, BOXFLAGS, 0); /* flags */
82 SetWindowLong(hwnd, BOXRESULT, 0); /* result from SaneEndDialog */
84 while ((gm=GetMessage(&msg, NULL, 0, 0)) > 0) {
85 flags=GetWindowLong(hwnd, BOXFLAGS);
86 if (!(flags & DF_END) && !IsDialogMessage(hwnd, &msg))
87 DispatchMessage(&msg);
93 PostQuitMessage(msg.wParam); /* We got a WM_QUIT, pass it on */
95 ret=GetWindowLong(hwnd, BOXRESULT);
100 void SaneEndDialog(HWND hwnd, int ret)
102 SetWindowLong(hwnd, BOXRESULT, ret);
103 SetWindowLong(hwnd, BOXFLAGS, DF_END);
106 BOOL init_winver(void)
108 ZeroMemory(&osVersion, sizeof(osVersion));
109 osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
110 return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
114 static FILE *debug_fp = NULL;
115 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
116 static int debug_got_console = 0;
118 void dputs(char *buf)
122 if (!debug_got_console) {
123 if (AllocConsole()) {
124 debug_got_console = 1;
125 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
129 debug_fp = fopen("debug.log", "w");
132 if (debug_hdl != INVALID_HANDLE_VALUE) {
133 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
135 fputs(buf, debug_fp);
142 * Minefield - a Windows equivalent for Electric Fence
145 #define PAGESIZE 4096
150 * We start by reserving as much virtual address space as Windows
151 * will sensibly (or not sensibly) let us have. We flag it all as
154 * Any allocation attempt is satisfied by committing one or more
155 * pages, with an uncommitted page on either side. The returned
156 * memory region is jammed up against the _end_ of the pages.
158 * Freeing anything causes instantaneous decommitment of the pages
159 * involved, so stale pointers are caught as soon as possible.
162 static int minefield_initialised = 0;
163 static void *minefield_region = NULL;
164 static long minefield_size = 0;
165 static long minefield_npages = 0;
166 static long minefield_curpos = 0;
167 static unsigned short *minefield_admin = NULL;
168 static void *minefield_pages = NULL;
170 static void minefield_admin_hide(int hide)
172 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
173 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
176 static void minefield_init(void)
182 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
183 minefield_region = VirtualAlloc(NULL, size,
184 MEM_RESERVE, PAGE_NOACCESS);
185 if (minefield_region)
188 minefield_size = size;
191 * Firstly, allocate a section of that to be the admin block.
192 * We'll need a two-byte field for each page.
194 minefield_admin = minefield_region;
195 minefield_npages = minefield_size / PAGESIZE;
196 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
197 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
198 minefield_pages = (char *) minefield_region + admin_size;
201 * Commit the admin region.
203 VirtualAlloc(minefield_admin, minefield_npages * 2,
204 MEM_COMMIT, PAGE_READWRITE);
207 * Mark all pages as unused (0xFFFF).
209 for (i = 0; i < minefield_npages; i++)
210 minefield_admin[i] = 0xFFFF;
213 * Hide the admin region.
215 minefield_admin_hide(1);
217 minefield_initialised = 1;
220 static void minefield_bomb(void)
222 div(1, *(int *) minefield_pages);
225 static void *minefield_alloc(int size)
228 int pos, lim, region_end, region_start;
232 npages = (size + PAGESIZE - 1) / PAGESIZE;
234 minefield_admin_hide(0);
237 * Search from current position until we find a contiguous
238 * bunch of npages+2 unused pages.
240 pos = minefield_curpos;
241 lim = minefield_npages;
243 /* Skip over used pages. */
244 while (pos < lim && minefield_admin[pos] != 0xFFFF)
246 /* Count unused pages. */
248 while (pos < lim && pos - start < npages + 2 &&
249 minefield_admin[pos] == 0xFFFF)
251 if (pos - start == npages + 2)
253 /* If we've reached the limit, reset the limit or stop. */
255 if (lim == minefield_npages) {
256 /* go round and start again at zero */
257 lim = minefield_curpos;
260 minefield_admin_hide(1);
266 minefield_curpos = pos - 1;
269 * We have npages+2 unused pages starting at start. We leave
270 * the first and last of these alone and use the rest.
272 region_end = (start + npages + 1) * PAGESIZE;
273 region_start = region_end - size;
274 /* FIXME: could align here if we wanted */
277 * Update the admin region.
279 for (i = start + 2; i < start + npages + 1; i++)
280 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
281 minefield_admin[start + 1] = region_start % PAGESIZE;
283 minefield_admin_hide(1);
285 VirtualAlloc((char *) minefield_pages + region_start, size,
286 MEM_COMMIT, PAGE_READWRITE);
287 return (char *) minefield_pages + region_start;
290 static void minefield_free(void *ptr)
292 int region_start, i, j;
294 minefield_admin_hide(0);
296 region_start = (char *) ptr - (char *) minefield_pages;
297 i = region_start / PAGESIZE;
298 if (i < 0 || i >= minefield_npages ||
299 minefield_admin[i] != region_start % PAGESIZE)
301 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
302 minefield_admin[j] = 0xFFFF;
305 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
307 minefield_admin_hide(1);
310 static int minefield_get_size(void *ptr)
312 int region_start, i, j;
314 minefield_admin_hide(0);
316 region_start = (char *) ptr - (char *) minefield_pages;
317 i = region_start / PAGESIZE;
318 if (i < 0 || i >= minefield_npages ||
319 minefield_admin[i] != region_start % PAGESIZE)
321 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
323 minefield_admin_hide(1);
325 return j * PAGESIZE - region_start;
328 void *minefield_c_malloc(size_t size)
330 if (!minefield_initialised)
332 return minefield_alloc(size);
335 void minefield_c_free(void *p)
337 if (!minefield_initialised)
343 * realloc _always_ moves the chunk, for rapid detection of code
344 * that assumes it won't.
346 void *minefield_c_realloc(void *p, size_t size)
350 if (!minefield_initialised)
352 q = minefield_alloc(size);
353 oldsize = minefield_get_size(p);
354 memcpy(q, p, (oldsize < size ? oldsize : size));
359 #endif /* MINEFIELD */