Skip to content

Commit

Permalink
fix #122
Browse files Browse the repository at this point in the history
  • Loading branch information
s3bk committed Jan 2, 2022
1 parent 99e70cd commit cad73c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
17 changes: 11 additions & 6 deletions pdf/src/parser/parse_xref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ use crate::primitive::{Primitive, Dictionary};
use crate::object::*;
use crate::parser::{parse_with_lexer, ParseFlags};
use crate::parser::parse_object::{parse_indirect_stream};
use std::convert::TryInto;

// Just the part of Parser which reads xref sections from xref stream.
/// Takes `&mut &[u8]` so that it can "consume" data as it reads
fn parse_xref_section_from_stream(first_id: i32, num_entries: i32, width: &[i32], data: &mut &[u8]) -> Result<XRefSection> {
fn parse_xref_section_from_stream(first_id: u32, num_entries: u32, width: &[usize], data: &mut &[u8]) -> Result<XRefSection> {
let mut entries = Vec::new();
let [w0, w1, w2]: [usize; 3] = width.try_into().map_err(|e| other!("invalid xref length array"))?;
if num_entries as usize * (w0 + w1 + w2) > data.len() {
bail!("not enough xref data");
}
for _ in 0..num_entries {
// println!("{:?}", &data[.. width.iter().map(|&i| i as usize).sum()]);
// TODO Check if width[i] are 0. Use default values from the PDF references.
let _type = read_u64_from_stream(width[0], data);
let field1 = read_u64_from_stream(width[1], data);
let field2 = read_u64_from_stream(width[2], data);
let _type = read_u64_from_stream(w0, data);
let field1 = read_u64_from_stream(w1, data);
let field2 = read_u64_from_stream(w2, data);

let entry =
match _type {
Expand All @@ -27,12 +32,12 @@ fn parse_xref_section_from_stream(first_id: i32, num_entries: i32, width: &[i32]
entries.push(entry);
}
Ok(XRefSection {
first_id: first_id as u32,
first_id,
entries,
})
}
/// Helper to read an integer with a certain amount of bits `width` from stream.
fn read_u64_from_stream(width: i32, data: &mut &[u8]) -> u64 {
fn read_u64_from_stream(width: usize, data: &mut &[u8]) -> u64 {
let mut result = 0;
for i in (0..width).rev() {
let base = 8 * i; // (width, 0]
Expand Down
12 changes: 6 additions & 6 deletions pdf/src/xref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ impl XRefTable {
data.extend_from_slice(&b.to_be_bytes()[8 - b_w ..]);
}
let info = XRefInfo {
size: size as i32,
index: vec![0, size as i32],
size: size as u32,
index: vec![0, size as u32],
prev: None,
w: vec![1, a_w as i32, b_w as i32],
w: vec![1, a_w, b_w],
};
Ok(Stream::new(info, data).hexencode())
}
Expand Down Expand Up @@ -210,19 +210,19 @@ impl XRefSection {
pub struct XRefInfo {
// XRefStream fields
#[pdf(key = "Size")]
pub size: i32,
pub size: u32,

//
#[pdf(key = "Index", default = "vec![0, size]")]
/// Array of pairs of integers for each subsection, (first object number, number of entries).
/// Default value (assumed when None): `(0, self.size)`.
pub index: Vec<i32>,
pub index: Vec<u32>,

#[pdf(key = "Prev")]
prev: Option<i32>,

#[pdf(key = "W")]
pub w: Vec<i32>,
pub w: Vec<usize>,
}

// read_xref_table
Expand Down

0 comments on commit cad73c4

Please sign in to comment.