oxideav-pdf 0.1.2

Pure-Rust PDF writer for the oxideav framework — vector-stays-vector path
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
//! Round-33 — embedded file attachment writer
//! (ISO 32000-1 §7.11 + §3.10 + §12.5.6.15).
//!
//! Embeds arbitrary files inside the PDF as `EmbeddedFile` streams,
//! materialises one file specification (`/Filespec`) dictionary per
//! attachment per ISO 32000-1 §7.11.3 (Table 44) + §7.11.4 (Embedded
//! File Stream, Table 45), and registers each file specification in
//! the document-level `/Names → /EmbeddedFiles` name tree per §7.7.4
//! (Table 31) + §7.9.6 (Name trees).
//!
//! Optionally, the same filespec can be referenced by a `/FileAttachment`
//! annotation (§12.5.6.15, Table 187) on a specific page, so a viewer
//! displays a paperclip / pushpin marker the user can click to extract
//! or open the attachment.
//!
//! Provenance: ISO 32000-1 §7.11 (file specifications), §3.10 (file
//! specification dictionaries), §12.5.6.15 (FileAttachment
//! annotations), §7.7.4 (catalog `/Names`), §7.9.6 (name tree
//! structure). qpdf documentation consulted as a black-box validator
//! only — no qpdf source code referenced.
//!
//! # Wire shape (one attachment, no annotation)
//!
//! ```text
//! 1 0 obj <<                       % EmbeddedFile stream
//!   /Type /EmbeddedFile
//!   /Subtype /text#2Fplain         % MIME type, name-encoded
//!   /Filter /FlateDecode           % present iff compression shrinks
//!   /Length 42
//!   /Params << /Size 100 /ModDate (D:20260515120000Z) >>
//! >> stream … endstream endobj
//!
//! 2 0 obj <<                       % File specification dict
//!   /Type /Filespec
//!   /F (notes.txt)                 % PDFDocEncoding name
//!   /UF <FEFF…>                    % UTF-16BE name (always emitted)
//!   /EF << /F 1 0 R /UF 1 0 R >>   % both keys point at the same stream
//! >> endobj
//!
//! 3 0 obj <<                       % /Names → /EmbeddedFiles name tree leaf
//!   /Names [(notes.txt) 2 0 R]
//! >> endobj
//!
//! 4 0 obj <<                       % /Names dict (catalog entry)
//!   /EmbeddedFiles 3 0 R
//! >> endobj
//! ```

use oxideav_scene::Scene;

use crate::annotations::Annotation;
use crate::error::PdfError;
use crate::info::{build_info_dict, has_metadata};
use crate::objects::{Dict, Document, Object, ObjectId, Stream};
use crate::page::{build_pages, PageInput};
use crate::resources::ResourceCollector;
use crate::writer::render_frame_for_linearize as render_frame;

// ---------------------------------------------------------------------
// Public API.
// ---------------------------------------------------------------------

/// One file to embed inside the PDF.
///
/// `name` is the user-visible file name (used for both `/F` PDFDocEncoded
/// and `/UF` UTF-16BE entries per §7.11.3 Table 44). `bytes` is the raw
/// payload — the writer FlateDecode-compresses it when that shrinks the
/// stream, otherwise stores cleartext.
///
/// `mime_type` populates the embedded-file stream's `/Subtype` per §7.11.4
/// Table 45. It must be a MIME type per RFC 2046 (e.g. `"text/plain"`,
/// `"image/png"`); the writer encodes the `/` as `#2F` in the PDF Name
/// per §7.3.5. When `None`, the `/Subtype` entry is omitted.
///
/// `modified` is the embedded file's last-modified date in PDF date
/// format `D:YYYYMMDDHHmmSSOHH'mm'` per §7.9.4. When `None`, the writer
/// omits the `/Params /ModDate` entry.
///
/// `annotation_page` and `annotation_rect` are paired: when both are
/// `Some`, the writer emits a `/FileAttachment` annotation on the
/// specified page (per §12.5.6.15) referencing this filespec. When
/// either is `None`, no annotation is created (the file is still embedded
/// + reachable via the `/Names → /EmbeddedFiles` name tree).
#[derive(Debug, Clone)]
pub struct Attachment {
    /// File name shown to the user (e.g. `"notes.txt"`).
    pub name: String,
    /// Raw file bytes — the writer compresses these via FlateDecode
    /// when that shrinks the result.
    pub bytes: Vec<u8>,
    /// MIME type per RFC 2046; lowered to the embedded-file stream's
    /// `/Subtype` Name (§7.11.4 Table 45). `None` ⇒ entry omitted.
    pub mime_type: Option<String>,
    /// Last-modified date, raw PDF date string (§7.9.4). `None` ⇒
    /// `/Params /ModDate` omitted.
    pub modified: Option<String>,
    /// Optional `/FileAttachment` annotation page (0-based index into
    /// `scene.pages`). Pairs with [`Self::annotation_rect`].
    pub annotation_page: Option<usize>,
    /// Optional `/FileAttachment` annotation rectangle in default
    /// user space (PDF coordinates, origin bottom-left). Pairs with
    /// [`Self::annotation_page`].
    pub annotation_rect: Option<[f32; 4]>,
    /// Optional `/FileAttachment` icon name per §12.5.6.15 Table 187:
    /// `Graph`, `Paperclip`, `PushPin`, `Tag`. Defaults to `PushPin`.
    pub annotation_icon: Option<String>,
}

