• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // HashCalc.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "../../../../C/Alloc.h"
6 #include "../../../../C/CpuArch.h"
7 
8 #include "../../../Common/DynLimBuf.h"
9 #include "../../../Common/IntToString.h"
10 #include "../../../Common/StringToInt.h"
11 
12 #include "../../Common/FileStreams.h"
13 #include "../../Common/ProgressUtils.h"
14 #include "../../Common/StreamObjects.h"
15 #include "../../Common/StreamUtils.h"
16 
17 #include "../../Archive/Common/ItemNameUtils.h"
18 #include "../../Archive/IArchive.h"
19 
20 #include "EnumDirItems.h"
21 #include "HashCalc.h"
22 
23 using namespace NWindows;
24 
25 #ifdef Z7_EXTERNAL_CODECS
26 extern const CExternalCodecs *g_ExternalCodecs_Ptr;
27 #endif
28 
29 class CHashMidBuf
30 {
31   void *_data;
32 public:
CHashMidBuf()33   CHashMidBuf(): _data(NULL) {}
operator void*()34   operator void *() { return _data; }
Alloc(size_t size)35   bool Alloc(size_t size)
36   {
37     if (_data)
38       return false;
39     _data = ::MidAlloc(size);
40     return _data != NULL;
41   }
~CHashMidBuf()42   ~CHashMidBuf() { ::MidFree(_data); }
43 };
44 
45 static const char * const k_DefaultHashMethod = "CRC32";
46 
SetMethods(DECL_EXTERNAL_CODECS_LOC_VARS const UStringVector & hashMethods)47 HRESULT CHashBundle::SetMethods(DECL_EXTERNAL_CODECS_LOC_VARS const UStringVector &hashMethods)
48 {
49   UStringVector names = hashMethods;
50   if (names.IsEmpty())
51     names.Add(UString(k_DefaultHashMethod));
52 
53   CRecordVector<CMethodId> ids;
54   CObjectVector<COneMethodInfo> methods;
55 
56   unsigned i;
57   for (i = 0; i < names.Size(); i++)
58   {
59     COneMethodInfo m;
60     RINOK(m.ParseMethodFromString(names[i]))
61 
62     if (m.MethodName.IsEmpty())
63       m.MethodName = k_DefaultHashMethod;
64 
65     if (m.MethodName == "*")
66     {
67       CRecordVector<CMethodId> tempMethods;
68       GetHashMethods(EXTERNAL_CODECS_LOC_VARS tempMethods);
69       methods.Clear();
70       ids.Clear();
71       FOR_VECTOR (t, tempMethods)
72       {
73         unsigned index = ids.AddToUniqueSorted(tempMethods[t]);
74         if (ids.Size() != methods.Size())
75           methods.Insert(index, m);
76       }
77       break;
78     }
79     else
80     {
81       // m.MethodName.RemoveChar(L'-');
82       CMethodId id;
83       if (!FindHashMethod(EXTERNAL_CODECS_LOC_VARS m.MethodName, id))
84         return E_NOTIMPL;
85       unsigned index = ids.AddToUniqueSorted(id);
86       if (ids.Size() != methods.Size())
87         methods.Insert(index, m);
88     }
89   }
90 
91   for (i = 0; i < ids.Size(); i++)
92   {
93     CMyComPtr<IHasher> hasher;
94     AString name;
95     RINOK(CreateHasher(EXTERNAL_CODECS_LOC_VARS ids[i], name, hasher))
96     if (!hasher)
97       throw "Can't create hasher";
98     const COneMethodInfo &m = methods[i];
99     {
100       CMyComPtr<ICompressSetCoderProperties> scp;
101       hasher.QueryInterface(IID_ICompressSetCoderProperties, &scp);
102       if (scp)
103         RINOK(m.SetCoderProps(scp, NULL))
104     }
105     const UInt32 digestSize = hasher->GetDigestSize();
106     if (digestSize > k_HashCalc_DigestSize_Max)
107       return E_NOTIMPL;
108     CHasherState &h = Hashers.AddNew();
109     h.DigestSize = digestSize;
110     h.Hasher = hasher;
111     h.Name = name;
112     for (unsigned k = 0; k < k_HashCalc_NumGroups; k++)
113       h.InitDigestGroup(k);
114   }
115 
116   return S_OK;
117 }
118 
InitForNewFile()119 void CHashBundle::InitForNewFile()
120 {
121   CurSize = 0;
122   FOR_VECTOR (i, Hashers)
123   {
124     CHasherState &h = Hashers[i];
125     h.Hasher->Init();
126     h.InitDigestGroup(k_HashCalc_Index_Current);
127   }
128 }
129 
Update(const void * data,UInt32 size)130 void CHashBundle::Update(const void *data, UInt32 size)
131 {
132   CurSize += size;
133   FOR_VECTOR (i, Hashers)
134     Hashers[i].Hasher->Update(data, size);
135 }
136 
SetSize(UInt64 size)137 void CHashBundle::SetSize(UInt64 size)
138 {
139   CurSize = size;
140 }
141 
AddDigests(Byte * dest,const Byte * src,UInt32 size)142 static void AddDigests(Byte *dest, const Byte *src, UInt32 size)
143 {
144   unsigned next = 0;
145   /*
146   // we could use big-endian addition for sha-1 and sha-256
147   // but another hashers are little-endian
148   if (size > 8)
149   {
150     for (unsigned i = size; i != 0;)
151     {
152       i--;
153       next += (unsigned)dest[i] + (unsigned)src[i];
154       dest[i] = (Byte)next;
155       next >>= 8;
156     }
157   }
158   else
159   */
160   {
161     for (unsigned i = 0; i < size; i++)
162     {
163       next += (unsigned)dest[i] + (unsigned)src[i];
164       dest[i] = (Byte)next;
165       next >>= 8;
166     }
167   }
168 
169   // we use little-endian to store extra bytes
170   dest += k_HashCalc_DigestSize_Max;
171   for (unsigned i = 0; i < k_HashCalc_ExtraSize; i++)
172   {
173     next += (unsigned)dest[i];
174     dest[i] = (Byte)next;
175     next >>= 8;
176   }
177 }
178 
AddDigest(unsigned groupIndex,const Byte * data)179 void CHasherState::AddDigest(unsigned groupIndex, const Byte *data)
180 {
181   NumSums[groupIndex]++;
182   AddDigests(Digests[groupIndex], data, DigestSize);
183 }
184 
Final(bool isDir,bool isAltStream,const UString & path)185 void CHashBundle::Final(bool isDir, bool isAltStream, const UString &path)
186 {
187   if (isDir)
188     NumDirs++;
189   else if (isAltStream)
190   {
191     NumAltStreams++;
192     AltStreamsSize += CurSize;
193   }
194   else
195   {
196     NumFiles++;
197     FilesSize += CurSize;
198   }
199 
200   Byte pre[16];
201   memset(pre, 0, sizeof(pre));
202   if (isDir)
203     pre[0] = 1;
204 
205   FOR_VECTOR (i, Hashers)
206   {
207     CHasherState &h = Hashers[i];
208     if (!isDir)
209     {
210       h.Hasher->Final(h.Digests[0]); // k_HashCalc_Index_Current
211       if (!isAltStream)
212         h.AddDigest(k_HashCalc_Index_DataSum, h.Digests[0]);
213     }
214 
215     h.Hasher->Init();
216     h.Hasher->Update(pre, sizeof(pre));
217     h.Hasher->Update(h.Digests[0], h.DigestSize);
218 
219     for (unsigned k = 0; k < path.Len(); k++)
220     {
221       wchar_t c = path[k];
222 
223       // 21.04: we want same hash for linux and windows paths
224       #if CHAR_PATH_SEPARATOR != '/'
225       if (c == CHAR_PATH_SEPARATOR)
226         c = '/';
227       // if (c == (wchar_t)('\\' + 0xf000)) c = '\\'; // to debug WSL
228       // if (c > 0xf000 && c < 0xf080) c -= 0xf000; // to debug WSL
229       #endif
230 
231       Byte temp[2] = { (Byte)(c & 0xFF), (Byte)((c >> 8) & 0xFF) };
232       h.Hasher->Update(temp, 2);
233     }
234 
235     Byte tempDigest[k_HashCalc_DigestSize_Max];
236     h.Hasher->Final(tempDigest);
237     if (!isAltStream)
238       h.AddDigest(k_HashCalc_Index_NamesSum, tempDigest);
239     h.AddDigest(k_HashCalc_Index_StreamsSum, tempDigest);
240   }
241 }
242 
243 
CSum_Name_OriginalToEscape(const AString & src,AString & dest)244 static void CSum_Name_OriginalToEscape(const AString &src, AString &dest)
245 {
246   dest.Empty();
247   for (unsigned i = 0; i < src.Len();)
248   {
249     char c = src[i++];
250     if (c == '\n')
251     {
252       dest.Add_Char('\\');
253       c = 'n';
254     }
255     else if (c == '\\')
256       dest.Add_Char('\\');
257     dest.Add_Char(c);
258   }
259 }
260 
261 
CSum_Name_EscapeToOriginal(const char * s,AString & dest)262 static bool CSum_Name_EscapeToOriginal(const char *s, AString &dest)
263 {
264   bool isOK = true;
265   dest.Empty();
266   for (;;)
267   {
268     char c = *s++;
269     if (c == 0)
270       break;
271     if (c == '\\')
272     {
273       const char c1 = *s;
274       if (c1 == 'n')
275       {
276         c = '\n';
277         s++;
278       }
279       else if (c1 == '\\')
280       {
281         c = c1;
282         s++;
283       }
284       else
285       {
286         // original md5sum returns NULL for such bad strings
287         isOK = false;
288       }
289     }
290     dest.Add_Char(c);
291   }
292   return isOK;
293 }
294 
295 
296 
SetSpacesAndNul(char * s,unsigned num)297 static void SetSpacesAndNul(char *s, unsigned num)
298 {
299   for (unsigned i = 0; i < num; i++)
300     s[i] = ' ';
301   s[num] = 0;
302 }
303 
304 static const unsigned kHashColumnWidth_Min = 4 * 2;
305 
GetColumnWidth(unsigned digestSize)306 static unsigned GetColumnWidth(unsigned digestSize)
307 {
308   const unsigned width = digestSize * 2;
309   return width < kHashColumnWidth_Min ? kHashColumnWidth_Min: width;
310 }
311 
312 
AddHashResultLine(AString & _s,const CObjectVector<CHasherState> & hashers)313 static void AddHashResultLine(
314     AString &_s,
315     // bool showHash,
316     // UInt64 fileSize, bool showSize,
317     const CObjectVector<CHasherState> &hashers
318     // unsigned digestIndex, = k_HashCalc_Index_Current
319     )
320 {
321   FOR_VECTOR (i, hashers)
322   {
323     const CHasherState &h = hashers[i];
324     char s[k_HashCalc_DigestSize_Max * 2 + 64];
325     s[0] = 0;
326     // if (showHash)
327       HashHexToString(s, h.Digests[k_HashCalc_Index_Current], h.DigestSize);
328     const unsigned pos = (unsigned)strlen(s);
329     const int numSpaces = (int)GetColumnWidth(h.DigestSize) - (int)pos;
330     if (numSpaces > 0)
331       SetSpacesAndNul(s + pos, (unsigned)numSpaces);
332     if (i != 0)
333       _s.Add_Space();
334     _s += s;
335   }
336 
337   /*
338   if (showSize)
339   {
340     _s.Add_Space();
341     static const unsigned kSizeField_Len = 13; // same as in HashCon.cpp
342     char s[kSizeField_Len + 32];
343     char *p = s;
344     SetSpacesAndNul(s, kSizeField_Len);
345     p = s + kSizeField_Len;
346     ConvertUInt64ToString(fileSize, p);
347     int numSpaces = (int)kSizeField_Len - (int)strlen(p);
348     if (numSpaces > 0)
349       p -= (unsigned)numSpaces;
350     _s += p;
351   }
352   */
353 }
354 
355 
Add_LF(CDynLimBuf & hashFileString,const CHashOptionsLocal & options)356 static void Add_LF(CDynLimBuf &hashFileString, const CHashOptionsLocal &options)
357 {
358   hashFileString += (char)(options.HashMode_Zero.Val ? 0 : '\n');
359 }
360 
361 
362 
363 
WriteLine(CDynLimBuf & hashFileString,const CHashOptionsLocal & options,const UString & path2,bool isDir,const AString & methodName,const AString & hashesString)364 static void WriteLine(CDynLimBuf &hashFileString,
365     const CHashOptionsLocal &options,
366     const UString &path2,
367     bool isDir,
368     const AString &methodName,
369     const AString &hashesString)
370 {
371   if (options.HashMode_OnlyHash.Val)
372   {
373     hashFileString += hashesString;
374     Add_LF(hashFileString, options);
375     return;
376   }
377 
378   UString path = path2;
379 
380   bool isBin = false;
381   const bool zeroMode = options.HashMode_Zero.Val;
382   const bool tagMode = options.HashMode_Tag.Val;
383 
384 #if CHAR_PATH_SEPARATOR != '/'
385   path.Replace(WCHAR_PATH_SEPARATOR, L'/');
386   // path.Replace((wchar_t)('\\' + 0xf000), L'\\'); // to debug WSL
387 #endif
388 
389   AString utf8;
390   ConvertUnicodeToUTF8(path, utf8);
391 
392   AString esc;
393   CSum_Name_OriginalToEscape(utf8, esc);
394 
395   if (!zeroMode)
396   {
397     if (esc != utf8)
398     {
399       /* Original md5sum writes escape in that case.
400       We do same for compatibility with original md5sum. */
401       hashFileString += '\\';
402     }
403   }
404 
405   if (isDir && !esc.IsEmpty() && esc.Back() != '/')
406     esc.Add_Slash();
407 
408   if (tagMode)
409   {
410     if (!methodName.IsEmpty())
411     {
412       hashFileString += methodName;
413       hashFileString += ' ';
414     }
415     hashFileString += '(';
416     hashFileString += esc;
417     hashFileString += ')';
418     hashFileString += " = ";
419   }
420 
421   hashFileString += hashesString;
422 
423   if (!tagMode)
424   {
425     hashFileString += ' ';
426     hashFileString += (char)(isBin ? '*' : ' ');
427     hashFileString += esc;
428   }
429 
430   Add_LF(hashFileString, options);
431 }
432 
433 
434 
WriteLine(CDynLimBuf & hashFileString,const CHashOptionsLocal & options,const UString & path,bool isDir,const CHashBundle & hb)435 static void WriteLine(CDynLimBuf &hashFileString,
436     const CHashOptionsLocal &options,
437     const UString &path,
438     bool isDir,
439     const CHashBundle &hb)
440 {
441   AString methodName;
442   if (!hb.Hashers.IsEmpty())
443     methodName = hb.Hashers[0].Name;
444 
445   AString hashesString;
446   AddHashResultLine(hashesString, hb.Hashers);
447   WriteLine(hashFileString, options, path, isDir, methodName, hashesString);
448 }
449 
450 
HashCalc(DECL_EXTERNAL_CODECS_LOC_VARS const NWildcard::CCensor & censor,const CHashOptions & options,AString & errorInfo,IHashCallbackUI * callback)451 HRESULT HashCalc(
452     DECL_EXTERNAL_CODECS_LOC_VARS
453     const NWildcard::CCensor &censor,
454     const CHashOptions &options,
455     AString &errorInfo,
456     IHashCallbackUI *callback)
457 {
458   CDirItems dirItems;
459   dirItems.Callback = callback;
460 
461   if (options.StdInMode)
462   {
463     CDirItem di;
464     if (!di.SetAs_StdInFile())
465       return GetLastError_noZero_HRESULT();
466     dirItems.Items.Add(di);
467   }
468   else
469   {
470     RINOK(callback->StartScanning())
471 
472     dirItems.SymLinks = options.SymLinks.Val;
473     dirItems.ScanAltStreams = options.AltStreamsMode;
474     dirItems.ExcludeDirItems = censor.ExcludeDirItems;
475     dirItems.ExcludeFileItems = censor.ExcludeFileItems;
476 
477     dirItems.ShareForWrite = options.OpenShareForWrite;
478 
479     HRESULT res = EnumerateItems(censor,
480         options.PathMode,
481         UString(),
482         dirItems);
483 
484     if (res != S_OK)
485     {
486       if (res != E_ABORT)
487         errorInfo = "Scanning error";
488       return res;
489     }
490     RINOK(callback->FinishScanning(dirItems.Stat))
491   }
492 
493   unsigned i;
494   CHashBundle hb;
495   RINOK(hb.SetMethods(EXTERNAL_CODECS_LOC_VARS options.Methods))
496   // hb.Init();
497 
498   hb.NumErrors = dirItems.Stat.NumErrors;
499 
500   UInt64 totalSize = 0;
501   if (options.StdInMode)
502   {
503     RINOK(callback->SetNumFiles(1))
504   }
505   else
506   {
507     totalSize = dirItems.Stat.GetTotalBytes();
508     RINOK(callback->SetTotal(totalSize))
509   }
510 
511   const UInt32 kBufSize = 1 << 15;
512   CHashMidBuf buf;
513   if (!buf.Alloc(kBufSize))
514     return E_OUTOFMEMORY;
515 
516   UInt64 completeValue = 0;
517 
518   RINOK(callback->BeforeFirstFile(hb))
519 
520   /*
521   CDynLimBuf hashFileString((size_t)1 << 31);
522   const bool needGenerate = !options.HashFilePath.IsEmpty();
523   */
524 
525   for (i = 0; i < dirItems.Items.Size(); i++)
526   {
527     CMyComPtr<ISequentialInStream> inStream;
528     UString path;
529     bool isDir = false;
530     bool isAltStream = false;
531 
532     if (options.StdInMode)
533     {
534 #if 1
535       inStream = new CStdInFileStream;
536 #else
537       if (!CreateStdInStream(inStream))
538       {
539         const DWORD lastError = ::GetLastError();
540         const HRESULT res = callback->OpenFileError(FString("stdin"), lastError);
541         hb.NumErrors++;
542         if (res != S_FALSE && res != S_OK)
543           return res;
544         continue;
545       }
546 #endif
547     }
548     else
549     {
550       path = dirItems.GetLogPath(i);
551       const CDirItem &di = dirItems.Items[i];
552      #ifdef _WIN32
553       isAltStream = di.IsAltStream;
554      #endif
555 
556       #ifndef UNDER_CE
557       // if (di.AreReparseData())
558       if (di.ReparseData.Size() != 0)
559       {
560         CBufInStream *inStreamSpec = new CBufInStream();
561         inStream = inStreamSpec;
562         inStreamSpec->Init(di.ReparseData, di.ReparseData.Size());
563       }
564       else
565       #endif
566       {
567         CInFileStream *inStreamSpec = new CInFileStream;
568         inStreamSpec->Set_PreserveATime(options.PreserveATime);
569         inStream = inStreamSpec;
570         isDir = di.IsDir();
571         if (!isDir)
572         {
573           const FString phyPath = dirItems.GetPhyPath(i);
574           if (!inStreamSpec->OpenShared(phyPath, options.OpenShareForWrite))
575           {
576             const HRESULT res = callback->OpenFileError(phyPath, ::GetLastError());
577             hb.NumErrors++;
578             if (res != S_FALSE)
579               return res;
580             continue;
581           }
582           if (!options.StdInMode)
583           {
584             UInt64 curSize = 0;
585             if (inStreamSpec->GetSize(&curSize) == S_OK)
586             {
587               if (curSize > di.Size)
588               {
589                 totalSize += curSize - di.Size;
590                 RINOK(callback->SetTotal(totalSize))
591                 // printf("\ntotal = %d MiB\n", (unsigned)(totalSize >> 20));
592               }
593             }
594           }
595           // inStreamSpec->ReloadProps();
596         }
597       }
598     }
599 
600     RINOK(callback->GetStream(path, isDir))
601     UInt64 fileSize = 0;
602 
603     hb.InitForNewFile();
604 
605     if (!isDir)
606     {
607       for (UInt32 step = 0;; step++)
608       {
609         if ((step & 0xFF) == 0)
610         {
611           // printf("\ncompl = %d\n", (unsigned)(completeValue >> 20));
612           RINOK(callback->SetCompleted(&completeValue))
613         }
614         UInt32 size;
615         RINOK(inStream->Read(buf, kBufSize, &size))
616         if (size == 0)
617           break;
618         hb.Update(buf, size);
619         fileSize += size;
620         completeValue += size;
621       }
622     }
623 
624     hb.Final(isDir, isAltStream, path);
625 
626     /*
627     if (needGenerate
628         && (options.HashMode_Dirs.Val || !isDir))
629     {
630       WriteLine(hashFileString,
631           options,
632           path, // change it
633           isDir,
634           hb);
635 
636       if (hashFileString.IsError())
637         return E_OUTOFMEMORY;
638     }
639     */
640 
641     RINOK(callback->SetOperationResult(fileSize, hb, !isDir))
642     RINOK(callback->SetCompleted(&completeValue))
643   }
644 
645   /*
646   if (needGenerate)
647   {
648     NFile::NIO::COutFile file;
649     if (!file.Create(us2fs(options.HashFilePath), true)) // createAlways
650       return GetLastError_noZero_HRESULT();
651     if (!file.WriteFull(hashFileString, hashFileString.Len()))
652       return GetLastError_noZero_HRESULT();
653   }
654   */
655 
656   return callback->AfterLastFile(hb);
657 }
658 
659 
HashHexToString(char * dest,const Byte * data,size_t size)660 void HashHexToString(char *dest, const Byte *data, size_t size)
661 {
662   if (!data)
663   {
664     for (size_t i = 0; i < size; i++)
665     {
666       dest[0] = ' ';
667       dest[1] = ' ';
668       dest += 2;
669     }
670     *dest = 0;
671     return;
672   }
673 
674   if (size > 8)
675     ConvertDataToHex_Lower(dest, data, size);
676   else if (size == 0)
677   {
678     *dest = 0;
679     return;
680   }
681   else
682   {
683     const char *dest_start = dest;
684     dest += size * 2;
685     *dest = 0;
686     do
687     {
688       const size_t b = *data++;
689       dest -= 2;
690       dest[0] = GET_HEX_CHAR_UPPER(b >> 4);
691       dest[1] = GET_HEX_CHAR_UPPER(b & 15);
692     }
693     while (dest != dest_start);
694   }
695 }
696 
WriteToString(unsigned digestIndex,char * s) const697 void CHasherState::WriteToString(unsigned digestIndex, char *s) const
698 {
699   HashHexToString(s, Digests[digestIndex], DigestSize);
700 
701   if (digestIndex != 0 && NumSums[digestIndex] != 1)
702   {
703     unsigned numExtraBytes = GetNumExtraBytes_for_Group(digestIndex);
704     if (numExtraBytes > 4)
705       numExtraBytes = 8;
706     else // if (numExtraBytes >= 0)
707       numExtraBytes = 4;
708     // if (numExtraBytes != 0)
709     {
710       s += strlen(s);
711       *s++ = '-';
712       // *s = 0;
713       HashHexToString(s, GetExtraData_for_Group(digestIndex), numExtraBytes);
714     }
715   }
716 }
717 
718 
719 
720 // ---------- Hash Handler ----------
721 
722 namespace NHash {
723 
724 #define IsWhite(c) ((c) == ' ' || (c) == '\t')
725 
IsDir() const726 bool CHashPair::IsDir() const
727 {
728   if (Name.IsEmpty() || Name.Back() != '/')
729     return false;
730   // here we expect that Dir items contain only zeros or no Hash
731   for (size_t i = 0; i < Hash.Size(); i++)
732     if (Hash.ConstData()[i] != 0)
733       return false;
734   return true;
735 }
736 
737 
ParseCksum(const char * s)738 bool CHashPair::ParseCksum(const char *s)
739 {
740   const char *end;
741 
742   const UInt32 crc = ConvertStringToUInt32(s, &end);
743   if (*end != ' ')
744     return false;
745   end++;
746 
747   const UInt64 size = ConvertStringToUInt64(end, &end);
748   if (*end != ' ')
749     return false;
750   end++;
751 
752   Name = end;
753 
754   Hash.Alloc(4);
755   SetBe32(Hash, crc)
756 
757   Size_from_Arc = size;
758   Size_from_Arc_Defined = true;
759 
760   return true;
761 }
762 
763 
764 
SkipWhite(const char * s)765 static const char *SkipWhite(const char *s)
766 {
767   while (IsWhite(*s))
768     s++;
769   return s;
770 }
771 
772 static const char * const k_CsumMethodNames[] =
773 {
774     "sha256"
775   , "sha224"
776 //  , "sha512/224"
777 //  , "sha512/256"
778   , "sha512"
779   , "sha384"
780   , "sha1"
781   , "md5"
782   , "blake2b"
783   , "crc64"
784   , "crc32"
785   , "cksum"
786 };
787 
GetMethod_from_FileName(const UString & name)788 static UString GetMethod_from_FileName(const UString &name)
789 {
790   AString s;
791   ConvertUnicodeToUTF8(name, s);
792   const int dotPos = s.ReverseFind_Dot();
793   const char *src = s.Ptr();
794   bool isExtension = false;
795   if (dotPos >= 0)
796   {
797     isExtension = true;
798     src = s.Ptr(dotPos + 1);
799   }
800   const char *m = "";
801   unsigned i;
802   for (i = 0; i < Z7_ARRAY_SIZE(k_CsumMethodNames); i++)
803   {
804     m = k_CsumMethodNames[i];
805     if (isExtension)
806     {
807       if (StringsAreEqual_Ascii(src, m))
808         break;
809     }
810     else if (IsString1PrefixedByString2_NoCase_Ascii(src, m))
811       if (StringsAreEqual_Ascii(src + strlen(m), "sums"))
812         break;
813   }
814   UString res;
815   if (i != Z7_ARRAY_SIZE(k_CsumMethodNames))
816     res = m;
817   return res;
818 }
819 
820 
Parse(const char * s)821 bool CHashPair::Parse(const char *s)
822 {
823   // here we keep compatibility with original md5sum / shasum
824   bool escape = false;
825 
826   s = SkipWhite(s);
827 
828   if (*s == '\\')
829   {
830     s++;
831     escape = true;
832   }
833   Escape = escape;
834 
835   // const char *kMethod = GetMethod_from_FileName(s);
836   // if (kMethod)
837   if ((size_t)(FindNonHexChar(s) - s) < 4)
838   {
839     // BSD-style checksum line
840     {
841       const char *s2 = s;
842       for (; *s2 != 0; s2++)
843       {
844         const char c = *s2;
845         if (c == 0)
846           return false;
847         if (c == ' ' || c == '(')
848           break;
849       }
850       Method.SetFrom(s, (unsigned)(s2 - s));
851       s = s2;
852     }
853     IsBSD = true;
854     if (*s == ' ')
855       s++;
856     if (*s != '(')
857       return false;
858     s++;
859     {
860       const char *s2 = s;
861       for (; *s2 != 0; s2++)
862       {}
863       for (;;)
864       {
865         s2--;
866         if (s2 < s)
867           return false;
868         if (*s2 == ')')
869           break;
870       }
871       Name.SetFrom(s, (unsigned)(s2 - s));
872       s = s2 + 1;
873     }
874 
875     s = SkipWhite(s);
876     if (*s != '=')
877       return false;
878     s++;
879     s = SkipWhite(s);
880   }
881 
882   {
883     const size_t numChars = (size_t)(FindNonHexChar(s) - s) & ~(size_t)1;
884     Hash.Alloc(numChars / 2);
885     if ((size_t)(ParseHexString(s, Hash) - Hash) != numChars / 2)
886       throw 101;
887     HashString.SetFrom(s, (unsigned)numChars);
888     s += numChars;
889   }
890 
891   if (IsBSD)
892   {
893     if (*s != 0)
894       return false;
895     if (escape)
896     {
897       const AString temp (Name);
898       return CSum_Name_EscapeToOriginal(temp, Name);
899     }
900     return true;
901   }
902 
903   if (*s == 0)
904     return true;
905 
906   if (*s != ' ')
907     return false;
908   s++;
909   const char c = *s;
910   if (c != ' '
911       && c != '*'
912       && c != 'U' // shasum Universal
913       && c != '^' // shasum 0/1
914      )
915     return false;
916   Mode = c;
917   s++;
918   if (escape)
919     return CSum_Name_EscapeToOriginal(s, Name);
920   Name = s;
921   return true;
922 }
923 
924 
GetLine(CByteBuffer & buf,bool zeroMode,bool cr_lf_Mode,size_t & posCur,AString & s)925 static bool GetLine(CByteBuffer &buf, bool zeroMode, bool cr_lf_Mode, size_t &posCur, AString &s)
926 {
927   s.Empty();
928   size_t pos = posCur;
929   const Byte *p = buf;
930   unsigned numDigits = 0;
931   for (; pos < buf.Size(); pos++)
932   {
933     const Byte b = p[pos];
934     if (b == 0)
935     {
936       numDigits = 1;
937       break;
938     }
939     if (zeroMode)
940       continue;
941     if (b == 0x0a)
942     {
943       numDigits = 1;
944       break;
945     }
946     if (!cr_lf_Mode)
947       continue;
948     if (b == 0x0d)
949     {
950       if (pos + 1 >= buf.Size())
951       {
952         numDigits = 1;
953         break;
954         // return false;
955       }
956       if (p[pos + 1] == 0x0a)
957       {
958         numDigits = 2;
959         break;
960       }
961     }
962   }
963   s.SetFrom((const char *)(p + posCur), (unsigned)(pos - posCur));
964   posCur = pos + numDigits;
965   return true;
966 }
967 
968 
Is_CR_LF_Data(const Byte * buf,size_t size)969 static bool Is_CR_LF_Data(const Byte *buf, size_t size)
970 {
971   bool isCrLf = false;
972   for (size_t i = 0; i < size;)
973   {
974     const Byte b = buf[i];
975     if (b == 0x0a)
976       return false;
977     if (b == 0x0d)
978     {
979       if (i == size - 1)
980         return false;
981       if (buf[i + 1] != 0x0a)
982         return false;
983       isCrLf = true;
984       i += 2;
985     }
986     else
987       i++;
988   }
989   return isCrLf;
990 }
991 
992 
993 static const Byte kArcProps[] =
994 {
995   // kpidComment,
996   kpidCharacts
997 };
998 
999 static const Byte kProps[] =
1000 {
1001   kpidPath,
1002   kpidSize,
1003   kpidPackSize,
1004   kpidMethod
1005 };
1006 
1007 static const Byte kRawProps[] =
1008 {
1009   kpidChecksum
1010 };
1011 
1012 
Z7_COM7F_IMF(CHandler::GetParent (UInt32,UInt32 * parent,UInt32 * parentType))1013 Z7_COM7F_IMF(CHandler::GetParent(UInt32 /* index */ , UInt32 *parent, UInt32 *parentType))
1014 {
1015   *parentType = NParentType::kDir;
1016   *parent = (UInt32)(Int32)-1;
1017   return S_OK;
1018 }
1019 
Z7_COM7F_IMF(CHandler::GetNumRawProps (UInt32 * numProps))1020 Z7_COM7F_IMF(CHandler::GetNumRawProps(UInt32 *numProps))
1021 {
1022   *numProps = Z7_ARRAY_SIZE(kRawProps);
1023   return S_OK;
1024 }
1025 
Z7_COM7F_IMF(CHandler::GetRawPropInfo (UInt32 index,BSTR * name,PROPID * propID))1026 Z7_COM7F_IMF(CHandler::GetRawPropInfo(UInt32 index, BSTR *name, PROPID *propID))
1027 {
1028   *propID = kRawProps[index];
1029   *name = NULL;
1030   return S_OK;
1031 }
1032 
Z7_COM7F_IMF(CHandler::GetRawProp (UInt32 index,PROPID propID,const void ** data,UInt32 * dataSize,UInt32 * propType))1033 Z7_COM7F_IMF(CHandler::GetRawProp(UInt32 index, PROPID propID, const void **data, UInt32 *dataSize, UInt32 *propType))
1034 {
1035   *data = NULL;
1036   *dataSize = 0;
1037   *propType = 0;
1038 
1039   if (propID == kpidChecksum)
1040   {
1041     const CHashPair &hp = HashPairs[index];
1042     if (hp.Hash.Size() > 0)
1043     {
1044       *data = hp.Hash;
1045       *dataSize = (UInt32)hp.Hash.Size();
1046       *propType = NPropDataType::kRaw;
1047     }
1048     return S_OK;
1049   }
1050 
1051   return S_OK;
1052 }
1053 
1054 IMP_IInArchive_Props
1055 IMP_IInArchive_ArcProps
1056 
Z7_COM7F_IMF(CHandler::GetNumberOfItems (UInt32 * numItems))1057 Z7_COM7F_IMF(CHandler::GetNumberOfItems(UInt32 *numItems))
1058 {
1059   *numItems = HashPairs.Size();
1060   return S_OK;
1061 }
1062 
Add_OptSpace_String(UString & dest,const char * src)1063 static void Add_OptSpace_String(UString &dest, const char *src)
1064 {
1065   dest.Add_Space_if_NotEmpty();
1066   dest += src;
1067 }
1068 
Z7_COM7F_IMF(CHandler::GetArchiveProperty (PROPID propID,PROPVARIANT * value))1069 Z7_COM7F_IMF(CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value))
1070 {
1071   NCOM::CPropVariant prop;
1072   switch (propID)
1073   {
1074     case kpidPhySize: if (_phySize != 0) prop = _phySize; break;
1075     /*
1076     case kpidErrorFlags:
1077     {
1078       UInt32 v = 0;
1079       if (!_isArc) v |= kpv_ErrorFlags_IsNotArc;
1080       // if (_sres == k_Base64_RES_NeedMoreInput) v |= kpv_ErrorFlags_UnexpectedEnd;
1081       if (v != 0)
1082         prop = v;
1083       break;
1084     }
1085     */
1086     case kpidCharacts:
1087     {
1088       UString s;
1089       if (_hashSize_Defined)
1090       {
1091         s.Add_Space_if_NotEmpty();
1092         s.Add_UInt32(_hashSize * 8);
1093         s += "-bit";
1094       }
1095       if (!_nameExtenstion.IsEmpty())
1096       {
1097         s.Add_Space_if_NotEmpty();
1098         s += _nameExtenstion;
1099       }
1100       if (_is_PgpMethod)
1101       {
1102         Add_OptSpace_String(s, "PGP");
1103         if (!_pgpMethod.IsEmpty())
1104         {
1105           s.Add_Colon();
1106           s += _pgpMethod;
1107         }
1108       }
1109       if (_is_ZeroMode)
1110         Add_OptSpace_String(s, "ZERO");
1111       if (_are_there_Tags)
1112         Add_OptSpace_String(s, "TAG");
1113       if (_are_there_Dirs)
1114         Add_OptSpace_String(s, "DIRS");
1115       prop = s;
1116       break;
1117     }
1118 
1119     case kpidReadOnly:
1120     {
1121       if (_isArc)
1122         if (!CanUpdate())
1123           prop = true;
1124       break;
1125     }
1126     default: break;
1127   }
1128   prop.Detach(value);
1129   return S_OK;
1130 }
1131 
1132 
Z7_COM7F_IMF(CHandler::GetProperty (UInt32 index,PROPID propID,PROPVARIANT * value))1133 Z7_COM7F_IMF(CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value))
1134 {
1135   // COM_TRY_BEGIN
1136   NCOM::CPropVariant prop;
1137   const CHashPair &hp = HashPairs[index];
1138   switch (propID)
1139   {
1140     case kpidIsDir:
1141     {
1142       prop = hp.IsDir();
1143       break;
1144     }
1145     case kpidPath:
1146     {
1147       UString path;
1148       hp.Get_UString_Path(path);
1149 
1150       bool useBackslashReplacement = true;
1151       if (_supportWindowsBackslash && !hp.Escape && path.Find(L"\\\\") < 0)
1152       {
1153 #if WCHAR_PATH_SEPARATOR == L'/'
1154         path.Replace(L'\\', L'/');
1155 #else
1156         useBackslashReplacement = false;
1157 #endif
1158       }
1159       NArchive::NItemName::ReplaceToOsSlashes_Remove_TailSlash(
1160           path, useBackslashReplacement);
1161       prop = path;
1162       break;
1163     }
1164     case kpidSize:
1165     {
1166       // client needs processed size of last file
1167       if (hp.Size_from_Disk_Defined)
1168         prop = (UInt64)hp.Size_from_Disk;
1169       else if (hp.Size_from_Arc_Defined)
1170         prop = (UInt64)hp.Size_from_Arc;
1171       break;
1172     }
1173     case kpidPackSize:
1174     {
1175       prop = (UInt64)hp.Hash.Size();
1176       break;
1177     }
1178     case kpidMethod:
1179     {
1180       if (!hp.Method.IsEmpty())
1181         prop = hp.Method;
1182       break;
1183     }
1184     default: break;
1185   }
1186   prop.Detach(value);
1187   return S_OK;
1188   // COM_TRY_END
1189 }
1190 
1191 
ReadStream_to_Buf(IInStream * stream,CByteBuffer & buf,IArchiveOpenCallback * openCallback)1192 static HRESULT ReadStream_to_Buf(IInStream *stream, CByteBuffer &buf, IArchiveOpenCallback *openCallback)
1193 {
1194   buf.Free();
1195   UInt64 len;
1196   RINOK(InStream_AtBegin_GetSize(stream, len))
1197   if (len == 0 || len >= ((UInt64)1 << 31))
1198     return S_FALSE;
1199   buf.Alloc((size_t)len);
1200   UInt64 pos = 0;
1201   // return ReadStream_FALSE(stream, buf, (size_t)len);
1202   for (;;)
1203   {
1204     const UInt32 kBlockSize = ((UInt32)1 << 24);
1205     const UInt32 curSize = (len < kBlockSize) ? (UInt32)len : kBlockSize;
1206     UInt32 processedSizeLoc;
1207     RINOK(stream->Read((Byte *)buf + pos, curSize, &processedSizeLoc))
1208     if (processedSizeLoc == 0)
1209       return E_FAIL;
1210     len -= processedSizeLoc;
1211     pos += processedSizeLoc;
1212     if (len == 0)
1213       return S_OK;
1214     if (openCallback)
1215     {
1216       const UInt64 files = 0;
1217       RINOK(openCallback->SetCompleted(&files, &pos))
1218     }
1219   }
1220 }
1221 
1222 
Z7_COM7F_IMF(CHandler::Open (IInStream * stream,const UInt64 *,IArchiveOpenCallback * openCallback))1223 Z7_COM7F_IMF(CHandler::Open(IInStream *stream, const UInt64 *, IArchiveOpenCallback *openCallback))
1224 {
1225   COM_TRY_BEGIN
1226   {
1227     Close();
1228 
1229     CByteBuffer buf;
1230     RINOK(ReadStream_to_Buf(stream, buf, openCallback))
1231 
1232     CObjectVector<CHashPair> &pairs = HashPairs;
1233 
1234     bool zeroMode = false;
1235     bool cr_lf_Mode = false;
1236     {
1237       for (size_t i = 0; i < buf.Size(); i++)
1238         if (buf.ConstData()[i] == 0)
1239         {
1240           zeroMode = true;
1241           break;
1242         }
1243     }
1244     _is_ZeroMode = zeroMode;
1245     if (!zeroMode)
1246       cr_lf_Mode = Is_CR_LF_Data(buf, buf.Size());
1247 
1248     if (openCallback)
1249     {
1250       Z7_DECL_CMyComPtr_QI_FROM(
1251           IArchiveOpenVolumeCallback,
1252           openVolumeCallback, openCallback)
1253       if (openVolumeCallback)
1254       {
1255         NCOM::CPropVariant prop;
1256         RINOK(openVolumeCallback->GetProperty(kpidName, &prop))
1257         if (prop.vt == VT_BSTR)
1258           _nameExtenstion = GetMethod_from_FileName(prop.bstrVal);
1259       }
1260     }
1261 
1262     bool cksumMode = false;
1263     if (_nameExtenstion.IsEqualTo_Ascii_NoCase("cksum"))
1264       cksumMode = true;
1265     _is_CksumMode = cksumMode;
1266 
1267     size_t pos = 0;
1268     AString s;
1269     bool minusMode = false;
1270     unsigned numLines = 0;
1271 
1272     while (pos < buf.Size())
1273     {
1274       if (!GetLine(buf, zeroMode, cr_lf_Mode, pos, s))
1275         return S_FALSE;
1276       numLines++;
1277       if (s.IsEmpty())
1278         continue;
1279 
1280       if (s.IsPrefixedBy_Ascii_NoCase("; "))
1281       {
1282         if (numLines != 1)
1283           return S_FALSE;
1284         // comment line of FileVerifier++
1285         continue;
1286       }
1287 
1288       if (s.IsPrefixedBy_Ascii_NoCase("-----"))
1289       {
1290         if (minusMode)
1291           break; // end of pgp mode
1292         minusMode = true;
1293         if (s.IsPrefixedBy_Ascii_NoCase("-----BEGIN PGP SIGNED MESSAGE"))
1294         {
1295           if (_is_PgpMethod)
1296             return S_FALSE;
1297           if (!GetLine(buf, zeroMode, cr_lf_Mode, pos, s))
1298             return S_FALSE;
1299           const char *kStart = "Hash: ";
1300           if (!s.IsPrefixedBy_Ascii_NoCase(kStart))
1301             return S_FALSE;
1302           _pgpMethod = s.Ptr((unsigned)strlen(kStart));
1303           _is_PgpMethod = true;
1304         }
1305         continue;
1306       }
1307 
1308       CHashPair pair;
1309       pair.FullLine = s;
1310       if (cksumMode)
1311       {
1312         if (!pair.ParseCksum(s))
1313           return S_FALSE;
1314       }
1315       else if (!pair.Parse(s))
1316         return S_FALSE;
1317       pairs.Add(pair);
1318     }
1319 
1320     {
1321       unsigned hashSize = 0;
1322       bool hashSize_Dismatch = false;
1323       for (unsigned i = 0; i < HashPairs.Size(); i++)
1324       {
1325         const CHashPair &hp = HashPairs[i];
1326         if (i == 0)
1327           hashSize = (unsigned)hp.Hash.Size();
1328         else
1329           if (hashSize != hp.Hash.Size())
1330             hashSize_Dismatch = true;
1331 
1332         if (hp.IsBSD)
1333           _are_there_Tags = true;
1334         if (!_are_there_Dirs && hp.IsDir())
1335           _are_there_Dirs = true;
1336       }
1337       if (!hashSize_Dismatch && hashSize != 0)
1338       {
1339         _hashSize = hashSize;
1340         _hashSize_Defined = true;
1341       }
1342     }
1343 
1344     _phySize = buf.Size();
1345     _isArc = true;
1346     return S_OK;
1347   }
1348   COM_TRY_END
1349 }
1350 
1351 
ClearVars()1352 void CHandler::ClearVars()
1353 {
1354   _phySize = 0;
1355   _isArc = false;
1356   _is_CksumMode = false;
1357   _is_PgpMethod = false;
1358   _is_ZeroMode = false;
1359   _are_there_Tags = false;
1360   _are_there_Dirs = false;
1361   _hashSize_Defined = false;
1362   _hashSize = 0;
1363 }
1364 
1365 
Z7_COM7F_IMF(CHandler::Close ())1366 Z7_COM7F_IMF(CHandler::Close())
1367 {
1368   ClearVars();
1369   _nameExtenstion.Empty();
1370   _pgpMethod.Empty();
1371   HashPairs.Clear();
1372   return S_OK;
1373 }
1374 
1375 
CheckDigests(const Byte * a,const Byte * b,size_t size)1376 static bool CheckDigests(const Byte *a, const Byte *b, size_t size)
1377 {
1378   if (size <= 8)
1379   {
1380     /* we use reversed order for one digest, when text representation
1381        uses big-order for crc-32 and crc-64 */
1382     for (size_t i = 0; i < size; i++)
1383       if (a[i] != b[size - 1 - i])
1384         return false;
1385     return true;
1386   }
1387   {
1388     for (size_t i = 0; i < size; i++)
1389       if (a[i] != b[i])
1390         return false;
1391     return true;
1392   }
1393 }
1394 
1395 
AddDefaultMethod(UStringVector & methods,unsigned size)1396 static void AddDefaultMethod(UStringVector &methods, unsigned size)
1397 {
1398   const char *m = NULL;
1399        if (size == 32) m = "sha256";
1400   else if (size == 20) m = "sha1";
1401   else if (size == 16) m = "md5";
1402   else if (size ==  8) m = "crc64";
1403   else if (size ==  4) m = "crc32";
1404   else
1405     return;
1406   #ifdef Z7_EXTERNAL_CODECS
1407   const CExternalCodecs *_externalCodecs = g_ExternalCodecs_Ptr;
1408   #endif
1409   CMethodId id;
1410   if (FindHashMethod(EXTERNAL_CODECS_LOC_VARS
1411       AString(m), id))
1412     methods.Add(UString(m));
1413 }
1414 
1415 
Z7_COM7F_IMF(CHandler::Extract (const UInt32 * indices,UInt32 numItems,Int32 testMode,IArchiveExtractCallback * extractCallback))1416 Z7_COM7F_IMF(CHandler::Extract(const UInt32 *indices, UInt32 numItems,
1417     Int32 testMode, IArchiveExtractCallback *extractCallback))
1418 {
1419   COM_TRY_BEGIN
1420 
1421   /*
1422   if (testMode == 0)
1423     return E_NOTIMPL;
1424   */
1425 
1426   const bool allFilesMode = (numItems == (UInt32)(Int32)-1);
1427   if (allFilesMode)
1428     numItems = HashPairs.Size();
1429   if (numItems == 0)
1430     return S_OK;
1431 
1432   #ifdef Z7_EXTERNAL_CODECS
1433   const CExternalCodecs *_externalCodecs = g_ExternalCodecs_Ptr;
1434   #endif
1435 
1436   CHashBundle hb_Glob;
1437   // UStringVector methods = options.Methods;
1438   UStringVector methods;
1439 
1440   if (methods.IsEmpty() && !_nameExtenstion.IsEmpty())
1441   {
1442     AString utf;
1443     ConvertUnicodeToUTF8(_nameExtenstion, utf);
1444     CMethodId id;
1445     if (FindHashMethod(EXTERNAL_CODECS_LOC_VARS utf, id))
1446       methods.Add(_nameExtenstion);
1447   }
1448 
1449   if (methods.IsEmpty() && !_pgpMethod.IsEmpty())
1450   {
1451     CMethodId id;
1452     if (FindHashMethod(EXTERNAL_CODECS_LOC_VARS _pgpMethod, id))
1453       methods.Add(UString(_pgpMethod));
1454   }
1455 
1456   if (methods.IsEmpty() && _pgpMethod.IsEmpty() && _hashSize_Defined)
1457     AddDefaultMethod(methods, _hashSize);
1458 
1459   RINOK(hb_Glob.SetMethods(
1460       EXTERNAL_CODECS_LOC_VARS
1461       methods))
1462 
1463   Z7_DECL_CMyComPtr_QI_FROM(
1464       IArchiveUpdateCallbackFile,
1465       updateCallbackFile, extractCallback)
1466   if (!updateCallbackFile)
1467     return E_NOTIMPL;
1468   {
1469     Z7_DECL_CMyComPtr_QI_FROM(
1470         IArchiveGetDiskProperty,
1471         GetDiskProperty, extractCallback)
1472     if (GetDiskProperty)
1473     {
1474       UInt64 totalSize = 0;
1475       UInt32 i;
1476       for (i = 0; i < numItems; i++)
1477       {
1478         const UInt32 index = allFilesMode ? i : indices[i];
1479         const CHashPair &hp = HashPairs[index];
1480         if (hp.IsDir())
1481           continue;
1482         {
1483           NCOM::CPropVariant prop;
1484           RINOK(GetDiskProperty->GetDiskProperty(index, kpidSize, &prop))
1485           if (prop.vt != VT_UI8)
1486             continue;
1487           totalSize += prop.uhVal.QuadPart;
1488         }
1489       }
1490       RINOK(extractCallback->SetTotal(totalSize))
1491       // RINOK(Hash_SetTotalUnpacked->Hash_SetTotalUnpacked(indices, numItems));
1492     }
1493   }
1494 
1495   const UInt32 kBufSize = 1 << 15;
1496   CHashMidBuf buf;
1497   if (!buf.Alloc(kBufSize))
1498     return E_OUTOFMEMORY;
1499 
1500   CMyComPtr2_Create<ICompressProgressInfo, CLocalProgress> lps;
1501   lps->Init(extractCallback, false);
1502 
1503   for (UInt32 i = 0;; i++)
1504   {
1505     RINOK(lps->SetCur())
1506     if (i >= numItems)
1507       break;
1508     const UInt32 index = allFilesMode ? i : indices[i];
1509 
1510     CHashPair &hp = HashPairs[index];
1511 
1512     UString path;
1513     hp.Get_UString_Path(path);
1514 
1515     CMyComPtr<ISequentialInStream> inStream;
1516     const bool isDir = hp.IsDir();
1517     if (!isDir)
1518     {
1519       RINOK(updateCallbackFile->GetStream2(index, &inStream, NUpdateNotifyOp::kHashRead))
1520       if (!inStream)
1521       {
1522         continue; // we have shown error in GetStream2()
1523       }
1524       // askMode = NArchive::NExtract::NAskMode::kSkip;
1525     }
1526 
1527     Int32 askMode = testMode ?
1528         NArchive::NExtract::NAskMode::kTest :
1529         NArchive::NExtract::NAskMode::kExtract;
1530 
1531     CMyComPtr<ISequentialOutStream> realOutStream;
1532     RINOK(extractCallback->GetStream(index, &realOutStream, askMode))
1533 
1534     /* PrepareOperation() can expect kExtract to set
1535        Attrib and security of output file */
1536     askMode = NArchive::NExtract::NAskMode::kReadExternal;
1537 
1538     RINOK(extractCallback->PrepareOperation(askMode))
1539 
1540     const bool isAltStream = false;
1541 
1542     UInt64 fileSize = 0;
1543 
1544     CHashBundle hb_Loc;
1545 
1546     CHashBundle *hb_Use = &hb_Glob;
1547 
1548     HRESULT res_SetMethods = S_OK;
1549 
1550     UStringVector methods_loc;
1551 
1552     if (!hp.Method.IsEmpty())
1553     {
1554       hb_Use = &hb_Loc;
1555       CMethodId id;
1556       if (FindHashMethod(EXTERNAL_CODECS_LOC_VARS hp.Method, id))
1557       {
1558         methods_loc.Add(UString(hp.Method));
1559         RINOK(hb_Loc.SetMethods(
1560             EXTERNAL_CODECS_LOC_VARS
1561             methods_loc))
1562       }
1563       else
1564         res_SetMethods = E_NOTIMPL;
1565     }
1566     else if (methods.IsEmpty())
1567     {
1568       AddDefaultMethod(methods_loc, (unsigned)hp.Hash.Size());
1569       if (!methods_loc.IsEmpty())
1570       {
1571         hb_Use = &hb_Loc;
1572         RINOK(hb_Loc.SetMethods(
1573             EXTERNAL_CODECS_LOC_VARS
1574             methods_loc))
1575       }
1576     }
1577 
1578     const bool isSupportedMode = hp.IsSupportedMode();
1579     hb_Use->InitForNewFile();
1580 
1581     if (inStream)
1582     {
1583       for (UInt32 step = 0;; step++)
1584       {
1585         if ((step & 0xFF) == 0)
1586         {
1587           RINOK(lps.Interface()->SetRatioInfo(NULL, &fileSize))
1588         }
1589         UInt32 size;
1590         RINOK(inStream->Read(buf, kBufSize, &size))
1591         if (size == 0)
1592           break;
1593         hb_Use->Update(buf, size);
1594         if (realOutStream)
1595         {
1596           RINOK(WriteStream(realOutStream, buf, size))
1597         }
1598         fileSize += size;
1599       }
1600 
1601       hp.Size_from_Disk = fileSize;
1602       hp.Size_from_Disk_Defined = true;
1603     }
1604 
1605     realOutStream.Release();
1606     inStream.Release();
1607 
1608     lps->InSize += hp.Hash.Size();
1609     lps->OutSize += fileSize;
1610 
1611     hb_Use->Final(isDir, isAltStream, path);
1612 
1613     Int32 opRes = NArchive::NExtract::NOperationResult::kUnsupportedMethod;
1614     if (isSupportedMode
1615         && res_SetMethods != E_NOTIMPL
1616         && hb_Use->Hashers.Size() > 0
1617         )
1618     {
1619       const CHasherState &hs = hb_Use->Hashers[0];
1620       if (hs.DigestSize == hp.Hash.Size())
1621       {
1622         opRes = NArchive::NExtract::NOperationResult::kCRCError;
1623         if (CheckDigests(hp.Hash, hs.Digests[0], hs.DigestSize))
1624           if (!hp.Size_from_Arc_Defined || hp.Size_from_Arc == fileSize)
1625             opRes = NArchive::NExtract::NOperationResult::kOK;
1626       }
1627     }
1628 
1629     RINOK(extractCallback->SetOperationResult(opRes))
1630   }
1631 
1632   return S_OK;
1633   COM_TRY_END
1634 }
1635 
1636 
1637 // ---------- UPDATE ----------
1638 
1639 struct CUpdateItem
1640 {
1641   int IndexInArc;
1642   unsigned IndexInClient;
1643   UInt64 Size;
1644   bool NewData;
1645   bool NewProps;
1646   bool IsDir;
1647   UString Path;
1648 
CUpdateItemNHash::CUpdateItem1649   CUpdateItem(): Size(0), IsDir(false) {}
1650 };
1651 
1652 
GetPropString(IArchiveUpdateCallback * callback,UInt32 index,PROPID propId,UString & res,bool convertSlash)1653 static HRESULT GetPropString(IArchiveUpdateCallback *callback, UInt32 index, PROPID propId,
1654     UString &res,
1655     bool convertSlash)
1656 {
1657   NCOM::CPropVariant prop;
1658   RINOK(callback->GetProperty(index, propId, &prop))
1659   if (prop.vt == VT_BSTR)
1660   {
1661     res = prop.bstrVal;
1662     if (convertSlash)
1663       NArchive::NItemName::ReplaceSlashes_OsToUnix(res);
1664   }
1665   else if (prop.vt != VT_EMPTY)
1666     return E_INVALIDARG;
1667   return S_OK;
1668 }
1669 
1670 
Z7_COM7F_IMF(CHandler::GetFileTimeType (UInt32 * type))1671 Z7_COM7F_IMF(CHandler::GetFileTimeType(UInt32 *type))
1672 {
1673   *type = NFileTimeType::kUnix;
1674   return S_OK;
1675 }
1676 
1677 
Z7_COM7F_IMF(CHandler::UpdateItems (ISequentialOutStream * outStream,UInt32 numItems,IArchiveUpdateCallback * callback))1678 Z7_COM7F_IMF(CHandler::UpdateItems(ISequentialOutStream *outStream, UInt32 numItems,
1679     IArchiveUpdateCallback *callback))
1680 {
1681   COM_TRY_BEGIN
1682 
1683   if (_isArc && !CanUpdate())
1684     return E_NOTIMPL;
1685 
1686   /*
1687   Z7_DECL_CMyComPtr_QI_FROM(IArchiveUpdateCallbackArcProp,
1688       reportArcProp, callback)
1689   */
1690 
1691   CObjectVector<CUpdateItem> updateItems;
1692 
1693   UInt64 complexity = 0;
1694 
1695   UInt32 i;
1696   for (i = 0; i < numItems; i++)
1697   {
1698     CUpdateItem ui;
1699     Int32 newData;
1700     Int32 newProps;
1701     UInt32 indexInArc;
1702 
1703     if (!callback)
1704       return E_FAIL;
1705 
1706     RINOK(callback->GetUpdateItemInfo(i, &newData, &newProps, &indexInArc))
1707 
1708     ui.NewProps = IntToBool(newProps);
1709     ui.NewData = IntToBool(newData);
1710     ui.IndexInArc = (int)indexInArc;
1711     ui.IndexInClient = i;
1712     if (IntToBool(newProps))
1713     {
1714       {
1715         NCOM::CPropVariant prop;
1716         RINOK(callback->GetProperty(i, kpidIsDir, &prop))
1717         if (prop.vt == VT_EMPTY)
1718           ui.IsDir = false;
1719         else if (prop.vt != VT_BOOL)
1720           return E_INVALIDARG;
1721         else
1722           ui.IsDir = (prop.boolVal != VARIANT_FALSE);
1723       }
1724 
1725       RINOK(GetPropString(callback, i, kpidPath, ui.Path,
1726           true)) // convertSlash
1727       /*
1728       if (ui.IsDir && !ui.Name.IsEmpty() && ui.Name.Back() != '/')
1729         ui.Name += '/';
1730       */
1731     }
1732 
1733     if (IntToBool(newData))
1734     {
1735       NCOM::CPropVariant prop;
1736       RINOK(callback->GetProperty(i, kpidSize, &prop))
1737       if (prop.vt == VT_UI8)
1738       {
1739         ui.Size = prop.uhVal.QuadPart;
1740         complexity += ui.Size;
1741       }
1742       else if (prop.vt == VT_EMPTY)
1743         ui.Size = (UInt64)(Int64)-1;
1744       else
1745         return E_INVALIDARG;
1746     }
1747 
1748     updateItems.Add(ui);
1749   }
1750 
1751   if (complexity != 0)
1752   {
1753     RINOK(callback->SetTotal(complexity))
1754   }
1755 
1756   #ifdef Z7_EXTERNAL_CODECS
1757   const CExternalCodecs *_externalCodecs = g_ExternalCodecs_Ptr;
1758   #endif
1759 
1760   CHashBundle hb;
1761   UStringVector methods;
1762   if (!_methods.IsEmpty())
1763   {
1764     FOR_VECTOR(k, _methods)
1765     {
1766       methods.Add(_methods[k]);
1767     }
1768   }
1769   else if (_crcSize_WasSet)
1770   {
1771     AddDefaultMethod(methods, _crcSize);
1772   }
1773   else
1774   {
1775     Z7_DECL_CMyComPtr_QI_FROM(
1776         IArchiveGetRootProps,
1777         getRootProps, callback)
1778     if (getRootProps)
1779     {
1780       NCOM::CPropVariant prop;
1781       RINOK(getRootProps->GetRootProp(kpidArcFileName, &prop))
1782       if (prop.vt == VT_BSTR)
1783       {
1784         const UString method = GetMethod_from_FileName(prop.bstrVal);
1785         if (!method.IsEmpty())
1786           methods.Add(method);
1787       }
1788     }
1789   }
1790 
1791   RINOK(hb.SetMethods(EXTERNAL_CODECS_LOC_VARS methods))
1792 
1793   CMyComPtr2_Create<ICompressProgressInfo, CLocalProgress> lps;
1794   lps->Init(callback, true);
1795 
1796   const UInt32 kBufSize = 1 << 15;
1797   CHashMidBuf buf;
1798   if (!buf.Alloc(kBufSize))
1799     return E_OUTOFMEMORY;
1800 
1801   CDynLimBuf hashFileString((size_t)1 << 31);
1802 
1803   CHashOptionsLocal options = _options;
1804 
1805   if (_isArc)
1806   {
1807     if (!options.HashMode_Zero.Def && _is_ZeroMode)
1808       options.HashMode_Zero.Val = true;
1809     if (!options.HashMode_Tag.Def && _are_there_Tags)
1810       options.HashMode_Tag.Val = true;
1811     if (!options.HashMode_Dirs.Def && _are_there_Dirs)
1812       options.HashMode_Dirs.Val = true;
1813   }
1814   if (options.HashMode_OnlyHash.Val && updateItems.Size() != 1)
1815     options.HashMode_OnlyHash.Val = false;
1816 
1817   complexity = 0;
1818 
1819   for (i = 0; i < updateItems.Size(); i++)
1820   {
1821     lps->InSize = complexity;
1822     RINOK(lps->SetCur())
1823 
1824     const CUpdateItem &ui = updateItems[i];
1825 
1826     /*
1827     CHashPair item;
1828     if (!ui.NewProps)
1829       item = HashPairs[(unsigned)ui.IndexInArc];
1830     */
1831 
1832     if (ui.NewData)
1833     {
1834       UInt64 currentComplexity = ui.Size;
1835       UInt64 fileSize = 0;
1836 
1837       CMyComPtr<ISequentialInStream> fileInStream;
1838       bool needWrite = true;
1839       {
1840         HRESULT res = callback->GetStream(ui.IndexInClient, &fileInStream);
1841 
1842         if (res == S_FALSE)
1843           needWrite = false;
1844         else
1845         {
1846           RINOK(res)
1847 
1848           if (fileInStream)
1849           {
1850             Z7_DECL_CMyComPtr_QI_FROM(
1851                 IStreamGetSize,
1852                 streamGetSize, fileInStream)
1853             if (streamGetSize)
1854             {
1855               UInt64 size;
1856               if (streamGetSize->GetSize(&size) == S_OK)
1857                 currentComplexity = size;
1858             }
1859             /*
1860             Z7_DECL_CMyComPtr_QI_FROM(
1861                 IStreamGetProps,
1862                 getProps, fileInStream)
1863             if (getProps)
1864             {
1865               FILETIME mTime;
1866               UInt64 size2;
1867               if (getProps->GetProps(&size2, NULL, NULL, &mTime, NULL) == S_OK)
1868               {
1869                 currentComplexity = size2;
1870                 // item.MTime = NTime::FileTimeToUnixTime64(mTime);;
1871               }
1872             }
1873             */
1874           }
1875           else
1876           {
1877             currentComplexity = 0;
1878           }
1879         }
1880       }
1881 
1882       hb.InitForNewFile();
1883       const bool isDir = ui.IsDir;
1884 
1885       if (needWrite && fileInStream && !isDir)
1886       {
1887         for (UInt32 step = 0;; step++)
1888         {
1889           if ((step & 0xFF) == 0)
1890           {
1891             RINOK(lps.Interface()->SetRatioInfo(&fileSize, NULL))
1892             // RINOK(callback->SetCompleted(&completeValue));
1893           }
1894           UInt32 size;
1895           RINOK(fileInStream->Read(buf, kBufSize, &size))
1896           if (size == 0)
1897             break;
1898           hb.Update(buf, size);
1899           fileSize += size;
1900         }
1901         currentComplexity = fileSize;
1902       }
1903 
1904       fileInStream.Release();
1905       const bool isAltStream = false;
1906       hb.Final(isDir, isAltStream, ui.Path);
1907 
1908       if (options.HashMode_Dirs.Val || !isDir)
1909       {
1910         if (!hb.Hashers.IsEmpty())
1911           lps->OutSize += hb.Hashers[0].DigestSize;
1912         WriteLine(hashFileString,
1913             options,
1914             ui.Path,
1915             isDir,
1916             hb);
1917         if (hashFileString.IsError())
1918           return E_OUTOFMEMORY;
1919       }
1920 
1921       complexity += currentComplexity;
1922 
1923       /*
1924       if (reportArcProp)
1925       {
1926         PROPVARIANT prop;
1927         prop.vt = VT_EMPTY;
1928         prop.wReserved1 = 0;
1929 
1930         NCOM::PropVarEm_Set_UInt64(&prop, fileSize);
1931         RINOK(reportArcProp->ReportProp(NArchive::NEventIndexType::kOutArcIndex, ui.IndexInClient, kpidSize, &prop));
1932 
1933         for (unsigned k = 0; k < hb.Hashers.Size(); k++)
1934         {
1935           const CHasherState &hs = hb.Hashers[k];
1936 
1937           if (hs.DigestSize == 4 && hs.Name.IsEqualTo_Ascii_NoCase("crc32"))
1938           {
1939             NCOM::PropVarEm_Set_UInt32(&prop, GetUi32(hs.Digests[k_HashCalc_Index_Current]));
1940             RINOK(reportArcProp->ReportProp(NArchive::NEventIndexType::kOutArcIndex, ui.IndexInClient, kpidCRC, &prop));
1941           }
1942           else
1943           {
1944             RINOK(reportArcProp->ReportRawProp(NArchive::NEventIndexType::kOutArcIndex, ui.IndexInClient,
1945               kpidChecksum, hs.Digests[k_HashCalc_Index_Current],
1946               hs.DigestSize, NPropDataType::kRaw));
1947           }
1948           RINOK(reportArcProp->ReportFinished(NArchive::NEventIndexType::kOutArcIndex, ui.IndexInClient, NArchive::NUpdate::NOperationResult::kOK));
1949         }
1950       }
1951       */
1952       RINOK(callback->SetOperationResult(NArchive::NUpdate::NOperationResult::kOK))
1953     }
1954     else
1955     {
1956       // old data
1957       const CHashPair &existItem = HashPairs[(unsigned)ui.IndexInArc];
1958       if (ui.NewProps)
1959       {
1960         WriteLine(hashFileString,
1961             options,
1962             ui.Path,
1963             ui.IsDir,
1964             existItem.Method, existItem.HashString
1965             );
1966       }
1967       else
1968       {
1969         hashFileString += existItem.FullLine;
1970         Add_LF(hashFileString, options);
1971       }
1972     }
1973     if (hashFileString.IsError())
1974       return E_OUTOFMEMORY;
1975   }
1976 
1977   RINOK(WriteStream(outStream, hashFileString, hashFileString.Len()))
1978 
1979   return S_OK;
1980   COM_TRY_END
1981 }
1982 
1983 
1984 
SetProperty(const wchar_t * nameSpec,const PROPVARIANT & value)1985 HRESULT CHandler::SetProperty(const wchar_t *nameSpec, const PROPVARIANT &value)
1986 {
1987   UString name = nameSpec;
1988   name.MakeLower_Ascii();
1989   if (name.IsEmpty())
1990     return E_INVALIDARG;
1991 
1992   if (name.IsEqualTo("m")) // "hm" hash method
1993   {
1994     // COneMethodInfo omi;
1995     // RINOK(omi.ParseMethodFromPROPVARIANT(L"", value));
1996     // _methods.Add(omi.MethodName); // change it. use omi.PropsString
1997     if (value.vt != VT_BSTR)
1998       return E_INVALIDARG;
1999     UString s (value.bstrVal);
2000     _methods.Add(s);
2001     return S_OK;
2002   }
2003 
2004   if (name.IsEqualTo("flags"))
2005   {
2006     if (value.vt != VT_BSTR)
2007       return E_INVALIDARG;
2008     if (!_options.ParseString(value.bstrVal))
2009       return E_INVALIDARG;
2010     return S_OK;
2011   }
2012 
2013   if (name.IsEqualTo("backslash"))
2014     return PROPVARIANT_to_bool(value, _supportWindowsBackslash);
2015 
2016   if (name.IsPrefixedBy_Ascii_NoCase("crc"))
2017   {
2018     name.Delete(0, 3);
2019     _crcSize = 4;
2020     _crcSize_WasSet = true;
2021     return ParsePropToUInt32(name, value, _crcSize);
2022   }
2023 
2024   // common properties
2025   if (name.IsPrefixedBy_Ascii_NoCase("mt")
2026       || name.IsPrefixedBy_Ascii_NoCase("memuse"))
2027     return S_OK;
2028 
2029   return E_INVALIDARG;
2030 }
2031 
2032 
Z7_COM7F_IMF(CHandler::SetProperties (const wchar_t * const * names,const PROPVARIANT * values,UInt32 numProps))2033 Z7_COM7F_IMF(CHandler::SetProperties(const wchar_t * const *names, const PROPVARIANT *values, UInt32 numProps))
2034 {
2035   COM_TRY_BEGIN
2036 
2037   InitProps();
2038 
2039   for (UInt32 i = 0; i < numProps; i++)
2040   {
2041     RINOK(SetProperty(names[i], values[i]))
2042   }
2043   return S_OK;
2044   COM_TRY_END
2045 }
2046 
CHandler()2047 CHandler::CHandler()
2048 {
2049   ClearVars();
2050   InitProps();
2051 }
2052 
2053 }
2054 
2055 
2056 
CreateHashHandler_In()2057 static IInArchive  *CreateHashHandler_In()  { return new NHash::CHandler; }
CreateHashHandler_Out()2058 static IOutArchive *CreateHashHandler_Out() { return new NHash::CHandler; }
2059 
Codecs_AddHashArcHandler(CCodecs * codecs)2060 void Codecs_AddHashArcHandler(CCodecs *codecs)
2061 {
2062   {
2063     CArcInfoEx item;
2064 
2065     item.Name = "Hash";
2066     item.CreateInArchive = CreateHashHandler_In;
2067     item.CreateOutArchive = CreateHashHandler_Out;
2068     item.IsArcFunc = NULL;
2069     item.Flags =
2070         NArcInfoFlags::kKeepName
2071       | NArcInfoFlags::kStartOpen
2072       | NArcInfoFlags::kByExtOnlyOpen
2073       // | NArcInfoFlags::kPureStartOpen
2074       | NArcInfoFlags::kHashHandler
2075       ;
2076 
2077     // ubuntu uses "SHA256SUMS" file
2078     item.AddExts(UString (
2079         "sha256 sha512 sha224 sha384 sha1 sha md5"
2080         // "b2sum"
2081         " crc32 crc64"
2082         " asc"
2083         " cksum"
2084         ),
2085         UString());
2086 
2087     item.UpdateEnabled = (item.CreateOutArchive != NULL);
2088     item.SignatureOffset = 0;
2089     // item.Version = MY_VER_MIX;
2090     item.NewInterface = true;
2091 
2092     item.Signatures.AddNew().CopyFrom(NULL, 0);
2093 
2094     codecs->Formats.Add(item);
2095   }
2096 }
2097