summaryrefslogtreecommitdiff
path: root/org.fox.ttcomics/src/main/java/org/fox/ttcomics2/junrar/rarfile/FileHeader.java
blob: ad3743b6c91e58e46b97312fe16a250d09bc98cb (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Copyright (c) 2007 innoSysTec (R) GmbH, Germany. All rights reserved.
 * Original author: Edmund Wagner
 * Creation date: 22.05.2007
 *
 * Source: $HeadURL$
 * Last changed: $LastChangedDate$
 * 
 * 
 * the unrar licence applies to all junrar source and binary distributions 
 * you are not allowed to use this source to re-create the RAR compression algorithm
 *
 * Here some html entities which can be used for escaping javadoc tags:
 * "&":  "&" or "&"
 * "<":  "&#060;" or "&lt;"
 * ">":  "&#062;" or "&gt;"
 * "@":  "&#064;" 
 */
package org.fox.ttcomics2.junrar.rarfile;

import org.fox.ttcomics2.junrar.io.Raw;

import java.util.Calendar;
import java.util.Date;


public class FileHeader extends BlockHeader {


    private static final byte SALT_SIZE = 8;

    private static final byte NEWLHD_SIZE = 32;
    private final HostSystem hostOS;
    private final int fileCRC;
    private final int fileTime;
    private final byte[] fileNameBytes;
    private final byte[] salt = new byte[SALT_SIZE];
    private long unpSize;
    private byte unpVersion;
    private byte unpMethod;
    private short nameSize;
    private int highPackSize;
    private int highUnpackSize;
    private String fileName;
    private String fileNameW;
    private byte[] subData;
    private Date mTime;

    private Date cTime;

    private Date aTime;

    private Date arcTime;

    private long fullPackSize;

    private long fullUnpackSize;

    private int fileAttr;

    private int subFlags; // same as fileAttr (in header)

    private int recoverySectors = -1;

    public FileHeader(BlockHeader bh, byte[] fileHeader) {
        super(bh);

        int position = 0;
        unpSize = Raw.readIntLittleEndianAsLong(fileHeader, position);
        position += 4;
        hostOS = HostSystem.findHostSystem(fileHeader[4]);
        position++;

        fileCRC = Raw.readIntLittleEndian(fileHeader, position);
        position += 4;

        fileTime = Raw.readIntLittleEndian(fileHeader, position);
        position += 4;

        unpVersion |= fileHeader[13] & 0xff;
        position++;
        unpMethod |= fileHeader[14] & 0xff;
        position++;
        nameSize = Raw.readShortLittleEndian(fileHeader, position);
        position += 2;

        fileAttr = Raw.readIntLittleEndian(fileHeader, position);
        position += 4;
        if (isLargeBlock()) {
            highPackSize = Raw.readIntLittleEndian(fileHeader, position);
            position += 4;

            highUnpackSize = Raw.readIntLittleEndian(fileHeader, position);
            position += 4;
        } else {
            highPackSize = 0;
            highUnpackSize = 0;
            if (unpSize == 0xffffffff) {

                unpSize = 0xffffffff;
                highUnpackSize = Integer.MAX_VALUE;
            }

        }
        fullPackSize |= highPackSize;
        fullPackSize <<= 32;
        fullPackSize |= getPackSize();

        fullUnpackSize |= highUnpackSize;
        fullUnpackSize <<= 32;
        fullUnpackSize += unpSize;

        nameSize = nameSize > 4 * 1024 ? 4 * 1024 : nameSize;

        fileNameBytes = new byte[nameSize];
        for (int i = 0; i < nameSize; i++) {
            fileNameBytes[i] = fileHeader[position];
            position++;
        }

        if (isFileHeader()) {
            if (isUnicode()) {
                int length = 0;
                fileName = "";
                fileNameW = "";
                while (length < fileNameBytes.length
                        && fileNameBytes[length] != 0) {
                    length++;
                }
                byte[] name = new byte[length];
                System.arraycopy(fileNameBytes, 0, name, 0, name.length);
                fileName = new String(name);
                if (length != nameSize) {
                    length++;
                    fileNameW = FileNameDecoder.decode(fileNameBytes, length);
                }
            } else {
                fileName = new String(fileNameBytes);
                fileNameW = "";
            }
        }

        if (UnrarHeadertype.NewSubHeader.equals(headerType)) {
            int datasize = headerSize - NEWLHD_SIZE - nameSize;
            if (hasSalt()) {
                datasize -= SALT_SIZE;
            }
            if (datasize > 0) {
                subData = new byte[datasize];
                for (int i = 0; i < datasize; i++) {
                    subData[i] = (fileHeader[position]);
                    position++;
                }
            }

            if (NewSubHeaderType.SUBHEAD_TYPE_RR.byteEquals(fileNameBytes)) {
                recoverySectors = subData[8] + (subData[9] << 8)
                        + (subData[10] << 16) + (subData[11] << 24);
            }
        }

        if (hasSalt()) {
            for (int i = 0; i < SALT_SIZE; i++) {
                salt[i] = fileHeader[position];
                position++;
            }
        }
        mTime = getDateDos(fileTime);
        // TODO rartime -> extended

    }

    @Override
    public void print() {
        super.print();
        StringBuilder str = new StringBuilder();
        str.append("unpSize: " + getUnpSize());
        str.append("\nHostOS: " + hostOS.name());
        str.append("\nMDate: " + mTime);
        str.append("\nFileName: " + getFileNameString());
        str.append("\nunpMethod: " + Integer.toHexString(getUnpMethod()));
        str.append("\nunpVersion: " + Integer.toHexString(getUnpVersion()));
        str.append("\nfullpackedsize: " + getFullPackSize());
        str.append("\nfullunpackedsize: " + getFullUnpackSize());
        str.append("\nisEncrypted: " + isEncrypted());
        str.append("\nisfileHeader: " + isFileHeader());
        str.append("\nisSolid: " + isSolid());
        str.append("\nisSplitafter: " + isSplitAfter());
        str.append("\nisSplitBefore:" + isSplitBefore());
        str.append("\nunpSize: " + getUnpSize());
        str.append("\ndataSize: " + getDataSize());
        str.append("\nisUnicode: " + isUnicode());
        str.append("\nhasVolumeNumber: " + hasVolumeNumber());
        str.append("\nhasArchiveDataCRC: " + hasArchiveDataCRC());
        str.append("\nhasSalt: " + hasSalt());
        str.append("\nhasEncryptVersions: " + hasEncryptVersion());
        str.append("\nisSubBlock: " + isSubBlock());
    }

    private Date getDateDos(int time) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, (time >>> 25) + 1980);
        cal.set(Calendar.MONTH, ((time >>> 21) & 0x0f) - 1);
        cal.set(Calendar.DAY_OF_MONTH, (time >>> 16) & 0x1f);
        cal.set(Calendar.HOUR_OF_DAY, (time >>> 11) & 0x1f);
        cal.set(Calendar.MINUTE, (time >>> 5) & 0x3f);
        cal.set(Calendar.SECOND, (time & 0x1f) * 2);
        return cal.getTime();
    }

    public Date getArcTime() {
        return arcTime;
    }

    public void setArcTime(Date arcTime) {
        this.arcTime = arcTime;
    }

    public Date getATime() {
        return aTime;
    }

    public void setATime(Date time) {
        aTime = time;
    }

    public Date getCTime() {
        return cTime;
    }

    public void setCTime(Date time) {
        cTime = time;
    }

    public int getFileAttr() {
        return fileAttr;
    }

    public void setFileAttr(int fileAttr) {
        this.fileAttr = fileAttr;
    }

    public int getFileCRC() {
        return fileCRC;
    }

    public byte[] getFileNameByteArray() {
        return fileNameBytes;
    }

    public String getFileNameString() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileNameW() {
        return fileNameW;
    }

    public void setFileNameW(String fileNameW) {
        this.fileNameW = fileNameW;
    }

    public int getHighPackSize() {
        return highPackSize;
    }

    public int getHighUnpackSize() {
        return highUnpackSize;
    }

    public HostSystem getHostOS() {
        return hostOS;
    }

    public Date getMTime() {
        return mTime;
    }

    public void setMTime(Date time) {
        mTime = time;
    }

    public short getNameSize() {
        return nameSize;
    }

    public int getRecoverySectors() {
        return recoverySectors;
    }

    public byte[] getSalt() {
        return salt;
    }

    public byte[] getSubData() {
        return subData;
    }

    public int getSubFlags() {
        return subFlags;
    }

    public byte getUnpMethod() {
        return unpMethod;
    }

    public long getUnpSize() {
        return unpSize;
    }

    public byte getUnpVersion() {
        return unpVersion;
    }

    public long getFullPackSize() {
        return fullPackSize;
    }

    public long getFullUnpackSize() {
        return fullUnpackSize;
    }

    @Override
    public String toString() {
        return super.toString();
    }

    public boolean isSplitAfter() {
        return (this.flags & BlockHeader.LHD_SPLIT_AFTER) != 0;
    }

    public boolean isSplitBefore() {
        return (this.flags & LHD_SPLIT_BEFORE) != 0;
    }

    public boolean isSolid() {
        return (this.flags & LHD_SOLID) != 0;
    }

    public boolean isEncrypted() {
        return (this.flags & BlockHeader.LHD_PASSWORD) != 0;
    }

    public boolean isUnicode() {
        return (flags & LHD_UNICODE) != 0;
    }

    public boolean isFileHeader() {
        return UnrarHeadertype.FileHeader.equals(headerType);
    }

    public boolean hasSalt() {
        return (flags & LHD_SALT) != 0;
    }

    public boolean isLargeBlock() {
        return (flags & LHD_LARGE) != 0;
    }

    public boolean isDirectory() {
        return (flags & LHD_WINDOWMASK) == LHD_DIRECTORY;
    }
}