extend-docx 0.1.1

Rust parser, document model, and serializer for DOCX and legacy Word documents
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
use std::collections::HashMap;
use std::sync::LazyLock;

use miniz_oxide::inflate::decompress_to_vec_with_limit;

use crate::package::{OoxmlPackage, OoxmlPart};

const ZIP_LOCAL_FILE_HEADER_SIGNATURE: u32 = 0x0403_4b50;
const ZIP_CENTRAL_DIRECTORY_SIGNATURE: u32 = 0x0201_4b50;
const ZIP_EOCD_SIGNATURE: u32 = 0x0605_4b50;
const UTF8_FLAG: u16 = 0x0800;
const STORE_COMPRESSION: u16 = 0;
const DEFLATE_COMPRESSION: u16 = 8;

const DEFAULT_DOCUMENT_XML: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:body>
    <w:p><w:r><w:t/></w:r></w:p>
    <w:sectPr>
      <w:pgSz w:w="12240" w:h="15840"/>
      <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
    </w:sectPr>
  </w:body>
</w:document>"#;

const DEFAULT_CONTENT_TYPES_XML: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
  <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
  <Default Extension="xml" ContentType="application/xml"/>
  <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
</Types>"#;

const DEFAULT_ROOT_RELS_XML: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
</Relationships>"#;

const DEFAULT_DOCUMENT_RELS_XML: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"/>"#;

struct ZipEntry {
    name: String,
    compression_method: u16,
    compressed_size: u32,
    uncompressed_size: u32,
    local_header_offset: u32,
}

fn read_u16_le(bytes: &[u8], offset: usize) -> u16 {
    u16::from_le_bytes([bytes[offset], bytes[offset + 1]])
}

fn read_u32_le(bytes: &[u8], offset: usize) -> u32 {
    u32::from_le_bytes([
        bytes[offset],
        bytes[offset + 1],
        bytes[offset + 2],
        bytes[offset + 3],
    ])
}

fn write_u16_le(out: &mut [u8], offset: usize, value: u16) {
    out[offset..offset + 2].copy_from_slice(&value.to_le_bytes());
}

