bool KTar::KTarPrivate::readLonglink(char *buffer,QByteArray &longlink) { qint64 n = 0; //qDebug() << "reading longlink from pos " << q->device()->pos(); QIODevice *dev = q->device(); // read size of longlink from size field in header // size is in bytes including the trailing null (which we ignore) qint64 size = QByteArray( buffer + 0x7c, 12 ).trimmed().toLongLong( 0, 8 /*octal*/ ); size--; // ignore trailing null longlink.resize(size); qint64 offset = 0; while (size > 0) { int chunksize = qMin(size, 0x200LL); n = dev->read( longlink.data() + offset, chunksize ); if (n == -1) return false; size -= chunksize; offset += 0x200; }/*wend*/ // jump over the rest const int skip = 0x200 - (n % 0x200); if (skip <= 0x200) { if (dev->read(buffer,skip) != skip) return false; } return true; }
int EventStream::load(QIODevice &source) { uint8_t sig[4]; int32_t size; int offset; struct evt_file_header file_header; if (source.read((char *)&file_header, sizeof(file_header)) != sizeof(file_header)) { qDebug() << "Unable to read file header: " << source.errorString(); return -1; } if (memcmp(&file_header.magic1, EVENT_HDR_1, sizeof(file_header.magic1))) { qDebug() << "Error: File signature doesn't match"; qDebug("%x%x%x%x vs %x%x%x%x\n", file_header.magic1[0], file_header.magic1[1], file_header.magic1[2], file_header.magic1[3], EVENT_HDR_1[0], EVENT_HDR_1[1], EVENT_HDR_1[2], EVENT_HDR_1[3]); return -1; } size = _ntohl(file_header.count); for (offset=0; offset<size; offset++) { uint32_t addr; if (source.read((char *)&addr, sizeof(addr)) != sizeof(addr)) { qDebug() << "Unable to read addr table: " << source.errorString(); return -1; } addr = _ntohl(addr); _offsets.append(addr); } if (source.read((char *)sig, sizeof(sig)) != sizeof(sig)) { qDebug() << "Unable to read stream: " << source.errorString(); return -1; } if (memcmp(sig, EVENT_HDR_2, sizeof(sig))) { qDebug() << "Error: Main signature doesn't match"; qDebug("%x%x%x%x vs %x%x%x%x\n", sig[0], sig[1], sig[2], sig[3], EVENT_HDR_2[0], EVENT_HDR_2[1], EVENT_HDR_2[2], EVENT_HDR_2[3]); return -1; } for (offset=0; offset<size; offset++) { Event e(source, this); _events.append(e); } _currentEvents = _events; return 0; }
QVariant Message::parseValue(QIODevice &data, const quint8 wireType, const Types::ScalarType scalarType, const QString &tagPath) const { // A small sanity check. In this case, the wireType will take precedence. if ((scalarType != Types::Unknown) && (wireType != Types::LengthDelimeted) && (wireType != Types::getWireType(scalarType))) { qWarning() << tagPath << "wire type" << wireType << "does not match " "expected wire type" << Types::getWireType(scalarType) << "for " "scalar type" << scalarType; } switch (wireType) { case Types::Varint: // int32, int64, uint32, uint64, sint32, sint64, bool, enum. switch (scalarType) { case Types::Int32: return parseStandardVarint(data); case Types::Int64: return parseStandardVarint(data); case Types::Uint32: return parseUnsignedVarint(data); case Types::Uint64: return parseUnsignedVarint(data); case Types::Sint32: return parseSignedVarint(data); case Types::Sint64: return parseSignedVarint(data); case Types::Bool: return parseStandardVarint(data); case Types::Enumerator: return parseStandardVarint(data); default: return parseStandardVarint(data); } break; case Types::SixtyFourBit: // fixed64, sfixed64, double. switch (scalarType) { case Types::Fixed64: return parseFixedNumber<quint64>(data); case Types::Sfixed64: return parseFixedNumber<qint64>(data); case Types::Double: return parseFixedNumber<double>(data); default: return data.read(8); // The raw 8-byte sequence. } break; case Types::LengthDelimeted: // string, bytes, embedded messages, packed repeated fields. return parseLengthDelimitedValue(data, scalarType, tagPath); case Types::StartGroup: // deprecated. return parse(data, tagPath + pathSeparator); case Types::EndGroup: // deprecated. return QVariant(); // Caller will need to end the group started previously. case Types::ThirtyTwoBit: // fixed32, sfixed32, float. switch (scalarType) { case Types::Fixed32: return parseFixedNumber<quint32>(data); case Types::Sfixed32: return parseFixedNumber<qint32>(data); case Types::Float: return parseFixedNumber<float>(data); default: return data.read(4); // The raw 4-byte sequence. } break; } qWarning() << "invalid wireType:" << wireType << "(tagPath:" << tagPath << ')'; return QVariant(); }
void KArchiveFile::copyTo(const QString& dest) const { QFile f( dest + QLatin1Char('/') + name() ); if ( f.open( QIODevice::ReadWrite | QIODevice::Truncate ) ) { QIODevice* inputDev = createDevice(); // Read and write data in chunks to minimize memory usage const qint64 chunkSize = 1024 * 1024; qint64 remainingSize = d->size; QByteArray array; array.resize( int( qMin( chunkSize, remainingSize ) ) ); while ( remainingSize > 0 ) { const qint64 currentChunkSize = qMin( chunkSize, remainingSize ); const qint64 n = inputDev->read( array.data(), currentChunkSize ); Q_ASSERT( n == currentChunkSize ); f.write( array.data(), currentChunkSize ); remainingSize -= currentChunkSize; } f.close(); delete inputDev; } }
bool CharArchive::openAnimBoneCount() { if (!isOpen()) { qWarning() << "CharArchive::openAnimBoneCount" << "archive not opened"; return false; } _animBoneCount.clear(); LgpIterator it = _io.iterator(); while(it.hasNext()) { it.next(); const QString &fileName = it.fileName(); if(fileName.endsWith(".a", Qt::CaseInsensitive)) { QCoreApplication::processEvents(); QIODevice *aFile = it.file(); if(aFile && aFile->open(QIODevice::ReadOnly)) { quint32 boneCount; if(!aFile->seek(8) || aFile->read((char *)&boneCount, 4) != 4) { qWarning() << "CharArchive::openAnimBoneCount" << "animation error" << fileName; continue; } _animBoneCount.insert(boneCount, fileName.left(fileName.size()-2).toUpper()); } else { return false; } } } return true; }
void KoImageDataPrivate::copyToTemporary(QIODevice &device) { delete temporaryFile; temporaryFile = new KTemporaryFile(); temporaryFile->setPrefix("KoImageData"); if (!temporaryFile->open()) { kWarning(30006) << "open temporary file for writing failed"; errorCode = KoImageData::StorageFailed; return; } QCryptographicHash md5(QCryptographicHash::Md5); char buf[8096]; while (true) { device.waitForReadyRead(-1); qint64 bytes = device.read(buf, sizeof(buf)); if (bytes <= 0) break; // done! md5.addData(buf, bytes); do { bytes -= temporaryFile->write(buf, bytes); } while (bytes > 0); } key = KoImageDataPrivate::generateKey(md5.result()); temporaryFile->close(); //QFileInfo fi(*temporaryFile); dataStoreState = StateNotLoaded; }
const QByteArray& QxtMailAttachment::rawData() const { if (qxt_d->content == 0) { qWarning("QxtMailAttachment::rawData(): Content not set!"); static QByteArray defaultRv; return defaultRv; } if (qobject_cast<QBuffer *>(qxt_d->content.data()) == 0) { // content isn't hold in a buffer but in another kind of QIODevice // (probably a QFile...). Read the data and cache it into a buffer static QByteArray empty; QIODevice* c = content(); if (!c->isOpen() && !c->open(QIODevice::ReadOnly)) { qWarning() << "QxtMailAttachment::rawData(): Cannot open content for reading"; return empty; } QBuffer* cache = new QBuffer(); cache->open(QIODevice::WriteOnly); char buf[1024]; while (!c->atEnd()) { cache->write(buf, c->read(buf, 1024)); } if (qxt_d->deleteContent && qxt_d->content) qxt_d->content->deleteLater(); qxt_d->content = cache; qxt_d->deleteContent = true; } return qobject_cast<QBuffer *>(qxt_d->content.data())->data(); }
void KoImageDataPrivate::copyToTemporary(QIODevice &device) { delete temporaryFile; temporaryFile = new QTemporaryFile(QDir::tempPath() + "/" + qAppName() + QLatin1String("_XXXXXX")); if (!temporaryFile->open()) { warnFlake << "open temporary file for writing failed"; errorCode = KoImageData::StorageFailed; return; } QCryptographicHash md5(QCryptographicHash::Md5); char buf[8096]; while (true) { device.waitForReadyRead(-1); qint64 bytes = device.read(buf, sizeof(buf)); if (bytes <= 0) break; // done! md5.addData(buf, bytes); do { bytes -= temporaryFile->write(buf, bytes); } while (bytes > 0); } key = KoImageDataPrivate::generateKey(md5.result()); temporaryFile->close(); dataStoreState = StateNotLoaded; }
QVariant parseFixedNumber(QIODevice &data) { Q_ASSERT((sizeof(Type) == 4) || (sizeof(Type) == 8)); const QByteArray array = data.read(sizeof(Type)); if (array.size() != sizeof(Type)) return QVariant(); return qFromLittleEndian<Type>(reinterpret_cast<const uchar *>(array.constData())); }
static qint64 streamReadData(QIODevice &source, char *data, qint64 bytesTotal) { qint64 bytesRead = 0; qint64 bytesLeft = bytesTotal; int result; while (bytesLeft > 0) { result = source.read(data, bytesLeft); if (result < 0) { qDebug() << "Source had error:" << source.errorString(); return result; } if (result == 0) { if (!source.waitForReadyRead(-1)) { qDebug() << "Source is done, closing"; break; } } if (result > bytesTotal) { qDebug() << "Very bad. Wanted " << bytesTotal << ", but got " << result; } bytesRead += result; data += result; if (bytesLeft - result < 0) { qDebug() << "That was a really strange read"; } bytesLeft -= result; } if (bytesLeft < 0) { qDebug() << "How could bytesLeft (" << bytesLeft << ") be less than zero?"; } return bytesRead; }
bool KArchiveFile::copyTo(const QString &dest) const { QFile f(dest + QLatin1Char('/') + name()); if (f.open(QIODevice::ReadWrite | QIODevice::Truncate)) { QIODevice *inputDev = createDevice(); // Read and write data in chunks to minimize memory usage const qint64 chunkSize = 1024 * 1024; qint64 remainingSize = d->size; QByteArray array; array.resize(int(qMin(chunkSize, remainingSize))); while (remainingSize > 0) { const qint64 currentChunkSize = qMin(chunkSize, remainingSize); const qint64 n = inputDev->read(array.data(), currentChunkSize); Q_UNUSED(n) // except in Q_ASSERT Q_ASSERT(n == currentChunkSize); f.write(array.data(), currentChunkSize); remainingSize -= currentChunkSize; } f.setPermissions(withExecutablePerms(f.permissions(), permissions())); f.close(); delete inputDev; return true; } return false; }
/** * This tests the decompression using kfilterdev, basically. * To debug KTarPrivate::fillTempFile(). * * @dataProvider setupData */ void KArchiveTest::testUncompress() { QFETCH(QString, fileName); QFETCH(QString, mimeType); // testCreateTar must have been run first. QVERIFY(QFile::exists(fileName)); QIODevice *filterDev = KFilterDev::deviceForFile(fileName, mimeType, true); QVERIFY(filterDev); QByteArray buffer; buffer.resize(8*1024); kDebug() << "buffer.size()=" << buffer.size(); QVERIFY(filterDev->open(QIODevice::ReadOnly)); qint64 totalSize = 0; qint64 len = -1; while (!filterDev->atEnd() && len != 0) { len = filterDev->read(buffer.data(), buffer.size()); QVERIFY(len >= 0); totalSize += len; // kDebug() << "read len=" << len << " totalSize=" << totalSize; } filterDev->close(); delete filterDev; // kDebug() << "totalSize=" << totalSize; QVERIFY(totalSize > 26000); // 27648 here when using gunzip }
/*! \reimp */ void QxtXmlFileLoggerEngine::initLoggerEngine() { QxtAbstractFileLoggerEngine::initLoggerEngine(); // Mkay, we have an open file. We need to check that it's all valid. // at the end of this block of code, we either can't log, or the carat is ready for writing. /* <?xml version="1.0" encoding="UTF-8"?> <log> <entry type="Error" time="sometime"> <message>What's going on?</message> <message?Hi there</message> </entry> </log> */ QIODevice* file = device(); Q_ASSERT(file); if (file->size() == 0) { file->write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); file->write("<log>\n"); file->write("</log>"); } else { QByteArray data = file->read(64); if (!data.startsWith(QByteArray("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<log>"))) { QFile* ptr_fileTarget = static_cast<QFile*>(file); qxtLog->warning(QString(" is not a valid XML log file.").prepend(ptr_fileTarget->fileName())); killLoggerEngine(); return; } } }
QByteArray QIODeviceProto::read(qint64 maxSize) { QIODevice *item = qscriptvalue_cast<QIODevice*>(thisObject()); if (item) return item->read(maxSize); return QByteArray(); }
/*! \reimp */ void QNetworkDiskCache::updateMetaData(const QNetworkCacheMetaData &metaData) { #if defined(QNETWORKDISKCACHE_DEBUG) qDebug() << "QNetworkDiskCache::updateMetaData()" << metaData.url(); #endif QUrl url = metaData.url(); QIODevice *oldDevice = data(url); if (!oldDevice) { #if defined(QNETWORKDISKCACHE_DEBUG) qDebug() << "QNetworkDiskCache::updateMetaData(), no device!"; #endif return; } QIODevice *newDevice = prepare(metaData); if (!newDevice) { #if defined(QNETWORKDISKCACHE_DEBUG) qDebug() << "QNetworkDiskCache::updateMetaData(), no new device!" << url; #endif return; } char data[1024]; while (!oldDevice->atEnd()) { qint64 s = oldDevice->read(data, 1024); newDevice->write(data, s); } delete oldDevice; insert(newDevice); }
// streaming callbacks static int ReadFunc(void *opaque, uint8_t *buf, int buf_size) { QIODevice *io = (QIODevice*)opaque; buf_size = min(buf_size, (int) io->bytesAvailable()); return io->read((char*)buf, buf_size); }
qint64 QIODeviceProto::read(char *data, qint64 maxSize) { QIODevice *item = qscriptvalue_cast<QIODevice*>(thisObject()); if (item) return item->read(data, maxSize); return 0; }
void VideoData::copyToTemporary(QIODevice &device) { delete d; d = new VideoDataPrivate(); d->temporaryFile = new KTemporaryFile(); d->refCount.ref(); d->temporaryFile->setPrefix("KoVideoData"); if (!d->temporaryFile->open()) { kWarning(30006) << "open temporary file for writing failed"; d->errorCode = VideoData::StorageFailed; delete d; d = 0; return; } QCryptographicHash md5(QCryptographicHash::Md5); char buf[8192]; while (true) { device.waitForReadyRead(-1); qint64 bytes = device.read(buf, sizeof(buf)); if (bytes <= 0) break; // done! md5.addData(buf, bytes); do { bytes -= d->temporaryFile->write(buf, bytes); } while (bytes > 0); } d->key = VideoData::generateKey(md5.result()); d->temporaryFile->close(); QFileInfo fi(*(d->temporaryFile)); d->dataStoreState = StateSpooled; }
int QIOInputFunction(void* data, uint8_t* buf, size_t count) { QIODevice* io = static_cast<QIODevice*>(data); if (io->atEnd()) return 0; return io->read(reinterpret_cast<char*>(buf),count); }
quint32 Nicookie::readUint32LE(QIODevice &device) { QDataStream stream(device.read(4)); stream.setByteOrder(QDataStream::LittleEndian); quint32 i; stream >> i; return i; }
bool LibSingleFileInterface::copyFiles(const QList<QVariant> & files, const QString & destinationDirectory, Kerfuffle::ExtractionOptions options) { Q_UNUSED(files) Q_UNUSED(options) QString outputFileName = destinationDirectory; if (!destinationDirectory.endsWith(QLatin1Char('/'))) { outputFileName += QLatin1Char('/'); } outputFileName += uncompressedFileName(); outputFileName = overwriteFileName(outputFileName); if (outputFileName.isEmpty()) { return true; } kDebug() << "Extracting to" << outputFileName; QFile outputFile(outputFileName); if (!outputFile.open(QIODevice::WriteOnly)) { kDebug() << "Failed to open output file" << outputFile.errorString(); emit error(i18nc("@info", "Ark could not extract <filename>%1</filename>.", outputFile.fileName())); return false; } QIODevice *device = KFilterDev::deviceForFile(filename(), m_mimeType, false); if (!device) { kDebug() << "Could not create KFilterDev"; emit error(i18nc("@info", "Ark could not open <filename>%1</filename> for extraction.", filename())); return false; } device->open(QIODevice::ReadOnly); qint64 bytesRead; QByteArray dataChunk(1024*16, '\0'); // 16Kb while (true) { bytesRead = device->read(dataChunk.data(), dataChunk.size()); if (bytesRead == -1) { emit error(i18nc("@info", "There was an error while reading <filename>%1</filename> during extraction.", filename())); break; } else if (bytesRead == 0) { break; } outputFile.write(dataChunk.data(), bytesRead); } delete device; return true; }
qint8 readSignedByte(QIODevice &device) { qint8 result = 0; if (device.read(reinterpret_cast<char *> (&result), Q_INT64_C(1)) != Q_INT64_C(1)) { throw std::runtime_error("Unable to read file"); } return result; }
qint64 FlushedProcess::read(char * data, qint64 maxlen) { QIODevice * dev = readingIODevice(); if (dev == NULL) return -1; qint64 ret = dev->read(data,maxlen); if (ret < 0) { process.setErrorString(tr("Error reading from device!")); QMetaObject::invokeMethod(this,"error",Qt::QueuedConnection,Q_ARG(UnixProcess::ProcessError,UnixProcess::ReadError)); } return ret; }
qint32 readSignedDWord(QIODevice &device) { qint32 result = 0; if (device.read(reinterpret_cast<char *> (&result), Q_INT64_C(4)) != Q_INT64_C(4)) { throw std::runtime_error("Unable to read file"); } result = qFromLittleEndian(result); return result; }
QVariant parseUnsignedVarint(QIODevice &data) { quint64 result = 0; for (uchar byte = 0xFF, index = 0; byte >= 0x80; ++index) { const QByteArray array = data.read(1); if (array.isEmpty()) return QVariant(); byte = static_cast<uchar>(array.at(0)); result += (byte & Q_UINT64_C(0x7F)) << (7 * index); } return result; }
static bool copyData(QIODevice &inFile, QIODevice &outFile) { while (!inFile.atEnd()) { char buf[4096]; qint64 readLen = inFile.read(buf, 4096); if (readLen <= 0) return false; if (outFile.write(buf, readLen) != readLen) return false; } return true; }
QVariant Message::readLengthDelimitedValue(QIODevice &data) const { // Note: We're assuming length-delimited values use unsigned varints for lengths. // I haven't found any Protocl Buffers documentation to support / dispute this. const QVariant length = parseUnsignedVarint(data); if (!length.isValid()) { qWarning() << "failed to read prefix-delimited length"; return QVariant(); } const QByteArray value = data.read(length.toULongLong()); return (value.length() == length.toInt()) ? value : QVariant(); }
/* callback function for libisofs */ static int readf(char *buf, unsigned int start, unsigned int len, void *udata) { KISOFUNC; QIODevice* dev = (static_cast<KIso*>(udata))->device(); if (dev->seek((qint64)start << (qint64)11)) { if ((dev->read(buf, len << 11u)) != -1) return (len); } //qDebug() << "KIso::ReadRequest failed start: " << start << " len: " << len << endl; return -1; }
static void readPngChunk(png_structp png_ptr, png_bytep data, png_size_t length) { QIODevice *in = (QIODevice *)png_get_io_ptr(png_ptr); while (length) { int nr = in->read((char*)data, length); if (nr <= 0) { png_error(png_ptr, "Read Error"); return; } length -= nr; } }
bool copyData(QIODevice& src, QIODevice& dest) { bool success = false; if ((src.openMode() & QIODevice::ReadOnly) == 0) return false; if ((dest.openMode() & QIODevice::WriteOnly) == 0) return false; QByteArray bb( 65536, ' ' ); if (bb.size() > 0) // Check for memory allocation failure { qint64 byteswritten; qint64 bytesread = src.read( bb.data(), bb.size() ); success = (bytesread > 0); while (bytesread > 0) { byteswritten = dest.write( bb.data(), bytesread ); success &= (bytesread == byteswritten); bytesread = src.read( bb.data(), bb.size() ); } } return success; }