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.

194 lines
4.7 KiB

  1. #include "stdafx.h"
  2. #include "util.h"
  3. #include <strsafe.h>
  4. PCSTR FindFilePortion(PCSTR pszFile)
  5. {
  6. PCSTR psz = StrRChr(pszFile, '\\');
  7. if (psz)
  8. pszFile = psz + 1;
  9. psz = StrRChr(pszFile, '/');
  10. if (psz)
  11. return psz + 1;
  12. psz = StrRChr(pszFile, ':');
  13. return (psz ? psz + 1 : pszFile);
  14. }
  15. void reportOleError(HRESULT hr)
  16. {
  17. #ifdef _DEBUG
  18. char szTemp[100];
  19. StringCchPrintfA(szTemp, ARRAYSIZE(szTemp), "Ole error hresult %d\n",hr);
  20. MessageBox(NULL,szTemp,"Error",MB_OK);
  21. #endif
  22. }
  23. PSTR StrChr(PCSTR pszString, char ch)
  24. {
  25. while (*pszString) {
  26. while (IsDBCSLeadByte(*pszString))
  27. pszString = CharNextA(pszString);
  28. if (*pszString == ch)
  29. return (PSTR) pszString;
  30. else if (!*pszString)
  31. return NULL;
  32. pszString++;
  33. }
  34. return NULL;
  35. }
  36. PSTR StrRChr(PCSTR pszString, char ch)
  37. {
  38. PSTR psz = StrChr(pszString, ch);
  39. PSTR pszLast;
  40. if (!psz)
  41. return NULL;
  42. do {
  43. pszLast = psz;
  44. psz = StrChr(pszLast + 1, ch);
  45. } while (psz);
  46. return pszLast;
  47. }
  48. /***************************************************************************
  49. FUNCTION: HashFromSz
  50. PURPOSE: Convert a string into a hash representation
  51. PARAMETERS:
  52. pszKey
  53. RETURNS: Hash number
  54. COMMENTS:
  55. Shamelessly stolen from the WinHelp code, since after 6 years
  56. of use by up to 1 million help authors, there were no reports
  57. of collisions.
  58. MODIFICATION DATES:
  59. 10-Aug-1996 [ralphw] Stolen from WinHelp, removed special-case
  60. hash characters
  61. ***************************************************************************/
  62. // This constant defines the alphabet size for our hash function.
  63. static const HASH MAX_CHARS = 43;
  64. HASH WINAPI HashFromSz(PCSTR pszKey)
  65. {
  66. HASH hash = 0;
  67. int cch = strlen(pszKey);
  68. for (int ich = 0; ich < cch; ++ich) {
  69. // treat '/' and '\' as the same
  70. if (pszKey[ich] == '/')
  71. hash = (hash * MAX_CHARS) + ('\\' - '0');
  72. else if (pszKey[ich] <= 'Z')
  73. hash = (hash * MAX_CHARS) + (pszKey[ich] - '0');
  74. else
  75. hash = (hash * MAX_CHARS) + (pszKey[ich] - '0' - ('a' - 'A'));
  76. }
  77. /*
  78. * Since the value 0 is reserved as a nil value, if any context
  79. * string actually hashes to this value, we just move it.
  80. */
  81. return (hash == 0 ? 0 + 1 : hash);
  82. }
  83. CTitleInformation::CTitleInformation( CFileSystem* pFileSystem )
  84. {
  85. m_pFileSystem = pFileSystem;
  86. m_pszDefCaption = NULL;
  87. m_pszShortName = NULL;
  88. Initialize();
  89. }
  90. CTitleInformation::~CTitleInformation()
  91. {
  92. if(m_pszDefCaption)
  93. free((void *)m_pszDefCaption);
  94. if(m_pszShortName)
  95. free((void *)m_pszShortName);
  96. }
  97. HRESULT CTitleInformation::Initialize()
  98. {
  99. if( !m_pFileSystem )
  100. return S_FALSE;
  101. // open the title information file (#SYSTEM)
  102. CSubFileSystem* pSubFileSystem = new CSubFileSystem(m_pFileSystem); if(!pSubFileSystem) return E_FAIL;
  103. HRESULT hr = pSubFileSystem->OpenSub("#SYSTEM");
  104. if( FAILED(hr))
  105. return S_FALSE;
  106. // check the version of the title information file (#SYSTEM)
  107. DWORD dwVersion;
  108. DWORD cbRead;
  109. hr = pSubFileSystem->ReadSub(&dwVersion, sizeof(dwVersion), &cbRead);
  110. if( FAILED(hr) || cbRead != sizeof(dwVersion) )
  111. {
  112. delete pSubFileSystem;
  113. return STG_E_READFAULT;
  114. }
  115. // read in each and every item (skip those tags we don't care about)
  116. SYSTEM_TAG tag;
  117. for(;;) {
  118. // get the tag type
  119. hr = pSubFileSystem->ReadSub(&tag, sizeof(SYSTEM_TAG), &cbRead);
  120. if( FAILED(hr) || cbRead != sizeof(SYSTEM_TAG))
  121. break;
  122. // handle each tag according to it's type
  123. switch( tag.tag ) {
  124. // where all of our simple settings are stored
  125. case TAG_SHORT_NAME:
  126. m_pszShortName = (PCSTR) malloc(tag.cbTag);
  127. hr = pSubFileSystem->ReadSub((void*) m_pszShortName, tag.cbTag, &cbRead);
  128. break;
  129. case TAG_DEFAULT_CAPTION:
  130. m_pszDefCaption = (PCSTR) malloc(tag.cbTag);
  131. hr = pSubFileSystem->ReadSub((void*) m_pszDefCaption, tag.cbTag, &cbRead);
  132. break;
  133. // skip those we don't care about or don't know about
  134. default:
  135. hr = pSubFileSystem->SeekSub( tag.cbTag, SEEK_CUR );
  136. break;
  137. }
  138. if( FAILED(hr) ) {
  139. delete pSubFileSystem;
  140. return STG_E_READFAULT;
  141. }
  142. }
  143. delete pSubFileSystem;
  144. return S_OK;
  145. }