| 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:significant reason:default |
| 4 | |
| 5 | #ifndef QNONCONTIGUOUSBYTEDEVICE_P_H |
| 6 | #define QNONCONTIGUOUSBYTEDEVICE_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 for the convenience |
| 13 | // of a number of Qt sources files. This header file may change from |
| 14 | // version to version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include <QtCore/qobject.h> |
| 20 | #include <QtCore/qbytearray.h> |
| 21 | #include <QtCore/qbuffer.h> |
| 22 | #include <QtCore/qiodevice.h> |
| 23 | #include "private/qringbuffer_p.h" |
| 24 | |
| 25 | #include <memory> |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | class Q_CORE_EXPORT QNonContiguousByteDevice : public QObject |
| 30 | { |
| 31 | Q_OBJECT |
| 32 | public: |
| 33 | virtual const char *readPointer(qint64 maximumLength, qint64 &len) = 0; |
| 34 | virtual bool advanceReadPointer(qint64 amount) = 0; |
| 35 | virtual bool atEnd() const = 0; |
| 36 | virtual qint64 pos() const { return -1; } |
| 37 | virtual bool reset() = 0; |
| 38 | virtual qint64 size() const = 0; |
| 39 | |
| 40 | virtual ~QNonContiguousByteDevice(); |
| 41 | |
| 42 | protected: |
| 43 | QNonContiguousByteDevice(); |
| 44 | |
| 45 | Q_SIGNALS: |
| 46 | void readyRead(); |
| 47 | void readProgress(qint64 current, qint64 total); |
| 48 | }; |
| 49 | |
| 50 | class Q_CORE_EXPORT QNonContiguousByteDeviceFactory |
| 51 | { |
| 52 | public: |
| 53 | static QNonContiguousByteDevice *create(QIODevice *device); |
| 54 | static std::shared_ptr<QNonContiguousByteDevice> createShared(QIODevice *device); |
| 55 | |
| 56 | static QNonContiguousByteDevice *create(const QByteArray &byteArray); |
| 57 | static std::shared_ptr<QNonContiguousByteDevice> createShared(const QByteArray &byteArray); |
| 58 | |
| 59 | static QNonContiguousByteDevice *create(std::shared_ptr<QRingBuffer> ringBuffer); |
| 60 | static std::shared_ptr<QNonContiguousByteDevice> createShared(std::shared_ptr<QRingBuffer> ringBuffer); |
| 61 | |
| 62 | static QIODevice *wrap(QNonContiguousByteDevice *byteDevice); |
| 63 | }; |
| 64 | |
| 65 | // the actual implementations |
| 66 | // |
| 67 | |
| 68 | class QNonContiguousByteDeviceByteArrayImpl : public QNonContiguousByteDevice |
| 69 | { |
| 70 | Q_OBJECT |
| 71 | public: |
| 72 | explicit QNonContiguousByteDeviceByteArrayImpl(QByteArray ba); |
| 73 | explicit QNonContiguousByteDeviceByteArrayImpl(QBuffer *buffer); |
| 74 | ~QNonContiguousByteDeviceByteArrayImpl(); |
| 75 | const char *readPointer(qint64 maximumLength, qint64 &len) override; |
| 76 | bool advanceReadPointer(qint64 amount) override; |
| 77 | bool atEnd() const override; |
| 78 | bool reset() override; |
| 79 | qint64 size() const override; |
| 80 | qint64 pos() const override; |
| 81 | |
| 82 | protected: |
| 83 | QByteArray byteArray; |
| 84 | QByteArrayView view; |
| 85 | qint64 currentPosition = 0; |
| 86 | }; |
| 87 | |
| 88 | class QNonContiguousByteDeviceRingBufferImpl : public QNonContiguousByteDevice |
| 89 | { |
| 90 | Q_OBJECT |
| 91 | public: |
| 92 | explicit QNonContiguousByteDeviceRingBufferImpl(std::shared_ptr<QRingBuffer> rb); |
| 93 | ~QNonContiguousByteDeviceRingBufferImpl(); |
| 94 | const char *readPointer(qint64 maximumLength, qint64 &len) override; |
| 95 | bool advanceReadPointer(qint64 amount) override; |
| 96 | bool atEnd() const override; |
| 97 | bool reset() override; |
| 98 | qint64 size() const override; |
| 99 | qint64 pos() const override; |
| 100 | |
| 101 | protected: |
| 102 | std::shared_ptr<QRingBuffer> ringBuffer; |
| 103 | qint64 currentPosition = 0; |
| 104 | }; |
| 105 | |
| 106 | class QNonContiguousByteDeviceIoDeviceImpl : public QNonContiguousByteDevice |
| 107 | { |
| 108 | Q_OBJECT |
| 109 | public: |
| 110 | explicit QNonContiguousByteDeviceIoDeviceImpl(QIODevice *d); |
| 111 | ~QNonContiguousByteDeviceIoDeviceImpl(); |
| 112 | const char *readPointer(qint64 maximumLength, qint64 &len) override; |
| 113 | bool advanceReadPointer(qint64 amount) override; |
| 114 | bool atEnd() const override; |
| 115 | bool reset() override; |
| 116 | qint64 size() const override; |
| 117 | qint64 pos() const override; |
| 118 | |
| 119 | protected: |
| 120 | QIODevice *device; |
| 121 | QByteArray *currentReadBuffer; |
| 122 | qint64 currentReadBufferSize; |
| 123 | qint64 currentReadBufferAmount; |
| 124 | qint64 currentReadBufferPosition; |
| 125 | qint64 totalAdvancements; |
| 126 | bool eof; |
| 127 | qint64 initialPosition; |
| 128 | }; |
| 129 | |
| 130 | // ... and the reverse thing |
| 131 | class QByteDeviceWrappingIoDevice : public QIODevice |
| 132 | { |
| 133 | Q_OBJECT |
| 134 | public: |
| 135 | explicit QByteDeviceWrappingIoDevice(QNonContiguousByteDevice *bd); |
| 136 | ~QByteDeviceWrappingIoDevice(); |
| 137 | bool isSequential() const override; |
| 138 | bool atEnd() const override; |
| 139 | bool reset() override; |
| 140 | qint64 size() const override; |
| 141 | |
| 142 | protected: |
| 143 | qint64 readData(char *data, qint64 maxSize) override; |
| 144 | qint64 writeData(const char *data, qint64 maxSize) override; |
| 145 | |
| 146 | QNonContiguousByteDevice *byteDevice; |
| 147 | }; |
| 148 | |
| 149 | QT_END_NAMESPACE |
| 150 | |
| 151 | #endif |
| 152 | |