Skip to content

Commit 80ab7d6

Browse files
authored
Use object length instead of searching for end marker when set in dictionary (#95)
* Use object length instead of searching for end marker when set in dictionary
1 parent 2e8cf81 commit 80ab7d6

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/Document/Object/Item/CompressedObject/CompressedObjectByteOffsetParser.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ class CompressedObjectByteOffsetParser {
2020
public static function parse(Stream $stream, int $startOffsetObject, int $endOffsetObject, Dictionary $dictionary): CompressedObjectByteOffsets {
2121
$startStreamPos = $stream->getStartNextLineAfter(Marker::STREAM, $startOffsetObject, $endOffsetObject)
2222
?? throw new ParseFailureException(sprintf('Unable to locate marker %s', Marker::STREAM->value));
23-
$endStreamPos = $stream->firstPos(Marker::END_STREAM, $startStreamPos, $endOffsetObject)
24-
?? throw new ParseFailureException(sprintf('Unable to locate marker %s', Marker::END_STREAM->value));
25-
$eolPos = $stream->getEndOfCurrentLine($endStreamPos - 1, $endOffsetObject)
26-
?? throw new ParseFailureException(sprintf('Unable to locate marker %s', WhitespaceCharacter::LINE_FEED->value));
27-
$content = bin2hex(CompressedObjectContentParser::parseBinary($stream, $startStreamPos, $eolPos - $startStreamPos, $dictionary));
23+
if ($dictionary->getTypeForKey(DictionaryKey::LENGTH) === IntegerValue::class && ($lengthInteger = $dictionary->getValueForKey(DictionaryKey::LENGTH, IntegerValue::class)) !== null) {
24+
$length = $lengthInteger->value;
25+
} else {
26+
$endStreamPos = $stream->firstPos(Marker::END_STREAM, $startStreamPos, $endOffsetObject)
27+
?? throw new ParseFailureException(sprintf('Unable to locate marker %s', Marker::END_STREAM->value));
28+
$eolPos = $stream->getEndOfCurrentLine($endStreamPos - 1, $endOffsetObject)
29+
?? throw new ParseFailureException(sprintf('Unable to locate marker %s', WhitespaceCharacter::LINE_FEED->value));
30+
$length = $eolPos - $startStreamPos;
31+
}
32+
33+
$content = bin2hex(CompressedObjectContentParser::parseBinary($stream, $startStreamPos, $length, $dictionary));
2834
$first = $dictionary->getValueForKey(DictionaryKey::FIRST, IntegerValue::class)
2935
?? throw new RuntimeException('Expected a dictionary entry for "First", none found');
3036
$buffer = new InfiniteBuffer();

0 commit comments

Comments
 (0)