HyperDbg/hyperdbg/libhyperdbg/code/debugger/commands/meta-commands/formats.cpp

151 lines
3.4 KiB
C++
Raw Permalink Normal View History

/**
* @file formats.cpp
2022-01-18 22:38:56 +03:30
* @author Sina Karvandi (sina@hyperdbg.org)
* @brief .formats command
* @details
* @version 0.1
* @date 2020-05-27
*
* @copyright This project is released under the GNU Public License v3.
*
*/
#include "pch.h"
2020-08-28 04:03:12 -07:00
/**
2023-07-13 16:05:42 +09:00
* @brief help of the help command :)
*
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandFormatsHelp()
{
2022-04-10 22:24:56 +04:30
ShowMessages(".formats : shows a value or register in different formats.\n\n");
2022-02-09 02:16:58 +03:30
ShowMessages("syntax : \t.formats [Expression (string)]\n");
ShowMessages("\n");
ShowMessages("\t\te.g : .formats nt!ExAllocatePoolWithTag\n");
ShowMessages("\t\te.g : .formats nt!Kd_DEFAULT_Mask\n");
ShowMessages("\t\te.g : .formats nt!Kd_DEFAULT_Mask+5\n");
ShowMessages("\t\te.g : .formats 55\n");
ShowMessages("\t\te.g : .formats @rax\n");
ShowMessages("\t\te.g : .formats @rbx+@rcx\n");
ShowMessages("\t\te.g : .formats $pid\n");
}
2020-05-27 14:06:27 -07:00
2020-08-28 04:03:12 -07:00
/**
* @brief show results of .formats command
*
* @param U64Value
* @return VOID
2020-08-28 04:03:12 -07:00
*/
2021-03-22 18:19:39 +04:30
VOID
CommandFormatsShowResults(UINT64 U64Value)
{
time_t t;
struct tm * tmp;
CHAR MY_TIME[50];
UINT32 Character;
2021-03-22 18:19:39 +04:30
time(&t);
2021-03-22 18:19:39 +04:30
//
// localtime() uses the time pointed by t ,
// to fill a tm structure with the values that
// represent the corresponding local time.
//
2021-03-22 18:19:39 +04:30
tmp = localtime(&t);
2021-03-22 18:19:39 +04:30
//
// using strftime to display time
//
strftime(MY_TIME, sizeof(MY_TIME), "%x - %I:%M%p", tmp);
ShowMessages("evaluate expression:\n");
2021-03-22 18:19:39 +04:30
ShowMessages("Hex : %s\n", SeparateTo64BitValue(U64Value).c_str());
ShowMessages("Decimal : %d\n", U64Value);
ShowMessages("Octal : %o\n", U64Value);
2021-03-22 18:19:39 +04:30
ShowMessages("Binary : ");
PrintBits(sizeof(UINT64), &U64Value);
2021-03-22 18:19:39 +04:30
ShowMessages("\nChar : ");
2021-03-22 18:19:39 +04:30
//
// iterate through 8, 8 bits (8*6)
//
UCHAR * TempCharacter = (UCHAR *)&U64Value;
for (SIZE_T j = 0; j < sizeof(UINT64); j++)
2021-03-22 18:19:39 +04:30
{
Character = (UINT32)TempCharacter[j];
2021-03-22 18:19:39 +04:30
if (isprint(Character))
{
ShowMessages("%c", Character);
}
else
{
ShowMessages(".");
}
}
2021-03-22 18:19:39 +04:30
ShowMessages("\nTime : %s\n", MY_TIME);
ShowMessages("Float : %4.2f %+.0e %E\n", U64Value, U64Value, U64Value);
ShowMessages("Double : %.*e\n", DECIMAL_DIG, U64Value);
}
/**
* @brief handler of .formats command
*
* @param CommandTokens
2021-02-10 15:19:01 -08:00
* @param Command
*
* @return VOID
*/
2021-03-22 18:19:39 +04:30
VOID
CommandFormats(vector<CommandToken> CommandTokens, string Command)
2021-03-22 18:19:39 +04:30
{
UINT64 ConstantValue = 0;
BOOLEAN HasError = TRUE;
2021-03-22 18:19:39 +04:30
if (CommandTokens.size() == 1)
2021-03-22 18:19:39 +04:30
{
ShowMessages("incorrect use of the '%s'\n\n",
GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).c_str());
2021-03-22 18:19:39 +04:30
CommandFormatsHelp();
return;
}
//
2021-03-22 18:19:39 +04:30
// Trim the command
//
2021-03-22 18:19:39 +04:30
Trim(Command);
//
// Remove .formats from it
//
Command.erase(0, GetCaseSensitiveStringFromCommandToken(CommandTokens.at(0)).size());
//
2021-03-22 18:19:39 +04:30
// Trim it again
//
2021-03-22 18:19:39 +04:30
Trim(Command);
2021-09-07 01:38:36 +04:30
//
// Evaluate a single expression
2021-09-07 01:38:36 +04:30
//
ConstantValue = ScriptEngineEvalSingleExpression(Command, &HasError);
2021-09-07 01:38:36 +04:30
if (HasError)
2021-03-22 18:19:39 +04:30
{
ShowMessages("err, couldn't resolve error at '%s'\n", Command.c_str());
2021-03-22 18:19:39 +04:30
}
else
{
2021-04-05 02:33:27 +04:30
//
// Show formats results for a constant
2021-03-22 18:19:39 +04:30
//
2021-04-05 02:33:27 +04:30
CommandFormatsShowResults(ConstantValue);
2021-03-22 18:19:39 +04:30
}
2020-05-27 12:09:57 -07:00
}