caj2pdf-core 0.7.0

Platform-neutral CAJ-family conversion core
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
// SPDX-License-Identifier: MIT

//! Original synthetic C8 sources with generated application-info tails.
//! The XML text is invented; no document values or bytes are used.

use super::*;
use crate::hnc8::Hnc8Reader;
use crate::test_support::{CancelAfter, NEVER};
use crate::test_support::{field_of, kind_name};
use crate::{Limits, native::SeekableSource};
use flate2::{Compression, write::ZlibEncoder};
use std::io::{Cursor, Write};

const VALID: &str = "\u{feff}<?xml version='1.0' encoding='utf-8' ?> \n\
<!-- invented -->\n<Package>\n<Note-Package>\n\
<NoteItems Author=\"a > b\" ReadOnly='0'>\n\
<Item Type=\"1\"><RC l=\"1\" t=\"2\"/><Item Type=\"2\"/></Item>\n\
<Item Type=\"1\"><RC l=\"1\"/><Item Type=\"2\"/></Item>\n\
</NoteItems>\n<NoteItems><Item/></NoteItems>\n</Note-Package >\n\
<FileProperty-Package>\n<DOI> X&amp;Y&#x41;&#66;&lt;&gt;&quot;&apos; </DOI>\
<SCODE><![CDATA[S]]></SCODE><PCODE>P</PCODE>\n<DURL><![CDATA[ http://example.invalid/a?b&c ]]></DURL>\n\
</FileProperty-Package>\n<Item/><DOI>ignored path</DOI>\n</Package>\n<?trailing pi?>\n";

/// One-page C8 header and index, then a short invented body.
fn c8() -> Vec<u8> {
    let mut bytes = vec![0; 0x50 + 20];
    bytes[0] = 0xc8;
    bytes[8] = 1;
    bytes.extend(b"invented page body");
    bytes
}

fn deflate(xml: &[u8]) -> Vec<u8> {
    let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
    encoder.write_all(xml).unwrap();
    encoder.finish().unwrap()
}

/// Append `[decoded][compressed][stream]APPINFOSIGN <start>` to `bytes`.
fn framed(mut bytes: Vec<u8>, decoded: u32, stream: &[u8]) -> Vec<u8> {
    let start = bytes.len();
    bytes.extend(decoded.to_le_bytes());
    bytes.extend((stream.len() as u32).to_le_bytes());
    bytes.extend(stream);
    bytes.extend(format!("APPINFOSIGN {start}").as_bytes());
    bytes
}

fn package(xml: &str) -> Vec<u8> {
    framed(c8(), xml.len() as u32, &deflate(xml.as_bytes()))
}

fn read_with(
    bytes: Vec<u8>,
    limits: &Limits,
    cancellation: &CancelAfter,
) -> Result<Option<ApplicationInfo>> {
    (|| {
        let mut source = SeekableSource::new(Cursor::new(bytes)).unwrap();
        let mut reader = Hnc8Reader::open(&mut source, limits, cancellation)?;
        reader.application_info()
    })()
}

fn read(bytes: Vec<u8>) -> Result<Option<ApplicationInfo>> {
    read_with(bytes, &Limits::default(), &NEVER)
}

fn defect(result: Result<Option<ApplicationInfo>>) -> ApplicationInfoDefect {
    ApplicationInfoDefect::from(&result.unwrap_err())
}

fn xml_error(xml: &str) -> &'static str {
    let found = defect(read(package(xml)));
    assert_eq!(found.field, "application-info XML", "{xml:?}");
    found.reason
}

#[test]
fn valid_package_yields_selected_fields_only() {
    let info = read(package(VALID)).unwrap().unwrap();
    assert_eq!(info.doi.as_deref(), Some("X&YAB<>\"'"));
    assert_eq!(info.url.as_deref(), Some("http://example.invalid/a?b&c"));
    assert_eq!(info.note_count, 3);
    // Small I/O chunks split both the trailer and the stream reads.
    let limits = Limits {
        io_chunk_bytes: 7,
        ..Limits::default()
    };
    assert_eq!(
        read_with(package(VALID), &limits, &NEVER).unwrap(),
        Some(info)
    );
}

