-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathHexEditor.java
More file actions
94 lines (70 loc) · 2.76 KB
/
HexEditor.java
File metadata and controls
94 lines (70 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cz.jaybee.intelhex.cli;
import cz.jaybee.intelhex.IntelHexException;
import cz.jaybee.intelhex.Parser;
import cz.jaybee.intelhex.listeners.HexWriter;
import cz.jaybee.intelhex.listeners.RangeDetector;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
public class HexEditor {
public static void main(String[] args) throws IOException, IntelHexException {
String fileIn = "input.hex";
String fileOut = "output.hex";
int address = 0x00;
boolean replace = false;
byte[] data = new byte[0];
if (args.length == 0) {
System.out.println("usage:");
System.out.println(" hexEditor <hex_in> <hex_out> -replace <start address> <data>");
System.out.println();
return;
}
if (args.length >= 1) {
fileIn = args[0];
}
if (args.length >= 2) {
fileOut = args[1];
}
if (args.length >= 3 && "-replace".equals(args[2])) {
replace = true;
if (args.length >= 4) {
address = Integer.parseInt(args[3].substring(2),16);
}
if (args.length >= 5) {
data = fromHexString(args[4].substring(2));
}
}
try (FileInputStream is = new FileInputStream(fileIn)) {
OutputStream os = Files.newOutputStream(Paths.get(fileOut));
Parser parser = new Parser(is);
// 1st iteration - calculate maximum output range
RangeDetector rangeDetector = new RangeDetector();
parser.setDataListener(rangeDetector);
parser.parse();
is.getChannel().position(0);
// 2nd iteration - actual write of the output
HexWriter hexWriter = new HexWriter(rangeDetector.getFullRangeRegion(), os);
parser.setDataListener(hexWriter);
parser.parse();
if(replace) {
hexWriter.replaceData(address, data);
}
//save into file
hexWriter.save();
} catch (IOException ex) {
Logger.getLogger(Hex2bin.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static byte[] fromHexString(final String encoded) throws IntelHexException {
if ((encoded.length() % 2) != 0)
throw new IntelHexException("Input string must contain an even number of characters");
final byte[] result = new byte[encoded.length()/2];
final char[] enc = encoded.toCharArray();
for (int i = 0; i < enc.length; i += 2) {
result[i/2] = (byte) Integer.parseInt(String.valueOf(enc[i]) + enc[i + 1], 16);
}
return result;
}
}