parmesan-par2 0.6.0

Fast, standalone PAR2 (Reed-Solomon) creation, verification and repair tool
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
//! PAR2 packet serialization.
//!
//! Every PAR2 packet is a 64-byte header followed by a body whose length is a
//! multiple of 4. The header is:
//!
//! | offset | size | field                                            |
//! |--------|------|--------------------------------------------------|
//! | 0      | 8    | magic `PAR2\0PKT`                                |
//! | 8      | 8    | total packet length (u64 LE)                     |
//! | 16     | 16   | MD5 of the packet from offset 32 onward          |
//! | 32     | 16   | recovery set ID                                  |
//! | 48     | 16   | packet type                                      |
//!
//! All integers are little-endian.

use std::cmp::Ordering;

use md5::{Digest, Md5};

pub(crate) const MAGIC: [u8; 8] = *b"PAR2\0PKT";

/// Size of the fixed packet header.
pub const HEADER_LEN: usize = 64;

/// Packet type tag: Main packet.
pub const TYPE_MAIN: [u8; 16] = *b"PAR 2.0\0Main\0\0\0\0";
/// Packet type tag: File Description packet.
pub const TYPE_FILE_DESC: [u8; 16] = *b"PAR 2.0\0FileDesc";
/// Packet type tag: Input File Slice Checksum packet.
pub const TYPE_IFSC: [u8; 16] = *b"PAR 2.0\0IFSC\0\0\0\0";
/// Packet type tag: Recovery Slice packet.
pub const TYPE_RECOVERY: [u8; 16] = *b"PAR 2.0\0RecvSlic";
/// Packet type tag: Creator packet.
pub const TYPE_CREATOR: [u8; 16] = *b"PAR 2.0\0Creator\0";
/// Packet type tag: optional Unicode Filename packet.
pub const TYPE_UNICODE_FILENAME: [u8; 16] = *b"PAR 2.0\0UniFileN";

/// MD5 + CRC32 checksum of one (zero-padded) input slice.
#[derive(Debug, Clone, Copy)]
pub struct SliceChecksum {
    pub md5: [u8; 16],
    pub crc32: u32,
}

/// Compute the MD5 digest of a byte slice.
pub fn md5(data: &[u8]) -> [u8; 16] {
    let mut hasher = Md5::new();
    hasher.update(data);
    let mut out = [0u8; 16];
    out.copy_from_slice(&hasher.finalize());
    out
}

/// Zero-pad a byte vector up to the next multiple of 4.
fn pad_to_4(mut bytes: Vec<u8>) -> Vec<u8> {
    while !bytes.len().is_multiple_of(4) {
        bytes.push(0);
    }
    bytes
}

/// Serialize a complete PAR2 packet: the 64-byte header followed by `body`.
///
/// # Panics
///
/// Panics if `body`'s length is not a multiple of 4.
pub fn serialize_packet(
    recovery_set_id: &[u8; 16],
    packet_type: &[u8; 16],
    body: &[u8],
) -> Vec<u8> {
    assert!(
        body.len().is_multiple_of(4),
        "PAR2 packet body must be a multiple of 4 bytes"
    );
    let total = HEADER_LEN + body.len();
    let mut packet = Vec::with_capacity(total);
    packet.extend_from_slice(&MAGIC); // 0..8
    packet.extend_from_slice(&(total as u64).to_le_bytes()); // 8..16
    packet.extend_from_slice(&[0u8; 16]); // 16..32 — MD5 placeholder
    packet.extend_from_slice(recovery_set_id); // 32..48
    packet.extend_from_slice(packet_type); // 48..64
    packet.extend_from_slice(body); // 64..

    let hash = md5(&packet[32..]);
    packet[16..32].copy_from_slice(&hash);
    packet
}