#[test]
fn empty_and_missing_values_are_none() {
    for xml in [
        "<Package/>",
        "<?xml version=\"1.0\"?><Package><FileProperty-Package><DOI/><DURL> \t</DURL>\
         </FileProperty-Package></Package>",
    ] {
        assert_eq!(
            read(package(xml)).unwrap(),
            Some(ApplicationInfo::default())
        );
    }
}

#[test]
fn absent_trailers_and_other_variants_have_no_package() {
    assert_eq!(read(c8()).unwrap(), None);
    let mut digits = c8();
    digits.extend([b'7'; 40]);
    assert_eq!(read(digits).unwrap(), None);
    // HN-A is never read, even with a well-formed tail.
    let mut hna = vec![0; 0x15c + 20];
    hna[..8].copy_from_slice(b"HN\0\0\x90\x01\0\0");
    hna[0x90] = 1;
    let hna = framed(hna, VALID.len() as u32, &deflate(VALID.as_bytes()));
    assert_eq!(read(hna).unwrap(), None);
}

#[test]
fn malformed_trailers_are_located() {
    let base = c8().len() as u64;
    for (suffix, reason) in [
        (
            &b"APPINFOSIGN x"[..],
            "marker is not followed by a final decimal offset",
        ),
        (
            b"APPINFOSIGN7",
            "marker is not followed by a final decimal offset",
        ),
        (
            b"APPINFOSIGN 99999999999999999999",
            "offset overflows 64 bits",
        ),
    ] {
        let mut bytes = c8();
        bytes.extend(suffix);
        let found = defect(read(bytes));
        assert_eq!(found.offset, base);
        assert_eq!(found.field, "application-info trailer");
        assert_eq!(found.reason, reason);
    }
}

#[test]
fn declared_offsets_and_lengths_are_checked_against_the_file() {
    let lengths = "application-info lengths";
    let stream = deflate(VALID.as_bytes());
    let mut inside = c8();
    inside.extend(b"APPINFOSIGN 10");
    let mut beyond = c8();
    beyond.extend(b"APPINFOSIGN 999");
    let mut short = c8();
    let start = short.len();
    short.extend([0; 7]);
    short.extend(format!("APPINFOSIGN {start}").as_bytes());
    let mut gap = framed(c8(), VALID.len() as u32, &stream);
    let marker = gap.len() - format!("APPINFOSIGN {}", c8().len()).len();
    gap.insert(marker, 0);
    gap[c8().len() + 4..c8().len() + 8].copy_from_slice(&(stream.len() as u32).to_le_bytes());
    let cases = [
        (inside, "malformed"),
        (beyond, "truncated"),
        (short, "truncated"),
        (gap, "malformed"),
        (framed(c8(), 0, &stream), "malformed"),
    ];
    for (bytes, kind) in cases {
        let error = read(bytes).unwrap_err();
        assert_eq!(field_of(&error), lengths);
        assert_eq!(kind_name(&error), kind, "{error}");
    }
    let mut huge = framed(c8(), 1, &stream);
    huge[c8().len()..c8().len() + 4]
        .copy_from_slice(&(MAX_APPLICATION_INFO_BYTES + 1).to_le_bytes());
    let found = defect(read(huge));
    assert_eq!(
        (found.field, found.reason),
        ("application-info decoded bytes", "limit")
    );
    let mut wide = c8();
    let start = wide.len();
    wide.extend(1_u32.to_le_bytes());
    wide.extend((MAX_APPLICATION_INFO_BYTES + 1).to_le_bytes());
    wide.extend(format!("APPINFOSIGN {start}").as_bytes());
    let found = defect(read(wide));
    assert_eq!(
        (found.offset, found.field),
        (start as u64, "application-info compressed bytes")
    );
}

