oxidize-pdf 2.5.0

A pure Rust PDF generation and manipulation library with zero external dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
//! PDF corruption detection and analysis

use crate::error::Result;
use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::Path;

/// Types of PDF corruption
#[derive(Debug, Clone, PartialEq)]
pub enum CorruptionType {
    /// Missing or invalid PDF header
    InvalidHeader,
    /// Corrupted cross-reference table
    CorruptXRef,
    /// Missing EOF marker
    MissingEOF,
    /// Invalid object references
    BrokenReferences,
    /// Corrupted content streams
    CorruptStreams,
    /// Invalid page tree
    InvalidPageTree,
    /// Truncated file
    TruncatedFile,
    /// Multiple corruption types
    Multiple(Vec<CorruptionType>),
    /// Unknown corruption
    Unknown,
}

/// Corruption analysis report
#[derive(Debug)]
pub struct CorruptionReport {
    /// Primary corruption type
    pub corruption_type: CorruptionType,
    /// Severity level (0-10)
    pub severity: u8,
    /// Detailed error messages
    pub errors: Vec<String>,
    /// Recoverable sections
    pub recoverable_sections: Vec<RecoverableSection>,
    /// File statistics
    pub file_stats: FileStats,
}

/// A potentially recoverable section
#[derive(Debug)]
pub struct RecoverableSection {
    /// Section type
    pub section_type: SectionType,
    /// Start offset in file
    pub start_offset: u64,
    /// End offset in file
    pub end_offset: u64,
    /// Confidence level (0.0 - 1.0)
    pub confidence: f32,
}

/// Types of PDF sections
#[derive(Debug, Clone)]
pub enum SectionType {
    Header,
    Body,
    XRef,
    Trailer,
    Page(u32),
    Object(u32),
    Stream(u32),
}

/// File statistics
#[derive(Debug, Default)]
pub struct FileStats {
    /// Total file size
    pub file_size: u64,
    /// Number of readable bytes
    pub readable_bytes: u64,
    /// Estimated object count
    pub estimated_objects: usize,
    /// Found page references
    pub found_pages: usize,
}

/// Detect corruption in a PDF file
pub fn detect_corruption<P: AsRef<Path>>(path: P) -> Result<CorruptionReport> {
    let mut file = File::open(path)?;
    let mut reader = BufReader::new(&mut file);

    let file_size = reader.seek(SeekFrom::End(0))?;
    reader.seek(SeekFrom::Start(0))?;

    let mut report = CorruptionReport {
        corruption_type: CorruptionType::Unknown,
        severity: 0,
        errors: Vec::new(),
        recoverable_sections: Vec::new(),
        file_stats: FileStats {
            file_size,
            ..Default::default()
        },
    };

    // Check PDF header
    if !check_header(&mut reader, &mut report)? {
        report.corruption_type = CorruptionType::InvalidHeader;
        report.severity = 10;
        return Ok(report);
    }

    // Check for EOF marker
    check_eof(&mut reader, &mut report)?;

    // Scan for cross-reference tables
    scan_xref(&mut reader, &mut report)?;

    // Analyze object structure
    analyze_objects(&mut reader, &mut report)?;

    // Determine overall corruption type
    determine_corruption_type(&mut report);

    Ok(report)
}

fn check_header<R: Read + Seek>(reader: &mut R, report: &mut CorruptionReport) -> Result<bool> {
    let mut header = [0u8; 8];
    reader.seek(SeekFrom::Start(0))?;

    match reader.read_exact(&mut header) {
        Ok(_) => {
            if &header[0..5] == b"%PDF-" {
                report.recoverable_sections.push(RecoverableSection {
                    section_type: SectionType::Header,
                    start_offset: 0,
                    end_offset: 8,
                    confidence: 1.0,
                });
                Ok(true)
            } else {
                report.errors.push("Invalid PDF header".to_string());
                Ok(false)
            }
        }
        Err(e) => {
            report.errors.push(format!("Cannot read header: {e}"));
            Ok(false)
        }
    }
}

fn check_eof<R: Read + Seek>(reader: &mut R, report: &mut CorruptionReport) -> Result<()> {
    // Check last 1024 bytes for %%EOF
    let check_size = 1024.min(report.file_stats.file_size);
    let start_pos = report.file_stats.file_size.saturating_sub(check_size);

    reader.seek(SeekFrom::Start(start_pos))?;
    let mut buffer = vec![0u8; check_size as usize];
    reader.read_exact(&mut buffer)?;

    if !buffer.windows(5).any(|w| w == b"%%EOF") {
        report.errors.push("Missing %%EOF marker".to_string());
        report.severity = report.severity.max(5);
    }

    // Always report something for analysis
    if report.errors.is_empty() && report.severity == 0 {
        report
            .errors
            .push("PDF structure analysis complete".to_string());
    }

    Ok(())
}

