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