Fix for `double-alt-keystrokes'. Thanks to Leonid Lisovskiy.
[u/mdw/putty] / winmisc.c
CommitLineData
e0e7dff8 1/*
2 * winmisc.c: miscellaneous Windows-specific things.
3 */
4
5#include <windows.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include "putty.h"
8ee3ff16 9#include "winstuff.h"
e0e7dff8 10
11void platform_get_x11_auth(char *display, int *proto,
12 unsigned char *data, int *datalen)
13{
14 /* We don't support this at all under Windows. */
15}
9a30e26b 16
9fab77dc 17Filename filename_from_str(const char *str)
9a30e26b 18{
19 Filename ret;
20 strncpy(ret.path, str, sizeof(ret.path));
21 ret.path[sizeof(ret.path)-1] = '\0';
22 return ret;
23}
24
9fab77dc 25const char *filename_to_str(const Filename *fn)
9a30e26b 26{
9fab77dc 27 return fn->path;
9a30e26b 28}
29
30int filename_equal(Filename f1, Filename f2)
31{
32 return !strcmp(f1.path, f2.path);
33}
34
35int filename_is_null(Filename fn)
36{
37 return !*fn.path;
38}
d0912d1f 39
8ee3ff16 40int SaneDialogBox(HINSTANCE hinst,
41 LPCTSTR tmpl,
42 HWND hwndparent,
43 DLGPROC lpDialogFunc)
44{
2979718a 45 WNDCLASS wc;
46 HWND hwnd;
8ee3ff16 47 MSG msg;
2979718a 48 int flags;
49 int ret;
50
51 wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
52 wc.lpfnWndProc = DefDlgProc;
53 wc.cbClsExtra = 0;
54 wc.cbWndExtra = DLGWINDOWEXTRA + 8;
55 wc.hInstance = hinst;
56 wc.hIcon = NULL;
57 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
58 wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1);
59 wc.lpszMenuName = NULL;
60 wc.lpszClassName = "PuTTYConfigBox";
61 RegisterClass(&wc);
62
63 hwnd = CreateDialog(hinst, tmpl, hwndparent, lpDialogFunc);
64
65 SetWindowLong(hwnd, BOXFLAGS, 0); /* flags */
66 SetWindowLong(hwnd, BOXRESULT, 0); /* result from SaneEndDialog */
67
8ee3ff16 68 while (GetMessage(&msg, NULL, 0, 0)) {
2979718a 69 flags=GetWindowLong(hwnd, BOXFLAGS);
70 if (!(flags & DF_END) && !IsDialogMessage(hwnd, &msg))
8ee3ff16 71 DispatchMessage(&msg);
2979718a 72 if (flags & DF_END)
73 break;
8ee3ff16 74 }
2979718a 75
76 ret=GetWindowLong(hwnd, BOXRESULT);
77 DestroyWindow(hwnd);
78 return ret;
8ee3ff16 79}
80
81void SaneEndDialog(HWND hwnd, int ret)
82{
2979718a 83 SetWindowLong(hwnd, BOXRESULT, ret);
84 SetWindowLong(hwnd, BOXFLAGS, DF_END);
8ee3ff16 85}
86
d0912d1f 87#ifdef DEBUG
88static FILE *debug_fp = NULL;
89static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
90static int debug_got_console = 0;
91
92void dputs(char *buf)
93{
94 DWORD dw;
95
96 if (!debug_got_console) {
97 if (AllocConsole()) {
98 debug_got_console = 1;
99 debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
100 }
101 }
102 if (!debug_fp) {
103 debug_fp = fopen("debug.log", "w");
104 }
105
106 if (debug_hdl != INVALID_HANDLE_VALUE) {
107 WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
108 }
109 fputs(buf, debug_fp);
110 fflush(debug_fp);
111}
112#endif
113
114#ifdef MINEFIELD
115/*
116 * Minefield - a Windows equivalent for Electric Fence
117 */
118
119#define PAGESIZE 4096
120
121/*
122 * Design:
123 *
124 * We start by reserving as much virtual address space as Windows
125 * will sensibly (or not sensibly) let us have. We flag it all as
126 * invalid memory.
127 *
128 * Any allocation attempt is satisfied by committing one or more
129 * pages, with an uncommitted page on either side. The returned
130 * memory region is jammed up against the _end_ of the pages.
131 *
132 * Freeing anything causes instantaneous decommitment of the pages
133 * involved, so stale pointers are caught as soon as possible.
134 */
135
136static int minefield_initialised = 0;
137static void *minefield_region = NULL;
138static long minefield_size = 0;
139static long minefield_npages = 0;
140static long minefield_curpos = 0;
141static unsigned short *minefield_admin = NULL;
142static void *minefield_pages = NULL;
143
144static void minefield_admin_hide(int hide)
145{
146 int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
147 VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
148}
149
150static void minefield_init(void)
151{
152 int size;
153 int admin_size;
154 int i;
155
156 for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
157 minefield_region = VirtualAlloc(NULL, size,
158 MEM_RESERVE, PAGE_NOACCESS);
159 if (minefield_region)
160 break;
161 }
162 minefield_size = size;
163
164 /*
165 * Firstly, allocate a section of that to be the admin block.
166 * We'll need a two-byte field for each page.
167 */
168 minefield_admin = minefield_region;
169 minefield_npages = minefield_size / PAGESIZE;
170 admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
171 minefield_npages = (minefield_size - admin_size) / PAGESIZE;
172 minefield_pages = (char *) minefield_region + admin_size;
173
174 /*
175 * Commit the admin region.
176 */
177 VirtualAlloc(minefield_admin, minefield_npages * 2,
178 MEM_COMMIT, PAGE_READWRITE);
179
180 /*
181 * Mark all pages as unused (0xFFFF).
182 */
183 for (i = 0; i < minefield_npages; i++)
184 minefield_admin[i] = 0xFFFF;
185
186 /*
187 * Hide the admin region.
188 */
189 minefield_admin_hide(1);
190
191 minefield_initialised = 1;
192}
193
194static void minefield_bomb(void)
195{
196 div(1, *(int *) minefield_pages);
197}
198
199static void *minefield_alloc(int size)
200{
201 int npages;
202 int pos, lim, region_end, region_start;
203 int start;
204 int i;
205
206 npages = (size + PAGESIZE - 1) / PAGESIZE;
207
208 minefield_admin_hide(0);
209
210 /*
211 * Search from current position until we find a contiguous
212 * bunch of npages+2 unused pages.
213 */
214 pos = minefield_curpos;
215 lim = minefield_npages;
216 while (1) {
217 /* Skip over used pages. */
218 while (pos < lim && minefield_admin[pos] != 0xFFFF)
219 pos++;
220 /* Count unused pages. */
221 start = pos;
222 while (pos < lim && pos - start < npages + 2 &&
223 minefield_admin[pos] == 0xFFFF)
224 pos++;
225 if (pos - start == npages + 2)
226 break;
227 /* If we've reached the limit, reset the limit or stop. */
228 if (pos >= lim) {
229 if (lim == minefield_npages) {
230 /* go round and start again at zero */
231 lim = minefield_curpos;
232 pos = 0;
233 } else {
234 minefield_admin_hide(1);
235 return NULL;
236 }
237 }
238 }
239
240 minefield_curpos = pos - 1;
241
242 /*
243 * We have npages+2 unused pages starting at start. We leave
244 * the first and last of these alone and use the rest.
245 */
246 region_end = (start + npages + 1) * PAGESIZE;
247 region_start = region_end - size;
248 /* FIXME: could align here if we wanted */
249
250 /*
251 * Update the admin region.
252 */
253 for (i = start + 2; i < start + npages + 1; i++)
254 minefield_admin[i] = 0xFFFE; /* used but no region starts here */
255 minefield_admin[start + 1] = region_start % PAGESIZE;
256
257 minefield_admin_hide(1);
258
259 VirtualAlloc((char *) minefield_pages + region_start, size,
260 MEM_COMMIT, PAGE_READWRITE);
261 return (char *) minefield_pages + region_start;
262}
263
264static void minefield_free(void *ptr)
265{
266 int region_start, i, j;
267
268 minefield_admin_hide(0);
269
270 region_start = (char *) ptr - (char *) minefield_pages;
271 i = region_start / PAGESIZE;
272 if (i < 0 || i >= minefield_npages ||
273 minefield_admin[i] != region_start % PAGESIZE)
274 minefield_bomb();
275 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
276 minefield_admin[j] = 0xFFFF;
277 }
278
279 VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
280
281 minefield_admin_hide(1);
282}
283
284static int minefield_get_size(void *ptr)
285{
286 int region_start, i, j;
287
288 minefield_admin_hide(0);
289
290 region_start = (char *) ptr - (char *) minefield_pages;
291 i = region_start / PAGESIZE;
292 if (i < 0 || i >= minefield_npages ||
293 minefield_admin[i] != region_start % PAGESIZE)
294 minefield_bomb();
295 for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++);
296
297 minefield_admin_hide(1);
298
299 return j * PAGESIZE - region_start;
300}
301
302void *minefield_c_malloc(size_t size)
303{
304 if (!minefield_initialised)
305 minefield_init();
306 return minefield_alloc(size);
307}
308
309void minefield_c_free(void *p)
310{
311 if (!minefield_initialised)
312 minefield_init();
313 minefield_free(p);
314}
315
316/*
317 * realloc _always_ moves the chunk, for rapid detection of code
318 * that assumes it won't.
319 */
320void *minefield_c_realloc(void *p, size_t size)
321{
322 size_t oldsize;
323 void *q;
324 if (!minefield_initialised)
325 minefield_init();
326 q = minefield_alloc(size);
327 oldsize = minefield_get_size(p);
328 memcpy(q, p, (oldsize < size ? oldsize : size));
329 minefield_free(p);
330 return q;
331}
332
333#endif /* MINEFIELD */