2 util.h - some inline helper functions
3 Copyright (C) 2004 Klarälvdalens Datakonsult AB
4 2016 Bundesamt für Sicherheit in der Informationstechnik
5 Software engineering by Intevation GmbH
7 This file is part of GPGME++.
9 GPGME++ is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 GPGME++ is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public License
20 along with GPGME++; see the file COPYING.LIB. If not, write to the
21 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
26 #ifndef __GPGMEPP_UTIL_H__
27 #define __GPGMEPP_UTIL_H__
40 static inline const char *protect(const char *s)
42 return s ? s : "<null>" ;
45 static inline gpgme_error_t make_error(gpgme_err_code_t code)
47 return gpgme_err_make((gpgme_err_source_t)22, code);
50 static inline unsigned long to_pid(const std::string &s)
52 std::stringstream ss(s);
61 static inline gpgme_keylist_mode_t add_to_gpgme_keylist_mode_t(unsigned int oldmode, unsigned int newmodes)
63 if (newmodes & GpgME::Local) {
64 oldmode |= GPGME_KEYLIST_MODE_LOCAL;
66 if (newmodes & GpgME::Extern) {
67 oldmode |= GPGME_KEYLIST_MODE_EXTERN;
69 if (newmodes & GpgME::Signatures) {
70 oldmode |= GPGME_KEYLIST_MODE_SIGS;
72 if (newmodes & GpgME::SignatureNotations) {
73 oldmode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS;
75 if (newmodes & GpgME::Ephemeral) {
76 oldmode |= GPGME_KEYLIST_MODE_EPHEMERAL;
78 if (newmodes & GpgME::Validate) {
79 oldmode |= GPGME_KEYLIST_MODE_VALIDATE;
81 if (newmodes & GpgME::WithTofu) {
82 oldmode |= GPGME_KEYLIST_MODE_WITH_TOFU;
84 if (newmodes & GpgME::WithKeygrip) {
85 oldmode |= GPGME_KEYLIST_MODE_WITH_KEYGRIP;
88 if (newmodes & ~(GpgME::Local | GpgME::Extern | GpgME::Signatures | GpgME::SignatureNotations | GpgME::Ephemeral | GpgME::Validate)) {
89 //std::cerr << "GpgME::Context: keylist mode must be one of Local, "
90 //"Extern, Signatures, SignatureNotations, or Validate, or a combination thereof!" << std::endl;
93 return static_cast<gpgme_keylist_mode_t>(oldmode);
96 static inline unsigned int convert_from_gpgme_keylist_mode_t(unsigned int mode)
98 unsigned int result = 0;
99 if (mode & GPGME_KEYLIST_MODE_LOCAL) {
100 result |= GpgME::Local;
102 if (mode & GPGME_KEYLIST_MODE_EXTERN) {
103 result |= GpgME::Extern;
105 if (mode & GPGME_KEYLIST_MODE_SIGS) {
106 result |= GpgME::Signatures;
108 if (mode & GPGME_KEYLIST_MODE_SIG_NOTATIONS) {
109 result |= GpgME::SignatureNotations;
111 if (mode & GPGME_KEYLIST_MODE_EPHEMERAL) {
112 result |= GpgME::Ephemeral;
114 if (mode & GPGME_KEYLIST_MODE_VALIDATE) {
115 result |= GpgME::Validate;
118 if (mode & ~(GPGME_KEYLIST_MODE_LOCAL |
119 GPGME_KEYLIST_MODE_EXTERN |
120 GPGME_KEYLIST_MODE_SIG_NOTATIONS |
121 GPGME_KEYLIST_MODE_EPHEMERAL |
122 GPGME_KEYLIST_MODE_VALIDATE |
123 GPGME_KEYLIST_MODE_SIGS)) {
124 //std::cerr << "GpgME: WARNING: gpgme_get_keylist_mode() returned an unknown flag!" << std::endl;
130 static inline GpgME::Notation::Flags convert_from_gpgme_sig_notation_flags_t(unsigned int flags)
132 unsigned int result = 0;
133 if (flags & GPGME_SIG_NOTATION_HUMAN_READABLE) {
134 result |= GpgME::Notation::HumanReadable ;
136 if (flags & GPGME_SIG_NOTATION_CRITICAL) {
137 result |= GpgME::Notation::Critical ;
139 return static_cast<GpgME::Notation::Flags>(result);
142 static inline gpgme_sig_notation_flags_t add_to_gpgme_sig_notation_flags_t(unsigned int oldflags, unsigned int newflags)
144 unsigned int result = oldflags;
145 if (newflags & GpgME::Notation::HumanReadable) {
146 result |= GPGME_SIG_NOTATION_HUMAN_READABLE;
148 if (newflags & GpgME::Notation::Critical) {
149 result |= GPGME_SIG_NOTATION_CRITICAL;
151 return static_cast<gpgme_sig_notation_flags_t>(result);
154 #endif // __GPGMEPP_UTIL_H__