From 4b87e194b64285b53ff892471a5ff3ca28e1b058 Mon Sep 17 00:00:00 2001 From: hupan Date: Wed, 20 Nov 2019 11:27:55 +0800 Subject: [PATCH] =?UTF-8?q?add=20feature:=20UTF-8=20characters=20support?= =?UTF-8?q?=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- src/me/konloch/kontainer/io/DiskReader.java | 18 ++-- .../decompilers/ProcyonDecompiler.java | 4 +- .../club/bytecodeviewer/util/EncodeUtils.java | 98 +++++++++++++++++++ 4 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 src/the/bytecode/club/bytecodeviewer/util/EncodeUtils.java diff --git a/.gitignore b/.gitignore index 0d008861..6fa47a36 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ .classpath .project .DS_Store -*.iml \ No newline at end of file +*.iml +/out/ diff --git a/src/me/konloch/kontainer/io/DiskReader.java b/src/me/konloch/kontainer/io/DiskReader.java index 7dcad2d4..5581f5e1 100644 --- a/src/me/konloch/kontainer/io/DiskReader.java +++ b/src/me/konloch/kontainer/io/DiskReader.java @@ -1,5 +1,7 @@ package me.konloch.kontainer.io; +import the.bytecode.club.bytecodeviewer.util.EncodeUtils; + import java.io.BufferedReader; import java.io.FileReader; import java.io.File; @@ -54,20 +56,18 @@ public class DiskReader { /** * Used to load from file */ - public synchronized static String loadAsString(String fileName) - throws Exception { - String s = ""; + public synchronized static String loadAsString(String fileName) throws Exception { + StringBuilder s = new StringBuilder(); - BufferedReader reader = new BufferedReader(new FileReader(new File( - fileName))); - String add; + BufferedReader reader = new BufferedReader(new FileReader(new File(fileName))); - while ((add = reader.readLine()) != null) - s += add + System.getProperty("line.separator"); + for (String add = reader.readLine(); add != null; add = reader.readLine()) { + s.append(EncodeUtils.unicodeToString(add)).append(System.getProperty("line.separator")); + } reader.close(); - return s; + return s.toString(); } /** diff --git a/src/the/bytecode/club/bytecodeviewer/decompilers/ProcyonDecompiler.java b/src/the/bytecode/club/bytecodeviewer/decompilers/ProcyonDecompiler.java index 57be6136..7be7742b 100644 --- a/src/the/bytecode/club/bytecodeviewer/decompilers/ProcyonDecompiler.java +++ b/src/the/bytecode/club/bytecodeviewer/decompilers/ProcyonDecompiler.java @@ -35,6 +35,7 @@ import com.strobel.assembler.metadata.TypeDefinition; import com.strobel.assembler.metadata.TypeReference; import the.bytecode.club.bytecodeviewer.BytecodeViewer; +import the.bytecode.club.bytecodeviewer.util.EncodeUtils; import the.bytecode.club.bytecodeviewer.util.MiscUtils; /*************************************************************************** @@ -134,9 +135,8 @@ public class ProcyonDecompiler extends Decompiler { } StringWriter stringwriter = new StringWriter(); settings.getLanguage().decompileType(resolvedType, new PlainTextOutput(stringwriter), decompilationOptions); - String decompiledSource = stringwriter.toString(); - return decompiledSource; + return EncodeUtils.unicodeToString(stringwriter.toString()); } catch (StackOverflowError | Exception e) { StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); diff --git a/src/the/bytecode/club/bytecodeviewer/util/EncodeUtils.java b/src/the/bytecode/club/bytecodeviewer/util/EncodeUtils.java new file mode 100644 index 00000000..9fffedad --- /dev/null +++ b/src/the/bytecode/club/bytecodeviewer/util/EncodeUtils.java @@ -0,0 +1,98 @@ +package the.bytecode.club.bytecodeviewer.util; + +import org.apache.commons.lang3.StringUtils; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Encoding Convert Utils + * + * @author hupan + * @date 2019-11-19 14:29 + */ +public class EncodeUtils { + + public static String stringToUnicode(String s) { + try { + StringBuilder out = new StringBuilder(""); + byte[] bytes = s.getBytes("unicode"); + + for (int i = 0; i < bytes.length - 1; i += 2) { + out.append("\\u"); + String str = Integer.toHexString(bytes[i + 1] & 0xff); + for (int j = str.length(); j < 2; j++) { + out.append("0"); + } + String str1 = Integer.toHexString(bytes[i] & 0xff); + out.append(str1); + out.append(str); + } + return out.toString(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return null; + } + } + + public static String unicodeToString(String str) { + + Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))"); + Matcher matcher = pattern.matcher(str); + char ch; + while (matcher.find()) { + String group = matcher.group(2); + ch = (char) Integer.parseInt(group, 16); + String group1 = matcher.group(1); + str = str.replace(group1, ch + ""); + } + return str; + } + + public static String convertStringToUTF8(String s) { + if (s == null || StringUtils.EMPTY.equals(s)) { + return null; + } + StringBuilder sb = new StringBuilder(); + try { + char c; + for (int i = 0; i < s.length(); i++) { + c = s.charAt(i); + if (c >= 0 && c <= 255) { + sb.append(c); + } else { + byte[] b; + b = Character.toString(c).getBytes(StandardCharsets.UTF_8); + for (int value : b) { + int k = value; + k = k < 0 ? k + 256 : k; + sb.append(Integer.toHexString(k).toUpperCase()); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return sb.toString(); + } + + public static String convertUTF8ToString(String s) { + if (s == null || StringUtils.EMPTY.equals(s)) { + return null; + } + s = s.toUpperCase(); + int total = s.length() / 2; + int pos = 0; + byte[] buffer = new byte[total]; + for (int i = 0; i < total; i++) { + int start = i * 2; + buffer[i] = (byte) Integer.parseInt(s.substring(start, start + 2), 16); + pos++; + } + + return new String(buffer, 0, pos, StandardCharsets.UTF_8); + } + +}