fn write_u32_le(out: &mut [u8], offset: usize, value: u32) {
    out[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
}

fn find_eocd_offset(bytes: &[u8]) -> Result<usize, String> {
    const MINIMUM_LENGTH: usize = 22;
    if bytes.len() < MINIMUM_LENGTH {
        return Err("Invalid DOCX ZIP: too small to contain EOCD".to_string());
    }

    let max_comment_length = 0xffff;
    let search_start = bytes
        .len()
        .saturating_sub(MINIMUM_LENGTH + max_comment_length);

    let mut index = bytes.len().saturating_sub(MINIMUM_LENGTH);
    while index >= search_start {
        if read_u32_le(bytes, index) == ZIP_EOCD_SIGNATURE {
            return Ok(index);
        }
        if index == 0 {
            break;
        }
        index -= 1;
    }

    Err("Invalid DOCX ZIP: end of central directory not found".to_string())
}

fn parse_central_directory(bytes: &[u8]) -> Result<Vec<ZipEntry>, String> {
    let eocd_offset = find_eocd_offset(bytes)?;
    let total_entries = read_u16_le(bytes, eocd_offset + 10) as usize;
    let central_directory_offset = read_u32_le(bytes, eocd_offset + 16) as usize;

    let mut entries = Vec::with_capacity(total_entries);
    let mut cursor = central_directory_offset;

    for _ in 0..total_entries {
        if cursor.checked_add(46).is_none_or(|end| end > bytes.len())
            || read_u32_le(bytes, cursor) != ZIP_CENTRAL_DIRECTORY_SIGNATURE
        {
            return Err("Invalid DOCX ZIP: malformed central directory".to_string());
        }

        let compression_method = read_u16_le(bytes, cursor + 10);
        let compressed_size = read_u32_le(bytes, cursor + 20);
        let uncompressed_size = read_u32_le(bytes, cursor + 24);
        let file_name_length = read_u16_le(bytes, cursor + 28) as usize;
        let extra_field_length = read_u16_le(bytes, cursor + 30) as usize;
        let file_comment_length = read_u16_le(bytes, cursor + 32) as usize;
        let local_header_offset = read_u32_le(bytes, cursor + 42);

        let file_name_start = cursor + 46;
        let file_name_end = file_name_start + file_name_length;
        if file_name_end
            .checked_add(extra_field_length + file_comment_length)
            .is_none_or(|end| end > bytes.len())
        {
            return Err("Invalid DOCX ZIP: truncated directory entry".into());
        }
        let name = String::from_utf8_lossy(&bytes[file_name_start..file_name_end]).into_owned();

        entries.push(ZipEntry {
            name,
            compression_method,
            compressed_size,
            uncompressed_size,
            local_header_offset,
        });

        cursor = file_name_end + extra_field_length + file_comment_length;
    }

    Ok(entries)
}

fn inflate_raw(data: &[u8]) -> Result<Vec<u8>, String> {
    decompress_to_vec_with_limit(data, usize::MAX)
        .map_err(|error| format!("Failed to inflate DOCX ZIP entry: {error:?}"))
}

#[derive(Default)]
struct ZipInflater {
    #[cfg(all(feature = "native-deflate", not(target_arch = "wasm32")))]
    native: libdeflater::Decompressor,
}

impl ZipInflater {
    fn decode(&mut self, compressed: &[u8], output: &mut [u8]) -> Option<usize> {
        #[cfg(all(feature = "native-deflate", not(target_arch = "wasm32")))]
        if let Ok(length) = self.native.deflate_decompress(compressed, output) {
            return Some(length);
        }
        let (decoded, status) = zlib_rs::decompress_slice(
            output,
            compressed,
            zlib_rs::InflateConfig { window_bits: -15 },
        );
        (status == zlib_rs::ReturnCode::Ok).then_some(decoded.len())
    }
}

fn extract_entry_data(
    bytes: &[u8],
    entry: &ZipEntry,
    inflater: &mut ZipInflater,
) -> Result<Vec<u8>, String> {
    let local_header_offset = entry.local_header_offset as usize;

    if local_header_offset
        .checked_add(30)
        .is_none_or(|end| end > bytes.len())
        || read_u32_le(bytes, local_header_offset) != ZIP_LOCAL_FILE_HEADER_SIGNATURE
    {
        return Err(format!(
            "Invalid DOCX ZIP: bad local header for {}",
            entry.name
        ));
    }

    let file_name_length = read_u16_le(bytes, local_header_offset + 26) as usize;
    let extra_field_length = read_u16_le(bytes, local_header_offset + 28) as usize;
    let payload_offset = local_header_offset + 30 + file_name_length + extra_field_length;
    let payload_end = payload_offset
        .checked_add(entry.compressed_size as usize)
        .ok_or("Invalid DOCX ZIP: entry size overflow")?;
    let compressed_data = bytes
        .get(payload_offset..payload_end)
        .ok_or("Invalid DOCX ZIP: truncated entry")?;

    if entry.compression_method == STORE_COMPRESSION {
        return Ok(compressed_data.to_vec());
    }

    if entry.compression_method == DEFLATE_COMPRESSION {
        let expected = entry.uncompressed_size as usize;
        if expected <= 64 * 1024 * 1024
            && expected <= compressed_data.len().saturating_mul(512).max(1024 * 1024)
        {
            let mut output = vec![0; expected];
            if let Some(length) = inflater.decode(compressed_data, &mut output) {
                output.truncate(length);
                return Ok(output);
            }
        }
        return inflate_raw(compressed_data);
    }

    Err(format!(
        "Unsupported DOCX ZIP compression method {} for {}",
        entry.compression_method, entry.name
    ))
}

pub fn is_text_part(part_name: &str) -> bool {
    if part_name == "[Content_Types].xml" {
        return true;
    }

    match part_name.rsplit_once('.') {
        Some((_, extension)) => {
            extension.eq_ignore_ascii_case("xml")
                || extension.eq_ignore_ascii_case("rels")
                || extension.eq_ignore_ascii_case("txt")
        }
        None => false,
    }
}

fn decode_xml_part(bytes: Vec<u8>) -> Result<String, String> {
    let big_endian = bytes.starts_with(&[0xfe, 0xff]) || bytes.starts_with(&[0, b'<', 0, b'?']);
    let little_endian = bytes.starts_with(&[0xff, 0xfe]) || bytes.starts_with(&[b'<', 0, b'?', 0]);
    if big_endian || little_endian {
        let skip = if bytes.starts_with(&[0xfe, 0xff]) || bytes.starts_with(&[0xff, 0xfe]) {
            2
        } else {
            0
        };
        let body = &bytes[skip..];
        if body.len() % 2 != 0 {
            return Err("Invalid UTF-16 XML byte length".into());
        }
        let units = body
            .chunks_exact(2)
            .map(|pair| {
                if big_endian {
                    u16::from_be_bytes([pair[0], pair[1]])
                } else {
                    u16::from_le_bytes([pair[0], pair[1]])
                }
            })
            .collect::<Vec<_>>();
        let xml =
            String::from_utf16(&units).map_err(|error| format!("Invalid UTF-16 XML: {error}"))?;
        static ENCODING: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(|| {
            regex::Regex::new(
                r#"(?i)encoding[\x09-\x0D\x20]*=[\x09-\x0D\x20]*["']UTF-16(?:LE|BE)?["']"#,
            )
            .unwrap()
        });
        let encoding = &*ENCODING;
        return Ok(encoding.replace(&xml, "encoding=\"UTF-8\"").into_owned());
    }
    String::from_utf8(bytes)
        .map_err(|error| format!("Invalid or unsupported XML encoding: {error}"))
}

#[cfg(not(target_arch = "wasm32"))]
fn extract_entry_group(
    bytes: &[u8],
    entries: &[ZipEntry],
    group: &[usize],
) -> Vec<(usize, Result<Vec<u8>, String>)> {
    let mut inflater = ZipInflater::default();
    group.iter().map(|&index| {
        (index, extract_entry_data(bytes, &entries[index], &mut inflater))
    }).collect()
}

#[cfg(not(target_arch = "wasm32"))]
#[cold]
fn parallel_entry_data(
    bytes: &[u8],
    entries: &[ZipEntry],
) -> Option<Vec<Option<Result<Vec<u8>, String>>>> {
    if bytes.len() < 16 * 1024
        || entries.iter().map(|entry| u64::from(entry.uncompressed_size)).sum::<u64>() < 64 * 1024
        || entries.iter().filter(|entry| {
            !entry.name.ends_with('/')
                && entry.compression_method == DEFLATE_COMPRESSION
                && entry.uncompressed_size >= 16 * 1024
        }).take(2).count() < 2
    {
        return None;
    }
    let pool = crate::parse::worker_pool()?;
    let mut jobs: Vec<_> = entries.iter().enumerate()
        .filter_map(|(index, entry)| (!entry.name.ends_with('/')).then_some(index))
        .collect();
    jobs.sort_unstable_by_key(|&index| std::cmp::Reverse(entries[index].uncompressed_size));
    let mut groups: [Vec<usize>; 4] = std::array::from_fn(|_| Vec::new());
    let mut weights = [0u64; 4];
    for index in jobs {
        let group = (0..4).min_by_key(|&group| weights[group]).unwrap();
        weights[group] += u64::from(entries[index].uncompressed_size);
        groups[group].push(index);
    }
    let mut outputs: [Option<Vec<(usize, Result<Vec<u8>, String>)>>; 4] = std::array::from_fn(|_| None);
    let (caller, workers) = outputs.split_first_mut().unwrap();
    pool.in_place_scope(|scope| {
        for (slot, group) in workers.iter_mut().zip(&groups[1..]) {
            scope.spawn(move |_| *slot = Some(extract_entry_group(bytes, entries, group)));
        }
        *caller = Some(extract_entry_group(bytes, entries, &groups[0]));
    });
    let mut result: Vec<_> = (0..entries.len()).map(|_| None).collect();
    for group in outputs.into_iter().flatten() {
        for (index, value) in group {
            result[index] = Some(value);
        }
    }
    Some(result)
}

#[cfg(not(target_arch = "wasm32"))]
#[cold]
fn package_from_parallel_entries(
    entries: Vec<ZipEntry>,
    results: Vec<Option<Result<Vec<u8>, String>>>,
) -> Result<OoxmlPackage, String> {
    let mut parts = HashMap::new();
    let mut binary_assets = HashMap::new();
    for (entry, data) in entries.into_iter().zip(results) {
        if entry.name.ends_with('/') {
            continue;
        }
        let data = data.expect("scheduled ZIP entry")?;
        if is_text_part(&entry.name) {
            parts.insert(entry.name.clone(), OoxmlPart {
                name: entry.name,
                content: decode_xml_part(data)?,
            });
        } else {
            binary_assets.insert(entry.name, data);
        }
    }
    let package = OoxmlPackage { parts, binary_assets, warnings: Vec::new() };
    let main_part = crate::package::main_document_part_name(&package);
    if !package.parts.contains_key(&main_part) {
        return Err(format!("Invalid DOCX: missing {main_part}"));
    }
    Ok(package)
}

pub fn parse_docx(bytes: &[u8]) -> Result<OoxmlPackage, String> {
    if bytes.is_empty() {
        return Err("DOCX input cannot be empty".to_string());
    }

    let entries = parse_central_directory(bytes)?;
    #[cfg(not(target_arch = "wasm32"))]
    if bytes.len() >= 16 * 1024 {
        if let Some(parallel) = parallel_entry_data(bytes, &entries) {
            return package_from_parallel_entries(entries, parallel);
        }
    }
    let mut parts = HashMap::new();
    let mut binary_assets = HashMap::new();
    let mut inflater = ZipInflater::default();

    for entry in entries {
        if entry.name.ends_with('/') {
            continue;
        }

        let file_bytes = extract_entry_data(bytes, &entry, &mut inflater)?;

        if is_text_part(&entry.name) {
            parts.insert(
                entry.name.clone(),
                OoxmlPart {
                    name: entry.name,
                    content: decode_xml_part(file_bytes)?,
                },
            );
            continue;
        }

        binary_assets.insert(entry.name, file_bytes);
    }

    let package = OoxmlPackage {
        parts,
        binary_assets,
        warnings: Vec::new(),
    };
    let main_part = crate::package::main_document_part_name(&package);
    if !package.parts.contains_key(&main_part) {
        return Err(format!("Invalid DOCX: missing {main_part}"));
    }
    Ok(package)
}

fn create_crc32_table() -> [u32; 256] {
    let mut table = [0u32; 256];
    for index in 0..256 {
        let mut value = index as u32;
        for _ in 0..8 {
            value = if value & 1 == 1 {
                (value >> 1) ^ 0xedb8_8320
            } else {
                value >> 1
            };
        }
        table[index] = value;
    }
    table
}

static CRC32_TABLE: LazyLock<[u32; 256]> = LazyLock::new(create_crc32_table);

fn crc32(bytes: &[u8]) -> u32 {
    let mut value = 0xffff_ffffu32;
    for byte in bytes {
        value = (value >> 8) ^ CRC32_TABLE[((value ^ u32::from(*byte)) & 0xff) as usize];
    }
    value ^ 0xffff_ffff
}

fn concat_bytes(chunks: &[&[u8]]) -> Vec<u8> {
    let total_length: usize = chunks.iter().map(|chunk| chunk.len()).sum();
    let mut output = Vec::with_capacity(total_length);
    for chunk in chunks {
        output.extend_from_slice(chunk);
    }
    output
}

pub fn package_to_bytes(pkg: &OoxmlPackage) -> Result<Vec<u8>, String> {
    let mut entries: Vec<(String, Vec<u8>)> = pkg
        .parts
        .values()
        .map(|part| (part.name.clone(), part.content.as_bytes().to_vec()))
        .chain(
            pkg.binary_assets
                .iter()
                .map(|(name, data)| (name.clone(), data.clone())),
        )
        .collect();

    entries.sort_by(|left, right| left.0.cmp(&right.0));

    if entries.is_empty() {
        return Err("Cannot create DOCX ZIP from an empty package".to_string());
    }

    if entries.len() > 0xffff {
        return Err("Too many ZIP entries for non-ZIP64 writer".to_string());
    }

    let mut local_chunks: Vec<Vec<u8>> = Vec::new();
    let mut central_chunks: Vec<Vec<u8>> = Vec::new();
    let mut offset = 0u32;

    for (name, payload) in &entries {
        let name_bytes = name.as_bytes();
        let crc = crc32(payload);

        let mut local_header = vec![0u8; 30 + name_bytes.len() + payload.len()];
        write_u32_le(&mut local_header, 0, ZIP_LOCAL_FILE_HEADER_SIGNATURE);
        write_u16_le(&mut local_header, 4, 20);
        write_u16_le(&mut local_header, 6, UTF8_FLAG);
        write_u16_le(&mut local_header, 8, STORE_COMPRESSION);
        write_u16_le(&mut local_header, 10, 0);
        write_u16_le(&mut local_header, 12, 0);
        write_u32_le(&mut local_header, 14, crc);
        write_u32_le(&mut local_header, 18, payload.len() as u32);
        write_u32_le(&mut local_header, 22, payload.len() as u32);
        write_u16_le(&mut local_header, 26, name_bytes.len() as u16);
        write_u16_le(&mut local_header, 28, 0);
        local_header[30..30 + name_bytes.len()].copy_from_slice(name_bytes);
        local_header[30 + name_bytes.len()..].copy_from_slice(payload);
        local_chunks.push(local_header.clone());

        let mut central_header = vec![0u8; 46 + name_bytes.len()];
        write_u32_le(&mut central_header, 0, ZIP_CENTRAL_DIRECTORY_SIGNATURE);
        write_u16_le(&mut central_header, 4, 20);
        write_u16_le(&mut central_header, 6, 20);
        write_u16_le(&mut central_header, 8, UTF8_FLAG);
        write_u16_le(&mut central_header, 10, STORE_COMPRESSION);
        write_u16_le(&mut central_header, 12, 0);
        write_u16_le(&mut central_header, 14, 0);
        write_u32_le(&mut central_header, 16, crc);
        write_u32_le(&mut central_header, 20, payload.len() as u32);
        write_u32_le(&mut central_header, 24, payload.len() as u32);
        write_u16_le(&mut central_header, 28, name_bytes.len() as u16);
        write_u16_le(&mut central_header, 30, 0);
        write_u16_le(&mut central_header, 32, 0);
        write_u16_le(&mut central_header, 34, 0);
        write_u16_le(&mut central_header, 36, 0);
        write_u32_le(&mut central_header, 38, 0);
        write_u32_le(&mut central_header, 42, offset);
        central_header[46..].copy_from_slice(name_bytes);
        central_chunks.push(central_header);

        offset += local_header.len() as u32;
    }

    let central_directory_offset = offset;
    let central_directory: Vec<u8> = central_chunks.into_iter().flatten().collect();
    let local_file_data: Vec<u8> = local_chunks.into_iter().flatten().collect();

    let mut eocd = vec![0u8; 22];
    write_u32_le(&mut eocd, 0, ZIP_EOCD_SIGNATURE);
    write_u16_le(&mut eocd, 4, 0);
    write_u16_le(&mut eocd, 6, 0);
    write_u16_le(&mut eocd, 8, entries.len() as u16);
    write_u16_le(&mut eocd, 10, entries.len() as u16);
    write_u32_le(&mut eocd, 12, central_directory.len() as u32);
    write_u32_le(&mut eocd, 16, central_directory_offset);
    write_u16_le(&mut eocd, 20, 0);

    Ok(concat_bytes(&[&local_file_data, &central_directory, &eocd]))
}

pub fn create_minimal_docx_package(document_xml: Option<&str>) -> OoxmlPackage {
    let document_xml = document_xml.unwrap_or(DEFAULT_DOCUMENT_XML);

    let mut parts = HashMap::new();
    parts.insert(
        "[Content_Types].xml".to_string(),
        OoxmlPart {
            name: "[Content_Types].xml".to_string(),
            content: DEFAULT_CONTENT_TYPES_XML.to_string(),
        },
    );
    parts.insert(
        "_rels/.rels".to_string(),
        OoxmlPart {
            name: "_rels/.rels".to_string(),
            content: DEFAULT_ROOT_RELS_XML.to_string(),
        },
    );
    parts.insert(
        "word/document.xml".to_string(),
        OoxmlPart {
            name: "word/document.xml".to_string(),
            content: document_xml.to_string(),
        },
    );
    parts.insert(
        "word/_rels/document.xml.rels".to_string(),
        OoxmlPart {
            name: "word/_rels/document.xml.rels".to_string(),
            content: DEFAULT_DOCUMENT_RELS_XML.to_string(),
        },
    );

    OoxmlPackage {
        parts,
        binary_assets: HashMap::new(),
        warnings: Vec::new(),
    }
}

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

    const DOCUMENT_XML: &str = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>Hello DOCX</w:t></w:r></w:p></w:body></w:document>"#;

    struct ZipSourceEntry<'a> {
        name: &'a str,
        content: &'a str,
        deflate: bool,
    }

    fn create_zip(entries: &[ZipSourceEntry<'_>]) -> Vec<u8> {
        let mut local_chunks: Vec<Vec<u8>> = Vec::new();
        let mut central_chunks: Vec<Vec<u8>> = Vec::new();
        let mut offset = 0u32;

        for entry in entries {
            let name_bytes = entry.name.as_bytes();
            let uncompressed = entry.content.as_bytes();
            let compression_method = if entry.deflate {
                DEFLATE_COMPRESSION
            } else {
                STORE_COMPRESSION
            };
            let compressed = if entry.deflate {
                miniz_oxide::deflate::compress_to_vec(uncompressed, 6)
            } else {
                uncompressed.to_vec()
            };
            let crc = crc32(uncompressed);

            let mut local = vec![0u8; 30 + name_bytes.len() + compressed.len()];
            write_u32_le(&mut local, 0, ZIP_LOCAL_FILE_HEADER_SIGNATURE);
            write_u16_le(&mut local, 4, 20);
            write_u16_le(&mut local, 6, UTF8_FLAG);
            write_u16_le(&mut local, 8, compression_method);
            write_u16_le(&mut local, 10, 0);
            write_u16_le(&mut local, 12, 0);
            write_u32_le(&mut local, 14, crc);
            write_u32_le(&mut local, 18, compressed.len() as u32);
            write_u32_le(&mut local, 22, uncompressed.len() as u32);
            write_u16_le(&mut local, 26, name_bytes.len() as u16);
            write_u16_le(&mut local, 28, 0);
            local[30..30 + name_bytes.len()].copy_from_slice(name_bytes);
            local[30 + name_bytes.len()..].copy_from_slice(&compressed);
            local_chunks.push(local.clone());

            let mut central = vec![0u8; 46 + name_bytes.len()];
            write_u32_le(&mut central, 0, ZIP_CENTRAL_DIRECTORY_SIGNATURE);
            write_u16_le(&mut central, 4, 20);
            write_u16_le(&mut central, 6, 20);
            write_u16_le(&mut central, 8, UTF8_FLAG);
            write_u16_le(&mut central, 10, compression_method);
            write_u16_le(&mut central, 12, 0);
            write_u16_le(&mut central, 14, 0);
            write_u32_le(&mut central, 16, crc);
            write_u32_le(&mut central, 20, compressed.len() as u32);
            write_u32_le(&mut central, 24, uncompressed.len() as u32);
            write_u16_le(&mut central, 28, name_bytes.len() as u16);
            write_u16_le(&mut central, 30, 0);
            write_u16_le(&mut central, 32, 0);
            write_u16_le(&mut central, 34, 0);
            write_u16_le(&mut central, 36, 0);
            write_u32_le(&mut central, 38, 0);
            write_u32_le(&mut central, 42, offset);
            central[46..].copy_from_slice(name_bytes);
            central_chunks.push(central);

            offset += local.len() as u32;
        }

        let local_data: Vec<u8> = local_chunks.into_iter().flatten().collect();
        let central_data: Vec<u8> = central_chunks.into_iter().flatten().collect();

        let mut eocd = vec![0u8; 22];
        write_u32_le(&mut eocd, 0, ZIP_EOCD_SIGNATURE);
        write_u16_le(&mut eocd, 4, 0);
        write_u16_le(&mut eocd, 6, 0);
        write_u16_le(&mut eocd, 8, entries.len() as u16);
        write_u16_le(&mut eocd, 10, entries.len() as u16);
        write_u32_le(&mut eocd, 12, central_data.len() as u32);
        write_u32_le(&mut eocd, 16, local_data.len() as u32);
        write_u16_le(&mut eocd, 20, 0);

        concat_bytes(&[&local_data, &central_data, &eocd])
    }

    #[test]
    fn throws_on_empty_input() {
        let error = parse_docx(&[]).unwrap_err();
        assert!(error.contains("cannot be empty"));
    }

    #[test]
    fn parses_stored_zip_entries() {
        let zip = create_zip(&[
            ZipSourceEntry {
                name: "[Content_Types].xml",
                content: "<Types/>",
                deflate: false,
            },
            ZipSourceEntry {
                name: "word/document.xml",
                content: DOCUMENT_XML,
                deflate: false,
            },
        ]);
        let pkg = parse_docx(&zip).expect("parse stored zip");

        assert!(get_part(&pkg, "word/document.xml")
            .expect("document part")
            .content
            .contains("Hello DOCX"));
    }

    #[test]
    fn parses_deflated_zip_entries() {
        let zip = create_zip(&[
            ZipSourceEntry {
                name: "[Content_Types].xml",
                content: "<Types/>",
                deflate: true,
            },
            ZipSourceEntry {
                name: "word/document.xml",
                content: DOCUMENT_XML,
                deflate: true,
            },
        ]);
        let pkg = parse_docx(&zip).expect("parse deflated zip");

        assert!(get_part(&pkg, "word/document.xml")
            .expect("document part")
            .content
            .contains("Hello DOCX"));
    }

    #[test]
    fn writes_and_reparses_docx_zip_archives() {
        let source = create_minimal_docx_package(Some(DOCUMENT_XML));
        let zip = package_to_bytes(&source).expect("package to bytes");
        let reparsed = parse_docx(&zip).expect("reparse docx");

        assert!(reparsed
            .parts
            .get("word/document.xml")
            .expect("document part")
            .content
            .contains("Hello DOCX"));
    }
    #[cfg(not(target_arch = "wasm32"))]
    #[test]
    fn parallel_entries_preserve_payloads_duplicates_and_error_order() {
        let mut state = 123456789u32;
        let payload: String = (0..512 * 1024).map(|_| {
            state ^= state << 13;
            state ^= state >> 17;
            state ^= state << 5;
            char::from(32 + (state % 95) as u8)
        }).collect();
        let mut zip = create_zip(&[
            ZipSourceEntry { name: "word/document.xml", content: DOCUMENT_XML, deflate: false },
            ZipSourceEntry { name: "payload/a.bin", content: &payload, deflate: true },
            ZipSourceEntry { name: "payload/b.bin", content: &payload, deflate: true },
            ZipSourceEntry { name: "payload/a.bin", content: "replacement", deflate: true },
            ZipSourceEntry { name: "payload/", content: "", deflate: false },
        ]);
        let entries = parse_central_directory(&zip).unwrap();
        let mut inflater = ZipInflater::default();
        let expected: Vec<_> = entries.iter().map(|entry| {
            (!entry.name.ends_with('/')).then(|| extract_entry_data(&zip, entry, &mut inflater))
        }).collect();
        assert_eq!(parallel_entry_data(&zip, &entries).unwrap(), expected);
        let parsed = parse_docx(&zip).unwrap();
        assert_eq!(parsed.binary_assets["payload/a.bin"], b"replacement");
        assert_eq!(parsed.binary_assets["payload/b.bin"], payload.as_bytes());
        assert!(!parsed.binary_assets.contains_key("payload/"));
        let main = entries[0].local_header_offset as usize;
        let content = main + 30 + read_u16_le(&zip, main + 26) as usize;
        zip[content] = 0xff;
        let later = entries[2].local_header_offset as usize;
        zip[later..later + 4].fill(0);
        assert!(parse_docx(&zip).unwrap_err().starts_with("Invalid or unsupported XML encoding:"));
    }

}