Skip to content

Commit d0b34a5

Browse files
authored
mask bytes in chm unmarshalUInt32 to avoid sign extension (#2887)
1 parent 278fec0 commit d0b34a5

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/parser/microsoft/chm/ChmItsfHeader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,10 @@ private long unmarshalUInt32(byte[] data, long dest) throws TikaException {
395395
if (4 > getDataRemained()) {
396396
throw new TikaException("4 > dataLenght");
397397
}
398-
dest = data[this.getCurrentPlace()] | data[this.getCurrentPlace() + 1] << 8 |
399-
data[this.getCurrentPlace() + 2] << 16 | data[this.getCurrentPlace() + 3] << 24;
398+
dest = (data[this.getCurrentPlace()] & 0xff) |
399+
(data[this.getCurrentPlace() + 1] & 0xff) << 8 |
400+
(data[this.getCurrentPlace() + 2] & 0xff) << 16 |
401+
(data[this.getCurrentPlace() + 3] & 0xff) << 24;
400402

401403
setDataRemained(this.getDataRemained() - 4);
402404
this.setCurrentPlace(this.getCurrentPlace() + 4);

tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/main/java/org/apache/tika/parser/microsoft/chm/ChmLzxcControlData.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ private long unmarshalUInt32(byte[] data, long dest) throws ChmParsingException
225225
if (4 > getDataRemained()) {
226226
throw new ChmParsingException("4 > dataLenght");
227227
}
228-
dest = data[this.getCurrentPlace()] | data[this.getCurrentPlace() + 1] << 8 |
229-
data[this.getCurrentPlace() + 2] << 16 | data[this.getCurrentPlace() + 3] << 24;
228+
dest = (data[this.getCurrentPlace()] & 0xff) |
229+
(data[this.getCurrentPlace() + 1] & 0xff) << 8 |
230+
(data[this.getCurrentPlace() + 2] & 0xff) << 16 |
231+
(data[this.getCurrentPlace() + 3] & 0xff) << 24;
230232

231233
setDataRemained(this.getDataRemained() - 4);
232234
this.setCurrentPlace(this.getCurrentPlace() + 4);

tika-parsers/tika-parsers-standard/tika-parsers-standard-modules/tika-parser-microsoft-module/src/test/java/org/apache/tika/parser/microsoft/chm/TestChmLzxcControlData.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,22 @@ public void testGetSignaure() {
124124
chmLzxcControlData.getSignature().length);
125125
}
126126

127+
@Test
128+
public void testUInt32HighBitNotSignExtended() throws Exception {
129+
// size = 0x00008000 little-endian; the 0x80 byte has bit 7 set, so an
130+
// unmasked shift sign-extends it and yields -32768 instead of 32768
131+
byte[] data = new byte[ChmConstants.CHM_LZXC_MIN_LEN];
132+
data[1] = (byte) 0x80;
133+
byte[] sig = ChmConstants.LZXC.getBytes(UTF_8);
134+
System.arraycopy(sig, 0, data, 4, sig.length);
135+
data[8] = 0x01; // version
136+
data[12] = 0x02; // resetInterval
137+
data[16] = 0x02; // windowSize
138+
data[20] = 0x02; // windowsPerReset
139+
140+
ChmLzxcControlData control = new ChmLzxcControlData();
141+
control.parse(data, control);
142+
assertEquals(0x8000L, control.getSize());
143+
}
144+
127145
}

0 commit comments

Comments
 (0)