1 |
#if 0 /*UNUSED*/ |
2 |
/* wptMDSumDlg.cpp - Dialog to show hash values for files |
3 |
* Copyright (C) 2003, 2005, 2006, 2008 Timo Schulz |
4 |
* |
5 |
* This file is part of WinPT. |
6 |
* |
7 |
* WinPT is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* WinPT is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
*/ |
17 |
#ifdef HAVE_CONFIG_H |
18 |
#include <config.h> |
19 |
#endif |
20 |
|
21 |
#include <windows.h> |
22 |
#include <stdio.h> |
23 |
|
24 |
#include "resource.h" |
25 |
#include "wptTypes.h" |
26 |
#include "wptW32API.h" |
27 |
#include "wptGPG.h" |
28 |
#include "wptCommonCtl.h" |
29 |
#include "wptContext.h" |
30 |
#include "wptNLS.h" |
31 |
#include "wptErrors.h" |
32 |
|
33 |
|
34 |
/* maximum hash size in octets (sha512=64) */ |
35 |
#define MAX_HASHSIZE 64 |
36 |
|
37 |
/* Symbolic column IDs. */ |
38 |
enum md_col_t {COL_MD=0, COL_NAME}; |
39 |
|
40 |
|
41 |
/* A model which represents the contents of the list view. */ |
42 |
struct hashlist_model_s { |
43 |
struct hashlist_model_s *next; |
44 |
char *name; |
45 |
BYTE *md; |
46 |
DWORD mdlen; |
47 |
}; |
48 |
typedef struct hashlist_model_s *hashlist_model_t; |
49 |
|
50 |
|
51 |
/* Release the file list @hm. */ |
52 |
static void |
53 |
hashmodel_release (hashlist_model_t hm) |
54 |
{ |
55 |
hashlist_model_t t; |
56 |
|
57 |
while (hm) { |
58 |
t = hm->next; |
59 |
free_if_alloc (hm->name); |
60 |
free_if_alloc (hm->md); |
61 |
free_if_alloc (hm); |
62 |
hm = t; |
63 |
} |
64 |
} |
65 |
|
66 |
|
67 |
/* Add a new file with the name @name to the list @hm. */ |
68 |
static hashlist_model_t |
69 |
hashmodel_add_file (hashlist_model_t *hm, const char *name, |
70 |
const BYTE *mdbuf, size_t mdlen) |
71 |
{ |
72 |
hashlist_model_t t, n; |
73 |
|
74 |
t = new hashlist_model_s; |
75 |
memset (t, 0, sizeof *t); |
76 |
t->name = m_strdup (name); |
77 |
t->mdlen = mdlen; |
78 |
t->md = new BYTE[mdlen]; |
79 |
memcpy (t->md, mdbuf, mdlen); |
80 |
if (!*hm) |
81 |
*hm = t; |
82 |
else { |
83 |
for (n = *hm; n->next; n=n->next) |
84 |
; |
85 |
n->next = t; |
86 |
} |
87 |
return t; |
88 |
} |
89 |
|
90 |
|
91 |
/* Return a printable digest of the buffer @mdbuf. */ |
92 |
static const char* |
93 |
printable_digest (BYTE *mdbuf, size_t n, char *mdasc, size_t maxlen) |
94 |
{ |
95 |
if (n*4 > maxlen) |
96 |
return NULL; |
97 |
for (size_t i = 0; i < n; i++) |
98 |
sprintf (mdasc+2*i, "%02X", mdbuf[i]); |
99 |
return mdasc; |
100 |
} |
101 |
|
102 |
|
103 |
static const char* |
104 |
id2algo (gpgme_hash_algo_t mdalgo) |
105 |
{ |
106 |
switch (mdalgo) { |
107 |
case GPGME_MD_MD5: return "MD5"; |
108 |
case GPGME_MD_SHA1: return "SHA1"; |
109 |
case GPGME_MD_RMD160: return "RMD160"; |
110 |
case GPGME_MD_SHA256: return "SHA256"; |
111 |
case GPGME_MD_SHA384: return "SHA384"; |
112 |
case GPGME_MD_SHA512: return "SHA512"; |
113 |
default: break; |
114 |
} |
115 |
return ""; |
116 |
} |
117 |
|
118 |
|
119 |
/* Hash the selected file from the FM listview control in @md. |
120 |
Add the results to the listview @lv. */ |
121 |
static void |
122 |
hash_selected_files (md_file_s *md, listview_ctrl_t lv, hashlist_model_t *r_fl) |
123 |
{ |
124 |
hashlist_model_t item; |
125 |
BYTE mdbuf[MAX_HASHSIZE]; |
126 |
char fname[MAX_PATH+1]; |
127 |
char mdasc[4*MAX_HASHSIZE+1]; |
128 |
size_t n; |
129 |
int i; |
130 |
|
131 |
for (i = 0; i < listview_count_items (md->lv, 0); i++) { |
132 |
if (!listview_get_item_state (md->lv, i)) |
133 |
continue; |
134 |
listview_get_item_text (md->lv, i, 1, fname, DIM (fname)-1); |
135 |
if (!gpg_md_hash_file (md->mdalgo, fname, mdbuf, &n)) { |
136 |
const char *pmd = printable_digest(mdbuf, n, mdasc, DIM(mdasc)-1); |
137 |
item = hashmodel_add_file (r_fl, fname, mdbuf, n); |
138 |
listview_add_item2 (lv, "", (void*)item); |
139 |
listview_add_sub_item (lv, 0, COL_MD, pmd); |
140 |
listview_add_sub_item (lv, 0, COL_NAME, fname); |
141 |
} |
142 |
} |
143 |
} |
144 |
|
145 |
|
146 |
/* Return 1 if there is a tool available to check the file. |
147 |
(sha1sum, md5sum). */ |
148 |
static int |
149 |
tool_avail (gpgme_hash_algo_t mdalgo) |
150 |
{ |
151 |
if (mdalgo == GPGME_MD_SHA1 || mdalgo == GPGME_MD_MD5) |
152 |
return 1; |
153 |
return 0; |
154 |
} |
155 |
|
156 |
|
157 |
/* Sorting callback. */ |
158 |
static int CALLBACK |
159 |
sort_cb (LPARAM first, LPARAM second, LPARAM param) |
160 |
{ |
161 |
hashlist_model_t a, b; |
162 |
int sortby = (int)param; |
163 |
int cmpres = 0; |
164 |
|
165 |
a = (hashlist_model_t)first; |
166 |
b = (hashlist_model_t)second; |
167 |
if (sortby == COL_NAME) |
168 |
cmpres = stricmp (a->name, b->name); |
169 |
else |
170 |
cmpres = memcmp (a->md, b->md, a->mdlen); |
171 |
return cmpres; |
172 |
} |
173 |
|
174 |
|
175 |
/* Dialog box procedure to show and calculate file digests. */ |
176 |
BOOL CALLBACK |
177 |
mdsum_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) |
178 |
{ |
179 |
static listview_ctrl_t lv; |
180 |
static struct md_file_s *md; |
181 |
static hashlist_model_t hm = NULL; |
182 |
struct listview_column_s cols[] = { |
183 |
{0, 264, (char *)_("Digest")}, |
184 |
{1, 160, (char *)_("Name")}, |
185 |
{0, 0, 0} |
186 |
}; |
187 |
gpgme_data_t sumlist = NULL; |
188 |
const char *name, *algname; |
189 |
char fname[MAX_PATH+64], mdasc[MAX_HASHSIZE*4]; |
190 |
int i; |
191 |
|
192 |
switch (msg) { |
193 |
case WM_INITDIALOG: |
194 |
md = (md_file_s *)lparam; |
195 |
if (!md) |
196 |
BUG (NULL); |
197 |
listview_new (&lv, GetDlgItem (dlg, IDC_MDSUM_LIST)); |
198 |
for (i = 0; i < cols[i].width; i++) |
199 |
listview_add_column (lv, &cols[i]); |
200 |
hash_selected_files (md, lv, &hm); |
201 |
SetDlgItemText (dlg, IDC_MDSUM_COPY, _("&Save...")); |
202 |
SetDlgItemText (dlg, IDOK, _("&Close")); |
203 |
SetDlgItemText (dlg, IDC_MDSUM_TOCLIP, _("Save to clipboard")); |
204 |
SetWindowText (dlg, _("Print Message Digest")); |
205 |
SetForegroundWindow (dlg); |
206 |
break; |
207 |
|
208 |
case WM_DESTROY: |
209 |
if (lv) { |
210 |
listview_release (lv); |
211 |
lv = NULL; |
212 |
} |
213 |
if (hm) { |
214 |
hashmodel_release (hm); |
215 |
hm = NULL; |
216 |
} |
217 |
break; |
218 |
|
219 |
case WM_NOTIFY: |
220 |
if (((NMHDR *)lparam)->code == LVN_COLUMNCLICK) { |
221 |
NMLISTVIEW *nft = (LPNMLISTVIEW) lparam; |
222 |
ListView_SortItems (lv->ctrl, sort_cb, nft->iSubItem); |
223 |
} |
224 |
break; |
225 |
|
226 |
case WM_COMMAND: |
227 |
switch (LOWORD (wparam)) { |
228 |
case IDOK: |
229 |
EndDialog (dlg, TRUE); |
230 |
break; |
231 |
|
232 |
case IDCANCEL: |
233 |
EndDialog (dlg, FALSE); |
234 |
break; |
235 |
|
236 |
case IDC_MDSUM_COPY: |
237 |
algname = id2algo ((gpgme_hash_algo_t)md->mdalgo); |
238 |
if (gpgme_data_new (&sumlist)) |
239 |
BUG(0); |
240 |
if (!tool_avail ((gpgme_hash_algo_t)md->mdalgo)) { |
241 |
const char *s; |
242 |
|
243 |
s = "#warning '"; |
244 |
gpgme_data_write (sumlist, s, strlen (s)); |
245 |
gpgme_data_write (sumlist, algname, strlen (algname)); |
246 |
s = " ' sum is not yet available\r\n"; |
247 |
gpgme_data_write (sumlist, s, strlen (s)); |
248 |
} |
249 |
for (i = 0; i < listview_count_items (lv, 0); i++) { |
250 |
listview_get_item_text (lv, i, COL_MD, mdasc, DIM (mdasc)-1); |
251 |
listview_get_item_text (lv, i, COL_NAME, fname, DIM (fname)-1); |
252 |
|
253 |
gpgme_data_write (sumlist, mdasc, strlen (mdasc)); |
254 |
gpgme_data_write (sumlist, " ", 1); |
255 |
gpgme_data_write (sumlist, fname, strlen (fname)); |
256 |
gpgme_data_write (sumlist, "\r\n", 2); |
257 |
} |
258 |
if (IsDlgButtonChecked (dlg, IDC_MDSUM_TOCLIP)) { |
259 |
gpg_data_release_to_clipboard (sumlist, 0); |
260 |
break; |
261 |
} |
262 |
_snprintf (fname, DIM (fname)-1, "%s_sums.txt", algname); |
263 |
name = get_filesave_dlg (dlg, _("Select File to Save Checksums"), |
264 |
NULL, fname); |
265 |
if (name && *name) { |
266 |
gpgme_error_t err; |
267 |
|
268 |
err = gpg_data_release_and_set_file (sumlist, name); |
269 |
if (!err) { |
270 |
log_box (_("File Manager"), MB_OK, |
271 |
_("Checksums successfully saved in '%s'"), name); |
272 |
sumlist = NULL; |
273 |
} |
274 |
else |
275 |
msg_box (dlg, gpgme_strerror (err), _("File Manager"), MB_ERR); |
276 |
} |
277 |
if (sumlist) |
278 |
gpgme_data_release (sumlist); |
279 |
break; |
280 |
} |
281 |
break; |
282 |
} |
283 |
return FALSE; |
284 |
} |
285 |
#endif |