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
qprivatelinearbuffer_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:dynamic-buffer-handling
4
5#ifndef QPRIVATELINEARBUFFER_P_H
6#define QPRIVATELINEARBUFFER_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 <QtCore/qtconfigmacros.h>
20
21QT_BEGIN_NAMESPACE
22
23// This is QIODevice's read buffer, optimised for read(), isEmpty() and getChar()
24class QPrivateLinearBuffer
25{
26public:
27 QPrivateLinearBuffer() : len(0), first(0), buf(0), capacity(0) {
28 }
29 ~QPrivateLinearBuffer() {
30 delete [] buf;
31 }
32 void clear() {
33 first = buf;
34 len = 0;
35 }
36 qsizetype size() const
37 {
38 return len;
39 }
40 bool isEmpty() const {
41 return len == 0;
42 }
43 void skip(qsizetype n)
44 {
45 if (n >= len) {
46 clear();
47 } else {
48 len -= n;
49 first += n;
50 }
51 }
52 int getChar() {
53 if (len == 0)
54 return -1;
55 int ch = uchar(*first);
56 len--;
57 first++;
58 return ch;
59 }
60 qsizetype read(char* target, qsizetype size)
61 {
62 qsizetype r = (std::min)(size, len);
63 memcpy(target, first, r);
64 len -= r;
65 first += r;
66 return r;
67 }
68 char* reserve(qsizetype size)
69 {
70 makeSpace(size + len, freeSpaceAtEnd);
71 char* writePtr = first + len;
72 len += size;
73 return writePtr;
74 }
75 void chop(qsizetype size)
76 {
77 if (size >= len) {
78 clear();
79 } else {
80 len -= size;
81 }
82 }
83 QByteArray readAll() {
84 char* f = first;
85 qsizetype l = len;
86 clear();
87 return QByteArray(f, l);
88 }
89 qsizetype readLine(char* target, qsizetype size)
90 {
91 qsizetype r = (std::min)(size, len);
92 char* eol = static_cast<char*>(memchr(first, '\n', r));
93 if (eol)
94 r = 1+(eol-first);
95 memcpy(target, first, r);
96 len -= r;
97 first += r;
98 return r;
99 }
100 bool canReadLine() const {
101 return memchr(first, '\n', len);
102 }
103 void ungetChar(char c) {
104 if (first == buf) {
105 // underflow, the existing valid data needs to move to the end of the (potentially bigger) buffer
106 makeSpace(len+1, freeSpaceAtStart);
107 }
108 first--;
109 len++;
110 *first = c;
111 }
112 void ungetBlock(const char* block, qsizetype size)
113 {
114 if ((first - buf) < size) {
115 // underflow, the existing valid data needs to move to the end of the (potentially bigger) buffer
116 makeSpace(len + size, freeSpaceAtStart);
117 }
118 first -= size;
119 len += size;
120 memcpy(first, block, size);
121 }
122
123private:
124 enum FreeSpacePos {freeSpaceAtStart, freeSpaceAtEnd};
125 void makeSpace(size_t required, FreeSpacePos where) {
126 size_t newCapacity = (std::max)(capacity, size_t(QPRIVATELINEARBUFFER_BUFFERSIZE));
127 while (newCapacity < required)
128 newCapacity *= 2;
129 const qsizetype moveOffset = (where == freeSpaceAtEnd) ? 0 : qsizetype(newCapacity) - len;
130 if (newCapacity > capacity) {
131 // allocate more space
132 char* newBuf = new char[newCapacity];
133 memmove(newBuf + moveOffset, first, len);
134 delete [] buf;
135 buf = newBuf;
136 capacity = newCapacity;
137 } else {
138 // shift any existing data to make space
139 memmove(buf + moveOffset, first, len);
140 }
141 first = buf + moveOffset;
142 }
143
144private:
145 // length of the unread data
146 qsizetype len;
147 // start of the unread data
148 char* first;
149 // the allocated buffer
150 char* buf;
151 // allocated buffer size
152 size_t capacity;
153};
154
155QT_END_NAMESPACE
156
157#endif // QPRIVATELINEARBUFFER_P_H
#define QPRIVATELINEARBUFFER_BUFFERSIZE
void errorOccurred(int errorCode)
void javaThreadErrorOccurred(int errorCode)
qint64 bytesAvailable() const
qint64 readData(char *data, qint64 maxSize)
void javaReadyRead(jbyteArray buffer, int bufferLength)
bool setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType, QBluetoothSocket::SocketState socketState=QBluetoothSocket::SocketState::ConnectedState, QBluetoothSocket::OpenMode openMode=QBluetoothSocket::ReadWrite) override
void defaultSocketConnectFailed(const QJniObject &socket, const QJniObject &targetUuid, const QBluetoothUuid &qtTargetUuid)
void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid, QIODevice::OpenMode openMode) override
void connectToServiceHelper(const QBluetoothAddress &address, const QBluetoothUuid &uuid, QIODevice::OpenMode openMode) override
static QBluetoothUuid reverseUuid(const QBluetoothUuid &serviceUuid)
bool ensureNativeSocket(QBluetoothServiceInfo::Protocol type) override
QBluetoothAddress peerAddress() const override
void fallbackSocketConnectFailed(const QJniObject &socket, const QJniObject &targetUuid)
bool setSocketDescriptor(const QJniObject &socket, QBluetoothServiceInfo::Protocol socketType, QBluetoothSocket::SocketState socketState=QBluetoothSocket::SocketState::ConnectedState, QBluetoothSocket::OpenMode openMode=QBluetoothSocket::ReadWrite) override
QBluetoothAddress localAddress() const override
void connectToService(const QBluetoothServiceInfo &service, QIODevice::OpenMode openMode) override
qint64 writeData(const char *data, qint64 maxSize) override
void connectToService(const QBluetoothAddress &address, quint16 port, QIODevice::OpenMode openMode) override
qint64 readData(char *data, qint64 maxSize) override
bool fallBackReversedConnect(const QBluetoothUuid &uuid)
bool useReverseUuidWorkAroundConnect
static void convertAddress(const quint64 from, quint8(&to)[6])
static quint64 convertAddress(const quint8(&from)[6], quint64 *to=nullptr)