Example #1
0
bool PeParser::readPeSectionsFromFile()
{
	bool retValue = true;
	DWORD readOffset = 0;

	listPeSection.reserve(getNumberOfSections());


	if (openFileHandle())
	{
		for (WORD i = 0; i < getNumberOfSections(); i++)
		{
			readOffset = listPeSection[i].sectionHeader.PointerToRawData;

			listPeSection[i].normalSize = listPeSection[i].sectionHeader.SizeOfRawData;

			if (!readSectionFromFile(readOffset, listPeSection[i]))
			{
				retValue = false;
			}
			
		}

		closeFileHandle();
	}
	else
	{
		retValue = false;
	}

	return retValue;
}
Example #2
0
bool PeParser::getFileOverlay()
{
	DWORD numberOfBytesRead;
	bool retValue = false;

	if (!hasOverlayData())
	{
		return false;
	}

	if (openFileHandle())
	{
		DWORD overlayOffset = getSectionHeaderBasedFileSize();
		DWORD fileSize = (DWORD)ProcessAccessHelp::getFileSize(hFile);
		overlaySize = fileSize - overlayOffset;

		overlayData = new BYTE[overlaySize];

		SetFilePointer(hFile, overlayOffset, 0, FILE_BEGIN);

		if (ReadFile(hFile, overlayData, overlaySize, &numberOfBytesRead, 0))
		{
			retValue = true;
		}

		closeFileHandle();
	}

	return retValue;
}
Example #3
0
bool PeParser::readPeHeaderFromFile(bool readSectionHeaders)
{
	bool retValue = false;
	DWORD correctSize = 0;
	DWORD numberOfBytesRead = 0;

	DWORD readSize = getInitialHeaderReadSize(readSectionHeaders);

	headerMemory = new BYTE[readSize];

	if (openFileHandle())
	{
		fileSize = (DWORD)ProcessAccessHelp::getFileSize(hFile);

		if (ReadFile(hFile, headerMemory, readSize, &numberOfBytesRead, 0))
		{
			retValue = true;

			getDosAndNtHeader(headerMemory, (LONG)readSize);

			if (isValidPeFile())
			{
				correctSize = calcCorrectPeHeaderSize(readSectionHeaders);

				if (readSize < correctSize)
				{
					readSize = correctSize;

					if (fileSize > 0)
					{
						if (fileSize < correctSize)
						{
							readSize = fileSize;
						}
					}

					
					delete [] headerMemory;
					headerMemory = new BYTE[readSize];

					SetFilePointer(hFile, 0, 0, FILE_BEGIN);

					if (ReadFile(hFile, headerMemory, readSize, &numberOfBytesRead, 0))
					{
						getDosAndNtHeader(headerMemory, (LONG)readSize);
					}
				}
			}
		}

		closeFileHandle();
	}

	return retValue;
}
Example #4
0
  void TraceUtil::setFileName(const char* fileNameToUse)
  {
    std::string temporaryString(fileNameToUse);
    if (temporaryString.length() <= 0)
    {
      return;
    }

    // Close the existing file handle if one is already opened.
    closeFileHandle();

    // Open or reopen the file handle.
    impl_->fileName_ = temporaryString;
    openFileHandle();
  }
