Imported Upstream version 1.14.0
[platform/upstream/gpgme.git] / lang / cpp / src / util.h
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 static inline const char *protect(const char *s)
41 {
42     return s ? s : "<null>" ;
43 }
44
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
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
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::Ephemeral) {
76         oldmode |= GPGME_KEYLIST_MODE_EPHEMERAL;
77     }
78     if (newmodes & GpgME::Validate) {
79         oldmode |= GPGME_KEYLIST_MODE_VALIDATE;
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 #ifndef NDEBUG
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;
91     }
92 #endif
93     return static_cast<gpgme_keylist_mode_t>(oldmode);
94 }
95
96 static inline unsigned int convert_from_gpgme_keylist_mode_t(unsigned int mode)
97 {
98     unsigned int result = 0;
99     if (mode & GPGME_KEYLIST_MODE_LOCAL) {
100         result |= GpgME::Local;
101     }
102     if (mode & GPGME_KEYLIST_MODE_EXTERN) {
103         result |= GpgME::Extern;
104     }
105     if (mode & GPGME_KEYLIST_MODE_SIGS) {
106         result |= GpgME::Signatures;
107     }
108     if (mode & GPGME_KEYLIST_MODE_SIG_NOTATIONS) {
109         result |= GpgME::SignatureNotations;
110     }
111     if (mode & GPGME_KEYLIST_MODE_EPHEMERAL) {
112         result |= GpgME::Ephemeral;
113     }
114     if (mode & GPGME_KEYLIST_MODE_VALIDATE) {
115         result |= GpgME::Validate;
116     }
117 #ifndef NDEBUG
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;
125     }
126 #endif // NDEBUG
127     return result;
128 }
129
130 static inline GpgME::Notation::Flags convert_from_gpgme_sig_notation_flags_t(unsigned int flags)
131 {
132     unsigned int result = 0;
133     if (flags & GPGME_SIG_NOTATION_HUMAN_READABLE) {
134         result |= GpgME::Notation::HumanReadable ;
135     }
136     if (flags & GPGME_SIG_NOTATION_CRITICAL) {
137         result |= GpgME::Notation::Critical ;
138     }
139     return static_cast<GpgME::Notation::Flags>(result);
140 }
141
142 static inline gpgme_sig_notation_flags_t  add_to_gpgme_sig_notation_flags_t(unsigned int oldflags, unsigned int newflags)
143 {
144     unsigned int result = oldflags;
145     if (newflags & GpgME::Notation::HumanReadable) {
146         result |= GPGME_SIG_NOTATION_HUMAN_READABLE;
147     }
148     if (newflags & GpgME::Notation::Critical) {
149         result |= GPGME_SIG_NOTATION_CRITICAL;
150     }
151     return static_cast<gpgme_sig_notation_flags_t>(result);
152 }
153
154 #endif // __GPGMEPP_UTIL_H__