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