Be sure Size contains max ID

This commit is contained in:
Grégory Soutadé 2022-12-23 16:55:09 +01:00
parent 9d56c1d0b1
commit 988662aa99

View File

@ -605,6 +605,8 @@ namespace uPDFParser
char* subs, c; char* subs, c;
int ret; int ret;
ret = read(fd, buffer, sizeof(buffer)); ret = read(fd, buffer, sizeof(buffer));
if (ret <= 0)
EXCEPTION(TRUNCATED_FILE, "Unexpected end of file");
subs = (char*)memmem((void*)buffer, ret, (void*)"endstream", 9); subs = (char*)memmem((void*)buffer, ret, (void*)"endstream", 9);
if (subs) if (subs)
{ {
@ -852,6 +854,7 @@ namespace uPDFParser
::write(newFd, "\r", 1); ::write(newFd, "\r", 1);
int maxId = 0;
std::stringstream xref; std::stringstream xref;
int nbNewObjects = 0; int nbNewObjects = 0;
@ -861,14 +864,17 @@ namespace uPDFParser
std::vector<Object*>::iterator it; std::vector<Object*>::iterator it;
for(it=_objects.begin(); it!=_objects.end(); it++) for(it=_objects.begin(); it!=_objects.end(); it++)
{ {
if (!(*it)->isNew()) Object* object = *it;
if (object->objectId() > maxId)
maxId = object->objectId();
if (!object->isNew())
continue; continue;
nbNewObjects ++; nbNewObjects ++;
std::string objStr = (*it)->str(); std::string objStr = object->str();
curOffset = lseek(newFd, 0, SEEK_CUR); curOffset = lseek(newFd, 0, SEEK_CUR);
::write(newFd, objStr.c_str(), objStr.size()); ::write(newFd, objStr.c_str(), objStr.size());
xref << std::setw(0) << (*it)->objectId() << " 1\n"; xref << std::setw(0) << object->objectId() << " 1\n";
xref << std::setw(10) << curOffset << " " << std::setw(5) << (*it)->generationNumber() << " n\r\n"; // Here \r seems important xref << std::setw(10) << curOffset << " " << std::setw(5) << object->generationNumber() << " n\r\n"; // Here \r seems important
} }
if (!nbNewObjects) if (!nbNewObjects)
@ -885,6 +891,8 @@ namespace uPDFParser
trailer.deleteKey("Prev"); trailer.deleteKey("Prev");
if (xrefOffset != (off_t)-1) if (xrefOffset != (off_t)-1)
trailer.dictionary().addData("Prev", new Integer((int)xrefOffset)); trailer.dictionary().addData("Prev", new Integer((int)xrefOffset));
trailer.deleteKey("Size");
trailer.dictionary().addData("Size", new Integer(maxId+1));
std::string trailerStr = trailer.dictionary().str(); std::string trailerStr = trailer.dictionary().str();
::write(newFd, "trailer\n", 8); ::write(newFd, "trailer\n", 8);