/// Serialize a batch of recovery slices into packets, computing their MD5 hashes
/// in parallel using SIMD.
pub fn serialize_recovery_packets(
    recovery_set_id: &[u8; 16],
    slices: &[crate::encoder::RecoverySlice],
) -> Vec<Vec<u8>> {
    use md5_many::{Md5Many, Md5State};
    let many = Md5Many::new();

    let mut out_packets = Vec::with_capacity(slices.len());

    // Build the packets without MD5 first
    for slice in slices {
        let body_len = 4 + slice.data.len();
        let total = HEADER_LEN + body_len;
        let mut packet = Vec::with_capacity(total);
        packet.extend_from_slice(&MAGIC); // 0..8
        packet.extend_from_slice(&(total as u64).to_le_bytes()); // 8..16
        packet.extend_from_slice(&[0u8; 16]); // 16..32 — MD5 placeholder
        packet.extend_from_slice(recovery_set_id); // 32..48
        packet.extend_from_slice(&TYPE_RECOVERY); // 48..64
        packet.extend_from_slice(&slice.exponent.to_le_bytes()); // 64..68
        packet.extend_from_slice(&slice.data); // 68..
        out_packets.push(packet);
    }

    // Hash them in batches
    let lanes = many.lanes();
    for chunk in out_packets.chunks_mut(lanes) {
        let mut states = vec![Md5State::new(); chunk.len()];
        let mut inputs = Vec::with_capacity(chunk.len());
        for p in chunk.iter() {
            inputs.push(&p[32..]);
        }
        many.update_many(&mut states, &inputs);
        let mut hashes = vec![[0u8; 16]; chunk.len()];
        many.finalize_many(&states, &mut hashes);
        for (i, hash) in hashes.into_iter().enumerate() {
            chunk[i][16..32].copy_from_slice(&hash);
        }
    }

    out_packets
}

/// Order two File IDs the way the Main packet lists them.
///
/// PAR 2.0 spec, Main packet: File IDs "are sorted by numerical value
/// (treating them as 16-byte unsigned integers)", and every PAR2 integer is
/// little-endian, so the last byte is the most significant. par2cmdline
/// (`MD5Hash::operator<`) and ParPar (`BufferCompareReverse`) compare the
/// same way. This order also fixes which input slice gets which Reed–Solomon
/// constant, so every place that sorts files for a recovery set must use it.
pub fn cmp_file_id(a: &[u8; 16], b: &[u8; 16]) -> Ordering {
    a.iter().rev().cmp(b.iter().rev())
}

/// Build the body of a Main packet.
///
/// The recovery set contains exactly the given files (no non-recovery files).
/// File IDs are sorted with [`cmp_file_id`] so the body — and therefore the
/// recovery set ID — is canonical regardless of input order.
pub fn main_body(slice_size: u64, recovery_file_ids: &[[u8; 16]]) -> Vec<u8> {
    let mut sorted = recovery_file_ids.to_vec();
    sorted.sort_unstable_by(cmp_file_id);

    let mut body = Vec::with_capacity(12 + 16 * sorted.len());
    body.extend_from_slice(&slice_size.to_le_bytes());
    body.extend_from_slice(&(sorted.len() as u32).to_le_bytes());
    for id in &sorted {
        body.extend_from_slice(id);
    }
    body
}

/// The recovery set ID is the MD5 of the Main packet's body.
pub fn recovery_set_id(main_body: &[u8]) -> [u8; 16] {
    md5(main_body)
}

/// Compute a File ID: the MD5 of the 16k hash, the file length and the name.
pub fn compute_file_id(md5_16k: &[u8; 16], file_length: u64, name: &str) -> [u8; 16] {
    let mut input = Vec::with_capacity(24 + name.len());
    input.extend_from_slice(md5_16k);
    input.extend_from_slice(&file_length.to_le_bytes());
    input.extend_from_slice(name.as_bytes());
    md5(&input)
}

/// Build the body of a File Description packet.
pub fn file_description_body(
    file_id: &[u8; 16],
    md5_full: &[u8; 16],
    md5_16k: &[u8; 16],
    file_length: u64,
    name: &str,
) -> Vec<u8> {
    let mut body = Vec::with_capacity(56 + name.len() + 3);
    body.extend_from_slice(file_id);
    body.extend_from_slice(md5_full);
    body.extend_from_slice(md5_16k);
    body.extend_from_slice(&file_length.to_le_bytes());
    body.extend_from_slice(name.as_bytes());
    pad_to_4(body)
}

/// Build the body of a Unicode Filename packet.
///
/// PAR 2.0 spec, "Unicode Filename packet" (optional packets): the File ID
/// followed by the name in UTF-16, zero-padded to a multiple of 4 bytes and
/// not necessarily NUL-terminated. Encoded little-endian, as ParPar does.
pub fn unicode_filename_body(file_id: &[u8; 16], name: &str) -> Vec<u8> {
    let mut body = Vec::with_capacity(16 + 2 * name.len() + 2);
    body.extend_from_slice(file_id);
    for unit in name.encode_utf16() {
        body.extend_from_slice(&unit.to_le_bytes());
    }
    pad_to_4(body)
}