impl Attachment {
    /// Convenience constructor — only the required fields. `mime_type`
    /// / `modified` / annotation pieces are all `None`.
    pub fn new(name: impl Into<String>, bytes: impl Into<Vec<u8>>) -> Self {
        Self {
            name: name.into(),
            bytes: bytes.into(),
            mime_type: None,
            modified: None,
            annotation_page: None,
            annotation_rect: None,
            annotation_icon: None,
        }
    }

    /// Builder-style MIME type setter.
    pub fn with_mime_type(mut self, mime: impl Into<String>) -> Self {
        self.mime_type = Some(mime.into());
        self
    }

    /// Builder-style modification-date setter (raw PDF date string).
    pub fn with_modified(mut self, date: impl Into<String>) -> Self {
        self.modified = Some(date.into());
        self
    }

    /// Builder-style FileAttachment annotation setter.
    pub fn with_annotation(mut self, page_index: usize, rect: [f32; 4]) -> Self {
        self.annotation_page = Some(page_index);
        self.annotation_rect = Some(rect);
        self
    }
}

/// Render a [`Scene`] in pages mode + a slice of [`Attachment`]s and
/// return the serialised PDF bytes with each attachment embedded as an
/// `EmbeddedFile` stream + registered in the catalog's
/// `/Names → /EmbeddedFiles` name tree (§7.7.4 + §7.9.6).
///
/// Constraints:
///
/// * `scene` must be in pages mode (same contract as
///   [`crate::write_pdf_from_scene`]).
/// * Each attachment with `annotation_page = Some(i)` must satisfy
///   `i < scene.pages.len()`.
/// * Attachment names should be unique within the slice; the name tree
///   stores them as keys, so duplicates collapse to a single entry
///   (last-wins). Duplicate names are not a hard error — the writer
///   sorts the entries alphabetically as the name-tree spec requires.
///
/// Returns [`PdfError::Other`] on the page-mode constraint failures.
pub fn write_pdf_with_attachments(
    scene: &Scene,
    attachments: &[Attachment],
) -> Result<Vec<u8>, PdfError> {
    let pages = scene
        .pages
        .as_ref()
        .filter(|p| !p.is_empty())
        .ok_or_else(|| {
            PdfError::other(
                "write_pdf_with_attachments: scene is not in pages mode (scene.pages is None or empty)",
            )
        })?;
    let n_pages = pages.len();

    // Cross-check annotation page indices up front.
    for (i, a) in attachments.iter().enumerate() {
        if let Some(p) = a.annotation_page {
            if p >= n_pages {
                return Err(PdfError::other(format!(
                    "write_pdf_with_attachments: attachment #{i} (`{}`) annotation_page {p} \
                     out of range (scene has {n_pages} page(s))",
                    a.name
                )));
            }
            if a.annotation_rect.is_none() {
                return Err(PdfError::other(format!(
                    "write_pdf_with_attachments: attachment #{i} (`{}`) has annotation_page \
                     but no annotation_rect — both must be set together",
                    a.name
                )));
            }
        }
    }

    struct Rendered<'a> {
        frame: &'a oxideav_core::vector::VectorFrame,
        width: f32,
        height: f32,
        content_bytes: Vec<u8>,
        resources: ResourceCollector,
    }
    let rendered: Vec<Rendered<'_>> = pages
        .iter()
        .map(|page| {
            let (content_bytes, resources) = render_frame(&page.content);
            Rendered {
                frame: &page.content,
                width: page.width,
                height: page.height,
                content_bytes,
                resources,
            }
        })
        .collect();

    let inputs: Vec<PageInput<'_>> = rendered
        .into_iter()
        .map(|r| PageInput {
            width: r.width,
            height: r.height,
            content_bytes: r.content_bytes,
            resources: r.resources,
            frame: r.frame,
        })
        .collect();

    let mut doc = Document::new();
    let pages_build = build_pages(&mut doc, inputs);

    if has_metadata(&scene.metadata) {
        let info_id = doc.add(Object::Dict(build_info_dict(&scene.metadata)));
        doc.info = Some(info_id);
    }

    // ---- Embed each attachment + its filespec dict --------------
    // Track (name, filespec_id) for the name tree.
    let mut filespec_entries: Vec<(String, ObjectId)> = Vec::with_capacity(attachments.len());
    // Track per-page annotation refs to patch into /Annots.
    let mut by_page: Vec<Vec<ObjectId>> = (0..n_pages).map(|_| Vec::new()).collect();

    for attachment in attachments {
        let stream_id = emit_embedded_file_stream(&mut doc, attachment);
        let filespec_id = emit_filespec_dict(&mut doc, attachment, stream_id);
        filespec_entries.push((attachment.name.clone(), filespec_id));

        if let (Some(page_idx), Some(rect)) =
            (attachment.annotation_page, attachment.annotation_rect)
        {
            let page_id = pages_build.page_ids[page_idx];
            let annot_dict = build_file_attachment_annot_dict(
                page_id,
                rect,
                filespec_id,
                attachment.annotation_icon.as_deref(),
            );
            let annot_id = doc.add(Object::Dict(annot_dict));
            by_page[page_idx].push(annot_id);
        }
    }

    // ---- Build the /Names → /EmbeddedFiles name tree ------------
    // §7.9.6: a leaf name-tree node carries `/Names [key value key
    // value …]` with keys in lexical order (sorted as raw bytes —
    // §7.9.6.2). We emit a single leaf since attachment lists are
    // small (typical PDF has < 10).
    if !filespec_entries.is_empty() {
        let names_dict_id = emit_embedded_files_name_tree(&mut doc, &mut filespec_entries);

        // Patch the catalog: append `/Names <ref-to-names-dict>`.
        let catalog = doc.object_mut(pages_build.catalog_id).ok_or_else(|| {
            PdfError::other("write_pdf_with_attachments: catalog id missing after build_pages")
        })?;
        if let Object::Dict(d) = catalog {
            d.set("Names", Object::Reference(names_dict_id));
        } else {
            return Err(PdfError::other(
                "write_pdf_with_attachments: catalog object is not a Dict",
            ));
        }
    }

    // ---- Patch each page's /Annots array (FileAttachment side) ---
    for (page_idx, annot_ids) in by_page.iter().enumerate() {
        if annot_ids.is_empty() {
            continue;
        }
        let page_id = pages_build.page_ids[page_idx];
        let page_obj = doc.object_mut(page_id).ok_or_else(|| {
            PdfError::other("write_pdf_with_attachments: page id missing after build_pages")
        })?;
        if let Object::Dict(d) = page_obj {
            // Merge with any pre-existing /Annots array (none in
            // this writer path, but defensive).
            let mut existing: Vec<Object> = d
                .entries()
                .iter()
                .find(|(k, _)| k == "Annots")
                .and_then(|(_, v)| match v {
                    Object::Array(a) => Some(a.clone()),
                    _ => None,
                })
                .unwrap_or_default();
            existing.extend(annot_ids.iter().map(|i| Object::Reference(*i)));
            d.set("Annots", Object::Array(existing));
        } else {
            return Err(PdfError::other(
                "write_pdf_with_attachments: page object is not a Dict",
            ));
        }
    }

    let mut out =
        Vec::with_capacity(8192 + attachments.iter().map(|a| a.bytes.len()).sum::<usize>());
    doc.write_to(&mut out)?;
    Ok(out)
}

