Debian Codegraph @

src/uk/me/parabola/imgfmt/app/srt/SrtFileReader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2017.
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 package uk.me.parabola.imgfmt.app.srt;
14
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.nio.file.Files;
18 import java.nio.file.Paths;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import uk.me.parabola.imgfmt.app.BufferedImgFileReader;
25 import uk.me.parabola.imgfmt.app.ImgFile;
26 import uk.me.parabola.imgfmt.app.ImgFileReader;
27 import uk.me.parabola.imgfmt.app.Section;
28 import uk.me.parabola.imgfmt.app.labelenc.CharacterDecoder;
29 import uk.me.parabola.imgfmt.app.labelenc.CodeFunctions;
30 import uk.me.parabola.imgfmt.fs.DirectoryEntry;
31 import uk.me.parabola.imgfmt.fs.FileSystem;
32 import uk.me.parabola.imgfmt.fs.ImgChannel;
33 import uk.me.parabola.imgfmt.sys.FileImgChannel;
34 import uk.me.parabola.imgfmt.sys.ImgFS;
35
36 /**
37 * The sort file.
38 * Work in progress.
39 *
40
41 * @author Gerd Petermann
42 *
43 */
44 public class SrtFileReader extends ImgFile {
45 private Sort sort;
46 private final CharacterDecoder decoder;
47 private SRTHeader header = new SRTHeader();
48 private Section description = new Section();
49 private Section tableHeader = new Section();
50 private Section characterTable = new Section();
51 private Section srt5 = new Section();
52 private Section srt6 = new Section();
53 private Section srt7 = new Section();
54 private Section srt8 = new Section();
55 private int countExp;
56 private final Map<Integer, Integer> offsetToBlock = new HashMap<>();
57
58 public SrtFileReader (ImgChannel chan) {
59 CodeFunctions funcs = CodeFunctions.createEncoderForLBL("latin1");
60 decoder = funcs.getDecoder();
61 sort = new Sort();
62 setHeader(header);
63 setReader(new BufferedImgFileReader(chan));
64 header.readHeader(getReader());
65 sort.setHeaderLen(header.getHeaderLength());
66 readSrt1();
67 readDesc();
68 readTableHeader();
69 }
70
71 private void readTableHeader() {
72 ImgFileReader reader = getReader();
73 reader.position(tableHeader.getPosition());
74 int len = reader.get2u();
75 sort.setHeader3Len(len);
76 sort.setId1(reader.get2u());
77 sort.setId2(reader.get2u());
78 sort.setCodepage(reader.get2u());
79 if (sort.getCodepage() == 65001)
80 sort.setMulti(true);
81 reader.get4(); //?
82 characterTable.readSectionInfo(reader, true);
83 reader.position(reader.position() + 6); // padding?
84 srt5.readSectionInfo(reader, true);
85 reader.position(reader.position() + 6); // padding?
86 if (len > 0x2c) {
87 srt6.readSectionInfo(reader, false);
88 }
89 if (len > 0x34) {
90 reader.get4();
91 int maxCodeBlock = reader.get4();
92 sort.setMaxPage(maxCodeBlock);
93 srt7.readSectionInfo(reader, true);
94 reader.position(reader.position() + 6); // padding?
95 }
96 if (len > 0x44) {
97 srt8.readSectionInfo(reader, true);
98 }
99 readCharacterTable();
100 if (srt7.getSize() > 0) {
101 readSrt7();
102 readSrt8();
103 }
104 readExpansions();
105 }
106
107 private void readSrt7() {
108 ImgFileReader reader = getReader();
109 reader.position(tableHeader.getPosition() + srt7.getPosition());
110
111 int block = 1;
112 for (int i = 0; i < srt7.getNumItems(); i++) {
113 int val = reader.get4();
114 if (val >= 0)
115 offsetToBlock.put(val/srt8.getItemSize(), block);
116 block++;
117 }
118
119 }
120
121 private void readSrt8() {
122 ImgFileReader reader = getReader();
123 reader.position(tableHeader.getPosition() + srt8.getPosition());
124 int reclen = srt8.getItemSize();
125 int block = 1;
126 for (int i = 0; i < srt8.getNumItems(); i++) {
127 Integer nblock = offsetToBlock.get(i);
128 if (nblock != null)
129 block = nblock;
130 int flags = reader.get1u();
131 CodePosition cp = readCharPosition(reclen-1);
132 int ch = block*256 + (i % 256);
133
134 if ((flags & 0xf0) != 0) {
135 sort.add(ch, countExp + 1, 0, 0, flags);
136 countExp += ((flags >> 4) & 0xf) + 1;
137 }
138 else {
139 sort.add(ch, cp.getPrimary(), cp.getSecondary(), cp.getTertiary(), flags);
140 }
141 }
142
143 }
144
145 private void readCharacterTable() {
146 ImgFileReader reader = getReader();
147 reader.position(characterTable.getPosition());
148 int rs = characterTable.getItemSize();
149 long start = tableHeader.getPosition() + characterTable.getPosition();
150 reader.position(start);
151 for (int ch = 1; ch <= characterTable.getNumItems(); ch++) {
152 int flags = reader.get1u();
153 CodePosition cp = readCharPosition(rs-1);
154 if ((flags & 0xf0) != 0) {
155 sort.add(ch, countExp + 1, 0, 0, flags);
156 countExp += ((flags >> 4) & 0xf) + 1;
157 }
158 else {
159 sort.add(ch, cp.getPrimary(), cp.getSecondary(), cp.getTertiary(), flags);
160 }
161 }
162 }
163
164 /**
165 * Read the sort position information. The format varies depending on the posLength parameter.
166 *
167 * @param posLength The length of the position information (not the record length, just the
168 * part of it that encodes the positions).
169 */
170 private CodePosition readCharPosition(int posLength) {
171 ImgFileReader reader = getReader();
172 CodePosition cp = new CodePosition();
173 int rec;
174 if (posLength == 2) {
175 rec = reader.get2u();
176 cp.setPrimary(rec & 0xff);
177 cp.setSecondary((rec >> 8) & 0xf);
178 cp.setTertiary((rec >> 12) & 0xf);
179
180 } else if (posLength == 3) {
181 // what is the extra byte for ???
182 rec = reader.get3u();
183 cp.setPrimary(rec & 0xff);
184 cp.setSecondary((rec >> 8) & 0xf);
185 cp.setTertiary((rec >> 12) & 0xf);
186 } else if (posLength == 4) {
187 rec = reader.get4();
188 cp.setPrimary(rec & 0xffff);
189 cp.setSecondary((rec >> 16) & 0xff);
190 cp.setTertiary((rec >> 24) & 0xff);
191 } else {
192 throw new RuntimeException("unexpected value posLength " + posLength);
193 }
194 return cp;
195 }
196
197 private void readExpansions() {
198 ImgFileReader reader = getReader();
199 int reclen = srt5.getItemSize();
200 reader.position(tableHeader.getPosition() + srt5.getPosition());
201 List<CodePosition> expansionList = new ArrayList<>(srt5.getNumItems());
202 if (countExp != srt5.getNumItems()) {
203 throw new RuntimeException("unexpected number of expansions " + srt5.getNumItems() + " expected: " + countExp);
204 }
205 for (int i = 0; i < srt5.getNumItems(); i++) {
206 CodePosition cp = readCharPosition(reclen);
207 expansionList.add(cp);
208 }
209 sort.setExpansions(expansionList);
210 }
211
212 private void readSrt1() {
213 ImgFileReader reader = getReader();
214 reader.position(header.getHeaderLength());
215 description.readSectionInfo(reader, false);
216 tableHeader.readSectionInfo(reader, false);
217 }
218
219 private void readDesc() {
220 getReader().position(description.getPosition());
221 byte[] zString= getReader().getZString();
222 sort.setDescription(decodeToString(zString));
223 }
224
225 private String decodeToString(byte[] zString) {
226 decoder.reset();
227 for (byte b : zString)
228 decoder.addByte(b);
229
230 return decoder.getText().getText();
231 }
232
233 public Sort getSort() {
234 return sort;
235 }
236
237 /**
238 * Read in a sort description text file and create a SRT from it.
239 * @param args First arg is the text input file, the second is the name of the output file. The defaults are
240 * in.txt and out.srt.
241 */
242 public static void main(String[] args) throws IOException {
243 String infile = "we2.srt";
244 if (args.length > 0)
245 infile = args[0];
246
247 String outfile = "out.srt";
248 if (args.length > 1)
249 outfile = args[1];
250 try {
251 Files.delete(Paths.get(outfile, ""));
252 } catch (Exception e) {
253 }
254 ImgChannel inChannel = null;
255 FileSystem fs = null;
256 try {
257 if (infile.endsWith("srt"))
258 inChannel = new FileImgChannel(infile, "r");
259 else {
260 fs = ImgFS.openFs(infile);
261 List<DirectoryEntry> entries = fs.list();
262
263 // Find the TRE entry
264 String mapname = null;
265 for (DirectoryEntry ent : entries) {
266 if ("SRT".equals(ent.getExt())) {
267 mapname = ent.getName();
268 break;
269 }
270 }
271 inChannel = fs.open(mapname + ".SRT", "r");
272
273 ImgChannel chan = new FileImgChannel(outfile, "rw");
274 SrtFileReader tr = new SrtFileReader(inChannel);
275 tr.close();
276 Sort sort1 = tr.getSort();
277 SRTFile sf = new SRTFile(chan);
278 sf.setSort(sort1);
279 sf.write();
280 sf.close();
281 chan.close();
282 }
283 } catch (FileNotFoundException e) {
284 System.err.println("Could not open file: " + infile);
285 } finally {
286 if (fs != null) {
287 fs.close();
288 }
289 }
290
291 }
292 }