Skip to content

Commit f5fee21

Browse files
committed
WIP calc SHA1
1 parent cd280f3 commit f5fee21

7 files changed

+86
-18
lines changed

BKImage.cpp

+39-8
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
const wchar_t* S_CATALOG_HEADER_DEFAULT = L" Имя файла | Тип | Блоков Адрес Размер | Атр. |";
2020
const wchar_t* S_CATALOG_SEPARATOR_DEFAULT = L"-------------------------|------|------------------------|------|";
21-
const wchar_t* S_CATALOG_SEPARATOR_TAIL = L"------------------";
21+
const wchar_t* S_CATALOG_HEADER_SHA1 = L"| SHA1 ";
22+
const wchar_t* S_CATALOG_SEPARATOR_SHA1 = L"|-----------------------------------------";
2223
const wchar_t* S_CATALOG_HEADER_RAR_LIKE = L" Attributes Size Date Time Name";
2324
const wchar_t* S_CATALOG_SEPARATOR_RAR_LIKE = L"----------- --------- -------- ----- ----";
25+
const wchar_t* S_CATALOG_HEADER_RAR_LIKE_SHA1 = L" Attributes Size Date Time SHA1 Name";
26+
const wchar_t* S_CATALOG_SEPARATOR_RAR_LIKE_SHA1 = L"----------- --------- -------- ----- ---------------------------------------- ----";
2427

2528

2629
std::wstring g_AddOpErrorStr[] =
@@ -216,16 +219,25 @@ void CBKImage::PrintCatalogTableHead()
216219

217220
std::wcout << S_CATALOG_HEADER_DEFAULT;
218221
if (!strSpecific.empty())
219-
std::wcout << L" " << strSpecific;
222+
std::wcout << L" " << strSpecific << L" ";
223+
if (m_bCalcSHA1)
224+
std::wcout << S_CATALOG_HEADER_SHA1;
220225
std::wcout << std::endl;
226+
221227
std::wcout << S_CATALOG_SEPARATOR_DEFAULT;
222228
if (!strSpecific.empty())
223-
std::wcout << S_CATALOG_SEPARATOR_TAIL;
229+
{
230+
std::wstring strTail(strSpecific.length() + 2, L'-');
231+
std::wcout << strTail;
232+
}
233+
234+
if (m_bCalcSHA1)
235+
std::wcout << S_CATALOG_SEPARATOR_SHA1;
224236
}
225237
else if (m_nListingFormat == LISTING_FORMAT::RAR_LIKE)
226238
{
227-
std::wcout << S_CATALOG_HEADER_RAR_LIKE << std::endl;
228-
std::wcout << S_CATALOG_SEPARATOR_RAR_LIKE;
239+
std::wcout << (m_bCalcSHA1 ? S_CATALOG_HEADER_RAR_LIKE_SHA1 : S_CATALOG_HEADER_RAR_LIKE) << std::endl;
240+
std::wcout << (m_bCalcSHA1 ? S_CATALOG_SEPARATOR_RAR_LIKE_SHA1 : S_CATALOG_SEPARATOR_RAR_LIKE);
229241
}
230242

231243
std::wcout << std::endl;
@@ -239,7 +251,13 @@ void CBKImage::PrintCatalogTableTail()
239251

240252
std::wcout << S_CATALOG_SEPARATOR_DEFAULT;
241253
if (!strSpecific.empty())
242-
std::wcout << S_CATALOG_SEPARATOR_TAIL;
254+
{
255+
std::wstring strTail(strSpecific.length() + 2, L'-');
256+
std::wcout << strTail;
257+
}
258+
259+
if (m_bCalcSHA1)
260+
std::wcout << S_CATALOG_SEPARATOR_SHA1;
243261
}
244262

245263
std::wcout << std::endl;
@@ -399,7 +417,13 @@ void CBKImage::PrintItem(BKDirDataItem& fr, const int level, std::wstring dirpat
399417
if (!strSpecific.empty())
400418
{
401419
std::wstring strSpec = m_pFloppyImage->GetSpecificData(std::addressof(fr));
402-
std::wcout << strSpec << L" ";
420+
std::wcout << std::setw(strSpecific.length()) << std::left << strSpec;
421+
}
422+
423+
if (m_bCalcSHA1)
424+
{
425+
std::wstring strHash = m_pFloppyImage->CalcFileSHA1(&fr);
426+
std::wcout << L" | " << strHash;
403427
}
404428
}
405429
else if (m_nListingFormat == LISTING_FORMAT::RAR_LIKE)
@@ -428,6 +452,12 @@ void CBKImage::PrintItem(BKDirDataItem& fr, const int level, std::wstring dirpat
428452
else
429453
std::wcout << strDate << L" 00:00 ";
430454

455+
if (m_bCalcSHA1)
456+
{
457+
std::wstring strHash = m_pFloppyImage->CalcFileSHA1(&fr);
458+
std::wcout << strHash << L" ";
459+
}
460+
431461
std::wcout << dirpath << strName << L" ";
432462
}
433463

