Skip to content

Commit

Permalink
WIP calc SHA1
Browse files Browse the repository at this point in the history
  • Loading branch information
nzeemin committed May 10, 2024
1 parent cd280f3 commit f5fee21
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 18 deletions.
47 changes: 39 additions & 8 deletions BKImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

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


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

std::wcout << S_CATALOG_HEADER_DEFAULT;
if (!strSpecific.empty())
std::wcout << L" " << strSpecific;
std::wcout << L" " << strSpecific << L" ";
if (m_bCalcSHA1)
std::wcout << S_CATALOG_HEADER_SHA1;
std::wcout << std::endl;

std::wcout << S_CATALOG_SEPARATOR_DEFAULT;
if (!strSpecific.empty())
std::wcout << S_CATALOG_SEPARATOR_TAIL;
{
std::wstring strTail(strSpecific.length() + 2, L'-');
std::wcout << strTail;
}

if (m_bCalcSHA1)
std::wcout << S_CATALOG_SEPARATOR_SHA1;
}
else if (m_nListingFormat == LISTING_FORMAT::RAR_LIKE)
{
std::wcout << S_CATALOG_HEADER_RAR_LIKE << std::endl;
std::wcout << S_CATALOG_SEPARATOR_RAR_LIKE;
std::wcout << (m_bCalcSHA1 ? S_CATALOG_HEADER_RAR_LIKE_SHA1 : S_CATALOG_HEADER_RAR_LIKE) << std::endl;
std::wcout << (m_bCalcSHA1 ? S_CATALOG_SEPARATOR_RAR_LIKE_SHA1 : S_CATALOG_SEPARATOR_RAR_LIKE);
}

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

std::wcout << S_CATALOG_SEPARATOR_DEFAULT;
if (!strSpecific.empty())
std::wcout << S_CATALOG_SEPARATOR_TAIL;
{
std::wstring strTail(strSpecific.length() + 2, L'-');
std::wcout << strTail;
}

if (m_bCalcSHA1)
std::wcout << S_CATALOG_SEPARATOR_SHA1;
}

std::wcout << std::endl;
Expand Down Expand Up @@ -399,7 +417,13 @@ void CBKImage::PrintItem(BKDirDataItem& fr, const int level, std::wstring dirpat
if (!strSpecific.empty())
{
std::wstring strSpec = m_pFloppyImage->GetSpecificData(std::addressof(fr));
std::wcout << strSpec << L" ";
std::wcout << std::setw(strSpecific.length()) << std::left << strSpec;
}

if (m_bCalcSHA1)
{
std::wstring strHash = m_pFloppyImage->CalcFileSHA1(&fr);
std::wcout << L" | " << strHash;
}
}
else if (m_nListingFormat == LISTING_FORMAT::RAR_LIKE)
Expand Down Expand Up @@ -428,6 +452,12 @@ void CBKImage::PrintItem(BKDirDataItem& fr, const int level, std::wstring dirpat
else
std::wcout << strDate << L" 00:00 ";

if (m_bCalcSHA1)
{
std::wstring strHash = m_pFloppyImage->CalcFileSHA1(&fr);
std::wcout << strHash << L" ";
}

std::wcout << dirpath << strName << L" ";
}

Expand Down Expand Up @@ -1134,7 +1164,8 @@ bool CBKImage::AnalyseExportFile(AnalyseFileStruct *a)
if (f)
{
std::wstring origName = imgUtil::BKToUNICODE(a->OrigName, 16, m_pFloppyImage->m_pKoi8tbl);
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);
fwprintf(f, L"%-20ls:\t%ls\tload:%06o\tlen:%06o\tstart:%06o\n",
a->strName.c_str(), origName.c_str(), a->nAddr, a->nLen, nStartAddr);
fclose(f);
}

Expand Down
2 changes: 2 additions & 0 deletions BKImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class CBKImage
bool m_bCheckUseLongBinStatus; // состояние чекбоксов "использовать формат бин"
bool m_bCheckLogExtractStatus; // и "создавать лог извлечения" соответственно, проще их тут хранить, чем запрашивать сложными путями у родителя
LISTING_FORMAT m_nListingFormat;
bool m_bCalcSHA1; // Считать и показывать SHA1 для файлов

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

// Подсчитать и напечатать значение хэша SHA1 для образа диска
bool PrintImageSHA1();
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

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

