Line data Source code
1 : /*
2 : data.cpp - wraps a gpgme data object
3 : Copyright (C) 2003 Klarälvdalens Datakonsult AB
4 :
5 : This file is part of GPGME++.
6 :
7 : GPGME++ is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU Library General Public
9 : License as published by the Free Software Foundation; either
10 : version 2 of the License, or (at your option) any later version.
11 :
12 : GPGME++ 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 Library General Public License for more details.
16 :
17 : You should have received a copy of the GNU Library General Public License
18 : along with GPGME++; see the file COPYING.LIB. If not, write to the
19 : Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 : Boston, MA 02110-1301, USA.
21 : */
22 :
23 : #include "data_p.h"
24 : #include <error.h>
25 : #include <interfaces/dataprovider.h>
26 :
27 : #include <gpgme.h>
28 :
29 : #ifndef NDEBUG
30 : #include <iostream>
31 : #endif
32 :
33 103 : GpgME::Data::Private::~Private()
34 : {
35 103 : if (data) {
36 64 : gpgme_data_release(data);
37 : }
38 103 : }
39 :
40 6 : const GpgME::Data::Null GpgME::Data::null;
41 :
42 0 : GpgME::Data::Data()
43 : {
44 : gpgme_data_t data;
45 0 : const gpgme_error_t e = gpgme_data_new(&data);
46 0 : d.reset(new Private(e ? 0 : data));
47 0 : }
48 :
49 39 : GpgME::Data::Data(const Null &)
50 39 : : d(new Private(0))
51 : {
52 :
53 39 : }
54 :
55 0 : GpgME::Data::Data(gpgme_data_t data)
56 0 : : d(new Private(data))
57 : {
58 :
59 0 : }
60 :
61 0 : GpgME::Data::Data(const char *buffer, size_t size, bool copy)
62 : {
63 : gpgme_data_t data;
64 0 : const gpgme_error_t e = gpgme_data_new_from_mem(&data, buffer, size, int(copy));
65 0 : std::string sizestr = std::to_string(size);
66 : // Ignore errors as this is optional
67 0 : gpgme_data_set_flag(data, "size-hint", sizestr.c_str());
68 0 : d.reset(new Private(e ? 0 : data));
69 0 : }
70 :
71 0 : GpgME::Data::Data(const char *filename)
72 : {
73 : gpgme_data_t data;
74 0 : const gpgme_error_t e = gpgme_data_new(&data);
75 0 : d.reset(new Private(e ? 0 : data));
76 0 : if (!e) {
77 0 : setFileName(filename);
78 : }
79 0 : }
80 :
81 0 : GpgME::Data::Data(const char *filename, off_t offset, size_t length)
82 : {
83 : gpgme_data_t data;
84 0 : const gpgme_error_t e = gpgme_data_new_from_filepart(&data, filename, 0, offset, length);
85 0 : d.reset(new Private(e ? 0 : data));
86 0 : }
87 :
88 0 : GpgME::Data::Data(FILE *fp)
89 : {
90 : gpgme_data_t data;
91 0 : const gpgme_error_t e = gpgme_data_new_from_stream(&data, fp);
92 0 : d.reset(new Private(e ? 0 : data));
93 0 : }
94 :
95 0 : GpgME::Data::Data(FILE *fp, off_t offset, size_t length)
96 : {
97 : gpgme_data_t data;
98 0 : const gpgme_error_t e = gpgme_data_new_from_filepart(&data, 0, fp, offset, length);
99 0 : d.reset(new Private(e ? 0 : data));
100 0 : }
101 :
102 0 : GpgME::Data::Data(int fd)
103 : {
104 : gpgme_data_t data;
105 0 : const gpgme_error_t e = gpgme_data_new_from_fd(&data, fd);
106 0 : d.reset(new Private(e ? 0 : data));
107 0 : }
108 :
109 64 : GpgME::Data::Data(DataProvider *dp)
110 : {
111 64 : d.reset(new Private);
112 64 : if (!dp) {
113 64 : return;
114 : }
115 64 : if (!dp->isSupported(DataProvider::Read)) {
116 1 : d->cbs.read = 0;
117 : }
118 64 : if (!dp->isSupported(DataProvider::Write)) {
119 20 : d->cbs.write = 0;
120 : }
121 64 : if (!dp->isSupported(DataProvider::Seek)) {
122 0 : d->cbs.seek = 0;
123 : }
124 64 : if (!dp->isSupported(DataProvider::Release)) {
125 0 : d->cbs.release = 0;
126 : }
127 64 : const gpgme_error_t e = gpgme_data_new_from_cbs(&d->data, &d->cbs, dp);
128 64 : if (e) {
129 0 : d->data = 0;
130 : }
131 64 : if (dp->isSupported(DataProvider::Seek)) {
132 64 : off_t size = seek(0, SEEK_END);
133 64 : seek(0, SEEK_SET);
134 64 : std::string sizestr = std::to_string(size);
135 : // Ignore errors as this is optional
136 64 : gpgme_data_set_flag(d->data, "size-hint", sizestr.c_str());
137 : }
138 : #ifndef NDEBUG
139 : //std::cerr << "GpgME::Data(): DataProvider supports: "
140 : // << ( d->cbs.read ? "read" : "no read" ) << ", "
141 : // << ( d->cbs.write ? "write" : "no write" ) << ", "
142 : // << ( d->cbs.seek ? "seek" : "no seek" ) << ", "
143 : // << ( d->cbs.release ? "release" : "no release" ) << std::endl;
144 : #endif
145 : }
146 :
147 24 : bool GpgME::Data::isNull() const
148 : {
149 24 : return !d || !d->data;
150 : }
151 :
152 0 : GpgME::Data::Encoding GpgME::Data::encoding() const
153 : {
154 0 : switch (gpgme_data_get_encoding(d->data)) {
155 0 : case GPGME_DATA_ENCODING_NONE: return AutoEncoding;
156 0 : case GPGME_DATA_ENCODING_BINARY: return BinaryEncoding;
157 0 : case GPGME_DATA_ENCODING_BASE64: return Base64Encoding;
158 0 : case GPGME_DATA_ENCODING_ARMOR: return ArmorEncoding;
159 0 : case GPGME_DATA_ENCODING_MIME: return MimeEncoding;
160 : }
161 0 : return AutoEncoding;
162 : }
163 :
164 0 : GpgME::Error GpgME::Data::setEncoding(Encoding enc)
165 : {
166 0 : gpgme_data_encoding_t ge = GPGME_DATA_ENCODING_NONE;
167 0 : switch (enc) {
168 0 : case AutoEncoding: ge = GPGME_DATA_ENCODING_NONE; break;
169 0 : case BinaryEncoding: ge = GPGME_DATA_ENCODING_BINARY; break;
170 0 : case Base64Encoding: ge = GPGME_DATA_ENCODING_BASE64; break;
171 0 : case ArmorEncoding: ge = GPGME_DATA_ENCODING_ARMOR; break;
172 0 : case MimeEncoding: ge = GPGME_DATA_ENCODING_MIME; break;
173 : }
174 0 : return Error(gpgme_data_set_encoding(d->data, ge));
175 : }
176 :
177 0 : GpgME::Data::Type GpgME::Data::type() const
178 : {
179 0 : if (isNull()) {
180 0 : return Invalid;
181 : }
182 0 : switch (gpgme_data_identify(d->data, 0)) {
183 0 : case GPGME_DATA_TYPE_INVALID: return Invalid;
184 0 : case GPGME_DATA_TYPE_UNKNOWN: return Unknown;
185 0 : case GPGME_DATA_TYPE_PGP_SIGNED: return PGPSigned;
186 0 : case GPGME_DATA_TYPE_PGP_OTHER: return PGPOther;
187 0 : case GPGME_DATA_TYPE_PGP_KEY: return PGPKey;
188 0 : case GPGME_DATA_TYPE_CMS_SIGNED: return CMSSigned;
189 0 : case GPGME_DATA_TYPE_CMS_ENCRYPTED: return CMSEncrypted;
190 0 : case GPGME_DATA_TYPE_CMS_OTHER: return CMSOther;
191 0 : case GPGME_DATA_TYPE_X509_CERT: return X509Cert;
192 0 : case GPGME_DATA_TYPE_PKCS12: return PKCS12;
193 0 : case GPGME_DATA_TYPE_PGP_ENCRYPTED: return PGPEncrypted;
194 0 : case GPGME_DATA_TYPE_PGP_SIGNATURE: return PGPSignature;
195 : }
196 0 : return Invalid;
197 : }
198 :
199 0 : char *GpgME::Data::fileName() const
200 : {
201 0 : return gpgme_data_get_file_name(d->data);
202 : }
203 :
204 0 : GpgME::Error GpgME::Data::setFileName(const char *name)
205 : {
206 0 : return Error(gpgme_data_set_file_name(d->data, name));
207 : }
208 :
209 0 : ssize_t GpgME::Data::read(void *buffer, size_t length)
210 : {
211 0 : return gpgme_data_read(d->data, buffer, length);
212 : }
213 :
214 0 : ssize_t GpgME::Data::write(const void *buffer, size_t length)
215 : {
216 0 : return gpgme_data_write(d->data, buffer, length);
217 : }
218 :
219 128 : off_t GpgME::Data::seek(off_t offset, int whence)
220 : {
221 128 : return gpgme_data_seek(d->data, offset, whence);
222 18 : }
|