X-Git-Url: https://git.pterodactylus.net/?p=ecparse.git;a=blobdiff_plain;f=CollectionReader.cpp;h=7d47ea124795f96a7dae99ebeb984b4d8cb97f09;hp=2f4a3cd94da33960f636589caa3c920ff2f880f3;hb=HEAD;hpb=8c428eac937ccba2a2bb459ce9999a1adba5e6c0 diff --git a/CollectionReader.cpp b/CollectionReader.cpp index 2f4a3cd..7d47ea1 100644 --- a/CollectionReader.cpp +++ b/CollectionReader.cpp @@ -4,7 +4,6 @@ #include #include -#include #include "CollectionReader.h" #include "GlobalSettings.h" @@ -44,7 +43,7 @@ StringTag* CollectionReader::readStringTag(bool header) { growingBuffer.read(&tagLength, 2); GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag length %d.\n", __FILE__, __LINE__, tagLength); char* tagValue = (char*) malloc(tagLength + 1); - if (!ensureBufferCapacity(tagLength)) { + if (!ensureBufferCapacity(tagLength) || !tagValue) { return NULL; } growingBuffer.read(tagValue, tagLength); @@ -84,6 +83,21 @@ BlobTag* CollectionReader::readBlobTag(bool header) { return blobTag; } +HashTag* CollectionReader::readHashTag() { + if (!ensureBufferCapacity(1)) { + return NULL; + } + uint8_t tagId = 0; + growingBuffer.read(&tagId, 1); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag id %d.\n", __FILE__, __LINE__, tagId); + if (!ensureBufferCapacity(16)) { + return NULL; + } + char hash[16]; + growingBuffer.read(hash, 16); + return new HashTag(tagId, hash); +} + ED2KLink* CollectionReader::getNextLink() { if (readerInput->isEOF() && !growingBuffer.getRemaining()) { GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] readInput EOF reached.\n", __FILE__, __LINE__); @@ -95,7 +109,6 @@ ED2KLink* CollectionReader::getNextLink() { GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] readInput EOF reached.\n", __FILE__, __LINE__); return NULL; } - firstLink = false; } if (isTextCollection) { while (!readerInput->isEOF() && !isLineBreakPresent()) { @@ -127,8 +140,10 @@ ED2KLink* CollectionReader::getNextLink() { GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] got line: %s\n", __FILE__, __LINE__, line); ED2KLink* ed2kLink = ED2KLink::parseED2KLink(line); free(line); + firstLink = false; return ed2kLink; - } else { + } + if (firstLink) { /* read header */ if (!ensureBufferCapacity(4)) { return NULL; @@ -163,6 +178,91 @@ ED2KLink* CollectionReader::getNextLink() { GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] unknown tag type: %02x.\n", __FILE__, __LINE__, tagType); } } + fileCollectionCount = 0; + if (!ensureBufferCapacity(4)) { + return NULL; + } + growingBuffer.read(&fileCollectionCount, 4); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] will read %d files.\n", __FILE__, __LINE__, fileCollectionCount); + collectionFileIndex = 0; + firstLink = false; + } + if (collectionFileIndex < fileCollectionCount) { + uint32_t fileTagCount = 0; + if (!ensureBufferCapacity(4)) { + return NULL; + } + growingBuffer.read(&fileTagCount, 4); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] will read %d file tags.\n", __FILE__, __LINE__, fileTagCount); + StringTag* filenameTag = NULL; + uint64_t size = 0; + HashTag* hashTag = NULL; + for (uint32_t fileTagIndex = 0; fileTagIndex < fileTagCount; fileTagIndex++) { + uint8_t tagType = 0; + if (!ensureBufferCapacity(1)) { + return NULL; + } + growingBuffer.read(&tagType, 1); + tagType &= 0x7f; + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] reading tag type %d.\n", __FILE__, __LINE__, tagType); + if (tagType == 0x01) { + hashTag = readHashTag(); + if (!hashTag) { + return NULL; + } + } else if (tagType == 0x02) { + StringTag* stringTag = readStringTag(); + if (!stringTag) { + return NULL; + } + if (stringTag->getId() == 0x01) { + filenameTag = stringTag; + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read file name “%s”.\n", __FILE__, __LINE__, (char*) stringTag->getValue()); + } else if (stringTag->getId() == 0xf6) { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read file comment “%s”.\n", __FILE__, __LINE__, (char*) stringTag->getValue()); + delete stringTag; + } + } else if ((tagType == 0x03) || (tagType == 0x08) || (tagType == 0x09) || (tagType == 0x0b)) { + if (!ensureBufferCapacity(1)) { + return NULL; + } + uint8_t id = 0; + growingBuffer.read(&id, 1); + if (id == 0x02) { + int neededBytes = (tagType == 0x03) ? 4 : ((tagType == 0x08) ? 2 : ((tagType == 0x09) ? 1 : 8)); + if (!ensureBufferCapacity(neededBytes)) { + return NULL; + } + growingBuffer.read(&size, neededBytes); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] file size is %llu bytes.\n", __FILE__, __LINE__, size); + } else if (id == 0xf7) { + uint8_t fileRating = 0; + if (!ensureBufferCapacity(1)) { + return NULL; + } + growingBuffer.read(&fileRating, 1); + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read file rating %d.\n", __FILE__, __LINE__, fileRating); + } + } + } + collectionFileIndex++; + growingBuffer.cut(); + if (filenameTag && hashTag) { + ED2KLink* ed2kLink = new ED2KLink((char*) filenameTag->getValue(), size, hashTag->getValue()); + delete filenameTag; + delete hashTag; + return ed2kLink; + } else { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] not enough data to decode file.\n", __FILE__, __LINE__); + } + if (filenameTag) { + delete filenameTag; + } + if (hashTag) { + delete hashTag; + } + } else { + GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] reached end of collection.\n", __FILE__, __LINE__); } return NULL; } @@ -188,7 +288,6 @@ void CollectionReader::readMoreBytes() { } void CollectionReader::identifyCollectionType() { - int version; size_t readBytes; readBytes = readerInput->read(&version, 4); @@ -206,6 +305,7 @@ void CollectionReader::identifyCollectionType() { } else if (!strncmp("ed2k", (char*) &version, 4)) { isTextCollection = true; growingBuffer.write(&version, 4); + version = 0; GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] identified text collection\n", __FILE__, __LINE__); } else { GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] could not identify collection!\n", __FILE__, __LINE__);