1 |
werner |
36 |
/* wptMDSumDlg.cpp |
2 |
twoaday |
165 |
* Copyright (C) 2003, 2005, 2006 Timo Schulz |
3 |
werner |
36 |
* |
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 |
twoaday |
99 |
|
27 |
werner |
47 |
#include "resource.h" |
28 |
werner |
36 |
#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 |
twoaday |
130 |
|
37 |
twoaday |
225 |
/* Symbolic column IDs. */ |
38 |
|
|
enum md_col_t {COL_MD=0, COL_NAME}; |
39 |
|
|
|
40 |
|
|
|
41 |
werner |
36 |
/* Return a printable digest of the buffer @mdbuf. */ |
42 |
|
|
static const char* |
43 |
twoaday |
225 |
printable_digest (BYTE *mdbuf, size_t n) |
44 |
werner |
36 |
{ |
45 |
twoaday |
204 |
static char mdasc[128]; |
46 |
werner |
36 |
size_t i; |
47 |
twoaday |
204 |
|
48 |
werner |
36 |
for (i = 0; i < n; i++) |
49 |
|
|
sprintf (mdasc+2*i, "%02X", mdbuf[i]); |
50 |
|
|
return mdasc; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
|
54 |
twoaday |
130 |
static const char* |
55 |
twoaday |
204 |
id2algo (gpgme_hash_algo_t mdalgo) |
56 |
twoaday |
130 |
{ |
57 |
twoaday |
204 |
switch (mdalgo) { |
58 |
twoaday |
130 |
case GPGME_MD_MD5: return "MD5"; |
59 |
|
|
case GPGME_MD_SHA1: return "SHA1"; |
60 |
|
|
case GPGME_MD_RMD160: return "RMD160"; |
61 |
|
|
case GPGME_MD_SHA256: return "SHA256"; |
62 |
twoaday |
225 |
default: break; |
63 |
twoaday |
130 |
} |
64 |
|
|
return ""; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
|
68 |
twoaday |
204 |
/* Hash the selected file from the FM listview control in @md. |
69 |
|
|
Add the results to the listview @lv. */ |
70 |
|
|
static void |
71 |
|
|
hash_selected_files (md_file_s *md, listview_ctrl_t lv) |
72 |
|
|
{ |
73 |
|
|
BYTE mdbuf[32]; |
74 |
|
|
char fname[256+1]; |
75 |
|
|
int i; |
76 |
|
|
size_t n; |
77 |
|
|
|
78 |
twoaday |
225 |
for (i = 0; i < listview_count_items (md->lv, 0); i++) { |
79 |
|
|
if (listview_get_item_state (md->lv, i)) { |
80 |
twoaday |
204 |
listview_get_item_text (md->lv, i, 1, fname, sizeof (fname)-1); |
81 |
|
|
if (!gpg_md_hash_file (md->mdalgo, fname, mdbuf, &n)) { |
82 |
|
|
listview_add_item (lv, ""); |
83 |
twoaday |
225 |
listview_add_sub_item (lv, 0, COL_MD, printable_digest (mdbuf, n)); |
84 |
|
|
listview_add_sub_item (lv, 0, COL_NAME, fname); |
85 |
twoaday |
204 |
} |
86 |
|
|
} |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
|
91 |
twoaday |
225 |
/* Return 1 if there is a tool available to check the file. |
92 |
|
|
(sha1sum, md5sum). */ |
93 |
twoaday |
204 |
static int |
94 |
|
|
tool_avail (gpgme_hash_algo_t mdalgo) |
95 |
|
|
{ |
96 |
|
|
if (mdalgo == GPGME_MD_SHA1 || mdalgo == GPGME_MD_MD5) |
97 |
|
|
return 1; |
98 |
|
|
return 0; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
|
102 |
twoaday |
225 |
|
103 |
|
|
|
104 |
werner |
36 |
/* Dialog box procedure to show and calculate file digests. */ |
105 |
|
|
BOOL CALLBACK |
106 |
|
|
mdsum_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
107 |
|
|
{ |
108 |
|
|
static listview_ctrl_t lv; |
109 |
twoaday |
204 |
static struct md_file_s *md; |
110 |
werner |
36 |
struct listview_column_s cols[] = { |
111 |
|
|
{0, 264, (char *)_("Digest")}, |
112 |
twoaday |
225 |
{1, 160, (char *)_("Name")}, |
113 |
werner |
36 |
{0, 0, 0} |
114 |
|
|
}; |
115 |
|
|
gpgme_data_t sumlist; |
116 |
twoaday |
204 |
const char *name, *algname; |
117 |
|
|
char fname[300], mdasc[128]; |
118 |
werner |
36 |
int i; |
119 |
|
|
|
120 |
twoaday |
225 |
switch (msg) { |
121 |
werner |
36 |
case WM_INITDIALOG: |
122 |
|
|
md = (md_file_s *)lparam; |
123 |
twoaday |
204 |
if (!md) |
124 |
|
|
BUG (NULL); |
125 |
twoaday |
208 |
listview_new (&lv, GetDlgItem (dlg, IDC_MDSUM_LIST)); |
126 |
twoaday |
204 |
for (i = 0; i < cols[i].width; i++) |
127 |
twoaday |
225 |
listview_add_column (lv, &cols[i]); |
128 |
twoaday |
204 |
hash_selected_files (md, lv); |
129 |
twoaday |
99 |
SetDlgItemText (dlg, IDC_MDSUM_COPY, _("&Save...")); |
130 |
twoaday |
119 |
SetDlgItemText (dlg, IDOK, _("&Close")); |
131 |
twoaday |
167 |
SetDlgItemText (dlg, IDC_MDSUM_TOCLIP, _("Save to clipboard")); |
132 |
twoaday |
99 |
SetWindowText (dlg, _("Print Message Digest")); |
133 |
|
|
SetForegroundWindow (dlg); |
134 |
werner |
36 |
break; |
135 |
|
|
|
136 |
|
|
case WM_DESTROY: |
137 |
twoaday |
204 |
if (lv) { |
138 |
|
|
listview_release (lv); |
139 |
werner |
36 |
lv = NULL; |
140 |
|
|
} |
141 |
|
|
break; |
142 |
|
|
|
143 |
|
|
case WM_COMMAND: |
144 |
|
|
switch (LOWORD (wparam)) { |
145 |
|
|
case IDOK: |
146 |
|
|
EndDialog (dlg, TRUE); |
147 |
|
|
break; |
148 |
|
|
|
149 |
twoaday |
204 |
case IDCANCEL: |
150 |
|
|
EndDialog (dlg, FALSE); |
151 |
|
|
break; |
152 |
|
|
|
153 |
werner |
36 |
case IDC_MDSUM_COPY: |
154 |
twoaday |
204 |
algname = id2algo ((gpgme_hash_algo_t)md->mdalgo); |
155 |
werner |
36 |
if (gpgme_data_new (&sumlist)) |
156 |
|
|
BUG(0); |
157 |
twoaday |
204 |
if (!tool_avail ((gpgme_hash_algo_t)md->mdalgo)) { |
158 |
|
|
const char *s; |
159 |
|
|
|
160 |
|
|
s = "#warning '"; |
161 |
werner |
36 |
gpgme_data_write (sumlist, s, strlen (s)); |
162 |
twoaday |
204 |
gpgme_data_write (sumlist, algname, strlen (algname)); |
163 |
|
|
s = " ' sum is not yet available\r\n"; |
164 |
|
|
gpgme_data_write (sumlist, s, strlen (s)); |
165 |
werner |
36 |
} |
166 |
|
|
for (i = 0; i < listview_count_items (lv, 0); i++) { |
167 |
twoaday |
225 |
listview_get_item_text (lv, i, COL_MD, mdasc, DIM (mdasc)-1); |
168 |
|
|
listview_get_item_text (lv, i, COL_NAME, fname, DIM (fname)-1); |
169 |
werner |
36 |
|
170 |
|
|
gpgme_data_write (sumlist, mdasc, strlen (mdasc)); |
171 |
|
|
gpgme_data_write (sumlist, " ", 1); |
172 |
|
|
gpgme_data_write (sumlist, fname, strlen (fname)); |
173 |
|
|
gpgme_data_write (sumlist, "\r\n", 2); |
174 |
|
|
} |
175 |
twoaday |
165 |
if (IsDlgButtonChecked (dlg, IDC_MDSUM_TOCLIP)) { |
176 |
|
|
gpg_data_release_and_set_clipboard (sumlist, 0); |
177 |
|
|
break; |
178 |
|
|
} |
179 |
twoaday |
204 |
_snprintf (fname, sizeof (fname)-1, "%s_sums.txt", algname); |
180 |
twoaday |
225 |
name = get_filesave_dlg (dlg, _("Select File to Save Checksums"), |
181 |
twoaday |
130 |
NULL, fname); |
182 |
werner |
36 |
if (name && *name) { |
183 |
|
|
gpg_data_release_and_set_file (sumlist, name); |
184 |
twoaday |
130 |
log_box (_("File Manager"), MB_OK, |
185 |
|
|
_("Checksums successfully saved in '%s'"), name); |
186 |
werner |
36 |
} |
187 |
|
|
else |
188 |
|
|
gpgme_data_release (sumlist); |
189 |
|
|
break; |
190 |
|
|
} |
191 |
|
|
break; |
192 |
|
|
} |
193 |
|
|
return FALSE; |
194 |
|
|
} |