コード例 #1
0
ファイル: LAdapt.c プロジェクト: botonchou/AlgoFinal
/* ProcessText: read text files line by line and count ngrams */
void ProcessText(char *fn, bool lastFile)
{
   FILE *f;
   LabId id;
   bool isPipe;
   char word[256];

   if (trace&T_TOP) 
      printf("Reading source text file %s\n",(fn==NULL) ? "<stdin>" : fn);
   if ((fn!=NULL) && (strcmp(fn,"-")!=0)) {
      if ((f = FOpen(fn,LMTextFilter,&isPipe))==NULL)
	 HError(16410,"ProcessText: unable to open text file %s", fn);
   } else {
      f = stdin;
   }
   while (fscanf(f,"%255s",word)==1) {
      if (pruneWords) {
	 if ((id = GetLabId(word,FALSE))==NULL && (id = unkId)==NULL) {
	    stdBuf.used=0;
	    continue;
	 }
      } else {
	 id = GetLabId(word,TRUE);
      }
      if (trace&T_INP) printf("[%s]\n",id->name);
      PutShiftRegister(id,&stdBuf);
   }
   if (fn!=NULL) {
      FClose(f,isPipe);
      if (lastFile)
	 CompressBuffer(stdBuf.ngb,TRUE);
   } else {
      CompressBuffer(stdBuf.ngb,TRUE);
   } 
}
コード例 #2
0
ファイル: LAdapt.c プロジェクト: botonchou/AlgoFinal
/* PutShiftRegister: push word into shift register and extract ngram */
void PutShiftRegister(LabId id, ShiftReg *sr)
{
   int i;
   MapEntry *me;
   
   if (trace&T_SHR){
      printf("   %12s --> %s\n",id->name,sr->ngb->fn);
      fflush(stdout);
   }
   AddWordToMap(&wmap,id); 
   mapUpdated = TRUE;
   me = (MapEntry *)id->aux;
   sr->ng[sr->used++] = me->ndx;
   if (sr->used == nSize) {
      /* record ngram */
      StoreNGram(sr->ngb,sr->ng);
      /* shift words */
      sr->used--;
      for (i=0; i<sr->used; i++)
         sr->ng[i] = sr->ng[i+1];
      /* compress buffer if full */
      if (sr->ngb->used == sr->ngb->poolsize)  {
         CompressBuffer(sr->ngb,FALSE);
      }
   }
}
コード例 #3
0
	void DeflateStream::WriteByte(int byte){
		SPADES_MARK_FUNCTION();
		if(mode != CompressModeCompress){
			SPRaise("Attempted to write when decompressing");
		}
		if(!valid){
			SPRaise("State is invalid");
		}
		
		if(buffer.size() >= bufferSize){
			CompressBuffer();
		}
		
		buffer.push_back((char)byte);
	}
コード例 #4
0
	void DeflateStream::Write(const void *data, size_t bytes) {
		SPADES_MARK_FUNCTION();
		if(mode != CompressModeCompress){
			SPRaise("Attempted to write when decompressing");
		}
		if(!valid){
			SPRaise("State is invalid");
		}
		
		if(buffer.size() >= bufferSize){
			CompressBuffer();
		}
		
		const char *dt = reinterpret_cast<const char *>(data);
		buffer.insert(buffer.end(),
					  dt, dt + bytes);
	}