#[test]
fn stream_defects_are_rejected_without_partial_values() {
    let field = "application-info zlib stream";
    let xml = VALID.as_bytes();
    let stream = deflate(xml);
    let mut corrupt = stream.clone();
    corrupt[2] = 0xff;
    corrupt[3] = 0xff;
    let mut trailing = stream.clone();
    trailing.push(0);
    for (bytes, reason) in [
        (
            framed(c8(), xml.len() as u32, &corrupt),
            "invalid stream or checksum",
        ),
        (
            framed(c8(), xml.len() as u32, &stream[..stream.len() - 6]),
            "truncated stream",
        ),
        (framed(c8(), 4, &[]), "truncated stream"),
        (
            framed(c8(), xml.len() as u32 - 1, &stream),
            "output exceeds the declared length",
        ),
        (
            framed(c8(), xml.len() as u32 + 1, &stream),
            "stream ends before the declared lengths",
        ),
        (
            framed(c8(), xml.len() as u32, &trailing),
            "stream ends before the declared lengths",
        ),
    ] {
        let found = defect(read(bytes));
        assert_eq!((found.field, found.reason), (field, reason));
        assert!(found.offset >= c8().len() as u64 + 8);
    }
}

#[test]
fn allocation_limits_and_cancellation_are_reported() {
    let limits = Limits {
        io_chunk_bytes: 4096,
        max_allocation_bytes: TEXT_DECODER_RESERVATION_BYTES + 64,
        ..Limits::default()
    };
    let found = defect(read_with(package(VALID), &limits, &NEVER));
    assert_eq!(
        (found.field, found.reason),
        ("application-info allocation bytes", "limit")
    );
    // Count the queries of a complete read, then cancel at each one.
    let counter = CancelAfter::never();
    read_with(package(VALID), &Limits::default(), &counter).unwrap();
    let total = counter.queries();
    let mut cancelled = 0;
    for allowed in 0..total {
        let error = read_with(
            package(VALID),
            &Limits::default(),
            &CancelAfter::new(allowed),
        );
        if let Err(error) = error {
            assert_eq!(kind_name(&error), "cancelled");
            cancelled += 1;
        }
    }
    assert!(cancelled > 2, "{cancelled} of {total}");
    let error = {
        let mut source = SeekableSource::new(Cursor::new(package(VALID))).unwrap();
        let limits = Limits::default();
        let mut reader = Hnc8Reader::open(&mut source, &limits, &NEVER).unwrap();
        reader.cancellation = &ALWAYS;
        reader.application_info()
    }
    .unwrap_err();
    assert_eq!((error.offset, kind_name(&error)), (Some(0), "cancelled"));
}

static ALWAYS: CancelAfter = CancelAfter::new(0);

#[test]
fn package_markup_outside_the_admitted_subset_is_rejected() {
    let cases = [
        ("x<Package/>", "text outside the root element"),
        ("<Package/>x", "text outside the root element"),
        ("<?xml version='1.0'", "unterminated processing instruction"),
        (
            "<?xml encoding='GBK'?><Package/>",
            "declared encoding is not UTF-8",
        ),
        (
            "<?xml encoding=GBK?><Package/>",
            "declared encoding is not UTF-8",
        ),
        (
            "<?xml encoding?><Package/>",
            "declared encoding is not UTF-8",
        ),
        (
            "<?xml encoding='utf-8?><Package/>",
            "declared encoding is not UTF-8",
        ),
        ("<!-- open <Package/>", "unterminated comment"),
        (
            "<Package><![CDATA[x</Package>",
            "unterminated character data",
        ),
        (
            "<![CDATA[x]]><Package/>",
            "character data outside the root element",
        ),
        ("<!DOCTYPE p><Package/>", "declarations are not accepted"),
        ("<Package></Package", "unterminated end tag"),
        (
            "<Package></Other>",
            "end tag does not match the open element",
        ),
        ("</Package>", "end tag does not match the open element"),
        ("<Other/>", "root element is not one Package"),
        ("<Package/><Package/>", "root element is not one Package"),
        (
            "<Package><FileProperty-Package><DOI><b/></DOI></FileProperty-Package></Package>",
            "unexpected element inside DOI or DURL",
        ),
        (
            "<Package><FileProperty-Package><DURL/><DURL/></FileProperty-Package></Package>",
            "duplicate DOI or DURL",
        ),
        (" ", "root element is missing or unclosed"),
        ("<Package>", "root element is missing or unclosed"),
    ];
    for (xml, reason) in cases {
        assert_eq!(xml_error(xml), reason, "{xml:?}");
    }
    let invalid = framed(c8(), 3, &deflate(b"<\xff>"));
    assert_eq!(defect(read(invalid)).reason, "package is not UTF-8");
    let deep = format!("<Package>{}", "<a>".repeat(MAX_DEPTH));
    assert_eq!(xml_error(&deep), "elements nest too deeply");
    for tag in [
        "<>",
        "< Package/>",
        "<Package",
        "<Package a>",
        "<Package a=b>",
        "<Package =\"b\">",
        "<Package a=\"x<y\">",
        "<Package a=\"x\"b=\"y\">",
        "<Package a=\"open>",
        "<Package/ >",
        "<Package a = >",
    ] {
        assert_eq!(xml_error(tag), "malformed start tag", "{tag:?}");
    }
}

