1 /*
  2  * Copyright (c) 2016, 2023, 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.
  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 
 24 import java.io.BufferedReader;
 25 import java.io.ByteArrayInputStream;
 26 import java.io.File;
 27 import java.io.FileInputStream;
 28 import java.io.InputStream;
 29 import java.io.InputStreamReader;
 30 import java.nio.charset.StandardCharsets;
 31 import java.util.ArrayList;
 32 import java.util.List;
 33 
 34 /**
 35  * HexDumpReader provides utility methods to read a hex dump text file
 36  * and convert to an InputStream.  The format supported by the methods
 37  * can be generated by the following command.
 38  *
 39  * $ od -vw -t x1 foo | sed -r -e 's/^[0-9]+ ?//' -e 's/ //g' -e '/^$/d'
 40  */
 41 public final class HexDumpReader {
 42 
 43     // Utility class should not be instantiated
 44     private HexDumpReader() {}
 45 
 46     public static InputStream getStreamFromHexDump(String fileName) {
 47         return getStreamFromHexDump(new File(System.getProperty("test.src", "."),
 48                                              fileName));
 49     }
 50 
 51     public static InputStream getStreamFromHexDump(File hexFile) {
 52         ByteArrayBuilder bab = new ByteArrayBuilder();
 53         int lineNo = 0;
 54         try (BufferedReader reader
 55                  = new BufferedReader(new InputStreamReader(new FileInputStream(hexFile),
 56                 StandardCharsets.US_ASCII))) {
 57             String line;
 58             while ((line = reader.readLine()) != null) {
 59                 lineNo++;
 60                 line = line.trim();
 61                 // Skip blank and comment lines.
 62                 if (line.length() == 0) {
 63                     continue;
 64                 }
 65                 int x = line.indexOf('#');
 66                 if (x == 0) {
 67                     continue;
 68                 }
 69                 if (x > 0) {
 70                     line = line.substring(0, x).trim();
 71                 }
 72                 int len = line.length();
 73                 for (int i = 0; i < len; i += 2) {
 74                     bab.put((byte)Integer.parseInt(line, i, i + 2, 16));
 75                 }
 76             }
 77         } catch (Exception e) {
 78             throw new RuntimeException(hexFile.getName() + ":error:" + lineNo + ": " + e, e);
 79         }
 80         return new ByteArrayInputStream(bab.toArray());
 81     }
 82 
 83 
 84     private static class ByteArrayBuilder {
 85         private static final int BUFFER_SIZE = 4096;
 86 
 87         private int size;
 88         private List<byte[]> bytes;
 89         private byte[] current;
 90         private int offset;
 91 
 92         ByteArrayBuilder() {
 93             bytes = new ArrayList<>();
 94             current = new byte[BUFFER_SIZE];
 95         }
 96 
 97         void put(byte b) {
 98             if (offset == BUFFER_SIZE) {
 99                 bytes.add(current);
100                 current = new byte[BUFFER_SIZE];
101                 offset = 0;
102             }
103             current[offset++] = b;
104             size++;
105         }
106 
107         byte[] toArray() {
108             byte[] buf = new byte[size];
109             int ptr = 0;
110             for (byte[] ba : bytes) {
111                 System.arraycopy(ba, 0, buf, ptr, ba.length);
112                 ptr += ba.length;
113             }
114             System.arraycopy(current, 0, buf, ptr, offset);
115             assert ptr + offset == size;
116             return buf;
117         }
118     }
119 
120 }