Skip to main content

pdf_forms/
object_stream.rs

1use crate::parser;
2use crate::{Document, Error, Object, ObjectId, Result, Stream};
3use std::collections::BTreeMap;
4use std::num::TryFromIntError;
5use std::str::FromStr;
6
7use log::warn;
8#[cfg(feature = "rayon")]
9use rayon::prelude::*;
10
11#[derive(Debug)]
12pub struct ObjectStream {
13    pub objects: BTreeMap<ObjectId, Object>,
14    max_objects: usize,
15    compression_level: u32,
16}
17
18#[derive(Debug, Clone)]
19pub struct ObjectStreamBuilder {
20    max_objects: usize,
21    compression_level: u32,
22}
23
24#[derive(Debug, Clone)]
25pub struct ObjectStreamConfig {
26    pub max_objects_per_stream: usize,
27    pub compression_level: u32,
28}
29
30impl Default for ObjectStreamConfig {
31    fn default() -> Self {
32        Self {
33            max_objects_per_stream: 100,
34            compression_level: 6,
35        }
36    }
37}
38
39impl ObjectStream {
40    /// Parse an existing object stream
41    pub fn new(stream: &mut Stream) -> Result<ObjectStream> {
42        let _ = stream.decompress();
43
44        if stream.content.is_empty() {
45            return Ok(ObjectStream {
46                objects: BTreeMap::new(),
47                max_objects: 100,
48                compression_level: 6,
49            });
50        }
51
52        let first_offset = stream
53            .dict
54            .get(b"First")
55            .and_then(Object::as_i64)?
56            .try_into()
57            .map_err(|e: TryFromIntError| Error::NumericCast(e.to_string()))?;
58        let index_block = stream
59            .content
60            .get(..first_offset)
61            .ok_or(Error::InvalidOffset(first_offset))?;
62
63        let numbers_str = std::str::from_utf8(index_block)
64            .map_err(|e| Error::InvalidObjectStream(e.to_string()))?;
65        let numbers: Vec<_> = numbers_str
66            .split_whitespace()
67            .map(|number| u32::from_str(number).ok())
68            .collect();
69        let len = numbers.len() / 2 * 2; // Ensure only pairs.
70
71        let n = stream.dict.get(b"N").and_then(Object::as_i64)?;
72        if numbers.len().try_into().ok() != n.checked_mul(2) {
73            warn!("object stream: the object stream dictionary specifies a wrong number of objects")
74        }
75
76        let chunks_filter_map = |chunk: &[_]| {
77            let id = chunk[0]?;
78            let offset = first_offset + chunk[1]? as usize;
79
80            if offset >= stream.content.len() {
81                warn!("out-of-bounds offset in object stream");
82                return None;
83            }
84            // Skip leading whitespace — some PDFs emit newlines before objects in ObjStm
85            let mut start = offset;
86            while start < stream.content.len() && stream.content[start].is_ascii_whitespace() {
87                start += 1;
88            }
89            if start >= stream.content.len() {
90                warn!("only whitespace after offset in object stream");
91                return None;
92            }
93            let object = parser::direct_object(&stream.content[start..])?;
94
95            Some(((id, 0), object))
96        };
97        #[cfg(feature = "rayon")]
98        let objects = numbers[..len]
99            .par_chunks(2)
100            .filter_map(chunks_filter_map)
101            .collect();
102        #[cfg(not(feature = "rayon"))]
103        let objects = numbers[..len]
104            .chunks(2)
105            .filter_map(chunks_filter_map)
106            .collect();
107
108        Ok(ObjectStream {
109            objects,
110            max_objects: 100,
111            compression_level: 6,
112        })
113    }
114
115    /// Create a builder for constructing new object streams
116    pub fn builder() -> ObjectStreamBuilder {
117        ObjectStreamBuilder {
118            max_objects: 100,
119            compression_level: 6,
120        }
121    }
122
123    /// Add an object to the stream
124    pub fn add_object(&mut self, id: ObjectId, obj: Object) -> Result<()> {
125        // Check if object can be added to stream
126        if matches!(obj, Object::Stream(_)) {
127            return Err(Error::InvalidObjectStream(
128                "Stream objects cannot be stored in object streams".into(),
129            ));
130        }
131
132        // Check capacity
133        if self.objects.len() >= self.max_objects {
134            return Err(Error::InvalidObjectStream(format!(
135                "Object stream has reached maximum capacity of {} objects",
136                self.max_objects
137            )));
138        }
139
140        self.objects.insert(id, obj);
141        Ok(())
142    }
143
144    /// Get the number of objects in the stream
145    pub fn object_count(&self) -> usize {
146        self.objects.len()
147    }
148
149    /// Build the stream content in the format required by PDF spec
150    pub fn build_stream_content(&self) -> Result<Vec<u8>> {
151        if self.objects.is_empty() {
152            return Ok(Vec::new());
153        }
154
155        // Sort objects by ID for consistent output
156        let mut sorted_objects: Vec<_> = self.objects.iter().collect();
157        sorted_objects.sort_by_key(|(id, _)| *id);
158
159        // First build the offset table to know its size
160        let mut offset_entries = Vec::new();
161        let mut current_offset = 0;
162
163        for ((obj_num, _gen), obj) in &sorted_objects {
164            // Store the object number and its offset
165            offset_entries.push(format!("{obj_num} {current_offset}"));
166
167            // Calculate size of this object's serialization
168            let mut obj_bytes = Vec::new();
169            crate::writer::Writer::write_object(&mut obj_bytes, obj)?;
170            current_offset += obj_bytes.len() + 1; // +1 for space separator
171        }
172
173        // Build the complete offset table with proper spacing
174        let offset_table = offset_entries.join(" ") + " ";
175
176        // Now build the final content
177        let mut content = Vec::new();
178        content.extend_from_slice(offset_table.as_bytes());
179
180        // Add serialized objects with space separators
181        for ((_, _), obj) in &sorted_objects {
182            let mut obj_bytes = Vec::new();
183            crate::writer::Writer::write_object(&mut obj_bytes, obj)?;
184            content.extend_from_slice(&obj_bytes);
185            content.push(b' '); // Space separator between objects
186        }
187
188        Ok(content)
189    }
190
191    /// Convert to a Stream object ready for insertion into a PDF
192    pub fn to_stream_object(&self) -> Result<Stream> {
193        let content = self.build_stream_content()?;
194
195        // Calculate where the first object starts
196        // We need to find the size of the offset table
197        let mut sorted_objects: Vec<_> = self.objects.iter().collect();
198        sorted_objects.sort_by_key(|(id, _)| *id);
199
200        // Build the offset entries to calculate exact size
201        let mut offset_entries = Vec::new();
202        let mut current_offset = 0;
203
204        for ((obj_num, _gen), obj) in &sorted_objects {
205            offset_entries.push(format!("{obj_num} {current_offset}"));
206
207            // Calculate size of this object's serialization
208            let mut obj_bytes = Vec::new();
209            crate::writer::Writer::write_object(&mut obj_bytes, obj)?;
210            current_offset += obj_bytes.len() + 1; // +1 for space separator
211        }
212
213        // The offset table is joined with spaces and has a trailing space
214        let offset_table = offset_entries.join(" ") + " ";
215        let first_offset = offset_table.len();
216
217        let dict = dictionary! {
218            "Type" => "ObjStm",
219            "N" => self.objects.len() as i64,
220            "First" => first_offset as i64,
221        };
222
223        let mut stream = Stream::new(dict, content);
224
225        // Apply compression - object streams should always be compressed
226        if self.compression_level > 0 {
227            // Force compression by setting Filter directly
228            use flate2::Compression;
229            use flate2::write::ZlibEncoder;
230            use std::io::prelude::*;
231
232            let compression = match self.compression_level {
233                0 => Compression::none(),
234                1..=3 => Compression::fast(),
235                4..=6 => Compression::default(),
236                _ => Compression::best(),
237            };
238
239            let mut encoder = ZlibEncoder::new(Vec::new(), compression);
240            encoder.write_all(&stream.content)?;
241            let compressed = encoder.finish()?;
242
243            stream.dict.set("Filter", "FlateDecode");
244            stream.set_content(compressed);
245        }
246
247        Ok(stream)
248    }
249
250    /// Check if an object can be compressed into an object stream
251    pub fn can_be_compressed(id: ObjectId, obj: &Object, doc: &Document) -> bool {
252        // Rule 1: Stream objects cannot be compressed
253        if matches!(obj, Object::Stream(_)) {
254            return false;
255        }
256
257        // Rule 2: Objects with non-zero generation cannot be compressed
258        if id.1 != 0 {
259            return false;
260        }
261
262        // Rule 3: Only encryption dictionary cannot be compressed from trailer references
263        if let Ok(Object::Reference(encrypt_ref)) = doc.trailer.get(b"Encrypt") {
264            if id == *encrypt_ref {
265                return false;
266            }
267        }
268
269        // Rule 4: Specific object types that cannot be compressed
270        if let Object::Dictionary(dict) = obj {
271            if let Ok(type_obj) = dict.get(b"Type") {
272                if let Ok(type_name) = type_obj.as_name() {
273                    match type_name {
274                        // Cross-reference streams and object streams cannot be compressed
275                        b"XRef" => return false,
276                        b"ObjStm" => return false,
277
278                        // Catalog can only be excluded in linearized PDFs
279                        b"Catalog" if Self::is_linearized(doc) => {
280                            return false;
281                        }
282                        b"Catalog" => {}
283
284                        // Page, Pages, and all other types CAN be compressed
285                        _ => {}
286                    }
287                }
288            }
289        }
290
291        // Default: Allow compression
292        true
293    }
294
295    /// Check if a PDF document is linearized
296    fn is_linearized(doc: &Document) -> bool {
297        // In a linearized PDF, the first object after the header should be a
298        // linearization dictionary with /Linearized entry
299        // For simplicity, we check if any object has a /Linearized entry
300        for obj in doc.objects.values() {
301            if let Object::Dictionary(dict) = obj {
302                if dict.has(b"Linearized") {
303                    return true;
304                }
305            }
306        }
307        false
308    }
309}
310
311impl ObjectStreamBuilder {
312    /// Set the maximum number of objects per stream
313    pub fn max_objects(mut self, max: usize) -> Self {
314        self.max_objects = max;
315        self
316    }
317
318    /// Set the compression level (0-9)
319    pub fn compression_level(mut self, level: u32) -> Self {
320        self.compression_level = level;
321        self
322    }
323
324    /// Build the ObjectStream
325    pub fn build(self) -> ObjectStream {
326        ObjectStream {
327            objects: BTreeMap::new(),
328            max_objects: self.max_objects,
329            compression_level: self.compression_level,
330        }
331    }
332
333    /// Get the current max_objects setting
334    pub fn get_max_objects(&self) -> usize {
335        self.max_objects
336    }
337
338    /// Get the current compression_level setting
339    pub fn get_compression_level(&self) -> u32 {
340        self.compression_level
341    }
342}