parse header tags
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 6 Jul 2008 09:31:57 +0000 (11:31 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 6 Jul 2008 09:31:57 +0000 (11:31 +0200)
CollectionReader.cpp
CollectionReader.h

index ade7622..2f4a3cd 100644 (file)
@@ -23,6 +23,67 @@ bool CollectionReader::isLineBreakPresent() {
        return indexOfLineBreak != (size_t) -1;
 }
 
+StringTag* CollectionReader::readStringTag(bool header) {
+       if (header) {
+               uint16_t unknown = 0;
+               if (!ensureBufferCapacity(2)) {
+                       return NULL;
+               }
+               growingBuffer.read(&unknown, 2);
+       }
+       uint8_t tagName = 0;
+       if (!ensureBufferCapacity(1)) {
+               return NULL;
+       }
+       growingBuffer.read(&tagName, 1);
+       GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag name %d.\n", __FILE__, __LINE__, tagName);
+       uint16_t tagLength = 0;
+       if (!ensureBufferCapacity(2)) {
+               return NULL;
+       }
+       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)) {
+               return NULL;
+       }
+       growingBuffer.read(tagValue, tagLength);
+       tagValue[tagLength] = '\0';
+       StringTag* stringTag = new StringTag(tagName, tagValue);
+       free(tagValue);
+       return stringTag;
+}
+
+BlobTag* CollectionReader::readBlobTag(bool header) {
+       if (header) {
+               uint16_t unknown = 0;
+               if (!ensureBufferCapacity(2)) {
+                       return NULL;
+               }
+               growingBuffer.read(&unknown, 2);
+       }
+       uint8_t tagName = 0;
+       if (!ensureBufferCapacity(1)) {
+               return NULL;
+       }
+       growingBuffer.read(&tagName, 1);
+       GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag name %d.\n", __FILE__, __LINE__, tagName);
+       uint32_t tagLength = 0;
+       if (!ensureBufferCapacity(4)) {
+               return NULL;
+       }
+       growingBuffer.read(&tagLength, 4);
+       GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag length %d.\n", __FILE__, __LINE__, tagLength);
+       void* tagValue = (char*) malloc(tagLength);
+       if (!ensureBufferCapacity(tagLength)) {
+               return NULL;
+       }
+       growingBuffer.read(tagValue, tagLength);
+       BlobTag* blobTag = new BlobTag(tagName, tagValue, tagLength);
+       free(tagValue);
+       return blobTag;
+}
+
 ED2KLink* CollectionReader::getNextLink() {
        if (readerInput->isEOF() && !growingBuffer.getRemaining()) {
                GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] readInput EOF reached.\n", __FILE__, __LINE__);
@@ -83,37 +144,23 @@ ED2KLink* CollectionReader::getNextLink() {
                        growingBuffer.read(&tagType, 1);
                        GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag type %d.\n", __FILE__, __LINE__, tagType);
                        if (tagType == 0x02) {
-                               uint16_t unknown = 0;
-                               if (!ensureBufferCapacity(2)) {
-                                       return NULL;
-                               }
-                               growingBuffer.read(&unknown, 2);
-                               uint8_t tagName = 0;
-                               if (!ensureBufferCapacity(1)) {
-                                       return NULL;
-                               }
-                               growingBuffer.read(&tagName, 1);
-                               GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read tag name %d.\n", __FILE__, __LINE__, tagName);
-                               uint16_t tagLength = 0;
-                               if (!ensureBufferCapacity(2)) {
-                                       return NULL;
-                               }
-                               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)) {
-                                       return NULL;
-                               }
-                               growingBuffer.read(tagValue, tagLength);
-                               tagValue[tagLength] = '\0';
-                               if (tagName == 0x01) {
-                                       GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read FT_FILENAME: “%s”.\n", __FILE__, __LINE__, tagValue);
-                               } else if (tagName == 0x31) {
-                                       GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] read FT_COLLECTIONAUTHOR: “%s”.\n", __FILE__, __LINE__, tagValue);
+                               StringTag* stringTag = readStringTag(true);
+                               if (stringTag->getId() == 0x01) {
+                                       GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] FT_FILENAME: “%s”\n", __FILE__, __LINE__, (char*) stringTag->getValue());
+                               } else if (stringTag->getId() == 0x31) {
+                                       GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] FT_COLLECTIONAUTHOR: “%s”\n", __FILE__, __LINE__, (char*) stringTag->getValue());
                                } else {
-                                       GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] unknown tag: “%s”.\n", __FILE__, __LINE__, tagValue);
+                                       GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] unknown string in header: “%s”\n", __FILE__, __LINE__, (char*) stringTag->getValue());
                                }
                        } else if (tagType == 0x07) {
+                               BlobTag* blobTag = readBlobTag(true);
+                               if (blobTag->getId() == 0x32) {
+                                       GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] FT_COLLECTIONAUTHORKEY: %d bytes.\n", __FILE__, __LINE__, blobTag->getSize());
+                               } else {
+                                       GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] unknown block in header: %d bytes.\n", __FILE__, __LINE__, blobTag->getSize());
+                               }
+                       } else {
+                               GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] unknown tag type: %02x.\n", __FILE__, __LINE__, tagType);
                        }
                }
        }
index 72f652a..6e8f373 100644 (file)
@@ -7,6 +7,8 @@
 #include "ReaderInput.h"
 #include "ED2KLink.h"
 #include "GrowingBuffer.h"
+#include "StringTag.h"
+#include "BlobTag.h"
 
 class CollectionReader {
 
@@ -22,6 +24,9 @@ private:
        bool ensureBufferCapacity(size_t byteCount);
        void readMoreBytes();
 
+       BlobTag* readBlobTag(bool hader);
+       StringTag* readStringTag(bool header);
+
 private:
        ReaderInput* readerInput;
        GrowingBuffer growingBuffer;