]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winmisc.c
Quell more warnings and, perhaps, avoid alignment faults on 64-bit Windows.
[PuTTY.git] / windows / 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 char *platform_get_x_display(void) {
12     /* We may as well check for DISPLAY in case it's useful. */
13     return dupstr(getenv("DISPLAY"));
14 }
15
16 Filename filename_from_str(const char *str)
17 {
18     Filename ret;
19     strncpy(ret.path, str, sizeof(ret.path));
20     ret.path[sizeof(ret.path)-1] = '\0';
21     return ret;
22 }
23
24 const char *filename_to_str(const Filename *fn)
25 {
26     return fn->path;
27 }
28
29 int filename_equal(Filename f1, Filename f2)
30 {
31     return !strcmp(f1.path, f2.path);
32 }
33
34 int filename_is_null(Filename fn)
35 {
36     return !*fn.path;
37 }
38
39 char *get_username(void)
40 {
41     DWORD namelen;
42     char *user;
43
44     namelen = 0;
45     if (GetUserName(NULL, &namelen) == FALSE) {
46         /*
47          * Apparently this doesn't work at least on Windows XP SP2.
48          * Thus assume a maximum of 256. It will fail again if it
49          * doesn't fit.
50          */
51         namelen = 256;
52     }
53
54     user = snewn(namelen, char);
55     GetUserName(user, &namelen);
56
57     return user;
58 }
59
60 BOOL init_winver(void)
61 {
62     ZeroMemory(&osVersion, sizeof(osVersion));
63     osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
64     return GetVersionEx ( (OSVERSIONINFO *) &osVersion);
65 }
66
67 #ifdef DEBUG
68 static FILE *debug_fp = NULL;
69 static HANDLE debug_hdl = INVALID_HANDLE_VALUE;
70 static int debug_got_console = 0;
71
72 void dputs(char *buf)
73 {
74     DWORD dw;
75
76     if (!debug_got_console) {
77         if (AllocConsole()) {
78             debug_got_console = 1;
79             debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE);
80         }
81     }
82     if (!debug_fp) {
83         debug_fp = fopen("debug.log", "w");
84     }
85
86     if (debug_hdl != INVALID_HANDLE_VALUE) {
87         WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL);
88     }
89     fputs(buf, debug_fp);
90     fflush(debug_fp);
91 }
92 #endif
93
94 #ifdef MINEFIELD
95 /*
96  * Minefield - a Windows equivalent for Electric Fence
97  */
98
99 #define PAGESIZE 4096
100
101 /*
102  * Design:
103  * 
104  * We start by reserving as much virtual address space as Windows
105  * will sensibly (or not sensibly) let us have. We flag it all as
106  * invalid memory.
107  * 
108  * Any allocation attempt is satisfied by committing one or more
109  * pages, with an uncommitted page on either side. The returned
110  * memory region is jammed up against the _end_ of the pages.
111  * 
112  * Freeing anything causes instantaneous decommitment of the pages
113  * involved, so stale pointers are caught as soon as possible.
114  */
115
116 static int minefield_initialised = 0;
117 static void *minefield_region = NULL;
118 static long minefield_size = 0;
119 static long minefield_npages = 0;
120 static long minefield_curpos = 0;
121 static unsigned short *minefield_admin = NULL;
122 static void *minefield_pages = NULL;
123
124 static void minefield_admin_hide(int hide)
125 {
126     int access = hide ? PAGE_NOACCESS : PAGE_READWRITE;
127     VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL);
128 }
129
130 static void minefield_init(void)
131 {
132     int size;
133     int admin_size;
134     int i;
135
136     for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) {
137         minefield_region = VirtualAlloc(NULL, size,
138                                         MEM_RESERVE, PAGE_NOACCESS);
139         if (minefield_region)
140             break;
141     }
142     minefield_size = size;
143
144     /*
145      * Firstly, allocate a section of that to be the admin block.
146      * We'll need a two-byte field for each page.
147      */
148     minefield_admin = minefield_region;
149     minefield_npages = minefield_size / PAGESIZE;
150     admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1);
151     minefield_npages = (minefield_size - admin_size) / PAGESIZE;
152     minefield_pages = (char *) minefield_region + admin_size;
153
154     /*
155      * Commit the admin region.
156      */
157     VirtualAlloc(minefield_admin, minefield_npages * 2,
158                  MEM_COMMIT, PAGE_READWRITE);
159
160     /*
161      * Mark all pages as unused (0xFFFF).
162      */
163     for (i = 0; i < minefield_npages; i++)
164         minefield_admin[i] = 0xFFFF;
165
166     /*
167      * Hide the admin region.
168      */
169     minefield_admin_hide(1);
170
171     minefield_initialised = 1;
172 }
173
174 static void minefield_bomb(void)
175 {
176     div(1, *(int *) minefield_pages);
177 }
178
179 static void *minefield_alloc(int size)
180 {
181     int npages;
182     int pos, lim, region_end, region_start;
183     int start;
184     int i;
185
186     npages = (size + PAGESIZE - 1) / PAGESIZE;
187
188     minefield_admin_hide(0);
189
190     /*
191      * Search from current position until we find a contiguous
192      * bunch of npages+2 unused pages.
193      */
194     pos = minefield_curpos;
195     lim = minefield_npages;
196     while (1) {
197         /* Skip over used pages. */
198         while (pos < lim && minefield_admin[pos] != 0xFFFF)
199             pos++;
200         /* Count unused pages. */
201         start = pos;
202         while (pos < lim && pos - start < npages + 2 &&
203                minefield_admin[pos] == 0xFFFF)
204             pos++;
205         if (pos - start == npages + 2)
206             break;
207         /* If we've reached the limit, reset the limit or stop. */
208         if (pos >= lim) {
209             if (lim == minefield_npages) {
210                 /* go round and start again at zero */
211                 lim = minefield_curpos;
212                 pos = 0;
213             } else {
214                 minefield_admin_hide(1);
215                 return NULL;
216             }
217         }
218     }
219
220     minefield_curpos = pos - 1;
221
222     /*
223      * We have npages+2 unused pages starting at start. We leave
224      * the first and last of these alone and use the rest.
225      */
226     region_end = (start + npages + 1) * PAGESIZE;
227     region_start = region_end - size;
228     /* FIXME: could align here if we wanted */
229
230     /*
231      * Update the admin region.
232      */
233     for (i = start + 2; i < start + npages + 1; i++)
234         minefield_admin[i] = 0xFFFE;   /* used but no region starts here */
235     minefield_admin[start + 1] = region_start % PAGESIZE;
236
237     minefield_admin_hide(1);
238
239     VirtualAlloc((char *) minefield_pages + region_start, size,
240                  MEM_COMMIT, PAGE_READWRITE);
241     return (char *) minefield_pages + region_start;
242 }
243
244 static void minefield_free(void *ptr)
245 {
246     int region_start, i, j;
247
248     minefield_admin_hide(0);
249
250     region_start = (char *) ptr - (char *) minefield_pages;
251     i = region_start / PAGESIZE;
252     if (i < 0 || i >= minefield_npages ||
253         minefield_admin[i] != region_start % PAGESIZE)
254         minefield_bomb();
255     for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) {
256         minefield_admin[j] = 0xFFFF;
257     }
258
259     VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT);
260
261     minefield_admin_hide(1);
262 }
263
264 static int minefield_get_size(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
277     minefield_admin_hide(1);
278
279     return j * PAGESIZE - region_start;
280 }
281
282 void *minefield_c_malloc(size_t size)
283 {
284     if (!minefield_initialised)
285         minefield_init();
286     return minefield_alloc(size);
287 }
288
289 void minefield_c_free(void *p)
290 {
291     if (!minefield_initialised)
292         minefield_init();
293     minefield_free(p);
294 }
295
296 /*
297  * realloc _always_ moves the chunk, for rapid detection of code
298  * that assumes it won't.
299  */
300 void *minefield_c_realloc(void *p, size_t size)
301 {
302     size_t oldsize;
303     void *q;
304     if (!minefield_initialised)
305         minefield_init();
306     q = minefield_alloc(size);
307     oldsize = minefield_get_size(p);
308     memcpy(q, p, (oldsize < size ? oldsize : size));
309     minefield_free(p);
310     return q;
311 }
312
313 #endif                          /* MINEFIELD */