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