Skip to content

Commit

Permalink
WIP image SHA1
Browse files Browse the repository at this point in the history
  • Loading branch information
nzeemin committed May 9, 2024
1 parent 20930d7 commit cd280f3
Show file tree
Hide file tree
Showing 12 changed files with 423 additions and 6 deletions.
10 changes: 10 additions & 0 deletions BKImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ void CBKImage::PrintCatalogTableTail()
std::wcout << std::endl;
}

bool CBKImage::PrintImageSHA1()
{
std::wstring imageSha1 = m_pFloppyImage->CalcImageSHA1();
if (imageSha1.empty())
return false;

std::wcout << L"SHA1: " << imageSha1 << std::endl;
return true;
}

bool CBKImage::PrintImageInfo()
{
if (!m_pFloppyImage->ReadCurrentDir())
Expand Down
3 changes: 3 additions & 0 deletions BKImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ class CBKImage
}
inline void SetListingFormat(LISTING_FORMAT format) { m_nListingFormat = format; }

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

// Печать общей информации об образе диска
bool PrintImageInfo();

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


constexpr auto BLOCK_SIZE = 512;
Expand Down Expand Up @@ -238,3 +240,38 @@ bool CBKImgFile::IsFileOpen() const

return false;
}

std::wstring CBKImgFile::CalcImageSHA1()
{
if (!m_f)
return L"";

const size_t bufferSizeInBlocks = 16;
const size_t bufferSizeInBytes = BLOCK_SIZE * bufferSizeInBlocks;

long sizeTotal = GetFileSize();
if (sizeTotal < 0)
return L"";

std::vector<uint8_t> vec(bufferSizeInBytes);
uint32_t lbano = 0;
SHA1 hash;
long reminder = sizeTotal;
while (reminder > 0)
{
uint32_t blocksToRead = reminder >= bufferSizeInBytes
? bufferSizeInBlocks
: (reminder + BLOCK_SIZE - 1) / BLOCK_SIZE;

//TODO: Показать что за ошибка
if (!ReadLBA(vec.data(), lbano, blocksToRead))
return L"";

hash.update(vec.data(), blocksToRead * BLOCK_SIZE);

lbano += blocksToRead;
reminder -= blocksToRead * BLOCK_SIZE;
}

return strUtil::stringToWstring(hash.final());
}
8 changes: 5 additions & 3 deletions BKImgFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class CBKImgFile
bool Open(const fs::path &pathName, const bool bWrite);
void Close();

long GetFileSize() const;
bool SeekTo00() const;
bool IsFileOpen() const;

// установка новых значений CHS
// если какое-то значение == 255, то заданное значение не меняется
void SetGeometry(const uint8_t c, const uint8_t h, const uint8_t s);
Expand All @@ -47,7 +51,5 @@ class CBKImgFile
bool ReadLBA(void *buffer, const uint32_t lba, const uint32_t numSectors) const;
bool WriteLBA(void *buffer, const uint32_t lba, const uint32_t numSectors) const;

long GetFileSize() const;
bool SeekTo00() const;
bool IsFileOpen() const;
std::wstring CalcImageSHA1();
};
4 changes: 4 additions & 0 deletions bkdecmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ int wmain_impl(std::vector<std::wstring>& wargs)
std::wcout << L"Размер: " << g_BKImage.GetImgSize() << L" ";
//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;

std::wcout << std::endl;

// Main task
Expand Down
1 change: 1 addition & 0 deletions bkdecmd.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
<ClInclude Include="BKImage.h" />
<ClInclude Include="BKImgFile.h" />
<ClInclude Include="BKParseImage.h" />
<ClInclude Include="hashes\sha1.hpp" />
<ClInclude Include="imgos\BKDirDataItem.h" />
<ClInclude Include="imgos\BKDRT11Header.h" />
<ClInclude Include="imgos\BKFloppyImage_ANDos.h" />
Expand Down
3 changes: 3 additions & 0 deletions bkdecmd.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,8 @@
<ClInclude Include="imgos\BKDRT11Header.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="hashes\sha1.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
Loading

0 comments on commit cd280f3

Please sign in to comment.