SOURCES_IMGOS = $(foreach dir, ./imgos, $(wildcard $(dir)/*.cpp))
SOURCES = pch.cpp bkdecmd.cpp BKImage.cpp BKImgFile.cpp BKParseImage.cpp StringUtil.cpp $(SOURCES_IMGOS)
Expand Down
28 changes: 20 additions & 8 deletions bkdecmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ bool DoDiskDeleteFile();
// Globals

#ifdef _MSC_VER
#define OPTIONCHAR '/'
#define OPTIONSTR "/"
#define OPTIONCHAR L'/'
#define OPTIONSTR L"/"
#else
#define OPTIONCHAR '-'
#define OPTIONSTR "-"
#define OPTIONCHAR L'-'
#define OPTIONSTR L"-"
#endif

std::wstring g_sCommand;
std::wstring g_sImageFileName;
fs::path g_pathImageFileName;
std::wstring g_sFileName;
fs::path g_pathFileName;
bool g_okCalcSHA1 = false;

enum CommandRequirements
{
Expand Down Expand Up @@ -88,12 +89,14 @@ void PrintUsage()
{
std::wcout << std::endl << L"Использование:" << std::endl
<< L" Команды для работы с образами дисков:" << std::endl
<< L" bkdecmd l <ImageFile> - показать содержимое корневой директории" << std::endl
<< L" bkdecmd l <ImageFile> - показать содержимое корневой директории" << std::endl
<< L" bkdecmd lr <ImageFile> - показать содержимое диска рекурсивным обходом директорий" << std::endl
<< L" bkdecmd lm <ImageFile> - показать содержимое диска в RAR-подобном формате" << std::endl
<< L" bkdecmd e <ImageFile> <FileName> - извлечь файл" << std::endl
<< L" bkdecmd a <ImageFile> <FileName> - добавить файл" << std::endl
<< L" bkdecmd d <ImageFile> <FileName> - удалить файл" << std::endl;
<< L" bkdecmd d <ImageFile> <FileName> - удалить файл" << std::endl
<< L" Опции:" << std::endl
<< L" " << OPTIONSTR << L"sha1 Вычислять и показывать для файлов хэш SHA1" << std::endl;
}

bool ParseCommandLine(std::vector<std::wstring>& wargs)
Expand All @@ -103,6 +106,11 @@ bool ParseCommandLine(std::vector<std::wstring>& wargs)
const wchar_t* arg = warg.c_str();
if (arg[0] == OPTIONCHAR)
{
if (wcscmp(arg + 1, L"sha1") == 0)
{
g_okCalcSHA1 = true;
}
else
{
std::wcout << L"Неизвестная опция: " << arg << std::endl;
return false;
Expand Down Expand Up @@ -246,6 +254,7 @@ int wmain_impl(std::vector<std::wstring>& wargs)

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

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

if (!g_BKImage.PrintImageSHA1())
return 255;
if (g_okCalcSHA1)
{
if (!g_BKImage.PrintImageSHA1())
return 255;
}

std::wcout << std::endl;

Expand Down
2 changes: 1 addition & 1 deletion imgos/BKFloppyImage_Csidos3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const std::wstring CBKFloppyImage_Csidos3::GetSpecificData(BKDirDataItem *fr) co
{
int p0 = (dd >> 4) & 7;
int p1 = dd & 7;
str = imgUtil::string_format(L"%s; %d:%d\0", ((dd & 010) ? L"БК11" : L"БК10"), PgNumF2L[p0], PgNumF2L[p1]);
str = imgUtil::string_format(L"%ls; %d:%d\0", ((dd & 010) ? L"БК11" : L"БК10"), PgNumF2L[p0], PgNumF2L[p1]);
}

return str;
Expand Down
21 changes: 21 additions & 0 deletions imgos/BKFloppyImage_Prototype.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "../pch.h"
#include "BKFloppyImage_Prototype.h"
#include "../hashes/sha1.hpp"
#include "../StringUtil.h"


CBKFloppyImage_Prototype::CBKFloppyImage_Prototype(const PARSE_RESULT &image)
Expand Down Expand Up @@ -158,6 +160,25 @@ std::wstring CBKFloppyImage_Prototype::CalcImageSHA1()
return m_pFoppyImgFile.CalcImageSHA1();
}

std::wstring CBKFloppyImage_Prototype::CalcFileSHA1(BKDirDataItem *fr)
{
ASSERT(fr != nullptr);
if (fr == nullptr || (fr->nAttr & (FR_ATTR::DIR | FR_ATTR::LINK)) != 0)
return L"";

std::vector<uint8_t> vec(fr->nBlkSize * BLOCK_SIZE);
if (!ReadFile(fr, vec.data()))
{
//TODO: Показать ошибку
return L"";
}

SHA1 hash;
hash.update(vec.data(), fr->nSize);

return strUtil::stringToWstring(hash.final());
}

// виртуальная функция, для каждой ФС - своя реализация.
bool CBKFloppyImage_Prototype::ReadCurrentDir()
{
Expand Down
2 changes: 2 additions & 0 deletions imgos/BKFloppyImage_Prototype.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ class CBKFloppyImage_Prototype

std::wstring CalcImageSHA1();

std::wstring CalcFileSHA1(BKDirDataItem *fr);

public: // виртуальные функции

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

0 comments on commit f5fee21

Please sign in to comment.