From d868692e33220a20685b8c68a2e01ff8e7f76580 Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Sat, 14 Sep 2013 11:33:18 -0700 Subject: [PATCH] Truncate the filename for overly long class names --- .../org/jf/util/ClassFileNameHandler.java | 21 +++++++- .../org/jf/util/ClassFileNameHandlerTest.java | 49 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 util/src/test/java/org/jf/util/ClassFileNameHandlerTest.java 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()); + } +}