File indexing completed on 2024-04-21 11:25:02

0001 /*
0002     This file is part of the KDE Baloo project.
0003     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
0004     SPDX-FileCopyrightText: 2011 The LevelDB Authors. All rights reserved.
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later AND BSD-3-Clause
0007 */
0008 
0009 #ifndef BALOO_STORAGE_LEVELDB_UTIL_CODING_H
0010 #define BALOO_STORAGE_LEVELDB_UTIL_CODING_H
0011 
0012 #include <QByteArray>
0013 #include <QVector>
0014 
0015 namespace Baloo {
0016 
0017 /*
0018  * This is a stripped down version of various encode/decode functions for
0019  * 32/64 bit fixed/variable data types. If you need other functions than the
0020  * ones available here you can take a look in the git baloo history
0021  */
0022 
0023 inline void putFixed64(QByteArray* dst, quint64 value)
0024 {
0025     dst->append(reinterpret_cast<const char*>(&value), sizeof(value));
0026 }
0027 
0028 /*
0029  * temporaryStorage is used to avoid an internal allocation of a temporary
0030  * buffer which is needed for serialization. Since this function is normally
0031  * called inside a loop, the temporary buffer must not be reallocated on every
0032  * call.
0033  */
0034 void putDifferentialVarInt32(QByteArray &temporaryStorage, QByteArray* dst, const QVector<quint32>& values);
0035 char* getDifferentialVarInt32(char* input, char* limit, QVector<quint32>* values);
0036 extern const char* getVarint32Ptr(const char* p, const char* limit, quint32* v);
0037 
0038 inline quint64 decodeFixed64(const char* ptr)
0039 {
0040     // Load the raw bytes
0041     quint64 result;
0042     memcpy(&result, ptr, sizeof(result));  // gcc optimizes this to a plain load
0043     return result;
0044 }
0045 
0046 // Internal routine for use by fallback path of GetVarint32Ptr
0047 extern char* getVarint32PtrFallback(char* p, char* limit, quint32* value);
0048 inline char* getVarint32Ptr(char* p, char* limit, quint32* value)
0049 {
0050     if (p >= limit) {
0051         return nullptr;
0052     }
0053 
0054     quint32 result = *(reinterpret_cast<const unsigned char*>(p));
0055     if ((result & 128) == 0) {
0056         *value = result;
0057         return p + 1;
0058     }
0059 
0060     return getVarint32PtrFallback(p, limit, value);
0061 }
0062 
0063 }
0064 
0065 #endif