예제 #1
0
bool CTorrent::MakeTorrent(uint64 uPieceLength, bool bMerkle, const QString& Name, bool bPrivate)
{
    CFile* pFile = GetFile();

    if(!pFile->IsComplete())
    {
        LogLine(LOG_DEBUG | LOG_ERROR, tr("A torrent can not be made form an Incompelte file %1").arg(pFile->GetFileName()));
        return false;
    }

    if(uPieceLength < KB2B(16)) //if(!uPieceLength)
    {
        uPieceLength = pFile->GetFileSize() / (KB2B(40) / 20); // target hast set size 40 KB

        uint64 i = KB2B(16);
        for (; i < MB2B(32); i *= 2)
        {
            if (i >= uPieceLength)
                break;
        }
        uPieceLength = i;
    }

    m_TorrentInfo = new CTorrentInfo(this);
    m_TorrentInfo->SetTorrentName(Name.isEmpty() ? pFile->GetFileName() : Name);
    m_TorrentInfo->SetTotalLength(pFile->GetFileSize());
    if(bPrivate)
        m_TorrentInfo->SetPrivate();

    m_TorrentInfo->SetProperty("CreationTime", QDateTime::currentDateTime());

    ASSERT(m_pHash.isNull());
    if(bMerkle)
        m_pHash = CFileHashPtr(new CFileHashTree(HashTorrent, m_TorrentInfo->GetTotalLength(), uPieceLength));
    else
        m_pHash = CFileHashPtr(new CFileHashSet(HashTorrent, m_TorrentInfo->GetTotalLength(), uPieceLength));
    return true;
}
예제 #2
0
bool CTorrent::LoadTorrentFromFile(const QByteArray& InfoHash)
{
    ASSERT(m_TorrentInfo == NULL);
    m_TorrentInfo = new CTorrentInfo(this);

    ASSERT(m_pHash.isNull());
    m_pHash = CFileHashPtr(new CFileHash(HashTorrent));
    m_pHash->SetHash(InfoHash);

    QString TorrentFile = QString(InfoHash.toHex()) + ".torrent";

    if(m_TorrentInfo->LoadTorrentFile(theCore->m_TorrentManager->GetTorrentDir() + TorrentFile))
    {
        if(m_TorrentInfo->GetInfoHash() == InfoHash)
        {
            CFile* pFile = GetFile();

            if(pFile->GetFileSize() == 0)
                pFile->SetFileSize(m_TorrentInfo->GetTotalLength());

            LoadPieceHashes();

            if(m_TorrentInfo->IsMultiFile() && !pFile->IsMultiFile())
            {
                CFileHashPtr pMasterHash = pFile->GetMasterHash();
                if(!pMasterHash.isNull() && pMasterHash->GetHash() == InfoHash)
                {
                    LogLine(LOG_DEBUG | LOG_ERROR, tr("The multi file %1 is missing its proper index, restoring form torrent").arg(pFile->GetFileName()));
                    InstallMetadata();
                }
            }

            if(!m_TorrentInfo->IsEmpty() && !pFile->IsComplete() && !pFile->GetPartMap())
                SetupPartMap();
        }
        else
        {
            LogLine(LOG_DEBUG | LOG_ERROR, tr("The torrent file %1 contains an invalid infohash").arg(TorrentFile));
            delete m_TorrentInfo;
            m_TorrentInfo = new CTorrentInfo(this);
            m_TorrentInfo->SetInfoHash(InfoHash);
        }
    }
    else
        m_TorrentInfo->SetInfoHash(InfoHash);
    theCore->m_TorrentManager->RegisterInfoHash(m_TorrentInfo->GetInfoHash());

    return true; // Note: that is always true even if we fail to load as we always wil be able to proceed one way or another
}
_UINT32 CPPTXFile::ConvertPPTYToPPTX(std::wstring bsInput, std::wstring bsOutput, std::wstring bsThemesFolder)//bsOutput и файл и директория может быть 
{
	OOX::CPath pathLocalTempDirectory;
	
	if (m_fCallbackCompress)//если компрессора нет - конвертим в назначеную директорию 
		pathLocalTempDirectory = m_strTempDir ;
	else
		pathLocalTempDirectory = bsOutput; //выходной файл - папка

#ifdef _DEBUG
	#if defined(_WIN32) || defined (_WIN64)
		if (m_fCallbackCompress)
			pathLocalTempDirectory = _T("C:\\PPTMemory\\PPTX_test");
	#endif
#endif


	NSBinPptxRW::CPPTXWriter oWriter;
	oWriter.Init(pathLocalTempDirectory.GetPath());

	CFile oFileBinary;
	oFileBinary.OpenFile((std::wstring)bsInput);	
		LONG lFileSize = (LONG)oFileBinary.GetFileSize();
		BYTE* pSrcBuffer = new BYTE[lFileSize];
		oFileBinary.ReadFile(pSrcBuffer, (DWORD)lFileSize);
	oFileBinary.CloseFile();
	
	std::wstring strBsInput = bsInput;
    std::wstring srcFolder = NSDirectory::GetFolderPath(strBsInput);

	oWriter.OpenPPTY(pSrcBuffer, lFileSize, srcFolder, bsThemesFolder);
	
	RELEASEARRAYOBJECTS(pSrcBuffer);
	
	_UINT32 hRes = 0;

	if (m_fCallbackCompress)
	{
        std::wstring strOutput = bsOutput;
        std::wstring strInput = pathLocalTempDirectory.GetPath();

        hRes = m_fCallbackCompress(m_pCallbackArg, strInput, strOutput) ? 0 : AVS_FILEUTILS_ERROR_CONVERT;

        NSDirectory::DeleteDirectory(strInput);
	}
	return hRes;
}
예제 #4
0
bool JPGDecoder::CanDecode(const std::string &filename)
{
  CFile *fp = new CFile();
  bool ret = false;
  unsigned char magic[2];
  if (fp->Open(filename))
  {

    //JPEG image files begin with FF D8 and end with FF D9.
    // check for FF D8 big + little endian on start
    uint64_t readbytes = fp->Read(magic, 2);
    if (readbytes == 2)
    {
      if ((magic[0] == 0xd8 && magic[1] == 0xff) ||
          (magic[1] == 0xd8 && magic[0] == 0xff))
        ret = true;
    }

    if (ret)
    {
      ret = false;
      //check on FF D9 big + little endian on end
      uint64_t fileSize = fp->GetFileSize();
      fp->Seek(fileSize - 2);
      readbytes = fp->Read(magic, 2);
      if (readbytes == 2)
      {
        if ((magic[0] == 0xd9 && magic[1] == 0xff) ||
           (magic[1] == 0xd9 && magic[0] == 0xff))
          ret = true;
      }
    }
  }
  delete fp;
  return ret;
}
예제 #5
0
std::wstring COfficeOdfFileW::DetectTypeDocument(const std::wstring & pathOOX)
{
	std::wstring sRes;

	CFile file;

	CString fileContentType = std_string2string(pathOOX + FILE_SEPARATOR_STR + L"[Content_Types].xml");

	if (file.OpenFile(fileContentType) != S_OK) return sRes;

	int nBufferSize = min (file.GetFileSize(), 4096);
	BYTE *pBuffer = new BYTE[nBufferSize];

	file.ReadFile(pBuffer, nBufferSize);
	file.CloseFile();

	if (pBuffer != NULL)
	{

		const char *docxFormatLine = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
		const char *dotxFormatLine = "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml";
		const char *docmFormatLine = "application/vnd.ms-word.document.macroEnabled.main+xml";
		const char *dotmFormatLine = "application/vnd.ms-word.template.macroEnabledTemplate.main+xml";

		const char *xlsxFormatLine = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
		const char *xltxFormatLine = "application/vnd.openxmlformats-officedocument.spreadsheetml.template.main+xml";
		const char *xlsmFormatLine = "application/vnd.ms-excel.sheet.macroEnabled.main+xml";
		const char *xltmFormatLine = "application/vnd.ms-excel.template.macroEnabled.main+xml";

		const char *pptxFormatLine = "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml";
		const char *ppsxFormatLine = "application/vnd.openxmlformats-officedocument.presentationml.slideshow.main+xml";
		const char *potxFormatLine = "application/vnd.openxmlformats-officedocument.presentationml.template.main+xml";
		const char *pptmFormatLine = "application/vnd.ms-powerpoint.presentation.macroEnabled.main+xml";
		const char *ppsmFormatLine = "application/vnd.ms-powerpoint.slideshow.macroEnabled.main+xml";
		const char *potmFormatLine = "application/vnd.ms-powerpoint.template.macroEnabled.main+xml";

        std::string strContentTypes((char*)pBuffer, nBufferSize);

        int res = 0;
        if ( (res = strContentTypes.find(docxFormatLine))>0 || (res = strContentTypes.find(dotxFormatLine))>0 ||
            (res = strContentTypes.find(docmFormatLine))>0 || (res = strContentTypes.find(dotmFormatLine))>0)
		{
			sRes = L"text";
		}

        else if ((res = strContentTypes.find(xlsxFormatLine))>0 || (res = strContentTypes.find(xltxFormatLine))>0 ||
            (res = strContentTypes.find(xlsmFormatLine))>0 || (res = strContentTypes.find(xltmFormatLine))>0)
		{
			sRes = L"spreadsheet";
		}

        else if ((res = strContentTypes.find(pptxFormatLine) > 0) || /*(res = strContentTypes.find(ppsxFormatLine))>0 ||*/
            (res = strContentTypes.find(potxFormatLine))>0 || (res = strContentTypes.find(pptmFormatLine))>0 ||
            (res = strContentTypes.find(ppsmFormatLine))>0 || (res = strContentTypes.find(potmFormatLine))>0 ||
			(res = strContentTypes.find(ppsxFormatLine)) >0 )
		{
		}

		delete []pBuffer;
		pBuffer = NULL;

	}
	return sRes;
}
예제 #6
0
void CTorrent::SetupPartMap()
{
    ASSERT(!m_TorrentInfo->IsEmpty());

    CFile* pFile = GetFile();

    // Single File
    if(!m_TorrentInfo->IsMultiFile())
    {
        if(!pFile->GetPartMap())
            pFile->SetPartMap(CPartMapPtr(new CSynced<CPartMap>(m_TorrentInfo->GetTotalLength())));
        return;
    }

    // Multi File:

    CJoinedPartMap* pParts = qobject_cast<CJoinedPartMap*>(pFile->GetPartMap());
    if(pParts)
    {
        ASSERT(!pParts->GetLinks().isEmpty());
        return; // is already set up
    }
    pParts = new CJoinedPartMap(pFile->GetFileSize());

    pFile->GetInspector()->SetIndexSource(HashTorrent);

    CFileList* pList = pFile->GetList();

    uint64 Offset = 0;
    foreach(const CTorrentInfo::SFileInfo& SubFile, m_TorrentInfo->GetFiles())
    {
        if(SubFile.Length == 0)
        {
            LogLine(LOG_DEBUG | LOG_WARNING, tr("Ignoring empty file '%1' in torrent '%2'").arg(SubFile.FileName).arg(pFile->GetFileName()));
            continue;
        }

        CFile* pSubFile = new CFile();
        if(pFile->GetProperty("Temp").toBool())
            pSubFile->SetProperty("Temp", true);

        QString Dir = pFile->GetFileDir();
        Dir += pFile->GetFileName() + "/";
        if(!SubFile.FilePath.isEmpty())
            Dir += SubFile.FilePath.join("/") + "/";
        pSubFile->SetFileDir(Dir);

        pSubFile->AddEmpty(HashTorrent, SubFile.FileName, SubFile.Length, pFile->IsPending());

        // Note: SubFile->MasterHash is set in MasterFile->Resume

        uint64 uBegin = Offset;
        uint64 uEnd = Offset + SubFile.Length;
        Offset += SubFile.Length;

        CSharedPartMap* pSubParts = new CSharedPartMap(uEnd - uBegin);
        pSubFile->SetPartMap(CPartMapPtr(pSubParts));

        pParts->SetupLink(uBegin, uEnd, pSubFile->GetFileID());
        pSubParts->SetupLink(uBegin, uEnd, pFile->GetFileID());

        pList->AddFile(pSubFile);

        if(!pSubFile->IsPending())
            pSubFile->Resume();

        if(pFile->IsPaused(true))
            pSubFile->Pause();
        else if(pFile->IsStarted())
            pSubFile->Start();
    }

    pFile->SetPartMap(CPartMapPtr(pParts));
}
예제 #7
0
void CTorrent::OnFileHashed()
{
    CFile* pFile = GetFile();
    ASSERT(m_TorrentInfo);
    if(m_TorrentInfo->IsEmpty()) // are we making a torrent
    {
        QStringList Shared = theCore->Cfg()->GetStringList("Content/Shared");
        Shared.append(theCore->GetIncomingDir());
        Shared.append(theCore->GetTempDir());

        QList<CTorrentInfo::SFileInfo> Files;
        if(CJoinedPartMap* pParts = qobject_cast<CJoinedPartMap*>(pFile->GetPartMap()))
        {
            QMap<uint64, SPartMapLink*> Links = pParts->GetJoints();
            for(QMap<uint64, SPartMapLink*>::iterator I = Links.end(); I != Links.begin();)
            {
                SPartMapLink* pLink = *(--I);

                CFile* pSubFile = pFile->GetList()->GetFileByID(pLink->ID);
                if(!pSubFile)
                {
                    LogLine(LOG_DEBUG | LOG_ERROR, tr("A sub file of %1 has been being removed befoure the torrent was created").arg(pFile->GetFileName()));
                    pFile->TorrentHashed(this, false);
                    return;
                }

                CTorrentInfo::SFileInfo File;
                QString Root;
                QStringList Path = GetRelativeSharedPath(pSubFile->GetFilePath(), Shared, Root).split("/", QString::SkipEmptyParts);
                if(!Path.isEmpty())
                {
                    if(Path.count() > 1)
                        Path.removeFirst();
                    File.FileName = Path.takeLast();
                    File.FilePath = Path;
                }
                else
                    File.FileName = "unknown";
                File.Length = pSubFile->GetFileSize();
                Files.append(File);
            }
        }

        if(CFileHashTree* pHashTree = qobject_cast<CFileHashTree*>(m_pHash.data()))
            m_TorrentInfo->MakeMetadata(Files, pHashTree->GetPartSize(), QList<QByteArray>(), pHashTree->GetRootHash());
        else if(CFileHashSet* pHashSet = qobject_cast<CFileHashSet*>(m_pHash.data()))
            m_TorrentInfo->MakeMetadata(Files, pHashSet->GetPartSize(), pHashSet->GetHashSet());
        else {
            ASSERT(0);
        }

        if(!pFile->IsPending())
            SaveTorrentToFile();

        m_pHash->SetHash(m_TorrentInfo->GetInfoHash());
        theCore->m_TorrentManager->RegisterInfoHash(m_TorrentInfo->GetInfoHash());

        pFile->TorrentHashed(this, true);
    }
    else // we are importing a torrent
    {
        bool bMatch = false;

        if(CFileHashTree* pHashTree = qobject_cast<CFileHashTree*>(m_pHash.data()))
            bMatch = m_TorrentInfo->GetRootHash() == pHashTree->GetRootHash();
        else if(CFileHashSet* pHashSet = qobject_cast<CFileHashSet*>(m_pHash.data()))
            bMatch = m_TorrentInfo->GetPieceHashes() == pHashSet->GetHashSet();
        else {
            ASSERT(0);
        }

        if(bMatch)
        {
            if(!pFile->IsPending())
                SaveTorrentToFile();

            m_pHash->SetHash(m_TorrentInfo->GetInfoHash());
            theCore->m_TorrentManager->RegisterInfoHash(m_TorrentInfo->GetInfoHash());
        }

        pFile->TorrentHashed(this, bMatch);
    }
}
예제 #8
0
BOOL CKatakoi::GetNameIndex(CArcFile* pclArc, YCMemory<BYTE>& clmbtSec, DWORD& dwNameIndex)
{
	// Open the filename represented by the index
	TCHAR szPathToSec[MAX_PATH];

	if (GetPathToSec(szPathToSec, pclArc->GetArcPath()) == FALSE)
	{
		// sec5 file couldn't be found

//		MessageBox(pclArc->GetProg()->GetHandle(), _T("sec5ファイルが見つかりません。\nインストールフォルダ内にsec5ファイルが存在していない可能性があります。"), _T("エラー"), MB_OK);
		return FALSE;
	}

	CFile clfSec;

	if (!clfSec.OpenForRead(szPathToSec))
	{
		// Failed to open the sec5 file

		return FALSE;
	}

	DWORD dwSecSize = clfSec.GetFileSize();

	// Reading
	clmbtSec.resize(dwSecSize);
	clfSec.Read(&clmbtSec[0], dwSecSize);

	if (memcmp(&clmbtSec[0], "SEC5", 4) != 0)
	{
		// Incorrect sec5 file

		TCHAR szError[MAX_PATH * 2];

		_stprintf(szError, _T("%s is incorrect."), szPathToSec);
//		MessageBox(pclArc->GetProg()->GetHandle(), szError, _T("Error"), MB_OK);

		return FALSE;
	}

	// Find the RESR

	for (dwNameIndex = 8; dwNameIndex < dwSecSize; )
	{
		if (memcmp(&clmbtSec[dwNameIndex], "RESR", 4) == 0)
		{
			// Found "RESR"

			DWORD dwNameIndexSize = *(LPDWORD)&clmbtSec[dwNameIndex + 4];
			DWORD dwNameIndexFiles = *(LPDWORD)&clmbtSec[dwNameIndex + 8];

			dwNameIndex += 12;

			// Find the index that matches the name of the archive

			for (DWORD i = 0; i < dwNameIndexFiles; i++)
			{
				DWORD dwWork = 0;
				dwWork += strlen((char*)&clmbtSec[dwNameIndex + dwWork]) + 1;	// File name
				dwWork += strlen((char*)&clmbtSec[dwNameIndex + dwWork]) + 1;	// File type
				dwWork += strlen((char*)&clmbtSec[dwNameIndex + dwWork]) + 1;	// Archive type

				DWORD dwLength = *(LPDWORD)&clmbtSec[dwNameIndex + dwWork];		// Archive name + File number
				dwWork += 4;

				for (DWORD i = (dwNameIndex + dwWork); ; i++)
				{
					if (clmbtSec[i] == '\0')
					{
						// Index dex doesn't match the name of the archive

						break;
					}

					if (lstrcmp((LPCTSTR)&clmbtSec[i], pclArc->GetArcName()) == 0)
					{
						// Found a match with the name of the archive

						// Validity

						if (lstrcmp(PathFindFileName(szPathToSec), _T("toa.sec5")) == 0)
						{
							// 杏奈ちゃんにお願い
							pclArc->SetFlag(TRUE);
						}
						else if (lstrcmp(PathFindFileName(szPathToSec), _T("katakoi.sec5")) == 0)
						{
							// 片恋いの月
							pclArc->SetFlag(TRUE);
						}

						return TRUE;
					}
				}

				dwNameIndex += dwWork + dwLength;
			}
			break;
		}

		dwNameIndex += 8 + *(LPDWORD)&clmbtSec[dwNameIndex + 4];
	}

	// No file in the index was a match

//	MessageBox(pclArc->GetProg()->GetHandle(), _T("ファイル名の取得に失敗しました。\nアーカイブファイル名が変更されている可能性があります。"), _T("Error"), MB_OK);

	return FALSE;
}