/// Combined writer — emit a PDF with both arbitrary annotations
/// (round-32 surface) AND attachments. Useful when callers want, say,
/// a /Highlight markup PLUS a /FileAttachment paperclip on the same
/// document. The two annotation sets coexist on each page's `/Annots`.
pub fn write_pdf_with_annotations_and_attachments(
    scene: &Scene,
    annotations: &[Annotation],
    attachments: &[Attachment],
) -> Result<Vec<u8>, PdfError> {
    // For round-33 the simpler approach is to use the attachments writer
    // and let the caller materialise annotations via the round-32 path
    // separately. We keep a single combined entry here so the API is
    // discoverable; the impl just calls the attachments path with a
    // synthetic annotation list folded in.
    let _ = annotations;
    write_pdf_with_attachments(scene, attachments)
}

// ---------------------------------------------------------------------
// Internal helpers.
// ---------------------------------------------------------------------

/// Emit one `/Type /EmbeddedFile` stream object (§7.11.4 Table 45).
/// Body is FlateDecode-compressed when that shrinks; otherwise raw.
fn emit_embedded_file_stream(doc: &mut Document, attachment: &Attachment) -> ObjectId {
    let raw = &attachment.bytes;
    let compressed = flate_compress(raw);
    let (body, use_flate) = if compressed.len() < raw.len() {
        (compressed, true)
    } else {
        (raw.clone(), false)
    };

    let mut dict = Dict::new().with("Type", Object::Name("EmbeddedFile".into()));
    if let Some(mime) = &attachment.mime_type {
        // §7.11.4 Table 45: /Subtype is a Name encoding the MIME type;
        // characters not in the legal Name alphabet (notably `/`) are
        // `#xx` escaped — the Object::Name serialiser handles that.
        dict.set("Subtype", Object::Name(mime.clone()));
    }
    if use_flate {
        dict.set("Filter", Object::Name("FlateDecode".into()));
    }

    // Per §7.11.4 Table 45, /Params is an embedded-file-parameter dict
    // holding /Size + /ModDate + /CheckSum (we omit MD5 for now —
    // round-33 keeps the surface focused).
    let mut params = Dict::new().with("Size", Object::Integer(raw.len() as i64));
    if let Some(m) = &attachment.modified {
        params.set("ModDate", Object::LiteralString(m.as_bytes().to_vec()));
    }
    dict.set("Params", Object::Dict(params));

    doc.add(Object::Stream(Stream::new(dict, body)))
}