/// Serialize a file's File Description packet, followed by a Unicode
/// Filename packet when `name` is not ASCII.
///
/// The File Description name is written as UTF-8 either way. Emitting the
/// Unicode packet only for non-ASCII names matches ParPar's default
/// (`--unicode` auto-detection), which emits it right after the FileDesc.
pub fn serialize_file_description(
    recovery_set_id: &[u8; 16],
    file_id: &[u8; 16],
    md5_full: &[u8; 16],
    md5_16k: &[u8; 16],
    file_length: u64,
    name: &str,
) -> Vec<u8> {
    let mut packets = serialize_packet(
        recovery_set_id,
        &TYPE_FILE_DESC,
        &file_description_body(file_id, md5_full, md5_16k, file_length, name),
    );
    if !name.is_ascii() {
        packets.extend(serialize_packet(
            recovery_set_id,
            &TYPE_UNICODE_FILENAME,
            &unicode_filename_body(file_id, name),
        ));
    }
    packets
}

/// Build the body of an Input File Slice Checksum packet.
pub fn ifsc_body(file_id: &[u8; 16], slices: &[SliceChecksum]) -> Vec<u8> {
    let mut body = Vec::with_capacity(16 + 20 * slices.len());
    body.extend_from_slice(file_id);
    for slice in slices {
        body.extend_from_slice(&slice.md5);
        body.extend_from_slice(&slice.crc32.to_le_bytes());
    }
    body
}

/// Build the body of a Recovery Slice packet.
///
/// # Panics
///
/// Panics if `data`'s length is not a multiple of 4.
pub fn recovery_body(exponent: u32, data: &[u8]) -> Vec<u8> {
    assert!(
        data.len().is_multiple_of(4),
        "recovery slice data must be a multiple of 4 bytes"
    );
    let mut body = Vec::with_capacity(4 + data.len());
    body.extend_from_slice(&exponent.to_le_bytes());
    body.extend_from_slice(data);
    body
}

