Move hex dump logic to Memory API.

This commit is contained in:
Jordan Bancino 2023-01-01 22:10:23 +00:00
parent 69862a1e6e
commit e9aebab221
5 changed files with 91 additions and 75 deletions

View file

@ -15,6 +15,7 @@ Milestone: v0.2.0
[ ] Abstract /email/requestToken and /msidsn/requestToken [ ] Abstract /email/requestToken and /msidsn/requestToken
[ ] Document UserInteractiveAuth (move docs from Matrix) [ ] Document UserInteractiveAuth (move docs from Matrix)
[ ] Document MemoryHexDump
[~] User registration [~] User registration
[x] Username validation [x] Username validation
[x] Password hashing [x] Password hashing

View file

@ -25,10 +25,15 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
#include <pthread.h> #include <pthread.h>
#ifndef TELODENDRIA_MEMORY_TABLE_CHUNK #ifndef MEMORY_TABLE_CHUNK
#define TELODENDRIA_MEMORY_TABLE_CHUNK 256 #define MEMORY_TABLE_CHUNK 256
#endif
#ifndef MEMORY_HEXDUMP_WIDTH
#define MEMORY_HEXDUMP_WIDTH 16
#endif #endif
struct MemoryInfo struct MemoryInfo
@ -60,7 +65,7 @@ MemoryInsert(MemoryInfo * a)
if (!allocations) if (!allocations)
{ {
allocationsSize = TELODENDRIA_MEMORY_TABLE_CHUNK; allocationsSize = MEMORY_TABLE_CHUNK;
allocations = calloc(allocationsSize, sizeof(void *)); allocations = calloc(allocationsSize, sizeof(void *));
if (!allocations) if (!allocations)
{ {
@ -76,7 +81,7 @@ MemoryInsert(MemoryInfo * a)
size_t tmpAllocationsSize = allocationsSize; size_t tmpAllocationsSize = allocationsSize;
MemoryInfo **tmpAllocations; MemoryInfo **tmpAllocations;
allocationsSize += TELODENDRIA_MEMORY_TABLE_CHUNK; allocationsSize += MEMORY_TABLE_CHUNK;
tmpAllocations = calloc(allocationsSize, sizeof(void *)); tmpAllocations = calloc(allocationsSize, sizeof(void *));
if (!tmpAllocations) if (!tmpAllocations)
@ -419,3 +424,65 @@ void
hookArgs = args; hookArgs = args;
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
} }
void
MemoryHexDump(MemoryInfo * info, void (*printFunc) (size_t, char *, char *, void *), void *args)
{
char hexBuf[(MEMORY_HEXDUMP_WIDTH * 2) + MEMORY_HEXDUMP_WIDTH + 1];
char asciiBuf[MEMORY_HEXDUMP_WIDTH + 1];
size_t pI = 0;
size_t hI = 0;
size_t aI = 0;
const unsigned char *pc;
if (!info || !printFunc)
{
return;
}
pc = MemoryInfoGetPointer(info);
for (pI = 0; pI < MemoryInfoGetSize(info); pI++)
{
if (pI > 0 && pI % MEMORY_HEXDUMP_WIDTH == 0)
{
hexBuf[hI - 1] = '\0';
asciiBuf[aI] = '\0';
printFunc(pI - MEMORY_HEXDUMP_WIDTH, hexBuf, asciiBuf, args);
sprintf(hexBuf, "%02x ", pc[pI]);
hI = 3;
asciiBuf[0] = isprint(pc[pI]) ? pc[pI] : '.';
asciiBuf[1] = '\0';
aI = 1;
}
else
{
asciiBuf[aI] = isprint(pc[pI]) ? pc[pI] : '.';
aI++;
sprintf(hexBuf + hI, "%02x ", pc[pI]);
hI += 3;
}
}
while (hI < sizeof(hexBuf) - 2)
{
hexBuf[hI] = ' ';
hI++;
}
while (aI < sizeof(asciiBuf) - 1)
{
asciiBuf[aI] = ' ';
aI++;
}
hexBuf[hI] = '\0';
asciiBuf[aI] = '\0';
printFunc(pI - (pI % MEMORY_HEXDUMP_WIDTH), hexBuf, asciiBuf, args);
printFunc(pI, NULL, NULL, args);
}

View file

@ -75,6 +75,21 @@ TelodendriaMemoryHook(MemoryAction a, MemoryInfo * i, void *args)
MemoryInfoGetPointer(i)); MemoryInfoGetPointer(i));
} }
static void
TelodendriaHexDump(size_t off, char *hexBuf, char *asciiBuf, void *args)
{
LogConfig *lc = args;
if (hexBuf && asciiBuf)
{
Log(lc, LOG_DEBUG, "%04x: %s | %s |", off, hexBuf, asciiBuf);
}
else
{
Log(lc, LOG_DEBUG, "%04x", off);
}
}
static void static void
TelodendriaMemoryIterator(MemoryInfo * i, void *args) TelodendriaMemoryIterator(MemoryInfo * i, void *args)
{ {
@ -83,80 +98,11 @@ TelodendriaMemoryIterator(MemoryInfo * i, void *args)
/* We haven't freed the logger memory yet */ /* We haven't freed the logger memory yet */
if (MemoryInfoGetPointer(i) != lc) if (MemoryInfoGetPointer(i) != lc)
{ {
#define LEN 16
char hexBuf[(LEN * 2) + LEN + 1];
char asciiBuf[LEN + 1];
const unsigned char *pc = MemoryInfoGetPointer(i);
size_t pI = 0;
size_t hI = 0;
size_t aI = 0;
Log(lc, LOG_WARNING, "%s:%d: %lu bytes of memory at %p leaked.", Log(lc, LOG_WARNING, "%s:%d: %lu bytes of memory at %p leaked.",
MemoryInfoGetFile(i), MemoryInfoGetLine(i), MemoryInfoGetFile(i), MemoryInfoGetLine(i),
MemoryInfoGetSize(i), MemoryInfoGetPointer(i)); MemoryInfoGetSize(i), MemoryInfoGetPointer(i));
for (pI = 0; pI < MemoryInfoGetSize(i); pI++) MemoryHexDump(i, TelodendriaHexDump, lc);
{
if (pI > 0 && pI % LEN == 0)
{
hexBuf[hI - 1] = '\0';
asciiBuf[aI] = '\0';
Log(lc, LOG_DEBUG, "%04x: %s | %s |",
pI - LEN, hexBuf, asciiBuf);
sprintf(hexBuf, "%02x ", pc[pI]);
hI = 3;
if (isprint(pc[pI]))
{
asciiBuf[0] = pc[pI];
}
else
{
asciiBuf[0] = '.';
}
asciiBuf[1] = '\0';
aI = 1;
}
else
{
if (isprint(pc[pI]))
{
asciiBuf[aI] = pc[pI];
}
else
{
asciiBuf[aI] = '.';
}
aI++;
sprintf(hexBuf + hI, "%02x ", pc[pI]);
hI += 3;
}
}
while (hI < sizeof(hexBuf) - 2)
{
hexBuf[hI] = ' ';
hI++;
}
while (aI < sizeof(asciiBuf) - 1)
{
asciiBuf[aI] = ' ';
aI++;
}
hexBuf[hI] = '\0';
asciiBuf[aI] = '\0';
Log(lc, LOG_DEBUG, "%04x: %s | %s |",
pI - (pI % LEN), hexBuf, asciiBuf);
Log(lc, LOG_DEBUG, "%04x", pI);
#undef LEN
} }
} }

View file

@ -58,4 +58,6 @@ extern void MemoryIterate(void (*) (MemoryInfo *, void *), void *);
extern void MemoryHook(void (*) (MemoryAction, MemoryInfo *, void *), void *); extern void MemoryHook(void (*) (MemoryAction, MemoryInfo *, void *), void *);
extern void MemoryHexDump(MemoryInfo *, void (*) (size_t, char *, char *, void *), void *);
#endif #endif