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