correctly terminate extracted strings
[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 "GrowingBuffer.h"
10 #include "GlobalSettings.h"
11
12 static int getDigits(size_t number) {
13         int digits = 0;
14         do {
15                 number /= 10;
16                 digits++;
17         } while (number);
18         return digits;
19 }
20
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);
24
25         strcpy(this->filename, filename);
26         memcpy(this->hash, hash, 16);
27         this->size = size;
28
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);
33         }
34         sprintf(link + strlen(link), "|/");
35         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] created ed2kLink: %s\n", __FILE__, __LINE__, link);
36 }
37
38 ED2KLink::~ED2KLink() {
39         if (link) {
40                 free(link);
41         }
42         if (filename) {
43                 free(filename);
44         }
45         if (hash) {
46                 free(hash);
47         }
48 }
49
50 ED2KLink* ED2KLink::parseED2KLink(const char* buffer) {
51         const char* tempBuffer = buffer;
52         char* filename;
53         char* sizeString;
54         size_t size;
55         int hashIndex;
56         unsigned char hash[16];
57         char* pipeIndex;
58         GrowingBuffer growingBuffer;
59
60         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, buffer);
61
62         if (strncmp("ed2k://|file|", buffer, 13)) {
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         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;
78
79         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed filename: “%s”.\n", __FILE__, __LINE__, filename);
80
81         pipeIndex = strchr(tempBuffer, '|');
82         if (!pipeIndex) {
83                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__);
84                 return NULL;
85         }
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;
92
93         size = atol(sizeString);
94         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed size “%s” into %d.\n", __FILE__, __LINE__, sizeString, size);
95         free(sizeString);
96
97         for (hashIndex = 0; hashIndex < 16; hashIndex++) {
98                 char byteBuffer[3];
99                 byteBuffer[2] = '\0';
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));
103                         return NULL;
104                 }
105                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed ‘%c%c’ as %02x.\n", __FILE__, __LINE__, *(byteBuffer), *(byteBuffer + 1), hash[hashIndex]);
106         }
107
108         ED2KLink* ed2kLink = new ED2KLink(filename, size, hash);
109         free(filename);
110         return ed2kLink;
111 }
112
113 const char* ED2KLink::getLink() {
114         return this->link;
115 }
116
117 const char* ED2KLink::getFilename() {
118         return this->filename;
119 }
120
121 const size_t ED2KLink::getSize() {
122         return this->size;
123 }
124
125 const void* ED2KLink::getHash() {
126         return this->hash;
127 }
128
129