fn scan_xref<R: Read + Seek>(reader: &mut R, report: &mut CorruptionReport) -> Result<()> {
    reader.seek(SeekFrom::Start(0))?;
    let mut buffer = Vec::new();
    reader.read_to_end(&mut buffer)?;

    // Look for xref tables
    let mut xref_count = 0;
    let mut pos = 0;

    while let Some(xref_pos) = find_pattern(&buffer[pos..], b"xref") {
        let absolute_pos = pos + xref_pos;
        xref_count += 1;

        report.recoverable_sections.push(RecoverableSection {
            section_type: SectionType::XRef,
            start_offset: absolute_pos as u64,
            end_offset: (absolute_pos + 100) as u64, // Estimate
            confidence: 0.8,
        });

        pos = absolute_pos + 4;
    }

    if xref_count == 0 {
        report
            .errors
            .push("No cross-reference tables found".to_string());
        report.severity = report.severity.max(8);
    }

    Ok(())
}

fn analyze_objects<R: Read + Seek>(reader: &mut R, report: &mut CorruptionReport) -> Result<()> {
    reader.seek(SeekFrom::Start(0))?;
    let mut buffer = Vec::new();
    reader.read_to_end(&mut buffer)?;

    // Count objects
    let mut object_count = 0;
    let mut page_count = 0;
    let mut pos = 0;

    // Look for object definitions
    while pos < buffer.len() {
        if let Some(obj_pos) = find_pattern(&buffer[pos..], b" obj") {
            let absolute_pos = pos + obj_pos;
            object_count += 1;

            // Check if it's a page object
            let check_end = (absolute_pos + 200).min(buffer.len());
            if find_pattern(&buffer[absolute_pos..check_end], b"/Type /Page").is_some() {
                page_count += 1;
            }

            pos = absolute_pos + 4;
        } else {
            break;
        }
    }

    report.file_stats.estimated_objects = object_count;
    report.file_stats.found_pages = page_count;
    report.file_stats.readable_bytes = buffer.len() as u64;

    if object_count == 0 {
        report.errors.push("No PDF objects found".to_string());
        report.severity = 10;
    }

    Ok(())
}

fn determine_corruption_type(report: &mut CorruptionReport) {
    let mut types = Vec::new();

    for error in &report.errors {
        if error.contains("header") {
            types.push(CorruptionType::InvalidHeader);
        } else if error.contains("EOF") {
            types.push(CorruptionType::MissingEOF);
        } else if error.contains("cross-reference") || error.contains("xref") {
            types.push(CorruptionType::CorruptXRef);
        }
    }

    if types.is_empty() && report.severity > 0 {
        report.corruption_type = CorruptionType::Unknown;
    } else if types.len() == 1 {
        // SAFETY: We just checked len() == 1, so next() must return Some
        if let Some(corruption_type) = types.into_iter().next() {
            report.corruption_type = corruption_type;
        }
    } else if types.len() > 1 {
        report.corruption_type = CorruptionType::Multiple(types);
    }
}

fn find_pattern(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    if needle.is_empty() {
        return Some(0);
    }
    haystack
        .windows(needle.len())
        .position(|window| window == needle)
}

