Enable customizing the save statistics time interval
[qBittorrent.git] / src / base / bittorrent / ltqbitarray.cpp
blob270344df1561014c070abfa59a713fd3b99b944b
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022 Mike Tzou (Chocobo1)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "ltqbitarray.h"
31 #include <libtorrent/bitfield.hpp>
33 #include <QBitArray>
34 #include <QVarLengthArray>
36 namespace
38 unsigned char reverseByte(const unsigned char byte)
40 // https://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
41 static const unsigned char table[] =
43 #define R2(n) n, (n + (2 * 64)), (n + 64), (n + (3 * 64))
44 #define R4(n) R2(n), R2(n + (2 * 16)), R2(n + 16), R2(n + (3 * 16))
45 #define R6(n) R4(n), R4(n + (2 * 4)), R4(n + 4), R4(n + (3 * 4))
46 R6(0), R6(2), R6(1), R6(3)
47 #undef R6
48 #undef R4
49 #undef R2
51 return table[byte];
55 namespace BitTorrent::LT
57 QBitArray toQBitArray(const lt::bitfield &bits)
59 const int STACK_ALLOC_SIZE = 10 * 1024;
61 const char *bitsData = bits.data();
62 const int dataLength = (bits.size() + 7) / 8;
64 QVarLengthArray<char, STACK_ALLOC_SIZE> tmp(dataLength);
65 for (int i = 0; i < dataLength; ++i)
66 tmp[i] = reverseByte(bitsData[i]);
68 return QBitArray::fromBits(tmp.data(), bits.size());