@@ -1134,7 +1164,8 @@ bool CBKImage::AnalyseExportFile(AnalyseFileStruct *a)
11341164
if (f)
11351165
{
11361166
std::wstring origName = imgUtil::BKToUNICODE(a->OrigName, 16, m_pFloppyImage->m_pKoi8tbl);
1137-
fwprintf(f, L"%-20s:\t%s\tload:%06o\tlen:%06o\tstart:%06o\n", a->strName.c_str(), origName.c_str(), a->nAddr, a->nLen, nStartAddr);
1167+
fwprintf(f, L"%-20ls:\t%ls\tload:%06o\tlen:%06o\tstart:%06o\n",
1168+
a->strName.c_str(), origName.c_str(), a->nAddr, a->nLen, nStartAddr);
11381169
fclose(f);
11391170
}
11401171

BKImage.h

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class CBKImage
5656
bool m_bCheckUseLongBinStatus; // состояние чекбоксов "использовать формат бин"
5757
bool m_bCheckLogExtractStatus; // и "создавать лог извлечения" соответственно, проще их тут хранить, чем запрашивать сложными путями у родителя
5858
LISTING_FORMAT m_nListingFormat;
59+
bool m_bCalcSHA1; // Считать и показывать SHA1 для файлов
5960

6061
//PaneInfo m_PaneInfo; //TODO: Убрать
6162
std::vector<PaneInfo> m_vSelItems; //TODO: Убрать
@@ -118,6 +119,7 @@ class CBKImage
118119
m_bCheckLogExtractStatus = bStatus;
119120
}
120121
inline void SetListingFormat(LISTING_FORMAT format) { m_nListingFormat = format; }
122+
inline void SetCalcSHA1(bool flag) { m_bCalcSHA1 = flag; }
121123

122124
// Подсчитать и напечатать значение хэша SHA1 для образа диска
123125
bool PrintImageSHA1();

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
CXXFLAGS = -std=c++17 -Wall -g -O0
2+
CXXFLAGS = -std=c++17 -O3 -Wall
33

