"Fossies" - the Fresh Open Source Software Archive

Member "unrar/list.cpp" (4 May 2022, 15357 Bytes) of package /linux/misc/unrarsrc-6.1.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "list.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 6.1.2_vs_6.1.3.

    1 #include "rar.hpp"
    2 
    3 static void ListFileHeader(Archive &Arc,FileHeader &hd,bool &TitleShown,bool Verbose,bool Technical,bool Bare,bool DisableNames);
    4 static void ListSymLink(Archive &Arc);
    5 static void ListFileAttr(uint A,HOST_SYSTEM_TYPE HostType,wchar *AttrStr,size_t AttrSize);
    6 static void ListOldSubHeader(Archive &Arc);
    7 static void ListNewSubHeader(CommandData *Cmd,Archive &Arc);
    8 
    9 void ListArchive(CommandData *Cmd)
   10 {
   11   int64 SumPackSize=0,SumUnpSize=0;
   12   uint ArcCount=0,SumFileCount=0;
   13   bool Technical=(Cmd->Command[1]=='T');
   14   bool ShowService=Technical && Cmd->Command[2]=='A';
   15   bool Bare=(Cmd->Command[1]=='B');
   16   bool Verbose=(Cmd->Command[0]=='V');
   17 
   18   wchar ArcName[NM];
   19   while (Cmd->GetArcName(ArcName,ASIZE(ArcName)))
   20   {
   21     if (Cmd->ManualPassword)
   22       Cmd->Password.Clean(); // Clean user entered password before processing next archive.
   23 
   24     Archive Arc(Cmd);
   25     if (!Arc.WOpen(ArcName))
   26       continue;
   27     bool FileMatched=true;
   28     while (true)
   29     {
   30       int64 TotalPackSize=0,TotalUnpSize=0;
   31       uint FileCount=0;
   32       if (Arc.IsArchive(true))
   33       {
   34         bool TitleShown=false;
   35         if (!Bare)
   36         {
   37           Arc.ViewComment();
   38           mprintf(L"\n%s: %s",St(MListArchive),Arc.FileName);
   39           mprintf(L"\n%s: ",St(MListDetails));
   40           uint SetCount=0;
   41           const wchar *Fmt=Arc.Format==RARFMT14 ? L"RAR 1.4":(Arc.Format==RARFMT15 ? L"RAR 4":L"RAR 5");
   42           mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", Fmt);
   43           if (Arc.Solid)
   44             mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListSolid));
   45           if (Arc.SFXSize>0)
   46             mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListSFX));
   47           if (Arc.Volume)
   48             if (Arc.Format==RARFMT50)
   49             {
   50               // RAR 5.0 archives store the volume number in main header,
   51               // so it is already available now.
   52               if (SetCount++ > 0)
   53                 mprintf(L", ");
   54               mprintf(St(MVolumeNumber),Arc.VolNumber+1);
   55             }
   56             else
   57               mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListVolume));
   58           if (Arc.Protected)
   59             mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListRR));
   60           if (Arc.Locked)
   61             mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListLock));
   62           if (Arc.Encrypted)
   63             mprintf(L"%s%s", SetCount++ > 0 ? L", ":L"", St(MListEncHead));
   64           mprintf(L"\n");
   65         }
   66 
   67         wchar VolNumText[50];
   68         *VolNumText=0;
   69         while (Arc.ReadHeader()>0)
   70         {
   71           Wait(); // Allow quit listing with Ctrl+C.
   72           HEADER_TYPE HeaderType=Arc.GetHeaderType();
   73           if (HeaderType==HEAD_ENDARC)
   74           {
   75 #ifndef SFX_MODULE
   76             // Only RAR 1.5 archives store the volume number in end record.
   77             if (Arc.EndArcHead.StoreVolNumber && Arc.Format==RARFMT15)
   78               swprintf(VolNumText,ASIZE(VolNumText),L"%.10ls %u",St(MListVolume),Arc.VolNumber+1);
   79 #endif
   80             if (Technical && ShowService)
   81             {
   82               mprintf(L"\n%12ls: %ls",St(MListService),L"EOF");
   83               if (*VolNumText!=0)
   84                 mprintf(L"\n%12ls: %ls",St(MListFlags),VolNumText);
   85               mprintf(L"\n");
   86             }
   87             break;
   88           }
   89           switch(HeaderType)
   90           {
   91             case HEAD_FILE:
   92               FileMatched=Cmd->IsProcessFile(Arc.FileHead,NULL,MATCH_WILDSUBPATH,0,NULL,0)!=0;
   93               if (FileMatched)
   94               {
   95                 ListFileHeader(Arc,Arc.FileHead,TitleShown,Verbose,Technical,Bare,Cmd->DisableNames);
   96                 if (!Arc.FileHead.SplitBefore)
   97                 {
   98                   TotalUnpSize+=Arc.FileHead.UnpSize;
   99                   FileCount++;
  100                 }
  101                 TotalPackSize+=Arc.FileHead.PackSize;
  102               }
  103               break;
  104             case HEAD_SERVICE:
  105               if (FileMatched && !Bare)
  106               {
  107                 if (Technical && ShowService)
  108                   ListFileHeader(Arc,Arc.SubHead,TitleShown,Verbose,true,false,Cmd->DisableNames);
  109               }
  110               break;
  111           }
  112           Arc.SeekToNext();
  113         }
  114         if (!Bare && !Technical)
  115           if (TitleShown)
  116           {
  117             wchar UnpSizeText[20];
  118             itoa(TotalUnpSize,UnpSizeText,ASIZE(UnpSizeText));
  119         
  120             wchar PackSizeText[20];
  121             itoa(TotalPackSize,PackSizeText,ASIZE(PackSizeText));
  122         
  123             if (Verbose)
  124             {
  125               mprintf(L"\n----------- ---------  -------- ----- ---------- -----  --------  ----");
  126               mprintf(L"\n%21ls %9ls %3d%%  %-27ls %u",UnpSizeText,
  127                       PackSizeText,ToPercentUnlim(TotalPackSize,TotalUnpSize),
  128                       VolNumText,FileCount);
  129             }
  130             else
  131             {
  132               mprintf(L"\n----------- ---------  ---------- -----  ----");
  133               mprintf(L"\n%21ls  %-16ls  %u",UnpSizeText,VolNumText,FileCount);
  134             }
  135 
  136             SumFileCount+=FileCount;
  137             SumUnpSize+=TotalUnpSize;
  138             SumPackSize+=TotalPackSize;
  139             mprintf(L"\n");
  140           }
  141           else
  142             mprintf(St(MListNoFiles));
  143 
  144         ArcCount++;
  145 
  146 #ifndef NOVOLUME
  147         if (Cmd->VolSize!=0 && (Arc.FileHead.SplitAfter ||
  148             Arc.GetHeaderType()==HEAD_ENDARC && Arc.EndArcHead.NextVolume) &&
  149             MergeArchive(Arc,NULL,false,Cmd->Command[0]))
  150           Arc.Seek(0,SEEK_SET);
  151         else
  152 #endif
  153           break;
  154       }
  155       else
  156       {
  157         if (Cmd->ArcNames.ItemsCount()<2 && !Bare)
  158           mprintf(St(MNotRAR),Arc.FileName);
  159         break;
  160       }
  161     }
  162   }
  163 
  164   // Clean user entered password. Not really required, just for extra safety.
  165   if (Cmd->ManualPassword)
  166     Cmd->Password.Clean();
  167 
  168   if (ArcCount>1 && !Bare && !Technical)
  169   {
  170     wchar UnpSizeText[20],PackSizeText[20];
  171     itoa(SumUnpSize,UnpSizeText,ASIZE(UnpSizeText));
  172     itoa(SumPackSize,PackSizeText,ASIZE(PackSizeText));
  173 
  174     if (Verbose)
  175       mprintf(L"%21ls %9ls %3d%% %28ls %u",UnpSizeText,PackSizeText,
  176               ToPercentUnlim(SumPackSize,SumUnpSize),L"",SumFileCount);
  177     else
  178       mprintf(L"%21ls %18s %lu",UnpSizeText,L"",SumFileCount);
  179   }
  180 }
  181 
  182 
  183 enum LISTCOL_TYPE {
  184   LCOL_NAME,LCOL_ATTR,LCOL_SIZE,LCOL_PACKED,LCOL_RATIO,LCOL_CSUM,LCOL_ENCR
  185 };
  186 
  187 
  188 void ListFileHeader(Archive &Arc,FileHeader &hd,bool &TitleShown,bool Verbose,bool Technical,bool Bare,bool DisableNames)
  189 {
  190   if (!TitleShown && !Technical && !Bare)
  191   {
  192     if (Verbose)
  193     {
  194       mprintf(L"\n%ls",St(MListTitleV));
  195       if (!DisableNames)
  196         mprintf(L"\n----------- ---------  -------- ----- ---------- -----  --------  ----");
  197     }
  198     else
  199     {
  200       mprintf(L"\n%ls",St(MListTitleL));
  201       if (!DisableNames)
  202         mprintf(L"\n----------- ---------  ---------- -----  ----");
  203     }
  204     // Must be set even in DisableNames mode to suppress "0 files" output
  205     // unless no files are matched.
  206     TitleShown=true;
  207   }
  208   if (DisableNames)
  209     return;
  210 
  211   wchar *Name=hd.FileName;
  212   RARFORMAT Format=Arc.Format;
  213 
  214   if (Bare)
  215   {
  216     mprintf(L"%s\n",Name);
  217     return;
  218   }
  219 
  220   wchar UnpSizeText[30],PackSizeText[30];
  221   if (hd.UnpSize==INT64NDF)
  222     wcsncpyz(UnpSizeText,L"?",ASIZE(UnpSizeText));
  223   else
  224     itoa(hd.UnpSize,UnpSizeText,ASIZE(UnpSizeText));
  225   itoa(hd.PackSize,PackSizeText,ASIZE(PackSizeText));
  226 
  227   wchar AttrStr[30];
  228   if (hd.HeaderType==HEAD_SERVICE)
  229     swprintf(AttrStr,ASIZE(AttrStr),L"%cB",hd.Inherited ? 'I' : '.');
  230   else
  231     ListFileAttr(hd.FileAttr,hd.HSType,AttrStr,ASIZE(AttrStr));
  232 
  233   wchar RatioStr[10];
  234 
  235   if (hd.SplitBefore && hd.SplitAfter)
  236     wcsncpyz(RatioStr,L"<->",ASIZE(RatioStr));
  237   else
  238     if (hd.SplitBefore)
  239       wcsncpyz(RatioStr,L"<--",ASIZE(RatioStr));
  240     else
  241       if (hd.SplitAfter)
  242         wcsncpyz(RatioStr,L"-->",ASIZE(RatioStr));
  243       else
  244         swprintf(RatioStr,ASIZE(RatioStr),L"%d%%",ToPercentUnlim(hd.PackSize,hd.UnpSize));
  245 
  246   wchar DateStr[50];
  247   hd.mtime.GetText(DateStr,ASIZE(DateStr),Technical);
  248 
  249   if (Technical)
  250   {
  251     mprintf(L"\n%12s: %s",St(MListName),Name);
  252 
  253     bool FileBlock=hd.HeaderType==HEAD_FILE;
  254 
  255     if (!FileBlock && Arc.SubHead.CmpName(SUBHEAD_TYPE_STREAM))
  256     {
  257       mprintf(L"\n%12ls: %ls",St(MListType),St(MListStream));
  258       wchar StreamName[NM];
  259       GetStreamNameNTFS(Arc,StreamName,ASIZE(StreamName));
  260       mprintf(L"\n%12ls: %ls",St(MListTarget),StreamName);
  261     }
  262     else
  263     {
  264       const wchar *Type=St(FileBlock ? (hd.Dir ? MListDir:MListFile):MListService);
  265     
  266       if (hd.RedirType!=FSREDIR_NONE)
  267         switch(hd.RedirType)
  268         {
  269           case FSREDIR_UNIXSYMLINK:
  270             Type=St(MListUSymlink); break;
  271           case FSREDIR_WINSYMLINK:
  272             Type=St(MListWSymlink); break;
  273           case FSREDIR_JUNCTION:
  274             Type=St(MListJunction); break;
  275           case FSREDIR_HARDLINK:
  276             Type=St(MListHardlink); break;
  277           case FSREDIR_FILECOPY:
  278             Type=St(MListCopy);     break;
  279         }
  280       mprintf(L"\n%12ls: %ls",St(MListType),Type);
  281       if (hd.RedirType!=FSREDIR_NONE)
  282         if (Format==RARFMT15)
  283         {
  284           char LinkTargetA[NM];
  285           if (Arc.FileHead.Encrypted)
  286           {
  287             // Link data are encrypted. We would need to ask for password
  288             // and initialize decryption routine to display the link target.
  289             strncpyz(LinkTargetA,"*<-?->",ASIZE(LinkTargetA));
  290           }
  291           else
  292           {
  293             int DataSize=(int)Min(hd.PackSize,ASIZE(LinkTargetA)-1);
  294             Arc.Read(LinkTargetA,DataSize);
  295             LinkTargetA[DataSize > 0 ? DataSize : 0] = 0;
  296           }
  297           wchar LinkTarget[NM];
  298           CharToWide(LinkTargetA,LinkTarget,ASIZE(LinkTarget));
  299           mprintf(L"\n%12ls: %ls",St(MListTarget),LinkTarget);
  300         }
  301         else
  302           mprintf(L"\n%12ls: %ls",St(MListTarget),hd.RedirName);
  303     }
  304     if (!hd.Dir)
  305     {
  306       mprintf(L"\n%12ls: %ls",St(MListSize),UnpSizeText);
  307       mprintf(L"\n%12ls: %ls",St(MListPacked),PackSizeText);
  308       mprintf(L"\n%12ls: %ls",St(MListRatio),RatioStr);
  309     }
  310     bool WinTitles=false;
  311 #ifdef _WIN_ALL
  312     WinTitles=true;
  313 #endif
  314     if (hd.mtime.IsSet())
  315       mprintf(L"\n%12ls: %ls",St(WinTitles ? MListModified:MListMtime),DateStr);
  316     if (hd.ctime.IsSet())
  317     {
  318       hd.ctime.GetText(DateStr,ASIZE(DateStr),true);
  319       mprintf(L"\n%12ls: %ls",St(WinTitles ? MListCreated:MListCtime),DateStr);
  320     }
  321     if (hd.atime.IsSet())
  322     {
  323       hd.atime.GetText(DateStr,ASIZE(DateStr),true);
  324       mprintf(L"\n%12ls: %ls",St(WinTitles ? MListAccessed:MListAtime),DateStr);
  325     }
  326     mprintf(L"\n%12ls: %ls",St(MListAttr),AttrStr);
  327     if (hd.FileHash.Type==HASH_CRC32)
  328       mprintf(L"\n%12ls: %8.8X",
  329         hd.UseHashKey ? L"CRC32 MAC":hd.SplitAfter ? L"Pack-CRC32":L"CRC32",
  330         hd.FileHash.CRC32);
  331     if (hd.FileHash.Type==HASH_BLAKE2)
  332     {
  333       wchar BlakeStr[BLAKE2_DIGEST_SIZE*2+1];
  334       BinToHex(hd.FileHash.Digest,BLAKE2_DIGEST_SIZE,NULL,BlakeStr,ASIZE(BlakeStr));
  335       mprintf(L"\n%12ls: %ls",
  336         hd.UseHashKey ? L"BLAKE2 MAC":hd.SplitAfter ? L"Pack-BLAKE2":L"BLAKE2",
  337         BlakeStr);
  338     }
  339 
  340     const wchar *HostOS=L"";
  341     if (Format==RARFMT50 && hd.HSType!=HSYS_UNKNOWN)
  342       HostOS=hd.HSType==HSYS_WINDOWS ? L"Windows":L"Unix";
  343     if (Format==RARFMT15)
  344     {
  345       static const wchar *RarOS[]={
  346         L"DOS",L"OS/2",L"Windows",L"Unix",L"Mac OS",L"BeOS",L"WinCE",L"",L"",L""
  347       };
  348       if (hd.HostOS<ASIZE(RarOS))
  349         HostOS=RarOS[hd.HostOS];
  350     }
  351     if (*HostOS!=0)
  352       mprintf(L"\n%12ls: %ls",St(MListHostOS),HostOS);
  353 
  354     mprintf(L"\n%12ls: RAR %ls(v%d) -m%d -md=%d%s",St(MListCompInfo),
  355             Format==RARFMT15 ? L"1.5":L"5.0",
  356             hd.UnpVer==VER_UNKNOWN ? 0 : hd.UnpVer,hd.Method,
  357             hd.WinSize>=0x100000 ? hd.WinSize/0x100000:hd.WinSize/0x400,
  358             hd.WinSize>=0x100000 ? L"M":L"K");
  359 
  360     if (hd.Solid || hd.Encrypted)
  361     {
  362       mprintf(L"\n%12ls: ",St(MListFlags));
  363       if (hd.Solid)
  364         mprintf(L"%ls ",St(MListSolid));
  365       if (hd.Encrypted)
  366         mprintf(L"%ls ",St(MListEnc));
  367     }
  368 
  369     if (hd.Version)
  370     {
  371       uint Version=ParseVersionFileName(Name,false);
  372       if (Version!=0)
  373         mprintf(L"\n%12ls: %u",St(MListFileVer),Version);
  374     }
  375 
  376     if (hd.UnixOwnerSet)
  377     {
  378       mprintf(L"\n%12ls: ",L"Unix owner");
  379       if (*hd.UnixOwnerName!=0)
  380         mprintf(L"%ls",GetWide(hd.UnixOwnerName));
  381       else
  382         if (hd.UnixOwnerNumeric)
  383           mprintf(L"#%d",hd.UnixOwnerID);
  384       mprintf(L":");
  385       if (*hd.UnixGroupName!=0)
  386         mprintf(L"%ls",GetWide(hd.UnixGroupName));
  387       else
  388         if (hd.UnixGroupNumeric)
  389           mprintf(L"#%d",hd.UnixGroupID);
  390     }
  391 
  392     mprintf(L"\n");
  393     return;
  394   }
  395 
  396   mprintf(L"\n%c%10ls %9ls ",hd.Encrypted ? '*' : ' ',AttrStr,UnpSizeText);
  397 
  398   if (Verbose)
  399     mprintf(L"%9ls %4ls ",PackSizeText,RatioStr);
  400 
  401   mprintf(L" %ls  ",DateStr);
  402 
  403   if (Verbose)
  404   {
  405     if (hd.FileHash.Type==HASH_CRC32)
  406       mprintf(L"%8.8X  ",hd.FileHash.CRC32);
  407     else
  408       if (hd.FileHash.Type==HASH_BLAKE2)
  409       {
  410         byte *S=hd.FileHash.Digest;
  411         mprintf(L"%02x%02x..%02x  ",S[0],S[1],S[31]);
  412       }
  413       else
  414         mprintf(L"????????  ");
  415   }
  416   mprintf(L"%ls",Name);
  417 }
  418 
  419 /*
  420 void ListSymLink(Archive &Arc)
  421 {
  422   if (Arc.FileHead.HSType==HSYS_UNIX && (Arc.FileHead.FileAttr & 0xF000)==0xA000)
  423     if (Arc.FileHead.Encrypted)
  424     {
  425       // Link data are encrypted. We would need to ask for password
  426       // and initialize decryption routine to display the link target.
  427       mprintf(L"\n%22ls %ls",L"-->",L"*<-?->");
  428     }
  429     else
  430     {
  431       char FileName[NM];
  432       uint DataSize=(uint)Min(Arc.FileHead.PackSize,sizeof(FileName)-1);
  433       Arc.Read(FileName,DataSize);
  434       FileName[DataSize]=0;
  435       mprintf(L"\n%22ls %ls",L"-->",GetWide(FileName));
  436     }
  437 }
  438 */
  439 
  440 void ListFileAttr(uint A,HOST_SYSTEM_TYPE HostType,wchar *AttrStr,size_t AttrSize)
  441 {
  442   switch(HostType)
  443   {
  444     case HSYS_WINDOWS:
  445       swprintf(AttrStr,AttrSize,L"%c%c%c%c%c%c%c",
  446               (A & 0x2000)!=0 ? 'I' : '.',  // Not content indexed.
  447               (A & 0x0800)!=0 ? 'C' : '.',  // Compressed.
  448               (A & 0x0020)!=0 ? 'A' : '.',  // Archive.
  449               (A & 0x0010)!=0 ? 'D' : '.',  // Directory.
  450               (A & 0x0004)!=0 ? 'S' : '.',  // System.
  451               (A & 0x0002)!=0 ? 'H' : '.',  // Hidden.
  452               (A & 0x0001)!=0 ? 'R' : '.'); // Read-only.
  453       break;
  454     case HSYS_UNIX:
  455       switch (A & 0xF000)
  456       {
  457         case 0x4000:
  458           AttrStr[0]='d';
  459           break;
  460         case 0xA000:
  461           AttrStr[0]='l';
  462           break;
  463         default:
  464           AttrStr[0]='-';
  465           break;
  466       }
  467       swprintf(AttrStr+1,AttrSize-1,L"%c%c%c%c%c%c%c%c%c",
  468               (A & 0x0100) ? 'r' : '-',
  469               (A & 0x0080) ? 'w' : '-',
  470               (A & 0x0040) ? ((A & 0x0800)!=0 ? 's':'x'):((A & 0x0800)!=0 ? 'S':'-'),
  471               (A & 0x0020) ? 'r' : '-',
  472               (A & 0x0010) ? 'w' : '-',
  473               (A & 0x0008) ? ((A & 0x0400)!=0 ? 's':'x'):((A & 0x0400)!=0 ? 'S':'-'),
  474               (A & 0x0004) ? 'r' : '-',
  475               (A & 0x0002) ? 'w' : '-',
  476               (A & 0x0001) ? ((A & 0x200)!=0 ? 't' : 'x') : '-');
  477       break;
  478     case HSYS_UNKNOWN:
  479       wcsncpyz(AttrStr,L"?",AttrSize);
  480       break;
  481   }
  482 }