From ebed34719d5adfcfecfbf0431ffa212e5c32511c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Tue, 9 Mar 1999 20:46:40 +0100
Subject: [PATCH] Copied \r-fixes from pike-0.7

Rev: lib/modules/Tools.pmod/PEM.pmod:1.4
Rev: lib/modules/Tools.pmod/_PEM.pmod:1.2
---
 lib/modules/Tools.pmod/PEM.pmod  | 21 +++++++++++++++++----
 lib/modules/Tools.pmod/_PEM.pmod | 17 +++++++++++++++--
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/lib/modules/Tools.pmod/PEM.pmod b/lib/modules/Tools.pmod/PEM.pmod
index 0f56a5db08..35b5383c31 100644
--- a/lib/modules/Tools.pmod/PEM.pmod
+++ b/lib/modules/Tools.pmod/PEM.pmod
@@ -98,6 +98,9 @@ class rfc934 {
 
     for(; i < sizeof(parts); i++)
       {
+#ifdef PEM_DEBUG
+	werror(sprintf("parts[%d] = '%s'\n", i, parts[i]));
+#endif
 	if (sizeof(parts[i]) && (parts[i][0] == ' '))
 	  {
 	    /* Escape, just remove the "- " prefix */
@@ -123,10 +126,20 @@ class rfc934 {
 
 	int end = search(parts[i], "\n");
 	if (end >= 0)
-	  {
-	    boundary = "-" + parts[i][..end-1];
-	    current = parts[i][end..];
-	  }
+	{
+	  boundary = "-" + parts[i][..end-1];
+	  current = parts[i][end..];
+	} else {
+	  /* This is a special case that happens if the input data had
+	   * no terminating newline after the final boundary. */
+#ifdef PEM_DEBUG
+	  werror(sprintf("Final boundary, with no terminating newline.\n"
+			 "  boundary='%s'\n", boundary));
+#endif
+
+	  boundary = "-" + parts[i];
+	  break;
+	}
       }
     final_text = current;
     final_boundary = boundary;
diff --git a/lib/modules/Tools.pmod/_PEM.pmod b/lib/modules/Tools.pmod/_PEM.pmod
index 96adbf9387..b17c979677 100644
--- a/lib/modules/Tools.pmod/_PEM.pmod
+++ b/lib/modules/Tools.pmod/_PEM.pmod
@@ -10,9 +10,13 @@ object rfc822_start_re = Regexp("^([-a-zA-Z][a-zA-Z0-9]*[ \t]*:|[ \t]*\n\n)");
 
 /* Regexp used to extract the interesting part of an encapsulation
  * boundary. Also strips spaces, and requires that the string in the
- * middle between ---foo  --- is at least two characters long. */
+ * middle between ---foo  --- is at least two characters long. Also
+ * allow a trailing \r or other white space characters. */
 
-object rfc934_eb_re = Regexp("^-*[ \t]*([^- \t].*[^- \t])[ \t]*-*$");
+object rfc934_eb_re = Regexp(
+  "^-*[ \r\t]*([^- \r\t]"	/* First non dash-or-space character */
+  ".*[^- \r\t])"		/* Last non dash-or-space character */
+  "[ \r\t]*-*[ \r\t]*$");	/* Trailing space, dashes and space */
 
 /* Start and end markers for PEM */
 
@@ -44,6 +48,15 @@ string dash_stuff(string msg)
   return parts * "- -";
 }
 
+#if 0
+string chop_cr(string s)
+{
+  return (strlen(s) && (s[-1] == '\r'))
+    ? s[..strlen(s) - 2]
+    : s;
+}
+#endif
+
 /* Strip dashes */
 string extract_boundary(string s)
 {
-- 
GitLab