| 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 | #include "coding.h" |
| 10 | |
| 11 | namespace Baloo { |
| 12 | |
| 13 | static inline int encodeVarint32Internal(char* dst, quint32 v) { |
| 14 | // Operate on characters as unsigneds |
| 15 | unsigned char* ptr = reinterpret_cast<unsigned char*>(dst); |
| 16 | static const int B = 128; |
| 17 | if (v < (1<<7)) { |
| 18 | ptr[0] = v; |
| 19 | return 1; |
| 20 | } |
| 21 | if (v < (1<<14)) { |
| 22 | ptr[0] = v | B; |
| 23 | ptr[1] = v>>7; |
| 24 | return 2; |
| 25 | } |
| 26 | if (v < (1<<21)) { |
| 27 | ptr[0] = v | B; |
| 28 | ptr[1] = (v>>7) | B; |
| 29 | ptr[2] = v>>14; |
| 30 | return 3; |
| 31 | } |
| 32 | if (v < (1<<28)) { |
| 33 | ptr[0] = v | B; |
| 34 | ptr[1] = (v>>7) | B; |
| 35 | ptr[2] = (v>>14) | B; |
| 36 | ptr[3] = v>>21; |
| 37 | return 4; |
| 38 | } |
| 39 | |
| 40 | ptr[0] = v | B; |
| 41 | ptr[1] = (v>>7) | B; |
| 42 | ptr[2] = (v>>14) | B; |
| 43 | ptr[3] = (v>>21) | B; |
| 44 | ptr[4] = v>>28; |
| 45 | return 5; |
| 46 | } |
| 47 | |
| 48 | static inline void putVarint32Internal(char* dst, quint32 v, int &pos) |
| 49 | { |
| 50 | pos += encodeVarint32Internal(dst: &dst[pos], v); |
| 51 | } |
| 52 | |
| 53 | void putDifferentialVarInt32(QByteArray &temporaryStorage, QByteArray* dst, const QVector<quint32>& values) |
| 54 | { |
| 55 | temporaryStorage.resize(size: (values.size() + 1) * 5); // max size, correct size will be held in pos |
| 56 | int pos = 0; |
| 57 | putVarint32Internal(dst: temporaryStorage.data(), v: values.size(), pos); |
| 58 | |
| 59 | quint32 v = 0; |
| 60 | const auto itEnd = values.cend(); |
| 61 | for (auto it = values.cbegin(); it != itEnd; ++it) { |
| 62 | const quint32 n = *it; |
| 63 | putVarint32Internal(dst: temporaryStorage.data(), v: n - v, pos); |
| 64 | v = n; |
| 65 | } |
| 66 | dst->append(s: temporaryStorage.constData(), len: pos); |
| 67 | } |
| 68 | |
| 69 | char* getDifferentialVarInt32(char* p, char* limit, QVector<quint32>* values) |
| 70 | { |
| 71 | quint32 size = 0; |
| 72 | p = getVarint32Ptr(p, limit, value: &size); |
| 73 | values->resize(size); |
| 74 | |
| 75 | auto it = values->begin(); |
| 76 | auto end = values->end(); |
| 77 | |
| 78 | quint32 v = 0; |
| 79 | while (p && it != end) { |
| 80 | quint32 n = 0; |
| 81 | p = getVarint32Ptr(p, limit, value: &n); |
| 82 | |
| 83 | *it = (n + v); |
| 84 | v += n; |
| 85 | ++it; |
| 86 | } |
| 87 | values->erase(abegin: it, aend: end); |
| 88 | |
| 89 | return p; |
| 90 | } |
| 91 | |
| 92 | char* getVarint32PtrFallback(char* p, char* limit, quint32* value) |
| 93 | { |
| 94 | quint32 result = 0; |
| 95 | for (quint32 shift = 0; shift <= 28 && p < limit; shift += 7) { |
| 96 | quint32 byte = *(reinterpret_cast<const unsigned char*>(p)); |
| 97 | p++; |
| 98 | if (byte & 128) { |
| 99 | // More bytes are present |
| 100 | result |= ((byte & 127) << shift); |
| 101 | } else { |
| 102 | result |= (byte << shift); |
| 103 | *value = result; |
| 104 | return p; |
| 105 | } |
| 106 | } |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |