Object.cpp 17.8 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
Claudio Valerio's avatar
Claudio Valerio committed
5 6 7 8 9 10 11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Claudio Valerio's avatar
Claudio Valerio committed
15 16 17 18 19 20 21
#include "Object.h"
#include "Parser.h"
#include "Exception.h"
#include <string.h>
#include <algorithm>
#include <fstream>

22 23
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
24 25 26 27 28 29 30 31 32 33 34 35 36 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
using namespace merge_lib;

std::string NUMBERANDWHITESPACE(" 0123456789");


Object::~Object()
{   	
   _parents.clear();
   _children.clear();
   _content.clear();
}

Object * Object::getClone(std::vector<Object *> & clones)
{
   std::map<unsigned int, Object *> clonesMap;
   Object * clone = _getClone(clonesMap);
   std::map<unsigned int, Object *>::iterator conesIterator = clonesMap.begin();
   for(; conesIterator != clonesMap.end(); ++conesIterator)
      clones.push_back((*conesIterator).second);
   clonesMap.clear();
   resetIsPassed();
   return clone;
}

Object * Object::_getClone(std::map<unsigned int, Object *> & clones)
{
   _isPassed = true;
   unsigned int objectNumber = this->getObjectNumber();   
   Object * clone = new Object(objectNumber, this->_generationNumber, this->getObjectContent(), _fileName, _streamBounds, _hasStream);
   clone->_hasStreamInContent = _hasStreamInContent;
   clones.insert(std::pair<unsigned int, Object *>(objectNumber, clone));
   Children::iterator currentChild = _children.begin();

   for(; currentChild != _children.end(); ++currentChild)
   {
      Object * currentObject = (*currentChild).second.first;
      unsigned int childObjectNumber = currentObject->getObjectNumber();

      Object * cloneOfCurrentChild = 0;

      if(currentObject->isPassed())
      {
         cloneOfCurrentChild = clones[childObjectNumber];
      }
      else
      {
         cloneOfCurrentChild = currentObject->_getClone(clones);
      }
      ChildAndItPositionInContent newChild(
         cloneOfCurrentChild, 
         currentChild->second.second);

      clone->_children.insert(std::pair<unsigned int, ChildAndItPositionInContent>
         (newChild.first->getObjectNumber(), newChild)
         );
   }
   return clone;

}
void Object::addChild(Object * child, const std::vector<unsigned int> childPositionsInContent)
{
   child->_addParent(this);
   _addChild(child, childPositionsInContent);
}


Object::ReferencePositionsInContent Object::removeChild(Object * child)
{
   ReferencePositionsInContent positions = _children[child->getObjectNumber()].second;
   _children.erase(child->getObjectNumber());
   return positions;
}

void Object::forgetAboutChildren(unsigned int leftBound, unsigned int rightBound)
{
   std::vector<Object *> children = getChildrenByBounds(leftBound, rightBound);
   for(size_t i = 0; i < children.size(); ++i)
   {	     
      _children.erase(_children.find(children[i]->getObjectNumber()));		 
   }
}


Object * Object::getChild(unsigned int objectNumber)
{
   //TODO: check object before returning
   return _children[objectNumber].first;
}

std::vector<Object *> Object::getChildrenByBounds(unsigned int leftBound, unsigned int rightBound)
{
   std::vector<Object *> result;
   for(Children::iterator currentChild = _children.begin(); currentChild != _children.end(); ++currentChild)
   {
      ReferencePositionsInContent childPositions = (*currentChild).second.second;
      for(size_t i = 0; i < childPositions.size(); ++i)
      {
         unsigned int childPosition = childPositions[i];
         if((childPosition >= leftBound) &&  (childPosition <= rightBound))
         {
            result.push_back( (*currentChild).second.first);
            break;
         }
      }
   }
   return result;
}


std::vector<Object *> Object::getSortedByPositionChildren(unsigned int leftBound, unsigned int rightBound)
{
   std::vector<Object *> result;
   for(Children::iterator currentChild = _children.begin(); currentChild != _children.end(); ++currentChild)
   {
      ReferencePositionsInContent childPositions = (*currentChild).second.second;
      for(size_t i = 0; i < childPositions.size(); ++i)
      {
         unsigned int childPosition = childPositions[i];
         if((childPosition >= leftBound) &&  (childPosition <= rightBound))
         {
            unsigned int insertPosition = 0;
            for(unsigned int j(0); j < result.size(); ++j)
               if(childPosition > getChildPosition(result[j]))
                  insertPosition = j + 1;
            result.insert(result.begin() + insertPosition, (*currentChild).second.first);
            break;
         }
      }
   }
   return result;

}


