2 * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
9 #include "GlobalSettings.h"
11 static int getDigits(size_t number) {
20 ED2KLink::ED2KLink(const char* filename, const size_t size, const void* hash) {
21 this->filename = (char*) malloc(strlen(filename) + 1);
22 this->hash = malloc(16);
24 strcpy(this->filename, filename);
25 memcpy(this->hash, hash, 16);
28 link = (char*) calloc(13 + strlen(filename) + 1 + getDigits(size) + 1 + 32 + 2 + 1, 1);
29 sprintf(link, "ed2k://|file|%s|%u|", filename, size);
30 for (int i = 0; i < 16; i++) {
31 sprintf(link + strlen(link), "%02X", ((unsigned char*) hash)[i] & 0xff);
33 sprintf(link + strlen(link), "|/");
34 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] created ed2kLink: %s\n", __FILE__, __LINE__, link);
37 ED2KLink::~ED2KLink() {
49 ED2KLink* ED2KLink::parseED2KLink(const char* buffer) {
50 const char* tempBuffer = buffer;
55 unsigned char hash[16];
59 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, buffer);
61 if (strncmp("ed2k://|file|", buffer, 13)) {
66 pipeIndex = strchr(tempBuffer, '|');
68 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__);
71 stringLength = (char*) pipeIndex - tempBuffer;
72 filename = (char*) malloc(stringLength + 1);
73 memcpy(filename, tempBuffer, stringLength);
74 sizeString[stringLength] = '\0';
75 tempBuffer = pipeIndex + 1;
77 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed filename: “%s”.\n", __FILE__, __LINE__, filename);
79 pipeIndex = strchr(tempBuffer, '|');
81 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__);
84 stringLength = (char*) pipeIndex - tempBuffer;
85 sizeString = (char*) malloc(stringLength + 1);
86 memcpy(sizeString, tempBuffer, stringLength);
87 sizeString[stringLength] = '\0';
88 tempBuffer = pipeIndex + 1;
90 size = atol(sizeString);
91 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed size “%s” into %d.\n", __FILE__, __LINE__, sizeString, size);
94 for (hashIndex = 0; hashIndex < 16; hashIndex++) {
97 memcpy(byteBuffer, tempBuffer + hashIndex * 2, 2);
98 if (sscanf(byteBuffer, "%hhX", &hash[hashIndex]) != 1) {
99 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] could not parse ‘%c%c’ into a byte.\n", __FILE__, __LINE__, *(byteBuffer), *(byteBuffer + 1));
102 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed ‘%c%c’ as %02x.\n", __FILE__, __LINE__, *(byteBuffer), *(byteBuffer + 1), hash[hashIndex]);
105 ED2KLink* ed2kLink = new ED2KLink(filename, size, hash);
110 const char* ED2KLink::getLink() {
114 const char* ED2KLink::getFilename() {
115 return this->filename;
118 const size_t ED2KLink::getSize() {
122 const void* ED2KLink::getHash() {