+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;
+ int stringLength;
+
+ GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, tempBuffer);
+
+ 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;
+ pipeIndex = strchr(tempBuffer, '|');
+ if (!pipeIndex) {
+ GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__);
+ return NULL;
+ }
+ 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);
+
+ pipeIndex = strchr(tempBuffer, '|');
+ if (!pipeIndex) {
+ GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__);
+ return NULL;
+ }
+ 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);
+ 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* ed2kLink = new ED2KLink(filename, size, hash);
+ free(filename);
+ return ed2kLink;
+}
+
+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;
+}
+
+