unsigned int Object::getChildPosition(const Object * child)//throw (Exception)
{
   const ReferencePositionsInContent & childrenPostion = _children[child->getObjectNumber()].second;
   if(
      (childrenPostion.size() != 1) ||
      (_children[child->getObjectNumber()].first != child)
      )
      throw Exception("Internal error or wrong document (some reference is found twise)");
   return childrenPostion[0];
}

void Object::removeChildrenByBounds(unsigned int leftBound, unsigned int rightBound)
{
   std::vector<Object *> children = getChildrenByBounds(leftBound, rightBound);
   for(size_t i = 0; i < children.size(); ++i)
   {
      children[i]->removeHimself();
   }
}


const Object::Children & Object::getChildren()
{
   return _children;
}

void Object::removeHimself()
{
   if(!_parents.empty())
   {
      std::set<Object *>::iterator currentParent = _parents.begin();
      for(; currentParent != _parents.end(); ++currentParent)
      {
         (*currentParent)->removeChild(this);
      }
   }
}
unsigned int Object::getObjectNumber() const
{
   return _number;
}

unsigned int Object::getgenerationNumber() const
{
   return _generationNumber;
}

std::string & Object::getObjectContent()
{
   return _content;
}

void Object::_setObjectNumber(unsigned int objectNumber)
{
   if(!isPassed())
   {
      _isPassed = true;
      _oldNumber = _number;
      _number = objectNumber;
   }
}

void Object::setObjectContent(const std::string & objectContent)
{
   _content = objectContent;
}

void Object::appendContent(const std::string & addToContent)
{
   _content.append(addToContent);
}

void Object::eraseContent(unsigned int from, unsigned int size)
{
   int iSize = size;
   _recalculateReferencePositions(from + size, -iSize);
   _content.erase(from, size);
}

void Object::insertToContent(unsigned int position, const std::string & insertedStr)
{
   _recalculateReferencePositions(position, insertedStr.size());
   _content.insert(position, insertedStr);
}

void Object::insertToContent(unsigned int position, const char * insertedStr, unsigned int length)
{	
   _recalculateReferencePositions(position, length);
   _content.insert(position, insertedStr, length);	
}

//vector <object number, its size>
void Object::serialize(std::ofstream & out, std::map< unsigned int, std::pair<unsigned long long, unsigned int > > & sizesAndGenerationNumbers)
{
   //is this element already printed
   if(sizesAndGenerationNumbers.find(_number) != sizesAndGenerationNumbers.end()) return;

   std::string stream;
   if(_hasStream && !_hasStreamInContent)
   {	   
      getStream(stream);
      stream.append("endstream\n");	   
   }
   // xxxx + " " + "0" + " " + "obj" + "\n" + _content.size() + "endobj\n", where x - is a digit
   unsigned long long objectSizeForXref = (static_cast<unsigned int>(std::log10(static_cast<double>(_number))) + 1) + 14 + _content.size() + stream.size();    

   sizesAndGenerationNumbers.insert(std::pair<unsigned int, std::pair<unsigned long long, unsigned int > >(_number, std::make_pair(objectSizeForXref, _generationNumber)));

   _serialize(out, stream);
   stream.clear();
   stream.reserve();

   //call serialize of each child
   Children::iterator it;
   for ( it=_children.begin() ; it != _children.end(); it++ )
   {
      Object * currentChild = (*it).second.first;
      currentChild->serialize(out, sizesAndGenerationNumbers);
   }
}
void Object::recalculateObjectNumbers(unsigned int & newNumber)
{	
   _recalculateObjectNumbers(newNumber);
   resetIsPassed();
}

