aboutsummaryrefslogtreecommitdiff
path: root/lib/e2ee/qolmutils.h
blob: 17eee7a393c3145a37864f56aa8914650e4c4c1d (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
// SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#pragma once

#include <QByteArray>

#include "e2ee/e2ee.h"

namespace Quotient {

// Convert PicklingMode to key
QUOTIENT_API QByteArray toKey(const PicklingMode &mode);

class QUOTIENT_API RandomBuffer : public QByteArray {
public:
    explicit RandomBuffer(size_t size);
    ~RandomBuffer() { clear(); }

    // NOLINTNEXTLINE(google-explicit-constructor)
    QUO_IMPLICIT operator void*() { return data(); }
    char* chars() { return data(); }
    uint8_t* bytes() { return reinterpret_cast<uint8_t*>(data()); }

    Q_DISABLE_COPY(RandomBuffer)
    RandomBuffer(RandomBuffer&&) = default;
    void operator=(RandomBuffer&&) = delete;
};

[[deprecated("Create RandomBuffer directly")]] inline auto getRandom(
    size_t bufferSize)
{
    return RandomBuffer(bufferSize);
}

#define QOLM_INTERNAL_ERROR_X(Message_, LastError_) \
    qFatal("%s, internal error: %s", Message_, LastError_)

#define QOLM_INTERNAL_ERROR(Message_) \
    QOLM_INTERNAL_ERROR_X(Message_, lastError())

#define QOLM_FAIL_OR_LOG_X(InternalCondition_, Message_, LastErrorText_)   \
    do {                                                                   \
        const QString errorMsg{ (Message_) };                              \
        if (InternalCondition_)                                            \
            QOLM_INTERNAL_ERROR_X(qPrintable(errorMsg), (LastErrorText_)); \
        qWarning(E2EE).nospace() << errorMsg << ": " << (LastErrorText_);  \
    } while (false) /* End of macro */

#define QOLM_FAIL_OR_LOG(InternalFailureValue_, Message_)                      \
    QOLM_FAIL_OR_LOG_X(lastErrorCode() == (InternalFailureValue_), (Message_), \
                       lastError())

} // namespace Quotient