Branch data Line data Source code
1 : : // Copyright (C) 2021 The Qt Company Ltd.
2 : : // Copyright (C) 2019 Luxoft Sweden AB
3 : : // Copyright (C) 2018 Pelagicore AG
4 : : // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
5 : :
6 : : #ifndef LIBCRYPTOFUNCTION_H
7 : : #define LIBCRYPTOFUNCTION_H
8 : :
9 : : #include <QtCore/qglobal.h>
10 : : #include <utility>
11 : : #include <QtAppManCommon/global.h>
12 : :
13 : : QT_FORWARD_DECLARE_CLASS(QLibrary)
14 : :
15 : : #if defined(_MSC_VER) && (_MSC_VER <= 1800)
16 : : namespace std {
17 : : template <typename T> static typename std::add_rvalue_reference<T>::type declval();
18 : : }
19 : : #endif
20 : :
21 : : QT_BEGIN_NAMESPACE_AM
22 : :
23 : : namespace Cryptography {
24 : :
25 : : template <typename R> class LibCryptoResult
26 : : {
27 : : public:
28 : : LibCryptoResult(R r) : m_r(r) { }
29 : : LibCryptoResult(const LibCryptoResult &other) { m_r = other.m_r; }
30 : : LibCryptoResult operator=(const LibCryptoResult &that) { m_r = that.m_r; return *this; }
31 : : ~LibCryptoResult() { }
32 : 0 : R result() { return m_r; }
33 : : private:
34 : : R m_r;
35 : : };
36 : :
37 : : template<> class LibCryptoResult<void>
38 : : {
39 : : public:
40 : : LibCryptoResult() { }
41 : : void result() { }
42 : : };
43 : :
44 : : class LibCryptoFunctionBase
45 : : {
46 : : public:
47 : : static bool initialize(bool loadOpenSsl3LegacyProvider);
48 [ - + - + ]: 54 : static inline bool isOpenSSL11() { return s_isOpenSSL11; }
49 : : static inline bool isOpenSSL30() { return s_isOpenSSL30; }
50 : :
51 : : protected:
52 : : LibCryptoFunctionBase(const char *symbol);
53 : :
54 : : void resolve();
55 : :
56 : : const char *m_symbol;
57 : : void (*m_functionPtr)() = nullptr;
58 : :
59 : : private:
60 : : static QLibrary *s_library;
61 : : static bool s_isOpenSSL11;
62 : : static bool s_isOpenSSL30;
63 : : bool m_tried = false;
64 : : };
65 : :
66 : : template <typename F>
67 : : class LibCryptoFunction : protected LibCryptoFunctionBase
68 : : {
69 : : template <typename Result, typename ...Args> static Result returnType(Result (*)(Args...));
70 : : typedef decltype(returnType(std::declval<F>())) R;
71 : :
72 : : LibCryptoResult<R> m_defaultResult;
73 : :
74 : : public:
75 : : LibCryptoFunction(const char *symbol)
76 : : : LibCryptoFunctionBase(symbol)
77 : : { }
78 : :
79 : : LibCryptoFunction(const char *symbol, const LibCryptoResult<R> &defaultResult)
80 : : : LibCryptoFunctionBase(symbol)
81 : : , m_defaultResult(defaultResult)
82 : : { }
83 : :
84 : 722 : F functionPointer()
85 : : {
86 [ + + + + ]: 65 : if (Q_UNLIKELY(!m_functionPtr))
87 [ + - ]: 67 : resolve();
88 [ + - - - ]: 65 : return reinterpret_cast<F>(m_functionPtr);
89 : : }
90 : :
91 : : template <typename ...Args>
92 [ + + ]: 665 : R operator()(Args &&...args)
93 : : {
94 [ + + ]: 665 : if (Q_UNLIKELY(!functionPointer()))
95 : 0 : return m_defaultResult.result();
96 : 665 : return std::forward<F>(reinterpret_cast<F>(m_functionPtr))(std::forward<Args>(args)...);
97 : : }
98 : : };
99 : :
100 : : #define QT_AM_LIBCRYPTO_FUNCTION(f, typeof_f, ...) Cryptography::LibCryptoFunction<typeof_f> am_ ## f(#f, ##__VA_ARGS__)
101 : :
102 : : }
103 : :
104 : : QT_END_NAMESPACE_AM
105 : :
106 : : #endif // LIBCRYPTOFUNCTION_H
|