00427ffab6800bbb73ad2c0d4894e0e3977b0c32
[ecparse.git] / CollectionReader.cpp
1 /**
2  * © 2008 by David Roden <droden@gmail.com>
3  */
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include "CollectionReader.h"
8 #include "GlobalSettings.h"
9
10 CollectionReader::CollectionReader(ReaderInput* readerInput) {
11         this->readerInput = readerInput;
12         firstLink = true;
13 }
14
15 CollectionReader::~CollectionReader() {
16 }
17
18 bool CollectionReader::isLineBreakPresent() {
19         size_t indexOfLineBreak = growingBuffer.indexOf('\n');
20         GlobalSettings::isVerbose() && (indexOfLineBreak != (size_t) -1) && fprintf(stderr, "[%s:%d] found line break at %d.\n", __FILE__, __LINE__, indexOfLineBreak);
21         GlobalSettings::isVerbose() && (indexOfLineBreak == (size_t) -1) && fprintf(stderr, "[%s:%d] could not find line break.\n", __FILE__, __LINE__);
22         return indexOfLineBreak != (size_t) -1;
23 }
24
25 StringTag* CollectionReader::readStringTag(bool header) {
26         if (header) {
27                 uint16_t unknown = 0;
28                 if (!ensureBufferCapacity(2)) {
29                         return NULL;
30                 }
31                 growingBuffer.read(&unknown, 2);
32         }
33         uint8_t tagName = 0;
34         if (!ensureBufferCapacity(1)) {
35                 return NULL;
36         }
37         growingBuffer.read(&tagName, 1);
38         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag name %d.\n", __FILE__, __LINE__, tagName);
39         uint16_t tagLength = 0;
40         if (!ensureBufferCapacity(2)) {
41                 return NULL;
42         }
43         growingBuffer.read(&tagLength, 2);
44         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag length %d.\n", __FILE__, __LINE__, tagLength);
45         char* tagValue = (char*) malloc(tagLength + 1);
46         if (!ensureBufferCapacity(tagLength)) {
47                 return NULL;
48         }
49         growingBuffer.read(tagValue, tagLength);
50         tagValue[tagLength] = '\0';
51         StringTag* stringTag = new StringTag(tagName, tagValue);
52         free(tagValue);
53         return stringTag;
54 }
55
56 BlobTag* CollectionReader::readBlobTag(bool header) {
57         if (header) {
58                 uint16_t unknown = 0;
59                 if (!ensureBufferCapacity(2)) {
60                         return NULL;
61                 }
62                 growingBuffer.read(&unknown, 2);
63         }
64         uint8_t tagName = 0;
65         if (!ensureBufferCapacity(1)) {
66                 return NULL;
67         }
68         growingBuffer.read(&tagName, 1);
69         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag name %d.\n", __FILE__, __LINE__, tagName);
70         uint32_t tagLength = 0;
71         if (!ensureBufferCapacity(4)) {
72                 return NULL;
73         }
74         growingBuffer.read(&tagLength, 4);
75         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag length %d.\n", __FILE__, __LINE__, tagLength);
76         void* tagValue = (char*) malloc(tagLength);
77         if (!ensureBufferCapacity(tagLength)) {
78                 return NULL;
79         }
80         growingBuffer.read(tagValue, tagLength);
81         BlobTag* blobTag = new BlobTag(tagName, tagValue, tagLength);
82         free(tagValue);
83         return blobTag;
84 }
85
86 HashTag* CollectionReader::readHashTag() {
87         if (!ensureBufferCapacity(1)) {
88                 return NULL;
89         }
90         uint8_t tagId = 0;
91         growingBuffer.read(&tagId, 1);
92         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag id %d.\n", __FILE__, __LINE__, tagId);
93         if (!ensureBufferCapacity(16)) {
94                 return NULL;
95         }
96         char hash[16];
97         growingBuffer.read(hash, 16);
98         return new HashTag(tagId, hash);
99 }
100
101 ED2KLink* CollectionReader::getNextLink() {
102         if (readerInput->isEOF() && !growingBuffer.getRemaining()) {
103                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] readInput EOF reached.\n", __FILE__, __LINE__);
104                 return NULL;
105         }
106         if (firstLink) {
107                 identifyCollectionType();
108                 if (readerInput->isEOF() && !growingBuffer.getRemaining()) {
109                         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] readInput EOF reached.\n", __FILE__, __LINE__);
110                         return NULL;
111                 }
112         }
113         if (isTextCollection) {
114                 while (!readerInput->isEOF() && !isLineBreakPresent()) {
115                         readMoreBytes();
116                 }
117                 if (readerInput->isEOF() && !growingBuffer.getRemaining()) {
118                         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] readInput EOF reached.\n", __FILE__, __LINE__);
119                         return NULL;
120                 }
121                 size_t indexOfLineBreak = growingBuffer.indexOf('\n');
122                 char* line;
123                 if (indexOfLineBreak == (size_t) -1) {
124                         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] could not find line break, using remainder of file.\n", __FILE__, __LINE__);
125                         indexOfLineBreak = growingBuffer.getRemaining();
126                         line = (char*) malloc(indexOfLineBreak + 1);
127                         growingBuffer.read(line, indexOfLineBreak);
128                         line[indexOfLineBreak] = '\0';
129                 } else {
130                         line = (char*) malloc(indexOfLineBreak + 1);
131                         growingBuffer.read(line, indexOfLineBreak + 1);
132                         if (line[indexOfLineBreak] == '\n') {
133                                 line[indexOfLineBreak] = '\0';
134                         }
135                         if (line[indexOfLineBreak - 1] == '\r') {
136                                 line[indexOfLineBreak - 1] = '\0';
137                         }
138                 }
139                 growingBuffer.cut();
140                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] got line: %s\n", __FILE__, __LINE__, line);
141                 ED2KLink* ed2kLink = ED2KLink::parseED2KLink(line);
142                 free(line);
143                 return ed2kLink;
144         }
145         if (firstLink) {
146                 /* read header */
147                 if (!ensureBufferCapacity(4)) {
148                         return NULL;
149                 }
150                 uint32_t headerTagCount = 0;
151                 growingBuffer.read(&headerTagCount, 4);
152                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] will read %d header tags.\n", __FILE__, __LINE__, headerTagCount);
153                 for (uint32_t headerTagIndex = 0; headerTagIndex < headerTagCount; headerTagIndex++) {
154                         uint8_t tagType = 0;
155                         if (!ensureBufferCapacity(1)) {
156                                 return NULL;
157                         }
158                         growingBuffer.read(&tagType, 1);
159                         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag type %d.\n", __FILE__, __LINE__, tagType);
160                         if (tagType == 0x02) {
161                                 StringTag* stringTag = readStringTag(true);
162                                 if (stringTag->getId() == 0x01) {
163                                         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] FT_FILENAME: “%s”\n", __FILE__, __LINE__, (char*) stringTag->getValue());
164                                 } else if (stringTag->getId() == 0x31) {
165                                         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] FT_COLLECTIONAUTHOR: “%s”\n", __FILE__, __LINE__, (char*) stringTag->getValue());
166                                 } else {
167                                         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] unknown string in header: “%s”\n", __FILE__, __LINE__, (char*) stringTag->getValue());
168                                 }
169                         } else if (tagType == 0x07) {
170                                 BlobTag* blobTag = readBlobTag(true);
171                                 if (blobTag->getId() == 0x32) {
172                                         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] FT_COLLECTIONAUTHORKEY: %d bytes.\n", __FILE__, __LINE__, blobTag->getSize());
173                                 } else {
174                                         GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] unknown block in header: %d bytes.\n", __FILE__, __LINE__, blobTag->getSize());
175                                 }
176                         } else {
177                                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] unknown tag type: %02x.\n", __FILE__, __LINE__, tagType);
178                         }
179                 }
180                 fileCollectionCount = 0;
181                 if (!ensureBufferCapacity(4)) {
182                         return NULL;
183                 }
184                 growingBuffer.read(&fileCollectionCount, 4);
185                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] will read %d files.\n", __FILE__, __LINE__, fileCollectionCount);
186                 collectionFileIndex = 0;
187                 firstLink = false;
188         }
189         if (collectionFileIndex < fileCollectionCount) {
190         } else {
191                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] reached end of collection.\n", __FILE__, __LINE__);
192         }
193         return NULL;
194 }
195
196 bool CollectionReader::ensureBufferCapacity(size_t byteCount) {
197         while (!readerInput->isEOF() && (growingBuffer.getRemaining() < byteCount)) {
198                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] only %d bytes remaning, need at least %d, reading more bytes.\n", __FILE__, __LINE__, growingBuffer.getRemaining(), byteCount);
199                 readMoreBytes();
200         }
201         if (readerInput->isEOF() && (growingBuffer.getRemaining() < byteCount)) {
202                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] need %d more bytes, but file is EOF.\n", __FILE__, __LINE__, (byteCount - growingBuffer.getRemaining()));
203                 return false;
204         }
205         return true;
206 }
207
208 void CollectionReader::readMoreBytes() {
209         char buffer[1024];
210         size_t readBytes;
211
212         readBytes = readerInput->read(buffer, 1024);
213         growingBuffer.write(buffer, readBytes);
214 }
215
216 void CollectionReader::identifyCollectionType() {
217         size_t readBytes;
218
219         readBytes = readerInput->read(&version, 4);
220         if ((readBytes < 4) || readerInput->isEOF()) {
221                 return;
222         }
223         if (version == 0x01) {
224                 isTextCollection = false;
225                 this->version = 1;
226                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] identified binary collection, version 1\n", __FILE__, __LINE__);
227         } else if (version == 0x02) {
228                 isTextCollection = false;
229                 this->version = 2;
230                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] identified binary collection, version 2\n", __FILE__, __LINE__);
231         } else if (!strncmp("ed2k", (char*) &version, 4)) {
232                 isTextCollection = true;
233                 growingBuffer.write(&version, 4);
234                 version = 0;
235                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] identified text collection\n", __FILE__, __LINE__);
236         } else  {
237                 GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] could not identify collection!\n", __FILE__, __LINE__);
238         }
239 }
240