2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2020 The Qt Company Ltd.
|
|
|
|
// Copyright (C) 2019 Intel Corporation.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "qbitarray.h"
|
2012-05-18 18:12:35 +00:00
|
|
|
#include <qalgorithms.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <qdatastream.h>
|
|
|
|
#include <qdebug.h>
|
2016-03-15 17:10:12 +00:00
|
|
|
#include <qendian.h>
|
QBitArray: fix potential truncation in QDataStream op>>()
In Qt 5, a QBitArray could not contain more than INT_MAX bits, because
the then-size_type, int, cannot represent more, even if the underlying
storage could hold 8x as much, and the serialisation format, using
unsigned int, could represent 2x.
Therefore, reject old formats with sizes that exceed INT_MAX elements
as corrupt.
Likewise, the Qt 6 serialisation format unconditionally uses 64-bit
sizes, but 32-bit platforms still cannot represent more than
numeric_limits<qsizetype>::max() (= INT_MAX) bits in memory. This is a
valid stream for 64-bit platforms, though, so ideally, this should be
using SizeLimitsExeeded, which, however, is only available from Qt
6.7. So, for now, and until we have SizeLimitsExeeded, mark the stream
as corrupt here, too.
[ChangeLog][QtCore][QBitArray] Fixed undetected overflows in the
deserialisation (opertor>>()) from QDataStream.
Pick-to: 6.5 6.2 5.15
Change-Id: Ib24cf9218c06a3a05185723c77d4313611c2dd40
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 0808beace332631f8100b5700577f10f63e4630c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit d61a3337055e7849471a44ee7c3ce4b8ac39be9d)
2024-01-27 08:17:57 +00:00
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\class QBitArray
|
2012-08-23 10:22:38 +00:00
|
|
|
\inmodule QtCore
|
2011-04-27 10:05:43 +00:00
|
|
|
\brief The QBitArray class provides an array of bits.
|
|
|
|
|
|
|
|
\ingroup tools
|
|
|
|
\ingroup shared
|
|
|
|
\reentrant
|
|
|
|
|
|
|
|
A QBitArray is an array that gives access to individual bits and
|
2012-05-11 00:38:45 +00:00
|
|
|
provides operators (\l{operator&()}{AND}, \l{operator|()}{OR},
|
|
|
|
\l{operator^()}{XOR}, and \l{operator~()}{NOT}) that work on
|
|
|
|
entire arrays of bits. It uses \l{implicit sharing} (copy-on-write)
|
|
|
|
to reduce memory usage and to avoid the needless copying of data.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The following code constructs a QBitArray containing 200 bits
|
|
|
|
initialized to false (0):
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
To initialize the bits to true, either pass \c true as second
|
|
|
|
argument to the constructor, or call fill() later on.
|
|
|
|
|
|
|
|
QBitArray uses 0-based indexes, just like C++ arrays. To access
|
|
|
|
the bit at a particular index position, you can use operator[]().
|
|
|
|
On non-const bit arrays, operator[]() returns a reference to a
|
|
|
|
bit that can be used on the left side of an assignment. For
|
|
|
|
example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 1
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
For technical reasons, it is more efficient to use testBit() and
|
|
|
|
setBit() to access bits in the array than operator[](). For
|
|
|
|
example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 2
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-05-11 00:38:45 +00:00
|
|
|
QBitArray supports \c{&} (\l{operator&()}{AND}), \c{|}
|
|
|
|
(\l{operator|()}{OR}), \c{^} (\l{operator^()}{XOR}),
|
|
|
|
\c{~} (\l{operator~()}{NOT}), as well as
|
2011-04-27 10:05:43 +00:00
|
|
|
\c{&=}, \c{|=}, and \c{^=}. These operators work in the same way
|
|
|
|
as the built-in C++ bitwise operators of the same name. For
|
|
|
|
example:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 3
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
For historical reasons, QBitArray distinguishes between a null
|
|
|
|
bit array and an empty bit array. A \e null bit array is a bit
|
|
|
|
array that is initialized using QBitArray's default constructor.
|
|
|
|
An \e empty bit array is any bit array with size 0. A null bit
|
|
|
|
array is always empty, but an empty bit array isn't necessarily
|
|
|
|
null:
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 4
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
All functions except isNull() treat null bit arrays the same as
|
|
|
|
empty bit arrays; for example, QBitArray() compares equal to
|
|
|
|
QBitArray(0). We recommend that you always use isEmpty() and
|
|
|
|
avoid isNull().
|
|
|
|
|
2020-06-26 06:21:15 +00:00
|
|
|
\sa QByteArray, QList
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-26 10:16:39 +00:00
|
|
|
/*!
|
|
|
|
\fn QBitArray::QBitArray(QBitArray &&other)
|
|
|
|
|
|
|
|
Move-constructs a QBitArray instance, making it point at the same
|
|
|
|
object that \a other was pointing to.
|
|
|
|
|
|
|
|
\since 5.2
|
|
|
|
*/
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*! \fn QBitArray::QBitArray()
|
|
|
|
|
|
|
|
Constructs an empty bit array.
|
|
|
|
|
|
|
|
\sa isEmpty()
|
|
|
|
*/
|
|
|
|
|
2013-08-29 01:22:22 +00:00
|
|
|
/*
|
|
|
|
* QBitArray construction note:
|
|
|
|
*
|
|
|
|
* We overallocate the byte array by 1 byte. The first user bit is at
|
|
|
|
* d.data()[1]. On the extra first byte, we store the difference between the
|
|
|
|
* number of bits in the byte array (including this byte) and the number of
|
2019-08-02 05:56:30 +00:00
|
|
|
* bits in the bit array. Therefore, for a non-empty QBitArray, it's always a
|
|
|
|
* number between 8 and 15. For the empty one, d is the an empty QByteArray and
|
|
|
|
* *d.constData() is the QByteArray's terminating NUL (0) byte.
|
2013-08-29 01:22:22 +00:00
|
|
|
*
|
|
|
|
* This allows for fast calculation of the bit array size:
|
2020-09-23 09:37:40 +00:00
|
|
|
* inline qsizetype size() const { return (d.size() << 3) - *d.constData(); }
|
2013-08-29 01:22:22 +00:00
|
|
|
*/
|
|
|
|
|
2024-01-29 09:27:15 +00:00
|
|
|
static constexpr qsizetype storage_size(qsizetype size)
|
|
|
|
{
|
|
|
|
// avoid overflow when adding 7, by doing the arithmetic in unsigned space:
|
|
|
|
return qsizetype((size_t(size) + 7) / 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr qsizetype allocation_size(qsizetype size)
|
|
|
|
{
|
|
|
|
return size <= 0 ? 0 : storage_size(size) + 1;
|
|
|
|
}
|
|
|
|
|
2024-01-30 10:15:13 +00:00
|
|
|
static void adjust_head_and_tail(char *data, qsizetype storageSize, qsizetype logicalSize)
|
|
|
|
{
|
|
|
|
quint8 *c = reinterpret_cast<quint8 *>(data);
|
|
|
|
// store the difference between storage and logical size in d[0]:
|
|
|
|
*c = quint8(size_t(storageSize) * 8 - logicalSize);
|
|
|
|
// reset unallocated bits to 0:
|
|
|
|
if (logicalSize & 7)
|
|
|
|
*(c + 1 + logicalSize / 8) &= (1 << (logicalSize & 7)) - 1;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
Constructs a bit array containing \a size bits. The bits are
|
|
|
|
initialized with \a value, which defaults to false (0).
|
|
|
|
*/
|
2020-09-23 09:37:40 +00:00
|
|
|
QBitArray::QBitArray(qsizetype size, bool value)
|
2024-01-29 09:27:15 +00:00
|
|
|
: d(allocation_size(size), Qt::Uninitialized)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-05-30 12:51:18 +00:00
|
|
|
Q_ASSERT_X(size >= 0, "QBitArray::QBitArray", "Size must be greater than or equal to 0.");
|
2013-08-29 01:37:00 +00:00
|
|
|
if (size <= 0)
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
2013-08-29 01:37:00 +00:00
|
|
|
|
2020-10-20 10:19:21 +00:00
|
|
|
uchar *c = reinterpret_cast<uchar *>(d.data());
|
2013-08-29 01:37:00 +00:00
|
|
|
memset(c + 1, value ? 0xff : 0, d.size() - 1);
|
2024-01-30 10:15:13 +00:00
|
|
|
adjust_head_and_tail(d.data(), d.size(), size);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn qsizetype QBitArray::size() const
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the number of bits stored in the bit array.
|
|
|
|
|
|
|
|
\sa resize()
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn qsizetype QBitArray::count() const
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Same as size().
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
If \a on is true, this function returns the number of
|
|
|
|
1-bits stored in the bit array; otherwise the number
|
|
|
|
of 0-bits is returned.
|
|
|
|
*/
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype QBitArray::count(bool on) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype numBits = 0;
|
2011-04-27 10:05:43 +00:00
|
|
|
const quint8 *bits = reinterpret_cast<const quint8 *>(d.data()) + 1;
|
2013-09-11 23:25:37 +00:00
|
|
|
|
|
|
|
// the loops below will try to read from *end
|
|
|
|
// it's the QByteArray implicit NUL, so it will not change the bit count
|
|
|
|
const quint8 *const end = reinterpret_cast<const quint8 *>(d.end());
|
|
|
|
|
2013-09-12 22:31:50 +00:00
|
|
|
while (bits + 7 <= end) {
|
2016-03-15 17:10:12 +00:00
|
|
|
quint64 v = qFromUnaligned<quint64>(bits);
|
2013-09-12 22:31:50 +00:00
|
|
|
bits += 8;
|
2020-09-23 09:37:40 +00:00
|
|
|
numBits += qsizetype(qPopulationCount(v));
|
2013-09-12 22:31:50 +00:00
|
|
|
}
|
|
|
|
if (bits + 3 <= end) {
|
2016-03-15 17:10:12 +00:00
|
|
|
quint32 v = qFromUnaligned<quint32>(bits);
|
2011-04-27 10:05:43 +00:00
|
|
|
bits += 4;
|
2020-09-23 09:37:40 +00:00
|
|
|
numBits += qsizetype(qPopulationCount(v));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2013-09-11 23:25:37 +00:00
|
|
|
if (bits + 1 < end) {
|
2016-03-15 17:10:12 +00:00
|
|
|
quint16 v = qFromUnaligned<quint16>(bits);
|
2013-09-11 23:25:37 +00:00
|
|
|
bits += 2;
|
2020-09-23 09:37:40 +00:00
|
|
|
numBits += qsizetype(qPopulationCount(v));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2013-09-11 23:25:37 +00:00
|
|
|
if (bits < end)
|
2020-09-23 09:37:40 +00:00
|
|
|
numBits += qsizetype(qPopulationCount(bits[0]));
|
2013-09-11 23:25:37 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
return on ? numBits : size() - numBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Resizes the bit array to \a size bits.
|
|
|
|
|
|
|
|
If \a size is greater than the current size, the bit array is
|
|
|
|
extended to make it \a size bits with the extra bits added to the
|
|
|
|
end. The new bits are initialized to false (0).
|
|
|
|
|
|
|
|
If \a size is less than the current size, bits are removed from
|
|
|
|
the end.
|
|
|
|
|
|
|
|
\sa size()
|
|
|
|
*/
|
2020-09-23 09:37:40 +00:00
|
|
|
void QBitArray::resize(qsizetype size)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
if (!size) {
|
|
|
|
d.resize(0);
|
|
|
|
} else {
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype s = d.size();
|
2024-01-29 09:27:15 +00:00
|
|
|
d.resize(allocation_size(size));
|
2020-10-20 10:19:21 +00:00
|
|
|
uchar *c = reinterpret_cast<uchar *>(d.data());
|
2024-01-30 10:15:13 +00:00
|
|
|
if (d.size() > s)
|
2011-04-27 10:05:43 +00:00
|
|
|
memset(c + s, 0, d.size() - s);
|
2024-01-30 10:15:13 +00:00
|
|
|
adjust_head_and_tail(d.data(), d.size(), size);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \fn bool QBitArray::isEmpty() const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this bit array has size 0; otherwise returns
|
2011-04-27 10:05:43 +00:00
|
|
|
false.
|
|
|
|
|
|
|
|
\sa size()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn bool QBitArray::isNull() const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if this bit array is null; otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 5
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Qt makes a distinction between null bit arrays and empty bit
|
|
|
|
arrays for historical reasons. For most applications, what
|
|
|
|
matters is whether or not a bit array contains any data,
|
|
|
|
and this can be determined using isEmpty().
|
|
|
|
|
|
|
|
\sa isEmpty()
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn bool QBitArray::fill(bool value, qsizetype size = -1)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Sets every bit in the bit array to \a value, returning true if successful;
|
2013-10-02 14:51:05 +00:00
|
|
|
otherwise returns \c false. If \a size is different from -1 (the default),
|
2011-04-27 10:05:43 +00:00
|
|
|
the bit array is resized to \a size beforehand.
|
|
|
|
|
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 6
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa resize()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\overload
|
|
|
|
|
2014-06-24 09:54:56 +00:00
|
|
|
Sets bits at index positions \a begin up to (but not including) \a end
|
2011-04-27 10:05:43 +00:00
|
|
|
to \a value.
|
|
|
|
|
2014-06-24 09:54:56 +00:00
|
|
|
\a begin must be a valid index position in the bit array
|
|
|
|
(0 <= \a begin < size()).
|
|
|
|
|
|
|
|
\a end must be either a valid index position or equal to size(), in
|
|
|
|
which case the fill operation runs until the end of the array
|
|
|
|
(0 <= \a end <= size()).
|
|
|
|
|
|
|
|
Example:
|
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 15
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
void QBitArray::fill(bool value, qsizetype begin, qsizetype end)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
while (begin < end && begin & 0x7)
|
|
|
|
setBit(begin++, value);
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype len = end - begin;
|
2011-04-27 10:05:43 +00:00
|
|
|
if (len <= 0)
|
|
|
|
return;
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype s = len & ~qsizetype(0x7);
|
2020-10-20 10:19:21 +00:00
|
|
|
uchar *c = reinterpret_cast<uchar *>(d.data());
|
2011-04-27 10:05:43 +00:00
|
|
|
memset(c + (begin >> 3) + 1, value ? 0xff : 0, s >> 3);
|
|
|
|
begin += s;
|
|
|
|
while (begin < end)
|
|
|
|
setBit(begin++, value);
|
|
|
|
}
|
|
|
|
|
2018-01-19 04:44:43 +00:00
|
|
|
/*!
|
|
|
|
\fn const char *QBitArray::bits() const
|
|
|
|
\since 5.11
|
|
|
|
|
|
|
|
Returns a pointer to a dense bit array for this QBitArray. Bits are counted
|
2020-01-31 15:47:46 +00:00
|
|
|
upwards from the least significant bit in each byte. The number of bits
|
2018-01-19 04:44:43 +00:00
|
|
|
relevant in the last byte is given by \c{size() % 8}.
|
|
|
|
|
|
|
|
\sa fromBits(), size()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\since 5.11
|
|
|
|
|
|
|
|
Creates a QBitArray with the dense bit array located at \a data, with \a
|
2018-02-21 10:12:04 +00:00
|
|
|
size bits. The byte array at \a data must be at least \a size / 8 (rounded up)
|
2018-01-19 04:44:43 +00:00
|
|
|
bytes long.
|
|
|
|
|
|
|
|
If \a size is not a multiple of 8, this function will include the lowest
|
|
|
|
\a size % 8 bits from the last byte in \a data.
|
|
|
|
|
|
|
|
\sa bits()
|
|
|
|
*/
|
|
|
|
QBitArray QBitArray::fromBits(const char *data, qsizetype size)
|
|
|
|
{
|
|
|
|
QBitArray result;
|
2019-08-02 05:56:30 +00:00
|
|
|
if (size == 0)
|
|
|
|
return result;
|
2018-01-19 04:44:43 +00:00
|
|
|
|
2024-01-30 10:15:13 +00:00
|
|
|
auto &d = result.d;
|
|
|
|
d.resize(allocation_size(size));
|
|
|
|
memcpy(d.data() + 1, data, d.size() - 1);
|
|
|
|
adjust_head_and_tail(d.data(), d.size(), size);
|
2018-01-19 04:44:43 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-12-06 21:41:36 +00:00
|
|
|
/*!
|
2019-12-30 10:28:33 +00:00
|
|
|
\since 6.0
|
2019-12-06 21:41:36 +00:00
|
|
|
|
2019-12-30 10:28:33 +00:00
|
|
|
Returns the array of bit converted to an int. The conversion is based on \a endianness.
|
|
|
|
Converts up to the first 32 bits of the array to \c quint32 and returns it,
|
|
|
|
obeying \a endianness. If \a ok is not a null pointer, and the array has more
|
|
|
|
than 32 bits, \a ok is set to false and this function returns zero; otherwise,
|
|
|
|
it's set to true.
|
2019-12-06 21:41:36 +00:00
|
|
|
*/
|
|
|
|
quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcept
|
|
|
|
{
|
2019-12-30 10:28:33 +00:00
|
|
|
const qsizetype _size = size();
|
|
|
|
if (_size > 32) {
|
|
|
|
if (ok)
|
2019-12-06 21:41:36 +00:00
|
|
|
*ok = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-30 10:28:33 +00:00
|
|
|
if (ok)
|
2019-12-06 21:41:36 +00:00
|
|
|
*ok = true;
|
|
|
|
|
2019-12-30 10:28:33 +00:00
|
|
|
quint32 factor = 1;
|
2019-12-06 21:41:36 +00:00
|
|
|
quint32 total = 0;
|
2019-12-30 10:28:33 +00:00
|
|
|
for (qsizetype i = 0; i < _size; ++i, factor *= 2) {
|
|
|
|
const auto index = endianness == QSysInfo::Endian::LittleEndian ? i : (_size - i - 1);
|
|
|
|
if (testBit(index))
|
2019-12-06 21:41:36 +00:00
|
|
|
total += factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*! \fn bool QBitArray::isDetached() const
|
|
|
|
|
|
|
|
\internal
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn void QBitArray::detach()
|
|
|
|
|
|
|
|
\internal
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn void QBitArray::clear()
|
|
|
|
|
|
|
|
Clears the contents of the bit array and makes it empty.
|
|
|
|
|
|
|
|
\sa resize(), isEmpty()
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn void QBitArray::truncate(qsizetype pos)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Truncates the bit array at index position \a pos.
|
|
|
|
|
|
|
|
If \a pos is beyond the end of the array, nothing happens.
|
|
|
|
|
|
|
|
\sa resize()
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn bool QBitArray::toggleBit(qsizetype i)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Inverts the value of the bit at index position \a i, returning the
|
|
|
|
previous value of that bit as either true (if it was set) or false (if
|
|
|
|
it was unset).
|
|
|
|
|
|
|
|
If the previous value was 0, the new value will be 1. If the
|
|
|
|
previous value was 1, the new value will be 0.
|
|
|
|
|
|
|
|
\a i must be a valid index position in the bit array (i.e., 0 <=
|
|
|
|
\a i < size()).
|
|
|
|
|
|
|
|
\sa setBit(), clearBit()
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn bool QBitArray::testBit(qsizetype i) const
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if the bit at index position \a i is 1; otherwise
|
|
|
|
returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\a i must be a valid index position in the bit array (i.e., 0 <=
|
|
|
|
\a i < size()).
|
|
|
|
|
|
|
|
\sa setBit(), clearBit()
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn bool QBitArray::setBit(qsizetype i)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Sets the bit at index position \a i to 1.
|
|
|
|
|
|
|
|
\a i must be a valid index position in the bit array (i.e., 0 <=
|
|
|
|
\a i < size()).
|
|
|
|
|
|
|
|
\sa clearBit(), toggleBit()
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn void QBitArray::setBit(qsizetype i, bool value)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\overload
|
|
|
|
|
|
|
|
Sets the bit at index position \a i to \a value.
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn void QBitArray::clearBit(qsizetype i)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Sets the bit at index position \a i to 0.
|
|
|
|
|
|
|
|
\a i must be a valid index position in the bit array (i.e., 0 <=
|
|
|
|
\a i < size()).
|
|
|
|
|
|
|
|
\sa setBit(), toggleBit()
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn bool QBitArray::at(qsizetype i) const
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the value of the bit at index position \a i.
|
|
|
|
|
|
|
|
\a i must be a valid index position in the bit array (i.e., 0 <=
|
|
|
|
\a i < size()).
|
|
|
|
|
|
|
|
\sa operator[]()
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn QBitRef QBitArray::operator[](qsizetype i)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Returns the bit at index position \a i as a modifiable reference.
|
|
|
|
|
|
|
|
\a i must be a valid index position in the bit array (i.e., 0 <=
|
|
|
|
\a i < size()).
|
|
|
|
|
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 7
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The return value is of type QBitRef, a helper class for QBitArray.
|
|
|
|
When you get an object of type QBitRef, you can assign to
|
|
|
|
it, and the assignment will apply to the bit in the QBitArray
|
|
|
|
from which you got the reference.
|
|
|
|
|
|
|
|
The functions testBit(), setBit(), and clearBit() are slightly
|
|
|
|
faster.
|
|
|
|
|
|
|
|
\sa at(), testBit(), setBit(), clearBit()
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn bool QBitArray::operator[](qsizetype i) const
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\overload
|
|
|
|
*/
|
|
|
|
|
2022-07-23 00:23:08 +00:00
|
|
|
/*! \fn QBitArray::QBitArray(const QBitArray &other) noexcept
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Constructs a copy of \a other.
|
|
|
|
|
|
|
|
This operation takes \l{constant time}, because QBitArray is
|
|
|
|
\l{implicitly shared}. This makes returning a QBitArray from a
|
|
|
|
function very fast. If a shared instance is modified, it will be
|
|
|
|
copied (copy-on-write), and that takes \l{linear time}.
|
|
|
|
|
|
|
|
\sa operator=()
|
|
|
|
*/
|
|
|
|
|
2022-07-23 00:23:08 +00:00
|
|
|
/*! \fn QBitArray &QBitArray::operator=(const QBitArray &other) noexcept
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Assigns \a other to this bit array and returns a reference to
|
|
|
|
this bit array.
|
2012-09-20 20:36:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn QBitArray &QBitArray::operator=(QBitArray &&other)
|
2014-01-13 08:24:32 +00:00
|
|
|
\since 5.2
|
2012-09-20 20:36:05 +00:00
|
|
|
|
|
|
|
Moves \a other to this bit array and returns a reference to
|
|
|
|
this bit array.
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn void QBitArray::swap(QBitArray &other)
|
|
|
|
\since 4.8
|
|
|
|
|
|
|
|
Swaps bit array \a other with this bit array. This operation is very
|
|
|
|
fast and never fails.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn bool QBitArray::operator==(const QBitArray &other) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if \a other is equal to this bit array; otherwise
|
|
|
|
returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa operator!=()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn bool QBitArray::operator!=(const QBitArray &other) const
|
|
|
|
|
2013-10-02 14:51:05 +00:00
|
|
|
Returns \c true if \a other is not equal to this bit array;
|
|
|
|
otherwise returns \c false.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa operator==()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Performs the AND operation between all bits in this bit array and
|
|
|
|
\a other. Assigns the result to this bit array, and returns a
|
|
|
|
reference to it.
|
|
|
|
|
|
|
|
The result has the length of the longest of the two bit arrays,
|
|
|
|
with any missing bits (if one array is shorter than the other)
|
|
|
|
taken to be 0.
|
|
|
|
|
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 8
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa operator&(), operator|=(), operator^=(), operator~()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QBitArray &QBitArray::operator&=(const QBitArray &other)
|
|
|
|
{
|
|
|
|
resize(qMax(size(), other.size()));
|
2020-10-20 10:19:21 +00:00
|
|
|
uchar *a1 = reinterpret_cast<uchar *>(d.data()) + 1;
|
|
|
|
const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1;
|
|
|
|
qsizetype n = other.d.size() - 1;
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype p = d.size() - 1 - n;
|
2011-04-27 10:05:43 +00:00
|
|
|
while (n-- > 0)
|
|
|
|
*a1++ &= *a2++;
|
|
|
|
while (p-- > 0)
|
|
|
|
*a1++ = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Performs the OR operation between all bits in this bit array and
|
|
|
|
\a other. Assigns the result to this bit array, and returns a
|
|
|
|
reference to it.
|
|
|
|
|
|
|
|
The result has the length of the longest of the two bit arrays,
|
|
|
|
with any missing bits (if one array is shorter than the other)
|
|
|
|
taken to be 0.
|
|
|
|
|
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 9
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa operator|(), operator&=(), operator^=(), operator~()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QBitArray &QBitArray::operator|=(const QBitArray &other)
|
|
|
|
{
|
|
|
|
resize(qMax(size(), other.size()));
|
2020-10-20 10:19:21 +00:00
|
|
|
uchar *a1 = reinterpret_cast<uchar *>(d.data()) + 1;
|
2011-04-27 10:05:43 +00:00
|
|
|
const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1;
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype n = other.d.size() - 1;
|
2011-04-27 10:05:43 +00:00
|
|
|
while (n-- > 0)
|
|
|
|
*a1++ |= *a2++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Performs the XOR operation between all bits in this bit array and
|
|
|
|
\a other. Assigns the result to this bit array, and returns a
|
|
|
|
reference to it.
|
|
|
|
|
|
|
|
The result has the length of the longest of the two bit arrays,
|
|
|
|
with any missing bits (if one array is shorter than the other)
|
|
|
|
taken to be 0.
|
|
|
|
|
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 10
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa operator^(), operator&=(), operator|=(), operator~()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QBitArray &QBitArray::operator^=(const QBitArray &other)
|
|
|
|
{
|
|
|
|
resize(qMax(size(), other.size()));
|
2020-10-20 10:19:21 +00:00
|
|
|
uchar *a1 = reinterpret_cast<uchar *>(d.data()) + 1;
|
2011-04-27 10:05:43 +00:00
|
|
|
const uchar *a2 = reinterpret_cast<const uchar *>(other.d.constData()) + 1;
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype n = other.d.size() - 1;
|
2011-04-27 10:05:43 +00:00
|
|
|
while (n-- > 0)
|
|
|
|
*a1++ ^= *a2++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns a bit array that contains the inverted bits of this bit
|
|
|
|
array.
|
|
|
|
|
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 11
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa operator&(), operator|(), operator^()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QBitArray QBitArray::operator~() const
|
|
|
|
{
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype sz = size();
|
2011-04-27 10:05:43 +00:00
|
|
|
QBitArray a(sz);
|
|
|
|
const uchar *a1 = reinterpret_cast<const uchar *>(d.constData()) + 1;
|
2020-10-20 10:19:21 +00:00
|
|
|
uchar *a2 = reinterpret_cast<uchar *>(a.d.data()) + 1;
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype n = d.size() - 1;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
while (n-- > 0)
|
|
|
|
*a2++ = ~*a1++;
|
|
|
|
|
2020-10-20 10:19:21 +00:00
|
|
|
if (sz && sz % 8)
|
|
|
|
*(a2 - 1) &= (1 << (sz % 8)) - 1;
|
2011-04-27 10:05:43 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\relates QBitArray
|
|
|
|
|
|
|
|
Returns a bit array that is the AND of the bit arrays \a a1 and \a
|
|
|
|
a2.
|
|
|
|
|
|
|
|
The result has the length of the longest of the two bit arrays,
|
|
|
|
with any missing bits (if one array is shorter than the other)
|
|
|
|
taken to be 0.
|
|
|
|
|
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 12
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-09-15 08:32:50 +00:00
|
|
|
\sa {QBitArray::}{operator&=()}, {QBitArray::}{operator|()}, {QBitArray::}{operator^()}
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QBitArray operator&(const QBitArray &a1, const QBitArray &a2)
|
|
|
|
{
|
|
|
|
QBitArray tmp = a1;
|
|
|
|
tmp &= a2;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\relates QBitArray
|
|
|
|
|
|
|
|
Returns a bit array that is the OR of the bit arrays \a a1 and \a
|
|
|
|
a2.
|
|
|
|
|
|
|
|
The result has the length of the longest of the two bit arrays,
|
|
|
|
with any missing bits (if one array is shorter than the other)
|
|
|
|
taken to be 0.
|
|
|
|
|
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 13
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\sa QBitArray::operator|=(), operator&(), operator^()
|
|
|
|
*/
|
|
|
|
|
|
|
|
QBitArray operator|(const QBitArray &a1, const QBitArray &a2)
|
|
|
|
{
|
|
|
|
QBitArray tmp = a1;
|
|
|
|
tmp |= a2;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\relates QBitArray
|
|
|
|
|
|
|
|
Returns a bit array that is the XOR of the bit arrays \a a1 and \a
|
|
|
|
a2.
|
|
|
|
|
|
|
|
The result has the length of the longest of the two bit arrays,
|
|
|
|
with any missing bits (if one array is shorter than the other)
|
|
|
|
taken to be 0.
|
|
|
|
|
|
|
|
Example:
|
2012-03-20 18:37:07 +00:00
|
|
|
\snippet code/src_corelib_tools_qbitarray.cpp 14
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-09-15 08:32:50 +00:00
|
|
|
\sa {QBitArray}{operator^=()}, {QBitArray}{operator&()}, {QBitArray}{operator|()}
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QBitArray operator^(const QBitArray &a1, const QBitArray &a2)
|
|
|
|
{
|
|
|
|
QBitArray tmp = a1;
|
|
|
|
tmp ^= a2;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\class QBitRef
|
2012-08-23 10:22:38 +00:00
|
|
|
\inmodule QtCore
|
2011-04-27 10:05:43 +00:00
|
|
|
\reentrant
|
|
|
|
\brief The QBitRef class is an internal class, used with QBitArray.
|
|
|
|
|
|
|
|
\internal
|
|
|
|
|
|
|
|
The QBitRef is required by the indexing [] operator on bit arrays.
|
|
|
|
It is not for use in any other context.
|
|
|
|
*/
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
/*! \fn QBitRef::QBitRef (QBitArray& a, qsizetype i)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Constructs a reference to element \a i in the QBitArray \a a.
|
|
|
|
This is what QBitArray::operator[] constructs its return value
|
|
|
|
with.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn QBitRef::operator bool() const
|
|
|
|
|
|
|
|
Returns the value referenced by the QBitRef.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn bool QBitRef::operator!() const
|
|
|
|
|
|
|
|
\internal
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn QBitRef& QBitRef::operator= (const QBitRef& v)
|
|
|
|
|
|
|
|
Sets the value referenced by the QBitRef to that referenced by
|
|
|
|
QBitRef \a v.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \fn QBitRef& QBitRef::operator= (bool v)
|
|
|
|
\overload
|
|
|
|
|
|
|
|
Sets the value referenced by the QBitRef to \a v.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
QBitArray stream functions
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef QT_NO_DATASTREAM
|
|
|
|
/*!
|
|
|
|
\relates QBitArray
|
|
|
|
|
|
|
|
Writes bit array \a ba to stream \a out.
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\sa {Serializing Qt Data Types}{Format of the QDataStream operators}
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QDataStream &operator<<(QDataStream &out, const QBitArray &ba)
|
|
|
|
{
|
2024-01-27 09:32:20 +00:00
|
|
|
const qsizetype len = ba.size();
|
2020-09-23 09:37:40 +00:00
|
|
|
if (out.version() < QDataStream::Qt_6_0) {
|
2024-01-27 09:38:32 +00:00
|
|
|
if (Q_UNLIKELY(len > qsizetype{(std::numeric_limits<qint32>::max)()})) {
|
|
|
|
out.setStatus(QDataStream::WriteFailed); // ### SizeLimitExceeded
|
|
|
|
return out;
|
|
|
|
}
|
2024-01-27 09:32:20 +00:00
|
|
|
out << quint32(len);
|
2020-09-23 09:37:40 +00:00
|
|
|
} else {
|
2024-01-27 09:32:20 +00:00
|
|
|
out << quint64(len);
|
2020-09-23 09:37:40 +00:00
|
|
|
}
|
2024-01-27 09:32:20 +00:00
|
|
|
if (len > 0)
|
|
|
|
out.writeRawData(ba.d.data() + 1, ba.d.size() - 1);
|
|
|
|
return out;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\relates QBitArray
|
|
|
|
|
|
|
|
Reads a bit array into \a ba from stream \a in.
|
|
|
|
|
2012-03-20 18:37:07 +00:00
|
|
|
\sa {Serializing Qt Data Types}{Format of the QDataStream operators}
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
QDataStream &operator>>(QDataStream &in, QBitArray &ba)
|
|
|
|
{
|
|
|
|
ba.clear();
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype len;
|
|
|
|
if (in.version() < QDataStream::Qt_6_0) {
|
|
|
|
quint32 tmp;
|
|
|
|
in >> tmp;
|
QBitArray: fix potential truncation in QDataStream op>>()
In Qt 5, a QBitArray could not contain more than INT_MAX bits, because
the then-size_type, int, cannot represent more, even if the underlying
storage could hold 8x as much, and the serialisation format, using
unsigned int, could represent 2x.
Therefore, reject old formats with sizes that exceed INT_MAX elements
as corrupt.
Likewise, the Qt 6 serialisation format unconditionally uses 64-bit
sizes, but 32-bit platforms still cannot represent more than
numeric_limits<qsizetype>::max() (= INT_MAX) bits in memory. This is a
valid stream for 64-bit platforms, though, so ideally, this should be
using SizeLimitsExeeded, which, however, is only available from Qt
6.7. So, for now, and until we have SizeLimitsExeeded, mark the stream
as corrupt here, too.
[ChangeLog][QtCore][QBitArray] Fixed undetected overflows in the
deserialisation (opertor>>()) from QDataStream.
Pick-to: 6.5 6.2 5.15
Change-Id: Ib24cf9218c06a3a05185723c77d4313611c2dd40
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 0808beace332631f8100b5700577f10f63e4630c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit d61a3337055e7849471a44ee7c3ce4b8ac39be9d)
2024-01-27 08:17:57 +00:00
|
|
|
if (Q_UNLIKELY(tmp > quint32((std::numeric_limits<qint32>::max)()))) {
|
|
|
|
in.setStatus(QDataStream::ReadCorruptData);
|
|
|
|
return in;
|
|
|
|
}
|
2020-09-23 09:37:40 +00:00
|
|
|
len = tmp;
|
|
|
|
} else {
|
|
|
|
quint64 tmp;
|
|
|
|
in >> tmp;
|
QBitArray: fix potential truncation in QDataStream op>>()
In Qt 5, a QBitArray could not contain more than INT_MAX bits, because
the then-size_type, int, cannot represent more, even if the underlying
storage could hold 8x as much, and the serialisation format, using
unsigned int, could represent 2x.
Therefore, reject old formats with sizes that exceed INT_MAX elements
as corrupt.
Likewise, the Qt 6 serialisation format unconditionally uses 64-bit
sizes, but 32-bit platforms still cannot represent more than
numeric_limits<qsizetype>::max() (= INT_MAX) bits in memory. This is a
valid stream for 64-bit platforms, though, so ideally, this should be
using SizeLimitsExeeded, which, however, is only available from Qt
6.7. So, for now, and until we have SizeLimitsExeeded, mark the stream
as corrupt here, too.
[ChangeLog][QtCore][QBitArray] Fixed undetected overflows in the
deserialisation (opertor>>()) from QDataStream.
Pick-to: 6.5 6.2 5.15
Change-Id: Ib24cf9218c06a3a05185723c77d4313611c2dd40
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 0808beace332631f8100b5700577f10f63e4630c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit d61a3337055e7849471a44ee7c3ce4b8ac39be9d)
2024-01-27 08:17:57 +00:00
|
|
|
if (Q_UNLIKELY(tmp > quint64((std::numeric_limits<qsizetype>::max)()))) {
|
|
|
|
in.setStatus(QDataStream::ReadCorruptData); // ### SizeLimitExeeded
|
|
|
|
return in;
|
|
|
|
}
|
2020-09-23 09:37:40 +00:00
|
|
|
len = tmp;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
if (len == 0) {
|
2014-01-13 14:48:44 +00:00
|
|
|
ba.clear();
|
|
|
|
return in;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 09:37:40 +00:00
|
|
|
const qsizetype Step = 8 * 1024 * 1024;
|
2024-01-29 09:27:15 +00:00
|
|
|
const qsizetype totalBytes = storage_size(len);
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype allocated = 0;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
while (allocated < totalBytes) {
|
2020-09-23 09:37:40 +00:00
|
|
|
qsizetype blockSize = qMin(Step, totalBytes - allocated);
|
2011-04-27 10:05:43 +00:00
|
|
|
ba.d.resize(allocated + blockSize + 1);
|
|
|
|
if (in.readRawData(ba.d.data() + 1 + allocated, blockSize) != blockSize) {
|
|
|
|
ba.clear();
|
|
|
|
in.setStatus(QDataStream::ReadPastEnd);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
allocated += blockSize;
|
|
|
|
}
|
|
|
|
|
2024-01-30 10:15:13 +00:00
|
|
|
const auto fromStream = ba.d.back();
|
|
|
|
adjust_head_and_tail(ba.d.data(), ba.d.size(), len);
|
|
|
|
if (ba.d.back() != fromStream) {
|
2011-04-27 10:05:43 +00:00
|
|
|
ba.clear();
|
|
|
|
in.setStatus(QDataStream::ReadCorruptData);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
#endif // QT_NO_DATASTREAM
|
|
|
|
|
Implement QDebug stream operators for builtin classes
QDebug stream operator was added for:
QPixmap, QImage, QUuid, QBitArray, QLocale, QRegExp, QCursor,
QPalette, QTextFormat, QTextLength, QIcon and QSizePolicy
Change-Id: Ibcf5c9b599ba322d53cb106d8e5e157427ebe757
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
2012-01-04 13:14:08 +00:00
|
|
|
#ifndef QT_NO_DEBUG_STREAM
|
|
|
|
QDebug operator<<(QDebug dbg, const QBitArray &array)
|
|
|
|
{
|
2015-01-23 15:19:11 +00:00
|
|
|
QDebugStateSaver saver(dbg);
|
Implement QDebug stream operators for builtin classes
QDebug stream operator was added for:
QPixmap, QImage, QUuid, QBitArray, QLocale, QRegExp, QCursor,
QPalette, QTextFormat, QTextLength, QIcon and QSizePolicy
Change-Id: Ibcf5c9b599ba322d53cb106d8e5e157427ebe757
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
2012-01-04 13:14:08 +00:00
|
|
|
dbg.nospace() << "QBitArray(";
|
2020-09-23 09:37:40 +00:00
|
|
|
for (qsizetype i = 0; i < array.size();) {
|
Implement QDebug stream operators for builtin classes
QDebug stream operator was added for:
QPixmap, QImage, QUuid, QBitArray, QLocale, QRegExp, QCursor,
QPalette, QTextFormat, QTextLength, QIcon and QSizePolicy
Change-Id: Ibcf5c9b599ba322d53cb106d8e5e157427ebe757
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
2012-01-04 13:14:08 +00:00
|
|
|
if (array.testBit(i))
|
2015-01-23 15:19:11 +00:00
|
|
|
dbg << '1';
|
Implement QDebug stream operators for builtin classes
QDebug stream operator was added for:
QPixmap, QImage, QUuid, QBitArray, QLocale, QRegExp, QCursor,
QPalette, QTextFormat, QTextLength, QIcon and QSizePolicy
Change-Id: Ibcf5c9b599ba322d53cb106d8e5e157427ebe757
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
2012-01-04 13:14:08 +00:00
|
|
|
else
|
2015-01-23 15:19:11 +00:00
|
|
|
dbg << '0';
|
Implement QDebug stream operators for builtin classes
QDebug stream operator was added for:
QPixmap, QImage, QUuid, QBitArray, QLocale, QRegExp, QCursor,
QPalette, QTextFormat, QTextLength, QIcon and QSizePolicy
Change-Id: Ibcf5c9b599ba322d53cb106d8e5e157427ebe757
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
2012-01-04 13:14:08 +00:00
|
|
|
i += 1;
|
|
|
|
if (!(i % 4) && (i < array.size()))
|
2015-01-23 15:19:11 +00:00
|
|
|
dbg << ' ';
|
Implement QDebug stream operators for builtin classes
QDebug stream operator was added for:
QPixmap, QImage, QUuid, QBitArray, QLocale, QRegExp, QCursor,
QPalette, QTextFormat, QTextLength, QIcon and QSizePolicy
Change-Id: Ibcf5c9b599ba322d53cb106d8e5e157427ebe757
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
2012-01-04 13:14:08 +00:00
|
|
|
}
|
2015-01-23 15:19:11 +00:00
|
|
|
dbg << ')';
|
|
|
|
return dbg;
|
Implement QDebug stream operators for builtin classes
QDebug stream operator was added for:
QPixmap, QImage, QUuid, QBitArray, QLocale, QRegExp, QCursor,
QPalette, QTextFormat, QTextLength, QIcon and QSizePolicy
Change-Id: Ibcf5c9b599ba322d53cb106d8e5e157427ebe757
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
2012-01-04 13:14:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/*!
|
|
|
|
\fn DataPtr &QBitArray::data_ptr()
|
|
|
|
\internal
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\typedef QBitArray::DataPtr
|
|
|
|
\internal
|
|
|
|
*/
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|