diff --git a/TODO.txt b/TODO.txt index ab92f23..c9f58d5 100644 --- a/TODO.txt +++ b/TODO.txt @@ -15,6 +15,7 @@ Milestone: v0.2.0 [ ] Abstract /email/requestToken and /msidsn/requestToken [ ] Document UserInteractiveAuth (move docs from Matrix) +[ ] Document MemoryHexDump [~] User registration [x] Username validation [x] Password hashing diff --git a/src/Memory.c b/src/Memory.c index ba5ea71..2f59e67 100644 --- a/src/Memory.c +++ b/src/Memory.c @@ -25,10 +25,15 @@ #include #include +#include #include -#ifndef TELODENDRIA_MEMORY_TABLE_CHUNK -#define TELODENDRIA_MEMORY_TABLE_CHUNK 256 +#ifndef MEMORY_TABLE_CHUNK +#define MEMORY_TABLE_CHUNK 256 +#endif + +#ifndef MEMORY_HEXDUMP_WIDTH +#define MEMORY_HEXDUMP_WIDTH 16 #endif struct MemoryInfo @@ -60,7 +65,7 @@ MemoryInsert(MemoryInfo * a) if (!allocations) { - allocationsSize = TELODENDRIA_MEMORY_TABLE_CHUNK; + allocationsSize = MEMORY_TABLE_CHUNK; allocations = calloc(allocationsSize, sizeof(void *)); if (!allocations) { @@ -76,7 +81,7 @@ MemoryInsert(MemoryInfo * a) size_t tmpAllocationsSize = allocationsSize; MemoryInfo **tmpAllocations; - allocationsSize += TELODENDRIA_MEMORY_TABLE_CHUNK; + allocationsSize += MEMORY_TABLE_CHUNK; tmpAllocations = calloc(allocationsSize, sizeof(void *)); if (!tmpAllocations) @@ -419,3 +424,65 @@ void hookArgs = args; 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); +} diff --git a/src/Telodendria.c b/src/Telodendria.c index 4252dc1..2089479 100644 --- a/src/Telodendria.c +++ b/src/Telodendria.c @@ -75,6 +75,21 @@ TelodendriaMemoryHook(MemoryAction a, MemoryInfo * i, void *args) 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 TelodendriaMemoryIterator(MemoryInfo * i, void *args) { @@ -83,80 +98,11 @@ TelodendriaMemoryIterator(MemoryInfo * i, void *args) /* We haven't freed the logger memory yet */ 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.", MemoryInfoGetFile(i), MemoryInfoGetLine(i), MemoryInfoGetSize(i), MemoryInfoGetPointer(i)); - for (pI = 0; pI < MemoryInfoGetSize(i); pI++) - { - 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 + MemoryHexDump(i, TelodendriaHexDump, lc); } } diff --git a/src/UserInteractiveAuth.c b/src/UserInteractiveAuth.c index 8e443a0..b5f9a83 100644 --- a/src/UserInteractiveAuth.c +++ b/src/UserInteractiveAuth.c @@ -84,7 +84,7 @@ UserInteractiveAuth(HttpServerContext * context, Db * db, response = BuildDummyFlow(); HashMapSet(response, "session", - JsonValueString(UtilStringDuplicate(session))); + JsonValueString(UtilStringDuplicate(session))); return response; } diff --git a/src/include/Memory.h b/src/include/Memory.h index 5e42ba6..5503546 100644 --- a/src/include/Memory.h +++ b/src/include/Memory.h @@ -58,4 +58,6 @@ extern void MemoryIterate(void (*) (MemoryInfo *, void *), void *); extern void MemoryHook(void (*) (MemoryAction, MemoryInfo *, void *), void *); +extern void MemoryHexDump(MemoryInfo *, void (*) (size_t, char *, char *, void *), void *); + #endif