Lines 19-30
Link Here
|
19 |
|
19 |
|
20 |
import java.io.ByteArrayOutputStream; |
20 |
import java.io.ByteArrayOutputStream; |
21 |
import java.io.InputStream; |
21 |
import java.io.InputStream; |
|
|
22 |
import java.util.Set; |
23 |
import java.util.Collections; |
24 |
import java.util.HashSet; |
22 |
|
25 |
|
23 |
import org.apache.poi.hssf.dev.BiffViewer; |
26 |
import org.apache.poi.hssf.dev.BiffViewer; |
24 |
import org.apache.poi.util.LittleEndian; |
27 |
import org.apache.poi.util.LittleEndian; |
25 |
import org.apache.poi.util.LittleEndianInput; |
28 |
import org.apache.poi.util.LittleEndianInput; |
26 |
import org.apache.poi.util.LittleEndianInputStream; |
29 |
import org.apache.poi.util.LittleEndianInputStream; |
|
|
30 |
import org.apache.poi.util.RC4InputStream; |
27 |
|
31 |
|
|
|
32 |
import javax.crypto.Cipher; |
33 |
|
28 |
/** |
34 |
/** |
29 |
* Title: Record Input Stream<P> |
35 |
* Title: Record Input Stream<P> |
30 |
* Description: Wraps a stream and provides helper methods for the construction of records.<P> |
36 |
* Description: Wraps a stream and provides helper methods for the construction of records.<P> |
Lines 41-48
Link Here
|
41 |
*/ |
47 |
*/ |
42 |
private static final int DATA_LEN_NEEDS_TO_BE_READ = -1; |
48 |
private static final int DATA_LEN_NEEDS_TO_BE_READ = -1; |
43 |
private static final byte[] EMPTY_BYTE_ARRAY = { }; |
49 |
private static final byte[] EMPTY_BYTE_ARRAY = { }; |
44 |
|
50 |
|
45 |
/** |
51 |
private RC4InputStream rc4is; |
|
|
52 |
|
53 |
private static final Set<Short> unencryptSid = new HashSet<Short>(); |
54 |
static { |
55 |
/* |
56 |
[MS-XLS] — v20090708 |
57 |
Excel Binary File Format (.xls) Structure Specification |
58 |
*/ |
59 |
unencryptSid.add(BOFRecord.sid); |
60 |
unencryptSid.add(FilePassRecord.sid); |
61 |
// UsrExcl |
62 |
// FileLock |
63 |
unencryptSid.add(InterfaceHdrRecord.sid); |
64 |
// RRDInfo |
65 |
// RRDHead |
66 |
// TODO: Additionally, the lbPlyPos field of the BoundSheet8 record MUST NOT be encrypted. |
67 |
} |
68 |
|
69 |
/** |
46 |
* For use in {@link BiffViewer} which may construct {@link Record}s that don't completely |
70 |
* For use in {@link BiffViewer} which may construct {@link Record}s that don't completely |
47 |
* read all available data. This exception should never be thrown otherwise. |
71 |
* read all available data. This exception should never be thrown otherwise. |
48 |
*/ |
72 |
*/ |
Lines 77-89
Link Here
|
77 |
private int _currentDataOffset; |
101 |
private int _currentDataOffset; |
78 |
|
102 |
|
79 |
public RecordInputStream(InputStream in) throws RecordFormatException { |
103 |
public RecordInputStream(InputStream in) throws RecordFormatException { |
80 |
if (in instanceof LittleEndianInput) { |
104 |
rc4is = new RC4InputStream(in); |
81 |
// accessing directly is an optimisation |
105 |
_le = new LittleEndianInputStream(rc4is); |
82 |
_le = (LittleEndianInput) in; |
|
|
83 |
} else { |
84 |
// less optimal, but should work OK just the same. Often occurs in junit tests. |
85 |
_le = new LittleEndianInputStream(in); |
86 |
} |
87 |
_nextSid = readNextSid(); |
106 |
_nextSid = readNextSid(); |
88 |
} |
107 |
} |
89 |
|
108 |
|
Lines 100-105
Link Here
|
100 |
_currentDataOffset += LittleEndian.BYTE_SIZE; |
119 |
_currentDataOffset += LittleEndian.BYTE_SIZE; |
101 |
return _le.readUByte(); |
120 |
return _le.readUByte(); |
102 |
} |
121 |
} |
|
|
122 |
|
103 |
public int read(byte[] b, int off, int len) { |
123 |
public int read(byte[] b, int off, int len) { |
104 |
int limit = Math.min(len, remaining()); |
124 |
int limit = Math.min(len, remaining()); |
105 |
if (limit == 0) { |
125 |
if (limit == 0) { |
Lines 143-148
Link Here
|
143 |
} |
163 |
} |
144 |
return INVALID_SID_VALUE; |
164 |
return INVALID_SID_VALUE; |
145 |
} |
165 |
} |
|
|
166 |
rc4is.setDecrypt(false); |
146 |
int result = _le.readUShort(); |
167 |
int result = _le.readUShort(); |
147 |
if (result == INVALID_SID_VALUE) { |
168 |
if (result == INVALID_SID_VALUE) { |
148 |
throw new RecordFormatException("Found invalid sid (" + result + ")"); |
169 |
throw new RecordFormatException("Found invalid sid (" + result + ")"); |
Lines 169-174
Link Here
|
169 |
throw new RecordFormatException("The content of an excel record cannot exceed " |
190 |
throw new RecordFormatException("The content of an excel record cannot exceed " |
170 |
+ MAX_RECORD_DATA_SIZE + " bytes"); |
191 |
+ MAX_RECORD_DATA_SIZE + " bytes"); |
171 |
} |
192 |
} |
|
|
193 |
|
194 |
if (!unencryptSid.contains((short) _currentSid)) { |
195 |
rc4is.setDecrypt(true); |
196 |
} |
172 |
} |
197 |
} |
173 |
|
198 |
|
174 |
private void checkRecordPosition(int requiredByteCount) { |
199 |
private void checkRecordPosition(int requiredByteCount) { |
Lines 394-397
Link Here
|
394 |
// and before the formatting run data) |
419 |
// and before the formatting run data) |
395 |
return _nextSid == ContinueRecord.sid; |
420 |
return _nextSid == ContinueRecord.sid; |
396 |
} |
421 |
} |
|
|
422 |
|
423 |
public void setRC4Header(RC4Header rc4Header) { |
424 |
rc4is.setHeader(rc4Header); |
425 |
} |
397 |
} |
426 |
} |