1 | /* |
2 | This file is part of the KDE Baloo project. |
3 | SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org> |
4 | SPDX-FileCopyrightText: 2011 The LevelDB Authors. All rights reserved. |
5 | |
6 | SPDX-License-Identifier: LGPL-2.1-or-later AND BSD-3-Clause |
7 | */ |
8 | |
9 | #ifndef BALOO_STORAGE_LEVELDB_UTIL_CODING_H |
10 | #define BALOO_STORAGE_LEVELDB_UTIL_CODING_H |
11 | |
12 | #include <QByteArray> |
13 | #include <QVector> |
14 | |
15 | namespace Baloo { |
16 | |
17 | /* |
18 | * This is a stripped down version of various encode/decode functions for |
19 | * 32/64 bit fixed/variable data types. If you need other functions than the |
20 | * ones available here you can take a look in the git baloo history |
21 | */ |
22 | |
23 | inline void putFixed64(QByteArray* dst, quint64 value) |
24 | { |
25 | dst->append(s: reinterpret_cast<const char*>(&value), len: sizeof(value)); |
26 | } |
27 | |
28 | /* |
29 | * temporaryStorage is used to avoid an internal allocation of a temporary |
30 | * buffer which is needed for serialization. Since this function is normally |
31 | * called inside a loop, the temporary buffer must not be reallocated on every |
32 | * call. |
33 | */ |
34 | void putDifferentialVarInt32(QByteArray &temporaryStorage, QByteArray* dst, const QVector<quint32>& values); |
35 | char* getDifferentialVarInt32(char* input, char* limit, QVector<quint32>* values); |
36 | extern const char* getVarint32Ptr(const char* p, const char* limit, quint32* v); |
37 | |
38 | inline quint64 decodeFixed64(const char* ptr) |
39 | { |
40 | // Load the raw bytes |
41 | quint64 result; |
42 | memcpy(dest: &result, src: ptr, n: sizeof(result)); // gcc optimizes this to a plain load |
43 | return result; |
44 | } |
45 | |
46 | // Internal routine for use by fallback path of GetVarint32Ptr |
47 | extern char* getVarint32PtrFallback(char* p, char* limit, quint32* value); |
48 | inline char* getVarint32Ptr(char* p, char* limit, quint32* value) |
49 | { |
50 | if (p >= limit) { |
51 | return nullptr; |
52 | } |
53 | |
54 | quint32 result = *(reinterpret_cast<const unsigned char*>(p)); |
55 | if ((result & 128) == 0) { |
56 | *value = result; |
57 | return p + 1; |
58 | } |
59 | |
60 | return getVarint32PtrFallback(p, limit, value); |
61 | } |
62 | |
63 | } |
64 | |
65 | #endif |
66 | |