WINMISC.C 8.7 KB

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