// Mutates Data in place, returns new size. size_t MutationDispatcher::MutateImpl(uint8_t *Data, size_t Size, size_t MaxSize, const std::vector<Mutator> &Mutators) { assert(MaxSize > 0); if (Size == 0) { for (size_t i = 0; i < MaxSize; i++) Data[i] = RandCh(Rand); if (Options.OnlyASCII) ToASCII(Data, MaxSize); return MaxSize; } assert(Size > 0); // Some mutations may fail (e.g. can't insert more bytes if Size == MaxSize), // in which case they will return 0. // Try several times before returning un-mutated data. for (int Iter = 0; Iter < 100; Iter++) { auto M = Mutators[Rand(Mutators.size())]; size_t NewSize = (this->*(M.Fn))(Data, Size, MaxSize); if (NewSize && NewSize <= MaxSize) { if (Options.OnlyASCII) ToASCII(Data, NewSize); CurrentMutatorSequence.push_back(M); return NewSize; } } return std::min(Size, MaxSize); }
/* * .KB_C_FN_DEFINITION_START * void DebugPrintValue(int value) * This global function displays the decimal value specified. * .KB_C_FN_DEFINITION_END */ void DebugPrintValue(int value) { char dValue[16], *cPtr; int modValue, nextDigit, leading; cPtr = dValue; if (value < 0) { *cPtr++ = '-'; value = -value; } modValue = 1000000000; leading = 1; while (modValue) { nextDigit = value / modValue; if (nextDigit) { *cPtr++ = ToASCII(nextDigit); leading = 0; } else if (!leading) { *cPtr++ = '0'; } value %= modValue; modValue /= 10; } *cPtr = 0; DebugPrint(dValue); }
void Fuzzer::ShuffleAndMinimize() { bool PreferSmall = (Options.PreferSmallDuringInitialShuffle == 1 || (Options.PreferSmallDuringInitialShuffle == -1 && USF.GetRand().RandBool())); if (Options.Verbosity) Printf("PreferSmall: %d\n", PreferSmall); PrintStats("READ "); std::vector<Unit> NewCorpus; if (Options.ShuffleAtStartUp) { std::random_shuffle(Corpus.begin(), Corpus.end(), USF.GetRand()); if (PreferSmall) std::stable_sort( Corpus.begin(), Corpus.end(), [](const Unit &A, const Unit &B) { return A.size() < B.size(); }); } Unit &U = CurrentUnit; for (const auto &C : Corpus) { for (size_t First = 0; First < 1; First++) { U.clear(); size_t Last = std::min(First + Options.MaxLen, C.size()); U.insert(U.begin(), C.begin() + First, C.begin() + Last); if (Options.OnlyASCII) ToASCII(U); if (RunOne(U)) { NewCorpus.push_back(U); if (Options.Verbosity >= 2) Printf("NEW0: %zd L %zd\n", LastRecordedBlockCoverage, U.size()); } } } Corpus = NewCorpus; for (auto &X : Corpus) UnitHashesAddedToCorpus.insert(Hash(X)); PrintStats("INITED"); }
void CCircuitry::DoPrint(BYTE* pBuffer, DWORD nLength, LPCTSTR lpszHeader) { CString strLine = lpszHeader; strLine += ToASCII( (BYTE*)pBuffer, nLength ); theApp.Message( MSG_DEBUG, (LPCTSTR)strLine ); }
void Fuzzer::RunOneAndUpdateCorpus(uint8_t *Data, size_t Size) { if (TotalNumberOfRuns >= Options.MaxNumberOfRuns) return; if (Options.OnlyASCII) ToASCII(Data, Size); if (RunOne(Data, Size)) ReportNewCoverage({Data, Data + Size}); }
void Fuzzer::RunOneAndUpdateCorpus(Unit &U) { if (TotalNumberOfRuns >= Options.MaxNumberOfRuns) return; if (Options.OnlyASCII) ToASCII(U); if (RunOne(U)) ReportNewCoverage(U); }
void CEDPacket::Debug(LPCTSTR pszReason) const { #ifdef _DEBUG if ( m_nType == ED2K_C2C_SENDINGPART ) return; if ( m_nType == ED2K_C2C_HASHSETANSWER ) return; if ( m_nType == ED2K_C2C_COMPRESSEDPART ) return; CString strOutput; strOutput.Format( L"[ED2K]: '%s' %s %s", pszReason, GetType(), (LPCTSTR)ToASCII() ); CPacket::Debug( strOutput ); #endif }
bool CAI302Device::WriteNavpoint(unsigned id, const Waypoint &wp, OperationEnvironment &env) { if (!DownloadMode(env)) return false; char name[64], remark[64]; ToASCII(name, ARRAY_SIZE(name), wp.name.c_str()); ToASCII(remark, ARRAY_SIZE(remark), wp.comment.c_str()); if (!CAI302::DownloadNavpoint(port, wp.location, (int)wp.elevation, id, wp.IsTurnpoint(), wp.IsAirport(), false, wp.IsLandable(), wp.IsStartpoint(), wp.IsFinishpoint(), wp.flags.home, false, wp.IsTurnpoint(), false, name, remark, env)) { mode = Mode::UNKNOWN; return false; } return true; }
/* * .KB_C_FN_DEFINITION_START * void DebugPrintHex(int, int) * This global function displays the value with the number of digits specified. * .KB_C_FN_DEFINITION_END */ void DebugPrintHex(int digits, int value) { char dValue[11], *cPtr; int nextDigit; if ((digits < 1) || (digits > 8)) return ; cPtr = &dValue[10]; *cPtr-- = 0; while (digits--) { nextDigit = 0xF & value; *cPtr-- = ToASCII(nextDigit); value >>= 4; } *cPtr-- = 'x'; *cPtr = '0'; DebugPrint(cPtr); }
BOOL CConnection::OnRead() { if ( m_hSocket == INVALID_SOCKET ) return FALSE; DWORD dwRead = 0; //check how many bytes we could read ioctlsocket(m_hSocket, FIONREAD, &dwRead); if (0 == dwRead) return TRUE; BYTE pData[TEMP_BUFFER]; while (dwRead) { int nLength = min( ( dwRead & 0xFFFFF ), TEMP_BUFFER ); nLength = recv( m_hSocket, (char*)pData, nLength, 0 ); if ( nLength <= 0 ) break; if ( nLength > 0 && nLength <= TEMP_BUFFER ) { m_pInput->Add( pData, nLength ); } dwRead -= nLength; #ifdef _DEBUG { CString strLine; CTime pNow = CTime::GetCurrentTime(); strLine.Format( _T("RX [%.2i:%.2i:%.2i]"), pNow.GetHour(), pNow.GetMinute(), pNow.GetSecond() ); strLine += ' ' + ToASCII( (BYTE*)pData, nLength ); afxDump << (LPCTSTR)strLine << "\n"; } #endif } return TRUE; }
BOOL CConnection::OnWrite() { if ( m_hSocket == INVALID_SOCKET ) return FALSE; if ( m_pOutput->m_nLength == 0 ) return TRUE; DWORD tNow = GetTickCount(); DWORD nLimit = 0xFFFFFFFF; BYTE* pBuffer = m_pOutput->m_pBuffer; DWORD nBuffer = m_pOutput->m_nLength; while ( nLimit && nBuffer ) { int nLength = min( ( nBuffer & 0xFFFFF ), TEMP_BUFFER ); nLength = send( m_hSocket, (char*)pBuffer, nLength, 0 ); if ( nLength <= 0 ) break; #ifdef _DEBUG { CString strLine; CTime pNow = CTime::GetCurrentTime(); strLine.Format( _T("TX [%.2i:%.2i:%.2i]"), pNow.GetHour(), pNow.GetMinute(), pNow.GetSecond() ); strLine += ' ' + ToASCII( (BYTE*)pBuffer, nLength ); afxDump << (LPCTSTR)strLine << "\n"; } #endif pBuffer += nLength; nBuffer -= nLength; } DWORD nTotal = ( m_pOutput->m_nLength - nBuffer ); if ( nTotal ) m_pOutput->Remove( nTotal ); return TRUE; }
void CPacket::Debug(LPCTSTR pszReason) const { if ( m_nLength ) theApp.Message( MSG_DEBUG, _T("%s Size: %u bytes ASCII: %s HEX: %s"), pszReason, m_nLength, (LPCTSTR)ToASCII(), (LPCTSTR)ToHex() ); else theApp.Message( MSG_DEBUG, _T("%s Size: %u bytes"), pszReason, m_nLength ); }