| 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 | 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 | char *get_username(void) |
| 43 | { |
| 44 | DWORD namelen; |
| 45 | char *user; |
| 46 | |
| 47 | namelen = 0; |
| 48 | if (GetUserName(NULL, &namelen) == FALSE) |
| 49 | return NULL; |
| 50 | |
| 51 | user = snewn(namelen, char); |
| 52 | GetUserName(user, &namelen); |
| 53 | |
| 54 | return user; |
| 55 | } |
| 56 | |
| 57 | int SaneDialogBox(HINSTANCE hinst, |
| 58 | LPCTSTR tmpl, |
| 59 | HWND hwndparent, |
| 60 | DLGPROC lpDialogFunc) |
| 61 | { |
| 62 | WNDCLASS wc; |
| 63 | HWND hwnd; |
| 64 | MSG msg; |
| 65 | int flags; |
| 66 | int ret; |
| 67 | int gm; |
| 68 | |
| 69 | wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW; |
| 70 | wc.lpfnWndProc = DefDlgProc; |
| 71 | wc.cbClsExtra = 0; |
| 72 | wc.cbWndExtra = DLGWINDOWEXTRA + 8; |
| 73 | wc.hInstance = hinst; |
| 74 | wc.hIcon = NULL; |
| 75 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 76 | wc.hbrBackground = (HBRUSH) (COLOR_BACKGROUND +1); |
| 77 | wc.lpszMenuName = NULL; |
| 78 | wc.lpszClassName = "PuTTYConfigBox"; |
| 79 | RegisterClass(&wc); |
| 80 | |
| 81 | hwnd = CreateDialog(hinst, tmpl, hwndparent, lpDialogFunc); |
| 82 | |
| 83 | SetWindowLong(hwnd, BOXFLAGS, 0); /* flags */ |
| 84 | SetWindowLong(hwnd, BOXRESULT, 0); /* result from SaneEndDialog */ |
| 85 | |
| 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); |
| 90 | if (flags & DF_END) |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | if (gm == 0) |
| 95 | PostQuitMessage(msg.wParam); /* We got a WM_QUIT, pass it on */ |
| 96 | |
| 97 | ret=GetWindowLong(hwnd, BOXRESULT); |
| 98 | DestroyWindow(hwnd); |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | void SaneEndDialog(HWND hwnd, int ret) |
| 103 | { |
| 104 | SetWindowLong(hwnd, BOXRESULT, ret); |
| 105 | SetWindowLong(hwnd, BOXFLAGS, DF_END); |
| 106 | } |
| 107 | |
| 108 | BOOL init_winver(void) |
| 109 | { |
| 110 | ZeroMemory(&osVersion, sizeof(osVersion)); |
| 111 | osVersion.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); |
| 112 | return GetVersionEx ( (OSVERSIONINFO *) &osVersion); |
| 113 | } |
| 114 | |
| 115 | #ifdef DEBUG |
| 116 | static FILE *debug_fp = NULL; |
| 117 | static HANDLE debug_hdl = INVALID_HANDLE_VALUE; |
| 118 | static int debug_got_console = 0; |
| 119 | |
| 120 | void dputs(char *buf) |
| 121 | { |
| 122 | DWORD dw; |
| 123 | |
| 124 | if (!debug_got_console) { |
| 125 | if (AllocConsole()) { |
| 126 | debug_got_console = 1; |
| 127 | debug_hdl = GetStdHandle(STD_OUTPUT_HANDLE); |
| 128 | } |
| 129 | } |
| 130 | if (!debug_fp) { |
| 131 | debug_fp = fopen("debug.log", "w"); |
| 132 | } |
| 133 | |
| 134 | if (debug_hdl != INVALID_HANDLE_VALUE) { |
| 135 | WriteFile(debug_hdl, buf, strlen(buf), &dw, NULL); |
| 136 | } |
| 137 | fputs(buf, debug_fp); |
| 138 | fflush(debug_fp); |
| 139 | } |
| 140 | #endif |
| 141 | |
| 142 | #ifdef MINEFIELD |
| 143 | /* |
| 144 | * Minefield - a Windows equivalent for Electric Fence |
| 145 | */ |
| 146 | |
| 147 | #define PAGESIZE 4096 |
| 148 | |
| 149 | /* |
| 150 | * Design: |
| 151 | * |
| 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 |
| 154 | * invalid memory. |
| 155 | * |
| 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. |
| 159 | * |
| 160 | * Freeing anything causes instantaneous decommitment of the pages |
| 161 | * involved, so stale pointers are caught as soon as possible. |
| 162 | */ |
| 163 | |
| 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; |
| 171 | |
| 172 | static void minefield_admin_hide(int hide) |
| 173 | { |
| 174 | int access = hide ? PAGE_NOACCESS : PAGE_READWRITE; |
| 175 | VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL); |
| 176 | } |
| 177 | |
| 178 | static void minefield_init(void) |
| 179 | { |
| 180 | int size; |
| 181 | int admin_size; |
| 182 | int i; |
| 183 | |
| 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) |
| 188 | break; |
| 189 | } |
| 190 | minefield_size = size; |
| 191 | |
| 192 | /* |
| 193 | * Firstly, allocate a section of that to be the admin block. |
| 194 | * We'll need a two-byte field for each page. |
| 195 | */ |
| 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; |
| 201 | |
| 202 | /* |
| 203 | * Commit the admin region. |
| 204 | */ |
| 205 | VirtualAlloc(minefield_admin, minefield_npages * 2, |
| 206 | MEM_COMMIT, PAGE_READWRITE); |
| 207 | |
| 208 | /* |
| 209 | * Mark all pages as unused (0xFFFF). |
| 210 | */ |
| 211 | for (i = 0; i < minefield_npages; i++) |
| 212 | minefield_admin[i] = 0xFFFF; |
| 213 | |
| 214 | /* |
| 215 | * Hide the admin region. |
| 216 | */ |
| 217 | minefield_admin_hide(1); |
| 218 | |
| 219 | minefield_initialised = 1; |
| 220 | } |
| 221 | |
| 222 | static void minefield_bomb(void) |
| 223 | { |
| 224 | div(1, *(int *) minefield_pages); |
| 225 | } |
| 226 | |
| 227 | static void *minefield_alloc(int size) |
| 228 | { |
| 229 | int npages; |
| 230 | int pos, lim, region_end, region_start; |
| 231 | int start; |
| 232 | int i; |
| 233 | |
| 234 | npages = (size + PAGESIZE - 1) / PAGESIZE; |
| 235 | |
| 236 | minefield_admin_hide(0); |
| 237 | |
| 238 | /* |
| 239 | * Search from current position until we find a contiguous |
| 240 | * bunch of npages+2 unused pages. |
| 241 | */ |
| 242 | pos = minefield_curpos; |
| 243 | lim = minefield_npages; |
| 244 | while (1) { |
| 245 | /* Skip over used pages. */ |
| 246 | while (pos < lim && minefield_admin[pos] != 0xFFFF) |
| 247 | pos++; |
| 248 | /* Count unused pages. */ |
| 249 | start = pos; |
| 250 | while (pos < lim && pos - start < npages + 2 && |
| 251 | minefield_admin[pos] == 0xFFFF) |
| 252 | pos++; |
| 253 | if (pos - start == npages + 2) |
| 254 | break; |
| 255 | /* If we've reached the limit, reset the limit or stop. */ |
| 256 | if (pos >= lim) { |
| 257 | if (lim == minefield_npages) { |
| 258 | /* go round and start again at zero */ |
| 259 | lim = minefield_curpos; |
| 260 | pos = 0; |
| 261 | } else { |
| 262 | minefield_admin_hide(1); |
| 263 | return NULL; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | minefield_curpos = pos - 1; |
| 269 | |
| 270 | /* |
| 271 | * We have npages+2 unused pages starting at start. We leave |
| 272 | * the first and last of these alone and use the rest. |
| 273 | */ |
| 274 | region_end = (start + npages + 1) * PAGESIZE; |
| 275 | region_start = region_end - size; |
| 276 | /* FIXME: could align here if we wanted */ |
| 277 | |
| 278 | /* |
| 279 | * Update the admin region. |
| 280 | */ |
| 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; |
| 284 | |
| 285 | minefield_admin_hide(1); |
| 286 | |
| 287 | VirtualAlloc((char *) minefield_pages + region_start, size, |
| 288 | MEM_COMMIT, PAGE_READWRITE); |
| 289 | return (char *) minefield_pages + region_start; |
| 290 | } |
| 291 | |
| 292 | static void minefield_free(void *ptr) |
| 293 | { |
| 294 | int region_start, i, j; |
| 295 | |
| 296 | minefield_admin_hide(0); |
| 297 | |
| 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) |
| 302 | minefield_bomb(); |
| 303 | for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) { |
| 304 | minefield_admin[j] = 0xFFFF; |
| 305 | } |
| 306 | |
| 307 | VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT); |
| 308 | |
| 309 | minefield_admin_hide(1); |
| 310 | } |
| 311 | |
| 312 | static int minefield_get_size(void *ptr) |
| 313 | { |
| 314 | int region_start, i, j; |
| 315 | |
| 316 | minefield_admin_hide(0); |
| 317 | |
| 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) |
| 322 | minefield_bomb(); |
| 323 | for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++); |
| 324 | |
| 325 | minefield_admin_hide(1); |
| 326 | |
| 327 | return j * PAGESIZE - region_start; |
| 328 | } |
| 329 | |
| 330 | void *minefield_c_malloc(size_t size) |
| 331 | { |
| 332 | if (!minefield_initialised) |
| 333 | minefield_init(); |
| 334 | return minefield_alloc(size); |
| 335 | } |
| 336 | |
| 337 | void minefield_c_free(void *p) |
| 338 | { |
| 339 | if (!minefield_initialised) |
| 340 | minefield_init(); |
| 341 | minefield_free(p); |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * realloc _always_ moves the chunk, for rapid detection of code |
| 346 | * that assumes it won't. |
| 347 | */ |
| 348 | void *minefield_c_realloc(void *p, size_t size) |
| 349 | { |
| 350 | size_t oldsize; |
| 351 | void *q; |
| 352 | if (!minefield_initialised) |
| 353 | minefield_init(); |
| 354 | q = minefield_alloc(size); |
| 355 | oldsize = minefield_get_size(p); |
| 356 | memcpy(q, p, (oldsize < size ? oldsize : size)); |
| 357 | minefield_free(p); |
| 358 | return q; |
| 359 | } |
| 360 | |
| 361 | #endif /* MINEFIELD */ |