From d5b0e5c612bfa6c7fca8460c42648f568ba71a1e Mon Sep 17 00:00:00 2001 From: David Holdeman Date: Fri, 8 Nov 2024 17:23:39 +0800 Subject: [PATCH 1/2] test wrapping period test paths where encoding is not needed test line beginning with period --- test/mail/encoders/quoted_printable_test.exs | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/mail/encoders/quoted_printable_test.exs b/test/mail/encoders/quoted_printable_test.exs index 71f4087..d96d97c 100644 --- a/test/mail/encoders/quoted_printable_test.exs +++ b/test/mail/encoders/quoted_printable_test.exs @@ -146,6 +146,42 @@ defmodule Mail.Encoders.QuotedPrintableTest do assert Mail.Encoders.QuotedPrintable.encode(message) == encoding end + test "encodes line beginning with a period" do + message = ".xxx" + + encoding = + "=2Exxx" + + assert Mail.Encoders.QuotedPrintable.encode(message) == encoding + end + + test "encodes 75 chars ending with a period" do + message = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx." + + encoding = + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx." + + assert Mail.Encoders.QuotedPrintable.encode(message) == encoding + end + + test "encodes 76 chars ending with a period" do + message = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx." + + encoding = + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=\r\n=2E" + + assert Mail.Encoders.QuotedPrintable.encode(message) == encoding + end + + test "encodes 77 chars ending with a period" do + message = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx." + + encoding = + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=\r\nx." + + assert Mail.Encoders.QuotedPrintable.encode(message) == encoding + end + test "decodes empty string" do assert Mail.Encoders.QuotedPrintable.decode("") == "" end From db25a142331a641a6904c0dcd68554f65b9a4e19 Mon Sep 17 00:00:00 2001 From: David Holdeman Date: Fri, 8 Nov 2024 17:51:06 +0800 Subject: [PATCH 2/2] encode period --- lib/mail/encoders/quoted_printable.ex | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/mail/encoders/quoted_printable.ex b/lib/mail/encoders/quoted_printable.ex index 1e5863e..a0ed783 100644 --- a/lib/mail/encoders/quoted_printable.ex +++ b/lib/mail/encoders/quoted_printable.ex @@ -8,7 +8,7 @@ defmodule Mail.Encoders.QuotedPrintable do @new_line "=\r\n" @max_length 76 - @reserved_chars [?=, ??, ?_] + @reserved_chars [?=, ??, ?_, ?.] @doc """ Encodes a string into a quoted-printable encoded string. @@ -55,6 +55,21 @@ defmodule Mail.Encoders.QuotedPrintable do end end + # Encode ASCII period character + def encode(<>, max_length, acc, line_length) when char in [?.] do + escaped = "=" <> Base.encode16(<>) + line_length = line_length + byte_size(<>) + + case line_length do + 1 -> + encode(tail, max_length, acc <> escaped, byte_size(escaped)) + x when x < max_length -> + encode(tail, max_length, acc <> <>, line_length) + _ -> + encode(tail, max_length, acc <> @new_line <> escaped, byte_size(escaped)) + end + end + # Encode all other characters. def encode(<>, max_length, acc, line_length) do escaped = "=" <> Base.encode16(<>)