increase version number to 0.1
[ecparse.git] / ED2KLink.cpp
1 /**
2  * © 2008 by David ‘Bombe’ Roden <bombe@pterodactylus.net>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "ED2KLink.h"
9 #include "GlobalSettings.h"
10
11 static int getDigits(size_t number) {
12         int digits = 0;
13         do {
14                 number /= 10;
15                 digits++;
16         } while (number);
17         return digits;
18 }
19
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);
23
24         strcpy(this->filename, filename);
25         memcpy(this->hash, hash, 16);
26         this->size = size;
27
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);
32         }
33         sprintf(link + strlen(link), "|/");
34         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] created ed2kLink: %s\n", __FILE__, __LINE__, link);
35 }
36
37 ED2KLink::~ED2KLink() {
38         if (link) {
39                 free(link);
40         }
41         if (filename) {
42                 free(filename);
43         }
44         if (hash) {
45                 free(hash);
46         }
47 }
48
49 ED2KLink* ED2KLink::parseED2KLink(const char* buffer) {
50         const char* tempBuffer = buffer;
51         char* filename;
52         char* sizeString;
53         size_t size;
54         int hashIndex;
55         unsigned char hash[16];
56         char* pipeIndex;
57         int stringLength;
58
59         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, tempBuffer);
60
61         if (strncmp("ed2k://|file|", tempBuffer, 13)) {
62                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] line does not start with “ed2k://|file|”!\n", __FILE__, __LINE__);
63                 return NULL;
64         }
65
66         tempBuffer += 13;
67         pipeIndex = strchr(tempBuffer, '|');
68         if (!pipeIndex) {
69                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__);
70                 return NULL;
71         }
72         stringLength = (char*) pipeIndex - tempBuffer;
73         filename = (char*) malloc(stringLength + 1);
74         memcpy(filename, tempBuffer, stringLength);
75         filename[stringLength] = '\0';
76         tempBuffer = pipeIndex + 1;
77
78         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed filename: “%s”.\n", __FILE__, __LINE__, filename);
79
80         pipeIndex = strchr(tempBuffer, '|');
81         if (!pipeIndex) {
82                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__);
83                 return NULL;
84         }
85         stringLength = (char*) pipeIndex - tempBuffer;
86         sizeString = (char*) malloc(stringLength + 1);
87         memcpy(sizeString, tempBuffer, stringLength);
88         sizeString[stringLength] = '\0';
89         tempBuffer = pipeIndex + 1;
90
91         size = atol(sizeString);
92         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed size “%s” into %d.\n", __FILE__, __LINE__, sizeString, size);
93         free(sizeString);
94
95         for (hashIndex = 0; hashIndex < 16; hashIndex++) {
96                 char byteBuffer[3];
97                 byteBuffer[2] = '\0';
98                 memcpy(byteBuffer, tempBuffer + hashIndex * 2, 2);
99                 if (sscanf(byteBuffer, "%hhX", &hash[hashIndex]) != 1) {
100                         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] could not parse ‘%c%c’ into a byte.\n", __FILE__, __LINE__, *(byteBuffer), *(byteBuffer + 1));
101                         return NULL;
102                 }
103                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed ‘%c%c’ as %02x.\n", __FILE__, __LINE__, *(byteBuffer), *(byteBuffer + 1), hash[hashIndex]);
104         }
105
106         ED2KLink* ed2kLink = new ED2KLink(filename, size, hash);
107         free(filename);
108         return ed2kLink;
109 }
110
111 const char* ED2KLink::getLink() {
112         return this->link;
113 }
114
115 const char* ED2KLink::getFilename() {
116         return this->filename;
117 }
118
119 const size_t ED2KLink::getSize() {
120         return this->size;
121 }
122
123 const void* ED2KLink::getHash() {
124         return this->hash;
125 }
126
127