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.

177 lines
4.1 KiB

  1. //
  2. // MODULE: CHMREAD.CPP
  3. //
  4. // PURPOSE: Template file decoder
  5. //
  6. // PROJECT: Generic Troubleshooter DLL for Microsoft AnswerPoint
  7. //
  8. // COMPANY: Saltmine Creative, Inc. (206)-633-4743 [email protected]
  9. //
  10. // AUTHOR: Oleg Kalosha
  11. //
  12. // ORIGINAL DATE: 07.01.98
  13. //
  14. //
  15. #include "stdafx.h"
  16. #include "fs.h"
  17. #include "apgts.h"
  18. #include "chmread.h"
  19. #define CHM_READ_BUFFER_INITIAL_SIZE 1024
  20. // reads ALL stream (ppBuffer is reallocated)
  21. // IN: szStreamName - file name within CHM file
  22. // IN: szFileName - full oath and name of CHM file
  23. // OUT: ppBuffer - data read
  24. HRESULT ReadChmFile(LPCTSTR szFileName, LPCTSTR szStreamName, void** ppBuffer, DWORD* pdwRead)
  25. {
  26. HRESULT hr =S_OK;
  27. CFileSystem* pFileSystem =NULL;
  28. CSubFileSystem* pSubFileSystem =NULL;
  29. int i =0;
  30. // Init ITSS.
  31. pFileSystem = new CFileSystem();
  32. if (! SUCCEEDED((hr = pFileSystem->Init())) )
  33. {
  34. delete pFileSystem;
  35. return hr; // Unable to init the ITSS store.
  36. }
  37. // attempt to open the .CHM file.
  38. if (! SUCCEEDED((hr = pFileSystem->Open(szFileName))) )
  39. {
  40. delete pFileSystem;
  41. return hr; // Unable to open and init the ITSS store.
  42. }
  43. while (true, ++i)
  44. {
  45. ULONG read =0;
  46. // attempt to open the stream.
  47. pSubFileSystem = new CSubFileSystem(pFileSystem);
  48. if (! SUCCEEDED((hr = pSubFileSystem->OpenSub(szStreamName))) )
  49. {
  50. delete pSubFileSystem;
  51. delete pFileSystem;
  52. return hr; // Unable to open the specified stream.
  53. }
  54. // alloc
  55. *ppBuffer = new char[i * CHM_READ_BUFFER_INITIAL_SIZE];
  56. if (*ppBuffer == NULL)
  57. {
  58. delete pSubFileSystem;
  59. delete pFileSystem;
  60. return S_FALSE; // THOUGH need to return error indicating disability to allocate memory
  61. }
  62. // read.
  63. hr = pSubFileSystem->ReadSub(*ppBuffer, i * CHM_READ_BUFFER_INITIAL_SIZE, &read);
  64. if (read < ULONG(i * CHM_READ_BUFFER_INITIAL_SIZE))
  65. {
  66. *pdwRead = read;
  67. break;
  68. }
  69. else
  70. {
  71. delete pSubFileSystem;
  72. delete [] *ppBuffer;
  73. }
  74. }
  75. delete pSubFileSystem;
  76. delete pFileSystem;
  77. return hr;
  78. }
  79. bool GetNetworkRelatedResourceDirFromReg(CString network, CString* path)
  80. {
  81. HKEY hKey = 0;
  82. CString sub_key = CString(TSREGKEY_TL) + "\\" + network;
  83. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  84. sub_key,
  85. NULL,
  86. KEY_READ,
  87. &hKey))
  88. {
  89. DWORD dwType = REG_SZ;
  90. TCHAR buf[MAXBUF] = {_T('\0')};
  91. DWORD dwSize = MAXBUF - 1;
  92. if (ERROR_SUCCESS == RegQueryValueEx(hKey,
  93. FRIENDLY_PATH,
  94. NULL,
  95. &dwType,
  96. (LPBYTE)buf,
  97. &dwSize))
  98. {
  99. *path = buf;
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. bool IsNetworkRelatedResourceDirCHM(CString path)
  106. {
  107. path.TrimRight();
  108. CString extension = path.Right(4 * sizeof(TCHAR));
  109. return 0 == extension.CompareNoCase(CHM_DEFAULT);
  110. }
  111. CString ExtractResourceDir(CString path)
  112. {
  113. // For example, from string
  114. // "d:\TShooter Projects\TShootLocal\http\lan_chm\lan.chm"
  115. // need to extract "d:\TShooter Projects\TShootLocal\http\lan_chm"
  116. int index =0;
  117. CString strCHM = ExtractCHM(path);
  118. CString strRes;
  119. if (strCHM.GetLength())
  120. {
  121. index = path.Find(strCHM);
  122. if (index > 0 && path.GetAt(index-sizeof(_T('\\'))) == _T('\\'))
  123. strRes = path.Left(index-sizeof(_T('\\')));
  124. else if (index == 0)
  125. strRes = "";
  126. }
  127. return strRes;
  128. }
  129. CString ExtractFileName(CString path)
  130. {
  131. // Extracts file name (with extension)
  132. int index =0;
  133. if (-1 == (index = path.ReverseFind(_T('\\'))))
  134. {
  135. if (-1 == (index = path.ReverseFind(_T(':'))))
  136. index = 0;
  137. else
  138. index += sizeof(_T(':'));
  139. }
  140. else
  141. index += sizeof(_T('\\'));
  142. return (LPCTSTR)path + index;
  143. }
  144. CString ExtractCHM(CString path)
  145. {
  146. // For example, from string
  147. // "d:\TShooter Projects\TShootLocal\http\lan_chm\lan.chm"
  148. // need to extract "lan.chm"
  149. if (!IsNetworkRelatedResourceDirCHM(path))
  150. return "";
  151. return ExtractFileName(path);
  152. }