-
Notifications
You must be signed in to change notification settings - Fork 3
/
locknote.cpp
303 lines (257 loc) · 8.27 KB
/
locknote.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Steganos LockNote - self-modifying encrypted notepad
// Copyright (C) 2006-2010 Steganos GmbH
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "stdafx.h"
#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>
#include <process.h>
#include <atlfile.h>
#include "resource.h"
#include "locknoteView.h"
#include "aboutdlg.h"
#include "passworddlg.h"
#include "MainFrm.h"
#include "aesphm.cpp"
CAppModule _Module;
int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
TCHAR szModulePath[MAX_PATH] = {'\0'};
::GetModuleFileName(Utils::GetModuleHandle(), szModulePath, MAX_PATH);
CMessageLoop theLoop;
_Module.AddMessageLoop(&theLoop);
if (__argc == 3)
{
char* lpszCommand = __argv[1];
char* lpszPath = __argv[2];
if (!_tcscmp(lpszCommand, _T("-writeback")))
{
while (!::CopyFile(szModulePath, lpszPath, FALSE))
{
// wait and retry, maybe the process is still in use
Sleep(100);
}
_tspawnl(_P_NOWAIT, lpszPath, Utils::Quote(lpszPath).c_str(), _T("-erase"), Utils::Quote(szModulePath).c_str(), NULL);
return 0;
}
else if (!_tcscmp(lpszCommand,_T("-erase")))
{
while (PathFileExists(lpszPath) && !::DeleteFile(lpszPath))
{
// wait and retry, maybe the process is still in use
Sleep(100);
}
return 0;
}
}
if (__argc > 1)
{
int nResult = Utils::MessageBox(NULL, STR(IDS_ASK_CONVERT_FILES), MB_YESNO | MB_ICONQUESTION);
if (nResult == IDYES)
{
int nConverted = 0;
std::string encryptPassword;
std::list<std::string> decryptPasswords;
for (int nIndex = 1; nIndex < __argc; nIndex++)
{
std::string filename = __argv[nIndex];
UINT uLength = _tcslen(filename.c_str());
std::string extension = &filename[uLength-4];
std::string newfilename = filename;
strlwr((char*)extension.c_str());
if (extension == _T(".txt"))
{
_tcscpy(&newfilename[uLength-4], _T(".exe"));
std::string text;
std::string password;
if (LoadTextFromFile(filename, text, password))
{
if (SaveTextToFile(newfilename, text, encryptPassword))
{
nConverted += 1;
}
}
}
/*
// convert SLIM back to text
else if (extension == _T(".exe"))
{
_tcscpy(&newfilename[uLength-4], _T(".txt"));
}
*/
else
{
std::string text;
text.resize(32768);
sprintf((char*)text.c_str(), STR(IDS_CANT_CONVERT).c_str(), filename.c_str());
Utils::MessageBox(NULL, text, MB_OK | MB_ICONERROR);
}
}
if (nConverted)
{
std::string text;
text.resize(32768);
sprintf((char*)text.c_str(), STR(IDS_CONVERT_DONE).c_str(), nConverted);
Utils::MessageBox(NULL, text, MB_OK | MB_ICONINFORMATION);
}
}
return 0;
}
TCHAR szFileMappingName[MAX_PATH];
strcpy(szFileMappingName, szModulePath);
TCHAR* szChar = szFileMappingName;
while (*szChar)
{
if ((*szChar == '\\')||(*szChar == ':'))
{
*szChar = '_';
}
szChar++;
};
CAtlFileMapping<HWND> fmSingleInstanceHWND;
BOOL bAlreadyExisted = FALSE;
fmSingleInstanceHWND.MapSharedMem(sizeof(HWND), szFileMappingName, &bAlreadyExisted);
if (bAlreadyExisted)
{
HWND hWndMain = *fmSingleInstanceHWND;
::SetForegroundWindow(hWndMain);
return 0;
}
std::string text;
std::string data;
std::string password;
Utils::LoadResource(_T("CONTENT"), _T("PAYLOAD"), data);
if (_tcslen(data.c_str()))
{
password = GetPasswordDlg();
if (!_tcslen(password.c_str()))
return -1;
if (!Utils::DecryptString(data, password, text))
{
MessageBox(NULL, STR(IDS_INVALID_PASSWORD), MB_OK | MB_ICONERROR);
return -1;
}
}
else
{
text = STR(IDS_WELCOME);
}
CMainFrame wndMain;
wndMain.m_password = password;
wndMain.m_text = text;
// get window sizes from resource
std::string strSizeX;
std::string strSizeY;
Utils::LoadResource(_T("SIZEX"), _T("INFORMATION"), strSizeX);
Utils::LoadResource(_T("SIZEY"), _T("INFORMATION"), strSizeY);
if (_tcslen(strSizeX.c_str()) && _tcslen(strSizeY.c_str()))
{
wndMain.m_nWindowSizeX = atoi(strSizeX.c_str());
wndMain.m_nWindowSizeY = atoi(strSizeY.c_str());
}
// get font size
std::string fontsize;
Utils::LoadResource(_T("FONTSIZE"), _T("INFORMATION"), fontsize);
if (_tcslen(fontsize.c_str()))
{
// fontsize format: 10 (example)
wndMain.m_nFontSize = atoi(fontsize.c_str());
}
else
{
wndMain.m_nFontSize = 10;
}
// get font set
std::string fontface;
Utils::LoadResource(_T("TYPEFACE"), _T("INFORMATION"), fontface);
if (_tcslen(fontface.c_str()))
{
// font typeface format: "Lucida Console" (example)
wndMain.m_strFontName = fontface;
}
else
{
wndMain.m_strFontName = NAME_FONT_LUCIDA_CONSOLE;
}
if(wndMain.CreateEx() == NULL)
{
ATLTRACE(_T("Main window creation failed!\n"));
return 0;
}
wndMain.CheckFontSize();
wndMain.CheckFontTypeFace();
*fmSingleInstanceHWND = wndMain.m_hWnd;
wndMain.ShowWindow(nCmdShow);
int nRet = theLoop.Run();
_Module.RemoveMessageLoop();
if (((wndMain.m_text != text) || (wndMain.m_password != password) || wndMain.m_bTraitsChanged) && (_tcslen(wndMain.m_password.c_str())))
{
password = wndMain.m_password;
data = _T("");
if (_tcslen(wndMain.m_text.c_str()))
{
Utils::EncryptString(wndMain.m_text, password, data);
}
else
{
MessageBox(NULL, STR(IDS_TEXT_IS_ENCRYPTED), MB_OK | MB_ICONINFORMATION);
}
TCHAR szModulePath[MAX_PATH] = {'\0'};
TCHAR szTempPath[MAX_PATH] = {'\0'};
TCHAR szFileName[MAX_PATH] = {'\0'};
::GetModuleFileName(Utils::GetModuleHandle(), szModulePath, MAX_PATH);
::GetTempPath(MAX_PATH, szTempPath);
::GetTempFileName(szTempPath, _T("STG"), 0, szFileName);
::CopyFile(szModulePath, szFileName, FALSE);
Utils::UpdateResource(szFileName, _T("CONTENT"), _T("PAYLOAD"), data);
// write window sizes to resource
std::string sizeinfo;
char szSizeInfo[MAX_PATH] = "";
sprintf(szSizeInfo, "%d", wndMain.m_nWindowSizeX);
sizeinfo = szSizeInfo;
Utils::UpdateResource(szFileName, _T("SIZEX"), _T("INFORMATION"), sizeinfo);
sprintf(szSizeInfo, "%d", wndMain.m_nWindowSizeY);
sizeinfo = szSizeInfo;
Utils::UpdateResource(szFileName, _T("SIZEY"), _T("INFORMATION"), sizeinfo);
// write font size
std::string fontsize;
char szFontSize[MAX_PATH] = "";
sprintf(szFontSize, "%d", wndMain.m_nFontSize);
fontsize = szFontSize;
Utils::UpdateResource(szFileName, _T("FONTSIZE"), _T("INFORMATION"), fontsize);
// write font typeface
Utils::UpdateResource(szFileName, _T("TYPEFACE"), _T("INFORMATION"), wndMain.m_strFontName);
_tspawnl(_P_NOWAIT, szFileName, Utils::Quote(szFileName).c_str(), _T("-writeback"), Utils::Quote(szModulePath).c_str(), NULL);
}
return nRet;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
HRESULT hRes = ::CoInitialize(NULL);
// If you are running on NT 4.0 or higher you can use the following call instead to
// make the EXE free threaded. This means that calls come in on a random RPC thread.
// HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
ATLASSERT(SUCCEEDED(hRes));
// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
::DefWindowProc(NULL, 0, 0, 0L);
AtlInitCommonControls(ICC_BAR_CLASSES); // add flags to support other controls
hRes = _Module.Init(NULL, hInstance);
ATLASSERT(SUCCEEDED(hRes));
int nRet = Run(lpstrCmdLine, nCmdShow);
_Module.Term();
::CoUninitialize();
return nRet;
}