1 | /* |
2 | util.h - some inline helper functions |
3 | Copyright (C) 2004 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 | // -*- c++ -*- |
24 | #ifndef __GPGMEPP_UTIL_H__ |
25 | #define __GPGMEPP_UTIL_H__ |
26 | |
27 | #include <gpgme++/global.h> |
28 | #include <gpgme++/notation.h> |
29 | |
30 | #include <gpgme.h> |
31 | |
32 | #ifndef NDEBUG |
33 | #include <iostream> |
34 | #endif |
35 | #include <sstream> |
36 | #include <string> |
37 | |
38 | static inline const char * protect( const char * s ) { |
39 | return s ? s : "<null>" ; |
40 | } |
41 | |
42 | static inline gpgme_error_t make_error( gpgme_err_code_t code ) { |
43 | return gpgme_err_make( (gpgme_err_source_t)22, code ); |
44 | } |
45 | |
46 | static inline unsigned long to_pid( const std::string & s ) { |
47 | std::stringstream ss( s ); |
48 | unsigned int result; |
49 | if ( ss >> result ) { |
50 | return result; |
51 | } else { |
52 | return 0U; |
53 | } |
54 | } |
55 | |
56 | static inline gpgme_keylist_mode_t add_to_gpgme_keylist_mode_t( unsigned int oldmode, unsigned int newmodes ) { |
57 | if ( newmodes & GpgME::Local ) { |
58 | oldmode |= GPGME_KEYLIST_MODE_LOCAL; |
59 | } |
60 | if ( newmodes & GpgME::Extern ) { |
61 | oldmode |= GPGME_KEYLIST_MODE_EXTERN; |
62 | } |
63 | if ( newmodes & GpgME::Signatures ) { |
64 | oldmode |= GPGME_KEYLIST_MODE_SIGS; |
65 | } |
66 | if ( newmodes & GpgME::SignatureNotations ) { |
67 | #ifdef HAVE_GPGME_KEYLIST_MODE_SIG_NOTATIONS |
68 | oldmode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS; |
69 | #elif !defined(NDEBUG) |
70 | ;//std::cerr << "GpgME: ignoring SignatureNotations keylist flag (gpgme too old)." << std::endl; |
71 | #endif |
72 | } |
73 | if ( newmodes & GpgME::Ephemeral ) { |
74 | #ifdef HAVE_GPGME_KEYLIST_MODE_EPHEMERAL |
75 | oldmode |= GPGME_KEYLIST_MODE_EPHEMERAL; |
76 | #elif !defined(NDEBUG) |
77 | ;//std::cerr << "GpgME: ignoring Ephemeral keylist flag (gpgme too old)." << std::endl; |
78 | #endif |
79 | } |
80 | if ( newmodes & GpgME::Validate ) { |
81 | oldmode |= GPGME_KEYLIST_MODE_VALIDATE; |
82 | } |
83 | #ifndef NDEBUG |
84 | if ( newmodes & ~( GpgME::Local | GpgME::Extern | GpgME::Signatures | GpgME::SignatureNotations | GpgME::Ephemeral | GpgME::Validate ) ) { |
85 | //std::cerr << "GpgME::Context: keylist mode must be one of Local, " |
86 | //"Extern, Signatures, SignatureNotations, or Validate, or a combination thereof!" << std::endl; |
87 | } |
88 | #endif |
89 | return static_cast<gpgme_keylist_mode_t>( oldmode ); |
90 | } |
91 | |
92 | static inline unsigned int convert_from_gpgme_keylist_mode_t( unsigned int mode ) { |
93 | unsigned int result = 0; |
94 | if ( mode & GPGME_KEYLIST_MODE_LOCAL ) { |
95 | result |= GpgME::Local; |
96 | } |
97 | if ( mode & GPGME_KEYLIST_MODE_EXTERN ) { |
98 | result |= GpgME::Extern; |
99 | } |
100 | if ( mode & GPGME_KEYLIST_MODE_SIGS ) { |
101 | result |= GpgME::Signatures; |
102 | } |
103 | #ifdef HAVE_GPGME_KEYLIST_MODE_SIG_NOTATIONS |
104 | if ( mode & GPGME_KEYLIST_MODE_SIG_NOTATIONS ) { |
105 | result |= GpgME::SignatureNotations; |
106 | } |
107 | #endif |
108 | #ifdef HAVE_GPGME_KEYLIST_MODE_EPHEMERAL |
109 | if ( mode & GPGME_KEYLIST_MODE_EPHEMERAL ) { |
110 | result |= GpgME::Ephemeral; |
111 | } |
112 | #endif |
113 | if ( mode & GPGME_KEYLIST_MODE_VALIDATE ) { |
114 | result |= GpgME::Validate; |
115 | } |
116 | #ifndef NDEBUG |
117 | if ( mode & ~( GPGME_KEYLIST_MODE_LOCAL | |
118 | GPGME_KEYLIST_MODE_EXTERN | |
119 | #ifdef HAVE_GPGME_KEYLIST_MODE_SIG_NOTATIONS |
120 | GPGME_KEYLIST_MODE_SIG_NOTATIONS | |
121 | #endif |
122 | #ifdef HAVE_GPGME_KEYLIST_MODE_EPHEMERAL |
123 | GPGME_KEYLIST_MODE_EPHEMERAL | |
124 | #endif |
125 | GPGME_KEYLIST_MODE_VALIDATE | |
126 | GPGME_KEYLIST_MODE_SIGS ) ) { |
127 | //std::cerr << "GpgME: WARNING: gpgme_get_keylist_mode() returned an unknown flag!" << std::endl; |
128 | } |
129 | #endif // NDEBUG |
130 | return result; |
131 | } |
132 | |
133 | static inline GpgME::Notation::Flags convert_from_gpgme_sig_notation_flags_t( unsigned int flags ) { |
134 | #ifdef HAVE_GPGME_SIG_NOTATION_FLAGS_T |
135 | unsigned int result = 0; |
136 | #ifdef HAVE_GPGME_SIG_NOTATION_HUMAN_READABLE |
137 | if ( flags & GPGME_SIG_NOTATION_HUMAN_READABLE ) { |
138 | result |= GpgME::Notation::HumanReadable ; |
139 | } |
140 | #endif |
141 | #ifdef HAVE_GPGME_SIG_NOTATION_CRITICAL |
142 | if ( flags & GPGME_SIG_NOTATION_CRITICAL ) { |
143 | result |= GpgME::Notation::Critical ; |
144 | } |
145 | #endif |
146 | return static_cast<GpgME::Notation::Flags>( result ); |
147 | #else |
148 | return GpgME::Notation::NoFlags; |
149 | #endif |
150 | } |
151 | #ifdef HAVE_GPGME_SIG_NOTATION_FLAGS_T |
152 | static inline gpgme_sig_notation_flags_t add_to_gpgme_sig_notation_flags_t( unsigned int oldflags, unsigned int newflags ) { |
153 | unsigned int result = oldflags; |
154 | if ( newflags & GpgME::Notation::HumanReadable ) { |
155 | #ifdef HAVE_GPGME_SIG_NOTATION_HUMAN_READABLE |
156 | result |= GPGME_SIG_NOTATION_HUMAN_READABLE; |
157 | #elif !defined(NDEBUG) |
158 | //std::cerr << "GpgME::Context: ignoring HumanReadable signature notation flag (gpgme too old)" << std::endl; |
159 | #endif |
160 | } |
161 | if ( newflags & GpgME::Notation::Critical ) { |
162 | #ifdef HAVE_GPGME_SIG_NOTATION_CRITICAL |
163 | result |= GPGME_SIG_NOTATION_CRITICAL; |
164 | #elif !defined(NDEBUG) |
165 | //std::cerr << "GpgME::Context: ignoring Critical signature notation flag (gpgme too old)" << std::endl; |
166 | #endif |
167 | } |
168 | return static_cast<gpgme_sig_notation_flags_t>( result ); |
169 | } |
170 | #endif |
171 | |
172 | #endif // __GPGMEPP_UTIL_H__ |
173 | |