Skip to content

Commit aa138d4

Browse files
Option for computing the hash of the output file while checking it.
1 parent 5047e91 commit aa138d4

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

Source/CLI/Global.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ int global::SetOutputFileName(const char* FileName)
3434
return 0;
3535
}
3636

37+
//---------------------------------------------------------------------------
38+
int global::SetComputeOutputHash()
39+
{
40+
Actions.set(Action_ComputeOutputHash, true);
41+
return 0;
42+
}
43+
3744
//---------------------------------------------------------------------------
3845
int global::SetBinName(const char* FileName)
3946
{
@@ -304,6 +311,8 @@ int global::SetAll(bool Value)
304311
return ReturnValue;
305312
if (int ReturnValue = SetHash(Value))
306313
return ReturnValue;
314+
if (int ReturnValue = SetComputeOutputHash())
315+
return ReturnValue;
307316
return 0;
308317
}
309318

@@ -726,6 +735,12 @@ int global::ManageCommandLine(const char* argv[], int argc)
726735
if (Value)
727736
return Value;
728737
}
738+
else if (strcmp(argv[i], "--compute-output-hash") == 0)
739+
{
740+
int Value = SetComputeOutputHash();
741+
if (Value)
742+
return Value;
743+
}
729744
else if (strcmp(argv[i], "--quick-check") == 0)
730745
{
731746
int Value = SetQuickCheck();

Source/CLI/Global.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class global
8585
int SetVersion(const char* Value);
8686
int SetOption(const char* argv[], int& i, int argc);
8787
int SetOutputFileName(const char* FileName);
88+
int SetComputeOutputHash();
8889
int SetBinName(const char* FileName);
8990
int SetLicenseKey(const char* Key, bool Add);
9091
int SetSubLicenseId(uint64_t Id);

Source/CLI/Help.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ ReturnValue Help(const char* Name)
109109
" ${Input}.mkv if the input is a folder, or ${Input}.RAWcooked if\n"
110110
" input is a file, such as a DPX.\n"
111111
"\n"
112+
" --compute-output-hash\n"
113+
" Compute the hash of the output file while checking it.\n"
114+
"\n"
112115
" --output-version value\n"
113116
" Set the version of the output..\n"
114117
" The default output value is 1.\n"

Source/CLI/rawcooked.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ Set the name of the output file or folder to \fIvalue\fR.
6767
.br
6868
The default output value is opposite to the input. Expect \fI${Input}.mkv\fR if the input is a folder, or \fI${Input}.RAWcooked\fR if input is a file, such as a DPX.
6969
.TP
70+
.B --compute-output-hash
71+
Compute the hash of the output file while checking it.
72+
.TP
7073
.B --output-version \fIvalue\fR
7174
Set the version of the output.
7275
.br

Source/Lib/Compressed/Matroska/Matroska.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#include <iomanip>
2727
#include <iostream>
2828
#include <thread>
29+
extern "C"
30+
{
31+
#include "md5.h"
32+
}
2933
//---------------------------------------------------------------------------
3034

3135
//---------------------------------------------------------------------------
@@ -307,6 +311,11 @@ void matroska::ParseBuffer()
307311

308312
Buffer_Offset = 0;
309313
Level = 0;
314+
MD5_CTX MD5;
315+
uint64_t MD5_Offset = 0;
316+
auto ComputeOutputHash = (Actions[Action_Decode] || Actions[Action_Check]) && Actions[Action_ComputeOutputHash];
317+
if (ComputeOutputHash)
318+
MD5_Init(&MD5);
310319

311320
// Progress indicator
312321
Cluster_Timestamp = 0;
@@ -356,8 +365,18 @@ void matroska::ParseBuffer()
356365
}
357366
}
358367

368+
auto MD5_Size = Buffer_Offset - MD5_Offset;
369+
auto NeedRemap = Buffer_Offset - Buffer_Offset_LowerLimit >= 0x100000; // TODO: when multi-threaded frame decoding is implemented, we need to check that all thread don't need anymore memory below this value
370+
371+
// Compute MD5 of the whole file
372+
if (ComputeOutputHash && (NeedRemap || MD5_Size >= 0x100000))
373+
{
374+
MD5_Update(&MD5, Buffer.Data() + MD5_Offset, MD5_Size);
375+
MD5_Offset += MD5_Size;
376+
}
377+
359378
// Check if we can indicate the system that we'll not need anymore memory below this value, without indicating it too much
360-
if (Buffer_Offset > Buffer_Offset_LowerLimit + 1024 * 1024 && Buffer_Offset < Buffer.Size()) // TODO: when multi-threaded frame decoding is implemented, we need to check that all thread don't need anymore memory below this value
379+
if (NeedRemap)
361380
{
362381
FileMap->Remap(Buffer_Offset, Buffer_Offset + 256 * 1024 * 1024);
363382
Buffer = *FileMap;
@@ -391,6 +410,12 @@ void matroska::ParseBuffer()
391410
}
392411

393412
// Clean up
413+
md5 MD5_Result;
414+
if (ComputeOutputHash)
415+
{
416+
MD5_Update(&MD5, Buffer.Data() + MD5_Offset, Buffer_Offset - MD5_Offset);
417+
MD5_Final(MD5_Result.data(), &MD5);
418+
}
394419
if (RAWcooked_LibraryName.empty())
395420
{
396421
if (Hashes_FromRAWcooked)
@@ -413,6 +438,15 @@ void matroska::ParseBuffer()
413438
ProgressIndicator_Thread->join();
414439
delete ProgressIndicator_Thread;
415440
}
441+
442+
// Show MD5
443+
if (ComputeOutputHash)
444+
{
445+
std::cout << "\nInfo: Output file MD5 is ";
446+
for (size_t i = 0; i < MD5_Result.size(); i++)
447+
std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)MD5_Result[i];
448+
std::cout << '.' << std::endl;
449+
}
416450
}
417451

418452
//---------------------------------------------------------------------------

Source/Lib/Utils/FileIO/Input_Base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ enum action : uint8_t
4040
Action_Info,
4141
Action_FrameMd5,
4242
Action_FrameMd5An,
43+
Action_ComputeOutputHash,
4344
Action_QuickCheckAfterEncode, // Internal, indicating the 2nd pass
4445
Action_Max
4546
};

0 commit comments

Comments
 (0)