From 042975b980e6c085b8da1d3ac5b2823e357b914a Mon Sep 17 00:00:00 2001 From: Eugene Livis Date: Mon, 9 Sep 2019 13:27:17 -0400 Subject: [PATCH 1/2] Added a utility method to utf-8 sanitize file names --- .../org/sleuthkit/autopsy/coreutils/FileUtil.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java index 5b432124ba..4a7e6da65f 100644 --- a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java +++ b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java @@ -20,6 +20,7 @@ package org.sleuthkit.autopsy.coreutils; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.util.logging.Level; import org.openide.filesystems.FileObject; import java.nio.file.Files; @@ -171,6 +172,18 @@ public class FileUtil { //with underscores. We are only keeping \ as it could be part of the path. return fileName.replaceAll("[\\p{Cntrl}/:\"*?<>|]+", "_"); } + + /** + * UTF-8 sanitize and escape special characters in a file name or a file name component + * + * @param fileName to escape + * + * @return Sanitized string + */ + public static String utf8SanitizeFileName(String fileName) { + Charset charset = Charset.forName("UTF-8"); + return charset.decode(charset.encode(escapeFileName(fileName))).toString(); + } /** * Test if the current user has read and write access to the dirPath. From 4c05fd5a219aa84fc738599cef76405433e44220 Mon Sep 17 00:00:00 2001 From: Eugene Livis Date: Tue, 10 Sep 2019 17:00:41 -0400 Subject: [PATCH 2/2] Added utility method to UTF-8 sanitize a string --- Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java index 4a7e6da65f..6422fbcad2 100644 --- a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java +++ b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java @@ -21,6 +21,7 @@ package org.sleuthkit.autopsy.coreutils; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.logging.Level; import org.openide.filesystems.FileObject; import java.nio.file.Files; @@ -181,7 +182,7 @@ public class FileUtil { * @return Sanitized string */ public static String utf8SanitizeFileName(String fileName) { - Charset charset = Charset.forName("UTF-8"); + Charset charset = StandardCharsets.UTF_8; return charset.decode(charset.encode(escapeFileName(fileName))).toString(); }