improve parsing
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 5 Jul 2008 19:02:35 +0000 (21:02 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 5 Jul 2008 19:02:35 +0000 (21:02 +0200)
ED2KLink.cpp

index f6bcb50..1a82066 100644 (file)
@@ -54,6 +54,7 @@ ED2KLink* ED2KLink::parseED2KLink(const char* buffer) {
        size_t size;
        int hashIndex;
        unsigned char hash[16];
+       char* pipeIndex;
        GrowingBuffer growingBuffer;
 
        GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] trying to parse “%s”...\n", __FILE__, __LINE__, buffer);
@@ -63,23 +64,29 @@ ED2KLink* ED2KLink::parseED2KLink(const char* buffer) {
        }
 
        tempBuffer += 13;
-       while (*tempBuffer != '|') {
-               growingBuffer.write(tempBuffer++, 1);
+       pipeIndex = strchr(tempBuffer, '|');
+       if (!pipeIndex) {
+               GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__);
+               return NULL;
        }
+       growingBuffer.write(tempBuffer, (char*) pipeIndex - tempBuffer);
        filename = (char*) malloc(growingBuffer.getLimit() + 1);
        growingBuffer.read(filename, growingBuffer.getLimit());
        growingBuffer.clear();
-       tempBuffer++;
+       tempBuffer = pipeIndex + 1;
 
        GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed filename: “%s”.\n", __FILE__, __LINE__, filename);
 
-       while (*tempBuffer != '|') {
-               growingBuffer.write(tempBuffer++, 1);
+       pipeIndex = strchr(tempBuffer, '|');
+       if (!pipeIndex) {
+               GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] expected pipe character (‘|’) but found none.\n", __FILE__, __LINE__);
+               return NULL;
        }
+       growingBuffer.write(tempBuffer, (char*) pipeIndex - tempBuffer);
        sizeString = (char*) malloc(growingBuffer.getLimit() + 1);
        growingBuffer.read(sizeString, growingBuffer.getLimit());
        growingBuffer.clear();
-       tempBuffer++;
+       tempBuffer = pipeIndex + 1;
 
        size = atol(sizeString);
        GlobalSettings::isVerbose() && fprintf(stderr, "[%s:%d] parsed size “%s” into %d.\n", __FILE__, __LINE__, sizeString, size);