2 * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
9 #include "GrowingBuffer.h"
10 #include "GlobalSettings.h"
12 static int getDigits(size_t number) {
21 ED2KLink::ED2KLink(const char* filename, const size_t size, const void* hash) {
22 this->filename = (char*) malloc(strlen(filename));
23 this->hash = malloc(16);
25 strcpy(this->filename, filename);
26 memcpy(this->hash, hash, 16);
29 link = (char*) calloc(13 + (strlen(filename) + 1) + 1 + getDigits(size) + 1 + 32 + 2, 1);
30 sprintf(link, "ed2k://|file|%s|%u|", filename, size);
31 for (int i = 0; i < 16; i++) {
32 sprintf(link + strlen(link), "%02X", ((unsigned char*) hash)[i] & 0xff);
34 sprintf(link + strlen(link), "|/");
35 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] created ed2kLink: %s\n", __FILE__, __LINE__, link);
38 ED2KLink::~ED2KLink() {
50 ED2KLink* ED2KLink::parseED2KLink(const char* buffer) {
51 const char* tempBuffer = buffer;
56 unsigned char hash[16];
58 GrowingBuffer growingBuffer;
60 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, buffer);
62 if (strncmp("ed2k://|file|", buffer, 13)) {
67 pipeIndex = strchr(tempBuffer, '|');
69 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__);
72 growingBuffer.write(tempBuffer, (char*) pipeIndex - tempBuffer);
73 filename = (char*) malloc(growingBuffer.getLimit() + 1);
74 growingBuffer.read(filename, growingBuffer.getLimit());
75 sizeString[growingBuffer.getLimit()] = '\0';
76 growingBuffer.clear();
77 tempBuffer = pipeIndex + 1;
79 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed filename: “%s”.\n", __FILE__, __LINE__, filename);
81 pipeIndex = strchr(tempBuffer, '|');
83 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__);
86 growingBuffer.write(tempBuffer, (char*) pipeIndex - tempBuffer);
87 sizeString = (char*) malloc(growingBuffer.getLimit() + 1);
88 growingBuffer.read(sizeString, growingBuffer.getLimit());
89 sizeString[growingBuffer.getLimit()] = '\0';
90 growingBuffer.clear();
91 tempBuffer = pipeIndex + 1;
93 size = atol(sizeString);
94 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed size “%s” into %d.\n", __FILE__, __LINE__, sizeString, size);
97 for (hashIndex = 0; hashIndex < 16; hashIndex++) {
100 memcpy(byteBuffer, tempBuffer + hashIndex * 2, 2);
101 if (sscanf(byteBuffer, "%hhX", &hash[hashIndex]) != 1) {
102 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] could not parse ‘%c%c’ into a byte.\n", __FILE__, __LINE__, *(byteBuffer), *(byteBuffer + 1));
105 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed ‘%c%c’ as %02x.\n", __FILE__, __LINE__, *(byteBuffer), *(byteBuffer + 1), hash[hashIndex]);
108 ED2KLink* ed2kLink = new ED2KLink(filename, size, hash);
113 const char* ED2KLink::getLink() {
117 const char* ED2KLink::getFilename() {
118 return this->filename;
121 const size_t ED2KLink::getSize() {
125 const void* ED2KLink::getHash() {