1 |
/* wptGPGMEWrapper.cpp - Object oriented wrapper for GPGME |
/* wptGPGMEWrapper.cpp - Object oriented wrapper for GPGME |
2 |
* Copyright (C) 2006 Timo Schulz |
* Copyright (C) 2006, 2007 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
66 |
return gpgme_get_armor (ctx)? true: false; |
return gpgme_get_armor (ctx)? true: false; |
67 |
} |
} |
68 |
|
|
69 |
/* Export key pattern @patt to file @outfile. |
|
70 |
|
/* Import keys from a memory buffer. |
71 |
|
Return value: 0 on success. */ |
72 |
|
gpgme_error_t |
73 |
|
GPGME::importFromBuffer (const char *indata) |
74 |
|
{ |
75 |
|
gpgme_data_t keydata = NULL; |
76 |
|
gpgme_error_t err; |
77 |
|
|
78 |
|
err = gpgme_data_new_from_mem (&keydata, indata, strlen (indata), 1); |
79 |
|
if (err) |
80 |
|
return err; |
81 |
|
|
82 |
|
op_begin (); |
83 |
|
err = gpgme_op_import (ctx, keydata); |
84 |
|
op_end (); |
85 |
|
|
86 |
|
gpgme_data_release (keydata); |
87 |
|
return err; |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
|
/* Import the keys from the clipboard. |
92 |
|
Return value: 0 on success. */ |
93 |
|
gpgme_error_t |
94 |
|
GPGME::importFromClipboard (void) |
95 |
|
{ |
96 |
|
gpgme_data_t keydata = NULL; |
97 |
|
gpgme_error_t err; |
98 |
|
|
99 |
|
err = gpg_data_new_from_clipboard (&keydata, 0); |
100 |
|
if (err) |
101 |
|
return err; |
102 |
|
|
103 |
|
op_begin (); |
104 |
|
err = gpgme_op_import (ctx, keydata); |
105 |
|
op_end (); |
106 |
|
|
107 |
|
gpgme_data_release (keydata); |
108 |
|
return err; |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
/* Import the keys from the file @file. |
113 |
|
Return value: 0 on success. */ |
114 |
|
gpgme_error_t |
115 |
|
GPGME::importFromFile (const char *file) |
116 |
|
{ |
117 |
|
gpgme_data_t keydata; |
118 |
|
gpgme_error_t err; |
119 |
|
|
120 |
|
err = gpgme_data_new_from_file (&keydata, file, 1); |
121 |
|
if (err) |
122 |
|
return err; |
123 |
|
|
124 |
|
op_begin (); |
125 |
|
err = gpgme_op_import (ctx, keydata); |
126 |
|
op_end (); |
127 |
|
|
128 |
|
gpgme_data_release (keydata); |
129 |
|
return err; |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
/* Return the import result for the last GPG operation. */ |
134 |
|
gpgme_import_result_t |
135 |
|
GPGME::importGetResult (void) |
136 |
|
{ |
137 |
|
return gpgme_op_import_result (ctx); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
|
/* Export a single key or all keys which matches the pattern @patt |
142 |
|
to file @outfile. |
143 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
144 |
gpgme_error_t |
gpgme_error_t |
145 |
GPGME::exportToFile (const char *patt, const char *outfile) |
GPGME::exportToFile (const char *patt, const char *outfile) |
153 |
op_begin (); |
op_begin (); |
154 |
err = gpgme_op_export (ctx, patt, 0, dat); |
err = gpgme_op_export (ctx, patt, 0, dat); |
155 |
op_end (); |
op_end (); |
156 |
if (err) { |
if (err) |
157 |
gpgme_data_release (dat); |
gpgme_data_release (dat); |
158 |
return err; |
else { |
159 |
|
err = gpg_data_release_and_set_file (dat, outfile); |
160 |
|
if (err) |
161 |
|
gpgme_data_release (dat); |
162 |
} |
} |
163 |
err = gpg_data_release_and_set_file (dat, outfile); |
|
164 |
return err; |
return err; |
165 |
} |
} |
166 |
|
|
167 |
|
/* Export multiple keys using @pattern. */ |
168 |
|
gpgme_error_t |
169 |
|
GPGME::exportToFile (const char *pattern[], const char *outfile) |
170 |
|
{ |
171 |
|
gpgme_error_t err; |
172 |
|
gpgme_data_t dat; |
173 |
|
|
174 |
|
err = gpgme_data_new (&dat); |
175 |
|
if (err) |
176 |
|
return err; |
177 |
|
|
178 |
|
op_begin (); |
179 |
|
err = gpgme_op_export_ext (ctx, pattern, 0, dat); |
180 |
|
op_end (); |
181 |
|
if (err) |
182 |
|
gpgme_data_release (dat); |
183 |
|
else { |
184 |
|
err = gpg_data_release_and_set_file (dat, outfile); |
185 |
|
if (err) |
186 |
|
gpgme_data_release (dat); |
187 |
|
} |
188 |
|
|
189 |
|
return err; |
190 |
|
} |
191 |
/* Export key pattern @patt to the clipboard. |
/* Export key pattern @patt to the clipboard. |
192 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
193 |
gpgme_error_t |
gpgme_error_t |
202 |
op_begin (); |
op_begin (); |
203 |
err = gpgme_op_export (ctx, patt, 0, dat); |
err = gpgme_op_export (ctx, patt, 0, dat); |
204 |
op_end (); |
op_end (); |
205 |
if (err) { |
if (err) |
206 |
gpgme_data_release (dat); |
gpgme_data_release (dat); |
207 |
return err; |
else |
208 |
} |
gpg_data_release_to_clipboard (dat, chg_ver? 1 : 0); |
|
gpg_data_release_and_set_clipboard (dat, chg_ver? 1 : 0); |
|
209 |
return err; |
return err; |
210 |
} |
} |
211 |
|
|
212 |
|
gpgme_error_t |
213 |
|
GPGME::exportToClipboard (const char *pattern[]) |
214 |
|
{ |
215 |
|
gpgme_data_t dat; |
216 |
|
gpgme_error_t err; |
217 |
|
|
218 |
|
err = gpgme_data_new (&dat); |
219 |
|
if (err) |
220 |
|
return err; |
221 |
|
op_begin (); |
222 |
|
err = gpgme_op_export_ext (ctx, pattern, 0, dat); |
223 |
|
op_end (); |
224 |
|
if (err) |
225 |
|
gpgme_data_release (dat); |
226 |
|
else |
227 |
|
gpg_data_release_to_clipboard (dat, chg_ver? 1: 0); |
228 |
|
return err; |
229 |
|
} |
230 |
|
|
231 |
/* Export key pattern @patt to the buffer @outdata. |
/* Export key pattern @patt to the buffer @outdata. |
232 |
Return value: 0 on success. */ |
Return value: 0 on success. */ |
250 |
} |
} |
251 |
p = gpgme_data_release_and_get_mem (dat, &n); |
p = gpgme_data_release_and_get_mem (dat, &n); |
252 |
|
|
253 |
|
/* we use this detour because p were allocated inside |
254 |
|
the gpgme DLL and cannot be freed with 'free' because |
255 |
|
the DLL might be linked against debug libs but the application |
256 |
|
run ins release mode. */ |
257 |
*outdata = new char[n+1]; |
*outdata = new char[n+1]; |
258 |
memcpy (*outdata, p, n); |
memcpy (*outdata, p, n); |
259 |
(*outdata)[n] = 0; |
(*outdata)[n] = 0; |