void DiscoverClient::discover(quint16 port, const QByteArray &negotiation_string) { m_socket = new QUdpSocket(this); m_port = port; m_negotiation_string = negotiation_string.leftJustified(128, char(0), true); connect(m_socket, &QUdpSocket::readyRead, this, &DiscoverClient::readyRead); updateListEndpoints(); QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &DiscoverClient::write); timer->start(100); }
void tst_QByteArray::leftJustified() { QByteArray a; a = "ABC"; QCOMPARE(a.leftJustified(5,'-'), QByteArray("ABC--")); QCOMPARE(a.leftJustified(4,'-'), QByteArray("ABC-")); QCOMPARE(a.leftJustified(4), QByteArray("ABC ")); QCOMPARE(a.leftJustified(3), QByteArray("ABC")); QCOMPARE(a.leftJustified(2), QByteArray("ABC")); QCOMPARE(a.leftJustified(1), QByteArray("ABC")); QCOMPARE(a.leftJustified(0), QByteArray("ABC")); QByteArray n; QVERIFY(!n.leftJustified(3).isNull()); // I expected true QCOMPARE(a.leftJustified(4,' ',true), QByteArray("ABC ")); QCOMPARE(a.leftJustified(3,' ',true), QByteArray("ABC")); QCOMPARE(a.leftJustified(2,' ',true), QByteArray("AB")); QCOMPARE(a.leftJustified(1,' ',true), QByteArray("A")); QCOMPARE(a.leftJustified(0,' ',true), QByteArray("")); }
// This function goes through the entire byte array // and tries to see whether this is a valid UTF-8 sequence. // If it's valid, this is probably a UTF-8 string. bool HTMLEncodingResolver::IsValidUtf8(const QByteArray &string) { // This is an implementation of the Perl code written here: // http://www.w3.org/International/questions/qa-forms-utf-8 // // Basically, UTF-8 has a very specific byte-pattern. This function // checks if the sent byte-sequence conforms to this pattern. // If it does, chances are *very* high that this is UTF-8. // // This function is written to be fast, not pretty. if (string.isNull()) { return false; } int index = 0; while (index < string.size()) { QByteArray dword = string.mid(index, 4); if (dword.size() < 4) { dword = dword.leftJustified(4, '\0'); } const unsigned char *bytes = (const unsigned char *) dword.constData(); // ASCII if (bytes[0] == 0x09 || bytes[0] == 0x0A || bytes[0] == 0x0D || (0x20 <= bytes[0] && bytes[0] <= 0x7E) ) { index += 1; } // non-overlong 2-byte else if ((0xC2 <= bytes[0] && bytes[0] <= 0xDF) && (0x80 <= bytes[1] && bytes[1] <= 0xBF) ) { index += 2; } else if ((bytes[0] == 0xE0 && // excluding overlongs (0xA0 <= bytes[1] && bytes[1] <= 0xBF) && (0x80 <= bytes[2] && bytes[2] <= 0xBF)) || (((0xE1 <= bytes[0] && bytes[0] <= 0xEC) || // straight 3-byte bytes[0] == 0xEE || bytes[0] == 0xEF) && (0x80 <= bytes[1] && bytes[1] <= 0xBF) && (0x80 <= bytes[2] && bytes[2] <= 0xBF)) || (bytes[0] == 0xED && // excluding surrogates (0x80 <= bytes[1] && bytes[1] <= 0x9F) && (0x80 <= bytes[2] && bytes[2] <= 0xBF)) ) { index += 3; } else if ((bytes[0] == 0xF0 && // planes 1-3 (0x90 <= bytes[1] && bytes[1] <= 0xBF) && (0x80 <= bytes[2] && bytes[2] <= 0xBF) && (0x80 <= bytes[3] && bytes[3] <= 0xBF)) || ((0xF1 <= bytes[0] && bytes[0] <= 0xF3) && // planes 4-15 (0x80 <= bytes[1] && bytes[1] <= 0xBF) && (0x80 <= bytes[2] && bytes[2] <= 0xBF) && (0x80 <= bytes[3] && bytes[3] <= 0xBF)) || (bytes[0] == 0xF4 && // plane 16 (0x80 <= bytes[1] && bytes[1] <= 0x8F) && (0x80 <= bytes[2] && bytes[2] <= 0xBF) && (0x80 <= bytes[3] && bytes[3] <= 0xBF)) ) { index += 4; } else { return false; } } return true; }
/*! Cabrillo export */ void Log::exportCabrillo(QFile *cbrFile,QString call,QString snt_exch1,QString snt_exch2,QString snt_exch3,QString snt_exch4) const { QSqlQueryModel m; #if QT_VERSION < 0x050000 m.setQuery("SELECT * FROM log where valid='true'", db); #else m.setQuery("SELECT * FROM log where valid=1", db); #endif while (m.canFetchMore()) { m.fetchMore(); } if (m.rowCount() == 0) return; // nothing to do // determine max field widths for sent/received data int sfw[MAX_EXCH_FIELDS], rfw[MAX_EXCH_FIELDS]; for (int i = 0; i < MAX_EXCH_FIELDS; i++) { sfw[i] = 0.; rfw[i] = 0; } QByteArray snt[MAX_EXCH_FIELDS]; for (int i = 0; i < m.rowCount(); i++) { // skip qsos marked as invalid if (!m.record(i).value(SQL_COL_VALID).toBool()) continue; // for sent exchange, if log field is empty use config file value snt[0]=m.record(i).value(SQL_COL_SNT1).toByteArray(); if (snt[0].isEmpty()) snt[0]=snt_exch1.toLatin1(); int j = snt[0].size(); if (j > sfw[0]) sfw[0] = j; snt[1]=m.record(i).value(SQL_COL_SNT2).toByteArray(); if (snt[1].isEmpty()) snt[1]=snt_exch2.toLatin1(); j = snt[1].size(); if (j > sfw[1]) sfw[1] = j; snt[2]=m.record(i).value(SQL_COL_SNT3).toByteArray(); if (snt[2].isEmpty()) snt[2]=snt_exch3.toLatin1(); j = snt[2].size(); if (j > sfw[2]) sfw[2] = j; snt[3]=m.record(i).value(SQL_COL_SNT4).toByteArray(); if (snt[3].isEmpty()) snt[3]=snt_exch4.toLatin1(); j = snt[3].size(); if (j > sfw[3]) sfw[3] = j; j = m.record(i).value(SQL_COL_RCV1).toByteArray().size(); if (j > rfw[0]) rfw[0] = j; j = m.record(i).value(SQL_COL_RCV2).toByteArray().size(); if (j > rfw[1]) rfw[1] = j; j = m.record(i).value(SQL_COL_RCV3).toByteArray().size(); if (j > rfw[2]) rfw[2] = j; j = m.record(i).value(SQL_COL_RCV4).toByteArray().size(); } for (int i = 0; i < m.rowCount(); i++) { // skip qsos marked as invalid if (!m.record(i).value(SQL_COL_VALID).toBool()) continue; // for sent exchange, if log field is empty use config file value snt[0]=m.record(i).value(SQL_COL_SNT1).toByteArray(); if (snt[0].isEmpty()) snt[0]=snt_exch1.toLatin1(); snt[1]=m.record(i).value(SQL_COL_SNT2).toByteArray(); if (snt[1].isEmpty()) snt[1]=snt_exch2.toLatin1(); snt[2]=m.record(i).value(SQL_COL_SNT3).toByteArray(); if (snt[2].isEmpty()) snt[2]=snt_exch3.toLatin1(); snt[3]=m.record(i).value(SQL_COL_SNT4).toByteArray(); if (snt[3].isEmpty()) snt[3]=snt_exch4.toLatin1(); QString tmp; tmp = "QSO: "; QString tmp2 = QString::number(qRound(m.record(i).value(SQL_COL_FREQ).toDouble() / 1000.0)); for (int j = 0; j < (5 - tmp2.size()); j++) { tmp = tmp + " "; } tmp = tmp + tmp2; switch (m.record(i).value(SQL_COL_MODE).toInt()) { case RIG_MODE_CW: case RIG_MODE_CWR: tmp = tmp + " CW "; break; case RIG_MODE_USB: case RIG_MODE_LSB: case RIG_MODE_AM: case RIG_MODE_FM: tmp = tmp + " PH "; break; case RIG_MODE_RTTY: case RIG_MODE_RTTYR: tmp = tmp + " RY "; break; default: tmp = tmp + " CW "; break; } // in SQL log, date is of format MMddyyyy tmp2 = m.record(i).value(SQL_COL_DATE).toByteArray(); tmp = tmp + tmp2.right(4) + "-" + tmp2.left(2) + "-" + tmp2.mid(2, 2); tmp = tmp + " " + m.record(i).value(SQL_COL_TIME).toByteArray(); tmp = tmp + " " + call; int n = 11 - call.size(); for (int j = 0; j < n; j++) tmp.append(" "); for (int j = 0; j < nExchange; j++) { QByteArray s; s.clear(); switch (j) { case 0: s=snt[0]; s = s.leftJustified(sfw[0], ' '); break; case 1: s=snt[1]; s = s.leftJustified(sfw[1], ' '); break; case 2: s=snt[2]; s = s.leftJustified(sfw[2], ' '); break; case 3: s=snt[3]; s = s.leftJustified(sfw[3], ' '); break; } tmp = tmp + s + " "; } tmp = tmp + m.record(i).value(SQL_COL_CALL).toByteArray(); n = 11 - m.record(i).value(SQL_COL_CALL).toByteArray().size(); for (int j = 0; j < n; j++) tmp.append(" "); for (int j = 0; j < nExchange; j++) { switch (j) { case 0: tmp = tmp + m.record(i).value(SQL_COL_RCV1).toByteArray().leftJustified(rfw[0], ' '); break; case 1: tmp = tmp + m.record(i).value(SQL_COL_RCV2).toByteArray().leftJustified(rfw[1], ' '); break; case 2: tmp = tmp + m.record(i).value(SQL_COL_RCV3).toByteArray().leftJustified(rfw[2], ' '); break; case 3: tmp = tmp + m.record(i).value(SQL_COL_RCV4).toByteArray().leftJustified(rfw[3], ' '); break; } tmp = tmp + " "; } tmp = tmp + "\n"; cbrFile->write(tmp.toLatin1()); } cbrFile->write("END-OF-LOG:\n"); cbrFile->close(); }
QByteArray Dispatcher::fixedCIDLength(QByteArray CID) { return CID.leftJustified(24, (char)0x00, true); }
QByteArray Cipherer::process(const QByteArray& data, const QByteArray& keyData, Direction direction, int cipherID) { if (data.isEmpty()) { return QByteArray(); } if (keyData.isEmpty()) { return QByteArray(); } if (!avaliableCipherTypes.contains(cipherID)) { return QByteArray(); } QByteArray initVectorData ("aes128-cbc-pkcs7", 16); QByteArray formalizedKey = keyData.leftJustified(16, '\0', true); unsigned char aesKey[16]; memset(aesKey, 0, 16); memcpy(aesKey, formalizedKey.data(), 16); const unsigned char* inputData = (const unsigned char*)data.data(); size_t inputLength = data.length(); QByteArray resultArray; EVP_CIPHER_CTX *ctx; if (direction == Direction::Encode) { const size_t approximateEncodedDataLength = ((inputLength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE; unsigned char* encodedData = new unsigned char[approximateEncodedDataLength]; memset(encodedData, 0, approximateEncodedDataLength); int len; int actualEncodedDataLength; /* Create and initialise the context */ if(!(ctx = EVP_CIPHER_CTX_new())) { delete[] encodedData; return QByteArray(); } if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, aesKey, (uchar*)initVectorData.data())) { delete[] encodedData; return QByteArray(); } if(1 != EVP_EncryptUpdate(ctx, encodedData, &len, inputData, inputLength)) { delete[] encodedData; return QByteArray(); } actualEncodedDataLength = len; if(1 != EVP_EncryptFinal_ex(ctx, encodedData + len, &len)) { delete[] encodedData; return QByteArray(); } actualEncodedDataLength += len; EVP_CIPHER_CTX_free(ctx); resultArray = QByteArray((const char*)encodedData, actualEncodedDataLength); delete[] encodedData; } else { unsigned char* decodedData = new unsigned char[inputLength]; memset(decodedData, 0, inputLength); int len; int actualDecodedDataLength; if(!(ctx = EVP_CIPHER_CTX_new())) { delete[] decodedData; return QByteArray(); } if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, aesKey, (uchar*)initVectorData.data())) { delete[] decodedData; return QByteArray(); } if(1 != EVP_DecryptUpdate(ctx, decodedData, &len, inputData, inputLength)) { delete[] decodedData; return QByteArray(); } actualDecodedDataLength = len; if(1 != EVP_DecryptFinal_ex(ctx, decodedData + len, &len)) { delete[] decodedData; return QByteArray(); } actualDecodedDataLength += len; EVP_CIPHER_CTX_free(ctx); resultArray = QByteArray((const char*)decodedData, actualDecodedDataLength); delete[] decodedData; } return resultArray; }