Ejemplo n.º 1
0
static int HEXDump_FindCallBack(FindData &fd, HDFDCB_Data *pUserParam)
{
    if (!fd.fileMatched)
        return 0;
    int argc(pUserParam->argc);
    TCHAR **argv(pUserParam->argv);
    BinaryFind &bf(pUserParam->bf);
    FILE *fp = NULL;
    _tfopen_s(&fp, fd.fullPath.c_str(), _T("rb"));
    if (fp != NULL)
    {
        int nMatches(0);

        bf.SetFindBuffer();
        _tprintf(_T("%s\n"), fd.fullPath.c_str());
        pUserParam->nFiles++;
        Progress prog;
        const TCHAR *argStr = FindArgValue(argc, argv, _T("-o="));
        if (argStr != NULL) {
            long long offset(StringUtils::getLLfromStr(argStr));
            _fseeki64(fp, offset, offset >= 0 ? SEEK_SET : SEEK_END);
        }
        long long fileOffset(_ftelli64(fp));
        long long sizeToRead(fd.GetFileSize());
        const long long fileSize(sizeToRead);
        argStr = FindArgValue(argc, argv, _T("-s="));
        if (argStr != NULL) {
            long long szRead = StringUtils::getLLfromStr(argStr);
            if (fileOffset + szRead > sizeToRead)
                sizeToRead = sizeToRead - fileOffset;
            else
                sizeToRead = szRead;
        }
        size_t findDumpSize(0), findDumpOffset(-16);
        if (bf.HasFindPattern()) {
            argStr = FindArgValue(argc, argv, _T("-d"));
            if (argStr != NULL) {
                if (*argStr == '=') {
                    findDumpSize = StringUtils::getLLfromStr(argStr + 1);
                    STR_SKIP_TILL_CHAR(argStr, ';');
                    if (*argStr)
                        findDumpOffset = StringUtils::getLLfromStr(argStr + 1);
                }
                if (findDumpSize <= 0)
                    findDumpSize = 48;
            }
        }
        prog.SetTask(sizeToRead);
        BinaryData buffer(NULL, 4 * 1024 * 1024);
        while (sizeToRead > 0)
        {
            buffer.ReadFromFile(fp);
            if (buffer.DataSize() <= 0)
                break;
            sizeToRead -= buffer.DataSize();
            if (bf.HasFindPattern()) {
                bf.SetFindBuffer(buffer);
                while (true)
                {
                    long long findPos = bf.FindNext();
                    if (findPos >= 0) {
                        _tprintf(_T("%08llX=-%08llX\n"), fileOffset + findPos, fileSize - (fileOffset + findPos));
                        if (findDumpSize > 0) {
                            const long long curPos(_ftelli64(fp));
                            long long newPos(fileOffset + findPos + findDumpOffset);
                            if (newPos < 0)
                                newPos = 0;
                            newPos &= ~0xf;
                            BinaryData bd(NULL, findDumpSize);
                            bd.ReadFromFile(fp, 0, newPos);
                            HexDump(bd, newPos);
                            _fseeki64(fp, curPos, SEEK_SET);
                        }
                        ++nMatches;
                    }
                    else break;
                }
            }
            else
                fileOffset = HexDump(buffer, fileOffset);
            if (prog.UpdateProgress(prog.GetCurrentDone() + buffer.DataSize()))
                _tprintf(_T("\r%02.02f%%\r"), prog.GetCurrentPercentageDone());
        }
        _tprintf(_T("\r            \r"));
        fclose(fp);
        if (nMatches > 0) {
            pUserParam->nFound++;
            if (nMatches > pUserParam->nMaxMatchPerFile)
                pUserParam->nMaxMatchPerFile = nMatches;
            if (nMatches > 1)
                _tprintf(_T("%d matches\n"), nMatches);
        }
    }
    return 0;
}