(obj: PdfObject, objNum: number, genNum: number)
| 581 | * Decrypt an object's strings and stream data. |
| 582 | */ |
| 583 | const decryptObject = (obj: PdfObject, objNum: number, genNum: number): PdfObject => { |
| 584 | try { |
| 585 | if (!securityHandler?.isAuthenticated) { |
| 586 | return obj; |
| 587 | } |
| 588 | |
| 589 | if (obj instanceof PdfString) { |
| 590 | const decrypted = securityHandler.decryptString(obj.bytes, objNum, genNum); |
| 591 | |
| 592 | return new PdfString(decrypted, obj.format); |
| 593 | } |
| 594 | |
| 595 | if (obj instanceof PdfArray) { |
| 596 | const decryptedItems: PdfObject[] = []; |
| 597 | |
| 598 | for (const item of obj) { |
| 599 | decryptedItems.push(decryptObject(item, objNum, genNum)); |
| 600 | } |
| 601 | |
| 602 | return new PdfArray(decryptedItems); |
| 603 | } |
| 604 | |
| 605 | // Check PdfStream BEFORE PdfDict (PdfStream extends PdfDict) |
| 606 | if (obj instanceof PdfStream) { |
| 607 | // Check if this stream should be encrypted |
| 608 | const streamType = obj.getName("Type")?.value; |
| 609 | |
| 610 | if (!securityHandler.shouldEncryptStream(streamType)) { |
| 611 | return obj; |
| 612 | } |
| 613 | |
| 614 | // Decrypt stream data |
| 615 | const decryptedData = securityHandler.decryptStream(obj.data, objNum, genNum); |
| 616 | |
| 617 | // Create new stream with decrypted data |
| 618 | // Copy dictionary entries (strings in dict will be decrypted when accessed) |
| 619 | const newStream = new PdfStream(obj, decryptedData); |
| 620 | |
| 621 | // Decrypt strings in the dictionary entries |
| 622 | for (const [key, value] of obj) { |
| 623 | const decryptedValue = decryptObject(value, objNum, genNum); |
| 624 | |
| 625 | if (decryptedValue !== value) { |
| 626 | newStream.set(key.value, decryptedValue); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | return newStream; |
| 631 | } |
| 632 | |
| 633 | if (obj instanceof PdfDict) { |
| 634 | const decryptedDict = new PdfDict(); |
| 635 | |
| 636 | for (const [key, value] of obj) { |
| 637 | decryptedDict.set(key.value, decryptObject(value, objNum, genNum)); |
| 638 | } |
| 639 | |
| 640 | return decryptedDict; |
nothing calls this directly
no test coverage detected