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