X-Git-Url: https://git.pterodactylus.net/?p=ecparse.git;a=blobdiff_plain;f=ED2KLink.cpp;h=6ddcdc4b38646269e9c9da27b439430c689ac07f;hp=0e633a466a2dcf2d4f78e2b402d6ccd463e46898;hb=7cb0ee62f4712e3f79b387ee0648da27551e9755;hpb=7182eee63bf7ce0fa946f3bd0060af7770f13256 diff --git a/ED2KLink.cpp b/ED2KLink.cpp index 0e633a4..6ddcdc4 100644 --- a/ED2KLink.cpp +++ b/ED2KLink.cpp @@ -6,7 +6,6 @@ #include #include #include "ED2KLink.h" -#include "GrowingBuffer.h" #include "GlobalSettings.h" static int getDigits(size_t number) { @@ -19,14 +18,14 @@ static int getDigits(size_t number) { } ED2KLink::ED2KLink(const char* filename, const size_t size, const void* hash) { - this->filename = (char*) malloc(strlen(filename)); + this->filename = (char*) malloc(strlen(filename) + 1); this->hash = malloc(16); strcpy(this->filename, filename); memcpy(this->hash, hash, 16); this->size = size; - link = (char*) calloc(13 + (strlen(filename) + 1) + 1 + getDigits(size) + 1 + 32 + 2, 1); + link = (char*) calloc(13 + strlen(filename) + 1 + getDigits(size) + 1 + 32 + 2 + 1, 1); sprintf(link, "ed2k://|file|%s|%u|", filename, size); for (int i = 0; i < 16; i++) { sprintf(link + strlen(link), "%02X", ((unsigned char*) hash)[i] & 0xff); @@ -53,42 +52,60 @@ ED2KLink* ED2KLink::parseED2KLink(const char* buffer) { char* sizeString; size_t size; int hashIndex; - char hash[16]; - GrowingBuffer growingBuffer; + unsigned char hash[16]; + char* pipeIndex; + int stringLength; - GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, buffer); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, tempBuffer); - if (strncmp("ed2k://|file|", buffer, 13)) { + if (strncmp("ed2k://|file|", tempBuffer, 13)) { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] line does not start with “ed2k://|file|”!\n", __FILE__, __LINE__); return NULL; } 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; } - filename = (char*) malloc(growingBuffer.getLimit() + 1); - growingBuffer.read(filename, growingBuffer.getLimit()); - growingBuffer.clear(); - tempBuffer++; + stringLength = (char*) pipeIndex - tempBuffer; + filename = (char*) malloc(stringLength + 1); + memcpy(filename, tempBuffer, stringLength); + filename[stringLength] = '\0'; + 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; } - sizeString = (char*) malloc(growingBuffer.getLimit() + 1); - growingBuffer.read(sizeString, growingBuffer.getLimit()); - growingBuffer.clear(); - tempBuffer++; + stringLength = (char*) pipeIndex - tempBuffer; + sizeString = (char*) malloc(stringLength + 1); + memcpy(sizeString, tempBuffer, stringLength); + sizeString[stringLength] = '\0'; + 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]); } - return new ED2KLink(filename, size, hash); + ED2KLink* ed2kLink = new ED2KLink(filename, size, hash); + free(filename); + return ed2kLink; } const char* ED2KLink::getLink() {