Line data Source code
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 :
40 15753 : static inline const char *protect(const char *s)
41 : {
42 15753 : return s ? s : "<null>" ;
43 : }
44 :
45 0 : static inline gpgme_error_t make_error(gpgme_err_code_t code)
46 : {
47 0 : return gpgme_err_make((gpgme_err_source_t)22, code);
48 : }
49 :
50 0 : static inline unsigned long to_pid(const std::string &s)
51 : {
52 0 : std::stringstream ss(s);
53 : unsigned int result;
54 0 : if (ss >> result) {
55 0 : return result;
56 : } else {
57 0 : return 0U;
58 : }
59 : }
60 :
61 29 : static inline gpgme_keylist_mode_t add_to_gpgme_keylist_mode_t(unsigned int oldmode, unsigned int newmodes)
62 : {
63 29 : if (newmodes & GpgME::Local) {
64 25 : oldmode |= GPGME_KEYLIST_MODE_LOCAL;
65 : }
66 29 : if (newmodes & GpgME::Extern) {
67 1 : oldmode |= GPGME_KEYLIST_MODE_EXTERN;
68 : }
69 29 : if (newmodes & GpgME::Signatures) {
70 15 : oldmode |= GPGME_KEYLIST_MODE_SIGS;
71 : }
72 29 : if (newmodes & GpgME::SignatureNotations) {
73 8 : oldmode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS;
74 : }
75 29 : if (newmodes & GpgME::Ephemeral) {
76 0 : oldmode |= GPGME_KEYLIST_MODE_EPHEMERAL;
77 : }
78 29 : if (newmodes & GpgME::Validate) {
79 15 : oldmode |= GPGME_KEYLIST_MODE_VALIDATE;
80 : }
81 29 : if (newmodes & GpgME::WithTofu) {
82 14 : oldmode |= GPGME_KEYLIST_MODE_WITH_TOFU;
83 : }
84 : #ifndef NDEBUG
85 29 : if (newmodes & ~(GpgME::Local | GpgME::Extern | GpgME::Signatures | GpgME::SignatureNotations | GpgME::Ephemeral | GpgME::Validate)) {
86 : //std::cerr << "GpgME::Context: keylist mode must be one of Local, "
87 : //"Extern, Signatures, SignatureNotations, or Validate, or a combination thereof!" << std::endl;
88 : }
89 : #endif
90 29 : return static_cast<gpgme_keylist_mode_t>(oldmode);
91 : }
92 :
93 14 : static inline unsigned int convert_from_gpgme_keylist_mode_t(unsigned int mode)
94 : {
95 14 : unsigned int result = 0;
96 14 : if (mode & GPGME_KEYLIST_MODE_LOCAL) {
97 14 : result |= GpgME::Local;
98 : }
99 14 : if (mode & GPGME_KEYLIST_MODE_EXTERN) {
100 0 : result |= GpgME::Extern;
101 : }
102 14 : if (mode & GPGME_KEYLIST_MODE_SIGS) {
103 0 : result |= GpgME::Signatures;
104 : }
105 14 : if (mode & GPGME_KEYLIST_MODE_SIG_NOTATIONS) {
106 0 : result |= GpgME::SignatureNotations;
107 : }
108 14 : if (mode & GPGME_KEYLIST_MODE_EPHEMERAL) {
109 0 : result |= GpgME::Ephemeral;
110 : }
111 14 : if (mode & GPGME_KEYLIST_MODE_VALIDATE) {
112 0 : result |= GpgME::Validate;
113 : }
114 : #ifndef NDEBUG
115 14 : if (mode & ~(GPGME_KEYLIST_MODE_LOCAL |
116 : GPGME_KEYLIST_MODE_EXTERN |
117 : GPGME_KEYLIST_MODE_SIG_NOTATIONS |
118 : GPGME_KEYLIST_MODE_EPHEMERAL |
119 : GPGME_KEYLIST_MODE_VALIDATE |
120 : GPGME_KEYLIST_MODE_SIGS)) {
121 : //std::cerr << "GpgME: WARNING: gpgme_get_keylist_mode() returned an unknown flag!" << std::endl;
122 : }
123 : #endif // NDEBUG
124 14 : return result;
125 : }
126 :
127 0 : static inline GpgME::Notation::Flags convert_from_gpgme_sig_notation_flags_t(unsigned int flags)
128 : {
129 0 : unsigned int result = 0;
130 0 : if (flags & GPGME_SIG_NOTATION_HUMAN_READABLE) {
131 0 : result |= GpgME::Notation::HumanReadable ;
132 : }
133 0 : if (flags & GPGME_SIG_NOTATION_CRITICAL) {
134 0 : result |= GpgME::Notation::Critical ;
135 : }
136 0 : return static_cast<GpgME::Notation::Flags>(result);
137 : }
138 :
139 0 : static inline gpgme_sig_notation_flags_t add_to_gpgme_sig_notation_flags_t(unsigned int oldflags, unsigned int newflags)
140 : {
141 0 : unsigned int result = oldflags;
142 0 : if (newflags & GpgME::Notation::HumanReadable) {
143 0 : result |= GPGME_SIG_NOTATION_HUMAN_READABLE;
144 : }
145 0 : if (newflags & GpgME::Notation::Critical) {
146 0 : result |= GPGME_SIG_NOTATION_CRITICAL;
147 : }
148 0 : return static_cast<gpgme_sig_notation_flags_t>(result);
149 : }
150 :
151 : #endif // __GPGMEPP_UTIL_H__
|