Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HashCon.cpp « Console « UI « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 273f21c85358ef71fa00187ec44cfd6a54b8ec07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// HashCon.cpp

#include "StdAfx.h"

#include "../../../Common/IntToString.h"
#include "../../../Common/StringConvert.h"

#include "../../../Windows/ErrorMsg.h"

#include "ConsoleClose.h"
#include "HashCon.h"

static const wchar_t *kEmptyFileAlias = L"[Content]";

static const char *kScanningMessage = "Scanning";

HRESULT CHashCallbackConsole::CheckBreak()
{
  return NConsoleClose::TestBreakSignal() ? E_ABORT : S_OK;
}

HRESULT CHashCallbackConsole::StartScanning()
{
  (*OutStream) << kScanningMessage;
  return CheckBreak();
}

HRESULT CHashCallbackConsole::ScanProgress(UInt64 /* numFolders */, UInt64 /* numFiles */, UInt64 /* totalSize */, const wchar_t * /* path */, bool /* isDir */)
{
  return CheckBreak();
}

HRESULT CHashCallbackConsole::CanNotFindError(const wchar_t *name, DWORD systemError)
{
  return CanNotFindError_Base(name, systemError);
}

HRESULT CHashCallbackConsole::FinishScanning()
{
  (*OutStream) << endl << endl;
  return CheckBreak();
}

HRESULT CHashCallbackConsole::SetNumFiles(UInt64 /* numFiles */)
{
  return CheckBreak();
}

HRESULT CHashCallbackConsole::SetTotal(UInt64 size)
{
  if (EnablePercents)
    m_PercentPrinter.SetTotal(size);
  return CheckBreak();
}

HRESULT CHashCallbackConsole::SetCompleted(const UInt64 *completeValue)
{
  if (completeValue && EnablePercents)
  {
    m_PercentPrinter.SetRatio(*completeValue);
    m_PercentPrinter.PrintRatio();
  }
  return CheckBreak();
}

static void AddMinuses(AString &s, unsigned num)
{
  for (unsigned i = 0; i < num; i++)
    s += '-';
}

static void SetSpaces(char *s, int num)
{
  for (int i = 0; i < num; i++)
    s[i] = ' ';
}

static void SetSpacesAndNul(char *s, int num)
{
  SetSpaces(s, num);
  s[num] = 0;
}

static void AddSpaces(UString &s, int num)
{
  for (int i = 0; i < num; i++)
    s += ' ';
}

static const int kSizeField_Len = 13;
static const int kNameField_Len = 12;

static unsigned GetColumnWidth(unsigned digestSize)
{
  unsigned width = digestSize * 2;
  const unsigned kMinColumnWidth = 8;
  return width < kMinColumnWidth ? kMinColumnWidth: width;
}

void CHashCallbackConsole::PrintSeparatorLine(const CObjectVector<CHasherState> &hashers)
{
  AString s;
  for (unsigned i = 0; i < hashers.Size(); i++)
  {
    const CHasherState &h = hashers[i];
    AddMinuses(s, GetColumnWidth(h.DigestSize));
    s += ' ';
  }
  AddMinuses(s, kSizeField_Len);
  s += "  ";
  AddMinuses(s, kNameField_Len);
  m_PercentPrinter.PrintString(s);
  m_PercentPrinter.PrintNewLine();
}

HRESULT CHashCallbackConsole::BeforeFirstFile(const CHashBundle &hb)
{
  UString s;
  FOR_VECTOR (i, hb.Hashers)
  {
    const CHasherState &h = hb.Hashers[i];
    s += h.Name;
    AddSpaces(s, (int)GetColumnWidth(h.DigestSize) - h.Name.Len() + 1);
  }
  UString s2 = L"Size";
  AddSpaces(s, kSizeField_Len - s2.Len());
  s += s2;
  s += L"  ";
  s += L"Name";
  m_PercentPrinter.PrintString(s);
  m_PercentPrinter.PrintNewLine();
  PrintSeparatorLine(hb.Hashers);
  return CheckBreak();
}

HRESULT CHashCallbackConsole::OpenFileError(const wchar_t *name, DWORD systemError)
{
  FailedCodes.Add(systemError);
  FailedFiles.Add(name);
  // if (systemError == ERROR_SHARING_VIOLATION)
  {
    m_PercentPrinter.PrintString(name);
    m_PercentPrinter.PrintString(": WARNING: ");
    m_PercentPrinter.PrintString(NWindows::NError::MyFormatMessage(systemError));
    return S_FALSE;
  }
  // return systemError;
}

