| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.tdbfmt; |
| 18 |
|
| 19 |
import java.io.BufferedInputStream; |
| 20 |
import java.io.BufferedOutputStream; |
| 21 |
import java.io.ByteArrayInputStream; |
| 22 |
import java.io.FileInputStream; |
| 23 |
import java.io.FileOutputStream; |
| 24 |
import java.io.IOException; |
| 25 |
import java.io.InputStream; |
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.List; |
| 28 |
import java.util.zip.CRC32; |
| 29 |
import java.util.zip.CheckedOutputStream; |
| 30 |
|
| 31 |
import uk.me.parabola.imgfmt.app.Area; |
| 32 |
import uk.me.parabola.io.EndOfFileException; |
| 33 |
import uk.me.parabola.io.StructuredInputStream; |
| 34 |
import uk.me.parabola.log.Logger; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class TdbFile { |
| 42 |
private static final Logger log = Logger.getLogger(TdbFile.class); |
| 43 |
|
| 44 |
public static final int TDB_V407 = 407; |
| 45 |
|
| 46 |
|
| 47 |
private int tdbVersion; |
| 48 |
|
| 49 |
|
| 50 |
private HeaderBlock headerBlock; |
| 51 |
private CopyrightBlock copyrightBlock = new CopyrightBlock(); |
| 52 |
private OverviewMapBlock overviewMapBlock; |
| 53 |
private final List<DetailMapBlock> detailBlocks = new ArrayList<>(); |
| 54 |
private final RBlock rblock = new RBlock(); |
| 55 |
private final TBlock tblock = new TBlock(); |
| 56 |
private String overviewDescription; |
| 57 |
private int codePage; |
| 58 |
|
| 59 |
public TdbFile() { |
| 60 |
} |
| 61 |
|
| 62 |
public TdbFile(int tdbVersion) { |
| 63 |
this.tdbVersion = tdbVersion; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static TdbFile read(String name) throws IOException { |
| 74 |
TdbFile tdb = new TdbFile(); |
| 75 |
|
| 76 |
try (InputStream is = new BufferedInputStream(new FileInputStream(name))) { |
| 77 |
tdb.load(is); |
| 78 |
} |
| 79 |
|
| 80 |
return tdb; |
| 81 |
} |
| 82 |
|
| 83 |
public void setProductInfo(int familyId, int productId, |
| 84 |
short productVersion, String seriesName, String familyName, String overviewDescription, |
| 85 |
byte enableProfile) |
| 86 |
{ |
| 87 |
headerBlock = new HeaderBlock(tdbVersion); |
| 88 |
headerBlock.setFamilyId((short) familyId); |
| 89 |
headerBlock.setProductId((short) productId); |
| 90 |
headerBlock.setProductVersion(productVersion); |
| 91 |
headerBlock.setSeriesName(seriesName); |
| 92 |
headerBlock.setFamilyName(familyName); |
| 93 |
headerBlock.setEnableProfile(enableProfile); |
| 94 |
this.overviewDescription = overviewDescription; |
| 95 |
} |
| 96 |
|
| 97 |
public void setCodePage(int codePage) { |
| 98 |
this.codePage = codePage; |
| 99 |
headerBlock.setCodePage(codePage); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public void addCopyright(String msg) { |
| 107 |
if (msg.isEmpty()) |
| 108 |
return; |
| 109 |
|
| 110 |
CopyrightSegment seg = new CopyrightSegment(CopyrightSegment.CODE_COPYRIGHT_TEXT_STRING, 3, msg); |
| 111 |
copyrightBlock.addSegment(seg); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public void setOverview(Area bounds, String number) { |
| 120 |
overviewMapBlock = new OverviewMapBlock(); |
| 121 |
overviewMapBlock.setArea(bounds); |
| 122 |
overviewMapBlock.setMapName(number); |
| 123 |
overviewMapBlock.setDescription(overviewDescription); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public void addDetail(DetailMapBlock detail) { |
| 132 |
detailBlocks.add(detail); |
| 133 |
} |
| 134 |
|
| 135 |
public void write(String name) throws IOException { |
| 136 |
|
| 137 |
if (headerBlock == null || overviewMapBlock == null) |
| 138 |
throw new IOException("Attempting to write file without being fully set up"); |
| 139 |
|
| 140 |
try (CheckedOutputStream stream = new CheckedOutputStream( |
| 141 |
new BufferedOutputStream(new FileOutputStream(name)), |
| 142 |
new CRC32())) |
| 143 |
{ |
| 144 |
headerBlock.writeTo(stream, codePage); |
| 145 |
|
| 146 |
copyrightBlock.writeTo(stream, codePage); |
| 147 |
|
| 148 |
if (tdbVersion >= TDB_V407) { |
| 149 |
rblock.writeTo(stream, codePage); |
| 150 |
} |
| 151 |
|
| 152 |
overviewMapBlock.writeTo(stream, codePage); |
| 153 |
|
| 154 |
for (DetailMapBlock detail : detailBlocks) { |
| 155 |
detail.writeTo(stream, codePage); |
| 156 |
} |
| 157 |
|
| 158 |
if (tdbVersion >= TDB_V407) { |
| 159 |
tblock.setSum(stream.getChecksum().getValue()); |
| 160 |
tblock.writeTo(stream, codePage); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
private void load(InputStream ds) throws IOException { |
| 172 |
|
| 173 |
boolean eof = false; |
| 174 |
while (!eof) { |
| 175 |
try { |
| 176 |
readBlock(ds); |
| 177 |
} catch (EndOfFileException ignore) { |
| 178 |
eof = true; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
private void readBlock(InputStream is) throws IOException { |
| 190 |
int blockType = is.read(); |
| 191 |
if (blockType == -1) |
| 192 |
throw new EndOfFileException(); |
| 193 |
|
| 194 |
int blockLength = readBlockLength(is); |
| 195 |
if (blockLength == -1) |
| 196 |
throw new EndOfFileException(); |
| 197 |
|
| 198 |
byte[] body = new byte[blockLength]; |
| 199 |
int n = is.read(body); |
| 200 |
if (n < 0) |
| 201 |
throw new IOException("failed to read block"); |
| 202 |
|
| 203 |
StructuredInputStream ds = new StructuredInputStream(new ByteArrayInputStream(body)); |
| 204 |
switch (blockType) { |
| 205 |
case HeaderBlock.BLOCK_ID: |
| 206 |
headerBlock = new HeaderBlock(ds); |
| 207 |
log.info("header block seen", headerBlock); |
| 208 |
break; |
| 209 |
case CopyrightBlock.BLOCK_ID: |
| 210 |
log.info("copyright block"); |
| 211 |
copyrightBlock = new CopyrightBlock(ds); |
| 212 |
break; |
| 213 |
case OverviewMapBlock.BLOCK_ID: |
| 214 |
overviewMapBlock = new OverviewMapBlock(ds); |
| 215 |
log.info("overview block", overviewMapBlock); |
| 216 |
break; |
| 217 |
case DetailMapBlock.BLOCK_ID: |
| 218 |
DetailMapBlock db = new DetailMapBlock(ds); |
| 219 |
log.info("detail block", db); |
| 220 |
detailBlocks.add(db); |
| 221 |
break; |
| 222 |
default: |
| 223 |
log.warn("Unknown block in tdb file"); |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
private int readBlockLength(InputStream is) throws IOException { |
| 229 |
int b1 = is.read(); |
| 230 |
if (b1 < 0) |
| 231 |
return -1; |
| 232 |
|
| 233 |
int b2 = is.read(); |
| 234 |
if (b2 < 0) |
| 235 |
return -1; |
| 236 |
|
| 237 |
return (b2 & 0xff) << 8 | b1 & 0xff; |
| 238 |
} |
| 239 |
|
| 240 |
public int getTdbVersion() { |
| 241 |
return headerBlock.getTdbVersion(); |
| 242 |
} |
| 243 |
} |