#[test]
fn entities_and_field_sizes_are_bounded() {
    let doi = |text: &str| {
        format!("<Package><FileProperty-Package><DOI>{text}</DOI></FileProperty-Package></Package>")
    };
    assert_eq!(xml_error(&doi("a&amp")), "unterminated entity reference");
    for reference in [
        "&nbsp;",
        "&#;",
        "&#x;",
        "&#0;",
        "&#+5;",
        "&#xD800;",
        "&#x+1;",
        "&#99999999999;",
    ] {
        assert_eq!(
            xml_error(&doi(reference)),
            "unknown entity or character reference",
            "{reference}"
        );
    }
    let longest = "d".repeat(MAX_APPLICATION_INFO_FIELD_BYTES);
    let info = read(package(&doi(&longest))).unwrap().unwrap();
    assert_eq!(info.doi.unwrap().len(), MAX_APPLICATION_INFO_FIELD_BYTES);
    for text in [
        format!("{longest}e"),
        format!("{longest}&amp;"),
        format!("<![CDATA[{longest}e]]>"),
    ] {
        let found = defect(read(package(&doi(&text))));
        assert_eq!(
            (found.field, found.reason),
            ("application-info field bytes", "limit")
        );
    }
}

#[test]
fn defects_display_their_location_and_reason() {
    let found = defect(read(package("<Other/>")));
    assert_eq!(
        found.to_string(),
        format!(
            "ignored C8 application-info package at byte {}: application-info XML: root element is not one Package",
            c8().len() + 8
        )
    );
    assert_eq!(
        ApplicationInfoStatus::default(),
        ApplicationInfoStatus::Absent
    );
}

#[test]
fn native_links_use_bounded_reads_and_keep_damaged_tails_as_warnings() {
    let xml = "<Package><Note-Package><NoteItems Unit='PointsM' Version='1.0'><Item Type='Link' Page='1' Color='00808080' LineStyle='4' ShowType='1'><RC l='100' t='200' r='900' b='500'/><Item Type='UrlLink' Aim='https://example.invalid/'/></Item></NoteItems></Note-Package></Package>";
    let limits = Limits {
        io_chunk_bytes: 7,
        ..Limits::default()
    };
    let run = |data: Vec<u8>, cancel: &CancelAfter| {
        let mut source = SeekableSource::new(Cursor::new(data)).unwrap();
        Hnc8Reader::open(&mut source, &limits, cancel)?.application_info_with_links()
    };
    let (report, links) = run(package(xml), &NEVER).unwrap();
    assert_eq!(report.status, ApplicationInfoStatus::Read);
    assert_eq!(links.len(), 1);
    for changed in [
        xml.replace("r='900'", "r='0'"),
        xml.replace("</Package>", "</Wrong>"),
    ] {
        let (report, links) = run(package(&changed), &NEVER).unwrap();
        assert!(matches!(report.status, ApplicationInfoStatus::Ignored(_)));
        assert!(report.info.is_none() && links.is_empty());
    }
    assert!(matches!(
        run(
            package(&xml.replace("LineStyle='4'", "LineStyle='9'")),
            &NEVER
        )
        .unwrap_err()
        .kind,
        ErrorKind::UnsupportedFormat
    ));
    let counter = CancelAfter::never();
    run(package(xml), &counter).unwrap();
    for allowed in [0, counter.queries() / 2, counter.queries() - 1] {
        assert!(matches!(
            run(package(xml), &CancelAfter::new(allowed))
                .unwrap_err()
                .kind,
            ErrorKind::Cancelled
        ));
    }
}