2 * winmisc.c: miscellaneous Windows-specific things
9 OSVERSIONINFO osVersion
;
11 void platform_get_x11_auth(char *display
, int *proto
,
12 unsigned char *data
, int *datalen
)
14 /* We don't support this at all under Windows. */
17 const char platform_x11_best_transport
[] = "localhost";
19 Filename
filename_from_str(const char *str
)
22 strncpy(ret
.path
, str
, sizeof(ret
.path
));
23 ret
.path
[sizeof(ret
.path
)-1] = '\0';
27 const char *filename_to_str(const Filename
*fn
)
32 int filename_equal(Filename f1
, Filename f2
)
34 return !strcmp(f1
.path
, f2
.path
);
37 int filename_is_null(Filename fn
)
42 char *get_username(void)
48 if (GetUserName(NULL
, &namelen
) == FALSE
)
51 user
= snewn(namelen
, char);
52 GetUserName(user
, &namelen
);
57 int SaneDialogBox(HINSTANCE hinst
,
69 wc
.style
= CS_DBLCLKS
| CS_SAVEBITS
| CS_BYTEALIGNWINDOW
;
70 wc
.lpfnWndProc
= DefDlgProc
;
72 wc
.cbWndExtra
= DLGWINDOWEXTRA
+ 8;
75 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
76 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+1);
77 wc
.lpszMenuName
= NULL
;
78 wc
.lpszClassName
= "PuTTYConfigBox";
81 hwnd
= CreateDialog(hinst
, tmpl
, hwndparent
, lpDialogFunc
);
83 SetWindowLong(hwnd
, BOXFLAGS
, 0); /* flags */
84 SetWindowLong(hwnd
, BOXRESULT
, 0); /* result from SaneEndDialog */
86 while ((gm
=GetMessage(&msg
, NULL
, 0, 0)) > 0) {
87 flags
=GetWindowLong(hwnd
, BOXFLAGS
);
88 if (!(flags
& DF_END
) && !IsDialogMessage(hwnd
, &msg
))
89 DispatchMessage(&msg
);
95 PostQuitMessage(msg
.wParam
); /* We got a WM_QUIT, pass it on */
97 ret
=GetWindowLong(hwnd
, BOXRESULT
);
102 void SaneEndDialog(HWND hwnd
, int ret
)
104 SetWindowLong(hwnd
, BOXRESULT
, ret
);
105 SetWindowLong(hwnd
, BOXFLAGS
, DF_END
);
108 BOOL
init_winver(void)
110 ZeroMemory(&osVersion
, sizeof(osVersion
));
111 osVersion
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFO
);
112 return GetVersionEx ( (OSVERSIONINFO
*) &osVersion
);
116 static FILE *debug_fp
= NULL
;
117 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
118 static int debug_got_console
= 0;
120 void dputs(char *buf
)
124 if (!debug_got_console
) {
125 if (AllocConsole()) {
126 debug_got_console
= 1;
127 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
131 debug_fp
= fopen("debug.log", "w");
134 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
135 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
137 fputs(buf
, debug_fp
);
144 * Minefield - a Windows equivalent for Electric Fence
147 #define PAGESIZE 4096
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
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.
160 * Freeing anything causes instantaneous decommitment of the pages
161 * involved, so stale pointers are caught as soon as possible.
164 static int minefield_initialised
= 0;
165 static void *minefield_region
= NULL
;
166 static long minefield_size
= 0;
167 static long minefield_npages
= 0;
168 static long minefield_curpos
= 0;
169 static unsigned short *minefield_admin
= NULL
;
170 static void *minefield_pages
= NULL
;
172 static void minefield_admin_hide(int hide
)
174 int access
= hide ? PAGE_NOACCESS
: PAGE_READWRITE
;
175 VirtualProtect(minefield_admin
, minefield_npages
* 2, access
, NULL
);
178 static void minefield_init(void)
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
)
190 minefield_size
= size
;
193 * Firstly, allocate a section of that to be the admin block.
194 * We'll need a two-byte field for each page.
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
;
203 * Commit the admin region.
205 VirtualAlloc(minefield_admin
, minefield_npages
* 2,
206 MEM_COMMIT
, PAGE_READWRITE
);
209 * Mark all pages as unused (0xFFFF).
211 for (i
= 0; i
< minefield_npages
; i
++)
212 minefield_admin
[i
] = 0xFFFF;
215 * Hide the admin region.
217 minefield_admin_hide(1);
219 minefield_initialised
= 1;
222 static void minefield_bomb(void)
224 div(1, *(int *) minefield_pages
);
227 static void *minefield_alloc(int size
)
230 int pos
, lim
, region_end
, region_start
;
234 npages
= (size
+ PAGESIZE
- 1) / PAGESIZE
;
236 minefield_admin_hide(0);
239 * Search from current position until we find a contiguous
240 * bunch of npages+2 unused pages.
242 pos
= minefield_curpos
;
243 lim
= minefield_npages
;
245 /* Skip over used pages. */
246 while (pos
< lim
&& minefield_admin
[pos
] != 0xFFFF)
248 /* Count unused pages. */
250 while (pos
< lim
&& pos
- start
< npages
+ 2 &&
251 minefield_admin
[pos
] == 0xFFFF)
253 if (pos
- start
== npages
+ 2)
255 /* If we've reached the limit, reset the limit or stop. */
257 if (lim
== minefield_npages
) {
258 /* go round and start again at zero */
259 lim
= minefield_curpos
;
262 minefield_admin_hide(1);
268 minefield_curpos
= pos
- 1;
271 * We have npages+2 unused pages starting at start. We leave
272 * the first and last of these alone and use the rest.
274 region_end
= (start
+ npages
+ 1) * PAGESIZE
;
275 region_start
= region_end
- size
;
276 /* FIXME: could align here if we wanted */
279 * Update the admin region.
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
;
285 minefield_admin_hide(1);
287 VirtualAlloc((char *) minefield_pages
+ region_start
, size
,
288 MEM_COMMIT
, PAGE_READWRITE
);
289 return (char *) minefield_pages
+ region_start
;
292 static void minefield_free(void *ptr
)
294 int region_start
, i
, j
;
296 minefield_admin_hide(0);
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
)
303 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++) {
304 minefield_admin
[j
] = 0xFFFF;
307 VirtualFree(ptr
, j
* PAGESIZE
- region_start
, MEM_DECOMMIT
);
309 minefield_admin_hide(1);
312 static int minefield_get_size(void *ptr
)
314 int region_start
, i
, j
;
316 minefield_admin_hide(0);
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
)
323 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++);
325 minefield_admin_hide(1);
327 return j
* PAGESIZE
- region_start
;
330 void *minefield_c_malloc(size_t size
)
332 if (!minefield_initialised
)
334 return minefield_alloc(size
);
337 void minefield_c_free(void *p
)
339 if (!minefield_initialised
)
345 * realloc _always_ moves the chunk, for rapid detection of code
346 * that assumes it won't.
348 void *minefield_c_realloc(void *p
, size_t size
)
352 if (!minefield_initialised
)
354 q
= minefield_alloc(size
);
355 oldsize
= minefield_get_size(p
);
356 memcpy(q
, p
, (oldsize
< size ? oldsize
: size
));
361 #endif /* MINEFIELD */