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