X-Git-Url: https://git.pterodactylus.net/?p=ecparse.git;a=blobdiff_plain;f=ED2KLink.cpp;h=05b8d97c1e06f3f8f547882c15efd3b8620106a6;hp=2b4d08a592d1f9b24987386d0cddafb1713bdd13;hb=b85b2b73e8493ad0f379da96f87d0d3e12c56413;hpb=440d9987544bc516dd2e6ba19ab459cf016952dd diff --git a/ED2KLink.cpp b/ED2KLink.cpp index 2b4d08a..05b8d97 100644 --- a/ED2KLink.cpp +++ b/ED2KLink.cpp @@ -8,7 +8,6 @@ #include "ED2KLink.h" #include "GrowingBuffer.h" #include "GlobalSettings.h" -#include "GlobalSettings.h" static int getDigits(size_t number) { int digits = 0; @@ -27,12 +26,13 @@ ED2KLink::ED2KLink(const char* filename, const size_t size, const void* hash) { memcpy(this->hash, hash, 16); this->size = size; - this->link = (char*) calloc(13 + (strlen(filename) + 1) + 1 + getDigits(size) + 1 + 32 + 2, 1); - sprintf(this->link, "ed2k://|file|%s|%u|", filename, size); + link = (char*) calloc(13 + (strlen(filename) + 1) + 1 + getDigits(size) + 1 + 32 + 2, 1); + sprintf(link, "ed2k://|file|%s|%u|", filename, size); for (int i = 0; i < 16; i++) { - sprintf(this->link + strlen(this->link), "%02x", *((int*) this->hash + i)); + sprintf(link + strlen(link), "%02X", ((unsigned char*) hash)[i] & 0xff); } - sprintf(this->link + strlen(this->link), "|/"); + sprintf(link + strlen(link), "|/"); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] created ed2kLink: %s\n", __FILE__, __LINE__, link); } ED2KLink::~ED2KLink() { @@ -48,13 +48,13 @@ ED2KLink::~ED2KLink() { } ED2KLink* ED2KLink::parseED2KLink(const char* buffer) { - ED2KLink* ed2kLink; const char* tempBuffer = buffer; char* filename; char* sizeString; size_t size; int hashIndex; - char hash[16]; + unsigned char hash[16]; + char* pipeIndex; GrowingBuffer growingBuffer; GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, buffer); @@ -64,32 +64,47 @@ ED2KLink* ED2KLink::parseED2KLink(const char* buffer) { } tempBuffer += 13; - while (*tempBuffer != '|') { - growingBuffer.write(tempBuffer++, 1); + pipeIndex = strchr(tempBuffer, '|'); + if (!pipeIndex) { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__); + return NULL; } + growingBuffer.write(tempBuffer, (char*) pipeIndex - tempBuffer); filename = (char*) malloc(growingBuffer.getLimit() + 1); growingBuffer.read(filename, growingBuffer.getLimit()); growingBuffer.clear(); - tempBuffer++; + tempBuffer = pipeIndex + 1; GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed filename: “%s”.\n", __FILE__, __LINE__, filename); - while (*tempBuffer != '|') { - growingBuffer.write(tempBuffer++, 1); + pipeIndex = strchr(tempBuffer, '|'); + if (!pipeIndex) { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__); + return NULL; } + growingBuffer.write(tempBuffer, (char*) pipeIndex - tempBuffer); sizeString = (char*) malloc(growingBuffer.getLimit() + 1); growingBuffer.read(sizeString, growingBuffer.getLimit()); growingBuffer.clear(); - tempBuffer++; + tempBuffer = pipeIndex + 1; size = atol(sizeString); GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed size “%s” into %d.\n", __FILE__, __LINE__, sizeString, size); - - for (hashIndex = 0; hashIndex < 16; hashIndex++, tempBuffer += 2) { - sscanf(tempBuffer, "%hhx", hash + hashIndex); + free(sizeString); + + for (hashIndex = 0; hashIndex < 16; hashIndex++) { + char byteBuffer[3]; + byteBuffer[2] = '\0'; + memcpy(byteBuffer, tempBuffer + hashIndex * 2, 2); + if (sscanf(byteBuffer, "%hhX", &hash[hashIndex]) != 1) { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] could not parse ‘%c%c’ into a byte.\n", __FILE__, __LINE__, *(byteBuffer), *(byteBuffer + 1)); + return NULL; + } + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed ‘%c%c’ as %02x.\n", __FILE__, __LINE__, *(byteBuffer), *(byteBuffer + 1), hash[hashIndex]); } - ed2kLink = new ED2KLink(filename, size, hash); + ED2KLink* ed2kLink = new ED2KLink(filename, size, hash); + free(filename); return ed2kLink; }