mirror of
https://github.com/alliedmodders/metamod-source.git
synced 2025-02-20 13:54:14 +01:00
Replace fscanf with faster parsing (#127)
This commit is contained in:
parent
b5f160ffb4
commit
2fe0c36845
@ -59,6 +59,7 @@ namespace SourceHook
|
||||
// On linux, first check /proc/self/maps
|
||||
unsigned long laddr = reinterpret_cast<unsigned long>(addr);
|
||||
|
||||
bool bFound = false;
|
||||
FILE *pF = fopen("/proc/self/maps", "r");
|
||||
if (pF) {
|
||||
// Linux /proc/self/maps -> parse
|
||||
@ -67,10 +68,18 @@ namespace SourceHook
|
||||
// 08048000-0804c000 r-xp 00000000 03:03 1010107 /bin/cat
|
||||
unsigned long rlower, rupper;
|
||||
char r, w, x;
|
||||
while (fscanf(pF, "%lx-%lx %c%c%c", &rlower, &rupper, &r, &w, &x) != EOF) {
|
||||
char *buffer = NULL;
|
||||
size_t bufsize = 0;
|
||||
while (getline(&buffer, &bufsize, pF) != -1) {
|
||||
char *addr_split;
|
||||
char *prot_split;
|
||||
rlower = strtoul(buffer, &addr_split, 16);
|
||||
rupper = strtoul(&addr_split[1], &prot_split, 16);
|
||||
// Check whether we're IN THERE!
|
||||
if (laddr >= rlower && laddr < rupper) {
|
||||
fclose(pF);
|
||||
r = prot_split[1];
|
||||
w = prot_split[2];
|
||||
x = prot_split[3];
|
||||
*bits = 0;
|
||||
if (r == 'r')
|
||||
*bits |= SH_MEM_READ;
|
||||
@ -78,19 +87,13 @@ namespace SourceHook
|
||||
*bits |= SH_MEM_WRITE;
|
||||
if (x == 'x')
|
||||
*bits |= SH_MEM_EXEC;
|
||||
return true;
|
||||
}
|
||||
// Read to end of line
|
||||
int c;
|
||||
while ((c = fgetc(pF)) != '\n') {
|
||||
if (c == EOF)
|
||||
break;
|
||||
}
|
||||
if (c == EOF)
|
||||
bFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(buffer);
|
||||
fclose(pF);
|
||||
return false;
|
||||
return bFound;
|
||||
}
|
||||
pF = fopen("/proc/curproc/map", "r");
|
||||
if (pF) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user