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