HyperDbg Debugger
Loading...
Searching...
No Matches
script_include.h File Reference

Include file resolver declarations. More...

Go to the source code of this file.

Macros

#define MAX_PATH_LEN   1024

Functions

VOID ResolveIncludePath (const char *IncludeFilePath, char *OutPath)
 Resolves an include file path to a full absolute path.
BOOLEAN FileExists (const char *Path)
 Checks whether a file exists at the given path.
BOOLEAN ParseIncludeFile (char *IncludeFile, char **Buffer)
 Reads and parses an include file into a buffer.
char * InsertStrNew (char *Str, int InputIdx, const char *Buf)
 Inserts a string into another string at a given index.

Detailed Description

Include file resolver declarations.

Author
M.H. Gholamrezaei (mh@hy.nosp@m.perd.nosp@m.bg.or.nosp@m.g)
Version
0.1
Date
2020-10-22

Macro Definition Documentation

◆ MAX_PATH_LEN

#define MAX_PATH_LEN   1024

Function Documentation

◆ FileExists()

BOOLEAN FileExists ( const char * Path)

Checks whether a file exists at the given path.

Parameters
Paththe file path to check
Returns
BOOLEAN TRUE if the file exists, FALSE otherwise
153{
154 DWORD Attr = GetFileAttributesA(Path);
155
156 if (Attr == INVALID_FILE_ATTRIBUTES)
157 return FALSE;
158
159 if (Attr & FILE_ATTRIBUTE_DIRECTORY)
160 return FALSE;
161
162 return TRUE;
163}
#define TRUE
Definition BasicTypes.h:114
#define FALSE
Definition BasicTypes.h:113
unsigned long DWORD
Definition BasicTypes.h:38

◆ InsertStrNew()

char * InsertStrNew ( char * Str,
int InputIdx,
const char * Buf )

Inserts a string into another string at a given index.

Parameters
Strthe original string (will be reallocated)
InputIdxthe index at which to insert
Bufthe string to insert
Returns
char * the new string, or the original on allocation failure
74{
75 SIZE_T LenStr = strlen(Str);
76 SIZE_T LenBuf = strlen(Buf);
77
78 if (InputIdx < 0 || (SIZE_T)InputIdx > LenStr)
79 return Str;
80
81 char * NewStr = (char *)realloc(Str, LenStr + LenBuf + 1);
82 if (!NewStr)
83 return Str;
84
85 memmove(NewStr + InputIdx + LenBuf,
86 NewStr + InputIdx,
87 LenStr - InputIdx + 1);
88
89 memcpy(NewStr + InputIdx, Buf, LenBuf);
90
91 return NewStr;
92}
unsigned int InputIdx
number of read characters from input
Definition globals.c:18

◆ ParseIncludeFile()

BOOLEAN ParseIncludeFile ( char * IncludeFile,
char ** Buffer )

Reads and parses an include file into a buffer.

Parameters
IncludeFilethe path to the include file
Bufferpointer to receive the allocated file content buffer
Returns
BOOLEAN TRUE if the file was parsed successfully, FALSE otherwise
174{
175 FILE * Fp = fopen(IncludeFile, "r");
176 fseek(Fp, 0, SEEK_END);
177 LONG Size = ftell(Fp);
178 rewind(Fp);
179 *Buffer = calloc(Size + 1, 1);
180 fread(*Buffer, 1, Size, Fp);
181 (*Buffer)[Size] = '\0';
182 Trim(*Buffer);
183 SIZE_T Len = strlen(*Buffer);
184 if ((*Buffer)[0] == '?' &&
185 (*Buffer)[1] == ' ' &&
186 (*Buffer)[2] == '{' &&
187 (*Buffer)[Len - 1] == '}')
188 {
189 memmove(
190 *Buffer,
191 *Buffer + 3,
192 Len - 3 + 1);
193
194 (*Buffer)[strlen(*Buffer) - 1] = '\0';
195
196 return TRUE;
197 }
198 return FALSE;
199}
long LONG
Definition BasicTypes.h:28
VOID Trim(std::string &s)
trim from both ends and start of a string (in place)
Definition common.cpp:715

◆ ResolveIncludePath()

VOID ResolveIncludePath ( const char * IncludeFilePath,
char * OutPath )

Resolves an include file path to a full absolute path.

Parameters
IncludeFilePaththe include file path (relative or absolute)
OutPaththe buffer to receive the resolved path
Returns
VOID
122{
123 if (IsAbsolutePath(IncludeFilePath))
124 {
125 strncpy(OutPath, IncludeFilePath, MAX_PATH_LEN - 1);
126 OutPath[MAX_PATH_LEN - 1] = '\0';
127 return;
128 }
129
130 CHAR ExeDir[MAX_PATH_LEN];
131 GetModuleFileNameA(NULL, ExeDir, MAX_PATH_LEN);
132
133 char * P = strrchr(ExeDir, '\\');
134 if (P)
135 *P = '\0';
136
137 snprintf(
138 OutPath,
140 "%s\\%s",
141 ExeDir,
142 IncludeFilePath);
143}
char CHAR
Definition BasicTypes.h:33
BOOLEAN IsAbsolutePath(const char *Path)
Checks whether a file path is absolute.
Definition script_include.c:101
#define MAX_PATH_LEN
Definition script_include.h:15