1 /*
 2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package org.openjdk.skara.email;
24 
25 import java.io.UnsupportedEncodingException;
26 import java.nio.charset.StandardCharsets;
27 import java.util.Base64;
28 import java.util.regex.Pattern;
29 
30 public class MimeText {
31     private final static Pattern encodePattern = Pattern.compile("([^\\x00-\\x7f]+)");
32     private final static Pattern decodePattern = Pattern.compile("=\\?([A-Za-z0-9_.-]+)\\?([bBqQ])\\?(.*?)\\?=");
33     private final static Pattern decodeQuotedPrintablePattern = Pattern.compile("=([0-9A-F]{2})");
34 
35     public static String encode(String raw) {
36         var quoteMatcher = encodePattern.matcher(raw);
37         return quoteMatcher.replaceAll(mo -> "=?utf-8?b?" + Base64.getEncoder().encodeToString(String.valueOf(mo.group(1)).getBytes(StandardCharsets.UTF_8)) + "?=");
38     }
39 
40     public static String decode(String encoded) {
41         var quotedMatcher = decodePattern.matcher(encoded);
42         return quotedMatcher.replaceAll(mo -> {
43             try {
44                 if (mo.group(2).toUpperCase().equals("B")) {
45                     return new String(Base64.getDecoder().decode(mo.group(3)), mo.group(1));
46                 } else {
47                     var quotedPrintableMatcher = decodeQuotedPrintablePattern.matcher(mo.group(3));
48                     return quotedPrintableMatcher.replaceAll(qmo -> {
49                         var byteValue = new byte[1];
50                         byteValue[0] = (byte)Integer.parseInt(qmo.group(1), 16);
51                         try {
52                             return new String(byteValue, mo.group(1));
53                         } catch (UnsupportedEncodingException e) {
54                             throw new RuntimeException(e);
55                         }
56                     });
57                 }
58             } catch (UnsupportedEncodingException e) {
59                 throw new RuntimeException(e);
60             }
61         });
62     }
63 }