2021-12-07 22:47:41 +01:00
# include "content/views/view_hashes.hpp"
2020-11-11 00:13:09 +01:00
2021-01-13 17:28:27 +01:00
# include <hex/providers/provider.hpp>
2021-02-03 11:54:41 +01:00
# include <hex/helpers/crypto.hpp>
2020-11-11 00:13:09 +01:00
# include <vector>
2020-11-19 11:37:16 +01:00
2021-12-07 22:47:41 +01:00
namespace hex : : plugin : : builtin {
2020-11-11 00:13:09 +01:00
2021-12-07 22:47:41 +01:00
ViewHashes : : ViewHashes ( ) : View ( " hex.builtin.view.hashes.name " ) {
2021-03-27 11:36:36 +01:00
EventManager : : subscribe < EventDataChanged > ( this , [ this ] ( ) {
2020-11-12 09:38:52 +01:00
this - > m_shouldInvalidate = true ;
} ) ;
2020-11-28 15:53:11 +01:00
2021-03-27 11:36:36 +01:00
EventManager : : subscribe < EventRegionSelected > ( this , [ this ] ( Region region ) {
2020-11-28 15:53:11 +01:00
if ( this - > m_shouldMatchSelection ) {
2021-09-20 23:40:36 +02:00
if ( region . address = = size_t ( - 1 ) ) {
this - > m_hashRegion [ 0 ] = this - > m_hashRegion [ 1 ] = 0 ;
} else {
this - > m_hashRegion [ 0 ] = region . address ;
2021-10-26 17:21:48 +02:00
this - > m_hashRegion [ 1 ] = region . size + 1 ; //WARNING: get size - 1 as region size
2021-09-20 23:40:36 +02:00
}
2020-11-28 15:53:11 +01:00
this - > m_shouldInvalidate = true ;
}
} ) ;
2020-11-11 00:13:09 +01:00
}
ViewHashes : : ~ ViewHashes ( ) {
2021-03-27 11:36:36 +01:00
EventManager : : unsubscribe < EventDataChanged > ( this ) ;
EventManager : : unsubscribe < EventRegionSelected > ( this ) ;
2020-11-11 00:13:09 +01:00
}
2021-02-02 23:11:23 +01:00
template < size_t Size >
static void formatBigHexInt ( std : : array < u8 , Size > dataArray , char * buffer , size_t bufferSize ) {
2020-11-12 12:00:50 +01:00
for ( int i = 0 ; i < dataArray . size ( ) ; i + + )
2021-02-02 23:11:23 +01:00
snprintf ( buffer + 2 * i , bufferSize - 2 * i , " %02X " , dataArray [ i ] ) ;
2020-11-12 12:00:50 +01:00
}
2020-12-22 18:10:01 +01:00
void ViewHashes : : drawContent ( ) {
2021-12-07 22:47:41 +01:00
if ( ImGui : : Begin ( View : : toWindowName ( " hex.builtin.view.hashes.name " ) . c_str ( ) , & this - > getWindowOpenState ( ) , ImGuiWindowFlags_NoCollapse ) ) {
2021-08-28 14:22:02 +02:00
if ( ImGui : : BeginChild ( " ##scrolling " , ImVec2 ( 0 , 0 ) , false , ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav ) ) {
2020-11-11 00:13:09 +01:00
2021-09-21 02:29:54 +02:00
auto provider = ImHexApi : : Provider : : get ( ) ;
if ( ImHexApi : : Provider : : isValid ( ) & & provider - > isAvailable ( ) ) {
2020-11-11 00:13:09 +01:00
2021-08-28 14:22:02 +02:00
ImGui : : TextUnformatted ( " hex.common.region " _lang ) ;
ImGui : : Separator ( ) ;
2020-11-11 00:13:09 +01:00
2021-08-28 14:22:02 +02:00
ImGui : : InputScalarN ( " ##nolabel " , ImGuiDataType_U64 , this - > m_hashRegion , 2 , nullptr , nullptr , " %08X " , ImGuiInputTextFlags_CharsHexadecimal ) ;
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
2020-11-11 09:18:35 +01:00
2021-08-28 14:22:02 +02:00
ImGui : : Checkbox ( " hex.common.match_selection " _lang , & this - > m_shouldMatchSelection ) ;
if ( ImGui : : IsItemEdited ( ) ) {
// Force execution of Region Selection Event
EventManager : : post < RequestSelectionChange > ( Region { 0 , 0 } ) ;
this - > m_shouldInvalidate = true ;
}
2020-11-12 12:00:50 +01:00
2021-08-28 14:22:02 +02:00
ImGui : : NewLine ( ) ;
2021-12-07 22:47:41 +01:00
ImGui : : TextUnformatted ( " hex.builtin.view.hashes.settings " _lang ) ;
2021-08-28 14:22:02 +02:00
ImGui : : Separator ( ) ;
2020-11-28 15:53:11 +01:00
2021-12-07 22:47:41 +01:00
if ( ImGui : : BeginCombo ( " hex.builtin.view.hashes.function " _lang , hashFunctionNames [ this - > m_currHashFunction ] . second , 0 ) )
2021-10-26 17:21:48 +02:00
{
for ( int i = 0 ; i < hashFunctionNames . size ( ) ; i + + )
{
bool is_selected = ( this - > m_currHashFunction = = i ) ;
if ( ImGui : : Selectable ( hashFunctionNames [ i ] . second , is_selected ) )
this - > m_currHashFunction = i ;
if ( is_selected )
ImGui : : SetItemDefaultFocus ( ) ; // Set the initial focus when opening the combo (scrolling + for keyboard navigation support in the upcoming navigation branch)
}
ImGui : : EndCombo ( ) ;
2021-08-28 14:22:02 +02:00
this - > m_shouldInvalidate = true ;
2021-10-26 17:21:48 +02:00
}
2020-11-12 12:00:50 +01:00
2021-08-28 14:22:02 +02:00
size_t dataSize = provider - > getSize ( ) ;
if ( this - > m_hashRegion [ 1 ] > = provider - > getBaseAddress ( ) + dataSize )
this - > m_hashRegion [ 1 ] = provider - > getBaseAddress ( ) + dataSize ;
2020-11-12 12:00:50 +01:00
2020-11-28 15:53:11 +01:00
2021-10-26 17:21:48 +02:00
switch ( hashFunctionNames [ this - > m_currHashFunction ] . first ) {
case HashFunctions : : Crc8 :
{
static int polynomial = 0x07 , init = 0x0000 , xorout = 0x0000 ;
static bool reflectIn = false , reflectOut = false ;
2021-12-07 22:47:41 +01:00
ImGui : : InputInt ( " hex.builtin.view.hashes.iv " _lang , & init , 0 , 0 , ImGuiInputTextFlags_CharsHexadecimal ) ;
2021-10-26 17:21:48 +02:00
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
2021-12-07 22:47:41 +01:00
ImGui : : InputInt ( " hex.builtin.view.hashes.xorout " _lang , & xorout , 0 , 0 , ImGuiInputTextFlags_CharsHexadecimal ) ;
2021-10-26 17:21:48 +02:00
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
ImGui : : Checkbox ( " hex.common.reflectIn " _lang , & reflectIn ) ;
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
ImGui : : Checkbox ( " hex.common.reflectOut " _lang , & reflectOut ) ;
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
2021-12-07 22:47:41 +01:00
ImGui : : InputInt ( " hex.builtin.view.hashes.poly " _lang , & polynomial , 0 , 0 , ImGuiInputTextFlags_CharsHexadecimal ) ;
2021-10-26 17:21:48 +02:00
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
ImGui : : NewLine ( ) ;
static u8 result = 0 ;
if ( this - > m_shouldInvalidate )
result = crypt : : crc8 ( provider , this - > m_hashRegion [ 0 ] , this - > m_hashRegion [ 1 ] ,
polynomial , init , xorout , reflectIn , reflectOut ) ;
char buffer [ sizeof ( result ) * 2 + 1 ] ;
snprintf ( buffer , sizeof ( buffer ) , " %02X " , result ) ;
2021-12-07 22:47:41 +01:00
ImGui : : TextUnformatted ( " hex.builtin.view.hashes.result " _lang ) ;
2021-10-26 17:21:48 +02:00
ImGui : : Separator ( ) ;
ImGui : : InputText ( " ##nolabel " , buffer , ImGuiInputTextFlags_ReadOnly ) ;
}
break ;
case HashFunctions : : Crc16 :
{
static int polynomial = 0x8005 , init = 0x0000 , xorout = 0x0000 ;
static bool reflectIn = false , reflectOut = false ;
2021-12-07 22:47:41 +01:00
ImGui : : InputInt ( " hex.builtin.view.hashes.iv " _lang , & init , 0 , 0 , ImGuiInputTextFlags_CharsHexadecimal ) ;
2021-10-26 17:21:48 +02:00
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
2021-12-07 22:47:41 +01:00
ImGui : : InputInt ( " hex.builtin.view.hashes.xorout " _lang , & xorout , 0 , 0 , ImGuiInputTextFlags_CharsHexadecimal ) ;
2021-10-26 17:21:48 +02:00
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
ImGui : : Checkbox ( " hex.common.reflectIn " _lang , & reflectIn ) ;
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
ImGui : : Checkbox ( " hex.common.reflectOut " _lang , & reflectOut ) ;
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
2021-12-07 22:47:41 +01:00
ImGui : : InputInt ( " hex.builtin.view.hashes.poly " _lang , & polynomial , 0 , 0 , ImGuiInputTextFlags_CharsHexadecimal ) ;
2021-10-26 17:21:48 +02:00
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
ImGui : : NewLine ( ) ;
static u16 result = 0 ;
if ( this - > m_shouldInvalidate )
result = crypt : : crc16 ( provider , this - > m_hashRegion [ 0 ] , this - > m_hashRegion [ 1 ] ,
polynomial , init , xorout , reflectIn , reflectOut ) ;
char buffer [ sizeof ( result ) * 2 + 1 ] ;
snprintf ( buffer , sizeof ( buffer ) , " %04X " , result ) ;
2021-12-07 22:47:41 +01:00
ImGui : : TextUnformatted ( " hex.builtin.view.hashes.result " _lang ) ;
2021-10-26 17:21:48 +02:00
ImGui : : Separator ( ) ;
ImGui : : InputText ( " ##nolabel " , buffer , ImGuiInputTextFlags_ReadOnly ) ;
}
break ;
case HashFunctions : : Crc32 :
{
static int polynomial = 0x04C11DB7 , init = 0xFFFFFFFF , xorout = 0xFFFFFFFF ;
static bool reflectIn = true , reflectOut = true ;
2021-12-07 22:47:41 +01:00
ImGui : : InputInt ( " hex.builtin.view.hashes.iv " _lang , & init , 0 , 0 , ImGuiInputTextFlags_CharsHexadecimal ) ;
2021-10-26 17:21:48 +02:00
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
2020-11-12 12:00:50 +01:00
2021-12-07 22:47:41 +01:00
ImGui : : InputInt ( " hex.builtin.view.hashes.xorout " _lang , & xorout , 0 , 0 , ImGuiInputTextFlags_CharsHexadecimal ) ;
2021-10-26 17:21:48 +02:00
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
2020-11-12 12:00:50 +01:00
2021-10-26 17:21:48 +02:00
ImGui : : Checkbox ( " hex.common.reflectIn " _lang , & reflectIn ) ;
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
2020-11-28 15:53:11 +01:00
2021-10-26 17:21:48 +02:00
ImGui : : Checkbox ( " hex.common.reflectOut " _lang , & reflectOut ) ;
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
2020-11-12 12:00:50 +01:00
2021-12-07 22:47:41 +01:00
ImGui : : InputInt ( " hex.builtin.view.hashes.poly " _lang , & polynomial , 0 , 0 , ImGuiInputTextFlags_CharsHexadecimal ) ;
2021-10-26 17:21:48 +02:00
if ( ImGui : : IsItemEdited ( ) ) this - > m_shouldInvalidate = true ;
2020-11-12 12:00:50 +01:00
2021-10-26 17:21:48 +02:00
ImGui : : NewLine ( ) ;
2020-11-28 15:53:11 +01:00
2021-10-26 17:21:48 +02:00
static u32 result = 0 ;
if ( this - > m_shouldInvalidate )
result = crypt : : crc32 ( provider , this - > m_hashRegion [ 0 ] , this - > m_hashRegion [ 1 ] ,
polynomial , init , xorout , reflectIn , reflectOut ) ;
char buffer [ sizeof ( result ) * 2 + 1 ] ;
snprintf ( buffer , sizeof ( buffer ) , " %08X " , result ) ;
2021-12-07 22:47:41 +01:00
ImGui : : TextUnformatted ( " hex.builtin.view.hashes.result " _lang ) ;
2021-10-26 17:21:48 +02:00
ImGui : : Separator ( ) ;
ImGui : : InputText ( " ##nolabel " , buffer , ImGuiInputTextFlags_ReadOnly ) ;
}
break ;
case HashFunctions : : Md5 :
{
static std : : array < u8 , 16 > result = { 0 } ;
if ( this - > m_shouldInvalidate )
result = crypt : : md5 ( provider , this - > m_hashRegion [ 0 ] , this - > m_hashRegion [ 1 ] ) ;
char buffer [ sizeof ( result ) * 2 + 1 ] ;
formatBigHexInt ( result , buffer , sizeof ( buffer ) ) ;
ImGui : : NewLine ( ) ;
2021-12-07 22:47:41 +01:00
ImGui : : TextUnformatted ( " hex.builtin.view.hashes.result " _lang ) ;
2021-10-26 17:21:48 +02:00
ImGui : : Separator ( ) ;
ImGui : : InputText ( " ##nolabel " , buffer , ImGuiInputTextFlags_ReadOnly ) ;
2020-11-12 12:00:50 +01:00
}
2021-10-26 17:21:48 +02:00
break ;
case HashFunctions : : Sha1 :
{
static std : : array < u8 , 20 > result = { 0 } ;
2020-11-12 12:00:50 +01:00
2021-10-26 17:21:48 +02:00
if ( this - > m_shouldInvalidate )
result = crypt : : sha1 ( provider , this - > m_hashRegion [ 0 ] , this - > m_hashRegion [ 1 ] ) ;
char buffer [ sizeof ( result ) * 2 + 1 ] ;
formatBigHexInt ( result , buffer , sizeof ( buffer ) ) ;
ImGui : : NewLine ( ) ;
2021-12-07 22:47:41 +01:00
ImGui : : TextUnformatted ( " hex.builtin.view.hashes.result " _lang ) ;
2021-10-26 17:21:48 +02:00
ImGui : : Separator ( ) ;
ImGui : : InputText ( " ##nolabel " , buffer , ImGuiInputTextFlags_ReadOnly ) ;
}
break ;
case HashFunctions : : Sha224 :
{
static std : : array < u8 , 28 > result = { 0 } ;
if ( this - > m_shouldInvalidate )
result = crypt : : sha224 ( provider , this - > m_hashRegion [ 0 ] , this - > m_hashRegion [ 1 ] ) ;
char buffer [ sizeof ( result ) * 2 + 1 ] ;
formatBigHexInt ( result , buffer , sizeof ( buffer ) ) ;
ImGui : : NewLine ( ) ;
2021-12-07 22:47:41 +01:00
ImGui : : TextUnformatted ( " hex.builtin.view.hashes.result " _lang ) ;
2021-10-26 17:21:48 +02:00
ImGui : : Separator ( ) ;
ImGui : : InputText ( " ##nolabel " , buffer , ImGuiInputTextFlags_ReadOnly ) ;
}
break ;
case HashFunctions : : Sha256 :
{
static std : : array < u8 , 32 > result ;
if ( this - > m_shouldInvalidate )
result = crypt : : sha256 ( provider , this - > m_hashRegion [ 0 ] , this - > m_hashRegion [ 1 ] ) ;
char buffer [ sizeof ( result ) * 2 + 1 ] ;
formatBigHexInt ( result , buffer , sizeof ( buffer ) ) ;
ImGui : : NewLine ( ) ;
2021-12-07 22:47:41 +01:00
ImGui : : TextUnformatted ( " hex.builtin.view.hashes.result " _lang ) ;
2021-10-26 17:21:48 +02:00
ImGui : : Separator ( ) ;
ImGui : : InputText ( " ##nolabel " , buffer , ImGuiInputTextFlags_ReadOnly ) ;
}
break ;
case HashFunctions : : Sha384 :
{
static std : : array < u8 , 48 > result ;
if ( this - > m_shouldInvalidate )
result = crypt : : sha384 ( provider , this - > m_hashRegion [ 0 ] , this - > m_hashRegion [ 1 ] ) ;
char buffer [ sizeof ( result ) * 2 + 1 ] ;
formatBigHexInt ( result , buffer , sizeof ( buffer ) ) ;
ImGui : : NewLine ( ) ;
2021-12-07 22:47:41 +01:00
ImGui : : TextUnformatted ( " hex.builtin.view.hashes.result " _lang ) ;
2021-10-26 17:21:48 +02:00
ImGui : : Separator ( ) ;
ImGui : : InputText ( " ##nolabel " , buffer , ImGuiInputTextFlags_ReadOnly ) ;
}
break ;
case HashFunctions : : Sha512 :
{
static std : : array < u8 , 64 > result ;
if ( this - > m_shouldInvalidate )
result = crypt : : sha512 ( provider , this - > m_hashRegion [ 0 ] , this - > m_hashRegion [ 1 ] ) ;
char buffer [ sizeof ( result ) * 2 + 1 ] ;
formatBigHexInt ( result , buffer , sizeof ( buffer ) ) ;
ImGui : : NewLine ( ) ;
2021-12-07 22:47:41 +01:00
ImGui : : TextUnformatted ( " hex.builtin.view.hashes.result " _lang ) ;
2021-10-26 17:21:48 +02:00
ImGui : : Separator ( ) ;
ImGui : : InputText ( " ##nolabel " , buffer , ImGuiInputTextFlags_ReadOnly ) ;
}
break ;
2020-11-11 09:18:35 +01:00
}
2020-11-11 00:13:09 +01:00
}
2021-10-26 17:21:48 +02:00
this - > m_shouldInvalidate = false ;
2020-11-11 00:13:09 +01:00
}
2021-08-28 16:02:53 +02:00
ImGui : : EndChild ( ) ;
2020-11-11 00:13:09 +01:00
}
2020-11-11 10:47:02 +01:00
ImGui : : End ( ) ;
2020-11-11 00:13:09 +01:00
}
2020-12-22 18:10:01 +01:00
void ViewHashes : : drawMenu ( ) {
2020-11-23 23:57:19 +01:00
2020-11-11 00:13:09 +01:00
}
2021-10-26 17:21:48 +02:00
}