X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=ED2KLink.cpp;h=1a8206651f7edc5ff258c1bafb431db6eef2bc3e;hb=87c3276013058baf0a606510737ae03ae1c79727;hp=cd269402e70b581661cdbd7cee7f7570128976a2;hpb=f931be4f06ed62465a0c2c09f56dbc2cf6a7170a;p=ecparse.git diff --git a/ED2KLink.cpp b/ED2KLink.cpp index cd26940..1a82066 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,3 +47,75 @@ ED2KLink::~ED2KLink() { } } +ED2KLink* ED2KLink::parseED2KLink(const char* buffer) { + const char* tempBuffer = buffer; + char* filename; + char* sizeString; + size_t size; + int hashIndex; + unsigned char hash[16]; + char* pipeIndex; + 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; + 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 = pipeIndex + 1; + + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed filename: “%s”.\n", __FILE__, __LINE__, filename); + + 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 = 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++) { + char byteBuffer[3]; + byteBuffer[2] = '\0'; + memcpy(byteBuffer, tempBuffer + hashIndex * 2, 2); + sscanf(byteBuffer, "%hhX", &hash[hashIndex]); + 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); +} + +const char* ED2KLink::getLink() { + return this->link; +} + +const char* ED2KLink::getFilename() { + return this->filename; +} + +const size_t ED2KLink::getSize() { + return this->size; +} + +const void* ED2KLink::getHash() { + return this->hash; +} + +