bool QDataProtocol::ParseCommonHeader( QBuffer &buffer, QByteArray &byData ) { qint32 nUInt32Len = sizeof ( quint32 ); bool bRet = true; byData = buffer.read( nUInt32Len ); if ( byData.isEmpty( ) ) { bRet = false; goto ERROR_PROCESS; } dataPackage.header.common.nDataLength = N2H< quint32 >( byData ); byData = buffer.read( nUInt32Len ); if ( byData.isEmpty( ) ) { bRet = false; goto ERROR_PROCESS; } dataPackage.header.common.nReserved = N2H< quint32 >( byData ); byData = buffer.read( nUInt32Len ); if ( byData.isEmpty( ) ) { bRet = false; goto ERROR_PROCESS; } dataPackage.header.common.eParcelType = ( PackageType ) N2H< quint32 >( byData ); ERROR_PROCESS: return bRet; }
TEST_F(ProtocolFactory, SelectFragmented) { QBuffer device; QByteArray data1("TEST"); QByteArray data2("TESTDATA1\nOTHER"); MockProtocol mock1; MockProtocol mock2; MockProtocol mockOutput; EXPECT_CALL(mock1, HandshakeSize()).WillRepeatedly(Return(2)); EXPECT_CALL(mock2, HandshakeSize()).WillRepeatedly(Return(10)); EXPECT_CALL(mock1, ConstructIfSuitable(QByteArray("TE"), nullptr)).InSequence(_seq) .WillOnce(Return(nullptr)); EXPECT_CALL(mock1, ConstructIfSuitable(QByteArray("TE"), nullptr)).InSequence(_seq) .WillOnce(Return(nullptr)); EXPECT_CALL(mock2, ConstructIfSuitable(QByteArray("TESTDATA1\n"), nullptr)).InSequence(_seq) .WillOnce(Return(&mockOutput)); _factory.RegisterProtocol(&mock1); _factory.RegisterProtocol(&mock2); device.setBuffer(&data1); device.open(QIODevice::ReadOnly); EXPECT_EQ(_factory.CreateProtocol(device), nullptr); EXPECT_EQ(device.read(15), QByteArray("TEST")); device.close(); device.setBuffer(&data2); device.open(QIODevice::ReadOnly); EXPECT_EQ(_factory.CreateProtocol(device), &mockOutput); EXPECT_EQ(device.read(5), QByteArray("OTHER")); device.close(); }
bool CNetHeader::Load(QBuffer& buffer) { quint32 nRead = buffer.read((char*)&m_nMsgLen, sizeof(m_nMsgLen)); //if (buffer.read((char*)&m_nMsgLen, sizeof(m_nMsgLen)) != sizeof(m_nMsgLen)){return false;} if (buffer.read((char*)&m_nMsgId, sizeof(m_nMsgId)) != sizeof(m_nMsgId)){return false;} if (buffer.read((char*)&m_nFlags, sizeof(m_nFlags)) != sizeof(m_nFlags)){return false;} if (m_nMsgLen < HEADER_SIZE) { Q_ASSERT(NULL); return false; } if ((m_nMsgId < AIRPDF_MSG_ID_SRV_VERSION) || (m_nMsgId>AIRPDF_MSG_LAST_MSG)) { Q_ASSERT(NULL); return false; } if ((m_nFlags&MSG_FLAG_STREAM_MSG)==0) {//none stream msg has 10485760 =10MB limit if (m_nMsgLen>10485760) { Q_ASSERT(NULL); return false; } } return true; }
bool QDataProtocol::ParseTableHeader( QBuffer &buffer, QByteArray &byData ) { bool bRet = true; qint32 nUInt32Len = sizeof ( quint32 ); qint32 nUInt16Len = sizeof ( quint16 ); byData = buffer.read( nUInt32Len ); if ( byData.isEmpty( ) ) { bRet = false; goto ERROR_PROCESS; } dataPackage.header.aux.headerTable.eType = ( TableType ) N2H< quint32 >( byData ); byData = buffer.read( nUInt16Len ); if ( byData.isEmpty( ) ) { bRet = false; goto ERROR_PROCESS; } dataPackage.header.aux.headerTable.nRowCount = N2H< quint16 >( byData ); byData = buffer.read( nUInt16Len ); if ( byData.isEmpty( ) ) { bRet = false; goto ERROR_PROCESS; } dataPackage.header.aux.headerTable.nColumnCount = N2H< quint16 >( byData ); byData = buffer.read( nUInt32Len ); if ( byData.isEmpty( ) ) { bRet = false; goto ERROR_PROCESS; } dataPackage.header.aux.headerTable.nReserved = N2H< quint32 >( byData ); ERROR_PROCESS: return bRet; }
bool QDataProtocol::ParseFileHeader( QBuffer &buffer, QByteArray &byData ) { qint32 nUInt32Len = sizeof ( quint32 ); bool bRet = true; byData = buffer.read( nUInt32Len ); if ( byData.isEmpty( ) ) { bRet = false; goto ERROR_PROCESS; } dataPackage.header.aux.headerFile.eType = ( FileType ) N2H< quint32 >( byData ); byData = buffer.read( nUInt32Len ); if ( byData.isEmpty( ) ) { bRet = false; goto ERROR_PROCESS; } dataPackage.header.aux.headerFile.nNameLength = N2H< quint32 >( byData ); byData = buffer.read( nUInt32Len ); if ( byData.isEmpty( ) ) { bRet = false; goto ERROR_PROCESS; } dataPackage.header.aux.headerFile.nContentLength = N2H< quint32 >( byData ); byData = buffer.read( nUInt32Len ); if ( byData.isEmpty( ) ) { bRet = false; goto ERROR_PROCESS; } dataPackage.header.aux.headerFile.nReserved = N2H< quint32 >( byData ); ERROR_PROCESS: return bRet; }
void tst_QRingBuffer::readPointerAtPositionWriteRead() { //create some data QBuffer inData; inData.open(QIODevice::ReadWrite); inData.putChar(0x42); inData.putChar(0x23); inData.write("Qt rocks!"); for (int i = 0; i < 5000; i++) inData.write(QString("Number %1").arg(i).toUtf8()); inData.reset(); QVERIFY(inData.size() > 0); //put the inData in the QRingBuffer QRingBuffer ringBuffer; qint64 remaining = inData.size(); while (remaining > 0) { // write in chunks of 50 bytes // this ensures there will be multiple QByteArrays inside the QRingBuffer // since QRingBuffer is then only using individual arrays of around 4000 bytes qint64 thisWrite = qMin(remaining, qint64(50)); char *pos = ringBuffer.reserve(thisWrite); inData.read(pos, thisWrite); remaining -= thisWrite; } // was data put into it? QVERIFY(ringBuffer.size() > 0); QCOMPARE(qint64(ringBuffer.size()), inData.size()); //read from the QRingBuffer in loop, put back into another QBuffer QBuffer outData; outData.open(QIODevice::ReadWrite); remaining = ringBuffer.size(); while (remaining > 0) { qint64 thisRead; // always try to read as much as possible const char *buf = ringBuffer.readPointerAtPosition(ringBuffer.size() - remaining, thisRead); outData.write(buf, thisRead); remaining -= thisRead; } outData.reset(); QVERIFY(outData.size() > 0); // was the data read from the QRingBuffer the same as the one written into it? QCOMPARE(outData.size(), inData.size()); QVERIFY(outData.buffer().startsWith(inData.buffer())); }
qint8 CNetMsgBaseBuffered::OnReceivedData(QBuffer& buffer, quint32 nSize) { qint32 nMsgLen = m_header.m_nMsgLen-CNetHeader::HEADER_SIZE; if ((nMsgLen > 10485760) || (nMsgLen < 1)) //10 MB limit for buffered msgs { Q_ASSERT(NULL); AIRPDF_LOG(LOG_LEVEL_ERROR, QString("Invalid msg %1 . Msg len %2 excited.").arg(m_header.m_nMsgId).arg(m_header.m_nMsgLen)); return -1; } if (m_pArrData== NULL) { m_pArrData = new QByteArray(); m_pArrData->reserve(nMsgLen); } qint32 nRead = nMsgLen-m_pArrData->size(); if (nSize < nRead) { nRead = nSize; } m_pArrData->append(buffer.read(nRead)); if (m_pArrData->size() == nMsgLen) { qint8 nRet = -1; { QDataStream inStream(m_pArrData, QIODevice::ReadOnly); inStream.setByteOrder(QDataStream::LittleEndian); if (LoadMsg(inStream)) { nRet = 0; } else { AIRPDF_LOG(LOG_LEVEL_ERROR, QString("Invalid msg %1 . Msg format error.").arg(m_header.m_nMsgId)); } } delete m_pArrData; m_pArrData = NULL; return nRet; } return 1;//not all data yet }
QList<QByteArray> CNetworkManager::getPacketsFromBuffer(QByteArray &buffer) { QList<QByteArray> ret; QByteArray packet; QByteArray endOfFrame; QDataStream stream(&endOfFrame, QIODevice::WriteOnly); stream << (quint32) 0xFFFF << (quint32) 0xFFFF; QBuffer in; in.setBuffer(&buffer); in.open(QIODevice::ReadOnly); do { QByteArray c = in.read(1); packet.append(c); if (packet.contains(endOfFrame)) { ret.append(packet); packet.clear(); } } while (in.bytesAvailable()); return ret; }
IO::Format IO::partFormat(const QString& filename) { QFile file(filename); if (!file.open(QIODevice::ReadOnly)) return UnknownFormat; const QByteArray contentsBegin = file.read(2048); // Assume a binary-based format // -- Binary STL ? const int binaryStlHeaderSize = 80 + sizeof(quint32); if (contentsBegin.size() >= binaryStlHeaderSize) { QBuffer buffer; buffer.setData(contentsBegin); buffer.open(QIODevice::ReadOnly); buffer.seek(80); // Skip header quint32 facetsCount = 0; buffer.read(reinterpret_cast<char*>(&facetsCount), sizeof(quint32)); const unsigned facetSize = (sizeof(float) * 12) + sizeof(quint16); if ((facetSize * facetsCount + binaryStlHeaderSize) == file.size()) return BinaryStlFormat; } // Assume a text-based format const QString contentsBeginText(contentsBegin); if (contentsBeginText.contains(QRegExp("^.{72}S\\s*[0-9]+\\s*[\\n\\r\\f]"))) return IgesFormat; if (contentsBeginText.contains(QRegExp("^\\s*ISO-10303-21\\s*;\\s*HEADER"))) return StepFormat; if (contentsBeginText.contains(QRegExp("^\\s*DBRep_DrawableShape"))) return OccBrepFormat; if (contentsBeginText.contains(QRegExp("^\\s*solid"))) return AsciiStlFormat; // Fallback case return UnknownFormat; }
//-------------------------------------------------------------------- void tst_QIODevice::unget() { #if defined(Q_OS_WINCE) && defined(WINCE_EMULATOR_TEST) QSKIP("Networking tests in a WinCE emulator are unstable", SkipAll); #endif QBuffer buffer; buffer.open(QBuffer::ReadWrite); buffer.write("ZXCV"); buffer.seek(0); QCOMPARE(buffer.read(4), QByteArray("ZXCV")); QCOMPARE(buffer.pos(), qint64(4)); buffer.ungetChar('a'); buffer.ungetChar('b'); buffer.ungetChar('c'); buffer.ungetChar('d'); QCOMPARE(buffer.pos(), qint64(0)); char buf[6]; QCOMPARE(buffer.readLine(buf, 5), qint64(4)); QCOMPARE(buffer.pos(), qint64(4)); QCOMPARE(static_cast<const char*>(buf), "dcba"); buffer.ungetChar('a'); buffer.ungetChar('b'); buffer.ungetChar('c'); buffer.ungetChar('d'); QCOMPARE(buffer.pos(), qint64(0)); for (int i = 0; i < 5; ++i) { buf[0] = '@'; buf[1] = '@'; QTest::ignoreMessage(QtWarningMsg, "QIODevice::readLine: Called with maxSize < 2"); QCOMPARE(buffer.readLine(buf, 1), qint64(-1)); QCOMPARE(buffer.readLine(buf, 2), qint64(i < 4 ? 1 : -1)); switch (i) { case 0: QCOMPARE(buf[0], 'd'); break; case 1: QCOMPARE(buf[0], 'c'); break; case 2: QCOMPARE(buf[0], 'b'); break; case 3: QCOMPARE(buf[0], 'a'); break; case 4: QCOMPARE(buf[0], '\0'); break; } QCOMPARE(buf[1], i < 4 ? '\0' : '@'); } buffer.ungetChar('\n'); QCOMPARE(buffer.readLine(), QByteArray("\n")); buffer.seek(1); buffer.readLine(buf, 3); QCOMPARE(static_cast<const char*>(buf), "XC"); buffer.seek(4); buffer.ungetChar('Q'); QCOMPARE(buffer.readLine(buf, 3), qint64(1)); for (int i = 0; i < 2; ++i) { QTcpSocket socket; QIODevice *dev; QByteArray result; const char *lineResult; if (i == 0) { dev = &buffer; result = QByteArray("ZXCV"); lineResult = "ZXCV"; } else { socket.connectToHost(QtNetworkSettings::serverName(), 80); socket.write("GET / HTTP/1.0\r\n\r\n"); QVERIFY(socket.waitForReadyRead()); dev = &socket; result = QByteArray("HTTP"); lineResult = "Date"; } char ch, ch2; dev->seek(0); dev->getChar(&ch); dev->ungetChar(ch); QCOMPARE(dev->peek(4), result); dev->getChar(&ch); dev->getChar(&ch2); dev->ungetChar(ch2); dev->ungetChar(ch); QCOMPARE(dev->read(1), result.left(1)); QCOMPARE(dev->read(3), result.right(3)); if (i == 0) dev->seek(0); else dev->readLine(); dev->getChar(&ch); dev->ungetChar(ch); dev->readLine(buf, 5); QCOMPARE(static_cast<const char*>(buf), lineResult); if (i == 1) socket.close(); } }
void UploadAssetTask::run() { auto data = _receivedMessage->getMessage(); QBuffer buffer { &data }; buffer.open(QIODevice::ReadOnly); MessageID messageID; buffer.read(reinterpret_cast<char*>(&messageID), sizeof(messageID)); uint64_t fileSize; buffer.read(reinterpret_cast<char*>(&fileSize), sizeof(fileSize)); qDebug() << "UploadAssetTask reading a file of " << fileSize << "bytes from" << uuidStringWithoutCurlyBraces(_senderNode->getUUID()); auto replyPacket = NLPacket::create(PacketType::AssetUploadReply); replyPacket->writePrimitive(messageID); if (fileSize > MAX_UPLOAD_SIZE) { replyPacket->writePrimitive(AssetServerError::AssetTooLarge); } else { QByteArray fileData = buffer.read(fileSize); auto hash = hashData(fileData); auto hexHash = hash.toHex(); qDebug() << "Hash for uploaded file from" << uuidStringWithoutCurlyBraces(_senderNode->getUUID()) << "is: (" << hexHash << ") "; QFile file { _resourcesDir.filePath(QString(hexHash)) }; bool existingCorrectFile = false; if (file.exists()) { // check if the local file has the correct contents, otherwise we overwrite if (file.open(QIODevice::ReadOnly) && hashData(file.readAll()) == hash) { qDebug() << "Not overwriting existing verified file: " << hexHash; existingCorrectFile = true; replyPacket->writePrimitive(AssetServerError::NoError); replyPacket->write(hash); } else { qDebug() << "Overwriting an existing file whose contents did not match the expected hash: " << hexHash; file.close(); } } if (!existingCorrectFile) { if (file.open(QIODevice::WriteOnly) && file.write(fileData) == qint64(fileSize)) { qDebug() << "Wrote file" << hexHash << "to disk. Upload complete"; file.close(); replyPacket->writePrimitive(AssetServerError::NoError); replyPacket->write(hash); } else { qWarning() << "Failed to upload or write to file" << hexHash << " - upload failed."; // upload has failed - remove the file and return an error auto removed = file.remove(); if (!removed) { qWarning() << "Removal of failed upload file" << hexHash << "failed."; } replyPacket->writePrimitive(AssetServerError::FileOperationFailed); } } } auto nodeList = DependencyManager::get<NodeList>(); nodeList->sendPacket(std::move(replyPacket), *_senderNode); }