author | weijun |
Thu, 18 Jul 2019 08:53:06 +0800 | |
changeset 57488 | 94691d8e746f |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
28662 | 1 |
/* |
57488
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. |
28662 | 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 |
||
57488
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
24 |
import java.io.ByteArrayOutputStream; |
28662 | 25 |
import java.io.File; |
57488
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
26 |
import java.io.InputStream; |
28662 | 27 |
import java.io.IOException; |
57488
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
28 |
import java.util.jar.JarFile; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
29 |
import java.util.jar.Manifest; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
30 |
import java.util.zip.ZipFile; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
31 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
32 |
import static java.nio.charset.StandardCharsets.UTF_8; |
28662 | 33 |
|
34 |
/** |
|
35 |
* Helper class. |
|
36 |
*/ |
|
37 |
public class Utils { |
|
38 |
||
39 |
static void createFiles(String... filenames) throws IOException { |
|
40 |
for (String filename : filenames) { |
|
41 |
new File(filename).createNewFile(); |
|
42 |
} |
|
43 |
} |
|
44 |
||
57488
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
45 |
static void printNonPrintableCharactersEscaped(byte[] manifest) |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
46 |
throws IOException { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
47 |
// keep byte sequences encoding multi-byte UTF-8 encoded and composite |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
48 |
// characters together as much as possible before decoding into a String |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
49 |
ByteArrayOutputStream lineBuf = new ByteArrayOutputStream(); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
50 |
for (int i = 0; i < manifest.length; i++) { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
51 |
switch (manifest[i]) { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
52 |
case '\t': |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
53 |
lineBuf.write("\\t".getBytes(UTF_8)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
54 |
break; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
55 |
case '\r': |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
56 |
lineBuf.write("\\r".getBytes(UTF_8)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
57 |
if (i + 1 >= manifest.length || manifest[i + 1] != '\n') { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
58 |
System.out.println(lineBuf.toString(UTF_8)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
59 |
lineBuf.reset(); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
60 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
61 |
break; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
62 |
case '\n': |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
63 |
lineBuf.write("\\n".getBytes(UTF_8)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
64 |
System.out.println(lineBuf.toString(UTF_8)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
65 |
lineBuf.reset(); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
66 |
break; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
67 |
default: |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
68 |
lineBuf.write(manifest[i]); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
69 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
70 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
71 |
if (lineBuf.size() > 0) { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
72 |
System.out.println(lineBuf.toString(UTF_8)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
73 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
74 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
75 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
76 |
static void echoManifest(byte[] manifest, String msg) throws IOException { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
77 |
System.out.println("-".repeat(72)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
78 |
System.out.println(msg); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
79 |
System.out.println("-".repeat(72)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
80 |
printNonPrintableCharactersEscaped(manifest); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
81 |
System.out.println("-".repeat(72)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
82 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
83 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
84 |
static byte[] readJarManifestBytes(String jarFilename) throws IOException { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
85 |
return readJarEntryBytes(jarFilename, JarFile.MANIFEST_NAME); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
86 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
87 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
88 |
static byte[] readJarEntryBytes(String jarFilename, String jarEntryname) |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
89 |
throws IOException { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
90 |
try ( |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
91 |
ZipFile jar = new ZipFile(jarFilename); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
92 |
InputStream is = jar.getInputStream(jar.getEntry(jarEntryname)); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
93 |
) { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
94 |
return is.readAllBytes(); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
95 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
96 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
97 |
|
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
98 |
static String escapeStringWithNumbers(String lineBreak) { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
99 |
String escaped = ""; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
100 |
byte[] bytes = lineBreak.getBytes(UTF_8); |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
101 |
for (int i = 0; i < bytes.length; i++) { |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
102 |
escaped += bytes[i]; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
103 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
104 |
return escaped; |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
105 |
} |
94691d8e746f
8217375: jarsigner breaks old signature with long lines in manifest
weijun
parents:
47216
diff
changeset
|
106 |
|
28662 | 107 |
} |