X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=ED2KLink.cpp;h=0e633a466a2dcf2d4f78e2b402d6ccd463e46898;hb=7182eee63bf7ce0fa946f3bd0060af7770f13256;hp=8664d0cf7b76c33b19f9ab7425d72bd3e877d703;hpb=8b192302f2b689ca76c937b1fed928e9f19370b3;p=ecparse.git diff --git a/ED2KLink.cpp b/ED2KLink.cpp index 8664d0c..0e633a4 100644 --- a/ED2KLink.cpp +++ b/ED2KLink.cpp @@ -6,6 +6,8 @@ #include #include #include "ED2KLink.h" +#include "GrowingBuffer.h" +#include "GlobalSettings.h" static int getDigits(size_t number) { int digits = 0; @@ -24,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() { @@ -44,6 +47,50 @@ ED2KLink::~ED2KLink() { } } +ED2KLink* ED2KLink::parseED2KLink(const char* buffer) { + const char* tempBuffer = buffer; + char* filename; + char* sizeString; + size_t size; + int hashIndex; + char hash[16]; + GrowingBuffer growingBuffer; + + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, buffer); + + if (strncmp("ed2k://|file|", buffer, 13)) { + return NULL; + } + + tempBuffer += 13; + while (*tempBuffer != '|') { + growingBuffer.write(tempBuffer++, 1); + } + filename = (char*) malloc(growingBuffer.getLimit() + 1); + growingBuffer.read(filename, growingBuffer.getLimit()); + growingBuffer.clear(); + tempBuffer++; + + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed filename: “%s”.\n", __FILE__, __LINE__, filename); + + while (*tempBuffer != '|') { + growingBuffer.write(tempBuffer++, 1); + } + sizeString = (char*) malloc(growingBuffer.getLimit() + 1); + growingBuffer.read(sizeString, growingBuffer.getLimit()); + growingBuffer.clear(); + tempBuffer++; + + 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); + } + + return new ED2KLink(filename, size, hash); +} + const char* ED2KLink::getLink() { return this->link; }