/// Build the body of a Creator packet.
pub fn creator_body(creator: &str) -> Vec<u8> {
    pad_to_4(creator.as_bytes().to_vec())
}

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

    #[test]
    fn md5_known_vectors() {
        assert_eq!(
            md5(b""),
            [
                0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8,
                0x42, 0x7e
            ]
        );
        assert_eq!(
            md5(b"abc"),
            [
                0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1,
                0x7f, 0x72
            ]
        );
    }

    #[test]
    fn all_type_tags_are_16_bytes() {
        for tag in [
            TYPE_MAIN,
            TYPE_FILE_DESC,
            TYPE_IFSC,
            TYPE_RECOVERY,
            TYPE_CREATOR,
            TYPE_UNICODE_FILENAME,
        ] {
            assert_eq!(tag.len(), 16);
            assert!(tag.starts_with(b"PAR 2.0\0"));
        }
    }

    #[test]
    fn serialized_packet_has_correct_header() {
        let rsid = [0x11u8; 16];
        let body = b"abcd1234"; // 8 bytes, multiple of 4
        let packet = serialize_packet(&rsid, &TYPE_CREATOR, body);

        assert_eq!(&packet[0..8], b"PAR2\0PKT");
        assert_eq!(
            u64::from_le_bytes(packet[8..16].try_into().unwrap()),
            (HEADER_LEN + body.len()) as u64
        );
        assert_eq!(&packet[32..48], &rsid);
        assert_eq!(&packet[48..64], &TYPE_CREATOR);
        assert_eq!(&packet[64..], body);
        // The stored hash covers everything from offset 32 onward.
        assert_eq!(&packet[16..32], &md5(&packet[32..]));
        assert!(packet.len().is_multiple_of(4));
    }

    #[test]
    fn main_body_sorts_file_ids_and_recovery_set_id_is_its_md5() {
        let ids = [[0x03u8; 16], [0x01u8; 16], [0x02u8; 16]];
        let body = main_body(768_000, &ids);

        assert_eq!(u64::from_le_bytes(body[0..8].try_into().unwrap()), 768_000);
        assert_eq!(u32::from_le_bytes(body[8..12].try_into().unwrap()), 3);
        // Sorted: 0x01.., 0x02.., 0x03..
        assert_eq!(&body[12..28], &[0x01u8; 16]);
        assert_eq!(&body[28..44], &[0x02u8; 16]);
        assert_eq!(&body[44..60], &[0x03u8; 16]);
        assert_eq!(recovery_set_id(&body), md5(&body));
    }

    #[test]
    fn file_ids_sort_as_little_endian_integers() {
        // Byte order disagrees with numeric order: `low` starts with 0xFF but
        // its most significant (last) byte is 0x00.
        let mut low = [0u8; 16];
        low[0] = 0xFF;
        let mut high = [0u8; 16];
        high[15] = 0x01;
        assert_eq!(cmp_file_id(&low, &high), Ordering::Less);
        assert_eq!(cmp_file_id(&high, &low), Ordering::Greater);
        assert_eq!(cmp_file_id(&low, &low), Ordering::Equal);

        let body = main_body(16_384, &[high, low]);
        assert_eq!(&body[12..28], &low);
        assert_eq!(&body[28..44], &high);
    }

    #[test]
    fn file_id_is_md5_of_hash_length_and_name() {
        let md5_16k = [0xABu8; 16];
        let id = compute_file_id(&md5_16k, 1234, "movie.mkv");

        let mut expected_input = Vec::new();
        expected_input.extend_from_slice(&md5_16k);
        expected_input.extend_from_slice(&1234u64.to_le_bytes());
        expected_input.extend_from_slice(b"movie.mkv");
        assert_eq!(id, md5(&expected_input));
    }

    #[test]
    fn file_description_body_layout_and_padding() {
        let body = file_description_body(&[1; 16], &[2; 16], &[3; 16], 5000, "ab");
        assert_eq!(&body[0..16], &[1u8; 16]);
        assert_eq!(&body[16..32], &[2u8; 16]);
        assert_eq!(&body[32..48], &[3u8; 16]);
        assert_eq!(u64::from_le_bytes(body[48..56].try_into().unwrap()), 5000);
        assert_eq!(&body[56..58], b"ab");
        assert!(body.len().is_multiple_of(4)); // 58 -> padded to 60
        assert_eq!(body.len(), 60);
    }

    #[test]
    fn unicode_filename_body_is_padded_utf16le() {
        // "é" is one UTF-16 unit: 2 bytes, padded to 4.
        let body = unicode_filename_body(&[1; 16], "é");
        assert_eq!(&body[0..16], &[1u8; 16]);
        assert_eq!(&body[16..], &[0xE9, 0x00, 0x00, 0x00]);
        // Two units fill a 4-byte boundary exactly: no NUL terminator.
        let body = unicode_filename_body(&[1; 16], "çã");
        assert_eq!(&body[16..], &[0xE7, 0x00, 0xE3, 0x00]);
        // Astral characters become a surrogate pair.
        let body = unicode_filename_body(&[1; 16], "\u{1F600}");
        assert_eq!(&body[16..], &[0x3D, 0xD8, 0x00, 0xDE]);
    }

    #[test]
    fn file_description_adds_unicode_packet_only_for_non_ascii_names() {
        let ascii = serialize_file_description(&[0; 16], &[1; 16], &[2; 16], &[3; 16], 9, "a.bin");
        assert_eq!(ascii.len(), HEADER_LEN + 56 + 8);
        assert_eq!(&ascii[48..64], &TYPE_FILE_DESC);

        let name = "Ação.bin";
        let both = serialize_file_description(&[0; 16], &[1; 16], &[2; 16], &[3; 16], 9, name);
        let desc_len = u64::from_le_bytes(both[8..16].try_into().unwrap()) as usize;
        assert_eq!(&both[48..64], &TYPE_FILE_DESC);
        assert_eq!(
            &both[HEADER_LEN + 56..HEADER_LEN + 56 + name.len()],
            name.as_bytes(),
            "FileDesc keeps the UTF-8 name"
        );
        let uni = &both[desc_len..];
        assert_eq!(&uni[48..64], &TYPE_UNICODE_FILENAME);
        assert_eq!(&uni[HEADER_LEN..HEADER_LEN + 16], &[1u8; 16]);
        assert_eq!(
            &uni[HEADER_LEN + 16..],
            &unicode_filename_body(&[1; 16], name)[16..]
        );
    }

    #[test]
    fn ifsc_body_packs_20_bytes_per_slice() {
        let slices = [
            SliceChecksum {
                md5: [9; 16],
                crc32: 0x1122_3344,
            },
            SliceChecksum {
                md5: [8; 16],
                crc32: 0xAABB_CCDD,
            },
        ];
        let body = ifsc_body(&[7; 16], &slices);
        assert_eq!(body.len(), 16 + 20 * 2);
        assert_eq!(&body[0..16], &[7u8; 16]);
        assert_eq!(&body[16..32], &[9u8; 16]);
        assert_eq!(
            u32::from_le_bytes(body[32..36].try_into().unwrap()),
            0x1122_3344
        );
    }

    #[test]
    fn recovery_body_prefixes_the_exponent() {
        let data = vec![0u8; 8];
        let body = recovery_body(42, &data);
        assert_eq!(u32::from_le_bytes(body[0..4].try_into().unwrap()), 42);
        assert_eq!(body.len(), 12);
    }

    #[test]
    fn creator_body_is_padded() {
        assert_eq!(creator_body("pesto"), b"pesto\0\0\0");
    }
}