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 <gpgme++/config-gpgme++.h>
24
25#include "data_p.h"
26#include <gpgme++/error.h>
27#include <gpgme++/interfaces/dataprovider.h>
28
29#include <gpgme.h>
30
31#ifndef NDEBUG
32#include <iostream>
33#endif
34
35GpgME::Data::Private::~Private() {
36 if ( data ) {
37 gpgme_data_release( data );
38 }
39}
40
41GpgME::Data::Null GpgME::Data::null;
42
43GpgME::Data::Data() {
44 gpgme_data_t data;
45 const gpgme_error_t e = gpgme_data_new( &data );
46 d.reset( new Private( e ? 0 : data ) );
47}
48
49GpgME::Data::Data( const Null & )
50 : d( new Private( 0 ) )
51{
52
53}
54
55GpgME::Data::Data( gpgme_data_t data )
56 : d( new Private( data ) )
57{
58
59}
60
61GpgME::Data::Data( const char * buffer, size_t size, bool copy ) {
62 gpgme_data_t data;
63 const gpgme_error_t e = gpgme_data_new_from_mem( &data, buffer, size, int( copy ) );
64 d.reset( new Private( e ? 0 : data ) );
65}
66
67GpgME::Data::Data( const char * filename ) {
68 gpgme_data_t data;
69 const gpgme_error_t e = gpgme_data_new( &data );
70 d.reset( new Private( e ? 0 : data ) );
71 if ( !e ) {
72 setFileName( filename );
73 }
74}
75
76GpgME::Data::Data( const char * filename, off_t offset, size_t length ) {
77 gpgme_data_t data;
78 const gpgme_error_t e = gpgme_data_new_from_filepart( &data, filename, 0, offset, length );
79 d.reset( new Private( e ? 0 : data ) );
80}
81
82GpgME::Data::Data( FILE * fp ) {
83 gpgme_data_t data;
84 const gpgme_error_t e = gpgme_data_new_from_stream( &data, fp );
85 d.reset( new Private( e ? 0 : data ) );
86}
87
88GpgME::Data::Data( FILE * fp, off_t offset, size_t length ) {
89 gpgme_data_t data;
90 const gpgme_error_t e = gpgme_data_new_from_filepart( &data, 0, fp, offset, length );
91 d.reset( new Private( e ? 0 : data ) );
92}
93
94GpgME::Data::Data( int fd ) {
95 gpgme_data_t data;
96 const gpgme_error_t e = gpgme_data_new_from_fd( &data, fd );
97 d.reset( new Private( e ? 0 : data ) );
98}
99
100GpgME::Data::Data( DataProvider * dp ) {
101 d.reset( new Private );
102 if ( !dp ) {
103 return;
104 }
105 if ( !dp->isSupported( DataProvider::Read ) ) {
106 d->cbs.read = 0;
107 }
108 if ( !dp->isSupported( DataProvider::Write ) ) {
109 d->cbs.write = 0;
110 }
111 if ( !dp->isSupported( DataProvider::Seek ) ) {
112 d->cbs.seek = 0;
113 }
114 if ( !dp->isSupported( DataProvider::Release ) ) {
115 d->cbs.release = 0;
116 }
117 const gpgme_error_t e = gpgme_data_new_from_cbs( &d->data, &d->cbs, dp );
118 if ( e ) {
119 d->data = 0;
120 }
121#ifndef NDEBUG
122 //std::cerr << "GpgME::Data(): DataProvider supports: "
123 // << ( d->cbs.read ? "read" : "no read" ) << ", "
124 // << ( d->cbs.write ? "write" : "no write" ) << ", "
125 // << ( d->cbs.seek ? "seek" : "no seek" ) << ", "
126 // << ( d->cbs.release ? "release" : "no release" ) << std::endl;
127#endif
128}
129
130
131
132bool GpgME::Data::isNull() const {
133 return !d || !d->data;
134}
135
136GpgME::Data::Encoding GpgME::Data::encoding() const {
137 switch ( gpgme_data_get_encoding( d->data ) ) {
138 case GPGME_DATA_ENCODING_NONE: return AutoEncoding;
139 case GPGME_DATA_ENCODING_BINARY: return BinaryEncoding;
140 case GPGME_DATA_ENCODING_BASE64: return Base64Encoding;
141 case GPGME_DATA_ENCODING_ARMOR: return ArmorEncoding;
142 }
143 return AutoEncoding;
144}
145
146GpgME::Error GpgME::Data::setEncoding( Encoding enc ) {
147 gpgme_data_encoding_t ge = GPGME_DATA_ENCODING_NONE;
148 switch ( enc ) {
149 case AutoEncoding: ge = GPGME_DATA_ENCODING_NONE; break;
150 case BinaryEncoding: ge = GPGME_DATA_ENCODING_BINARY; break;
151 case Base64Encoding: ge = GPGME_DATA_ENCODING_BASE64; break;
152 case ArmorEncoding: ge = GPGME_DATA_ENCODING_ARMOR; break;
153 }
154 return Error( gpgme_data_set_encoding( d->data, ge ) );
155}
156
157char * GpgME::Data::fileName() const {
158#ifdef HAVE_GPGME_DATA_SET_FILE_NAME
159 return gpgme_data_get_file_name( d->data );
160#else
161 return 0;
162#endif
163}
164
165GpgME::Error GpgME::Data::setFileName( const char * name ) {
166#ifdef HAVE_GPGME_DATA_SET_FILE_NAME
167 return Error( gpgme_data_set_file_name( d->data, name ) );
168#else
169 (void)name;
170 return Error();
171#endif
172}
173
174ssize_t GpgME::Data::read( void * buffer, size_t length ) {
175 return gpgme_data_read( d->data, buffer, length );
176}
177
178ssize_t GpgME::Data::write( const void * buffer, size_t length ) {
179 return gpgme_data_write( d->data, buffer, length );
180}
181
182off_t GpgME::Data::seek( off_t offset, int whence ) {
183 return gpgme_data_seek( d->data, offset, whence );
184}
185