Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbytedatabuffer_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:data-parser
4
5#ifndef QBYTEDATABUFFER_P_H
6#define QBYTEDATABUFFER_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtNetwork/private/qtnetworkglobal_p.h>
20
21#include <QtCore/qbytearray.h>
22#include <QtCore/qlist.h>
23
24#include <climits>
25
26QT_BEGIN_NAMESPACE
27
28// this class handles a list of QByteArrays. It is a variant of QRingBuffer
29// that avoid malloc/realloc/memcpy.
30class QByteDataBuffer
31{
32private:
33 QList<QByteArray> buffers;
34 qint64 bufferCompleteSize = 0;
35 qint64 firstPos = 0;
36public:
37 static inline void popFront(QByteArray &ba, qint64 n)
38 {
39 ba = QByteArray(ba.constData() + n, ba.size() - n);
40 }
41
42 inline void squeezeFirst()
43 {
44 if (!buffers.isEmpty() && firstPos > 0) {
45 popFront(buffers.first(), firstPos);
46 firstPos = 0;
47 }
48 }
49
50 inline void append(const QByteDataBuffer& other)
51 {
52 if (other.isEmpty())
53 return;
54
55 buffers.append(other.buffers);
56 bufferCompleteSize += other.byteAmount();
57
58 if (other.firstPos > 0)
59 popFront(buffers[bufferCount() - other.bufferCount()], other.firstPos);
60 }
61
62 inline void append(QByteDataBuffer &&other)
63 {
64 if (other.isEmpty())
65 return;
66
67 auto otherBufferCount = other.bufferCount();
68 auto otherByteAmount = other.byteAmount();
69 buffers.append(std::move(other.buffers));
70 bufferCompleteSize += otherByteAmount;
71
72 if (other.firstPos > 0)
73 popFront(buffers[bufferCount() - otherBufferCount], other.firstPos);
74 }
75
76 inline void append(const QByteArray& bd)
77 {
78 append(QByteArray(bd));
79 }
80
81 inline void append(QByteArray &&bd)
82 {
83 if (bd.isEmpty())
84 return;
85
86 bufferCompleteSize += bd.size();
87 buffers.append(std::move(bd));
88 }
89
90 inline void prepend(const QByteArray& bd)
91 {
92 prepend(QByteArray(bd));
93 }
94
95 inline void prepend(QByteArray &&bd)
96 {
97 if (bd.isEmpty())
98 return;
99
100 squeezeFirst();
101
102 bufferCompleteSize += bd.size();
103 buffers.prepend(std::move(bd));
104 }
105
106 // return the first QByteData. User of this function has to free() its .data!
107 // preferably use this function to read data.
108 inline QByteArray read()
109 {
110 Q_ASSERT(!isEmpty());
111 squeezeFirst();
112 bufferCompleteSize -= buffers.first().size();
113 return buffers.takeFirst();
114 }
115
116 // return everything. User of this function has to free() its .data!
117 // avoid to use this, it might malloc and memcpy.
118 inline QByteArray readAll()
119 {
120 return read(byteAmount());
121 }
122
123 // return amount. User of this function has to free() its .data!
124 // avoid to use this, it might malloc and memcpy.
125 inline QByteArray read(qint64 amount)
126 {
127 amount = qMin(byteAmount(), amount);
128 if constexpr (sizeof(qsizetype) == sizeof(int)) { // 32-bit
129 // While we cannot overall have more than INT_MAX memory allocated,
130 // the QByteArrays we hold may be shared copies of each other,
131 // causing byteAmount() to exceed INT_MAX.
132 if (amount > INT_MAX)
133 qBadAlloc(); // what resize() would do if it saw past the truncation
134 }
135 QByteArray byteData;
136 byteData.resize(qsizetype(amount));
137 read(byteData.data(), byteData.size());
138 return byteData;
139 }
140
141 // return amount bytes. User of this function has to free() its .data!
142 // avoid to use this, it will memcpy.
143 qint64 read(char* dst, qint64 amount)
144 {
145 amount = qMin(amount, byteAmount());
146 qint64 originalAmount = amount;
147 char *writeDst = dst;
148
149 while (amount > 0) {
150 const QByteArray &first = buffers.first();
151 qint64 firstSize = first.size() - firstPos;
152 if (amount >= firstSize) {
153 // take it completely
154 bufferCompleteSize -= firstSize;
155 amount -= firstSize;
156 memcpy(writeDst, first.constData() + firstPos, firstSize);
157 writeDst += firstSize;
158 firstPos = 0;
159 buffers.takeFirst();
160 } else {
161 // take a part of it & it is the last one to take
162 bufferCompleteSize -= amount;
163 memcpy(writeDst, first.constData() + firstPos, amount);
164 firstPos += amount;
165 amount = 0;
166 }
167 }
168
169 return originalAmount;
170 }
171
172 /*!
173 \internal
174 Returns a view into the first QByteArray contained inside,
175 ignoring any already read data. Call advanceReadPointer()
176 to advance the view forward. When a QByteArray is exhausted
177 the view returned by this function will view into another
178 QByteArray if any. Returns a default constructed view if
179 no data is available.
180
181 \sa advanceReadPointer
182 */
183 QByteArrayView readPointer() const
184 {
185 if (isEmpty())
186 return {};
187 return { buffers.first().constData() + qsizetype(firstPos),
188 buffers.first().size() - qsizetype(firstPos) };
189 }
190
191 /*!
192 \internal
193 Advances the read pointer by \a distance.
194
195 \sa readPointer
196 */
197 void advanceReadPointer(qint64 distance)
198 {
199 qint64 newPos = firstPos + distance;
200 if (isEmpty()) {
201 newPos = 0;
202 } else if (auto size = buffers.first().size(); newPos >= size) {
203 while (newPos >= size) {
204 bufferCompleteSize -= (size - firstPos);
205 newPos -= size;
206 buffers.pop_front();
207 if (isEmpty()) {
208 size = 0;
209 newPos = 0;
210 break;
211 }
212 size = buffers.front().size();
213 }
214 bufferCompleteSize -= newPos;
215 } else {
216 bufferCompleteSize -= newPos - firstPos;
217 }
218 firstPos = newPos;
219 }
220
221 inline char getChar()
222 {
223 Q_ASSERT_X(!isEmpty(), "QByteDataBuffer::getChar",
224 "Cannot read a char from an empty buffer!");
225 char c;
226 read(&c, 1);
227 return c;
228 }
229
230 inline void clear()
231 {
232 buffers.clear();
233 bufferCompleteSize = 0;
234 firstPos = 0;
235 }
236
237 // The byte count of all QByteArrays
238 inline qint64 byteAmount() const
239 {
240 return bufferCompleteSize;
241 }
242
243 // the number of QByteArrays
244 qsizetype bufferCount() const
245 {
246 return buffers.size();
247 }
248
249 inline bool isEmpty() const
250 {
251 return byteAmount() == 0;
252 }
253
254 inline qint64 sizeNextBlock() const
255 {
256 if (buffers.isEmpty())
257 return 0;
258 else
259 return buffers.first().size() - firstPos;
260 }
261
262 QByteArray &operator[](qsizetype i)
263 {
264 if (i == 0)
265 squeezeFirst();
266
267 return buffers[i];
268 }
269
270 inline bool canReadLine() const {
271 qsizetype i = 0;
272 if (i < buffers.size()) {
273 if (buffers.at(i).indexOf('\n', firstPos) != -1)
274 return true;
275 ++i;
276
277 for (; i < buffers.size(); i++)
278 if (buffers.at(i).contains('\n'))
279 return true;
280 }
281 return false;
282 }
283};
284
285QT_END_NAMESPACE
286
287#endif // QBYTEDATABUFFER_P_H