コード例 #5
0
ファイル: secconfig.cpp プロジェクト: AlexWMF/Carberp
BOOL InsertSectionConfigInPE(PVOID pvPEBase,DWORD dwPESize,PVOID pvData,DWORD dwDataSize,PVOID *ppvNewPE,DWORD *pdwNewPESize)
{
	BOOL bRet = FALSE;
	PVOID pvCompressedData;
	DWORD dwCompressedDataSize;
	PVOID pvConfigData;
	DWORD dwConfigDataSize;
	DWORD dwSeed = GetTickCount();
	PIMAGE_SECTION_HEADER pNewSection;
	DWORD dwSize;

	if (CompressBuffer(pvData,dwDataSize,&pvCompressedData,&dwCompressedDataSize))
	{
		dwConfigDataSize = dwCompressedDataSize + sizeof(SECTION_CONFIG_HEADER);

		if (pvConfigData = malloc(dwConfigDataSize))
		{
			PSECTION_CONFIG_HEADER pSecCfgHeader = (PSECTION_CONFIG_HEADER)pvConfigData;

			pSecCfgHeader->dwDecompressedSize = dwDataSize;
			pSecCfgHeader->dwCompressedSize = dwCompressedDataSize;

			for (int i = 0; i < sizeof(pSecCfgHeader->bRc4Key); i++) 
			{
				pSecCfgHeader->bRc4Key[i] = (BYTE)RtlRandom(&dwSeed);
			}

			EncryptRc4(pSecCfgHeader->bRc4Key,sizeof(pSecCfgHeader->bRc4Key),&pSecCfgHeader[1],pvCompressedData,dwCompressedDataSize);

			if (pNewSection = InsertSectionHeader(pvPEBase,SECTION_CONFIG_NAME,dwConfigDataSize,IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ,&dwSize))
			{
				PVOID pvNewPE;
				DWORD dwNewPESize = pNewSection->PointerToRawData + pNewSection->SizeOfRawData;

				if (pvNewPE = malloc(dwNewPESize))
				{
					memcpy(pvNewPE,pvPEBase,dwPESize);

					PIMAGE_NT_HEADERS pNtHeaders = RtlImageNtHeader(pvNewPE);

					++(pNtHeaders->FileHeader.NumberOfSections);

					PIMAGE_SECTION_HEADER pVirtualLastSection = GetVirtualyLastSectionHeader(pNtHeaders);
					pVirtualLastSection[0] = *pNewSection;
					
					pNtHeaders->OptionalHeader.SizeOfImage += dwSize;

					memcpy((PVOID)((DWORD)pvNewPE + pNewSection->PointerToRawData),pvConfigData,dwConfigDataSize);

					DWORD dwHeaderSum, dwCheckSum;

					if (CheckSumMappedFile(pvNewPE,dwNewPESize,&dwHeaderSum,&dwCheckSum))
					{
						pNtHeaders->OptionalHeader.CheckSum = dwCheckSum;

						*ppvNewPE = pvNewPE;
						*pdwNewPESize = dwNewPESize;

						bRet = TRUE;
					}

					if (!bRet) free(pvNewPE);
				}

				free(pNewSection);
			}

			free(pvConfigData);
		}
	
		free(pvCompressedData);
	}

	return bRet;
}
コード例 #6
0
ファイル: SelfExtract.c プロジェクト: dannydraper/ByteGuard
BOOL MakeSelfExtractingPackage (HWND hwndDlg, char *szDestDir)
{
	int i, x;
	unsigned char inputFile [TC_MAX_PATH];
	unsigned char outputFile [TC_MAX_PATH];
	unsigned char szTmpFilePath [TC_MAX_PATH];
	unsigned char szTmp32bit [4] = {0};
	unsigned char *szTmp32bitPtr = szTmp32bit;
	unsigned char *buffer = NULL, *compressedBuffer = NULL;
	unsigned char *bufIndex = NULL;
	char tmpStr [2048];
	int bufLen = 0, compressedDataLen = 0, uncompressedDataLen = 0;

	x = strlen (szDestDir);
	if (x < 2)
		return FALSE;

	if (szDestDir[x - 1] != '\\')
		strcat (szDestDir, "\\");

	GetModuleFileName (NULL, inputFile, sizeof (inputFile));

	strcpy (outputFile, szDestDir);
	strncat (outputFile, OutputPackageFile, sizeof (outputFile) - strlen (outputFile) - 1);

	// Clone 'TrueCrypt Setup.exe' to create the base of the new self-extracting archive

	if (!TCCopyFile (inputFile, outputFile))
	{
		handleWin32Error (hwndDlg);
		PkgError ("Cannot copy 'TrueCrypt Setup.exe' to the package");
		return FALSE;
	}

	// Determine the buffer size needed for all the files and meta data and check if all required files exist

	bufLen = 0;

	for (i = 0; i < sizeof (szCompressedFiles) / sizeof (szCompressedFiles[0]); i++)
	{
		_snprintf (szTmpFilePath, sizeof(szTmpFilePath), "%s%s", szDestDir, szCompressedFiles[i]);

		if (!FileExists (szTmpFilePath))
		{
			char tmpstr [1000];

			_snprintf (tmpstr, sizeof(tmpstr), "File not found:\n\n'%s'", szTmpFilePath);
			remove (outputFile);
			PkgError (tmpstr);
			return FALSE;
		}

		bufLen += (int) GetFileSize64 (szTmpFilePath);

		bufLen += 2;					// 16-bit filename length
		bufLen += strlen(szCompressedFiles[i]);	// Filename
		bufLen += 4;					// CRC-32
		bufLen += 4;					// 32-bit file length
	}

	buffer = malloc (bufLen + 524288);	// + 512K reserve 
	if (buffer == NULL)
	{
		PkgError ("Cannot allocate memory for uncompressed data");
		remove (outputFile);
		return FALSE;
	}


	// Write the start marker
	if (!SaveBufferToFile (MAG_START_MARKER, outputFile, strlen (MAG_START_MARKER), TRUE))
	{
		PkgError ("Cannot write the start marker");
		remove (outputFile);
		return FALSE;
	}


	bufIndex = buffer;

	// Copy all required files and their meta data to the buffer
	for (i = 0; i < sizeof (szCompressedFiles) / sizeof (szCompressedFiles[0]); i++)
	{
		DWORD tmpFileSize;
		unsigned char *tmpBuffer;

		_snprintf (szTmpFilePath, sizeof(szTmpFilePath), "%s%s", szDestDir, szCompressedFiles[i]);

		tmpBuffer = LoadFile (szTmpFilePath, &tmpFileSize);

		if (tmpBuffer == NULL)
		{
			char tmpstr [1000];

			free (tmpBuffer);
			_snprintf (tmpstr, sizeof(tmpstr), "Cannot load file \n'%s'", szTmpFilePath);
			remove (outputFile);
			PkgError (tmpstr);
			goto msep_err;
		}

		// Copy the filename length to the main buffer
		mputWord (bufIndex, (WORD) strlen(szCompressedFiles[i]));

		// Copy the filename to the main buffer
		memcpy (bufIndex, szCompressedFiles[i], strlen(szCompressedFiles[i]));
		bufIndex += strlen(szCompressedFiles[i]);

		// Compute CRC-32 hash of the uncompressed file and copy it to the main buffer
		mputLong (bufIndex, GetCrc32 (tmpBuffer, tmpFileSize));

		// Copy the file length to the main buffer
		mputLong (bufIndex, (unsigned __int32) tmpFileSize);

		// Copy the file contents to the main buffer
		memcpy (bufIndex, tmpBuffer, tmpFileSize);
		bufIndex += tmpFileSize;

		free (tmpBuffer);
	}

	// Calculate the total size of the uncompressed data
	uncompressedDataLen = (int) (bufIndex - buffer);

	// Write total size of the uncompressed data
	szTmp32bitPtr = szTmp32bit;
	mputLong (szTmp32bitPtr, (unsigned __int32) uncompressedDataLen);
	if (!SaveBufferToFile (szTmp32bit, outputFile, sizeof (szTmp32bit), TRUE))
	{
		remove (outputFile);
		PkgError ("Cannot write the total size of the uncompressed data");
		return FALSE;
	}

	// Compress all the files and meta data in the buffer to create a solid archive

	compressedBuffer = malloc (uncompressedDataLen + 524288);	// + 512K reserve
	if (compressedBuffer == NULL)
	{
		remove (outputFile);
		PkgError ("Cannot allocate memory for compressed data");
		return FALSE;
	}

	compressedDataLen = CompressBuffer (compressedBuffer, buffer, uncompressedDataLen);
	if (compressedDataLen <= 0)
	{
		remove (outputFile);
		PkgError ("Failed to compress the data");
		return FALSE;
	}

	free (buffer);

	// Write the total size of the compressed data
	szTmp32bitPtr = szTmp32bit;
	mputLong (szTmp32bitPtr, (unsigned __int32) compressedDataLen);
	if (!SaveBufferToFile (szTmp32bit, outputFile, sizeof (szTmp32bit), TRUE))
	{
		remove (outputFile);
		PkgError ("Cannot write the total size of the compressed data");
		return FALSE;
	}

	// Write the compressed data
	if (!SaveBufferToFile (compressedBuffer, outputFile, compressedDataLen, TRUE))
	{
		remove (outputFile);
		PkgError ("Cannot write compressed data to the package");
		return FALSE;
	}

	// Write the end marker
	if (!SaveBufferToFile (MagEndMarker, outputFile, strlen (MagEndMarker), TRUE))
	{
		remove (outputFile);
		PkgError ("Cannot write the end marker");
		return FALSE;
	}

	free (compressedBuffer);

	// Compute and write CRC-32 hash of the entire package
	{
		DWORD tmpFileSize;
		char *tmpBuffer;

		tmpBuffer = LoadFile (outputFile, &tmpFileSize);

		if (tmpBuffer == NULL)
		{
			handleWin32Error (hwndDlg);
			remove (outputFile);
			PkgError ("Cannot load the package to compute CRC");
			return FALSE;
		}

		// Zero all bytes that change when the exe is digitally signed (except appended blocks).
		WipeSignatureAreas (tmpBuffer);

		szTmp32bitPtr = szTmp32bit;
		mputLong (szTmp32bitPtr, GetCrc32 (tmpBuffer, tmpFileSize));
		if (!SaveBufferToFile (szTmp32bit, outputFile, sizeof (szTmp32bit), TRUE))
		{
			remove (outputFile);
			PkgError ("Cannot write the total size of the compressed data");
			return FALSE;
		}

		free (tmpBuffer);
	}

	sprintf (tmpStr, "Self-extracting package successfully created (%s)", outputFile);
	PkgInfo (tmpStr);
	return TRUE;

msep_err:
	free (buffer);
	free (compressedBuffer);
	return FALSE;
}
コード例 #7
0
ファイル: builder.cpp プロジェクト: hoangcuongflp/packerPE32
int main(int argc, char* argv[])
{
	char* strStub, *strTarget = 0;
	char strOutput[] = "output.exe";
	if (argc > 2)
	{
		DWORD sizeStub, sizeTarget,sizeTargetCompressed = 0;
		LPVOID ptrStub, ptrTarget,ptrTargetTemp, ptrOutput,ptrTargetAligned = NULL;
		//////////////
		PIMAGE_DOS_HEADER dosHeader,dosHeaderTarget = NULL;
		PIMAGE_NT_HEADERS ntHeader,ntHeaderTarget = NULL;
		PIMAGE_SECTION_HEADER sectionHeader = NULL;
		PIMAGE_FILE_HEADER fileHeader = NULL;
		DWORD nbSections = 0;
		DWORD sizeHeaders = 0;
		DWORD sizeHeadersWritten = 0;
		DWORD virtualAddress = 0;
		DWORD rawSize = 0;
		DWORD sizeOfImage = 0;
		DWORD ptrRawData = 0;
		//////////////
		sizeTarget = 0;
		strStub = argv[1];
		strTarget = argv[2];

		ptrTargetTemp = fileToMem(strTarget,&sizeTarget,0);
		ptrTarget = CompressBuffer(ptrTargetTemp, sizeTarget,&sizeTargetCompressed);
		ptrStub = fileToMem(strStub, &sizeStub, sizeTargetCompressed);
		printf("[....] Stub loaded.\n");
		
		printf("[....] Target loaded.\n");
		printf("Size before compression : %d\n", sizeTarget);
		printf("Size after compression : %d\n", sizeTargetCompressed);
		dosHeader = (PIMAGE_DOS_HEADER)ptrStub;
		dosHeaderTarget = (PIMAGE_DOS_HEADER)ptrTargetTemp;
		if (dosHeader->e_magic != 0x5A4D)
			exit(-10);
		ntHeader = (PIMAGE_NT_HEADERS)((PUCHAR)dosHeader + dosHeader->e_lfanew);
		ntHeaderTarget = (PIMAGE_NT_HEADERS)((PUCHAR)dosHeaderTarget + dosHeaderTarget->e_lfanew);
		if(ntHeader->Signature != 0x00004550)
			exit(-10);
		sectionHeader = (PIMAGE_SECTION_HEADER)IMAGE_FIRST_SECTION(ntHeader);
		fileHeader = (PIMAGE_FILE_HEADER)&ntHeader->FileHeader;
		nbSections = fileHeader->NumberOfSections;
		printf("[...] Stub ... : \n");
		/*
		printf("Number of sections : %d\n", nbSections);
		for (DWORD i = 0;i < nbSections;i++)
		{
			printf("... Name %s : \n", sectionHeader[i].Name);
			printf("... SizeRawData 0x%x : \n", sectionHeader[i].SizeOfRawData);
			printf("... PointerToRawData 0x%x : \n", sectionHeader[i].PointerToRawData);
			printf("... VirtualAddress 0x%x : \n", sectionHeader[i].VirtualAddress);
			printf("\n\n");
		}*/
		// We need to check if there is enough space to add a sectionheader in sectionheader tab
		sizeHeaders = ntHeader->OptionalHeader.SizeOfHeaders;
		sizeHeadersWritten = fileHeader->SizeOfOptionalHeader; // Size of Optional
		sizeHeadersWritten += sizeof(IMAGE_FILE_HEADER);
		sizeHeadersWritten += sizeof(IMAGE_SECTION_HEADER)*nbSections; // Size of all header for each sections
		sizeHeadersWritten += dosHeader->e_lfanew; 
		/*Ok so IMAGE_DOS_HEADER add some padding
		We need to use this instead of sizeof
		dosHeader->e_lfanew	0x000000c8	long
		sizeof(IMAGE_DOS_HEADER)	0x00000040	unsigned int
		*/
		sizeHeaders -= sizeHeadersWritten;
		//printf("Space left in headers : %x\n", sizeHeaders);
		IMAGE_SECTION_HEADER newSection,test;
		
		newSection.Characteristics = IMAGE_SCN_MEM_READ ;// This on will be change later
		CopyMemory(&newSection.Name, (void *)".pack3", strlen(".pack3") + 1);
		// Not used ?
		newSection.NumberOfLinenumbers = 0;
		newSection.NumberOfRelocations = 0;
		newSection.PointerToLinenumbers = 0;
		newSection.PointerToRelocations = 0;  
		////////

		
		virtualAddress = sectionHeader[nbSections - 1].SizeOfRawData
			/ ntHeader->OptionalHeader.SectionAlignment;
		virtualAddress = (virtualAddress + 1)*ntHeader->OptionalHeader.SectionAlignment;
		virtualAddress += sectionHeader[nbSections - 1].VirtualAddress;
		newSection.VirtualAddress = virtualAddress;
		//We need to calc the RVA for this new section
		//So we get the last RVA and add the size rounded with the section alignement
		newSection.Misc.VirtualSize = sizeTargetCompressed;
		// Same for rawSize
		rawSize = sizeTargetCompressed
			/ ntHeader->OptionalHeader.FileAlignment;
		if (sizeTargetCompressed % ntHeader->OptionalHeader.FileAlignment);
			rawSize++;
		rawSize = (rawSize) * ntHeader->OptionalHeader.FileAlignment;
		newSection.SizeOfRawData = rawSize;
		// Pointer to rawdata calc
		ptrRawData = sectionHeader[nbSections - 1].PointerToRawData+ sectionHeader[nbSections - 1].SizeOfRawData;
		newSection.PointerToRawData = ptrRawData;


		
		sizeOfImage = ntHeader->OptionalHeader.SizeOfImage + rawSize;

		//fileHeader->NumberOfSections = nbSections + 1;

		ntHeader->OptionalHeader.SizeOfImage = sizeOfImage ;
		CopyMemory(&sectionHeader[nbSections], &newSection, sizeof(IMAGE_SECTION_HEADER));
		////// Adjust allocated memory to stabilize execution
		CopyMemory(&newSection.Name, (void *)".pack4", strlen(".pack4") + 1);

		DWORD delta = ntHeaderTarget->OptionalHeader.SizeOfImage - ntHeader->OptionalHeader.SizeOfImage;
		DWORD rvaAligned = rawSize;
		rvaAligned /= ntHeader->OptionalHeader.SectionAlignment;
		if (rawSize%ntHeader->OptionalHeader.SectionAlignment)
			rvaAligned++;
		rvaAligned =(rvaAligned)*ntHeader->OptionalHeader.SectionAlignment;
		delta /= ntHeader->OptionalHeader.SectionAlignment;
		delta = (delta+1)*ntHeader->OptionalHeader.SectionAlignment;
		newSection.VirtualAddress += rvaAligned;
		newSection.Misc.VirtualSize = delta;
		newSection.SizeOfRawData = 0;
		ntHeader->OptionalHeader.SizeOfImage += delta;
		CopyMemory(&sectionHeader[nbSections+1], &newSection, sizeof(IMAGE_SECTION_HEADER));
		fileHeader->NumberOfSections = nbSections + 2;
		


		/////
		ptrTargetAligned = VirtualAlloc(0, rawSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
		CopyMemory(ptrTargetAligned,ptrTarget,sizeTargetCompressed);
		ptrRawData += (DWORD)ptrStub;
		CopyMemory((PVOID)ptrRawData, ptrTargetAligned, rawSize);
		memToFile(ptrStub, strOutput, sizeStub + rawSize);
		printf("[...] Build done !");
		//printf("IMAGE_SECTION_HEADER size: %d\n", sizeof(IMAGE_SECTION_HEADER));
		//printf("Fragment Size : %d\n", sizeFragment);
			
		getc(stdin);
		VirtualFree(ptrTargetTemp, sizeTarget,MEM_DECOMMIT);
	}
	else
		return -1;
	return 0;
}