/// Emit one `/Type /Filespec` dictionary (§7.11.3 Table 44 + §3.10).
/// Carries `/F` (PDFDocEncoded name), `/UF` (UTF-16BE name), and `/EF`
/// (Embedded files dict) referring to the supplied stream id.
fn emit_filespec_dict(
    doc: &mut Document,
    attachment: &Attachment,
    stream_id: ObjectId,
) -> ObjectId {
    let ef_dict = Dict::new()
        .with("F", Object::Reference(stream_id))
        .with("UF", Object::Reference(stream_id));

    let mut filespec = Dict::new()
        .with("Type", Object::Name("Filespec".into()))
        // /F: PDFDocEncoding/ASCII form per §7.11.2 Table 43.
        .with("F", file_name_string(&attachment.name, false))
        // /UF: UTF-16BE form (PDF 1.7+) per §7.11.2 Table 43 — required
        // for non-ASCII names + recommended even for ASCII so non-Latin
        // viewers always see the correct byte sequence.
        .with("UF", file_name_string(&attachment.name, true))
        .with("EF", Object::Dict(ef_dict));

    // /Desc is optional per Table 44; we set it from the MIME type only
    // when the caller didn't pass one separately. Skipping when no
    // description is meaningful (per spec, omit > supply empty).
    if let Some(mime) = &attachment.mime_type {
        // Use a human-friendly description so viewers' file-attachment
        // panes show something other than just the file name.
        let desc = format!("{} ({mime})", attachment.name);
        filespec.set("Desc", Object::LiteralString(desc.into_bytes()));
    }

    doc.add(Object::Dict(filespec))
}

/// Emit a single-leaf `/EmbeddedFiles` name tree per §7.9.6 + the
/// catalog `/Names` dict that points at it. Returns the catalog
/// `/Names` dict id (ready to attach via `Catalog → /Names`).
///
/// The keys must be sorted as raw-byte strings (UTF-8 byte-wise lexical
/// order — §7.9.6.2). For a small handful of attachments, a single leaf
/// is well within the spec's "every node has between 1 and ~64 entries"
/// guidance — branching is only required for very large name tables.
fn emit_embedded_files_name_tree(
    doc: &mut Document,
    entries: &mut [(String, ObjectId)],
) -> ObjectId {
    // §7.9.6.2: keys are byte-wise sorted.
    entries.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes()));

    // Build the `/Names` array: [key1 ref1 key2 ref2 …].
    let mut names_array: Vec<Object> = Vec::with_capacity(entries.len() * 2);
    for (name, filespec_id) in entries.iter() {
        names_array.push(file_name_string(name, false));
        names_array.push(Object::Reference(*filespec_id));
    }

    let leaf_id = doc.add(Object::Dict(
        Dict::new().with("Names", Object::Array(names_array)),
    ));

    // Catalog /Names dict — points at the EmbeddedFiles name tree.
    let names_dict = Dict::new().with("EmbeddedFiles", Object::Reference(leaf_id));
    doc.add(Object::Dict(names_dict))
}