Example #5
0
void ModulatorSamplerSound::closeFileHandle()
{
	FOR_EVERY_SOUND(closeFileHandle());
}
Example #6
0
TitleList *getUpdateCIAs() {
  Handle dir, CIAFile;
  FS_DirectoryEntry ent;
  int ret;
  int ciafiles;
  TitleList *updateCIAs;
  char ciaPath[9 + 16 + 4 + 1]; // /updates/ + 16 hex digits + .cia + \0
  
  LOG_INFO("getUpdateCIAs");
  // Run through first and count .cia files.
  dir = openDirectory(CIAS_PATH);
  if(dir == 0) {
    LOG_ERROR("getUpdateCIAs: Failed to open SDMC:" CIAS_PATH ".\n");
    printf("Failed to open SDMC:" CIAS_PATH ".\n");
    goto error0;
  }
  ciafiles = 0;
  for(;;) {
    ret = getNextFile(dir, &ent);
    
    if(ret < 1) {
      if(ret == -1) {
        LOG_ERROR("getUpdateCIAs: Error reading directory.");
        printf("Error reading directory.\n");
        goto error2;
      }
      break;
    }
    
    simpleUTF16toASCII((char *)(ent.name));
    if(isCIAName((char *)(ent.name)) == 1) {
      ciafiles++;
    }
  }
  closeDirectory(dir);
  LOG_VERBOSE("Found %d files.", ciafiles);
  updateCIAs = initTitleList(ciafiles, 0);
  
  // Run through a second time and add CIA info.
  dir = openDirectory(CIAS_PATH);
  if(dir == 0) {
    LOG_ERROR("getUpdateCIAs: Failed to open SDMC:" CIAS_PATH ".\n");
    printf("Failed to open SDMC:" CIAS_PATH ".\n");
    goto error1;
  }
  ciafiles = 0;
  for(;;) {
    ret = getNextFile(dir, &ent);
    if(ret < 1) {
      if(ret == -1) {
        LOG_ERROR("getUpdateCIAs: Error reading directory.");
        printf("Error reading directory.\n");
        goto error2;
      }
      break;
    }
    
    simpleUTF16toASCII((char *)(ent.name));
    if(isCIAName((char *)(ent.name)) == 1) {
      snprintf(ciaPath, 9 + 16 + 4 + 1, CIAS_PATH "%s", ent.name);
      CIAFile = openFileHandle(ciaPath, FS_OPEN_READ);
      if(CIAFile == 0) {
        LOG_ERROR("getUpdateCIAs: Failed to open %s for read.\n", ciaPath);
        printf("Failed to open %s for read.\n", ciaPath);
        goto error2;
      }
      
      if(R_FAILED(AM_GetCiaFileInfo(MEDIATYPE_NAND, updateCIAs->title[ciafiles], CIAFile))) {
        LOG_ERROR("getUpdateCIAs: Failed to get information on %s.\n", ciaPath);
        printf("Failed to get information on %s.\n", ciaPath);
        goto error3;
      }
      
      closeFileHandle(CIAFile);
      
      ciafiles++;
      LOG_VERBOSE("getUpdateCIAs: Added %s.", (char *)(ent.name));
    }
  }
  closeDirectory(dir);
  
  LOG_INFO("getUpdateCIAs: Got CIAs from SD card.");
  return(updateCIAs);

error3:
  closeFileHandle(CIAFile);
error2:
  closeDirectory(dir);
error1:
  freeTitleList(updateCIAs);
error0:
  LOG_ERROR("getUpdateCIAs: Failed to get CIAs from SD card.");
  return(NULL);
}
Example #7
0
int installTitleFromCIA(const char *path, PrintConsole *con) {
  Handle CIAIn, CIAOut;
  u32 bytesread;
  u32 byteswritten;
  u64 totalread;
  int animpos;
  
  LOG_INFO("installTitleFromCIA: Installing CIA from %s.", path);
  
  CIAIn = openFileHandle(path, FS_OPEN_READ);
  if(CIAIn == 0) {
    LOG_ERROR("installTitleFromCIA: Failed to open %s.", path);
    printf("Failed to open file %s.\n", path);
    goto error0;
  }

#ifdef ARMED
  if(R_FAILED(AM_StartCiaInstall(MEDIATYPE_NAND, &CIAOut))) {
    LOG_ERROR("installTitleFromCIA: AM_StartCiaInstall failed.");
    printf("Failed to start CIA install.\n");
    goto error1;
  }
#endif
  
  bytesread = BUFFERSIZE;
  totalread = 0;
  while(bytesread == BUFFERSIZE) {
    printf("%c", anim[animpos]);
    stepFrame();
    con->cursorX--;
    animpos = (animpos + 1) % 4;
    
    if(R_FAILED(FSFILE_Read(CIAIn, &bytesread, totalread, buffer, BUFFERSIZE))) {
      printf("\nFailed to read %s around %llu!\n", path, totalread);
      stepFrame();
      goto error2;
    }
#ifdef ARMED
    if(R_FAILED(FSFILE_Write(CIAOut, &byteswritten, totalread, buffer, bytesread, FS_WRITE_FLUSH))) {
      printf("\nFailed to write %s around %llu!\n", path, totalread);
      stepFrame();
      goto error2;
    }
    if(byteswritten < bytesread) {
      LOG_ERROR("installTitleFromCIA: Incomplete write around %llu.", totalread);
      printf("\nIncompelete write around %llu!\n", totalread);
      stepFrame();
      goto error2;
    }
#endif

    totalread += bytesread;
  }

#ifdef ARMED
  if(R_FAILED(AM_FinishCiaInstall(MEDIATYPE_NAND, &CIAOut))) {
    LOG_ERROR("installTitleFromCIA: AM_FinishCiaInstall failed.");
    printf("Failed to finalize CIA install.\n");
    goto error2;
  }
#endif
  
  closeFileHandle(CIAIn);

  LOG_INFO("Successfully installed %s.", path);
  return(0);
  
error2:
#ifdef ARMED
  if(R_FAILED(AM_CancelCIAInstall(&CIAOut))) {
    printf("Couldn't cancel unsuccessful CIA install.\n");
  }
#endif
error1:
  closeFileHandle(CIAIn);
error0:

  LOG_ERROR("Failed installing %s.", path);
  return(-1);
}
Example #8
0
int checkUpdates(const UpdateInfo *info, TitleList *updateCIAs, PrintConsole *con) {
  Handle CIAHandle;
  char filename[UPDATE_FILENAME_LEN];
  md5_state_t md5;
  u32 bytesread;
  u64 totalread;
  char digest[16];
  char strdigest[33]; // 32 hex digits + \0
  TitleList *extraTitles;
  int i, j;
  int animpos;
  
  extraTitles = initTitleList(updateCIAs->nTitles, 1);
  if(extraTitles == NULL) {
    printf("Couldn't create title list.\n");
    goto error0;
  }
  memcpy(extraTitles->title, updateCIAs->title, sizeof(AM_TitleEntry *) * updateCIAs->nTitles);
  if(extraTitles->nTitles == 0) {
    printf("No titles found to check.\nThere is nothing to do.\n");
    goto error1;
  }
  
  for(i = 0; i < info->nUpdates; i++) {
    snprintf(filename, UPDATE_FILENAME_LEN, CIAS_PATH "%016llX.cia",
             info->updates[i].titleID);
    printf("Checking %s... ", filename);
    stepFrame();

    CIAHandle = openFileHandle(filename, FS_OPEN_READ);
    if(CIAHandle == 0) {
      if(info->info.region == CFG_REGION_JPN && info->updates[i].titleID == 0x000400102002CA00) {
        printf("Bad update missing, this is good.\n");
      } else {
        printf("\nOpen Failed!\n", filename);
        goto error1;
      }
    }

    md5_init(&md5);
    bytesread = BUFFERSIZE;
    totalread = 0;
    animpos = 0;
    while(bytesread == BUFFERSIZE) {
      printf("%c", anim[animpos]);
      con->cursorX--;
      animpos = (animpos + 1) % 4;
      
      if(R_FAILED(FSFILE_Read(CIAHandle, &bytesread, totalread, buffer, BUFFERSIZE))) {
        printf("\nFailed to read %s around %s!\n", filename, totalread);
        stepFrame();
        freeTitleList(extraTitles);
        goto error2;
      }
      md5_append(&md5, buffer, bytesread);
      
      totalread += bytesread;
    }
    closeFileHandle(CIAHandle);
    md5_finish(&md5, digest);

    printf(" \n");
    // Cheezy bin2hex
    snprintf(strdigest, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x \n",
      digest[0], digest[1], digest[2], digest[3], digest[4], digest[5],
      digest[6], digest[7], digest[8], digest[9], digest[10], digest[11], 
      digest[12], digest[13], digest[14], digest[15]);
      
    printf("%s ", strdigest);
    stepFrame();

    if(strncmp(info->updates[i].md5, strdigest, 33) != 0) {
      printf("!= \n%s FAIL\n", info->updates[i].md5);
      goto error1;
    }

    if(info->info.region == CFG_REGION_JPN && info->updates[i].titleID == 0x000400102002CA00) {
      printf("Firmware file known to brick Japanese 3DS\n" \
             "present!  It is recommended you delete the\n" \
             "file named " CIAS_PATH "000400102002CA00.cia\n" \
             "before installing the updates!\n" \
             "Press any key to continue . . .\n");
      stepFrame();
      waitKey();
    } else {
      printf("OK\n");
      stepFrame();

      for(j = 0; j < extraTitles->nTitles; j++) {
        if(extraTitles->title[j] == NULL) {
          continue;
        }
        if(extraTitles->title[j]->titleID == info->updates[i].titleID) {
          extraTitles->title[j] = NULL;
          break;
        }
      }
    }
  }

  rearrangeTitles(extraTitles);
  for(i = 0; i < extraTitles->nTitles; i++) {
    if(extraTitles->title[i] == NULL) {
      extraTitles->nTitles = i;
      break;
    }
  }
  if(extraTitles->nTitles > 0) {
    printf("These are the titles that were found but weren't\n" \
           "in the internal hash tables, meaning they aren't\n" \
           "part of the official update.  They will be\n" \
           "installed if you choose to install now.  Look\n" \
           "over them to see if they are what you want before\n" \
           "choosing to install.");
    waitKey();
    printTitlesMore(extraTitles, con);
    printf("Press any key to continue . . .");
    waitKey();
    printf("\n");
  } else {
    printf("No excess titles.\n");
  }
  freeTitleList(extraTitles);

  return(0);
  
error2:
  closeFileHandle(CIAHandle);
error1:
  freeTitleList(extraTitles);
error0:
  return(-1);
}
Example #9
0
 ///
 /// Destructor; simply close the file handle.
 ///
 TraceUtil::~TraceUtil()
 {
   closeFileHandle();
   delete impl_;
 }
