| 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 | #include "qnoncontiguousbytedevice_p.h" |
| 6 | #include <qbuffer.h> |
| 7 | #include <qdebug.h> |
| 8 | #include <qfile.h> |
| 9 | |
| 10 | #include <utility> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | /*! |
| 15 | \class QNonContiguousByteDevice |
| 16 | \inmodule QtCore |
| 17 | \brief A QNonContiguousByteDevice is a representation of a |
| 18 | file, array or buffer that allows access with a read pointer. |
| 19 | \since 4.6 |
| 20 | |
| 21 | The goal of this class is to have a data representation that |
| 22 | allows us to avoid doing a memcpy as we have to do with QIODevice. |
| 23 | |
| 24 | \sa QNonContiguousByteDeviceFactory |
| 25 | |
| 26 | \internal |
| 27 | */ |
| 28 | /*! |
| 29 | \fn virtual const char* QNonContiguousByteDevice::readPointer(qint64 maximumLength, qint64 &len) |
| 30 | |
| 31 | Return a byte pointer for at most \a maximumLength bytes of that device. |
| 32 | if \a maximumLength is -1, the caller does not care about the length and |
| 33 | the device may return what it desires to. |
| 34 | The actual number of bytes the pointer is valid for is returned in |
| 35 | the \a len variable. |
| 36 | \a len will be -1 if EOF or an error occurs. |
| 37 | If it was really EOF can then afterwards be checked with atEnd() |
| 38 | Returns 0 if it is not possible to read at that position. |
| 39 | |
| 40 | \sa atEnd() |
| 41 | |
| 42 | \internal |
| 43 | */ |
| 44 | /*! |
| 45 | \fn virtual bool QNonContiguousByteDevice::advanceReadPointer(qint64 amount) |
| 46 | |
| 47 | will advance the internal read pointer by \a amount bytes. |
| 48 | The old readPointer is invalid after this call. |
| 49 | |
| 50 | \sa readPointer() |
| 51 | |
| 52 | \internal |
| 53 | */ |
| 54 | /*! |
| 55 | \fn virtual bool QNonContiguousByteDevice::atEnd() const |
| 56 | |
| 57 | Returns \c true if everything has been read and the read |
| 58 | pointer cannot be advanced anymore. |
| 59 | |
| 60 | \sa readPointer(), advanceReadPointer(), reset() |
| 61 | |
| 62 | \internal |
| 63 | */ |
| 64 | /*! |
| 65 | \fn virtual bool QNonContiguousByteDevice::reset() |
| 66 | |
| 67 | Moves the internal read pointer back to the beginning. |
| 68 | Returns \c false if this was not possible. |
| 69 | |
| 70 | \sa atEnd() |
| 71 | |
| 72 | \internal |
| 73 | */ |
| 74 | /*! |
| 75 | \fn virtual qint64 QNonContiguousByteDevice::size() const |
| 76 | |
| 77 | Returns the size of the complete device or -1 if unknown. |
| 78 | May also return less/more than what can be actually read with readPointer() |
| 79 | |
| 80 | \internal |
| 81 | */ |
| 82 | /*! |
| 83 | \fn void QNonContiguousByteDevice::readyRead() |
| 84 | |
| 85 | Emitted when there is data available |
| 86 | |
| 87 | \internal |
| 88 | */ |
| 89 | /*! |
| 90 | \fn void QNonContiguousByteDevice::readProgress(qint64 current, qint64 total) |
| 91 | |
| 92 | Emitted when data has been "read" by advancing the read pointer |
| 93 | |
| 94 | \internal |
| 95 | */ |
| 96 | |
| 97 | QNonContiguousByteDevice::QNonContiguousByteDevice() : QObject((QObject*)nullptr) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | QNonContiguousByteDevice::~QNonContiguousByteDevice() |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | QNonContiguousByteDeviceByteArrayImpl::QNonContiguousByteDeviceByteArrayImpl(QByteArray ba) |
| 106 | : QNonContiguousByteDevice(), byteArray(std::move(ba)), view(byteArray) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | QNonContiguousByteDeviceByteArrayImpl::QNonContiguousByteDeviceByteArrayImpl(QBuffer *buffer) |
| 111 | : QNonContiguousByteDevice(), |
| 112 | byteArray(buffer->buffer()), |
| 113 | view(QByteArrayView(byteArray).sliced(pos: buffer->pos(), n: buffer->size() - buffer->pos())) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | QNonContiguousByteDeviceByteArrayImpl::~QNonContiguousByteDeviceByteArrayImpl() |
| 118 | { |
| 119 | } |
| 120 | |
| 121 | const char* QNonContiguousByteDeviceByteArrayImpl::readPointer(qint64 maximumLength, qint64 &len) |
| 122 | { |
| 123 | if (atEnd()) { |
| 124 | len = -1; |
| 125 | return nullptr; |
| 126 | } |
| 127 | |
| 128 | if (maximumLength != -1) |
| 129 | len = qMin(a: maximumLength, b: size() - currentPosition); |
| 130 | else |
| 131 | len = size() - currentPosition; |
| 132 | |
| 133 | return view.data() + currentPosition; |
| 134 | } |
| 135 | |
| 136 | bool QNonContiguousByteDeviceByteArrayImpl::advanceReadPointer(qint64 amount) |
| 137 | { |
| 138 | currentPosition += amount; |
| 139 | emit readProgress(current: currentPosition, total: size()); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | bool QNonContiguousByteDeviceByteArrayImpl::atEnd() const |
| 144 | { |
| 145 | return currentPosition >= size(); |
| 146 | } |
| 147 | |
| 148 | bool QNonContiguousByteDeviceByteArrayImpl::reset() |
| 149 | { |
| 150 | currentPosition = 0; |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | qint64 QNonContiguousByteDeviceByteArrayImpl::size() const |
| 155 | { |
| 156 | return view.size(); |
| 157 | } |
| 158 | |
| 159 | qint64 QNonContiguousByteDeviceByteArrayImpl::pos() const |
| 160 | { |
| 161 | return currentPosition; |
| 162 | } |
| 163 | |
| 164 | QNonContiguousByteDeviceRingBufferImpl::QNonContiguousByteDeviceRingBufferImpl(std::shared_ptr<QRingBuffer> rb) |
| 165 | : QNonContiguousByteDevice(), ringBuffer(std::move(rb)) |
| 166 | { |
| 167 | } |
| 168 | |
| 169 | QNonContiguousByteDeviceRingBufferImpl::~QNonContiguousByteDeviceRingBufferImpl() |
| 170 | { |
| 171 | } |
| 172 | |
| 173 | const char* QNonContiguousByteDeviceRingBufferImpl::readPointer(qint64 maximumLength, qint64 &len) |
| 174 | { |
| 175 | if (atEnd()) { |
| 176 | len = -1; |
| 177 | return nullptr; |
| 178 | } |
| 179 | |
| 180 | const char *returnValue = ringBuffer->readPointerAtPosition(pos: currentPosition, length&: len); |
| 181 | |
| 182 | if (maximumLength != -1) |
| 183 | len = qMin(a: len, b: maximumLength); |
| 184 | |
| 185 | return returnValue; |
| 186 | } |
| 187 | |
| 188 | bool QNonContiguousByteDeviceRingBufferImpl::advanceReadPointer(qint64 amount) |
| 189 | { |
| 190 | currentPosition += amount; |
| 191 | emit readProgress(current: currentPosition, total: size()); |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | bool QNonContiguousByteDeviceRingBufferImpl::atEnd() const |
| 196 | { |
| 197 | return currentPosition >= size(); |
| 198 | } |
| 199 | |
| 200 | qint64 QNonContiguousByteDeviceRingBufferImpl::pos() const |
| 201 | { |
| 202 | return currentPosition; |
| 203 | } |
| 204 | |
| 205 | bool QNonContiguousByteDeviceRingBufferImpl::reset() |
| 206 | { |
| 207 | currentPosition = 0; |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | qint64 QNonContiguousByteDeviceRingBufferImpl::size() const |
| 212 | { |
| 213 | return ringBuffer->size(); |
| 214 | } |
| 215 | |
| 216 | QNonContiguousByteDeviceIoDeviceImpl::QNonContiguousByteDeviceIoDeviceImpl(QIODevice *d) |
| 217 | : QNonContiguousByteDevice(), |
| 218 | device(d), |
| 219 | currentReadBuffer(nullptr), |
| 220 | currentReadBufferSize(16 * 1024), |
| 221 | currentReadBufferAmount(0), |
| 222 | currentReadBufferPosition(0), |
| 223 | totalAdvancements(0), |
| 224 | eof(false), |
| 225 | initialPosition(d->pos()) |
| 226 | { |
| 227 | connect(sender: device, signal: &QIODevice::readyRead, context: this, |
| 228 | slot: &QNonContiguousByteDevice::readyRead); |
| 229 | connect(sender: device, signal: &QIODevice::readChannelFinished, context: this, |
| 230 | slot: &QNonContiguousByteDevice::readyRead); |
| 231 | } |
| 232 | |
| 233 | QNonContiguousByteDeviceIoDeviceImpl::~QNonContiguousByteDeviceIoDeviceImpl() |
| 234 | { |
| 235 | delete currentReadBuffer; |
| 236 | } |
| 237 | |
| 238 | const char *QNonContiguousByteDeviceIoDeviceImpl::readPointer(qint64 maximumLength, qint64 &len) |
| 239 | { |
| 240 | if (eof) { |
| 241 | len = -1; |
| 242 | return nullptr; |
| 243 | } |
| 244 | |
| 245 | if (currentReadBuffer == nullptr) |
| 246 | currentReadBuffer = new QByteArray(currentReadBufferSize, '\0'); // lazy alloc |
| 247 | |
| 248 | if (maximumLength == -1) |
| 249 | maximumLength = currentReadBufferSize; |
| 250 | |
| 251 | if (currentReadBufferAmount - currentReadBufferPosition > 0) { |
| 252 | len = currentReadBufferAmount - currentReadBufferPosition; |
| 253 | return currentReadBuffer->data() + currentReadBufferPosition; |
| 254 | } |
| 255 | |
| 256 | qint64 haveRead = device->read(data: currentReadBuffer->data(), |
| 257 | maxlen: qMin(a: maximumLength, b: currentReadBufferSize)); |
| 258 | |
| 259 | if ((haveRead == -1) || (haveRead == 0 && device->atEnd() && !device->isSequential())) { |
| 260 | eof = true; |
| 261 | len = -1; |
| 262 | // size was unknown before, emit a readProgress with the final size |
| 263 | if (size() == -1) |
| 264 | emit readProgress(current: totalAdvancements, total: totalAdvancements); |
| 265 | return nullptr; |
| 266 | } |
| 267 | |
| 268 | currentReadBufferAmount = haveRead; |
| 269 | currentReadBufferPosition = 0; |
| 270 | |
| 271 | len = haveRead; |
| 272 | return currentReadBuffer->data(); |
| 273 | } |
| 274 | |
| 275 | bool QNonContiguousByteDeviceIoDeviceImpl::advanceReadPointer(qint64 amount) |
| 276 | { |
| 277 | totalAdvancements += amount; |
| 278 | |
| 279 | // normal advancement |
| 280 | currentReadBufferPosition += amount; |
| 281 | |
| 282 | if (size() == -1) |
| 283 | emit readProgress(current: totalAdvancements, total: totalAdvancements); |
| 284 | else |
| 285 | emit readProgress(current: totalAdvancements, total: size()); |
| 286 | |
| 287 | // advancing over that what has actually been read before |
| 288 | if (currentReadBufferPosition > currentReadBufferAmount) { |
| 289 | qint64 i = currentReadBufferPosition - currentReadBufferAmount; |
| 290 | while (i > 0) { |
| 291 | if (!device->getChar(c: nullptr)) { |
| 292 | emit readProgress(current: totalAdvancements - i, total: size()); |
| 293 | return false; // ### FIXME handle eof |
| 294 | } |
| 295 | i--; |
| 296 | } |
| 297 | |
| 298 | currentReadBufferPosition = 0; |
| 299 | currentReadBufferAmount = 0; |
| 300 | } |
| 301 | |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | bool QNonContiguousByteDeviceIoDeviceImpl::atEnd() const |
| 306 | { |
| 307 | return eof; |
| 308 | } |
| 309 | |
| 310 | bool QNonContiguousByteDeviceIoDeviceImpl::reset() |
| 311 | { |
| 312 | bool reset = (initialPosition == 0) ? device->reset() : device->seek(pos: initialPosition); |
| 313 | if (reset) { |
| 314 | eof = false; // assume eof is false, it will be true after a read has been attempted |
| 315 | totalAdvancements = 0; // reset the progress counter |
| 316 | if (currentReadBuffer) { |
| 317 | delete currentReadBuffer; |
| 318 | currentReadBuffer = nullptr; |
| 319 | } |
| 320 | currentReadBufferAmount = 0; |
| 321 | currentReadBufferPosition = 0; |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | qint64 QNonContiguousByteDeviceIoDeviceImpl::size() const |
| 329 | { |
| 330 | // note that this is different from the size() implementation of QIODevice! |
| 331 | |
| 332 | if (device->isSequential()) |
| 333 | return -1; |
| 334 | |
| 335 | return device->size() - initialPosition; |
| 336 | } |
| 337 | |
| 338 | qint64 QNonContiguousByteDeviceIoDeviceImpl::pos() const |
| 339 | { |
| 340 | if (device->isSequential()) |
| 341 | return -1; |
| 342 | |
| 343 | return device->pos(); |
| 344 | } |
| 345 | |
| 346 | QByteDeviceWrappingIoDevice::QByteDeviceWrappingIoDevice(QNonContiguousByteDevice *bd) |
| 347 | : QIODevice(nullptr), byteDevice(bd) |
| 348 | { |
| 349 | connect(sender: bd, signal: &QNonContiguousByteDevice::readyRead, context: this, slot: &QIODevice::readyRead); |
| 350 | |
| 351 | open(mode: ReadOnly); |
| 352 | } |
| 353 | |
| 354 | QByteDeviceWrappingIoDevice::~QByteDeviceWrappingIoDevice() |
| 355 | = default; |
| 356 | |
| 357 | bool QByteDeviceWrappingIoDevice::isSequential() const |
| 358 | { |
| 359 | return (byteDevice->size() == -1); |
| 360 | } |
| 361 | |
| 362 | bool QByteDeviceWrappingIoDevice::atEnd() const |
| 363 | { |
| 364 | return byteDevice->atEnd(); |
| 365 | } |
| 366 | |
| 367 | bool QByteDeviceWrappingIoDevice::reset() |
| 368 | { |
| 369 | return byteDevice->reset(); |
| 370 | } |
| 371 | |
| 372 | qint64 QByteDeviceWrappingIoDevice::size() const |
| 373 | { |
| 374 | if (isSequential()) |
| 375 | return 0; |
| 376 | |
| 377 | return byteDevice->size(); |
| 378 | } |
| 379 | |
| 380 | qint64 QByteDeviceWrappingIoDevice::readData(char *data, qint64 maxSize) |
| 381 | { |
| 382 | qint64 len; |
| 383 | const char *readPointer = byteDevice->readPointer(maximumLength: maxSize, len); |
| 384 | if (len == -1) |
| 385 | return -1; |
| 386 | |
| 387 | memcpy(dest: data, src: readPointer, n: len); |
| 388 | byteDevice->advanceReadPointer(amount: len); |
| 389 | return len; |
| 390 | } |
| 391 | |
| 392 | qint64 QByteDeviceWrappingIoDevice::writeData(const char *data, qint64 maxSize) |
| 393 | { |
| 394 | Q_UNUSED(data); |
| 395 | Q_UNUSED(maxSize); |
| 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | /*! |
| 400 | \class QNonContiguousByteDeviceFactory |
| 401 | \inmodule QtCore |
| 402 | \since 4.6 |
| 403 | |
| 404 | Creates a QNonContiguousByteDevice out of a QIODevice, |
| 405 | QByteArray etc. |
| 406 | |
| 407 | \sa QNonContiguousByteDevice |
| 408 | |
| 409 | \internal |
| 410 | */ |
| 411 | |
| 412 | /*! |
| 413 | \fn static QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(QIODevice *device) |
| 414 | |
| 415 | Create a QNonContiguousByteDevice out of a QIODevice. |
| 416 | For QFile, QBuffer and all other QIoDevice, sequential or not. |
| 417 | |
| 418 | \internal |
| 419 | */ |
| 420 | QNonContiguousByteDevice *QNonContiguousByteDeviceFactory::create(QIODevice *device) |
| 421 | { |
| 422 | // shortcut if it is a QBuffer |
| 423 | if (QBuffer *buffer = qobject_cast<QBuffer *>(object: device)) { |
| 424 | return new QNonContiguousByteDeviceByteArrayImpl(buffer); |
| 425 | } |
| 426 | |
| 427 | // ### FIXME special case if device is a QFile that supports map() |
| 428 | // then we can actually deal with the file without using read/peek |
| 429 | |
| 430 | // generic QIODevice |
| 431 | return new QNonContiguousByteDeviceIoDeviceImpl(device); // FIXME |
| 432 | } |
| 433 | |
| 434 | /*! |
| 435 | Create a QNonContiguousByteDevice out of a QIODevice, return it in a std::shared_ptr. |
| 436 | For QFile, QBuffer and all other QIODevice, sequential or not. |
| 437 | |
| 438 | \internal |
| 439 | */ |
| 440 | std::shared_ptr<QNonContiguousByteDevice> QNonContiguousByteDeviceFactory::createShared(QIODevice *device) |
| 441 | { |
| 442 | // shortcut if it is a QBuffer |
| 443 | if (QBuffer *buffer = qobject_cast<QBuffer*>(object: device)) |
| 444 | return std::make_shared<QNonContiguousByteDeviceByteArrayImpl>(args&: buffer); |
| 445 | |
| 446 | // ### FIXME special case if device is a QFile that supports map() |
| 447 | // then we can actually deal with the file without using read/peek |
| 448 | |
| 449 | // generic QIODevice |
| 450 | return std::make_shared<QNonContiguousByteDeviceIoDeviceImpl>(args&: device); // FIXME |
| 451 | } |
| 452 | |
| 453 | /*! |
| 454 | \fn static QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(std::shared_ptr<QRingBuffer> ringBuffer) |
| 455 | |
| 456 | Create a QNonContiguousByteDevice out of a QRingBuffer. |
| 457 | |
| 458 | \internal |
| 459 | */ |
| 460 | QNonContiguousByteDevice * |
| 461 | QNonContiguousByteDeviceFactory::create(std::shared_ptr<QRingBuffer> ringBuffer) |
| 462 | { |
| 463 | return new QNonContiguousByteDeviceRingBufferImpl(std::move(ringBuffer)); |
| 464 | } |
| 465 | |
| 466 | /*! |
| 467 | Create a QNonContiguousByteDevice out of a QRingBuffer, return it in a std::shared_ptr. |
| 468 | |
| 469 | \internal |
| 470 | */ |
| 471 | std::shared_ptr<QNonContiguousByteDevice> |
| 472 | QNonContiguousByteDeviceFactory::createShared(std::shared_ptr<QRingBuffer> ringBuffer) |
| 473 | { |
| 474 | return std::make_shared<QNonContiguousByteDeviceRingBufferImpl>(args: std::move(ringBuffer)); |
| 475 | } |
| 476 | |
| 477 | /*! |
| 478 | \fn static QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(QByteArray *byteArray) |
| 479 | |
| 480 | Create a QNonContiguousByteDevice out of a QByteArray. |
| 481 | |
| 482 | \internal |
| 483 | */ |
| 484 | QNonContiguousByteDevice* QNonContiguousByteDeviceFactory::create(const QByteArray &byteArray) |
| 485 | { |
| 486 | return new QNonContiguousByteDeviceByteArrayImpl(byteArray); |
| 487 | } |
| 488 | |
| 489 | /*! |
| 490 | Create a QNonContiguousByteDevice out of a QByteArray. |
| 491 | |
| 492 | \internal |
| 493 | */ |
| 494 | std::shared_ptr<QNonContiguousByteDevice> |
| 495 | QNonContiguousByteDeviceFactory::createShared(const QByteArray &byteArray) |
| 496 | { |
| 497 | return std::make_shared<QNonContiguousByteDeviceByteArrayImpl>(args: byteArray); |
| 498 | } |
| 499 | |
| 500 | /*! |
| 501 | \fn static QIODevice* QNonContiguousByteDeviceFactory::wrap(QNonContiguousByteDevice* byteDevice) |
| 502 | |
| 503 | Wrap the \a byteDevice (possibly again) into a QIODevice. |
| 504 | |
| 505 | \internal |
| 506 | */ |
| 507 | QIODevice *QNonContiguousByteDeviceFactory::wrap(QNonContiguousByteDevice *byteDevice) |
| 508 | { |
| 509 | // ### FIXME if it already has been based on QIoDevice, we could that one out again |
| 510 | // and save some calling |
| 511 | |
| 512 | // needed for FTP backend |
| 513 | |
| 514 | return new QByteDeviceWrappingIoDevice(byteDevice); |
| 515 | } |
| 516 | |
| 517 | QT_END_NAMESPACE |
| 518 | |
| 519 | #include "moc_qnoncontiguousbytedevice_p.cpp" |
| 520 | |