Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
4.0 KiB

  1. #include <private.h>
  2. #include <strsafe.h>
  3. BOOL
  4. StripCVSymbolPath (
  5. LPSTR DestinationFile
  6. )
  7. {
  8. LOADED_IMAGE LoadedImage;
  9. DWORD DirCnt;
  10. IMAGE_DEBUG_DIRECTORY UNALIGNED *DebugDirs, *CvDebugDir;
  11. PVOID pData;
  12. ULONG mysize;
  13. BOOL rc = FALSE;
  14. if (MapAndLoad(
  15. DestinationFile,
  16. NULL,
  17. &LoadedImage,
  18. FALSE,
  19. FALSE) == FALSE) {
  20. return (FALSE);
  21. }
  22. __try {
  23. pData = ImageDirectoryEntryToData(LoadedImage.MappedAddress,
  24. FALSE,
  25. IMAGE_DIRECTORY_ENTRY_SECURITY,
  26. &DirCnt
  27. );
  28. if (pData || DirCnt) {
  29. __leave; // Signed - can't change it
  30. }
  31. pData = ImageDirectoryEntryToData(LoadedImage.MappedAddress,
  32. FALSE,
  33. IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR,
  34. &DirCnt
  35. );
  36. if (pData) {
  37. // COR header found - see if it's strong signed
  38. if (((IMAGE_COR20_HEADER *)pData)->Flags & COMIMAGE_FLAGS_STRONGNAMESIGNED) {
  39. __leave; // Strong name signed - can't change it.
  40. }
  41. }
  42. pData = ImageDirectoryEntryToData(LoadedImage.MappedAddress,
  43. FALSE,
  44. IMAGE_DIRECTORY_ENTRY_DEBUG,
  45. &DirCnt
  46. );
  47. if (!DebugDirectoryIsUseful(pData, DirCnt)) {
  48. __leave; // No debug data to change.
  49. }
  50. DebugDirs = (IMAGE_DEBUG_DIRECTORY UNALIGNED *)pData;
  51. DirCnt /= sizeof(IMAGE_DEBUG_DIRECTORY);
  52. CvDebugDir = NULL;
  53. while (DirCnt) {
  54. DirCnt--;
  55. if (DebugDirs[DirCnt].Type == IMAGE_DEBUG_TYPE_CODEVIEW) {
  56. CvDebugDir = &DebugDirs[DirCnt];
  57. break;
  58. }
  59. }
  60. if (!CvDebugDir) {
  61. __leave; // No CV debug data.
  62. }
  63. if (CvDebugDir->PointerToRawData != 0) {
  64. PCVDD pDebugDir;
  65. pDebugDir = (PCVDD) (CvDebugDir->PointerToRawData + (PCHAR)LoadedImage.MappedAddress);
  66. if (pDebugDir->dwSig == '01BN' || pDebugDir->dwSig == 'SDSR' ) {
  67. // Got a PDB. The name immediately follows the signature.
  68. LPSTR szMyDllToLoad;
  69. CHAR PdbName[sizeof(((PRSDSI)(0))->szPdb)];
  70. CHAR Filename[_MAX_FNAME];
  71. CHAR FileExt[_MAX_EXT];
  72. if (pDebugDir->dwSig == '01BN' ) {
  73. mysize=sizeof(NB10IH);
  74. } else {
  75. mysize=sizeof(RSDSIH);
  76. }
  77. if (mysize < CvDebugDir->SizeOfData) { // make sure there's enough space to work with
  78. ZeroMemory(PdbName, sizeof(PdbName));
  79. memcpy(PdbName, ((PCHAR)pDebugDir) + mysize, __min(CvDebugDir->SizeOfData - mysize, sizeof(PdbName) - 1));
  80. _splitpath(PdbName, NULL, NULL, Filename, FileExt);
  81. ZeroMemory( ((char *)pDebugDir) + mysize, CvDebugDir->SizeOfData - mysize); // zero the old record
  82. StringCbCopy(((char *)pDebugDir) + mysize, CvDebugDir->SizeOfData - mysize, Filename);
  83. StringCbCat( ((char *)pDebugDir) + mysize, CvDebugDir->SizeOfData - mysize, FileExt );
  84. CvDebugDir->SizeOfData = mysize + strlen( ((char *)pDebugDir) + mysize) + 1;
  85. } else {
  86. __leave;
  87. }
  88. }
  89. rc = TRUE;
  90. }
  91. } __finally {
  92. UnMapAndLoad(&LoadedImage);
  93. }
  94. return(rc);
  95. }