-
Notifications
You must be signed in to change notification settings - Fork 78
/
DlgEaEditor.cpp
427 lines (352 loc) · 12.4 KB
/
DlgEaEditor.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*****************************************************************************/
/* DlgEaEditor.cpp Copyright (c) Ladislav Zezula 2005 */
/*---------------------------------------------------------------------------*/
/* Description : */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 16.08.05 1.00 Lad Created */
/*****************************************************************************/
#include "FileTest.h"
#include "resource.h"
//-----------------------------------------------------------------------------
// Local functions
static int nChangingEdit = 0;
static DWORD TwoDigitsToBinValue(LPCTSTR szBinValue)
{
TCHAR chDigit1;
TCHAR chDigit2;
// Verify the digits
if(!isxdigit(szBinValue[0]) || !isxdigit(szBinValue[1]))
return (DWORD)0x100;
chDigit2 = (TCHAR)(UINT_PTR)CharUpper((LPTSTR)szBinValue[0]);
chDigit1 = (TCHAR)(UINT_PTR)CharUpper((LPTSTR)szBinValue[1]);
if(chDigit1 > '9')
chDigit1 -= 'A' - '9' - 1;
if(chDigit2 > '9')
chDigit2 -= 'A' - '9' - 1;
return ((chDigit2 - '0') << 0x04) | (chDigit1 - '0');
}
static int CTextToBinArray(LPTSTR szTextValue, LPBYTE pbBinValue)
{
DWORD dwBinValue;
int nLength = 0;
while(*szTextValue != 0)
{
if(*szTextValue == _T('\\'))
{
szTextValue++;
switch(*szTextValue)
{
case _T('\\'):
*pbBinValue = '\\';
break;
case _T('x'):
case _T('X'):
szTextValue++;
dwBinValue = TwoDigitsToBinValue(szTextValue);
if(dwBinValue == 0x0100)
return -1;
*pbBinValue = (BYTE)dwBinValue;
szTextValue++;
break;
}
}
else
*pbBinValue = (BYTE)(*szTextValue);
szTextValue++;
pbBinValue++;
nLength++;
}
return nLength;
}
static int BinTextToBinArray(LPTSTR szBinValue, LPBYTE pbBinValue)
{
DWORD dwBinValue;
int nLength = 0;
while(*szBinValue != 0)
{
// Skip spaces
while(0 < *szBinValue && *szBinValue <= 0x20)
szBinValue++;
dwBinValue = TwoDigitsToBinValue(szBinValue);
if(dwBinValue == 0x0100)
return -1;
*pbBinValue++ = (BYTE)dwBinValue;
szBinValue += 2;
nLength++;
// Must be a space there
if(*szBinValue > 0x20)
return -1;
// Skip spaces
while(0 < *szBinValue && *szBinValue <= 0x20)
szBinValue++;
}
return nLength;
}
static int BinArrayToBinText(LPBYTE pbBinValue, int nBinLength, LPTSTR szBinValue, size_t cchBinValue)
{
LPTSTR szBinValueEnd = szBinValue + cchBinValue;
for(int i = 0; i < nBinLength; i++, pbBinValue++)
{
StringCchPrintf(szBinValue, (szBinValueEnd - szBinValue), _T("%02lX "), (*pbBinValue & 0x000000FF));
szBinValue += 3;
}
*szBinValue = 0;
return nBinLength;
}
static int BinArrayToCText(LPBYTE pbBinValue, int nBinLength, LPTSTR szTextValue, size_t cchTextLength)
{
LPTSTR szTextEnd = szTextValue + cchTextLength;
for(int i = 0; i < nBinLength; i++, pbBinValue++)
{
if(0x20 <= *pbBinValue && *pbBinValue < 0x80)
{
*szTextValue++ = (TCHAR)*pbBinValue;
}
else
{
StringCchPrintf(szTextValue, (szTextEnd - szTextValue), _T("\\x%02lX"), (*pbBinValue & 0x000000FF));
szTextValue += 4;
}
}
*szTextValue = 0;
return nBinLength;
}
static void UpdateDialog(HWND hDlg)
{
BOOL bEnable;
nChangingEdit++;
bEnable = (IsDlgButtonChecked(hDlg, IDC_RADIO1) == BST_CHECKED);
EnableWindow(GetDlgItem(hDlg, IDC_DATA_VALUE_TEXT), bEnable);
bEnable = (IsDlgButtonChecked(hDlg, IDC_RADIO2) == BST_CHECKED);
EnableWindow(GetDlgItem(hDlg, IDC_DATA_VALUE_BIN), bEnable);
nChangingEdit--;
}
static void SetDlgItemCText(HWND hDlg, UINT nIDCtrl, LPBYTE pbData, int nLength)
{
LPTSTR szBinText;
size_t cchLength = nLength * 4 + 1;
szBinText = new TCHAR[cchLength];
if(szBinText != NULL)
{
BinArrayToCText(pbData, nLength, szBinText, cchLength);
SetDlgItemText(hDlg, nIDCtrl, szBinText);
delete [] szBinText;
}
}
static void SetDlgItemBin(HWND hDlg, UINT nIDCtrl, LPBYTE pbData, int nLength)
{
LPTSTR szBinText;
size_t cchLength = nLength * 4 + 1;
szBinText = new TCHAR[cchLength];
if(szBinText != NULL)
{
BinArrayToBinText(pbData, nLength, szBinText, cchLength);
SetDlgItemText(hDlg, nIDCtrl, szBinText);
delete [] szBinText;
}
}
// Text value has changed
static int UpdateEaValueBin(HWND hDlg)
{
LPTSTR szTextValue;
LPTSTR szBinValue;
LPBYTE pbBinValue;
HWND hSrcEdit = GetDlgItem(hDlg, IDC_DATA_VALUE_TEXT);
HWND hTrgEdit = GetDlgItem(hDlg, IDC_DATA_VALUE_BIN);
int nTextLength = GetWindowTextLength(hSrcEdit);
int cchTextLength = nTextLength * 3 + 1;
int nBinLength;
szTextValue = new TCHAR[nTextLength + 1];
szBinValue = new TCHAR[cchTextLength];
pbBinValue = new BYTE[nTextLength + 1];
GetWindowText(hSrcEdit, szTextValue, nTextLength + 1);
// Process the binary buffer and convert it to the binary data
nChangingEdit++;
nBinLength = CTextToBinArray(szTextValue, pbBinValue);
if(nBinLength != -1)
{
BinArrayToBinText(pbBinValue, nBinLength, szBinValue, cchTextLength);
SetWindowText(hTrgEdit, szBinValue);
}
else
SetWindowTextRc(hTrgEdit, IDS_CONVERSION_ERROR);
nChangingEdit--;
delete [] pbBinValue;
delete [] szBinValue;
delete [] szTextValue;
return TRUE;
}
// Binary value has changed
static int UpdateEaValueText(HWND hDlg)
{
LPTSTR szTextValue;
LPTSTR szBinValue;
LPBYTE pbBinValue;
HWND hSrcEdit = GetDlgItem(hDlg, IDC_DATA_VALUE_BIN);
HWND hTrgEdit = GetDlgItem(hDlg, IDC_DATA_VALUE_TEXT);
int nTextLength = GetWindowTextLength(hSrcEdit);
int cchTextLength = nTextLength * 4 + 1;
int nBinLength;
szTextValue = new TCHAR[cchTextLength];
szBinValue = new TCHAR[nTextLength + 1];
pbBinValue = new BYTE[nTextLength + 1];
GetWindowText(hSrcEdit, szBinValue, nTextLength + 1);
CharUpper(szBinValue);
// Process the binary buffer and convert it to the binary data
nChangingEdit++;
nBinLength = BinTextToBinArray(szBinValue, pbBinValue);
if(nBinLength != -1)
{
BinArrayToCText(pbBinValue, nBinLength, szTextValue, cchTextLength);
SetWindowText(hTrgEdit, szTextValue);
}
else
SetWindowTextRc(hTrgEdit, IDS_CONVERSION_ERROR);
nChangingEdit--;
delete [] pbBinValue;
delete [] szBinValue;
delete [] szTextValue;
return TRUE;
}
//-----------------------------------------------------------------------------
// Dialog handlers
static int OnInitDialog(HWND hDlg, LPARAM lParam)
{
PFILE_FULL_EA_INFORMATION * ppEaItem = (PFILE_FULL_EA_INFORMATION *)lParam;
PFILE_FULL_EA_INFORMATION pEaItem = *ppEaItem;
HWND hWndChild;
// Configure the dialog
SetWindowLongPtr(hDlg, DWLP_USER, lParam);
SetDialogIcon(hDlg, IDI_FILE_TEST);
CenterWindowToParent(hDlg);
// Set the dialog title to "Insert" or to "Edit"
SetWindowTextRc(hDlg, pEaItem ? IDS_EDIT_EA_TITLE : IDS_INSERT_EA_TITLE);
// Set the name and value limit
hWndChild = GetDlgItem(hDlg, IDC_DATA_NAME);
if(hWndChild != NULL)
Edit_LimitText(hWndChild, (BYTE)-1);
// Fill the dialog
nChangingEdit++;
CheckDlgButton(hDlg, IDC_RADIO1, BST_CHECKED);
if(pEaItem != NULL)
{
if(hWndChild != NULL)
SetWindowTextA(hWndChild, pEaItem->EaName);
SetDlgItemCText(hDlg,
IDC_DATA_VALUE_TEXT,
(LPBYTE)pEaItem->EaName + pEaItem->EaNameLength + 1,
pEaItem->EaValueLength);
SetDlgItemBin(hDlg,
IDC_DATA_VALUE_BIN,
(LPBYTE)pEaItem->EaName + pEaItem->EaNameLength + 1,
pEaItem->EaValueLength);
}
UpdateDialog(hDlg);
nChangingEdit--;
return TRUE;
}
static BOOL OnSaveDialog(HWND hDlg)
{
PFILE_FULL_EA_INFORMATION * PtrReturnEa = (PFILE_FULL_EA_INFORMATION *)GetWindowLongPtr(hDlg, DWLP_USER);
PFILE_FULL_EA_INFORMATION NewEaItem = NULL;
PFILE_FULL_EA_INFORMATION OldEaItem = PtrReturnEa[0];
LPTSTR szBinBuffer = NULL;
HWND hWndName = GetDlgItem(hDlg, IDC_DATA_NAME);
HWND hWndValue = GetDlgItem(hDlg, IDC_DATA_VALUE_BIN);
int nTotalLength;
int nValueLength = GetWindowTextLength(hWndValue);
int nNameLength = 0;
if(hWndName != NULL)
{
// Retrieve the name length
nNameLength = GetWindowTextLength(hWndName);
if(nNameLength == 0)
{
// Don't accept empty name
MessageBoxRc(hDlg, IDS_ERROR, IDS_NO_EA_NAME);
SetFocus(hWndName);
return FALSE;
}
// Make sure that the name length is not bigger than 255
if(nNameLength > 255)
nNameLength = 255;
}
// Create new EA entry
nTotalLength = sizeof(FILE_FULL_EA_INFORMATION) + nNameLength + nValueLength + 2;
szBinBuffer = new TCHAR[nValueLength + 1];
NewEaItem = (PFILE_FULL_EA_INFORMATION)(new char[nTotalLength]);
// Initialize the EA entry
ZeroMemory(NewEaItem, nTotalLength);
// Fill the name and value
if(hWndName != NULL)
GetWindowTextA(hWndName, NewEaItem->EaName, nNameLength + 1);
NewEaItem->EaNameLength = (UCHAR)nNameLength;
GetWindowText(hWndValue, szBinBuffer, nValueLength + 1);
NewEaItem->EaValueLength = (USHORT)BinTextToBinArray(szBinBuffer,
(LPBYTE)NewEaItem->EaName + NewEaItem->EaNameLength + 1);
if(NewEaItem->EaValueLength == (USHORT)-1)
{
MessageBoxRc(hDlg, IDS_ERROR, IDS_CONVERSION_ERROR_MSG);
delete [] szBinBuffer;
delete [] NewEaItem;
return FALSE;
}
// Set the correct length of the EA item
NewEaItem->NextEntryOffset = GetEaEntrySize(NewEaItem);
// Replace the item
if(OldEaItem != NULL)
delete OldEaItem;
PtrReturnEa[0] = NewEaItem;
delete [] szBinBuffer;
return TRUE;
}
static int OnCommand(HWND hDlg, UINT nNotify, UINT nIDCtrl)
{
if(nNotify == BN_CLICKED)
{
switch(nIDCtrl)
{
case IDC_RADIO1:
UpdateDialog(hDlg);
SetFocus(GetDlgItem(hDlg, IDC_DATA_VALUE_TEXT));
return TRUE;
case IDC_RADIO2:
UpdateDialog(hDlg);
SetFocus(GetDlgItem(hDlg, IDC_DATA_VALUE_BIN));
return TRUE;
case IDOK:
if(OnSaveDialog(hDlg) == FALSE)
return TRUE;
// No break here !!
case IDCANCEL:
EndDialog(hDlg, nIDCtrl);
break;
}
}
if(nNotify == EN_CHANGE && nChangingEdit == 0)
{
switch(nIDCtrl)
{
case IDC_DATA_VALUE_BIN:
return UpdateEaValueText(hDlg);
case IDC_DATA_VALUE_TEXT:
return UpdateEaValueBin(hDlg);
}
}
return FALSE;
}
static INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// Dialog initialization
if(uMsg == WM_INITDIALOG)
return OnInitDialog(hDlg, lParam);
if(uMsg == WM_COMMAND)
return OnCommand(hDlg, HIWORD(wParam), LOWORD(wParam));
return FALSE;
}
INT_PTR EaEditorDialog(HWND hParent, PFILE_FULL_EA_INFORMATION * ppEaItem)
{
return DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_EA_EDITOR), hParent, DialogProc, (LPARAM)ppEaItem);
}