void Object::_recalculateObjectNumbers(unsigned int & newNumber)
{	
   _setObjectNumber(newNumber);

   Children::iterator childIterator;
   for ( childIterator = _children.begin() ; childIterator != _children.end(); ++childIterator )
   {
      Object * currentChild = (*childIterator).second.first;
      if(currentChild->isPassed()) continue;                
      currentChild->_recalculateObjectNumbers(++newNumber);        
   }

   //recalculate referencies in content
   for ( childIterator = _children.begin() ; childIterator != _children.end(); ++childIterator)
   {    
      Object * currentChild = (*childIterator).second.first;
      //if(currentChild->getOldNumber() == currentChild->getObjectNumber()) continue;
      const ReferencePositionsInContent & refPositionForcurrentChild = (*childIterator).second.second;
      const std::string & oldNumberStr = Utils::uIntToStr(currentChild->getOldNumber());
      const std::string & newNumber = Utils::uIntToStr(currentChild->getObjectNumber());
      const unsigned int newNumberStringSize = newNumber.size();
      const unsigned int oldNumberStringSize = oldNumberStr.size();
      unsigned int diff = newNumberStringSize;
      if (newNumberStringSize > oldNumberStringSize)
      {

         for(size_t referencePositionIter(0); referencePositionIter < refPositionForcurrentChild.size(); ++referencePositionIter)               
         {
            _recalculateReferencePositions(refPositionForcurrentChild[referencePositionIter], newNumberStringSize - oldNumberStringSize);
            for(size_t referenceStringInter(oldNumberStringSize); referenceStringInter < newNumberStringSize; ++referenceStringInter )  
               _content.insert(
               refPositionForcurrentChild[referencePositionIter] + referenceStringInter, 
               1,
               newNumber[referenceStringInter]);                
         }
         diff = oldNumberStringSize;
      }
      if (newNumberStringSize < oldNumberStringSize)
      {
         for(size_t referencePositionIter(0); referencePositionIter < refPositionForcurrentChild.size(); ++referencePositionIter)
         {
            _recalculateReferencePositions(refPositionForcurrentChild[referencePositionIter], newNumberStringSize - oldNumberStringSize);
            _content.erase(refPositionForcurrentChild[referencePositionIter] + newNumberStringSize, 
               oldNumberStringSize - newNumberStringSize
               );
         }
      }    

      for(unsigned int i = 0; i < diff; i++)
         for(size_t referencePositionIter(0); referencePositionIter < refPositionForcurrentChild.size(); ++referencePositionIter)            
            _content[i + refPositionForcurrentChild[referencePositionIter]] = newNumber[i];


   }    
}

//this method should be called in case changing object's content
void Object::_recalculateReferencePositions(unsigned int changedReference, int displacement)
{
   Children::iterator childIterator;
   for ( childIterator = _children.begin() ; childIterator != _children.end(); ++childIterator )
   {
      ReferencePositionsInContent & refPositionForcurrentChild = (*childIterator).second.second;
      for(size_t i = 0; i < refPositionForcurrentChild.size(); ++i)
         if(refPositionForcurrentChild[i] > changedReference)
            refPositionForcurrentChild[i] += displacement;

   }
}


void Object::_retrieveMaxObjectNumber(unsigned int & maxNumber)
{

   if(isPassed())  return;
   _isPassed = true;
   if(maxNumber < _number)
      maxNumber = _number;
   Children::iterator it;
   for ( it=_children.begin() ; it != _children.end(); ++it )
      (*it).second.first->_retrieveMaxObjectNumber(maxNumber);       
}

//TODO add check for absent token
bool Object::_findObject(const std::string & token, Object* & foundObject, unsigned int & tokenPositionInContent)
{
   _isPassed = true;
   tokenPositionInContent = Parser::findToken(_content,token);
372
   if((int)tokenPositionInContent != -1)
Claudio Valerio's avatar
Claudio Valerio committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
   {
      foundObject = this;
      return true;
   }      
   for (Children::iterator  it=_children.begin() ; it != _children.end(); ++it )
      if((!(*it).second.first->_isPassed) &&
         ((*it).second.first->_findObject(token, foundObject, tokenPositionInContent)))
         return true;

   return false;
}

bool Object::findObject(const std::string & token, Object* & foundObject, unsigned int & tokenPositionInContent)
{
   bool result = _findObject(token, foundObject, tokenPositionInContent);
   resetIsPassed();
   if(result)
   {	
      return true;
   }
   return false;
}

void Object::retrieveMaxObjectNumber(unsigned int & maxNumber)
{
   _retrieveMaxObjectNumber(maxNumber);
   resetIsPassed();   
}

//methods 
void Object::_addChild(Object * child, const ReferencePositionsInContent & childPositionsInContent)
{
   ChildAndItPositionInContent childAndItPositions(child, childPositionsInContent);
   unsigned int childObjectNumber = child->getObjectNumber();
   while(_children.count(childObjectNumber))
      ++childObjectNumber;
   _children.insert(std::pair<unsigned int, ChildAndItPositionInContent > (childObjectNumber, childAndItPositions));
}


void Object::_addParent(Object * child)
{
   _parents.insert(child);
}
void Object::_serialize(std::ofstream  & out, const std::string & stream)
{
	out << _number << " " << _generationNumber << " obj\n" << _content << stream << "endobj\n";
   out.flush();
}

