void JTEStyler::SaveTokenStart ( const TokenExtra& data ) { itsTokenStartCount++; if (itsTokenStartCount >= itsTokenStartFactor) { itsTokenStartCount = 0; itsTokenStartList->AppendElement(TokenData(itsTokenStart, data)); } }
void JTEStyler::UpdateStyles ( const JTextEditor* te, const JString& text, JRunArray<JTextEditor::Font>* styles, JIndexRange* recalcRange, JIndexRange* redrawRange, const JBoolean deletion, JArray<TokenData>* tokenStartList ) { if (!itsActiveFlag) { tokenStartList->RemoveAll(); return; } const JSize textLength = text.GetLength(); if (textLength == 0) { tokenStartList->RemoveAll(); return; } te->GetDefaultFont(&itsDefFontID, &itsFontSize, &itsDefFontStyle, &itsDefFontAlign); itsFontName = te->GetDefaultFontName(); TokenData tokenData; if (recalcRange->first == 1 && recalcRange->last >= text.GetLength()) { itsRedoAllFlag = kJTrue; itsCheckRange.Set(1, text.GetLength()); tokenStartList->RemoveAll(); tokenData = TokenData(1, GetFirstTokenExtraData()); tokenStartList->AppendElement(tokenData); styles->RemoveAll(); } else { itsRedoAllFlag = kJFalse; // calculate the range that needs to be checked JIndex firstIndex = recalcRange->first; JIndex lastIndex = recalcRange->last; if ((deletion && firstIndex > 1) || firstIndex > textLength) { // This fixes the case when the last character of the token is deleted. firstIndex--; // This fixes the case when the style run and the token both // end at the inserted text. (e.g. "x [ y ]" <- "x // y ]") lastIndex++; } if (lastIndex > textLength) { // We can't decrease recalcRange's end index, and we can't find // textLength+1 in *styles. lastIndex = textLength; } JIndex runIndex1, firstIndexInRun1; JBoolean ok = styles->FindRun(firstIndex, &runIndex1, &firstIndexInRun1); assert( ok ); JIndex runIndex2 = runIndex1; JIndex firstIndexInRun2 = firstIndexInRun1; ok = styles->FindRun(firstIndex, lastIndex, &runIndex2, &firstIndexInRun2); assert( ok ); run_assert(styles, lastIndex, runIndex2, firstIndexInRun2); itsCheckRange.Set(firstIndexInRun1, firstIndexInRun2 + styles->GetRunLength(runIndex2)-1); // let derived class expand the range JIndexRange savedRange = itsCheckRange; PreexpandCheckRange(text, *styles, *recalcRange, deletion, &itsCheckRange); assert( itsCheckRange.Contains(savedRange) && itsCheckRange.last <= styles->GetElementCount() ); // find nearest token in front of itsCheckRange if (tokenStartList->IsEmpty()) { tokenData = TokenData(1, GetFirstTokenExtraData()); tokenStartList->AppendElement(tokenData); } else { JBoolean foundTokenStart; TokenData target(itsCheckRange.first, TokenExtra()); JIndex tokenStartIndex = tokenStartList->SearchSorted1(target, JOrderedSetT::kAnyMatch, &foundTokenStart); if (!foundTokenStart) { tokenStartIndex--; // wants to insert -after- the value } tokenData = tokenStartList->GetElement(tokenStartIndex); // the rest of the token starts are invalid const JSize tokenStartCount = tokenStartList->GetElementCount(); if (tokenStartIndex < tokenStartCount) { tokenStartList->RemoveNextElements(tokenStartIndex+1, tokenStartCount - tokenStartIndex); } } // While typing in one place, it is much faster to back up from itsCheckRange // than to start from the top. itsTokenRunIndex = runIndex1; itsTokenFirstInRun = firstIndexInRun1; ok = styles->FindRun(firstIndex, tokenData.startIndex, &itsTokenRunIndex, &itsTokenFirstInRun); assert( ok ); run_assert(styles, tokenData.startIndex, itsTokenRunIndex, itsTokenFirstInRun); } // prepare to accumulate new token starts itsTokenStartList = tokenStartList; itsTokenStartCount = 0; itsTokenStart = tokenData.startIndex; // scan the text and adjust the styles std::istrstream input(text.GetCString(), text.GetLength()); JSeekg(input, itsTokenStart-1); itsTE = te; itsFontMgr = te->TEGetFontManager(); itsText = &text; itsStyles = styles; itsRecalcRange = recalcRange; itsRedrawRange = redrawRange; #if DEBUG_TIMING_INFO JStopWatch timer; timer.StartTimer(); #endif Scan(input, tokenData.data); #if DEBUG_TIMING_INFO timer.StopTimer(); cout << "JTEStyler: " << timer.PrintTimeInterval() << endl; #endif itsTE = NULL; itsFontMgr = NULL; itsText = NULL; itsStyles = NULL; itsRecalcRange = NULL; itsRedrawRange = NULL; itsTokenStartList = NULL; }
QList<TokenData> QCNG::tokens() const { qWarning() << "Start enumerationg providers"; QHash<SslCertificate,QCNGCache> cache; DWORD count = 0; NCryptProviderName *names = nullptr; NCryptEnumStorageProviders( &count, &names, NCRYPT_SILENT_FLAG ); for( DWORD i = 0; i < count; ++i ) { qWarning() << "Found provider" << QString::fromWCharArray(names[i].pszName); if( wcscmp( names[i].pszName, MS_SMART_CARD_KEY_STORAGE_PROVIDER ) == 0 ) { for( const QString &reader: QPCSC::instance().readers() ) { qWarning() << reader; QString scope = QString(R"(\\.\%1\)").arg(reader); d->enumKeys( cache, names[i].pszName, LPCWSTR(scope.utf16()) ); } } else d->enumKeys( cache, names[i].pszName ); } NCryptFreeBuffer( names ); d->cache = cache; qWarning() << "End enumerationg providers"; QList<TokenData> result; for(QHash<SslCertificate,QCNGCache>::const_iterator i = cache.constBegin(); i != cache.constEnd(); ++i) { TokenData t; t.setCard(i.key().type() & SslCertificate::EstEidType || i.key().type() & SslCertificate::DigiIDType ? i.value().guid : i.key().subjectInfo(QSslCertificate::CommonName)); t.setCert(i.key()); result << t; } return result; } TokenData QCNG::selectCert( const SslCertificate &cert ) { qWarning() << "Select:" << cert.subjectInfo( "CN" ); if( !d->cache.contains( cert ) ) return TokenData(); d->selected = d->cache[cert]; qWarning() << "Found:" << d->selected.guid << d->selected.key; TokenData t; t.setCard( cert.type() & SslCertificate::EstEidType || cert.type() & SslCertificate::DigiIDType ? d->selected.guid : cert.subjectInfo( QSslCertificate::CommonName ) ); t.setCert( cert ); return t; } QByteArray QCNG::sign( int method, const QByteArray &digest ) const { d->err = PinUnknown; BCRYPT_PKCS1_PADDING_INFO padInfo = { NCRYPT_SHA256_ALGORITHM }; switch( method ) { case NID_sha224: padInfo.pszAlgId = L"SHA224"; break; case NID_sha256: padInfo.pszAlgId = NCRYPT_SHA256_ALGORITHM; break; case NID_sha384: padInfo.pszAlgId = NCRYPT_SHA384_ALGORITHM; break; case NID_sha512: padInfo.pszAlgId = NCRYPT_SHA512_ALGORITHM; break; case NID_md5_sha1: //padInfo.pszAlgId = L"SHAMD5"; break; default: break; } DWORD size = 0; QByteArray res; NCRYPT_KEY_HANDLE k = d->key(); QString algo(5, 0); SECURITY_STATUS err = NCryptGetProperty(k, NCRYPT_ALGORITHM_GROUP_PROPERTY, PBYTE(algo.data()), DWORD((algo.size() + 1) * 2), &size, 0); algo.resize(size/2 - 1); bool isRSA = algo == "RSA"; err = NCryptSignHash(k, isRSA ? &padInfo : nullptr, PBYTE(digest.constData()), DWORD(digest.size()), nullptr, 0, &size, isRSA ? BCRYPT_PAD_PKCS1 : 0); if(FAILED(err)) return res; res.resize(int(size)); err = NCryptSignHash(k, isRSA ? &padInfo : nullptr, PBYTE(digest.constData()), DWORD(digest.size()), PBYTE(res.data()), DWORD(res.size()), &size, isRSA ? BCRYPT_PAD_PKCS1 : 0); NCryptFreeObject( k ); switch( err ) { case ERROR_SUCCESS: d->err = PinOK; return res; case SCARD_W_CANCELLED_BY_USER: d->err = PinCanceled; break; default: res.clear(); break; } return res; }