1 |
twoaday |
133 |
/* wptGPGMEWrapper.cpp - Object oriented wrapper for GPGME |
2 |
|
|
* Copyright (C) 2006 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2 |
9 |
|
|
* of the License, or (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 GNU |
14 |
|
|
* 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 |
|
|
|
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
|
|
|
25 |
|
|
#include <windows.h> |
26 |
|
|
#include <stdio.h> |
27 |
|
|
#include <string.h> |
28 |
|
|
|
29 |
|
|
#include "gpgme.h" |
30 |
|
|
#include "wptGPG.h" |
31 |
|
|
#include "wptGPGME.h" |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
/* Constructor to build gpgme context. */ |
35 |
|
|
GPGME::GPGME () |
36 |
|
|
{ |
37 |
|
|
gpgme_new (&ctx); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
/* Destructor to release gpgme context. */ |
41 |
|
|
GPGME::~GPGME () |
42 |
|
|
{ |
43 |
|
|
if (ctx != NULL) |
44 |
|
|
gpgme_release (ctx); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
void |
48 |
|
|
GPGME::setChangeVersion (bool val) |
49 |
|
|
{ |
50 |
|
|
chg_ver = val; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
/* Enable or disable armor. */ |
54 |
|
|
void |
55 |
|
|
GPGME::setArmor (bool val) |
56 |
|
|
{ |
57 |
|
|
gpgme_set_armor (ctx, val? 1 : 0); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
bool |
61 |
|
|
GPGME::getArmor (void) |
62 |
|
|
{ |
63 |
|
|
return gpgme_get_armor (ctx)? true: false; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
/* Export key pattern @patt to file @outfile. |
67 |
|
|
Return value: 0 on success. */ |
68 |
|
|
gpgme_error_t |
69 |
|
|
GPGME::exportToFile (const char *patt, const char *outfile) |
70 |
|
|
{ |
71 |
|
|
gpgme_data_t dat; |
72 |
|
|
gpgme_error_t err; |
73 |
|
|
|
74 |
|
|
err = gpgme_data_new (&dat); |
75 |
|
|
if (err) |
76 |
|
|
return err; |
77 |
|
|
err = gpgme_op_export (ctx, patt, 0, dat); |
78 |
|
|
if (err) { |
79 |
|
|
gpgme_data_release (dat); |
80 |
|
|
return err; |
81 |
|
|
} |
82 |
|
|
err = gpg_data_release_and_set_file (dat, outfile); |
83 |
|
|
return err; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
|
87 |
|
|
/* Export key pattern @patt to the clipboard. |
88 |
|
|
Return value: 0 on success. */ |
89 |
|
|
gpgme_error_t |
90 |
|
|
GPGME::exportToClipboard (const char *patt) |
91 |
|
|
{ |
92 |
|
|
gpgme_data_t dat; |
93 |
|
|
gpgme_error_t err; |
94 |
|
|
|
95 |
|
|
err = gpgme_data_new (&dat); |
96 |
|
|
if (err) |
97 |
|
|
return err; |
98 |
|
|
err = gpgme_op_export (ctx, patt, 0, dat); |
99 |
|
|
if (err) { |
100 |
|
|
gpgme_data_release (dat); |
101 |
|
|
return err; |
102 |
|
|
} |
103 |
|
|
gpg_data_release_and_set_clipboard (dat, chg_ver? 1 : 0); |
104 |
|
|
return err; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
|
108 |
|
|
/* Export key pattern @patt to the buffer @outdata. |
109 |
|
|
Return value: 0 on success. */ |
110 |
|
|
gpgme_error_t |
111 |
|
|
GPGME::exportToBuffer (const char *patt, char **outdata) |
112 |
|
|
{ |
113 |
|
|
gpgme_error_t err; |
114 |
|
|
gpgme_data_t dat; |
115 |
|
|
size_t n; |
116 |
|
|
char *p; |
117 |
|
|
|
118 |
|
|
err = gpgme_data_new (&dat); |
119 |
|
|
if (err) |
120 |
|
|
return err; |
121 |
|
|
err = gpgme_op_export (ctx, patt, 0, dat); |
122 |
|
|
if (err) { |
123 |
|
|
gpgme_data_release (dat); |
124 |
|
|
return err; |
125 |
|
|
} |
126 |
|
|
p = gpgme_data_release_and_get_mem (dat, &n); |
127 |
|
|
|
128 |
|
|
*outdata = new char[n+1]; |
129 |
|
|
memcpy (*outdata, p, n); |
130 |
|
|
(*outdata)[n] = 0; |
131 |
|
|
gpgme_free (p); |
132 |
|
|
|
133 |
|
|
return err; |
134 |
|
|
} |