Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit62fb5ba

Browse files
committed
More work on#2
Tidied up the handling of invalid sectors in the DIFAT; special valuesnow always terminate the DIFAT and FREE_SECTOR is no longer a warning asa terminator (the AAF spec is rather ambiguous on this point but it'sworth following the de-facto standard practice)
1 parent8bc177c commit62fb5ba

9 files changed

+74
-17
lines changed

‎compoundfiles/reader.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ def _load_master_fat(self):
357357
# Note: when reading the master-FAT we deliberately disregard the
358358
# master-FAT sector count read from the header as implementations may
359359
# set this incorrectly. Instead, we scan for END_OF_CHAIN (or
360-
# FREE_SECTOR) in the DIFAT after each read and stop when we find it. # In order to avoid infinite loops (in the case of a stupid or
360+
# FREE_SECTOR) in the DIFAT after each read and stop when we find it.
361+
# In order to avoid infinite loops (in the case of a stupid or
361362
# malicious file) we keep track of each sector we seek to and quit in
362363
# the event of a repeat
363364
self._master_fat=array(native_str('L'))
@@ -392,24 +393,21 @@ def _load_master_fat(self):
392393
# Check for an END_OF_CHAIN marker in the existing stream
393394
forindexinrange(checked,len(self._master_fat)):
394395
value=self._master_fat[index]
395-
ifvalue==END_OF_CHAIN:
396-
break
397-
elifvalue==FREE_SECTOR:
398-
warnings.warn(
399-
CompoundFileMasterFatWarning(
400-
'DIFAT terminated by FREE_SECTOR'))
401-
value=END_OF_CHAIN
402-
break
403-
elifself._max_sector<value<=MAX_NORMAL_SECTOR:
404-
warnings.warn(
405-
CompoundFileMasterFatWarning(
406-
'sector in DIFAT chain beyond file end (%d)'%value))
396+
ifvalue>self._max_sector:
397+
ifvaluein (END_OF_CHAIN,FREE_SECTOR):
398+
pass
399+
elifvalue>MAX_NORMAL_SECTOR:
400+
warnings.warn(
401+
CompoundFileMasterFatWarning(
402+
'DIFAT terminated by invalid special '
403+
'value (%d)'%value))
404+
else:
405+
warnings.warn(
406+
CompoundFileMasterFatWarning(
407+
'sector in DIFAT chain beyond file '
408+
'end (%d)'%value))
407409
value=END_OF_CHAIN
408410
break
409-
elifvalue>MAX_NORMAL_SECTOR:
410-
warnings.warn(
411-
CompoundFileMasterFatWarning(
412-
'invalid special value in DIFAT chain (%d)'%value))
413411
ifvalue==END_OF_CHAIN:
414412
delself._master_fat[index:]
415413
break

‎tests/invalid_master_eof.dat

3 KB
Binary file not shown.

‎tests/invalid_master_loop.dat

60.5 KB
Binary file not shown.

‎tests/invalid_master_overrun.dat

60.5 KB
Binary file not shown.

‎tests/invalid_master_special.dat

3 KB
Binary file not shown.

‎tests/invalid_master_underrun.dat

60.5 KB
Binary file not shown.

‎tests/strange_master_ext.dat

60.5 KB
Binary file not shown.

‎tests/strange_master_full.dat

60.5 KB
Binary file not shown.

‎tests/test_function.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,22 @@ def test_invalid_dir_sector_count():
302302
assertlen(w)==1
303303
verify_example(doc)
304304

305+
deftest_invalid_master_sectors():
306+
withwarnings.catch_warnings(record=True)asw:
307+
# Same as example.dat with second master FAT block pointing beyond EOF;
308+
# reader ignores it and all further blocks
309+
withcf.CompoundFileReader('tests/invalid_master_eof.dat')asdoc:
310+
assertissubclass(w[0].category,cf.CompoundFileMasterFatWarning)
311+
assertlen(w)==1
312+
verify_example(doc)
313+
withwarnings.catch_warnings(record=True)asw:
314+
# Same as example.dat with second master FAT block set to
315+
# MASTER_FAT_SECTOR; reader ignores it and all further blocks
316+
withcf.CompoundFileReader('tests/invalid_master_special.dat')asdoc:
317+
assertissubclass(w[0].category,cf.CompoundFileMasterFatWarning)
318+
assertlen(w)==1
319+
verify_example(doc)
320+
305321
deftest_invalid_master_ext():
306322
withwarnings.catch_warnings(record=True)asw:
307323
# Same as example.dat with master FAT extension pointer set to
@@ -376,6 +392,49 @@ def test_strange_mini_sector_size():
376392
assertlen(w)==1
377393
verify_example(doc)
378394

395+
deftest_strange_master_full():
396+
# Same as example.dat with FAT extended to include enough blank sectors to
397+
# fill the DIFAT without an extension (e.g. a compound document where lots
398+
# of contents have been deleted and the FAT hasn't been compressed but
399+
# simply overwritten)
400+
withwarnings.catch_warnings(record=True)asw:
401+
withcf.CompoundFileReader('tests/strange_master_full.dat')asdoc:
402+
assertlen(w)==0
403+
verify_example(doc)
404+
405+
deftest_strange_master_ext():
406+
# Same as example.dat with FAT extended to include enough blank sectors to
407+
# necessitate a DIFAT extension (e.g. a compound document where lots of
408+
# contents have been deleted and the FAT hasn't been compressed but simply
409+
# overwritten)
410+
withwarnings.catch_warnings(record=True)asw:
411+
withcf.CompoundFileReader('tests/strange_master_ext.dat')asdoc:
412+
assertlen(w)==0
413+
verify_example(doc)
414+
415+
deftest_invalid_master_loop():
416+
withpytest.raises(cf.CompoundFileMasterLoopError):
417+
# Same as strange_master_ext.dat but with DIFAT extension sector filled
418+
# and terminated with a self-reference
419+
doc=cf.CompoundFileReader('tests/invalid_master_loop.dat')
420+
421+
deftest_invalid_master_len():
422+
withwarnings.catch_warnings(record=True)asw:
423+
# Same as strange_master_ext.dat with master extension count set to 2
424+
# (should be 1); reader ignores DIFAT count
425+
withcf.CompoundFileReader('tests/invalid_master_underrun.dat')asdoc:
426+
assertissubclass(w[0].category,cf.CompoundFileMasterFatWarning)
427+
assertlen(w)==1
428+
verify_example(doc)
429+
withwarnings.catch_warnings(record=True)asw:
430+
# Same as strange_master_ext.dat with master extension count set to 0
431+
# (should be 1); reader ignores DIFAT count
432+
withcf.CompoundFileReader('tests/invalid_master_overrun.dat')asdoc:
433+
assertissubclass(w[0].category,cf.CompoundFileMasterFatWarning)
434+
assertissubclass(w[1].category,cf.CompoundFileMasterFatWarning)
435+
assertlen(w)==2
436+
verify_example(doc)
437+
379438
deftest_stream_attr():
380439
withcf.CompoundFileReader('tests/example.dat')asdoc:
381440
withdoc.open('Storage 1/Stream 1')asf:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp