Update of /cvsroot/mahogany/M/src/modules/viewflt
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19644/src/modules/viewflt

Modified Files:
        PGP.cpp 
Log Message:
don't show PGP ASCII armour even if we don't have a configured PGP command

Index: PGP.cpp
===================================================================
RCS file: /cvsroot/mahogany/M/src/modules/viewflt/PGP.cpp,v
retrieving revision 1.21
retrieving revision 1.22
diff -b -u -2 -r1.21 -r1.22
--- PGP.cpp     23 Apr 2004 16:00:03 -0000      1.21
+++ PGP.cpp     13 Jul 2005 16:20:32 -0000      1.22
@@ -69,4 +69,21 @@
 #define PGP_END_SIG_SUFFIX _T("SIGNATURE-----")
 
+// ----------------------------------------------------------------------------
+// module global functions
+// ----------------------------------------------------------------------------
+
+// checks whether the given string starts with this substring and advances it
+// past it if it does
+static inline bool AdvanceIfMatches(const wxChar **p, const wxChar *substr)
+{
+   const size_t len = wxStrlen(substr);
+   if ( wxStrncmp(*p, substr, len) != 0 )
+      return false;
+
+   *p += len;
+
+   return true;
+}
+
 // ============================================================================
 // PGPFilter implementation
@@ -143,38 +160,34 @@
    //
    // there should be a BEGIN line near the start of the message
-   bool foundBegin = false;
+   const wxChar *beginNext = NULL;
    const wxChar *start = text.c_str();
    for ( size_t numLines = 0; numLines < 10; numLines++ )
    {
-      if ( wxStrncmp(start, PGP_BEGIN_PREFIX, wxStrlen(PGP_BEGIN_PREFIX)) == 0 
)
+      const wxChar *p = start;
+      if ( AdvanceIfMatches(&p, PGP_BEGIN_PREFIX) )
       {
-         foundBegin = true;
+         beginNext = p;
          break;
       }
 
       // try the next line (but only if not already at the end)
-      if ( *start )
-         start = wxStrchr(start, '\n');
-      else
+      if ( !*p )
          break;
-      if ( start )
-         start++; // skip '\n' itself
-      else
+
+      p = wxStrchr(start, '\n');
+      if ( !p )
          break; // no more text
+
+      start = p + 1; // skip '\n' itself
    }
 
-   if ( foundBegin )
+   if ( beginNext )
    {
       // is the message just signed or encrypted?
-      const wxChar *tail = start + wxStrlen(PGP_BEGIN_PREFIX);
       bool isKey = false;
-      bool isSigned = wxStrncmp(tail, _T("SIGNED "), 7 /* strlen("SIGNED ") 
*/) == 0;
-      if ( isSigned )
-      {
-         tail += 7;
-      } 
-      else
+      const bool isSigned = AdvanceIfMatches(&beginNext, _T("SIGNED "));
+      if ( !isSigned )
       {
-         isKey = wxStrncmp(tail, _T("PUBLIC KEY "), 11 /* strlen("PUBLIC KEY 
") */) == 0;
+         isKey = AdvanceIfMatches(&beginNext, _T("PUBLIC KEY "));
       }
 
@@ -186,5 +199,5 @@
       bool ok = !isKey;
 
-      if ( ok && wxStrncmp(tail, PGP_BEGIN_SUFFIX, wxStrlen(PGP_BEGIN_SUFFIX)) 
!= 0 )
+      if ( ok && !AdvanceIfMatches(&beginNext, PGP_BEGIN_SUFFIX) )
       {
          wxLogWarning(_("The BEGIN line doesn't end with expected suffix."));
@@ -195,4 +208,5 @@
       // end of the PGP part
       const wxChar *end = NULL; // unneeded but suppresses the compiler warning
+      const wxChar *endNext = NULL; // same
       if ( ok ) // ok, it starts with something valid
       {
@@ -215,7 +229,7 @@
             pc++;
 
-            if ( wxStrncmp(pc, PGP_END_PREFIX, wxStrlen(PGP_END_PREFIX)) == 0 )
+            if ( AdvanceIfMatches(&pc, PGP_END_PREFIX) )
             {
-               tail = pc + wxStrlen(PGP_END_PREFIX);
+               endNext = pc;
 
                foundEnd = true;
@@ -251,5 +265,5 @@
                                               : PGP_END_CRYPT_SUFFIX;
 
-         if ( wxStrncmp(tail, suffix, wxStrlen(suffix)) != 0 )
+         if ( !AdvanceIfMatches(&endNext, suffix) )
          {
             wxLogWarning(_("Mismatch between BEGIN and END lines."));
@@ -280,10 +294,59 @@
          {
             // pass everything between start and end to PGP for verification
-            pgpInfo = ClickablePGPInfo::CreateFromSigStatusCode
-                      (
-                        m_engine->VerifySignature(in, out, log),
-                        m_msgView,
-                        log
-                      );
+            const MCryptoEngine::Status
+               rc = m_engine->VerifySignature(in, out, log);
+            pgpInfo = ClickablePGPInfo::
+                        CreateFromSigStatusCode(rc, m_msgView, log);
+
+            // if we failed to check the signature, we need to remove the
+            // BEGIN/END lines from output ourselves (otherwise it would have
+            // been done by VerifySignature() itself)
+            if ( rc != MCryptoEngine::OK )
+            {
+               // beginNext points to the end of BEGIN line, go forward to the
+               // end of the headers (signalled by an empty line i.e. 2 EOLs)
+               beginNext = wxStrstr(beginNext, _T("\r\n\r\n"));
+               if ( beginNext )
+               {
+                  // endNext currently points to the end of END PGP SIGNATURE
+                  // line, rewind to the PGP_BEGIN_SIG line
+                  const wxChar *pc = endNext;
+                  for ( ;; )
+                  {
+                     // find the beginning of this line
+                     while ( *pc != '\n' && pc >= start )
+                     {
+                        pc--;
+                     }
+
+                     if ( pc < start )
+                     {
+                        // we exhausted the message without finding the
+                        // PGP_BEGIN_SIG line
+                        break;
+                     }
+
+                     pc++; // skip the "\n"
+
+                     if ( AdvanceIfMatches(&pc, PGP_BEGIN_SIG) )
+                     {
+                        // chop off PGP_BEGIN_SIG line as well
+                        while ( *pc != '\n' && pc >= start )
+                           pc--;
+
+                        if ( pc > start )
+                        {
+                           out = String(beginNext + 4, pc - 1);
+                        }
+
+                        break;
+                     }
+
+                     pc -= 3; // rewind beyond "\r\n"
+                     ASSERT_MSG( pc[1] == '\r',
+                                  _T("line doesn't end in\"\\r\\n\"?") );
+                  }
+               }
+            }
          }
          else // encrypted
@@ -311,6 +374,4 @@
          m_next->Process(out, viewer, style);
 
-         if ( pgpInfo )
-         {
             pgpInfo->SetLog(log);
             pgpInfo->SetRaw(in);
@@ -324,9 +385,4 @@
 
             viewer->InsertText(_T("\r\n"), style);
-         }
-         else // if log is not given to pgpInfo we need to delete it ourselves
-         {
-            delete log;
-         }
 
          // output the part after the END line, if any



-------------------------------------------------------
This SF.Net email is sponsored by the 'Do More With Dual!' webinar happening
July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual
core and dual graphics technology at this free one hour event hosted by HP,
AMD, and NVIDIA.  To register visit http://www.hp.com/go/dualwebinar
_______________________________________________
Mahogany-cvsupdates mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mahogany-cvsupdates

Reply via email to