()
| 276 | } |
| 277 | |
| 278 | private parseDict(): ParseResult { |
| 279 | this.shift(); // consume '<<' |
| 280 | |
| 281 | // Collect entries in array to pass to constructor (avoids setting dirty flag) |
| 282 | const entries: [PdfName, PdfObject][] = []; |
| 283 | |
| 284 | while (true) { |
| 285 | this.ensureBufferFilled(); |
| 286 | |
| 287 | if (this.buf1 === null || this.buf1.type === "eof") { |
| 288 | if (this.recoveryMode) { |
| 289 | this.warn("Unterminated dictionary at EOF"); |
| 290 | break; |
| 291 | } |
| 292 | |
| 293 | throw new ObjectParseError("Unterminated dictionary at EOF"); |
| 294 | } |
| 295 | |
| 296 | if (this.buf1.type === "delimiter" && this.buf1.value === ">>") { |
| 297 | // Partial shift: move buf2 to buf1, but DON'T fill buf2. |
| 298 | // This prevents reading past a "stream" keyword into binary data. |
| 299 | this.buf1 = this.buf2; |
| 300 | this.buf2 = null; |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | // Key must be a name |
| 305 | if (this.buf1.type !== "name") { |
| 306 | if (this.recoveryMode) { |
| 307 | this.warn(`Invalid dictionary key: expected name, got ${this.buf1.type}`); |
| 308 | // Skip the invalid key AND its would-be value (pair recovery) |
| 309 | this.skipInvalidPair(); |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | throw new ObjectParseError(`Invalid dictionary key: expected name, got ${this.buf1.type}`); |
| 314 | } |
| 315 | |
| 316 | const key = PdfName.of(this.buf1.value); |
| 317 | |
| 318 | this.shift(); // consume key |
| 319 | |
| 320 | // Check if value is missing (>> immediately after key) |
| 321 | this.ensureBufferFilled(); |
| 322 | const valueToken = this.current(); |
| 323 | if (valueToken !== null && valueToken.type === "delimiter" && valueToken.value === ">>") { |
| 324 | if (this.recoveryMode) { |
| 325 | this.warn(`Missing value for key ${key.value}`); |
| 326 | // Don't consume >>, let the loop handle it |
| 327 | continue; |
| 328 | } |
| 329 | |
| 330 | throw new ObjectParseError(`Missing value for key ${key.value}`); |
| 331 | } |
| 332 | |
| 333 | // Parse value |
| 334 | const valueResult = this.parseObject(); |
| 335 |
no test coverage detected