1 |
/* 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 |
#include "wptW32API.h" |
33 |
|
34 |
|
35 |
/* Constructor to build gpgme context. */ |
36 |
GPGME::GPGME () |
37 |
{ |
38 |
gpgme_new (&ctx); |
39 |
} |
40 |
|
41 |
/* Destructor to release gpgme context. */ |
42 |
GPGME::~GPGME () |
43 |
{ |
44 |
if (ctx != NULL) |
45 |
gpgme_release (ctx); |
46 |
} |
47 |
|
48 |
/* Enable or disable the feature to add the |
49 |
WinPT version to armored output. */ |
50 |
void |
51 |
GPGME::setChangeVersion (bool val) |
52 |
{ |
53 |
chg_ver = val; |
54 |
} |
55 |
|
56 |
/* Enable or disable armor. */ |
57 |
void |
58 |
GPGME::setArmor (bool val) |
59 |
{ |
60 |
gpgme_set_armor (ctx, val? 1 : 0); |
61 |
} |
62 |
|
63 |
bool |
64 |
GPGME::getArmor (void) |
65 |
{ |
66 |
return gpgme_get_armor (ctx)? true: false; |
67 |
} |
68 |
|
69 |
/* Export key pattern @patt to file @outfile. |
70 |
Return value: 0 on success. */ |
71 |
gpgme_error_t |
72 |
GPGME::exportToFile (const char *patt, const char *outfile) |
73 |
{ |
74 |
gpgme_data_t dat; |
75 |
gpgme_error_t err; |
76 |
|
77 |
err = gpgme_data_new (&dat); |
78 |
if (err) |
79 |
return err; |
80 |
op_begin (); |
81 |
err = gpgme_op_export (ctx, patt, 0, dat); |
82 |
op_end (); |
83 |
if (err) { |
84 |
gpgme_data_release (dat); |
85 |
return err; |
86 |
} |
87 |
err = gpg_data_release_and_set_file (dat, outfile); |
88 |
return err; |
89 |
} |
90 |
|
91 |
|
92 |
/* Export key pattern @patt to the clipboard. |
93 |
Return value: 0 on success. */ |
94 |
gpgme_error_t |
95 |
GPGME::exportToClipboard (const char *patt) |
96 |
{ |
97 |
gpgme_data_t dat; |
98 |
gpgme_error_t err; |
99 |
|
100 |
err = gpgme_data_new (&dat); |
101 |
if (err) |
102 |
return err; |
103 |
op_begin (); |
104 |
err = gpgme_op_export (ctx, patt, 0, dat); |
105 |
op_end (); |
106 |
if (err) { |
107 |
gpgme_data_release (dat); |
108 |
return err; |
109 |
} |
110 |
gpg_data_release_and_set_clipboard (dat, chg_ver? 1 : 0); |
111 |
return err; |
112 |
} |
113 |
|
114 |
|
115 |
/* Export key pattern @patt to the buffer @outdata. |
116 |
Return value: 0 on success. */ |
117 |
gpgme_error_t |
118 |
GPGME::exportToBuffer (const char *patt, char **outdata) |
119 |
{ |
120 |
gpgme_error_t err; |
121 |
gpgme_data_t dat; |
122 |
size_t n; |
123 |
char *p; |
124 |
|
125 |
err = gpgme_data_new (&dat); |
126 |
if (err) |
127 |
return err; |
128 |
op_begin (); |
129 |
err = gpgme_op_export (ctx, patt, 0, dat); |
130 |
op_end (); |
131 |
if (err) { |
132 |
gpgme_data_release (dat); |
133 |
return err; |
134 |
} |
135 |
p = gpgme_data_release_and_get_mem (dat, &n); |
136 |
|
137 |
*outdata = new char[n+1]; |
138 |
memcpy (*outdata, p, n); |
139 |
(*outdata)[n] = 0; |
140 |
gpgme_free (p); |
141 |
|
142 |
return err; |
143 |
} |