Ejemplo n.º 1
0
int _tmain(int argc, _TCHAR* argv[])
{
	if (argc < 3) {
		_tprintf(_T("Usage: rf <source file/disk path> <desnitantion folder/file>\nFor disk use \\\\.\\H: as source path"));
		return -1;
	}
	HANDLE hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
	if (hFile != INVALID_HANDLE_VALUE) {
		UINT64 currentDone(0), totalSize(0);
		LARGE_INTEGER fileSize = {0};
		GetFileSizeEx(hFile, &fileSize);
		totalSize = fileSize.QuadPart;
		if (totalSize == 0) { // Disk Path
			ULARGE_INTEGER li = {0};
			GetDiskFreeSpaceEx(argv[1]+4, NULL, &li, NULL);
			totalSize = li.QuadPart;
		}
		Progress progress;
		progress.SetTask(totalSize);
		ConsoleProgress cp;
		Path dstPath(argv[2]);
		auto updateWSTime = ProcessUtil::GetTickCount(); // Update Writing speed time
		lprintf(_T("\nTime Elapsed: "));
		CountTimer timeElapsed;
		lprintf(_T("\t\t\t\tTime Remaining: "));
		CountTimer timeRemaining(true);
		if (dstPath.IsDir()) {
            CRecoverManager recoverManger;
            recoverManger.SetInputFileHandle(hFile);
            recoverManger.SetSavePath(dstPath);
            recoverManger.SetTotal(totalSize);
            recoverManger.Initialize();
            recoverManger.BeginRecover();
			while (recoverManger.ProcessRecover()) {
                currentDone = recoverManger.GetCurrentDone();
				if (progress.UpdateProgress(currentDone))
					cp.ShowPercentage(progress.GetCurrentPercentageDone());
				auto curTime = ProcessUtil::GetTickCount();
				if (curTime - updateWSTime >= 5000) { // 5 secs passed
					timeRemaining.SetTimeDuration((timeElapsed.GetTimeDuration() * totalSize) / currentDone);
					updateWSTime = curTime;
				}
				timeElapsed.PrintTimeDuration();
				timeRemaining.PrintTimeDuration();
			}
            if (progress.UpdateProgress(currentDone))
                cp.ShowPercentage(progress.GetCurrentPercentageDone());
            recoverManger.EndRecover();
            progress.UpdateProgress(totalSize);
            cp.ShowPercentage(progress.GetCurrentPercentageDone());
        }
		else {
			CFileMapping fileDstMap;
			CFileMapping fileSrcMap;
			//fileSrcMap.GetFileMapping(hFile);
			fileSrcMap.SetFileHandle(hFile);
			//HANDLE hDestFile = CreateFile(argv[2], GENERIC_ALL, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
			//if (hDestFile != INVALID_HANDLE_VALUE) {
			//	fileDstMap.SetFileHandle(hDestFile);
			if (fileDstMap.GetFileMapping(argv[2], totalSize) != NULL) {
				std::vector<char> buffer;
				buffer.resize(1024*1024);
				size_t size = buffer.size();
				char *buf = &buffer[0];
				while (currentDone < totalSize) {
					DWORD nBytesRead(fileSrcMap.Read(buf, (UINT)size));
					if (nBytesRead > 0) {
						fileDstMap.Write(buf, nBytesRead);
						currentDone += nBytesRead;
						if (progress.UpdateProgress(currentDone))
							cp.ShowPercentage(progress.GetCurrentPercentageDone());
						auto curTime = ProcessUtil::GetTickCount();
						if (curTime - updateWSTime >= 5000) { // 5 secs passed
							double p = progress.GetCurrentPercentageDone();
							if (p < 0.01)
								p = 0.01;
							timeRemaining.SetTimeDuration(__int64((timeElapsed.GetTimeDuration() * (100-p)) / p));
							updateWSTime = curTime;
						}
						timeElapsed.PrintTimeDuration();
						timeRemaining.PrintTimeDuration();
					}
					else break;
				}
				//CloseHandle(hDestFile);
			}
			else _tprintf(_T("Cannot open file: '%s'\nError: %d"), argv[2], GetLastError());
		}
		CloseHandle(hFile);
	}
    else {
        bool bError(true);
        if (GetLastError() == ERROR_ACCESS_DENIED)
            bError = !ProcessUtil::RunApplication(argc, (LPCTSTR*)argv, RAF_ADMIN);
        if (bError)
            _tprintf(_T("Cannot open file: '%s'\nError: %d"), argv[1], GetLastError());
    }
	return 0;
}
Ejemplo n.º 2
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;
}