winmisc.c 8.5 KB

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