Show quoted text>use warnings;
But I always do, I swear! It's File.pm who doesn't! And lexically scoped "use warnings;" in my .pl can't help. OTOH, "-w" switch on command (shebang) line sets global $^W. A bit further off topic, CAM::PDF does "use warnings;", and when I modified it, in quite similar way, for internal use, to write XRef streams some years ago, the case with 65535 was caught. "Transferring" that patch to PDF::API2, I just forgot to zero gennum of 0th object. Sorry. If only I didn't forget, I hadn't to write all of the following :)
Show quoted text>Let me rephrase my question, then -- is the correct result to output the original, unchanged (almost) PDF, and then tack on these new and replacement objects, and maybe a cross reference stream, after the original %%EOF? You seem to have said "yes".
Yes. I'll try to clarify further, sorry if it may sound primitive. For performance reasons, many file formats allow incremental updates, with changes appended to intact original. (Not "almost", but 100% intact.) In GUIs, it's usually "Save" for incremental update, and "SaveAs" for clean re-write, not necessarily to another filename. It's same difference in CAM::PDF's methods "output" and "cleanoutput".
With "cleanoutput", all objects are re-numbered consecutively, getting fresh gennum of "0", and "holes" of ranges of free objects are eliminated. PDF::API2 simply can't do "cleanoutput". If file is opened and then saved, it's always incremental update, however confusing method's name "saveas" is. Even if all objects were changed and original content becomes useless, it's stored intact as it was, and new content is appended.
My patch in #121832 was an attempt to teach PDF::API2 to "cleanoutput". Now I think it's not worth it, since it can only output old-fashioned 1.4 classical XRef table, it tries complex and possibly fragile not-tested-enough things (as opposed to simple patch discussed in this thread), and nobody seems interested.
New patch only serves one purpose: users now can modify "modern" PDF files without necessity to downgrade them to 1.4 as preliminary step, and worrying why Reader can't read their files and whether PDF::API2 works at all, or not. But it's same old incremental update.
Show quoted text>Is that the only place that a value greater than xFF is ever going to show up?
OK, consider this. For gennum to become > 255, PDF file has to be updated at least 510 times. This minimum of 510 is possible if pattern of updates is strictly that each odd save removes an object (objnum marked free on save), and some info (completely new indirect object) is added on each even save (objnum re-used, gennum increased). If this pattern is not strict, gennum ever gets to > 255 after more, possibly much more number of updates. Multiply probability of such scenario by chance that people torturing this file never get worried about file size bloat, so they don't reset the progression by issuing "SaveAs" command somewhere in the middle.
Note, all of the above still happens in 1.4 era, with classical XRef table. If file gets to PDF::API2 in this state with any gennum >255 -- fine, it's not an issue for patch discussed.
If this file is updated to 1.5 before getting to PDF::API2 -- fine too, it was a clean "SaveAs", all gennums reset and never touched again by Acrobat/Reader.
That's why my suggestion was to set gennum to 0 instead of 255 -- it would be same "0" as in files saved with Reader. But in the end it's probably not so important.
Show quoted text>Should we consider treating it as 16-bits, if the standard permits
Is there any software that still tracks free list and re-uses objnums and increases gennums, even in Xref streams, and regardless of Adobe's own stance? I don't know! Have never heard of. I'd solve problems when (and if) they come: if anyone files a bug report and we see that it's because we (wrongly, it will appear) assumed gennums always zero (well, less than 255) in XRef streams -- fine, we'll know how to fix -- i.e. to use 'Cnn' ('CNn') template.