2 * @brief Common cpp helper stuff
4 * Copyright (C) 2018 Intevation GmbH
6 * This file is part of GpgOL.
8 * GpgOL is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * GpgOL is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
33 #include <gpgme++/context.h>
34 #include <gpgme++/error.h>
35 #include <gpgme++/configuration.h>
40 release_cArray (char **carray)
44 for (int idx = 0; carray[idx]; idx++)
55 s.erase (std::find_if (s.rbegin(), s.rend(), [] (int ch) {
56 return !std::isspace(ch);
63 s.erase (s.begin(), std::find_if (s.begin(), s.end(), [] (int ch) {
64 return !std::isspace(ch);
76 join(const std::vector<std::string>& v, const char *c, std::string& s)
79 for (auto p = v.begin(); p != v.end(); ++p)
90 vector_to_cArray(const std::vector<std::string> &vec)
92 char ** ret = (char**) xmalloc (sizeof (char*) * (vec.size() + 1));
93 for (size_t i = 0; i < vec.size(); i++)
95 ret[i] = xstrdup (vec[i].c_str());
97 ret[vec.size()] = NULL;
101 std::vector <std::string>
102 cArray_to_vector(const char **cArray)
104 std::vector<std::string> ret;
111 for (int i = 0; cArray[i]; i++)
113 ret.push_back (std::string (cArray[i]));
121 /* We cache the values only once. A change requires restart.
122 This is because checking this is very expensive as gpgconf
123 spawns each process to query the settings. */
133 const auto components = GpgME::Configuration::Component::load (err);
134 log_debug ("%s:%s: Checking for de-vs mode.",
138 log_error ("%s:%s: Failed to get gpgconf components: %s",
139 SRCNAME, __func__, err.asString ());
143 for (const auto &component: components)
145 if (component.name () && !strcmp (component.name (), "gpg"))
147 for (const auto &option: component.options ())
149 if (option.name () && !strcmp (option.name (), "compliance") &&
150 option.currentValue ().stringValue () &&
151 !stricmp (option.currentValue ().stringValue (), "de-vs"))
153 log_debug ("%s:%s: Detected de-vs mode",
167 std::map<std::string, std::string>
168 get_registry_subkeys (const char *path)
171 std::map<std::string, std::string> ret;
173 std::string regPath = GPGOL_REGPATH;
177 if (RegOpenKeyEx (HKEY_CURRENT_USER,
179 0, KEY_ENUMERATE_SUB_KEYS | KEY_READ,
180 &theKey) != ERROR_SUCCESS)
190 DWORD err = RegQueryInfoKey (theKey,
203 if (err != ERROR_SUCCESS)
206 RegCloseKey (theKey);
210 /* Add space for NULL */
214 char name[maxValueName + 1];
215 char value[maxValueLen + 1];
216 for (int i = 0; i < values; i++)
218 DWORD nameLen = maxValueName;
219 err = RegEnumValue (theKey, i,
227 if (err != ERROR_SUCCESS)
234 DWORD valueLen = maxValueLen;
235 err = RegQueryValueEx (theKey, name,
237 (BYTE*)value, &valueLen);
239 if (err != ERROR_SUCCESS)
249 ret.insert (std::make_pair (std::string (name, nameLen),
250 std::string (value, valueLen)));
252 RegCloseKey (theKey);
256 template<typename Out> void
257 internal_split (const std::string &s, char delim, Out result) {
258 std::stringstream ss(s);
260 while (std::getline (ss, item, delim))
266 std::vector<std::string>
267 gpgol_split (const std::string &s, char delim)
269 std::vector<std::string> elems;
270 internal_split (s, delim, std::back_inserter (elems));
275 string_to_hex(const std::string& input)
277 static const char* const lut = "0123456789ABCDEF";
278 size_t len = input.length();
281 output.reserve (3 * len + (len * 3 / 26));
282 for (size_t i = 0; i < len; ++i)
284 const unsigned char c = input[i];
285 output.push_back (lut[c >> 4]);
286 output.push_back (lut[c & 15]);
287 output.push_back (' ');
290 output.push_back ('\n');
297 is_binary (const std::string &input)
299 for (int i = 0; i < input.size() - 1; ++i)
301 const unsigned char c = input[i];
302 if (c < 32 && c != 0x0d && c != 0x0a)
311 to_cstr (const GpgME::Protocol &prot)
313 return prot == GpgME::CMS ? "S/MIME" :
314 prot == GpgME::OpenPGP ? "OpenPGP" :