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