source: trunk/EraserDll/Common.h @ 4

Revision 4, 8.2 KB checked in by lowjoel, 7 years ago (diff)

Added the source files from EraserDll? (SVN migration commit 2)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1// Common.h
2//
3// Eraser. Secure data removal. For Windows.
4// Copyright © 1997-2001  Sami Tolvanen (sami@tolvanen.com).
5//
6// This program is free software; you can redistribute it and/or
7// modify it under the terms of the GNU General Public License
8// as published by the Free Software Foundation; either version 2
9// of the License, or (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19// 02111-1307, USA.
20
21#ifndef COMMON_H
22#define COMMON_H
23
24// global variable definition
25//
26#ifdef GLOBAL_VARIABLES_HERE
27    #define GLOBALVAR
28    #define GLOBALINIT1(x)    (x)
29    #define GLOBALINIT2(x, y) (x, y)
30#else
31    #define GLOBALVAR extern
32    #define GLOBALINIT1(x)
33    #define GLOBALINIT2(x, y)
34#endif
35
36#include "random.h"
37#include "eraserdllinternal.h"
38// the context array
39//
40GLOBALVAR CEraserContext *eraserContextArray[ERASER_MAX_CONTEXT + 1];
41// this is so the library can be accessed from multiple threads
42GLOBALVAR CCriticalSection csContextArray;
43
44// helpers for context access control
45#define eraserContextArrayAccess() \
46    eraserTraceLock("eraserContextArrayAccess\n"); \
47    CSingleLock sl(&csContextArray, TRUE)
48#define eraserContextArrayRelease() \
49    eraserTraceLock("eraserContextArrayRelease\n"); \
50    sl.Unlock()
51#define eraserContextArrayRelock() \
52    eraserTraceLock("eraserContextArrayRelock\n"); \
53    sl.Lock()
54
55// initialization control
56//
57GLOBALVAR CCriticalSection csReferenceCount;
58GLOBALVAR E_UINT16 uReferenceCount GLOBALINIT1(0);
59GLOBALVAR CEvent evLibraryInitialized GLOBALINIT2(FALSE, TRUE);
60
61// helpers
62#define eraserIsLibraryInit() \
63    (WaitForSingleObject(evLibraryInitialized, 0) == WAIT_OBJECT_0)
64
65#define eraserLibraryInit() \
66    evLibraryInitialized.SetEvent(); \
67    csReferenceCount.Lock(); \
68    uReferenceCount++; \
69    csReferenceCount.Unlock()
70
71#define eraserLibraryUninit() \
72    csReferenceCount.Lock(); \
73    if (uReferenceCount > 0) { \
74        uReferenceCount--; \
75    } \
76    if (uReferenceCount == 0) { \
77        evLibraryInitialized.ResetEvent(); \
78    } \
79    csReferenceCount.Unlock()
80#define eraserLibraryUnlock() \
81    csReferenceCount.Lock(); \
82    uReferenceCount = 0; \
83    evLibraryInitialized.ResetEvent(); \
84    csReferenceCount.Unlock()
85
86
87// other global variables
88//
89GLOBALVAR bool isWindowsNT;
90bool ERASER_API IsWindowsNT();
91
92const E_UINT16 uExceptionBufferSize = 127;
93GLOBALVAR TCHAR szExceptionBuffer[uExceptionBufferSize];
94
95
96// safe characters for filenames (excluded some that are not that so common)
97//
98const E_UINT16 ERASER_SAFEARRAY_SIZE = 36;
99const LPCTSTR ERASER_SAFEARRAY = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
100
101inline void createRandomFileName(LPTSTR szFileName, E_UINT16 uNameLength, E_UINT16 uExtLength, E_UINT16 uCounter)
102{
103    // szFileName needs to be at least (uNameLength + 1 + uExtLength + 1) bytes long
104    try {
105        E_UINT32 uLength = uNameLength;
106
107        if (uExtLength > 0) {
108            uLength += uExtLength + 1;
109        }
110
111        if (uLength >= MAX_PATH) {
112            uLength = MAX_PATH - 1;
113        }
114
115        E_UINT8 uRandomArray[MAX_PATH];
116        isaacFill(uRandomArray, MAX_PATH);
117
118        for (E_UINT32 uIndex = 0; uIndex < uLength; uIndex++) {
119            szFileName[uIndex] = ERASER_SAFEARRAY[uRandomArray[uIndex] % ERASER_SAFEARRAY_SIZE];
120        }
121
122        ZeroMemory(uRandomArray, MAX_PATH);
123
124        if (uCounter > 0 && uNameLength >= 4) {
125            _snprintf(&szFileName[uNameLength - 4], 4, "%04X", uCounter);
126        }
127
128        if (uExtLength > 0) {
129            szFileName[uNameLength] = '.';
130        }
131
132        szFileName[uLength] = 0;
133    } catch (...) {
134        ASSERT(0);
135    }
136}
137
138const E_UINT16 uShortFileNameLength = 8 + 1 + 3;
139#define createRandomShortFileName(szFileName, uCounter) \
140    createRandomFileName((szFileName), 8, 3, (uCounter))
141
142
143// common helpers for all wipe functions to handle notification and
144// statistics
145//
146inline void
147countTotalProgressTasks(CEraserContext *context)
148{
149    // erasing unused disk space is divided to three steps when
150    // it comes to showing total progress
151
152    context->m_uProgressTasks = 0;
153    if (bitSet(context->m_lsSettings.m_uItems, diskClusterTips)) {
154        context->m_uProgressTasks++;
155    }
156    if (bitSet(context->m_lsSettings.m_uItems, diskFreeSpace)) {
157        context->m_uProgressTasks++;
158        if (isWindowsNT && isFileSystemNTFS(context->m_piCurrent)) {  // MFT records
159            context->m_uProgressTasks++;
160        }
161    }
162    if (bitSet(context->m_lsSettings.m_uItems, diskDirEntries)) {
163        context->m_uProgressTasks++;
164    }
165    if (context->m_uProgressTasks < 1) {
166        context->m_uProgressTasks = 1;
167    }
168}
169
170#pragma warning(disable : 4244)
171
172inline void
173increaseTotalProgressPercent(CEraserContext *context)
174{
175    // one task has been completed, increase m_uProgressTaskPercent
176    if (context->m_uProgressTasks > 1) {
177        context->m_uProgressTaskPercent += 100 / context->m_uProgressTasks;
178    }
179}
180
181#pragma warning(default : 4244)
182
183inline void
184setTotalProgress(CEraserContext *context)
185{
186    eraserContextAccess(context);
187
188    if (context->m_edtDataType == ERASER_DATA_FILES) {
189        context->m_uProgressTotalPercent =
190            (E_UINT8)( ((context->m_uProgressWipedFiles * 100) / context->m_uProgressFiles) +
191                       (context->m_uProgressPercent / context->m_uProgressFiles));
192    } else {
193        context->m_uProgressTotalPercent =
194            (E_UINT8)( ((context->m_uProgressWipedDrives * 100 + context->m_uProgressTaskPercent) /
195                            context->m_uProgressDrives) +
196                       (context->m_uProgressPercent / (context->m_uProgressTasks * context->m_uProgressDrives)));
197    }
198}
199
200inline void
201postStartNotification(CEraserContext *context)
202{
203    // send update only when starting the overwriting
204    if (!bitSet(context->m_uProgressFlags, progressCustom) && context->m_uProgressWiped == 0) {
205        eraserBeginNotify(context);
206    }
207}
208
209inline void
210postUpdateNotification(CEraserContext *context, E_UINT16 passes)
211{
212    if (!bitSet(context->m_uProgressFlags, progressCustom)) {
213        eraserContextAccess(context);
214
215        context->m_uProgressPercent =
216            (E_UINT8)(((E_UINT64)(context->m_uProgressWiped * 100)) /
217                     ((E_UINT64)(context->m_uProgressSize * passes)));
218
219        setTotalProgress(context);
220
221        E_UINT32 uTickCount = GetTickCount();
222        if (uTickCount > context->m_uProgressStartTime) {
223            E_UINT64 uSpeed =
224                context->m_uProgressWiped / (E_UINT64)(uTickCount - context->m_uProgressStartTime);
225
226            if (uSpeed > 0) {
227                context->m_uProgressTimeLeft =
228                    (E_UINT32)(((context->m_uProgressSize * passes) - context->m_uProgressWiped) /
229                             (uSpeed * 1000));
230            } else {
231                context->m_uProgressTimeLeft = 0;
232            }
233        }
234
235        eraserUpdateNotifyNoAccess(context);
236    }
237}
238
239inline void
240setEndStatistics(CEraserContext *context, E_UINT64& uWiped, E_UINT32& uPrevTime)
241{
242    E_UINT32 uTickCount = GetTickCount();
243
244    // wiped area
245    context->m_uStatWiped += uWiped;
246
247    // if the given area was completely overwritten at least once
248    if (uWiped >= context->m_uiFileSize.QuadPart) {
249        context->m_uStatErasedArea += context->m_uiFileSize.QuadPart;
250        context->m_uStatTips += context->m_uClusterSpace;
251    } else {
252        // the whole area was not even once completely overwritten
253        context->m_uStatErasedArea += uWiped;
254
255        if (context->m_uClusterSpace > 0 &&
256            uWiped > (context->m_uiFileSize.QuadPart - context->m_uClusterSpace)) {
257            // this is how much of the cluster tips actually was overwritten
258            context->m_uStatTips += context->m_uiFileSize.QuadPart -
259                                    context->m_uClusterSpace;
260        }
261    }
262
263    // time
264    if (uTickCount > uPrevTime) {
265        context->m_uStatTime += (uTickCount - uPrevTime);
266    }
267}
268
269
270#endif
Note: See TracBrowser for help on using the repository browser.