aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorBen Gruver <bgruv@google.com>2013-09-14 11:33:18 -0700
committerBen Gruver <bgruv@google.com>2013-09-14 11:33:18 -0700
commitd868692e33220a20685b8c68a2e01ff8e7f76580 (patch)
tree3a9fa044033cfb31feb281be572e36a86fd24f9a /util
parent5ff4ee9a3fc898dbe9a67386e984f14c21338391 (diff)
downloadsmali-d868692e33220a20685b8c68a2e01ff8e7f76580.tar.gz
Truncate the filename for overly long class names
Diffstat (limited to 'util')
-rw-r--r--util/src/main/java/org/jf/util/ClassFileNameHandler.java21
-rw-r--r--util/src/test/java/org/jf/util/ClassFileNameHandlerTest.java49
2 files changed, 69 insertions, 1 deletions
diff --git a/util/src/main/java/org/jf/util/ClassFileNameHandler.java b/util/src/main/java/org/jf/util/ClassFileNameHandler.java
index 79701a2e..73ac6b9d 100644
--- a/util/src/main/java/org/jf/util/ClassFileNameHandler.java
+++ b/util/src/main/java/org/jf/util/ClassFileNameHandler.java
@@ -31,6 +31,7 @@ package org.jf.util;
import ds.tree.RadixTree;
import ds.tree.RadixTreeImpl;
+import javax.annotation.Nonnull;
import java.io.*;
import java.nio.CharBuffer;
import java.util.regex.Pattern;
@@ -41,6 +42,9 @@ import java.util.regex.Pattern;
* class name to distinguish it from another class with a name that differes only by case. i.e. a.smali and a_2.smali
*/
public class ClassFileNameHandler {
+ // we leave an extra 10 characters to allow for a numeric suffix to be added, if it's needed
+ private static final int MAX_FILENAME_LENGTH = 245;
+
private PackageNameEntry top;
private String fileExtension;
private boolean modifyWindowsReservedFilenames;
@@ -83,6 +87,10 @@ public class ClassFileNameHandler {
packageElement += "#";
}
+ if (packageElement.length() > MAX_FILENAME_LENGTH) {
+ packageElement = shortenPathComponent(packageElement, MAX_FILENAME_LENGTH);
+ }
+
packageElements[elementIndex++] = packageElement;
elementStart = ++i;
}
@@ -101,11 +109,23 @@ public class ClassFileNameHandler {
packageElement += "#";
}
+ if ((packageElement.length() + fileExtension.length()) > MAX_FILENAME_LENGTH) {
+ packageElement = shortenPathComponent(packageElement, MAX_FILENAME_LENGTH - fileExtension.length());
+ }
+
packageElements[elementIndex] = packageElement;
return top.addUniqueChild(packageElements, 0);
}
+ @Nonnull
+ static String shortenPathComponent(@Nonnull String pathComponent, int maxLength) {
+ int toRemove = pathComponent.length() - maxLength + 1;
+
+ int firstIndex = (pathComponent.length()/2) - (toRemove/2);
+ return pathComponent.substring(0, firstIndex) + "#" + pathComponent.substring(firstIndex+toRemove);
+ }
+
private static boolean testForWindowsReservedFileNames(File path) {
File f = new File(path, "aux.smali");
if (f.exists()) {
@@ -242,7 +262,6 @@ public class ClassFileNameHandler {
}
}
-
if (pathElementsIndex == pathElements.length - 1) {
String fileName;
if (!isCaseSensitive()) {
diff --git a/util/src/test/java/org/jf/util/ClassFileNameHandlerTest.java b/util/src/test/java/org/jf/util/ClassFileNameHandlerTest.java
new file mode 100644
index 00000000..7aa47adf
--- /dev/null
+++ b/util/src/test/java/org/jf/util/ClassFileNameHandlerTest.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2013, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.jf.util;
+
+import junit.framework.Assert;
+import org.junit.Test;
+
+public class ClassFileNameHandlerTest {
+ @Test
+ public void testShortedPathComponent() {
+ StringBuilder sb = new StringBuilder();
+ for (int i=0; i<300; i++) {
+ sb.append((char)i);
+ }
+
+ String result = ClassFileNameHandler.shortenPathComponent(sb.toString(), 255);
+
+ Assert.assertEquals(255, result.length());
+ }
+}