Hi Dominik,

this patch makes AdjustBytes search for the ByteRange filler from the beacon position backwards.
It works for us when we add the ContentInfo key.
This was the only solution we tried.

Regards,
Pedro.

Em 08/11/2014 11:46, Dominik Seichter escreveu:

Hi Pedro

Sounds interesting. Can you provide a patch?

Regards
Dominik

Am 07.11.2014 05:09 schrieb "Pedro Lamarão" <pedro.lama...@prodist.com.br <mailto:pedro.lama...@prodist.com.br>>:

    Hello, all! I am successfully using PoDoFo to produce signed PDF
    files,
    many thanks to you all!

    I have discovered that the AdjustByteRanges has bad synergy with the
    dictionary implementation. AdjustByteRanges assume that the
    Contents key
    follows immediately after the ByteRange key. The dictionary
    implementation orders keys in alphabetic order. Therefore if one
    produces a document with the ContentInfo key in a signature field then
    AdjustByteRanges assumption breaks because ByteRange < ContentInfo <
    Contents.

    Suggested fixes are doing a linear search or using a custom dictionary
    order which always gives ByteRange < Contents < everything.

    Hope this helps!

    --
    Pedro Lamarão
    PRODIST TECHNOLOGIES
    www.prodist.com.br <http://www.prodist.com.br>
    Securing Critical Systems
    Tel.: (21) 4062-7188


    
------------------------------------------------------------------------------
    _______________________________________________
    Podofo-users mailing list
    Podofo-users@lists.sourceforge.net
    <mailto:Podofo-users@lists.sourceforge.net>
    https://lists.sourceforge.net/lists/listinfo/podofo-users



--
Pedro Lamarão
PRODIST TECHNOLOGIES
www.prodist.com.br
Securing Critical Systems
Tel.: (21) 4062-7188

--- 
C:/Users/PEDRO~1.LAM/AppData/Local/Temp/2/PdfSignOutputDevice.cpp-revBASE.svn001.tmp.cpp
    Sun Jul 20 17:00:29 2014
+++ P:/tmp/src/doc/PdfSignOutputDevice.cpp      Mon Nov 10 10:49:47 2014
@@ -137,6 +137,9 @@ void PdfSignOutputDevice::AdjustByteRange()
         PODOFO_RAISE_ERROR( ePdfError_InternalLogic );
     }
 
+    static char * const filler = "[ 0 1234567890 1234567890 1234567890]";
+    static size_t const fillerSize = sizeof("[ 0 1234567890 1234567890 
1234567890]") - 1;
+
     // Get final position
     size_t sFileEnd = GetLength();
     PdfArray arr;
@@ -147,34 +150,27 @@ void PdfSignOutputDevice::AdjustByteRange()
     std::string sPosition;
     PdfVariant(arr).ToString(sPosition, ePdfWriteMode_Compact);
     // Fill padding
-    unsigned int sPosSize = sizeof("[ 0 1234567890 1234567890 1234567890]")-1;
-    if(sPosition.size()<sPosSize)
+    if (sPosition.size() < fillerSize)
     {
-        // drop last ']'
-        sPosition.resize(sPosition.size()-1);
-        while(sPosition.size()<(sPosSize-1)) {
-            sPosition+=' ';
-        }
-        sPosition+=']';
-    }
+        sPosition.back() = ' ';
+        sPosition.resize(fillerSize, ' ');
+        sPosition.back() = ']';
+    }
 
-    m_pRealDevice->Seek(m_sBeaconPos-sPosition.size()-9);
-    char ch;
-    size_t offset = m_pRealDevice->Tell();
-
-    /* Sanity tests... */
-    PODOFO_RAISE_LOGIC_IF( m_pRealDevice->Read(&ch, 1) != 1, "Failed to read 1 
byte." );
-    if (ch == '0') {
-       /* probably clean write mode, whic means two more bytes back */
-       m_pRealDevice->Seek(m_sBeaconPos-sPosition.size()-11);
-       offset = m_pRealDevice->Tell();
-       PODOFO_RAISE_LOGIC_IF( m_pRealDevice->Read(&ch, 1) != 1, "Failed to 
read 1 byte." );
-    }
-
-    /* ...the file position should be at the '[' now */
-    PODOFO_RAISE_LOGIC_IF( ch != '[', "Failed to find byte range array start 
in the stream." );
-
-    m_pRealDevice->Seek(offset);
+    // Note: presumes ByteRange key precedes Contents key
+
+    m_pRealDevice->Seek(m_sBeaconPos - 9 - fillerSize);
+
+    auto offset = m_pRealDevice->Tell();
+    while (true)
+    {
+        char buffer [fillerSize] = { };
+        auto read = m_pRealDevice->Read(buffer, fillerSize);
+        if (strncmp(filler, buffer, fillerSize) == 0) break;
+        if (offset == 0) PODOFO_RAISE_ERROR_INFO( ePdfError_InternalLogic, 
"Failed to find byte range array start in the stream." );
+        m_pRealDevice->Seek(--offset);
+    }
+    m_pRealDevice->Seek(offset);
     m_pRealDevice->Write(sPosition.c_str(), sPosition.size());
 }
 
------------------------------------------------------------------------------
_______________________________________________
Podofo-users mailing list
Podofo-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/podofo-users

Reply via email to