author | lana |
Thu, 11 Sep 2014 14:28:19 -0700 | |
changeset 26520 | abaac15e3bd1 |
parent 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21805
diff
changeset
|
2 |
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package build.tools.addjsum; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.util.regex.*; |
|
30 |
||
31 |
/** Adds a checksum ("jsum") to the end of a text file. The algorithm |
|
32 |
used is known to the JVM and prevents trivial tampering with the |
|
33 |
class list used for class data sharing. |
|
34 |
*/ |
|
35 |
||
36 |
public class AddJsum { |
|
37 |
private static final long JSUM_SEED = 0xCAFEBABEBABECAFEL; |
|
38 |
||
39 |
public static void main(String[] args) throws Exception { |
|
40 |
if (args.length != 2) { |
|
41 |
System.err.println("Usage: java AddJsum [input file name] [output file name]"); |
|
42 |
System.exit(1); |
|
43 |
} |
|
44 |
||
45 |
try { |
|
46 |
File inFile = new File(args[0]); |
|
47 |
File outFile = new File(args[1]); |
|
48 |
BufferedReader reader = new BufferedReader(new FileReader(inFile)); |
|
49 |
BufferedWriter writer = new BufferedWriter(new FileWriter(outFile)); |
|
50 |
Pattern p = Pattern.compile("# [0-9A-Fa-f]*"); |
|
51 |
long computedJsum = JSUM_SEED; |
|
52 |
||
53 |
String line = null; |
|
54 |
while ((line = reader.readLine()) != null) { |
|
55 |
if (line.length() > 0 && line.charAt(0) == '#') { |
|
56 |
Matcher m = p.matcher(line); |
|
57 |
if (!m.matches()) { |
|
58 |
writer.write(line); |
|
59 |
writer.newLine(); |
|
60 |
} |
|
61 |
} else { |
|
62 |
computedJsum = jsum(computedJsum, line); |
|
63 |
writer.write(line); |
|
64 |
writer.newLine(); |
|
65 |
} |
|
66 |
} |
|
67 |
String hex = Long.toHexString(computedJsum); |
|
68 |
int diff = 16 - hex.length(); |
|
69 |
for (int i = 0; i < diff; i++) { |
|
70 |
hex = "0" + hex; |
|
71 |
} |
|
72 |
writer.write("# " + hex); |
|
73 |
writer.newLine(); |
|
74 |
reader.close(); |
|
75 |
writer.close(); |
|
76 |
} catch (IOException e) { |
|
77 |
System.err.println("Error reading or writing file"); |
|
78 |
throw(e); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
private static long jsum(long start, String str) { |
|
83 |
long h = start; |
|
84 |
int len = str.length(); |
|
85 |
for (int i = 0; i < len; i++) { |
|
86 |
char c = str.charAt(i); |
|
87 |
if (c <= ' ') { |
|
88 |
/* Skip spaces and control characters */ |
|
89 |
continue; |
|
90 |
} |
|
91 |
h = 31 * h + c; |
|
92 |
} |
|
93 |
return h; |
|
94 |
} |
|
95 |
} |