home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac / wdj0796.zip / BURKOLE.ZIP / COMDUMP.C < prev   
C/C++ Source or Header  |  1996-05-03  |  3KB  |  116 lines

  1. #include <stdio.h>
  2. #include <stdlib.h> // exit()
  3. #include <objbase.h>
  4.  
  5. static void A2U(const char* In, wchar_t* Out)    {
  6.     int Len = (int)strlen(In)+1;
  7.     MultiByteToWideChar(CP_ACP, 0, In, Len, Out, Len*2);
  8.     }
  9. static void U2A(wchar_t* In, char* Out)    {
  10.     int Len = lstrlenW(In)+1;
  11.     WideCharToMultiByte(CP_ACP, 0, In, Len, Out, Len, 0, 0);
  12.     }
  13.  
  14. void    DumpStream(IStream* File, int Indent=0) {
  15.     ULONG           BytesRead;
  16.     unsigned char   Buffer[8];
  17.     int             Offset = 0;
  18.  
  19.     while(File->Read(Buffer, 8, &BytesRead) == S_OK
  20.         && BytesRead > 0)   {
  21.         printf("%*s0x%04X ", Indent, "", Offset);
  22.         for(int i=0; i < BytesRead; ++i)
  23.             printf("%02X ", (unsigned int)(Buffer[i]));
  24.         printf("\n");
  25.         Offset  += 16;
  26.         }
  27.     }
  28.  
  29. void    DumpStorage(IStorage* Dir, int Indent=0)  {
  30.     HRESULT         Code;
  31.     IEnumSTATSTG*   Enum;
  32.     STATSTG         Info;
  33.     IStream*        Stream;
  34.     
  35.     Dir->EnumElements(0, 0, 0, &Enum);
  36.     while(Enum->Next(1, &Info, 0) == S_OK)
  37.         {
  38.         // convert, print out info about this file or dir
  39.         char    Name[256];
  40.         U2A(Info.pwcsName, Name);
  41.         printf("%*s/%s\n", Indent, "", Name);
  42.         
  43.         // if it's a storage, then open it and recurse
  44.         if(Info.type == STGTY_STORAGE)
  45.             {
  46.             IStorage*   SubDir;
  47.  
  48.             HRESULT Code = Dir->OpenStorage(Info.pwcsName, 0,
  49.                 STGM_READ|STGM_SHARE_EXCLUSIVE,
  50.                 0, 0, &SubDir);
  51.             if(Code != S_OK)
  52.                 {
  53.                 fprintf(stderr, "Could not open storage "
  54.                     "'%s'\n", Name);
  55.                 exit(EXIT_FAILURE);
  56.                 }
  57.             else
  58.                 {
  59.                 DumpStorage(SubDir, Indent+4);
  60.                 SubDir->Release();
  61.                 }
  62.             }
  63.         // if it's a file, open and dump it
  64.         else if(Info.type == STGTY_STREAM)
  65.             {
  66.             IStream*    File;
  67.             
  68.             HRESULT Code = Dir->OpenStream(Info.pwcsName, 0,
  69.                 STGM_READ|STGM_SHARE_EXCLUSIVE,
  70.                 0, &File);
  71.             if(Code != S_OK)
  72.                 {
  73.                 fprintf(stderr, "Could not open stream "
  74.                     "'%s'\n", Name);
  75.                 exit(EXIT_FAILURE);
  76.                 }
  77.             else
  78.                 {
  79.                 DumpStream(File, Indent+4);
  80.                 File->Release();
  81.                 }
  82.             }
  83.         CoTaskMemFree(Info.pwcsName);
  84.         }
  85.     Enum->Release();
  86.     }
  87.  
  88.  
  89. void Usage(void)    {
  90.     fprintf(stderr, "Usage: comdump <filename>\n");
  91.     exit(EXIT_FAILURE);
  92.     }
  93.  
  94. void main(int argc, char** argv)    {
  95.     if(argc != 2)
  96.         Usage();
  97.     wchar_t     wPath[256];
  98.     IStorage*   Root;
  99.     HRESULT     Result;
  100.  
  101.     A2U(argv[1], wPath);
  102.     Result = StgOpenStorage(wPath, 0, STGM_READ
  103.         |STGM_SHARE_EXCLUSIVE, 0, 0, &Root);
  104.     if(Result != S_OK)
  105.         fprintf(stderr, "Could not open '%s' (error code %d)",
  106.             argv[1], Result);
  107.     else
  108.         {
  109.         printf("Dump of compound file '%s'\n", argv[1]);
  110.         DumpStorage(Root);
  111.         Root->Release();
  112.         }
  113.     exit(EXIT_SUCCESS);
  114.     }
  115.  
  116.