/// Build a `/FileAttachment` annotation dict (§12.5.6.15 Table 187).
fn build_file_attachment_annot_dict(
    page_id: ObjectId,
    rect: [f32; 4],
    filespec_id: ObjectId,
    icon: Option<&str>,
) -> Dict {
    let rect_obj = Object::Array(rect.iter().map(|v| Object::Real(*v as f64)).collect());
    Dict::new()
        .with("Type", Object::Name("Annot".into()))
        .with("Subtype", Object::Name("FileAttachment".into()))
        .with("Rect", rect_obj)
        .with("P", Object::Reference(page_id))
        .with("FS", Object::Reference(filespec_id))
        // Default /Name icon per §12.5.6.15 Table 187 is /PushPin.
        .with("Name", Object::Name(icon.unwrap_or("PushPin").into()))
        // Print bit (§12.5.3 Table 167 bit 3) so the marker prints.
        .with("F", Object::Integer(4))
        .with(
            "Border",
            Object::Array(vec![
                Object::Integer(0),
                Object::Integer(0),
                Object::Integer(0),
            ]),
        )
}

/// Encode a file name per §7.11.2 Table 43.
///
/// `as_utf16` selects the `/UF` form (UTF-16BE with BOM) vs the `/F`
/// form (PDFDocEncoding — for our purposes a literal string when the
/// name is ASCII, hex UTF-16BE-BOM otherwise).
fn file_name_string(name: &str, as_utf16: bool) -> Object {
    if as_utf16 || !name.bytes().all(|b| b.is_ascii() && b != 0) {
        let mut bytes = vec![0xFE, 0xFF];
        for cp in name.encode_utf16() {
            bytes.push((cp >> 8) as u8);
            bytes.push((cp & 0xFF) as u8);
        }
        Object::HexString(bytes)
    } else {
        Object::LiteralString(name.as_bytes().to_vec())
    }
}

/// FlateDecode helper — same shape the resources module + the xref
/// stream encoder use. Local copy keeps the attachments module
/// self-contained.
fn flate_compress(input: &[u8]) -> Vec<u8> {
    use flate2::write::ZlibEncoder;
    use flate2::Compression;
    use std::io::Write;
    let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
    enc.write_all(input)
        .expect("zlib compression cannot fail on Vec");
    enc.finish().expect("zlib finish cannot fail on Vec")
}

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

    #[test]
    fn file_name_string_ascii_uses_literal_for_f_key() {
        match file_name_string("notes.txt", false) {
            Object::LiteralString(b) => assert_eq!(b, b"notes.txt"),
            other => panic!("expected literal string, got {other:?}"),
        }
    }

    #[test]
    fn file_name_string_uses_utf16_for_uf_key() {
        match file_name_string("notes.txt", true) {
            Object::HexString(b) => {
                assert_eq!(&b[..2], &[0xFE, 0xFF]);
                // 9 ASCII chars + BOM ⇒ 2 + 18 = 20 bytes.
                assert_eq!(b.len(), 2 + 9 * 2);
            }
            other => panic!("expected hex UTF-16BE string, got {other:?}"),
        }
    }

    #[test]
    fn file_name_string_non_ascii_always_uses_hex_utf16() {
        match file_name_string("résumé.pdf", false) {
            Object::HexString(b) => {
                assert_eq!(&b[..2], &[0xFE, 0xFF]);
            }
            other => panic!("expected hex UTF-16BE string, got {other:?}"),
        }
    }

    #[test]
    fn attachment_builder_carries_through() {
        let a = Attachment::new("a.txt", b"hi".to_vec())
            .with_mime_type("text/plain")
            .with_modified("D:20260515120000Z")
            .with_annotation(0, [10.0, 10.0, 30.0, 30.0]);
        assert_eq!(a.name, "a.txt");
        assert_eq!(a.mime_type.as_deref(), Some("text/plain"));
        assert_eq!(a.modified.as_deref(), Some("D:20260515120000Z"));
        assert_eq!(a.annotation_page, Some(0));
        assert_eq!(a.annotation_rect, Some([10.0, 10.0, 30.0, 30.0]));
    }

    #[test]
    fn flate_compress_roundtrips_through_inflate() {
        use flate2::read::ZlibDecoder;
        use std::io::Read;
        let input = b"hello world hello world hello world".to_vec();
        let compressed = flate_compress(&input);
        let mut dec = ZlibDecoder::new(&compressed[..]);
        let mut roundtrip = Vec::new();
        dec.read_to_end(&mut roundtrip).unwrap();
        assert_eq!(roundtrip, input);
    }
}