HyperDbg Debugger
Loading...
Searching...
No Matches
common-utils.cpp File Reference

common utils More...

#include "pch.h"

Functions

BOOLEAN IsFileExists (const std::string &FileName)
 check if a file exist or not
BOOLEAN IsDirExists (const std::string &DirPath)
 check if a path exist or not
BOOLEAN CreateDirectoryRecursive (const std::string &Path)
 create a directory recursively
VOID SplitPathAndArgs (std::vector< std::string > &Qargs, const std::string &Command)
 Split path and arguments and handle strings between quotes.
const std::vector< std::string > Split (const std::string &s, const char &c)
 general split command

Detailed Description

common utils

Author
Alee Amini (alee@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Sina Karvandi (sina@.nosp@m.hype.nosp@m.rdbg..nosp@m.org)
Version
0.1
Date
2021-06-10

Function Documentation

◆ CreateDirectoryRecursive()

BOOLEAN CreateDirectoryRecursive ( const std::string & Path)

create a directory recursively

Parameters
Pathpath of file
Returns
BOOLEAN
55{
56 SIZE_T Pos = 0;
57 do
58 {
59 Pos = Path.find_first_of("\\/", Pos + 1);
60 CreateDirectoryA(Path.substr(0, Pos).c_str(), NULL);
61
62 } while (Pos != std::string::npos && Pos <= Path.size());
63
64 if (IsDirExists(Path))
65 return TRUE;
66
67 return FALSE;
68}
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
BOOLEAN IsDirExists(const std::string &DirPath)
check if a path exist or not
Definition common-utils.cpp:34

◆ IsDirExists()

BOOLEAN IsDirExists ( const std::string & DirPath)

check if a path exist or not

Parameters
DirPathpath of dir
Returns
BOOLEAN shows whether the file exist or not
35{
36 DWORD Ftyp = GetFileAttributesA(DirPath.c_str());
37 if (Ftyp == INVALID_FILE_ATTRIBUTES)
38 return FALSE; // something is wrong with your path!
39
40 if (Ftyp & FILE_ATTRIBUTE_DIRECTORY)
41 return TRUE; // this is a directory!
42
43 return FALSE; // this is not a directory!
44}
unsigned long DWORD
Definition BasicTypes.h:38

◆ IsFileExists()

BOOLEAN IsFileExists ( const std::string & FileName)

check if a file exist or not

Parameters
FileNamepath of file
Returns
BOOLEAN shows whether the file exist or not
23{
24 struct stat buffer;
25 return (stat(FileName.c_str(), &buffer) == 0);
26}

◆ Split()

const std::vector< std::string > Split ( const std::string & s,
const char & c )

general split command

Parameters
starget string
csplitter (delimiter)
Returns
const vector<string>
152{
153 string Buff {""};
154 vector<string> V;
155
156 for (auto n : s)
157 {
158 if (n != c)
159 Buff += n;
160 else if (n == c && !Buff.empty())
161 {
162 V.push_back(Buff);
163 Buff.clear();
164 }
165 }
166 if (!Buff.empty())
167 V.push_back(Buff);
168
169 return V;
170}

◆ SplitPathAndArgs()

VOID SplitPathAndArgs ( std::vector< std::string > & Qargs,
const std::string & Command )

Split path and arguments and handle strings between quotes.

Parameters
Qargsthe vector to store the result of split
Commandthe command string to split
Returns
VOID
79{
80 int Len = (int)Command.length();
81 BOOLEAN Qot = FALSE;
82 BOOLEAN Sqot = FALSE;
83 int ArgLen;
84
85 for (int i = 0; i < Len; i++)
86 {
87 INT Start = i;
88 if (Command[i] == '\"')
89 {
90 Qot = TRUE;
91 }
92 else if (Command[i] == '\'')
93 Sqot = TRUE;
94
95 if (Qot)
96 {
97 i++;
98 Start++;
99 while (i < Len && Command[i] != '\"')
100 i++;
101 if (i < Len)
102 Qot = FALSE;
103 ArgLen = i - Start;
104 i++;
105 }
106 else if (Sqot)
107 {
108 i++;
109 while (i < Len && Command[i] != '\'')
110 i++;
111 if (i < Len)
112 Sqot = FALSE;
113 ArgLen = i - Start;
114 i++;
115 }
116 else
117 {
118 while (i < Len && Command[i] != ' ')
119 i++;
120 ArgLen = i - Start;
121 }
122
123 string Temp = Command.substr(Start, ArgLen);
124 if (!Temp.empty() && Temp != " ")
125 {
126 Qargs.push_back(Temp);
127 }
128 }
129
130 /*
131 for (int i = 0; i < Qargs.size(); i++)
132 {
133 std::cout << Qargs[i] << std::endl;
134 }
135
136 std::cout << Qargs.size();
137
138 if (Qot || Sqot)
139 std::cout << "One of the quotes is open\n";
140 */
141}
UCHAR BOOLEAN
Definition BasicTypes.h:35
int INT
Definition BasicTypes.h:43