44
SOURCES_IMGOS = $(foreach dir, ./imgos, $(wildcard $(dir)/*.cpp))
55
SOURCES = pch.cpp bkdecmd.cpp BKImage.cpp BKImgFile.cpp BKParseImage.cpp StringUtil.cpp $(SOURCES_IMGOS)

bkdecmd.cpp

+20-8
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ bool DoDiskDeleteFile();
3030
// Globals
3131

3232
#ifdef _MSC_VER
33-
#define OPTIONCHAR '/'
34-
#define OPTIONSTR "/"
33+
#define OPTIONCHAR L'/'
34+
#define OPTIONSTR L"/"
3535
#else
36-
#define OPTIONCHAR '-'
37-
#define OPTIONSTR "-"
36+
#define OPTIONCHAR L'-'
37+
#define OPTIONSTR L"-"
3838
#endif
3939

4040
std::wstring g_sCommand;
4141
std::wstring g_sImageFileName;
4242
fs::path g_pathImageFileName;
4343
std::wstring g_sFileName;
4444
fs::path g_pathFileName;
45+
bool g_okCalcSHA1 = false;
4546

4647
enum CommandRequirements
4748
{
@@ -88,12 +89,14 @@ void PrintUsage()
8889
{
8990
std::wcout << std::endl << L"Использование:" << std::endl
9091
<< L" Команды для работы с образами дисков:" << std::endl
91-
<< L" bkdecmd l <ImageFile> - показать содержимое корневой директории" << std::endl
92+
<< L" bkdecmd l <ImageFile> - показать содержимое корневой директории" << std::endl
9293
<< L" bkdecmd lr <ImageFile> - показать содержимое диска рекурсивным обходом директорий" << std::endl
9394
<< L" bkdecmd lm <ImageFile> - показать содержимое диска в RAR-подобном формате" << std::endl
9495
<< L" bkdecmd e <ImageFile> <FileName> - извлечь файл" << std::endl
9596
<< L" bkdecmd a <ImageFile> <FileName> - добавить файл" << std::endl
96-
<< L" bkdecmd d <ImageFile> <FileName> - удалить файл" << std::endl;
97+
<< L" bkdecmd d <ImageFile> <FileName> - удалить файл" << std::endl
98+
<< L" Опции:" << std::endl
99+
<< L" " << OPTIONSTR << L"sha1 Вычислять и показывать для файлов хэш SHA1" << std::endl;
97100
}
98101

99102
bool ParseCommandLine(std::vector<std::wstring>& wargs)
@@ -103,6 +106,11 @@ bool ParseCommandLine(std::vector<std::wstring>& wargs)
103106
const wchar_t* arg = warg.c_str();
104107
if (arg[0] == OPTIONCHAR)
105108
{
109+
if (wcscmp(arg + 1, L"sha1") == 0)
110+
{
111+
g_okCalcSHA1 = true;
112+
}
113+
else
106114
{
107115
std::wcout << L"Неизвестная опция: " << arg << std::endl;
108116
return false;
@@ -246,6 +254,7 @@ int wmain_impl(std::vector<std::wstring>& wargs)
246254

247255
// теперь, если образ опознался, надо создать объект, соответствующий файловой системе
248256
g_BKImage.ClearImgVector();
257+
g_BKImage.SetCalcSHA1(g_okCalcSHA1);
249258

250259
uint32_t flg = g_BKImage.Open(g_sParseResult);
251260
if (flg == 0)
@@ -259,8 +268,11 @@ int wmain_impl(std::vector<std::wstring>& wargs)
259268
//std::wcout << L"Свободно: " << g_BKImage.GetImageFreeSpace() << L" ";
260269
std::wcout << L"Режим: " << (g_BKImage.GetImageOpenStatus() ? L"RO" : L"RW") << std::endl;
261270

262-
if (!g_BKImage.PrintImageSHA1())
263-
return 255;
271+
if (g_okCalcSHA1)
272+
{
273+
if (!g_BKImage.PrintImageSHA1())
274+
return 255;
275+
}
264276

265277
std::wcout << std::endl;
266278

imgos/BKFloppyImage_Csidos3.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const std::wstring CBKFloppyImage_Csidos3::GetSpecificData(BKDirDataItem *fr) co
3131
{
3232
int p0 = (dd >> 4) & 7;
3333
int p1 = dd & 7;
34-
str = imgUtil::string_format(L"%s; %d:%d\0", ((dd & 010) ? L"БК11" : L"БК10"), PgNumF2L[p0], PgNumF2L[p1]);
34+
str = imgUtil::string_format(L"%ls; %d:%d\0", ((dd & 010) ? L"БК11" : L"БК10"), PgNumF2L[p0], PgNumF2L[p1]);
3535
}
3636

3737
return str;

imgos/BKFloppyImage_Prototype.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "../pch.h"
22
#include "BKFloppyImage_Prototype.h"
3+
#include "../hashes/sha1.hpp"
4+
#include "../StringUtil.h"
35

46

57
CBKFloppyImage_Prototype::CBKFloppyImage_Prototype(const PARSE_RESULT &image)
@@ -158,6 +160,25 @@ std::wstring CBKFloppyImage_Prototype::CalcImageSHA1()
158160
return m_pFoppyImgFile.CalcImageSHA1();
159161
}
160162

163+
std::wstring CBKFloppyImage_Prototype::CalcFileSHA1(BKDirDataItem *fr)
164+
{
165+
ASSERT(fr != nullptr);
166+
if (fr == nullptr || (fr->nAttr & (FR_ATTR::DIR | FR_ATTR::LINK)) != 0)
167+
return L"";
168+
169+
std::vector<uint8_t> vec(fr->nBlkSize * BLOCK_SIZE);
170+
if (!ReadFile(fr, vec.data()))
171+
{
172+
//TODO: Показать ошибку
173+
return L"";
174+
}
175+
176+
SHA1 hash;
177+
hash.update(vec.data(), fr->nSize);
178+
179+
return strUtil::stringToWstring(hash.final());
180+
}
181+
161182
// виртуальная функция, для каждой ФС - своя реализация.
162183
bool CBKFloppyImage_Prototype::ReadCurrentDir()
163184
{

imgos/BKFloppyImage_Prototype.h

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ class CBKFloppyImage_Prototype
135135

136136
std::wstring CalcImageSHA1();
137137

138+
std::wstring CalcFileSHA1(BKDirDataItem *fr);
139+
138140
public: // виртуальные функции
139141

140142
// Строка с названием поля для данных, специфических для заданной ОС

0 commit comments

Comments
 (0)