QString getDownloadFilename(QString title, QString url) { QString fileprefix = GetConfDir(); QDir dir(fileprefix); if (!dir.exists()) dir.mkdir(fileprefix); fileprefix += "/cache/metadata-thumbcache"; dir = QDir(fileprefix); if (!dir.exists()) dir.mkdir(fileprefix); QByteArray titlearr(title.toLatin1()); quint16 titleChecksum = qChecksum(titlearr.data(), titlearr.length()); QByteArray urlarr(url.toLatin1()); quint16 urlChecksum = qChecksum(urlarr.data(), urlarr.length()); QUrl qurl(url); QString ext = QFileInfo(qurl.path()).suffix(); QString basefilename = QString("thumbnail_%1_%2.%3") .arg(QString::number(urlChecksum)) .arg(QString::number(titleChecksum)).arg(ext); QString outputfile = QString("%1/%2").arg(fileprefix).arg(basefilename); return outputfile; }
bool SerialClient::writePacket(QByteArray& data) { quint16 checksum; checksum = qChecksum(data, data.size()); for(int i = 0;i < SERIAL_MAX_RETRY;i++) { m_stream << SERIAL_KEY; m_stream << data; m_stream << qChecksum(data.constData(), data.size()); if(checkOk()) return true; } return false; }
QString GetDownloadFilename(QString title, QString url) { QByteArray urlarr(url.toLatin1()); quint16 urlChecksum = qChecksum(urlarr.data(), urlarr.length()); QByteArray titlearr(title.toLatin1()); quint16 titleChecksum = qChecksum(titlearr.data(), titlearr.length()); QUrl qurl(url); QString ext = QFileInfo(qurl.path()).suffix(); QString basefilename = QString("download_%1_%2.%3") .arg(QString::number(urlChecksum)) .arg(QString::number(titleChecksum)).arg(ext); return basefilename; }
bool CNetRender::SendData(QTcpSocket *socket, sMessage msg) { // NetRender Message format (optional) // | qint32 | qint32 | qint32 |( 0 - ??? | quint16 |) // | command | id | size |( payload | checksum |) if (!socket) return false; if(socket->state() != QAbstractSocket::ConnectedState) return false; QByteArray byteArray; QDataStream socketWriteStream(&byteArray, QIODevice::ReadWrite); msg.size = msg.payload.size(); msg.id = actualId; // append header socketWriteStream << msg.command << msg.id << msg.size; // append payload if(msg.size > 0) { socketWriteStream.writeRawData(msg.payload.data(), msg.size); // append checksum qint16 checksum = qChecksum(msg.payload.data(), msg.size); socketWriteStream << checksum; } // write to socket socket->write(byteArray); //socket->waitForBytesWritten(); return true; }
quint16 getCrc(const QString &file_path) { quint16 result = 0; QFile file(file_path); if (file.open(QIODevice::ReadOnly)) { qint64 file_size = file.size(); if(file_size) { QDataStream in(&file); char *buf = new char[file_size]; int num_bytes_read = in.readRawData(buf, file_size); if (num_bytes_read != file_size) { log(QString("Number of bytes read(%) is not equal to file size(%2)" ": %3\n")); } result = qChecksum(buf, num_bytes_read); delete [] buf; } else { log(QString("File size is zero: %1\n").arg(file_path)); } } else { log(QString("Failed to open file for reading: %1\n").arg(file_path)); } return result; }
quint16 Zones::getFingerprint() const { quint64 x = 0; for (int i=0; i<ranges.size(); i++) { // from x += ranges[i].begin.toJulianDay(); // to x += ranges[i].end.toJulianDay(); // CP x += ranges[i].cp; // FTP x += ranges[i].ftp; // W' x += ranges[i].wprime; // W' x += ranges[i].pmax; // each zone definition (manual edit/default changed) for (int j=0; j<ranges[i].zones.count(); j++) { x += ranges[i].zones[j].lo; } } QByteArray ba = QByteArray::number(x); // we spot other things separately return qChecksum(ba, ba.length()); }
quint16 OSCPlugin::getHash(QString path) { quint16 hash; if (m_hash.contains(path)) hash = m_hash[path]; else { /* #include <QByteArray> #include <zlib.h> QByteArray ba("This is a Test 123 :)!"); ulong crc = crc32(0, NULL, 0); crc = crc32(crc, (const Bytef *)ba.data(), ba.size()); printf("CRC-32 = 0x%x\n", crc); */ /** No existing hash found. Add a new key to the table */ hash = qChecksum(path.toUtf8().data(), path.length()); m_hash[path] = hash; } return hash; }
// get fingerprint just for the range that applies on this date quint16 Zones::getFingerprint(QDate forDate) const { quint64 x = 0; // which range to apply ? int i = whichRange(forDate); if (i >= 0) { // CP x += ranges[i].cp; // FTP x += ranges[i].ftp; // W' x += ranges[i].wprime; // Pmax x += ranges[i].pmax; // each zone definition (manual edit/default changed) for (int j=0; j<ranges[i].zones.count(); j++) { x += ranges[i].zones[j].lo; } } QByteArray ba = QByteArray::number(x); // limits to only zones now as we sport weight separately return qChecksum(ba, ba.length()); }
void MTcpSocket::sendPacketInternal(const Mara::MPacket *pPacket) { QByteArray data; bool compressed = pPacket->compressed(); if(compressed) { QByteArray uncompressed(*(pPacket->serialize())); data = qCompress(uncompressed); qDebug("Compression Ratio: %d/%d", data.size(), uncompressed.size()); } else { data = *(pPacket->serialize()); } { QMutexLocker lock(&_mutex); write(_packetHeaderBytes); _socketStream << data.length(); _socketStream << pPacket->type(); write(data); _socketStream << qChecksum(data.constData(), data.length()); _socketStream << compressed; } if(pPacket->deleteOnSend()) delete pPacket; }
quint16 PaceZones::getFingerprint(QDate forDate) const { quint64 x = 0; int i = whichRange(forDate); if (i>=0) { // from x += ranges[i].begin.toJulianDay(); // to x += ranges[i].end.toJulianDay(); // CV x += ranges[i].cv; // each zone definition (manual edit/default changed) for (int j=0; j<ranges[i].zones.count(); j++) { x += ranges[i].zones[j].lo; } } QByteArray ba = QByteArray::number(x); return qChecksum(ba, ba.length()); }
/* Packets look like this: quint32 key = 4 bytes QByteArray data = n bytes quint16 checksum = 2 bytes */ bool SerialServer::readPacket(QByteArray *packetData) { while(1){ QByteArray data; quint32 key = 0; quint16 checksum = 0xFFFF; m_inStream >> key; if (key == HEADER_KEY) { m_inStream >> data; m_inStream >> checksum; qWarning("data.size()=%d", data.size()); qWarning("checksum=%x", checksum); if(checksum == qChecksum(data, data.size())) { *packetData = data; sendOk(); return true; } } m_inStream.skipRawData(512); m_inStream.resetStatus(); qWarning("Retry..."); if(m_quit) return false; }
bool QPicturePaintEngine::end() { Q_D(QPicturePaintEngine); #ifdef QT_PICTURE_DEBUG qDebug() << "QPicturePaintEngine::end()"; #endif d->pic_d->trecs++; d->s << (quint8) QPicturePrivate::PdcEnd << (quint8) 0; int cs_start = sizeof(quint32); // pos of checksum word int data_start = cs_start + sizeof(quint16); int brect_start = data_start + 2*sizeof(qint16) + 2*sizeof(quint8); int pos = d->pic_d->pictb.pos(); d->pic_d->pictb.seek(brect_start); if (d->pic_d->formatMajor >= 4) { // bounding rectangle QRect r = static_cast<QPicture *>(d->pdev)->boundingRect(); d->s << (qint32) r.left() << (qint32) r.top() << (qint32) r.width() << (qint32) r.height(); } d->s << (quint32) d->pic_d->trecs; // write number of records d->pic_d->pictb.seek(cs_start); QByteArray buf = d->pic_d->pictb.buffer(); quint16 cs = (quint16) qChecksum(buf.constData() + data_start, pos - data_start); d->s << cs; // write checksum d->pic_d->pictb.close(); setActive(false); return true; }
void Helpers::restoreBackup(QByteArray& data, const QDir& keystore) { QByteArray raw = qUncompress(data); QDataStream totalStream(&raw, QIODevice::ReadOnly); quint32 allSize; quint16 crc; totalStream >> allSize; totalStream >> crc; QByteArray all(allSize, '\0'); totalStream >> all; quint32 segmentSize; quint16 checkSum = qChecksum(all.data(), all.size()); // do CRC check if ( crc != checkSum ) { throw QString("CRC check mismatch: " + QString::number(crc) + " != " + QString::number(checkSum)); } else if ( !totalStream.atEnd() ) { throw QString("Unexpected data at end of restore stream"); } QByteArray testnetData; QDataStream allStream(&all, QIODevice::ReadOnly); allStream >> segmentSize; QByteArray settingsData(segmentSize, '\0'); allStream >> settingsData; allStream >> segmentSize; QByteArray addressData(segmentSize, '\0'); allStream >> addressData; if ( !allStream.atEnd() ) { // testnet addresses are present allStream >> segmentSize; testnetData = QByteArray(segmentSize, '\0'); allStream >> testnetData; }
QByteArray SimpleCrypt::encryptToByteArray(QByteArray plaintext) { if (m_keyParts.isEmpty()) { qWarning() << "No key set."; m_lastError = ErrorNoKeySet; return QByteArray(); } QByteArray ba = plaintext; CryptoFlags flags = CryptoFlagNone; if (m_compressionMode == CompressionAlways) { ba = qCompress(ba, 9); //maximum compression flags |= CryptoFlagCompression; } else if (m_compressionMode == CompressionAuto) { QByteArray compressed = qCompress(ba, 9); if (compressed.count() < ba.count()) { ba = compressed; flags |= CryptoFlagCompression; } } QByteArray integrityProtection; if (m_protectionMode == ProtectionChecksum) { flags |= CryptoFlagChecksum; QDataStream s(&integrityProtection, QIODevice::WriteOnly); s << qChecksum(ba.constData(), ba.length()); } else if (m_protectionMode == ProtectionHash) { flags |= CryptoFlagHash; QCryptographicHash hash(QCryptographicHash::Sha1); hash.addData(ba); integrityProtection += hash.result(); } //prepend a random char to the string char randomChar = char(qrand() & 0xFF); ba = randomChar + integrityProtection + ba; int pos(0); char lastChar(0); int cnt = ba.count(); while (pos < cnt) { ba[pos] = ba.at(pos) ^ m_keyParts.at(pos % 8) ^ lastChar; lastChar = ba.at(pos); ++pos; } QByteArray resultArray; resultArray.append(char(0x03)); //version for future updates to algorithm resultArray.append(char(flags)); //encryption flags resultArray.append(ba); m_lastError = ErrorNoError; return resultArray; }
QString QxtConfirmationMessagePrivate::key() const { QString value = overrideKey; if (value.isEmpty()) { const QString all = qxt_p().windowTitle() + qxt_p().text() + qxt_p().informativeText(); const QByteArray data = all.toLocal8Bit(); value = QString::number(qChecksum(data.constData(), data.length())); } return value; }
QByteArray SimpleCrypt::decryptToByteArray(QByteArray cypher) { if (m_keyParts.isEmpty()) { qWarning() << "No key set."; m_lastError = ErrorNoKeySet; return QByteArray(); } if (!cypher.length()) { m_lastError = ErrorUnknownVersion; return QByteArray(); } QByteArray ba = cypher; char version = ba.at(0); if (version !=3) { //we only work with version 3 m_lastError = ErrorUnknownVersion; qWarning() << "Invalid version or not a cyphertext."; return QByteArray(); } CryptoFlags flags = CryptoFlags(ba.at(1)); ba = ba.mid(2); int pos(0); int cnt(ba.count()); char lastChar = 0; while (pos < cnt) { char currentChar = ba[pos]; ba[pos] = ba.at(pos) ^ lastChar ^ m_keyParts.at(pos % 8); lastChar = currentChar; ++pos; } ba = ba.mid(1); //chop off the random number at the start bool integrityOk(true); if (flags.testFlag(CryptoFlagChecksum)) { if (ba.length() < 2) { m_lastError = ErrorIntegrityFailed; return QByteArray(); } quint16 storedChecksum; { QDataStream s(&ba, QIODevice::ReadOnly); s >> storedChecksum; } ba = ba.mid(2); quint16 checksum = qChecksum(ba.constData(), ba.length()); integrityOk = (checksum == storedChecksum); } else if (flags.testFlag(CryptoFlagHash)) {
void pAnswer::calculateChecksum(char* _data) { union checksum{ short int dig; char str[2]; }; checksum chksm; chksm.dig = qChecksum(_data + 2, get_size(_data) + 16 - 2); for(int i = 0; i < 2; i++) _data[i] = chksm.str[i]; }
bool Editor::open ( bool silentMode ) { bool ret = m_textEdit->open ( silentMode, m_filename, m_lastModified ); QByteArray array ( m_textEdit->toPlainText().toLocal8Bit() ); if ( array.count() ) { char *ptr = array.data(); quint16 check = qChecksum ( ptr, array.length() ); m_checksum = check; } m_timerCheckLastModified.start ( 5000 ); return ret; }
QString GetThumbnailFilename(QString url, QString title) { QString fileprefix = GetConfDir(); QDir dir(fileprefix); if (!dir.exists()) dir.mkdir(fileprefix); fileprefix += "/cache/netvision-thumbcache"; dir = QDir(fileprefix); if (!dir.exists()) dir.mkdir(fileprefix); QString sFilename = QString("%1/%2_%3") .arg(fileprefix) .arg(qChecksum(url.toLocal8Bit().constData(), url.toLocal8Bit().size())) .arg(qChecksum(title.toLocal8Bit().constData(), title.toLocal8Bit().size())); return sFilename; }
void DLockPacket::updateCrc() { // setting back to 0 first char empty = 0x00; packet.replace(1, 1, &empty, 1); packet.replace(2, 1, &empty, 1); // then generating the crc quint16 crc = qChecksum(packet.constData(), packet.length()); char low = crc; char high = (crc >> 8) & 0xff; // finally updating it packet.replace(1, 1, &high, 1); packet.replace(2, 1, &low, 1); }
void tst_BaselineExample::testDataDrivenChecksum_data() { QTest::addColumn<QString>("label"); const int numItems = 5; const char *tags[numItems] = {"short", "long", "empty", "signs", "html"}; const char *labels[numItems] = {"Ok!", "A really long button text that just does not seem to end", "", "!@#$%^&*()_", "<b>BOLD</b>"}; for (int i = 0; i<numItems; i++) { quint16 checksum = qChecksum(labels[i], qstrlen(labels[i])); QBaselineTest::newRow(tags[i], checksum) << labels[i]; } }
void MTestThread::newBinMsgFromClient(QByteArray in) { ++m_count; QString zz; zz.append(in); QByteArray zz1; for ( int i=0; i<60; ++i ) { // 60000 zz1.append("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\r\n"); } if ( in.size()!=zz.size() ) { ++m_err; qlDebug() << "Size incorrect!" << in.size() << zz.size() << m_err; m_netManager->sendBinaryMessage(zz1, m_packetN++); } else { m_netManager->sendBinaryMessage(in, m_packetN++); } if ( in != zz1) { ++m_err; qlDebug() << "Packet incorrect!" << qChecksum(in.constData(),in.size()) << qChecksum(zz1.constData(),zz1.size()) << m_err; } }
void CNetRender::ReceiveData(QTcpSocket *socket, sMessage *msg) { QDataStream socketReadStream(socket); qint64 bytesAvailable = socket->bytesAvailable(); while(bytesAvailable > 0) { if (msg->command == netRender_NONE) { if (socket->bytesAvailable() < (qint64)((sizeof(msg->command) + sizeof(msg->id) + sizeof(msg->size)))) { return; } // meta data available socketReadStream >> msg->command; socketReadStream >> msg->id; socketReadStream >> msg->size; } bytesAvailable = socket->bytesAvailable(); if(msg->size > 0) { if (bytesAvailable < (qint64)(sizeof(quint16) + msg->size)) { return; } // full payload available char* buffer = new char[msg->size]; socketReadStream.readRawData(buffer, msg->size); msg->payload.append(buffer, msg->size); quint16 crcCalculated = qChecksum(buffer, msg->size); quint16 crcReceived; socketReadStream >> crcReceived; delete[] buffer; if(crcCalculated != crcReceived) { //ResetMessage(msg); //socketReadStream.atEnd(); //socketReadStream.skipRawData(socket->bytesAvailable()); return; } msg->size = msg->payload.size(); } ProcessData(socket, msg); bytesAvailable = socket->bytesAvailable(); } }
quint16 OS2LPlugin::getHash(QString channel) { quint16 hash; if (m_hashMap.contains(channel)) hash = m_hashMap[channel]; else { /** No existing hash found. Add a new key to the table */ hash = qChecksum(channel.toUtf8().data(), channel.length()); m_hashMap[channel] = hash; } return hash; }
void SatelliteSingleSend::connected() { SATELLITE_PKGINFO_HEADER_TYPE header( SATELLITE_PKGINFO_MAGIC_VALUE ); header <<= 32; header |= mMessage.size(); header = qToBigEndian( header ); SATELLITE_PKGINFO_CHECKSUM_TYPE checksum( qChecksum( mMessage.constData(), mMessage.size() ) ); checksum = qToBigEndian( checksum ); mpServerConnection->write( (char*)(&header), SATELLITE_PKGINFO_HEADER_SIZE ); mpServerConnection->write( mMessage ); mpServerConnection->write( (char*)(&checksum), SATELLITE_PKGINFO_CHECKSUM_SIZE ); mpServerConnection->flush(); mpServerConnection->disconnectFromHost(); }
bool QPicture::play( QPainter *painter ) { if ( pictb.size() == 0 ) // nothing recorded return TRUE; pictb.open( IO_ReadOnly ); // open buffer device QDataStream s; s.setDevice( &pictb ); // attach data stream to buffer if ( !formatOk ) { // first time we read it char mf_id[4]; // picture header tag s.readRawBytes( mf_id, 4 ); // read actual tag if ( memcmp(mf_id, mfhdr_tag, 4) != 0 ) { // wrong header id #if defined(CHECK_RANGE) qWarning( "QPicture::play: Incorrect header" ); #endif pictb.close(); return FALSE; } int cs_start = sizeof(Q_UINT32); // pos of checksum word int data_start = cs_start + sizeof(Q_UINT16); Q_UINT16 cs,ccs; QByteArray buf = pictb.buffer(); // pointer to data s >> cs; // read checksum ccs = qChecksum( buf.data() + data_start, buf.size() - data_start ); if ( ccs != cs ) { #if defined(CHECK_STATE) qWarning( "QPicture::play: Invalid checksum %x, %x expected", ccs, cs ); #endif pictb.close(); return FALSE; } Q_UINT16 major, minor; s >> major >> minor; // read version number if ( major > mfhdr_maj ) { // new, incompatible version #if defined(CHECK_RANGE) qWarning( "QPicture::play: Incompatible version %d.%d", major, minor); #endif pictb.close(); return FALSE; } formatOk = TRUE; // picture seems to be ok formatMajor = major; formatMinor = minor; } else {
// FIXME hardcoded to 16 sender hashes quint8 UiStyle::StyledMessage::senderHash() const { if(_senderHash != 0xff) return _senderHash; QString nick = nickFromMask(sender()).toLower(); if(!nick.isEmpty()) { int chopCount = 0; while(chopCount < nick.size() && nick.at(nick.count() - 1 - chopCount) == '_') chopCount++; if(chopCount < nick.size()) nick.chop(chopCount); } quint16 hash = qChecksum(nick.toAscii().data(), nick.toAscii().size()); return (_senderHash = (hash & 0xf) + 1); }
bool pAnswer::verifyChecksum(char* _data) { union checksum{ short int dig; char str[2]; }; checksum chksm; short int tdig = qChecksum(_data + 2, get_size(_data) + 16 - 2); for(int i = 0; i < 2; i++) chksm.str[i] = _data[i]; if(tdig == chksm.dig) return true; return false; }
quint16 BodyMeasure::getFingerprint() const { quint64 x = 0; x += 1000.0 * weightkg; x += 1000.0 * fatkg; x += 1000.0 * musclekg; x += 1000.0 * boneskg; x += 1000.0 * leankg; x += 1000.0 * fatpercent; QByteArray ba = QByteArray::number(x); return qChecksum(ba, ba.length()) + Measure::getFingerprint(); }
void QCOMM::timerEvent(QTimerEvent *event) { if(event->timerId()==timerid){ HEARTBEAT heart; heart.header.sizeofpack = sizeof heart; heart.header.magic[0] ='M'; heart.header.magic[1] ='D'; heart.header.seq = heartbeatnum++; heart.customerID = 0xaa; heart.fileterminal = hasterminalfile(); heart.licensetimeout = islicensetimeout(); heart.netinvade = isnetinvade(); heart.numofpack = numberofudppack; heart.header.crc = qChecksum((char *)(&heart+sizeof (heart.header)),sizeof heart-sizeof (heart.header)); udpsend((char*)&heart,sizeof(heart)); } }