1 |
/* wptMDSumDlg.cpp |
2 |
* Copyright (C) 2003, 2005, 2006 Timo Schulz |
3 |
* |
4 |
* This file is part of WinPT. |
5 |
* |
6 |
* WinPT is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* WinPT 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 WinPT; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
#ifdef HAVE_CONFIG_H |
21 |
#include <config.h> |
22 |
#endif |
23 |
|
24 |
#include <windows.h> |
25 |
#include <stdio.h> |
26 |
|
27 |
#include "resource.h" |
28 |
#include "wptTypes.h" |
29 |
#include "wptW32API.h" |
30 |
#include "wptGPG.h" |
31 |
#include "wptCommonCtl.h" |
32 |
#include "wptContext.h" |
33 |
#include "wptNLS.h" |
34 |
#include "wptErrors.h" |
35 |
|
36 |
|
37 |
/* Return a printable digest of the buffer @mdbuf. */ |
38 |
static const char* |
39 |
printable_digest (byte *mdbuf, size_t n) |
40 |
{ |
41 |
static char mdasc[128]; |
42 |
size_t i; |
43 |
|
44 |
for (i = 0; i < n; i++) |
45 |
sprintf (mdasc+2*i, "%02X", mdbuf[i]); |
46 |
return mdasc; |
47 |
} |
48 |
|
49 |
|
50 |
static const char* |
51 |
id2algo (gpgme_hash_algo_t mdalgo) |
52 |
{ |
53 |
switch (mdalgo) { |
54 |
case GPGME_MD_MD5: return "MD5"; |
55 |
case GPGME_MD_SHA1: return "SHA1"; |
56 |
case GPGME_MD_RMD160: return "RMD160"; |
57 |
case GPGME_MD_SHA256: return "SHA256"; |
58 |
default: return ""; |
59 |
} |
60 |
return ""; |
61 |
} |
62 |
|
63 |
|
64 |
/* Hash the selected file from the FM listview control in @md. |
65 |
Add the results to the listview @lv. */ |
66 |
static void |
67 |
hash_selected_files (md_file_s *md, listview_ctrl_t lv) |
68 |
{ |
69 |
BYTE mdbuf[32]; |
70 |
char fname[256+1]; |
71 |
int i; |
72 |
size_t n; |
73 |
|
74 |
for (i = 0; i < listview_count_items( md->lv, 0 ); i++) { |
75 |
if( listview_get_item_state (md->lv, i)) { |
76 |
listview_get_item_text (md->lv, i, 1, fname, sizeof (fname)-1); |
77 |
if (!gpg_md_hash_file (md->mdalgo, fname, mdbuf, &n)) { |
78 |
listview_add_item (lv, ""); |
79 |
listview_add_sub_item (lv, 0, 0, printable_digest (mdbuf, n)); |
80 |
listview_add_sub_item (lv, 0, 1, fname); |
81 |
} |
82 |
} |
83 |
} |
84 |
} |
85 |
|
86 |
|
87 |
static int |
88 |
tool_avail (gpgme_hash_algo_t mdalgo) |
89 |
{ |
90 |
if (mdalgo == GPGME_MD_SHA1 || mdalgo == GPGME_MD_MD5) |
91 |
return 1; |
92 |
return 0; |
93 |
} |
94 |
|
95 |
|
96 |
/* Dialog box procedure to show and calculate file digests. */ |
97 |
BOOL CALLBACK |
98 |
mdsum_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
99 |
{ |
100 |
static listview_ctrl_t lv; |
101 |
static struct md_file_s *md; |
102 |
struct listview_column_s cols[] = { |
103 |
{0, 264, (char *)_("Digest")}, |
104 |
{1, 128, (char *)_("Name")}, |
105 |
{0, 0, 0} |
106 |
}; |
107 |
gpgme_data_t sumlist; |
108 |
const char *name, *algname; |
109 |
char fname[300], mdasc[128]; |
110 |
int i; |
111 |
|
112 |
switch( msg ) { |
113 |
case WM_INITDIALOG: |
114 |
md = (md_file_s *)lparam; |
115 |
if (!md) |
116 |
BUG (NULL); |
117 |
if( listview_new (&lv)) |
118 |
BUG (NULL); |
119 |
lv->ctrl = GetDlgItem (dlg, IDC_MDSUM_LIST); |
120 |
for (i = 0; i < cols[i].width; i++) |
121 |
listview_add_column( lv, &cols[i]); |
122 |
hash_selected_files (md, lv); |
123 |
SetDlgItemText (dlg, IDC_MDSUM_COPY, _("&Save...")); |
124 |
SetDlgItemText (dlg, IDOK, _("&Close")); |
125 |
SetDlgItemText (dlg, IDC_MDSUM_TOCLIP, _("Save to clipboard")); |
126 |
SetWindowText (dlg, _("Print Message Digest")); |
127 |
SetForegroundWindow (dlg); |
128 |
break; |
129 |
|
130 |
case WM_SYSCOMMAND: |
131 |
if (wparam == SC_CLOSE) |
132 |
EndDialog (dlg, TRUE); |
133 |
return TRUE; |
134 |
|
135 |
case WM_DESTROY: |
136 |
if (lv) { |
137 |
listview_release (lv); |
138 |
lv = NULL; |
139 |
} |
140 |
break; |
141 |
|
142 |
case WM_COMMAND: |
143 |
switch (LOWORD (wparam)) { |
144 |
case IDOK: |
145 |
EndDialog (dlg, TRUE); |
146 |
break; |
147 |
|
148 |
case IDCANCEL: |
149 |
EndDialog (dlg, FALSE); |
150 |
break; |
151 |
|
152 |
case IDC_MDSUM_COPY: |
153 |
algname = id2algo ((gpgme_hash_algo_t)md->mdalgo); |
154 |
if (gpgme_data_new (&sumlist)) |
155 |
BUG(0); |
156 |
if (!tool_avail ((gpgme_hash_algo_t)md->mdalgo)) { |
157 |
const char *s; |
158 |
|
159 |
s = "#warning '"; |
160 |
gpgme_data_write (sumlist, s, strlen (s)); |
161 |
gpgme_data_write (sumlist, algname, strlen (algname)); |
162 |
s = " ' sum is not yet available\r\n"; |
163 |
gpgme_data_write (sumlist, s, strlen (s)); |
164 |
} |
165 |
for (i = 0; i < listview_count_items (lv, 0); i++) { |
166 |
listview_get_item_text (lv, i, 0, mdasc, DIM (mdasc)-1); |
167 |
listview_get_item_text (lv, i, 1, fname, DIM (fname)-1); |
168 |
|
169 |
gpgme_data_write (sumlist, mdasc, strlen (mdasc)); |
170 |
gpgme_data_write (sumlist, " ", 1); |
171 |
gpgme_data_write (sumlist, fname, strlen (fname)); |
172 |
gpgme_data_write (sumlist, "\r\n", 2); |
173 |
} |
174 |
if (IsDlgButtonChecked (dlg, IDC_MDSUM_TOCLIP)) { |
175 |
gpg_data_release_and_set_clipboard (sumlist, 0); |
176 |
break; |
177 |
} |
178 |
_snprintf (fname, sizeof (fname)-1, "%s_sums.txt", algname); |
179 |
name = get_filesave_dlg (dlg, _("Select file to save checksums"), |
180 |
NULL, fname); |
181 |
if (name && *name) { |
182 |
gpg_data_release_and_set_file (sumlist, name); |
183 |
log_box (_("File Manager"), MB_OK, |
184 |
_("Checksums successfully saved in '%s'"), name); |
185 |
} |
186 |
else |
187 |
gpgme_data_release (sumlist); |
188 |
break; |
189 |
} |
190 |
break; |
191 |
} |
192 |
return FALSE; |
193 |
} |