Skip to content
This repository has been archived by the owner on Nov 27, 2021. It is now read-only.

Commit

Permalink
Fix quoted-printable encoding (1.0.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
timmyRS committed Feb 24, 2019
1 parent ff6efd3 commit 4a83a2e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jSMTP is using slf4j, so Maven is the best way to include jSMTP:
<dependency>
<groupId>sh.hell</groupId>
<artifactId>jsmtp</artifactId>
<version>[1.0.0,2.0.0)</version>
<version>[1.0.1,2.0.0)</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>sh.hell</groupId>
<artifactId>jsmtp</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand Down
37 changes: 19 additions & 18 deletions src/sh/hell/jsmtp/content/SMTPEncoding.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sh.hell.jsmtp.content;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;

public enum SMTPEncoding
Expand Down Expand Up @@ -34,15 +35,15 @@ public String encode(String text)
if(this == QUOTED_PRINTABLE)
{
StringBuilder str = new StringBuilder();
for(char c : text.toCharArray())
for(byte b : text.getBytes(StandardCharsets.UTF_8))
{
if((c < 33 || c > 126 || c == 61) && c != 13 && c != 10)
if((b < 33 || b > 126 || b == 61) && b != 9 && b != 32)
{
str.append("=").append(String.format("%2X", (byte) c).replace(" ", "0"));
str.append("=").append(String.format("%2X", b).replace(" ", "0"));
}
else
{
str.append(c);
str.append((char) b);
}
}
return str.toString();
Expand All @@ -58,39 +59,39 @@ public String decode(String text)
{
if(this == QUOTED_PRINTABLE)
{
StringBuilder str = new StringBuilder();
int decoding = 0;
final ArrayList<Byte> content = new ArrayList<>();
StringBuilder decodeChars = new StringBuilder();
for(char c : text.toCharArray())
{
if(decoding > 0)
{
if(c > 32 && c < 127 && c != 61)
decodeChars.append(c);
if(decoding++ >= 2)
{
decodeChars.append(c);
if(decoding != 2)
{
decoding++;
continue;
}
str.append((char) Integer.parseInt(decodeChars.toString(), 16));
content.add((byte) Integer.parseInt(decodeChars.toString(), 16));
decoding = 0;
decodeChars = new StringBuilder();
}
decoding = 0;
decodeChars = new StringBuilder();
}
else
{
if(c == 61) // =
if(c == '=')
{
decoding = 1;
}
else
{
str.append(c);
content.add((byte) c);
}
}
}
return str.toString();
byte[] bytearr = new byte[content.size()];
for(int i = 0; i < content.size(); i++)
{
bytearr[i] = content.get(i);
}
return new String(bytearr, StandardCharsets.UTF_8);
}
else if(this == BASE64)
{
Expand Down
8 changes: 4 additions & 4 deletions testsrc/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class Tests
@Test(timeout = 1000L)
public void testEncodingAndDecoding()
{
final String text = "Hêlló, wörld!\n\n.\n";
assertEquals(text, SMTPEncoding.EIGHTBIT.decode(SMTPEncoding.EIGHTBIT.encode(text)));
assertEquals(text, SMTPEncoding.QUOTED_PRINTABLE.decode(SMTPEncoding.QUOTED_PRINTABLE.encode(text)));
assertEquals(text, SMTPEncoding.BASE64.decode(SMTPEncoding.BASE64.encode(text)));
assertEquals("H=C3=AAll=C3=B3, w=C3=B6rld!", SMTPEncoding.QUOTED_PRINTABLE.encode("Hêlló, wörld!"));
assertEquals("Hêlló, wörld!", SMTPEncoding.QUOTED_PRINTABLE.decode("H=C3=AAll=C3=B3, w=C3=B6rld!"));
assertEquals("SMOqbGzDsywgd8O2cmxkIQ==", SMTPEncoding.BASE64.encode("Hêlló, wörld!"));
assertEquals("Hêlló, wörld!", SMTPEncoding.BASE64.decode("SMOqbGzDsywgd8O2cmxkIQ=="));
}

@Test(timeout = 5000L)
Expand Down

0 comments on commit 4a83a2e

Please sign in to comment.