/** @brief getStream
*
* @todo: document this function
*/
bool Object::getStream(std::string & stream)
{
   if(!_hasStream && !_hasStreamInContent)
      return false;
   if( _hasStream && _hasStreamInContent)
   {
      if(_getStreamFromContent(stream))
         return true;
      else
         return false;
   }

   std::ifstream pdfFile;
   pdfFile.open (_fileName.c_str(), std::ios::binary );
   if (pdfFile.fail())
   {
      std::stringstream errorMessage("File ");
      errorMessage << _fileName << " is absent" << "\0";
      throw Exception(errorMessage);
   }
   // get length of file:
   int length = _streamBounds.second - _streamBounds.first;
   pdfFile.seekg (_streamBounds.first, std::ios_base::beg);
   stream.resize(length);
   pdfFile.read(&stream[0], length);   
   pdfFile.close();
   return true;
}

bool Object::_getStreamFromContent(std::string & stream)
{
   size_t stream_begin = _content.find("stream");
459
   if((int) stream_begin == -1 )
Claudio Valerio's avatar
Claudio Valerio committed
460 461 462 463
   {
      return false;
   }
   size_t stream_end = _content.find("endstream",stream_begin);
464
   if((int) stream_end == -1 )
Claudio Valerio's avatar
Claudio Valerio committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
   {
      return false;
   }
   stream_begin += strlen("stream");
   // need to skip trailing \r
   while(_content[stream_begin] == '\r')
   {
      stream_begin ++;
   }
   if( _content[stream_begin] == '\n')
   {
      stream_begin ++;
   }

   stream = _content.substr(stream_begin, stream_end - stream_begin);
   return true;
}


/** @brief getHeader
*
* @todo: document this function
*/
bool Object::getHeader(std::string &content)
{
   if( !hasStream() )
   {
      content = _content;
      return true;
   }
   size_t stream_begin = _content.find("stream");
   content = _content.substr(0,stream_begin);
   return true;
}


/** @brief hasStream
*
* @todo: document this function
*/
bool Object::hasStream()
{
   return _hasStream;
}

// the method returns the value of some object.
// For example, .../Length 123 /Filter will return  123
// For /Length 12 0 R will return the content of 12 0 obj
std::string Object::getNameSimpleValue(const std::string &content, const std::string &pattern, size_t start)
{
   size_t foundStart, foundEnd;
   std::string token = Parser::findTokenStr(content,pattern,start,foundStart,foundEnd);

   std::string value;
   size_t beg = 0;

   // Now token could be /Length 127 or /Length 12 0 R
   if( Parser::getNextWord(value,token,beg) )
   {
      // 127 or 12 
      std::string interm;
      if( Parser::getNextWord(interm,token,beg) )  // 0?
      {
         if( Parser::getNextWord(interm,token,beg) ) // R
         {
            if( interm == "R" )  // we found reference to object!
            {
               int number = Utils::stringToInt(value);
               Object *child = getChild(number);
               if( child )
               {
                  value = child->getObjectContent();
                  Parser::trim(value);                  
               }
               else
               {
                  std::cerr<<"Error::child object with number "<<number<<"is absent\n";
               }
            }
            else
            {
               std::cerr<<"Error:undefined format of token "<<token<<"\n";
            }
         }
         else
         {
            std::cerr<<"Error:undefined word"<<interm<<"\n";
         }
      }
   }
   return value;
}

Object* Object::findPatternInObjOrParents(const std::string &pattern)
{
   std::string content=getObjectContent();
561
   if((int) Parser::findToken(content,pattern,0) != -1 )
Claudio Valerio's avatar
Claudio Valerio committed
562 563 564 565 566 567 568 569 570 571
   {
      return this;
   }

   Object * parent = this;
   Object *foundObj = NULL;
   while(1)
   {
      unsigned int startOfParent = content.find("/Parent");
      unsigned int endOfParent = content.find(" R", startOfParent);
572
      if((int)startOfParent == -1)
Claudio Valerio's avatar
Claudio Valerio committed
573 574 575 576 577 578 579 580 581 582 583
      {
         break;
      }
      std::vector <Object *> parents = parent->getChildrenByBounds(startOfParent, endOfParent);
      if( parents.size() != 1 )
      {
         break;
      }
      parent = parents[0];
      std::string parentContent = parent->getObjectContent();
      unsigned int startOfPattern = parentContent.find(pattern);
584
      if((int)startOfPattern == -1)
Claudio Valerio's avatar
Claudio Valerio committed
585 586 587 588 589 590 591 592 593 594
      {
         content = parentContent;
         continue;
      }
      foundObj = parent;
      break;
   }
   return foundObj;
}