HRESULT CHashCallbackConsole::GetStream(const wchar_t *name, bool /* isFolder */)
{
  m_FileName = name;
  return CheckBreak();
}

void CHashCallbackConsole::PrintResultLine(UInt64 fileSize,
    const CObjectVector<CHasherState> &hashers, unsigned digestIndex, bool showHash)
{
  FOR_VECTOR (i, hashers)
  {
    const CHasherState &h = hashers[i];

    char s[k_HashCalc_DigestSize_Max * 2 + 64];
    s[0] = 0;
    if (showHash)
      AddHashHexToString(s, h.Digests[digestIndex], h.DigestSize);
    SetSpacesAndNul(s + strlen(s), (int)GetColumnWidth(h.DigestSize) - (int)strlen(s) + 1);
    m_PercentPrinter.PrintString(s);
  }
  char s[64];
  s[0] = 0;
  char *p = s;
  if (showHash && fileSize != 0)
  {
    p = s + 32;
    ConvertUInt64ToString(fileSize, p);
    int numSpaces = kSizeField_Len - (int)strlen(p);
    if (numSpaces > 0)
    {
      p -= numSpaces;
      SetSpaces(p, numSpaces);
    }
  }
  else
    SetSpacesAndNul(s, kSizeField_Len - (int)strlen(s));
  unsigned len = (unsigned)strlen(p);
  p[len] = ' ';
  p[len + 1] = ' ';
  p[len + 2] = 0;
  m_PercentPrinter.PrintString(p);
}

HRESULT CHashCallbackConsole::SetOperationResult(UInt64 fileSize, const CHashBundle &hb, bool showHash)
{
  PrintResultLine(fileSize, hb.Hashers, k_HashCalc_Index_Current, showHash);
  if (m_FileName.IsEmpty())
    m_PercentPrinter.PrintString(kEmptyFileAlias);
  else
    m_PercentPrinter.PrintString(m_FileName);
  m_PercentPrinter.PrintNewLine();
  return S_OK;
}

static const char *k_DigestTitles[] =
{
    " :"
  , " for data:              "
  , " for data and names:    "
  , " for streams and names: "
};

static void PrintSum(CStdOutStream &p, const CHasherState &h, unsigned digestIndex)
{
  char s[k_HashCalc_DigestSize_Max * 2 + 64];
  UString name = h.Name;
  AddSpaces(name, 6 - (int)name.Len());
  p << name;
  p << k_DigestTitles[digestIndex];
  s[0] = 0;
  AddHashHexToString(s, h.Digests[digestIndex], h.DigestSize);
  p << s;
  p << "\n";
}


void PrintHashStat(CStdOutStream &p, const CHashBundle &hb)
{
  FOR_VECTOR (i, hb.Hashers)
  {
    const CHasherState &h = hb.Hashers[i];
    p << "\n";
    PrintSum(p, h, k_HashCalc_Index_DataSum);
    if (hb.NumFiles != 1 || hb.NumDirs != 0)
      PrintSum(p, h, k_HashCalc_Index_NamesSum);
    if (hb.NumAltStreams != 0)
      PrintSum(p, h, k_HashCalc_Index_StreamsSum);
  }
}

void CHashCallbackConsole::PrintProperty(const char *name, UInt64 value)
{
  char s[32];
  s[0] = ':';
  s[1] = ' ';
  ConvertUInt64ToString(value, s + 2);
  m_PercentPrinter.PrintString(name);
  m_PercentPrinter.PrintString(s);
  m_PercentPrinter.PrintNewLine();
}

HRESULT CHashCallbackConsole::AfterLastFile(const CHashBundle &hb)
{
  PrintSeparatorLine(hb.Hashers);

  PrintResultLine(hb.FilesSize, hb.Hashers, k_HashCalc_Index_DataSum, true);
  m_PercentPrinter.PrintNewLine();
  m_PercentPrinter.PrintNewLine();
  
  if (hb.NumFiles != 1 || hb.NumDirs != 0)
  {
    if (hb.NumDirs != 0)
      PrintProperty("Folders", hb.NumDirs);
    PrintProperty("Files", hb.NumFiles);
  }
  PrintProperty("Size", hb.FilesSize);
  if (hb.NumAltStreams != 0)
  {
    PrintProperty("AltStreams", hb.NumAltStreams);
    PrintProperty("AltStreams size", hb.AltStreamsSize);
  }
  PrintHashStat(*m_PercentPrinter.OutStream, hb);
  m_PercentPrinter.PrintNewLine();
  return S_OK;
}