jdk/make/src/classes/build/tools/addjsum/AddJsum.java
changeset 37947 f69560487686
parent 37946 e420b9f05aaf
parent 37936 428ebc487445
child 37948 caf97b37ebec
equal deleted inserted replaced
37946:e420b9f05aaf 37947:f69560487686
     1 /*
       
     2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
       
     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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    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  *
       
    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.
       
    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 }