OverlayDocumentParser.cpp 6.79 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4 5
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
6
 *
Claudio Valerio's avatar
Claudio Valerio committed
7 8 9
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
10 11
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
12 13 14 15
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
16
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
19
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
20
 *
Claudio Valerio's avatar
Claudio Valerio committed
21
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
22
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
23
 */
24 25


Claudio Valerio's avatar
Claudio Valerio committed
26

Claudio Valerio's avatar
Claudio Valerio committed
27

28 29 30
#include "OverlayDocumentParser.h"
#include <fstream>
#include <string.h>
31
#include <QtGlobal>
32 33 34
#include "Exception.h"
#include "Object.h"

35 36
#include "core/memcheck.h"

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
using namespace merge_lib;
using namespace std;

int OverlayDocumentParser::DOC_PART_WITH_START_OF_XREF = 30;
unsigned int partSize = 10485760; // = 10 Mb

Document * OverlayDocumentParser::parseDocument(const char * fileName)
{
   _fileName = fileName;
   return Parser::parseDocument(fileName);
}


void OverlayDocumentParser::_readXRefAndCreateObjects()
{
   std::map<unsigned int, unsigned long> objectsAndPositions;
   _readXref(objectsAndPositions);
   std::map<unsigned int, unsigned long> objectsAndSizes;
   std::map<unsigned int, unsigned long>::iterator objAndSIter;
   std::map<unsigned int, unsigned long>::iterator objAndPIter;
   unsigned long fileSize = Utils::getFileSize(_fileName.c_str());

   for(objAndSIter = objectsAndPositions.begin(); objAndSIter != objectsAndPositions.end(); ++objAndSIter)
   {
      unsigned int nextPosition = fileSize;
      for(objAndPIter = objectsAndPositions.begin(); objAndPIter != objectsAndPositions.end(); ++objAndPIter)
      {
         if((objAndPIter->second > objAndSIter->second) && (objAndPIter->second < nextPosition))
Claudio Valerio's avatar
Claudio Valerio committed
65
            nextPosition = objAndPIter->second;            
66 67 68 69 70 71 72 73 74 75 76 77 78 79
      }
      objectsAndSizes[objAndSIter->first] = nextPosition - objAndSIter->second;
   }

   bool notEndOfFile = true;
   do
   {
      unsigned long partStart = fileSize;
      std::map<unsigned int, unsigned long>::iterator objIter;
      for(objIter = objectsAndPositions.begin(); objIter != objectsAndPositions.end(); ++objIter)
      {
         if(objIter->second < partStart)
            partStart = objIter->second;
      }
Claudio Valerio's avatar
Claudio Valerio committed
80
      unsigned long nextPartStart = partStart + partSize;                 
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

      if((nextPartStart) < fileSize)
         _getPartOfFileContent(partStart, partSize);
      else
      {
         _getPartOfFileContent(partStart, fileSize - partStart);
         nextPartStart = fileSize;
         notEndOfFile = false;
      }

      for(objIter = objectsAndPositions.begin(); objIter != objectsAndPositions.end(); )
      {
         if((objectsAndSizes[objIter->first] +  objIter->second <= nextPartStart) && (objIter->second >= partStart) && ((objIter->second < nextPartStart)))
         {
            std::pair<unsigned int, unsigned int> streamBounds;
            unsigned int objectNumber;
Claudio Valerio's avatar
Claudio Valerio committed
97
            unsigned int generationNumber;
98 99 100 101 102 103
            bool hasObjectStream;
            const std::string content = _getObjectContent(objIter->second - partStart, objectNumber, generationNumber, streamBounds, hasObjectStream);
            streamBounds.first += partStart;
            streamBounds.second += partStart;
            Object * newObject = new Object(objectNumber, generationNumber, content, _document->_documentName ,streamBounds, hasObjectStream);
            _objects[objectNumber] = newObject;
Claudio Valerio's avatar
Claudio Valerio committed
104
            std::map<unsigned int, unsigned long>::iterator temp = objIter;                   
105 106 107
            ++objIter;
            objectsAndPositions.erase(temp);
            continue;
Claudio Valerio's avatar
Claudio Valerio committed
108
         }                      
109 110 111 112 113 114 115
         ++objIter;           
      }
      partStart = nextPartStart;
   }
   while(notEndOfFile);   
}

116 117 118 119 120
void OverlayDocumentParser::_getFileContent(const char * fileName)
{
    Q_UNUSED(fileName);
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
void OverlayDocumentParser::_getPartOfFileContent(long startOfPart, unsigned int length)
{
   ifstream pdfFile;
   pdfFile.open (_fileName.c_str(), ios::binary );
   if (pdfFile.fail())
   {
      stringstream errorMessage("File ");
      errorMessage << _fileName << " is absent" << "\0";
      throw Exception(errorMessage);
   }   
   ios_base::seekdir dir;
   if(startOfPart >= 0)
      dir = ios_base::beg;
   else 
      dir = ios_base::end;
   pdfFile.seekg (startOfPart, dir);
   _fileContent.resize(length);
   pdfFile.read(&_fileContent[0], length);
   pdfFile.close();
}

void OverlayDocumentParser::_readXref(std::map<unsigned int, unsigned long> & objectsAndSizes)
{
   _getPartOfFileContent(- DOC_PART_WITH_START_OF_XREF, DOC_PART_WITH_START_OF_XREF);
   unsigned int startOfStartxref = _fileContent.find("startxref");
   unsigned int startOfNumber = _fileContent.find_first_of(Parser::NUMBERS, startOfStartxref);
   unsigned int endOfNumber = _fileContent.find_first_not_of(Parser::NUMBERS, startOfNumber + 1);
   std::string startXref = _fileContent.substr(startOfNumber, endOfNumber - startOfNumber);
   unsigned int strtXref = Utils::stringToInt(startXref);

   unsigned int sizeOfXref = Utils::getFileSize(_fileName.c_str()) - strtXref;
   _getPartOfFileContent(strtXref, sizeOfXref);
   unsigned int leftBoundOfObjectNumber = _fileContent.find("0 ") + strlen("0 ");
   unsigned int rightBoundOfObjectNumber = _fileContent.find_first_not_of(Parser::NUMBERS, leftBoundOfObjectNumber);
   std::string objectNuberStr = _fileContent.substr(leftBoundOfObjectNumber, rightBoundOfObjectNumber - leftBoundOfObjectNumber);
   unsigned long objectNumber = Utils::stringToInt(objectNuberStr);
   unsigned int startOfObjectPosition = _fileContent.find("0000000000 65535 f ") + strlen("0000000000 65535 f ");
   for(unsigned long i = 1; i < objectNumber; ++i)
   {
      startOfObjectPosition = _fileContent.find_first_of(Parser::NUMBERS, startOfObjectPosition);
      unsigned int endOfObjectPostion = _fileContent.find(" 00000 n", startOfObjectPosition);
      std::string objectPostionStr = _fileContent.substr(startOfObjectPosition, endOfObjectPostion - startOfObjectPosition);
      objectsAndSizes[i] = Utils::stringToInt(objectPostionStr);
      startOfObjectPosition = endOfObjectPostion + strlen(" 00000 n");
   }
}

unsigned int OverlayDocumentParser::_readTrailerAndReturnRoot()
{
   _getPartOfFileContent(- (3*DOC_PART_WITH_START_OF_XREF), (3*DOC_PART_WITH_START_OF_XREF));
   return Parser::_readTrailerAndReturnRoot();
}

unsigned int OverlayDocumentParser::_getStartOfXrefWithRoot()
{
   return 0;
}