Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions src/Document/Dictionary/DictionaryParseContext/NestingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@

namespace PrinsFrank\PdfParser\Document\Dictionary\DictionaryParseContext;

use PrinsFrank\PdfParser\Document\Generic\Parsing\InfiniteBuffer;

/** @internal */
class NestingContext {
private string $currentLevel;

/** @var array<string, DictionaryParseContext> */
private array $nestingContext = [];

/** @var array<string, InfiniteBuffer> */
/** @var array<string, string> */
private array $keyBuffer = [];

/** @var array<string, InfiniteBuffer> */
/** @var array<string, string> */
private array $valueBuffer = [];

public function __construct() {
Expand All @@ -24,12 +22,16 @@ public function __construct() {

public function incrementNesting(): self {
$this->currentLevel = (string) ($this->keyBuffer[$this->currentLevel] ?? (int) $this->currentLevel + 1);
$this->keyBuffer[$this->currentLevel] = '';
$this->valueBuffer[$this->currentLevel] = '';

return $this;
}

public function decrementNesting(): self {
array_pop($this->nestingContext);
unset($this->keyBuffer[$this->currentLevel]);
unset($this->valueBuffer[$this->currentLevel]);
$this->currentLevel = (string) array_key_last($this->nestingContext);

return $this;
Expand All @@ -45,44 +47,47 @@ public function getContext(): DictionaryParseContext {
return $this->nestingContext[$this->currentLevel] ?? DictionaryParseContext::ROOT;
}

public function getKeyBuffer(): InfiniteBuffer {
return $this->keyBuffer[$this->currentLevel] ??= new InfiniteBuffer();
public function addToKeyBuffer(string $char): void {
$this->keyBuffer[$this->currentLevel] .= $char;
}

public function addToKeyBuffer(string $char): self {
$this->getKeyBuffer()->addChar($char);
public function removeCharFromKeyBuffer(): void {
$this->keyBuffer[$this->currentLevel] = substr($this->keyBuffer[$this->currentLevel], 0, -1);
}

return $this;
public function getKeyBuffer(): string {
return $this->keyBuffer[$this->currentLevel] ?? '';
}

public function getValueBuffer(): InfiniteBuffer {
return $this->valueBuffer[$this->currentLevel] ??= new InfiniteBuffer();
public function addToValueBuffer(string $char): void {
$this->valueBuffer[$this->currentLevel] .= $char;
}

public function addToValueBuffer(string $char): self {
$this->getValueBuffer()->addChar($char);
public function removeCharFromValueBuffer(): void {
$this->valueBuffer[$this->currentLevel] = substr($this->valueBuffer[$this->currentLevel], 0, -1);
}

return $this;
public function getValueBuffer(): string {
return $this->valueBuffer[$this->currentLevel] ?? '';
}

/** @return list<string> */
public function getKeysFromRoot(): array {
$keysFromRoot = [];
foreach ($this->keyBuffer as $keyBuffer) {
$keyBufferString = (string) $keyBuffer;
if ($keyBufferString === '') {
if ($keyBuffer === '') {
continue;
}

$keysFromRoot[] = $keyBufferString;
$keysFromRoot[] = $keyBuffer;
}

return $keysFromRoot;
}

public function flush(): self {
($this->valueBuffer[$this->currentLevel] ?? null)?->flush();
($this->keyBuffer[$this->currentLevel] ?? null)?->flush();
$this->valueBuffer[$this->currentLevel] = '';
$this->keyBuffer[$this->currentLevel] = '';

return $this;
}
Expand Down
23 changes: 8 additions & 15 deletions src/Document/Dictionary/DictionaryParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class DictionaryParser {
public static function parse(?EncryptionContext $encryptionContext, Stream $stream, int $startPos, int $nrOfBytes): Dictionary {
$dictionaryArray = [];
$nestingContext = (new NestingContext())->setContext(DictionaryParseContext::ROOT);
$keyBuffer = $nestingContext->getKeyBuffer();
$valueBuffer = $nestingContext->getValueBuffer();
$arrayNestingLevel = 0;
$previousChar = $secondToLastChar = $currentContext = $contextBeforeComment = $previousIndexLevelDecrease = $previousIndexLevelIncrease = null;
foreach ($stream->chars($startPos, $nrOfBytes) as $index => $char) {
Expand All @@ -34,14 +32,12 @@ public static function parse(?EncryptionContext $encryptionContext, Stream $stre
&& $currentContext !== DictionaryParseContext::VALUE_IN_SQUARE_BRACKETS
&& $previousIndexLevelIncrease !== $index - 1) {
if ($currentContext === DictionaryParseContext::KEY) {
$keyBuffer->removeChar();
$nestingContext->removeCharFromKeyBuffer();
}

$previousIndexLevelIncrease = $index;
$nestingContext->setContext(DictionaryParseContext::DICTIONARY)->incrementNesting()->setContext(DictionaryParseContext::DICTIONARY);
$currentContext = DictionaryParseContext::DICTIONARY;
$keyBuffer = $nestingContext->getKeyBuffer();
$valueBuffer = $nestingContext->getValueBuffer();
} elseif ($char === DelimiterCharacter::LESS_THAN_SIGN->value && $currentContext === DictionaryParseContext::KEY) {
$nestingContext->setContext(DictionaryParseContext::VALUE);
$currentContext = DictionaryParseContext::VALUE;
Expand All @@ -50,13 +46,11 @@ public static function parse(?EncryptionContext $encryptionContext, Stream $stre
&& $secondToLastChar !== LiteralStringEscapeCharacter::REVERSE_SOLIDUS->value
&& $currentContext !== DictionaryParseContext::VALUE_IN_SQUARE_BRACKETS
&& $previousIndexLevelDecrease !== $index - 1) {
$valueBuffer->removeChar();
$nestingContext->removeCharFromValueBuffer();
self::flush($dictionaryArray, $nestingContext);
$previousIndexLevelDecrease = $index;
$nestingContext->decrementNesting()->flush();
$currentContext = $nestingContext->getContext();
$keyBuffer = $nestingContext->getKeyBuffer();
$valueBuffer = $nestingContext->getValueBuffer();
} elseif ($char === DelimiterCharacter::SOLIDUS->value
&& $previousChar !== LiteralStringEscapeCharacter::REVERSE_SOLIDUS->value
&& $currentContext !== DictionaryParseContext::VALUE_IN_SQUARE_BRACKETS) {
Expand Down Expand Up @@ -118,11 +112,11 @@ public static function parse(?EncryptionContext $encryptionContext, Stream $stre
$secondToLastChar = $previousChar;
$previousChar = $char;
if ($currentContext === DictionaryParseContext::KEY) {
$keyBuffer->addChar($char);
$nestingContext->addToKeyBuffer($char);
} elseif ($currentContext === DictionaryParseContext::VALUE_IN_PARENTHESES
|| $currentContext === DictionaryParseContext::VALUE_IN_SQUARE_BRACKETS
|| $currentContext === DictionaryParseContext::VALUE) {
$valueBuffer->addChar($char);
$nestingContext->addToValueBuffer($char);
}
}

Expand All @@ -131,14 +125,13 @@ public static function parse(?EncryptionContext $encryptionContext, Stream $stre

/** @param array<string, mixed> $dictionaryArray */
private static function flush(array &$dictionaryArray, NestingContext $nestingContext): void {
if ($nestingContext->getValueBuffer()->isEmpty() || $nestingContext->getKeyBuffer()->isEmpty()) {
if (($valueBuffer = $nestingContext->getValueBuffer()) === '' || ($keyBuffer = $nestingContext->getKeyBuffer()) === '') {
return;
}

$dictionaryArrayPointer = &$dictionaryArray;
$keys = $nestingContext->getKeysFromRoot();
foreach ($keys as $index => $key) {
if ($key === (string) $nestingContext->getKeyBuffer() && $index === array_key_last($keys)) {
foreach (($keys = $nestingContext->getKeysFromRoot()) as $index => $key) {
if ($key === $keyBuffer && $index === array_key_last($keys)) {
break;
}

Expand All @@ -147,7 +140,7 @@ private static function flush(array &$dictionaryArray, NestingContext $nestingCo
}

/** @phpstan-ignore offsetAccess.nonOffsetAccessible */
$dictionaryArrayPointer[(string) $nestingContext->getKeyBuffer()] = trim((string) $nestingContext->getValueBuffer());
$dictionaryArrayPointer[$keyBuffer] = trim($valueBuffer);
$nestingContext->flush();
}
}
48 changes: 0 additions & 48 deletions src/Document/Generic/Parsing/InfiniteBuffer.php

This file was deleted.

Loading