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.

192 lines
5.4 KiB

  1. #define DEFINE_STRCONST
  2. #define INITGUID
  3. #define INC_OLE2
  4. #include <windows.h>
  5. #include <initguid.h>
  6. #include <mimeole.h>
  7. #undef OE5_BETA2
  8. #include <msoeapi.h>
  9. #include "main.h"
  10. #include "stdio.h"
  11. static TCHAR c_szHr[] = "<HR>",
  12. c_szPT_Open[] = "<XMP>",
  13. c_szPT_Close[] = "</XMP>";
  14. HRESULT ExtractFolder(IStoreFolder *pFolder, LPSTR pszFileName);
  15. HRESULT FindFolder(LPSTR pszFolder, IStoreFolder **ppFolder);
  16. HRESULT HrCopyStream(LPSTREAM pstmIn, LPSTREAM pstmOut, ULONG *pcb);
  17. void __cdecl main(int argc, char *argv[])
  18. {
  19. IStoreFolder *pFolder;
  20. HRESULT hr;
  21. LPSTR pszFolder,
  22. pszFileName;
  23. if (argc != 3)
  24. {
  25. printf( "Usage: oedump <foldername> <filename>\n"
  26. " - converts a store folder to HTML and dumps to a file");
  27. return;
  28. }
  29. pszFolder = argv[1];
  30. pszFileName = argv[2];
  31. if (FAILED(OleInitialize(NULL)))
  32. {
  33. printf("CoInit failed\n\r");
  34. return;
  35. }
  36. hr = FindFolder(pszFolder, &pFolder);
  37. if (!FAILED(hr))
  38. {
  39. hr = ExtractFolder(pFolder, pszFileName);
  40. if (FAILED(hr))
  41. {
  42. printf(" - err: could not extract folder to '%s'", pszFileName);
  43. }
  44. pFolder->Release();
  45. }
  46. else
  47. {
  48. printf(" - err: could not find folder '%s'", pszFolder);
  49. }
  50. OleUninitialize();
  51. return;
  52. }
  53. HRESULT ExtractFolder(IStoreFolder *pFolder, LPSTR pszFileName)
  54. {
  55. HENUMSTORE hEnum;
  56. MESSAGEPROPS rProps;
  57. HRESULT hr;
  58. IMimeMessage *pMsg;
  59. IStream *pstmOut;
  60. IStream *pstm;
  61. rProps.cbSize = sizeof(MESSAGEPROPS);
  62. if (pFolder==NULL || pszFileName==NULL)
  63. return E_INVALIDARG;
  64. if (!FAILED(hr = MimeOleOpenFileStream(pszFileName, CREATE_ALWAYS, GENERIC_WRITE|GENERIC_READ, &pstmOut)))
  65. {
  66. if (pFolder->GetFirstMessage(0, 0, -1, &rProps, &hEnum)==S_OK)
  67. {
  68. do
  69. {
  70. if (!FAILED(pFolder->OpenMessage(rProps.dwMessageId, IID_IMimeMessage, (void **)&pMsg)))
  71. {
  72. if (!FAILED(pMsg->GetTextBody(TXT_HTML, IET_BINARY, &pstm, NULL)))
  73. {
  74. HrCopyStream(pstm, pstmOut, NULL);
  75. pstm->Release();
  76. }
  77. else
  78. {
  79. if (!FAILED(pMsg->GetTextBody(TXT_PLAIN, IET_BINARY, &pstm, NULL)))
  80. {
  81. // emit plaintext tags arounnd a non-html message
  82. pstmOut->Write(c_szPT_Open, lstrlen(c_szPT_Open), NULL);
  83. HrCopyStream(pstm, pstmOut, NULL);
  84. pstmOut->Write(c_szPT_Close, lstrlen(c_szPT_Close), NULL);
  85. pstm->Release();
  86. }
  87. }
  88. pstmOut->Write(c_szHr, lstrlen(c_szHr), NULL);
  89. // dump
  90. pMsg->Release();
  91. }
  92. rProps.cbSize = sizeof(MESSAGEPROPS);
  93. }
  94. while (pFolder->GetNextMessage(hEnum, 0, &rProps)==S_OK);
  95. pFolder->GetMessageClose(hEnum);
  96. }
  97. pstmOut->Commit(0);
  98. pstmOut->Release();
  99. }
  100. return hr;
  101. }
  102. HRESULT FindFolder(LPSTR pszFolder, IStoreFolder **ppFolder)
  103. {
  104. HRESULT hr;
  105. IStoreNamespace *pStore;
  106. FOLDERPROPS fp;
  107. HENUMSTORE hEnum;
  108. STOREFOLDERID dwFldr=0;
  109. *ppFolder = NULL;
  110. fp.cbSize = sizeof(FOLDERPROPS);
  111. hr = CoCreateInstance(CLSID_StoreNamespace, NULL, CLSCTX_INPROC_SERVER, IID_IStoreNamespace, (LPVOID*)&pStore);
  112. if (!FAILED(hr))
  113. {
  114. hr = pStore->Initialize(NULL, 0);
  115. if (!FAILED(hr))
  116. {
  117. if (!FAILED(pStore->GetFirstSubFolder(FOLDERID_ROOT, &fp, &hEnum)))
  118. {
  119. do
  120. {
  121. if (lstrcmpi(fp.szName, pszFolder)==0)
  122. {
  123. dwFldr = fp.dwFolderId;
  124. break;
  125. }
  126. fp.cbSize = sizeof(FOLDERPROPS); // msoeapi changes the size!
  127. }
  128. while (pStore->GetNextSubFolder(hEnum, &fp)==S_OK);
  129. pStore->GetSubFolderClose(hEnum);
  130. }
  131. if (dwFldr)
  132. pStore->OpenFolder(dwFldr, 0, ppFolder);
  133. }
  134. pStore->Release();
  135. }
  136. return (*ppFolder) ? S_OK : E_FAIL;
  137. }
  138. HRESULT HrCopyStream(LPSTREAM pstmIn, LPSTREAM pstmOut, ULONG *pcb)
  139. {
  140. // Locals
  141. HRESULT hr = S_OK;
  142. BYTE buf[4096];
  143. ULONG cbRead=0,
  144. cbTotal=0;
  145. do
  146. {
  147. if (pstmIn->Read(buf, sizeof(buf), &cbRead))
  148. goto exit;
  149. if (cbRead == 0) break;
  150. if (hr = pstmOut->Write(buf, cbRead, NULL))
  151. goto exit;
  152. cbTotal += cbRead;
  153. }
  154. while (cbRead == sizeof (buf));
  155. exit:
  156. if (pcb)
  157. *pcb = cbTotal;
  158. return hr;
  159. }