summaryrefslogtreecommitdiff
path: root/src/crypto-lib/libcryptofunction.h
blob: d13bcd2a5f44dc9940626f617ad258485ac92828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#pragma once

#include <QtCore/qglobal.h>
#include <utility>
#include <QtAppManCommon/global.h>

QT_FORWARD_DECLARE_CLASS(QLibrary)

#if defined(_MSC_VER) && (_MSC_VER <= 1800)
namespace std {
    template <typename T> static typename std::add_rvalue_reference<T>::type declval();
}
#endif

QT_BEGIN_NAMESPACE_AM

namespace Cryptography {

template <typename R> class LibCryptoResult
{
public:
    LibCryptoResult(R r) : m_r(r) { }
    LibCryptoResult(const LibCryptoResult &other) { m_r = other.m_r; }
    LibCryptoResult operator=(const LibCryptoResult &that) { m_r = that.m_r; return *this; }
    ~LibCryptoResult() { }
    R result() { return m_r; }
private:
    R m_r;
};

template<> class LibCryptoResult<void>
{
public:
    LibCryptoResult() { }
    void result() { }
};

class LibCryptoFunctionBase
{
public:
    static bool initialize(bool loadOpenSsl3LegacyProvider);
    static inline bool isOpenSSL11() { return s_isOpenSSL11; }
    static inline bool isOpenSSL30() { return s_isOpenSSL30; }

protected:
    LibCryptoFunctionBase(const char *symbol);

    void resolve();

    const char *m_symbol;
    void (*m_functionPtr)() = nullptr;

private:
    static QLibrary *s_library;
    static bool s_isOpenSSL11;
    static bool s_isOpenSSL30;
    bool m_tried = false;
};

template <typename F>
class LibCryptoFunction : protected LibCryptoFunctionBase
{
    template <typename Result, typename ...Args> static Result returnType(Result (*)(Args...));
    typedef decltype(returnType(std::declval<F>())) R;

    LibCryptoResult<R> m_defaultResult;

public:
    LibCryptoFunction(const char *symbol)
        : LibCryptoFunctionBase(symbol)
    { }

    LibCryptoFunction(const char *symbol, const LibCryptoResult<R> &defaultResult)
        : LibCryptoFunctionBase(symbol)
        , m_defaultResult(defaultResult)
    { }

    F functionPointer()
    {
        if (Q_UNLIKELY(!m_functionPtr))
            resolve();
        return reinterpret_cast<F>(m_functionPtr);
    }

    template <typename ...Args>
    R operator()(Args &&...args)
    {
        if (Q_UNLIKELY(!functionPointer()))
            return m_defaultResult.result();
        return std::forward<F>(reinterpret_cast<F>(m_functionPtr))(std::forward<Args>(args)...);
    }
};

#define AM_LIBCRYPTO_FUNCTION(f, typeof_f, ...) Cryptography::LibCryptoFunction<typeof_f> am_ ## f(#f, ##__VA_ARGS__)

}

QT_END_NAMESPACE_AM