Debian Codegraph @

src/uk/me/parabola/imgfmt/mdxfmt/MdxFile.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2009.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13
14 package uk.me.parabola.imgfmt.mdxfmt;
15
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.UnsupportedEncodingException;
19 import java.nio.ByteBuffer;
20 import java.nio.ByteOrder;
21 import java.nio.channels.FileChannel;
22 import java.nio.channels.WritableByteChannel;
23 import java.nio.charset.StandardCharsets;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import uk.me.parabola.log.Logger;
28
29 /**
30 * The MDX index file. Used with the global index. This is located
31 * at the family level in the windows registry and can perhaps index
32 * across different products (and maybe families), although such a thing
33 * hasn't been seen.
34 *
35 * @author Steve Ratcliffe
36 */
37 public class MdxFile {
38 private final char familyId;
39 private final char productId;
40
41 private final List<MapInfo> maps = new ArrayList<>();
42
43 /**
44 * Create with default family and product ids.
45 * @param familyId The default family id that will be used if no other one
46 * is supplied.
47 * @param productId The default product id for the maps indexed by this
48 * file.
49 */
50 public MdxFile(int familyId, int productId) {
51 this.familyId = (char) familyId;
52 this.productId = (char) productId;
53 }
54
55 /**
56 * Add a map with the default family and product id's and with equal
57 * name and hex name.
58 * @param name The map name (from the filename of the map) as an integer.
59 * @param hexname The map id that is inside the TRE header
60 * @param filename The file name of the map being added. Mainly for diagnostics,
61 * it is not needed for the file.
62 */
63 public void addMap(int name, int hexname, String filename) {
64 MapInfo info = new MapInfo();
65 info.setHexMapname(hexname);
66 info.setMapname(name);
67 info.setFamilyId(familyId);
68 info.setProductId(productId);
69 info.setFilename(filename);
70
71 maps.add(info);
72 }
73
74 /**
75 * Write the file out to the given filename.
76 */
77 public void write(String filename) throws IOException {
78 try (FileOutputStream stream = new FileOutputStream(filename); FileChannel chan = stream.getChannel();) {
79 ByteBuffer buf = ByteBuffer.allocate(1024);
80 buf.order(ByteOrder.LITTLE_ENDIAN);
81
82 writeHeader(chan, buf);
83 writeBody(chan, buf);
84 }
85 }
86
87 private void writeHeader(WritableByteChannel chan, ByteBuffer buf) throws IOException {
88 buf.put("Midx".getBytes(StandardCharsets.US_ASCII));
89 buf.putChar((char) 100);
90 buf.putInt(12);
91 buf.putInt(maps.size());
92
93 buf.flip();
94 chan.write(buf);
95 }
96
97 private void writeBody(WritableByteChannel chan, ByteBuffer buf) throws IOException {
98 // Sort the maps by the hex number.
99 maps.sort((o1, o2) -> Integer.compare(o1.getHexMapname(), o2.getHexMapname()));
100
101 for (MapInfo info : maps) {
102 // Although its not necessarily wrong for them to be zero, it probably
103 // sign that something is wrong.
104 if (info.getHexMapname() == 0 || info.getMapname() == 0)
105 Logger.defaultLogger.error("Invalid mapname for " + info.getFilename() + ", perhaps it is not a .img file");
106
107 buf.compact();
108 info.write(buf);
109
110 buf.flip();
111 chan.write(buf);
112 }
113 }
114 }