void compareMetaData(QByteArray data, const QString &expected, int otherFlags = 0) { QString decoded; // needs to be in in one map, with the entry called "v" data = "\xa1\x61v" + data; { CborParser parser; CborValue first; CborError err = cbor_parser_init(reinterpret_cast<const quint8 *>(data.constData()), data.length(), 0, &parser, &first); QVERIFY2(!err, QByteArrayLiteral(": Got error \"") + cbor_error_string(err) + "\""); err = parseOne(&first, &decoded, CborConvertAddMetadata | otherFlags); QVERIFY2(!err, QByteArrayLiteral(": Got error \"") + cbor_error_string(err) + "\"; decoded stream:\n" + decoded.toLatin1()); // check that we consumed everything QCOMPARE((void*)first.ptr, (void*)data.constEnd()); } QVERIFY(decoded.startsWith("{\"v\":")); QVERIFY(decoded.endsWith('}')); // qDebug() << "was" << decoded; // extract just the metadata static const char needle[] = "\"v$cbor\":{"; int pos = decoded.indexOf(needle); QCOMPARE(pos == -1, expected.isEmpty()); if (pos != -1) { decoded.chop(2); decoded = std::move(decoded).mid(pos + strlen(needle)); QCOMPARE(decoded, expected); } }
QByteArray QCSP::reverse( const QByteArray &data ) const { QByteArray reverse; for( QByteArray::const_iterator i = data.constEnd(); i != data.constBegin(); ) { --i; reverse += *i; } return reverse; }
void CPU::loadRom(const std::string& h, const std::string& g, const std::string& f, const std::string& e) { QFile file_h(QString::fromStdString(h)); if(!file_h.open(QIODevice::ReadOnly)) { error("Cannot open rom 1 : " + file_h.errorString().toStdString() + " : " + std::to_string(file_h.error())); } QFile file_g(QString::fromStdString(g)); if(!file_g.open(QIODevice::ReadOnly)) { error("Cannot open rom 2 : " + file_g.errorString().toStdString() + " : " + std::to_string(file_h.error())); } QFile file_f(QString::fromStdString(f)); if(!file_f.open(QIODevice::ReadOnly)) { error("Cannot open rom 3 : " + file_f.errorString().toStdString() + " : " + std::to_string(file_h.error())); } QFile file_e(QString::fromStdString(e)); if(!file_e.open(QIODevice::ReadOnly)) { error("Cannot open rom 4 : " + file_e.errorString().toStdString() + " : " + std::to_string(file_h.error())); } QByteArray hContents = file_h.readAll(); std::copy(hContents.constBegin(), hContents.constEnd(), m_state.mem.begin()); QByteArray gContents = file_g.readAll(); std::copy(gContents.constBegin(), gContents.constEnd(), m_state.mem.begin() + 0x800); QByteArray fContents = file_f.readAll(); std::copy(fContents.constBegin(), fContents.constEnd(), m_state.mem.begin() + 0x1000); QByteArray eContents = file_e.readAll(); std::copy(eContents.constBegin(), eContents.constEnd(), m_state.mem.begin() + 0x1800); }
QByteArray QCSP::decrypt( const QByteArray &data ) { HCRYPTKEY key = 0; if( !CryptGetUserKey( d->h, AT_KEYEXCHANGE, &key ) ) return QByteArray(); QByteArray reverse; for( QByteArray::const_iterator i = data.constEnd(); i != data.constBegin(); ) { --i; reverse += *i; } DWORD size = reverse.size(); bool result = CryptDecrypt( key, 0, true, 0, (BYTE*)reverse.data(), &size ); CryptDestroyKey( key ); return result ? reverse : QByteArray(); }
void compareOne_real(const QByteArray &data, const QString &expected, int flags, int line) { compareFailed = true; CborParser parser; CborValue first; CborError err = cbor_parser_init(reinterpret_cast<const quint8 *>(data.constData()), data.length(), 0, &parser, &first); QVERIFY2(!err, QByteArray::number(line) + ": Got error \"" + cbor_error_string(err) + "\""); QString decoded; err = parseOne(&first, &decoded, flags); QVERIFY2(!err, QByteArray::number(line) + ": Got error \"" + cbor_error_string(err) + "\"; decoded stream:\n" + decoded.toLatin1()); QCOMPARE(decoded, expected); // check that we consumed everything QCOMPARE((void*)first.ptr, (void*)data.constEnd()); compareFailed = false; }
QByteArray QCSP::sign( int method, const QByteArray &digest ) { ALG_ID alg = 0; switch( method ) { case NID_sha1: alg = CALG_SHA1; break; case NID_sha256: alg = CALG_SHA_256; break; case NID_sha384: alg = CALG_SHA_384; break; case NID_sha512: alg = CALG_SHA_512; break; case NID_sha224: default: return QByteArray(); } HCRYPTHASH hash = 0; if( !CryptCreateHash( d->h, alg, 0, 0, &hash ) ) return QByteArray(); if( !CryptSetHashParam( hash, HP_HASHVAL, (BYTE*)digest.constData(), 0 ) ) { CryptDestroyHash( hash ); return QByteArray(); } DWORD size = 256; QByteArray sig; sig.resize( size ); if( !CryptSignHashW( hash, AT_SIGNATURE, 0, 0, (BYTE*)sig.data(), &size ) ) sig.clear(); CryptDestroyHash( hash ); QByteArray reverse; for( QByteArray::const_iterator i = sig.constEnd(); i != sig.constBegin(); ) { --i; reverse += *i; } return reverse; }
QString CGI::encodeURL(const QString &rawText) { QByteArray utf = rawText.toUtf8(); QString enc; enc.reserve(utf.length()); // Make sure we at least have space for a normal US-ASCII URL QByteArray::const_iterator it = utf.constBegin(); while (it != utf.constEnd()) { const char ch = *it; if (('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ('0' <= ch && ch <= '9')) { enc.append(QLatin1Char(ch)); } else if (ch == ' ') { enc.append(QLatin1Char('+')); } else { switch (ch) { case '-': case '_': case '(': case ')': case '.': case '!': case '~': case '*': case '\'': enc.append(QLatin1Char(ch)); break; default: ushort c1 = (*it & 0xF0) >> 4; ushort c2 = (*it & 0x0F); enc.append(QLatin1Char('%')); enc.append(QLatin1Char(*(cgi_chars + c1))); enc.append(QLatin1Char(*(cgi_chars + c2))); break; } } ++it; } return enc; }
const char *MacroExpander::expand(const char *__first, const char *__last, QByteArray *__result) { const char *start = __first; __first = skip_blanks (__first, __last); lines = skip_blanks.lines; while (__first != __last) { if (*__first == '\n') { __result->append("\n# "); __result->append(QByteArray::number(env->currentLine)); __result->append(' '); __result->append('"'); __result->append(env->currentFile.toUtf8()); __result->append('"'); __result->append('\n'); ++lines; __first = skip_blanks (++__first, __last); lines += skip_blanks.lines; if (__first != __last && *__first == '#') break; } else if (*__first == '#') { __first = skip_blanks (++__first, __last); lines += skip_blanks.lines; const char *end_id = skip_identifier (__first, __last); const QByteArray fast_name(__first, end_id - __first); __first = end_id; if (const QByteArray *actual = resolve_formal (fast_name)) { __result->append('\"'); const char *actual_begin = actual->constData (); const char *actual_end = actual_begin + actual->size (); for (const char *it = skip_whitespaces (actual_begin, actual_end); it != actual_end; ++it) { if (*it == '"' || *it == '\\') { __result->append('\\'); __result->append(*it); } else if (*it == '\n') { __result->append('"'); __result->append('\n'); __result->append('"'); } else __result->append(*it); } __result->append('\"'); } else __result->append('#'); // ### warning message? } else if (*__first == '\"') { const char *next_pos = skip_string_literal (__first, __last); lines += skip_string_literal.lines; __result->append(__first, next_pos - __first); __first = next_pos; } else if (*__first == '\'') { const char *next_pos = skip_char_literal (__first, __last); lines += skip_char_literal.lines; __result->append(__first, next_pos - __first); __first = next_pos; } else if (comment_p (__first, __last)) { __first = skip_comment_or_divop (__first, __last); int n = skip_comment_or_divop.lines; lines += n; while (n-- > 0) __result->append('\n'); } else if (pp_isspace (*__first)) { for (; __first != __last; ++__first) { if (*__first == '\n' || !pp_isspace (*__first)) break; } __result->append(' '); } else if (pp_isdigit (*__first)) { const char *next_pos = skip_number (__first, __last); lines += skip_number.lines; __result->append(__first, next_pos - __first); __first = next_pos; } else if (pp_isalpha (*__first) || *__first == '_') { const char *name_begin = __first; const char *name_end = skip_identifier (__first, __last); __first = name_end; // advance // search for the paste token const char *next = skip_blanks (__first, __last); bool paste = false; if (next != __last && *next == '#') { paste = true; ++next; if (next != __last && *next == '#') __first = skip_blanks(++next, __last); } const QByteArray fast_name(name_begin, name_end - name_begin); if (const QByteArray *actual = resolve_formal (fast_name)) { const char *begin = actual->constData (); const char *end = begin + actual->size (); if (paste) { for (--end; end != begin - 1; --end) { if (! pp_isspace(*end)) break; } ++end; } __result->append(begin, end - begin); continue; } Macro *macro = env->resolve (fast_name); if (! macro || macro->isHidden() || env->hideNext) { if (fast_name.size () == 7 && fast_name [0] == 'd' && fast_name == "defined") env->hideNext = true; else env->hideNext = false; if (fast_name.size () == 8 && fast_name [0] == '_' && fast_name [1] == '_') { if (fast_name == "__LINE__") { __result->append(QByteArray::number(env->currentLine + lines)); continue; } else if (fast_name == "__FILE__") { __result->append('"'); __result->append(env->currentFile.toUtf8()); __result->append('"'); continue; } else if (fast_name == "__DATE__") { __result->append('"'); __result->append(QDate::currentDate().toString().toUtf8()); __result->append('"'); continue; } else if (fast_name == "__TIME__") { __result->append('"'); __result->append(QTime::currentTime().toString().toUtf8()); __result->append('"'); continue; } } __result->append(name_begin, name_end - name_begin); continue; } if (! macro->isFunctionLike()) { Macro *m = 0; if (! macro->definition().isEmpty()) { macro->setHidden(true); QByteArray __tmp; __tmp.reserve (256); MacroExpander expand_macro (env); expand_macro(macro->definition(), &__tmp); if (! __tmp.isEmpty ()) { const char *__tmp_begin = __tmp.constBegin(); const char *__tmp_end = __tmp.constEnd(); const char *__begin_id = skip_whitespaces (__tmp_begin, __tmp_end); const char *__end_id = skip_identifier (__begin_id, __tmp_end); if (__end_id == __tmp_end) { const QByteArray __id (__begin_id, __end_id - __begin_id); m = env->resolve (__id); } if (! m) *__result += __tmp; } macro->setHidden(false); } if (! m) continue; macro = m; } // function like macro const char *arg_it = skip_whitespaces (__first, __last); if (arg_it == __last || *arg_it != '(') { __result->append(name_begin, name_end - name_begin); lines += skip_whitespaces.lines; __first = arg_it; continue; } QVector<QByteArray> actuals; QVector<MacroArgumentReference> actuals_ref; actuals.reserve (5); ++arg_it; // skip '(' MacroExpander expand_actual (env, frame); const char *arg_end = skip_argument_variadics (actuals, macro, arg_it, __last); if (arg_it != arg_end) { actuals_ref.append(MacroArgumentReference(start_offset + (arg_it-start), arg_end - arg_it)); const QByteArray actual (arg_it, arg_end - arg_it); QByteArray expanded; expand_actual (actual.constBegin (), actual.constEnd (), &expanded); actuals.push_back (expanded); arg_it = arg_end; } while (arg_it != __last && *arg_end == ',') { ++arg_it; // skip ',' arg_end = skip_argument_variadics (actuals, macro, arg_it, __last); actuals_ref.append(MacroArgumentReference(start_offset + (arg_it-start), arg_end - arg_it)); const QByteArray actual (arg_it, arg_end - arg_it); QByteArray expanded; expand_actual (actual.constBegin (), actual.constEnd (), &expanded); actuals.push_back (expanded); arg_it = arg_end; } if (! (arg_it != __last && *arg_it == ')')) return __last; ++arg_it; // skip ')' __first = arg_it; pp_frame frame (macro, actuals); MacroExpander expand_macro (env, &frame); macro->setHidden(true); expand_macro (macro->definition(), __result); macro->setHidden(false); } else __result->append(*__first++); } return __first; }
int main(int argc, char *argv[]) { if (argc != 4) { std::cerr << "Usage: qpatch file.list oldQtDir newQtDir" << std::endl; return EXIT_FAILURE; } const QByteArray files = argv[1]; const QByteArray qtDirPath = argv[2]; const QByteArray newQtPath = argv[3]; if (qtDirPath.size() < newQtPath.size()) { std::cerr << "qpatch: error: newQtDir needs to be less than " << qtDirPath.size() << " characters." << std::endl; return EXIT_FAILURE; } QFile fn(QFile::decodeName(files)); if (! fn.open(QFile::ReadOnly)) { std::cerr << "qpatch: error: file not found" << std::endl; return EXIT_FAILURE; } QStringList filesToPatch, textFilesToPatch; bool readingTextFilesToPatch = false; // read the input file QTextStream in(&fn); forever { const QString line = in.readLine(); if (line.isNull()) break; else if (line.isEmpty()) continue; else if (line.startsWith(QLatin1String("%%"))) readingTextFilesToPatch = true; else if (readingTextFilesToPatch) textFilesToPatch.append(line); else filesToPatch.append(line); } foreach (QString fileName, filesToPatch) { QString prefix = QFile::decodeName(newQtPath); if (! prefix.endsWith(QLatin1Char('/'))) prefix += QLatin1Char('/'); fileName.prepend(prefix); QFile file(fileName); if (! file.open(QFile::ReadOnly)) { std::cerr << "qpatch: warning: file `" << qPrintable(fileName) << "' not found" << std::endl; continue; } const QByteArray source = file.readAll(); file.close(); int index = 0; if (! file.open(QFile::WriteOnly | QFile::Truncate)) { std::cerr << "qpatch: error: file `" << qPrintable(fileName) << "' not writable" << std::endl; continue; } std::cout << "patching file `" << qPrintable(fileName) << "'" << std::endl; forever { int start = source.indexOf(qtDirPath, index); if (start == -1) break; int endOfString = start; for (; endOfString < source.size(); ++endOfString) { if (! source.at(endOfString)) break; } ++endOfString; // include the '\0' if (index != start) file.write(source.constData() + index, start - index); int length = endOfString - start; QVector<char> s; for (const char *x = newQtPath.constData(); x != newQtPath.constEnd(); ++x) s.append(*x); const int qtDirPathLength = qtDirPath.size(); for (const char *x = source.constData() + start + qtDirPathLength; x != source.constData() + endOfString; ++x) s.append(*x); const int oldSize = s.size(); for (int i = oldSize; i < length; ++i) s.append('\0'); #if 0 std::cout << "replace string: " << source.mid(start, endOfString - start).constData() << " with: " << s.constData() << std::endl; #endif file.write(s.constData(), s.size()); index = endOfString; } if (index == 0) { std::cerr << "qpatch: warning: file `" << qPrintable(fileName) << "' didn't contain string to patch" << std::endl; } if (index != source.size()) file.write(source.constData() + index, source.size() - index); }