/// Quick corruption check
pub fn is_corrupted<P: AsRef<Path>>(path: P) -> bool {
    detect_corruption(path)
        .map(|report| report.severity > 0)
        .unwrap_or(true)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_corruption_type() {
        let corruption = CorruptionType::InvalidHeader;
        assert_eq!(corruption, CorruptionType::InvalidHeader);

        let multiple = CorruptionType::Multiple(vec![
            CorruptionType::InvalidHeader,
            CorruptionType::CorruptXRef,
        ]);
        assert!(matches!(multiple, CorruptionType::Multiple(_)));
    }

    #[test]
    fn test_find_pattern() {
        let haystack = b"Hello PDF world";
        assert_eq!(find_pattern(haystack, b"PDF"), Some(6));
        assert_eq!(find_pattern(haystack, b"XYZ"), None);
    }

    #[test]
    fn test_file_stats_default() {
        let stats = FileStats::default();
        assert_eq!(stats.file_size, 0);
        assert_eq!(stats.readable_bytes, 0);
        assert_eq!(stats.estimated_objects, 0);
        assert_eq!(stats.found_pages, 0);
    }

    #[test]
    fn test_corruption_type_debug_clone_eq() {
        let corruption = CorruptionType::InvalidHeader;
        let debug_str = format!("{corruption:?}");
        assert!(debug_str.contains("InvalidHeader"));

        let cloned = corruption.clone();
        assert_eq!(corruption, cloned);

        // Test all variants
        let variants = vec![
            CorruptionType::InvalidHeader,
            CorruptionType::CorruptXRef,
            CorruptionType::MissingEOF,
            CorruptionType::BrokenReferences,
            CorruptionType::CorruptStreams,
            CorruptionType::InvalidPageTree,
            CorruptionType::TruncatedFile,
            CorruptionType::Unknown,
        ];

        for variant in variants {
            let _ = format!("{variant:?}");
            let _ = variant.clone();
        }
    }

    #[test]
    fn test_corruption_type_multiple() {
        let types = vec![
            CorruptionType::InvalidHeader,
            CorruptionType::CorruptXRef,
            CorruptionType::MissingEOF,
        ];
        let multiple = CorruptionType::Multiple(types);

        match &multiple {
            CorruptionType::Multiple(inner) => {
                assert_eq!(inner.len(), 3);
                assert_eq!(inner[0], CorruptionType::InvalidHeader);
            }
            _ => panic!("Should be Multiple variant"),
        }
    }

    #[test]
    fn test_section_type_debug_clone() {
        let sections = vec![
            SectionType::Header,
            SectionType::Body,
            SectionType::XRef,
            SectionType::Trailer,
            SectionType::Page(42),
            SectionType::Object(123),
            SectionType::Stream(456),
        ];

        for section in sections {
            let debug_str = format!("{section:?}");
            assert!(!debug_str.is_empty());

            let cloned = section.clone();
            match (section, cloned) {
                (SectionType::Page(n1), SectionType::Page(n2)) => assert_eq!(n1, n2),
                (SectionType::Object(n1), SectionType::Object(n2)) => assert_eq!(n1, n2),
                (SectionType::Stream(n1), SectionType::Stream(n2)) => assert_eq!(n1, n2),
                _ => {}
            }
        }
    }

    #[test]
    fn test_recoverable_section_creation() {
        let section = RecoverableSection {
            section_type: SectionType::Page(1),
            start_offset: 100,
            end_offset: 500,
            confidence: 0.95,
        };

        assert_eq!(section.start_offset, 100);
        assert_eq!(section.end_offset, 500);
        assert_eq!(section.confidence, 0.95);

        let debug_str = format!("{section:?}");
        assert!(debug_str.contains("RecoverableSection"));
    }

    #[test]
    fn test_corruption_report_creation() {
        let report = CorruptionReport {
            corruption_type: CorruptionType::CorruptXRef,
            severity: 7,
            errors: vec!["Error 1".to_string(), "Error 2".to_string()],
            recoverable_sections: vec![RecoverableSection {
                section_type: SectionType::Header,
                start_offset: 0,
                end_offset: 10,
                confidence: 1.0,
            }],
            file_stats: FileStats {
                file_size: 1000,
                readable_bytes: 900,
                estimated_objects: 10,
                found_pages: 3,
            },
        };

        assert_eq!(report.severity, 7);
        assert_eq!(report.errors.len(), 2);
        assert_eq!(report.recoverable_sections.len(), 1);
        assert_eq!(report.file_stats.file_size, 1000);
    }

    #[test]
    fn test_find_pattern_various_cases() {
        // Pattern at start
        assert_eq!(find_pattern(b"xref table", b"xref"), Some(0));

        // Pattern at end
        assert_eq!(find_pattern(b"table xref", b"xref"), Some(6));

        // Pattern in middle
        assert_eq!(find_pattern(b"PDF xref table", b"xref"), Some(4));

        // Pattern not found
        assert_eq!(find_pattern(b"PDF table", b"xref"), None);

        // Empty haystack
        assert_eq!(find_pattern(b"", b"xref"), None);

        // Empty needle (edge case)
        assert_eq!(find_pattern(b"test", b""), Some(0));
    }

    #[test]
    fn test_determine_corruption_type_single() {
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 5,
            errors: vec!["Invalid header found".to_string()],
            recoverable_sections: vec![],
            file_stats: FileStats::default(),
        };

        determine_corruption_type(&mut report);
        assert_eq!(report.corruption_type, CorruptionType::InvalidHeader);
    }

    #[test]
    fn test_determine_corruption_type_multiple() {
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 8,
            errors: vec![
                "Invalid header".to_string(),
                "Missing EOF marker".to_string(),
                "Corrupt cross-reference table".to_string(),
            ],
            recoverable_sections: vec![],
            file_stats: FileStats::default(),
        };

        determine_corruption_type(&mut report);
        match report.corruption_type {
            CorruptionType::Multiple(types) => {
                assert_eq!(types.len(), 3);
                assert!(types.contains(&CorruptionType::InvalidHeader));
                assert!(types.contains(&CorruptionType::MissingEOF));
                assert!(types.contains(&CorruptionType::CorruptXRef));
            }
            _ => panic!("Should be Multiple corruption type"),
        }
    }

    #[test]
    fn test_determine_corruption_type_unknown() {
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 3,
            errors: vec!["Some generic error".to_string()],
            recoverable_sections: vec![],
            file_stats: FileStats::default(),
        };

        determine_corruption_type(&mut report);
        assert_eq!(report.corruption_type, CorruptionType::Unknown);
    }

    #[test]
    fn test_check_header_valid() {
        use std::io::Cursor;

        let data = b"%PDF-1.7\nrest of content";
        let mut cursor = Cursor::new(data);
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 0,
            errors: vec![],
            recoverable_sections: vec![],
            file_stats: FileStats::default(),
        };

        let result = check_header(&mut cursor, &mut report).unwrap();
        assert!(result);
        assert_eq!(report.recoverable_sections.len(), 1);
        assert_eq!(report.recoverable_sections[0].confidence, 1.0);
    }

    #[test]
    fn test_check_header_invalid() {
        use std::io::Cursor;

        let data = b"INVALID HEADER\nrest of content";
        let mut cursor = Cursor::new(data);
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 0,
            errors: vec![],
            recoverable_sections: vec![],
            file_stats: FileStats::default(),
        };

        let result = check_header(&mut cursor, &mut report).unwrap();
        assert!(!result);
        assert!(!report.errors.is_empty());
        assert!(report.errors[0].contains("Invalid PDF header"));
    }

    #[test]
    fn test_check_header_too_short() {
        use std::io::Cursor;

        let data = b"PDF"; // Too short
        let mut cursor = Cursor::new(data);
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 0,
            errors: vec![],
            recoverable_sections: vec![],
            file_stats: FileStats::default(),
        };

        let result = check_header(&mut cursor, &mut report).unwrap();
        assert!(!result);
        assert!(!report.errors.is_empty());
    }

    #[test]
    fn test_check_eof_present() {
        use std::io::Cursor;

        let data = b"%PDF-1.7\nsome content\n%%EOF\n";
        let mut cursor = Cursor::new(data);
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 0,
            errors: vec![],
            recoverable_sections: vec![],
            file_stats: FileStats {
                file_size: data.len() as u64,
                ..Default::default()
            },
        };

        check_eof(&mut cursor, &mut report).unwrap();
        // Should add "analysis complete" message
        assert_eq!(report.errors.len(), 1);
        assert!(report.errors[0].contains("analysis complete"));
        assert_eq!(report.severity, 0);
    }

    #[test]
    fn test_check_eof_missing() {
        use std::io::Cursor;

        let data = b"%PDF-1.7\nsome content without eof";
        let mut cursor = Cursor::new(data);
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 0,
            errors: vec![],
            recoverable_sections: vec![],
            file_stats: FileStats {
                file_size: data.len() as u64,
                ..Default::default()
            },
        };

        check_eof(&mut cursor, &mut report).unwrap();
        assert!(!report.errors.is_empty());
        assert!(report.errors[0].contains("Missing %%EOF"));
        assert_eq!(report.severity, 5);
    }

    #[test]
    fn test_scan_xref_found() {
        use std::io::Cursor;

        let data = b"%PDF-1.7\nxref\n0 1\n0000000000 65535 f\ntrailer\n";
        let mut cursor = Cursor::new(data);
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 0,
            errors: vec![],
            recoverable_sections: vec![],
            file_stats: FileStats::default(),
        };

        scan_xref(&mut cursor, &mut report).unwrap();
        assert!(report
            .recoverable_sections
            .iter()
            .any(|s| matches!(s.section_type, SectionType::XRef)));
        assert!(report.errors.is_empty() || !report.errors[0].contains("No cross-reference"));
    }

    #[test]
    fn test_scan_xref_not_found() {
        use std::io::Cursor;

        let data = b"%PDF-1.7\nNo cross reference table here";
        let mut cursor = Cursor::new(data);
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 0,
            errors: vec![],
            recoverable_sections: vec![],
            file_stats: FileStats::default(),
        };

        scan_xref(&mut cursor, &mut report).unwrap();
        assert!(!report.errors.is_empty());
        assert!(report.errors[0].contains("No cross-reference tables found"));
        assert_eq!(report.severity, 8);
    }

    #[test]
    fn test_analyze_objects_with_pages() {
        use std::io::Cursor;

        let data = b"1 0 obj\n<< /Type /Page >>\nendobj\n2 0 obj\n<< /Type /Catalog >>\nendobj";
        let mut cursor = Cursor::new(data);
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 0,
            errors: vec![],
            recoverable_sections: vec![],
            file_stats: FileStats::default(),
        };

        analyze_objects(&mut cursor, &mut report).unwrap();
        assert_eq!(report.file_stats.estimated_objects, 2);
        assert_eq!(report.file_stats.found_pages, 1);
        assert_eq!(report.file_stats.readable_bytes, data.len() as u64);
    }

    #[test]
    fn test_analyze_objects_no_objects() {
        use std::io::Cursor;

        let data = b"No PDF items here";
        let mut cursor = Cursor::new(data);
        let mut report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 0,
            errors: vec![],
            recoverable_sections: vec![],
            file_stats: FileStats::default(),
        };

        analyze_objects(&mut cursor, &mut report).unwrap();
        assert_eq!(report.file_stats.estimated_objects, 0);
        assert!(!report.errors.is_empty());
        assert!(report.errors[0].contains("No PDF objects"));
        assert_eq!(report.severity, 10);
    }

    #[test]
    fn test_is_corrupted_valid_file() {
        use std::fs::File;
        use std::io::Write;

        let temp_dir = std::env::temp_dir();
        let temp_path = temp_dir.join("valid_test.pdf");
        let mut file = File::create(&temp_path).unwrap();
        file.write_all(b"%PDF-1.7\n1 0 obj\n<< >>\nendobj\nxref\n0 1\n0000000000 65535 f\ntrailer\n<< >>\nstartxref\n0\n%%EOF").unwrap();

        let corrupted = is_corrupted(&temp_path);
        // May be false or true depending on analysis
        let _ = corrupted;

        // Cleanup
        let _ = std::fs::remove_file(temp_path);
    }

    #[test]
    fn test_is_corrupted_invalid_file() {
        use std::fs::File;
        use std::io::Write;

        let temp_dir = std::env::temp_dir();
        let temp_path = temp_dir.join("invalid_test.pdf");
        let mut file = File::create(&temp_path).unwrap();
        file.write_all(b"This is not a PDF").unwrap();

        let corrupted = is_corrupted(&temp_path);
        assert!(corrupted);

        // Cleanup
        let _ = std::fs::remove_file(temp_path);
    }

    #[test]
    fn test_is_corrupted_nonexistent_file() {
        let temp_dir = std::env::temp_dir();
        let temp_path = temp_dir.join("nonexistent_test.pdf");

        let corrupted = is_corrupted(&temp_path);
        assert!(corrupted); // Should return true for error case
    }

    #[test]
    fn test_detect_corruption_comprehensive() {
        use std::fs::File;
        use std::io::Write;

        let temp_dir = std::env::temp_dir();
        let temp_path = temp_dir.join("comprehensive_test.pdf");
        let mut file = File::create(&temp_path).unwrap();
        // Missing EOF marker
        file.write_all(b"%PDF-1.7\n1 0 obj\n<< /Type /Page >>\nendobj")
            .unwrap();

        let report = detect_corruption(&temp_path).unwrap();
        assert!(report.severity > 0);
        assert!(!report.errors.is_empty());

        // Cleanup
        let _ = std::fs::remove_file(temp_path);
    }

    #[test]
    fn test_file_stats_debug() {
        let stats = FileStats {
            file_size: 1000,
            readable_bytes: 950,
            estimated_objects: 10,
            found_pages: 3,
        };

        let debug_str = format!("{stats:?}");
        assert!(debug_str.contains("FileStats"));
        assert!(debug_str.contains("1000"));
        assert!(debug_str.contains("950"));
        assert!(debug_str.contains("10"));
        assert!(debug_str.contains("3"));
    }

    #[test]
    fn test_corruption_report_debug() {
        let report = CorruptionReport {
            corruption_type: CorruptionType::Unknown,
            severity: 5,
            errors: vec!["Test error".to_string()],
            recoverable_sections: vec![],
            file_stats: FileStats::default(),
        };

        let debug_str = format!("{report:?}");
        assert!(debug_str.contains("CorruptionReport"));
        assert!(debug_str.contains("Unknown"));
        assert!(debug_str.contains("5"));
    }
}