monotone

monotone Mtn Source Tree

Root/botan/es_win32.cpp

1/*************************************************
2* Win32 EntropySource Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/es_win32.h>
7#include <windows.h>
8#include <tlhelp32.h>
9
10namespace Botan {
11
12/*************************************************
13* Win32 Fast Poll *
14*************************************************/
15void Win32_EntropySource::do_fast_poll()
16 {
17 add_bytes(GetTickCount());
18 add_bytes(GetMessagePos());
19 add_bytes(GetMessageTime());
20 add_bytes(GetInputState());
21 add_bytes(GetCurrentProcessId());
22 add_bytes(GetCurrentThreadId());
23
24 SYSTEM_INFO sys_info;
25 GetSystemInfo(&sys_info);
26 add_bytes(&sys_info, sizeof(sys_info));
27
28 MEMORYSTATUS mem_info;
29 GlobalMemoryStatus(&mem_info);
30 add_bytes(&mem_info, sizeof(mem_info));
31
32 POINT point;
33 GetCursorPos(&point);
34 add_bytes(&point, sizeof(point));
35 GetCaretPos(&point);
36 add_bytes(&point, sizeof(point));
37 }
38
39/*************************************************
40* Win32 Slow Poll *
41*************************************************/
42void Win32_EntropySource::do_slow_poll()
43 {
44 const u32bit MAX_ITEMS = 256;
45 const u32bit HEAP_LISTS_MAX = 32;
46 const u32bit HEAP_OBJS_PER_LIST = 128;
47
48 do_fast_poll();
49
50 HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
51
52#define TOOLHELP32_ITER(DATA_TYPE, FUNC_FIRST, FUNC_NEXT) \
53 { \
54 u32bit items = 0; \
55 DATA_TYPE info; \
56 info.dwSize = sizeof(DATA_TYPE); \
57 if(FUNC_FIRST(snapshot, &info)) \
58 { \
59 do \
60 { \
61 if(items++ > MAX_ITEMS) break; \
62 add_bytes(&info, sizeof(info)); \
63 } while(FUNC_NEXT(snapshot, &info)); \
64 } \
65 }
66
67 TOOLHELP32_ITER(MODULEENTRY32, Module32First, Module32Next);
68 TOOLHELP32_ITER(PROCESSENTRY32, Process32First, Process32Next);
69 TOOLHELP32_ITER(THREADENTRY32, Thread32First, Thread32Next);
70
71#undef TOOLHELP32_ITER
72
73 u32bit heap_lists_found = 0;
74 HEAPLIST32 heap_list;
75 heap_list.dwSize = sizeof(HEAPLIST32);
76 if(Heap32ListFirst(snapshot, &heap_list))
77 {
78 do
79 {
80 add_bytes(&heap_list, sizeof(HEAPLIST32));
81
82 if(heap_lists_found++ > HEAP_LISTS_MAX)
83 break;
84
85 u32bit heap_objs_found = 0;
86 HEAPENTRY32 heap_entry;
87 heap_entry.dwSize = sizeof(HEAPENTRY32);
88 if(Heap32First(&heap_entry, heap_list.th32ProcessID,
89 heap_list.th32HeapID))
90 {
91 do
92 {
93 if(heap_objs_found++ > HEAP_OBJS_PER_LIST)
94 break;
95 add_bytes(&heap_entry, sizeof(HEAPENTRY32));
96 } while(Heap32Next(&heap_entry));
97 }
98 } while(Heap32ListNext(snapshot, &heap_list));
99 }
100
101 CloseHandle(snapshot);
102 }
103
104}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status