Example #10
0
bool PeParser::savePeFileToDisk( const WCHAR * newFile )
{
	bool retValue = true;
	DWORD dwFileOffset = 0, dwWriteSize = 0;

	if (getNumberOfSections() != listPeSection.size())
	{
		return false;
	}

	if (openWriteFileHandle(newFile))
	{
		//Dos header
		dwWriteSize = sizeof(IMAGE_DOS_HEADER);
		if (!ProcessAccessHelp::writeMemoryToFile(hFile, dwFileOffset, dwWriteSize, pDosHeader))
		{
			retValue = false;
		}
		dwFileOffset += dwWriteSize;


		if (dosStubSize && pDosStub)
		{
			//Dos Stub
			dwWriteSize = dosStubSize;
			if (!ProcessAccessHelp::writeMemoryToFile(hFile, dwFileOffset, dwWriteSize, pDosStub))
			{
				retValue = false;
			}
			dwFileOffset += dwWriteSize;
		}


		//Pe Header
		if (isPE32())
		{
			dwWriteSize = sizeof(IMAGE_NT_HEADERS32);
		}
		else
		{
			dwWriteSize = sizeof(IMAGE_NT_HEADERS64);
		}

		if (!ProcessAccessHelp::writeMemoryToFile(hFile, dwFileOffset, dwWriteSize, pNTHeader32))
		{
			retValue = false;
		}
		dwFileOffset += dwWriteSize;

		//section headers
		dwWriteSize = sizeof(IMAGE_SECTION_HEADER);

		for (WORD i = 0; i < getNumberOfSections(); i++)
		{
			if (!ProcessAccessHelp::writeMemoryToFile(hFile, dwFileOffset, dwWriteSize, &listPeSection[i].sectionHeader))
			{
				retValue = false;
				break;
			}
			dwFileOffset += dwWriteSize;
		}

		for (WORD i = 0; i < getNumberOfSections(); i++)
		{
			if (!listPeSection[i].sectionHeader.PointerToRawData)
				continue;


			if (listPeSection[i].sectionHeader.PointerToRawData > dwFileOffset)
			{
				dwWriteSize = listPeSection[i].sectionHeader.PointerToRawData - dwFileOffset; //padding

				if (!writeZeroMemoryToFile(hFile, dwFileOffset, dwWriteSize))
				{
					retValue = false;
					break;
				}
				dwFileOffset += dwWriteSize;
			}

			dwWriteSize = listPeSection[i].dataSize;

			if (dwWriteSize)
			{
				if (!ProcessAccessHelp::writeMemoryToFile(hFile, listPeSection[i].sectionHeader.PointerToRawData, dwWriteSize, listPeSection[i].data))
				{
					retValue = false;
					break;
				}
				dwFileOffset += dwWriteSize;

				if (listPeSection[i].dataSize < listPeSection[i].sectionHeader.SizeOfRawData) //padding
				{
					dwWriteSize = listPeSection[i].sectionHeader.SizeOfRawData - listPeSection[i].dataSize;

					if (!writeZeroMemoryToFile(hFile, dwFileOffset, dwWriteSize))
					{
						retValue = false;
						break;
					}
					dwFileOffset += dwWriteSize;
				}
			}

		}

		//add overlay?
		if (overlaySize && overlayData)
		{
			dwWriteSize = overlaySize;
			if (!ProcessAccessHelp::writeMemoryToFile(hFile, dwFileOffset, dwWriteSize, overlayData))
			{
				retValue = false;
			}
			dwFileOffset += dwWriteSize;
		}

		SetEndOfFile(hFile);

		closeFileHandle();
	}

	return retValue;
}