1 |
twoaday |
119 |
/* wptFileCBS.cpp - Customized I/O callbacks for GPGME |
2 |
werner |
36 |
* Copyright (C) 2005 Timo Schulz |
3 |
|
|
* Copyright (C) 2005 g10 Code GmbH |
4 |
|
|
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU General Public License |
9 |
|
|
* as published by the Free Software Foundation; either version 2 |
10 |
|
|
* of the License, or (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 GNU |
15 |
|
|
* General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
|
|
|
25 |
|
|
#include <windows.h> |
26 |
|
|
#include <stdio.h> |
27 |
twoaday |
119 |
#include <errno.h> |
28 |
werner |
36 |
#include <malloc.h> |
29 |
|
|
|
30 |
|
|
#include "gpgme.h" |
31 |
|
|
#include "wptListView.h" |
32 |
|
|
#include "wptGPG.h" |
33 |
|
|
#include "wptFileManager.h" |
34 |
|
|
#include "wptErrors.h" |
35 |
|
|
#include "wptTypes.h" |
36 |
|
|
|
37 |
twoaday |
119 |
void progress_callback (void *opaque, const char *what, int type, |
38 |
|
|
int off, int max); |
39 |
werner |
36 |
|
40 |
|
|
|
41 |
|
|
/* Predefined read callback. */ |
42 |
|
|
static long |
43 |
|
|
read_cb (void *handle, void *buffer, size_t size) |
44 |
|
|
{ |
45 |
|
|
file_data_t cb = (file_data_t)handle; |
46 |
|
|
struct progress_filter_s *pfx = (struct progress_filter_s *)cb->cb_value; |
47 |
twoaday |
119 |
DWORD nread = 0; |
48 |
werner |
36 |
|
49 |
twoaday |
119 |
ReadFile (cb->handle, buffer, size, &nread, NULL); |
50 |
|
|
|
51 |
werner |
36 |
/* XXX: there is a sync problem with the progress dialog. */ |
52 |
|
|
if (pfx) |
53 |
|
|
progress_callback (pfx, NULL, 0, cb->off, cb->size); |
54 |
twoaday |
119 |
cb->off += nread; |
55 |
|
|
return (long)nread; |
56 |
werner |
36 |
} |
57 |
|
|
|
58 |
|
|
|
59 |
|
|
/* Predefined write callback. */ |
60 |
|
|
static long |
61 |
|
|
write_cb (void *handle, const void *buffer, size_t size) |
62 |
|
|
{ |
63 |
|
|
file_data_t cb = (file_data_t)handle; |
64 |
twoaday |
119 |
DWORD nwritten; |
65 |
werner |
36 |
|
66 |
twoaday |
119 |
/* XXX: check if fd == INVALID_HANDLE_VALUE. */ |
67 |
|
|
|
68 |
|
|
if (!cb->handle) { |
69 |
|
|
SECURITY_ATTRIBUTES sec_attr; |
70 |
|
|
|
71 |
|
|
memset (&sec_attr, 0, sizeof (sec_attr)); |
72 |
|
|
sec_attr.bInheritHandle = FALSE; |
73 |
|
|
sec_attr.nLength = sizeof (sec_attr); |
74 |
|
|
cb->handle = CreateFile (cb->name, GENERIC_WRITE, FILE_SHARE_WRITE, |
75 |
|
|
&sec_attr, CREATE_ALWAYS, 0, NULL); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
WriteFile (cb->handle, buffer, size, &nwritten, NULL); |
79 |
|
|
return (long)nwritten; |
80 |
werner |
36 |
} |
81 |
|
|
|
82 |
|
|
|
83 |
|
|
/* Create a new data -> file association with a static callback. |
84 |
|
|
@fname is the file which is associated to the object. |
85 |
|
|
@r_cb is the context which holds all information. |
86 |
|
|
@for_read is 1 if the file is opened for read only. |
87 |
|
|
Return value: 0 on success. */ |
88 |
|
|
gpgme_error_t |
89 |
twoaday |
119 |
gpg_file_data_new (const char *fname, int flags, file_data_t *r_cb) |
90 |
werner |
36 |
|
91 |
|
|
{ |
92 |
|
|
gpgme_error_t err; |
93 |
|
|
file_data_t cb; |
94 |
twoaday |
119 |
HANDLE fd = NULL; |
95 |
|
|
SECURITY_ATTRIBUTES sec_attr; |
96 |
werner |
36 |
|
97 |
twoaday |
119 |
memset (&sec_attr, 0, sizeof (sec_attr)); |
98 |
|
|
sec_attr.bInheritHandle = FALSE; |
99 |
|
|
sec_attr.nLength = sizeof (sec_attr); |
100 |
|
|
if (flags & F_DATA_READ) { |
101 |
|
|
fd = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ, |
102 |
|
|
&sec_attr, OPEN_EXISTING, 0, NULL); |
103 |
|
|
if (fd == INVALID_HANDLE_VALUE) |
104 |
|
|
return gpgme_err_code_from_errno (ENOENT); |
105 |
|
|
} |
106 |
werner |
36 |
cb = (file_data_t)calloc (1, sizeof *cb); |
107 |
|
|
if (!cb) |
108 |
twoaday |
119 |
BUG (NULL); |
109 |
|
|
cb->name = strdup (fname); |
110 |
|
|
if (!cb->name) |
111 |
|
|
BUG (NULL); |
112 |
werner |
36 |
cb->cbs.read = read_cb; |
113 |
|
|
cb->cbs.write = write_cb; |
114 |
twoaday |
119 |
if (flags & F_DATA_READ) { |
115 |
|
|
cb->handle = fd; |
116 |
|
|
cb->size = GetFileSize (fd, NULL); |
117 |
werner |
36 |
cb->off = 0; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
err = gpgme_data_new_from_cbs (&cb->dat, &cb->cbs, cb); |
121 |
|
|
if (err) { |
122 |
twoaday |
119 |
CloseHandle (fd); |
123 |
werner |
36 |
free (cb); |
124 |
|
|
return err; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
*r_cb = cb; |
128 |
|
|
return err; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
|
132 |
|
|
/* Activate the progress callback for the given object @ctx. */ |
133 |
|
|
void |
134 |
|
|
gpg_file_data_set_cb (file_data_t ctx, struct progress_filter_s *pfx) |
135 |
|
|
{ |
136 |
|
|
ctx->cb_value = (void*)pfx; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
|
140 |
|
|
/* Release the context in @cb. Close all internal handles if possible. */ |
141 |
|
|
void |
142 |
|
|
gpg_file_data_release (file_data_t cb) |
143 |
|
|
{ |
144 |
|
|
if (!cb) |
145 |
|
|
return; |
146 |
|
|
if (cb->handle) { |
147 |
twoaday |
119 |
CloseHandle (cb->handle); |
148 |
|
|
cb->handle = NULL; |
149 |
werner |
36 |
} |
150 |
|
|
if (cb->dat) |
151 |
|
|
gpgme_data_release (cb->dat); |
152 |
twoaday |
119 |
safe_free (cb->name); |
153 |
|
|
safe_free (cb); |
154 |
werner |
36 |
} |